4 Based on BLESerial.cpp by Voita Molda
5 https://github.com/sandeepmistry/arduino-BLEPeripheral/blob/master/examples/serial/BLESerial.h
7 Last updated April 4th, 2016
10 #ifndef _BLE_STREAM_H_
11 #define _BLE_STREAM_H_
14 #if defined(_VARIANT_ARDUINO_101_X_)
16 #define _MAX_ATTR_DATA_LEN_ BLE_MAX_ATTR_DATA_LEN
18 #include <BLEPeripheral.h>
19 #define _MAX_ATTR_DATA_LEN_ BLE_ATTRIBUTE_MAX_VALUE_LENGTH
22 #define BLESTREAM_TXBUFFER_FLUSH_INTERVAL 80
23 #define BLESTREAM_MIN_FLUSH_INTERVAL 8 // minimum interval for flushing the TX buffer
25 // #define BLE_SERIAL_DEBUG
27 class BLEStream : public BLEPeripheral, public Stream
30 BLEStream(unsigned char req = 0, unsigned char rdy = 0, unsigned char rst = 0);
35 void setFlushInterval(int);
37 virtual int available(void);
38 virtual int peek(void);
39 virtual int read(void);
40 virtual void flush(void);
41 virtual size_t write(uint8_t byte);
43 virtual operator bool();
47 unsigned long _flushed;
49 static BLEStream* _instance;
53 size_t _rxCount() const;
54 unsigned char _rxBuffer[256];
56 unsigned char _txBuffer[_MAX_ATTR_DATA_LEN_];
58 BLEService _uartService = BLEService("6E400001-B5A3-F393-E0A9-E50E24DCCA9E");
59 BLEDescriptor _uartNameDescriptor = BLEDescriptor("2901", "UART");
60 BLECharacteristic _rxCharacteristic = BLECharacteristic("6E400002-B5A3-F393-E0A9-E50E24DCCA9E", BLEWriteWithoutResponse, _MAX_ATTR_DATA_LEN_);
61 BLEDescriptor _rxNameDescriptor = BLEDescriptor("2901", "RX - Receive Data (Write)");
62 BLECharacteristic _txCharacteristic = BLECharacteristic("6E400003-B5A3-F393-E0A9-E50E24DCCA9E", BLENotify, _MAX_ATTR_DATA_LEN_);
63 BLEDescriptor _txNameDescriptor = BLEDescriptor("2901", "TX - Transfer Data (Notify)");
65 void _received(const unsigned char* data, size_t size);
66 static void _received(BLECentral& /*central*/, BLECharacteristic& rxCharacteristic);
72 * Copied here as a hack to avoid having to install the BLEPeripheral libarary even if it's
76 BLEStream* BLEStream::_instance = NULL;
78 BLEStream::BLEStream(unsigned char req, unsigned char rdy, unsigned char rst) :
79 #if defined(_VARIANT_ARDUINO_101_X_)
82 BLEPeripheral(req, rdy, rst)
86 this->_rxHead = this->_rxTail = 0;
88 this->_flushInterval = BLESTREAM_TXBUFFER_FLUSH_INTERVAL;
89 BLEStream::_instance = this;
91 addAttribute(this->_uartService);
92 addAttribute(this->_uartNameDescriptor);
93 setAdvertisedServiceUuid(this->_uartService.uuid());
94 addAttribute(this->_rxCharacteristic);
95 addAttribute(this->_rxNameDescriptor);
96 this->_rxCharacteristic.setEventHandler(BLEWritten, BLEStream::_received);
97 addAttribute(this->_txCharacteristic);
98 addAttribute(this->_txNameDescriptor);
101 void BLEStream::begin(...)
103 BLEPeripheral::begin();
104 #ifdef BLE_SERIAL_DEBUG
105 Serial.println(F("BLEStream::begin()"));
109 bool BLEStream::poll()
111 // BLEPeripheral::poll is called each time connected() is called
112 this->_connected = BLEPeripheral::connected();
113 if (millis() > this->_flushed + this->_flushInterval) {
116 return this->_connected;
119 void BLEStream::end()
121 this->_rxCharacteristic.setEventHandler(BLEWritten, (void(*)(BLECentral&, BLECharacteristic&))NULL);
122 this->_rxHead = this->_rxTail = 0;
124 BLEPeripheral::disconnect();
127 int BLEStream::available(void)
129 // BLEPeripheral::poll only calls delay(1) in CurieBLE so skipping it here to avoid the delay
130 #ifndef _VARIANT_ARDUINO_101_X_
131 // TODO Need to do more testing to determine if all of these calls to BLEPeripheral::poll are
132 // actually necessary. Seems to run fine without them, but only minimal testing so far.
133 BLEPeripheral::poll();
135 int retval = (this->_rxHead - this->_rxTail + sizeof(this->_rxBuffer)) % sizeof(this->_rxBuffer);
136 #ifdef BLE_SERIAL_DEBUG
138 Serial.print(F("BLEStream::available() = "));
139 Serial.println(retval);
145 int BLEStream::peek(void)
147 #ifndef _VARIANT_ARDUINO_101_X_
148 BLEPeripheral::poll();
150 if (this->_rxTail == this->_rxHead) return -1;
151 uint8_t byte = this->_rxBuffer[this->_rxTail];
152 #ifdef BLE_SERIAL_DEBUG
153 Serial.print(F("BLEStream::peek() = 0x"));
154 Serial.println(byte, HEX);
159 int BLEStream::read(void)
161 #ifndef _VARIANT_ARDUINO_101_X_
162 BLEPeripheral::poll();
164 if (this->_rxTail == this->_rxHead) return -1;
165 this->_rxTail = (this->_rxTail + 1) % sizeof(this->_rxBuffer);
166 uint8_t byte = this->_rxBuffer[this->_rxTail];
167 #ifdef BLE_SERIAL_DEBUG
168 Serial.print(F("BLEStream::read() = 0x"));
169 Serial.println(byte, HEX);
174 void BLEStream::flush(void)
176 if (this->_txCount == 0) return;
177 #ifndef _VARIANT_ARDUINO_101_X_
178 // ensure there are available packets before sending
179 while(!this->_txCharacteristic.canNotify()) {
180 BLEPeripheral::poll();
183 this->_txCharacteristic.setValue(this->_txBuffer, this->_txCount);
184 this->_flushed = millis();
186 #ifdef BLE_SERIAL_DEBUG
187 Serial.println(F("BLEStream::flush()"));
191 size_t BLEStream::write(uint8_t byte)
193 #ifndef _VARIANT_ARDUINO_101_X_
194 BLEPeripheral::poll();
196 if (this->_txCharacteristic.subscribed() == false) return 0;
197 this->_txBuffer[this->_txCount++] = byte;
198 if (this->_txCount == sizeof(this->_txBuffer)) flush();
199 #ifdef BLE_SERIAL_DEBUG
200 Serial.print(F("BLEStream::write( 0x"));
201 Serial.print(byte, HEX);
202 Serial.println(F(") = 1"));
207 BLEStream::operator bool()
209 bool retval = this->_connected = BLEPeripheral::connected();
210 #ifdef BLE_SERIAL_DEBUG
211 Serial.print(F("BLEStream::operator bool() = "));
212 Serial.println(retval);
217 void BLEStream::setFlushInterval(int interval)
219 if (interval > BLESTREAM_MIN_FLUSH_INTERVAL) {
220 this->_flushInterval = interval;
224 void BLEStream::_received(const unsigned char* data, size_t size)
226 for (size_t i = 0; i < size; i++) {
227 this->_rxHead = (this->_rxHead + 1) % sizeof(this->_rxBuffer);
228 this->_rxBuffer[this->_rxHead] = data[i];
230 #ifdef BLE_SERIAL_DEBUG
231 Serial.print(F("BLEStream::received("));
232 for (int i = 0; i < size; i++) Serial.print(data[i], HEX);
233 Serial.println(F(")"));
237 void BLEStream::_received(BLECentral& /*central*/, BLECharacteristic& rxCharacteristic)
239 BLEStream::_instance->_received(rxCharacteristic.value(), rxCharacteristic.valueLength());
243 #endif // _BLE_STREAM_H_