|
4 | 4 | Based on BLESerial.cpp by Voita Molda
|
5 | 5 | https://github.com/sandeepmistry/arduino-BLEPeripheral/blob/master/examples/serial/BLESerial.h
|
6 | 6 |
|
7 |
| - Last updated March 13th, 2016 |
| 7 | + Last updated April 04th, 2016 |
8 | 8 | */
|
9 | 9 |
|
10 | 10 | #ifndef _BLE_STREAM_H_
|
|
22 | 22 | #define BLESTREAM_TXBUFFER_FLUSH_INTERVAL 80
|
23 | 23 | #define BLESTREAM_MIN_FLUSH_INTERVAL 8 // minimum interval for flushing the TX buffer
|
24 | 24 |
|
| 25 | +// #define BLE_SERIAL_DEBUG |
| 26 | + |
25 | 27 | class BLEStream : public BLEPeripheral, public Stream
|
26 | 28 | {
|
27 | 29 | public:
|
@@ -64,4 +66,178 @@ class BLEStream : public BLEPeripheral, public Stream
|
64 | 66 | static void _received(BLECentral& /*central*/, BLECharacteristic& rxCharacteristic);
|
65 | 67 | };
|
66 | 68 |
|
| 69 | + |
| 70 | +/* |
| 71 | + * BLEStream.cpp |
| 72 | + * Copied here as a hack to avoid having to install the BLEPeripheral libarary even if it's |
| 73 | + * not needed. |
| 74 | + */ |
| 75 | + |
| 76 | +BLEStream* BLEStream::_instance = NULL; |
| 77 | + |
| 78 | +BLEStream::BLEStream(unsigned char req, unsigned char rdy, unsigned char rst) : |
| 79 | +#if defined(_VARIANT_ARDUINO_101_X_) |
| 80 | + BLEPeripheral() |
| 81 | +#else |
| 82 | + BLEPeripheral(req, rdy, rst) |
| 83 | +#endif |
| 84 | +{ |
| 85 | + this->_txCount = 0; |
| 86 | + this->_rxHead = this->_rxTail = 0; |
| 87 | + this->_flushed = 0; |
| 88 | + this->_flushInterval = BLESTREAM_TXBUFFER_FLUSH_INTERVAL; |
| 89 | + BLEStream::_instance = this; |
| 90 | + |
| 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); |
| 99 | +} |
| 100 | + |
| 101 | +void BLEStream::begin(...) |
| 102 | +{ |
| 103 | + BLEPeripheral::begin(); |
| 104 | +#ifdef BLE_SERIAL_DEBUG |
| 105 | + Serial.println(F("BLEStream::begin()")); |
| 106 | +#endif |
| 107 | +} |
| 108 | + |
| 109 | +bool BLEStream::poll() |
| 110 | +{ |
| 111 | + // BLEPeripheral::poll is called each time connected() is called |
| 112 | + this->_connected = BLEPeripheral::connected(); |
| 113 | + if (millis() > this->_flushed + this->_flushInterval) { |
| 114 | + flush(); |
| 115 | + } |
| 116 | + return this->_connected; |
| 117 | +} |
| 118 | + |
| 119 | +void BLEStream::end() |
| 120 | +{ |
| 121 | + this->_rxCharacteristic.setEventHandler(BLEWritten, NULL); |
| 122 | + this->_rxHead = this->_rxTail = 0; |
| 123 | + flush(); |
| 124 | + BLEPeripheral::disconnect(); |
| 125 | +} |
| 126 | + |
| 127 | +int BLEStream::available(void) |
| 128 | +{ |
| 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(); |
| 134 | +#endif |
| 135 | + int retval = (this->_rxHead - this->_rxTail + sizeof(this->_rxBuffer)) % sizeof(this->_rxBuffer); |
| 136 | +#ifdef BLE_SERIAL_DEBUG |
| 137 | + if (retval > 0) { |
| 138 | + Serial.print(F("BLEStream::available() = ")); |
| 139 | + Serial.println(retval); |
| 140 | + } |
| 141 | +#endif |
| 142 | + return retval; |
| 143 | +} |
| 144 | + |
| 145 | +int BLEStream::peek(void) |
| 146 | +{ |
| 147 | +#ifndef _VARIANT_ARDUINO_101_X_ |
| 148 | + BLEPeripheral::poll(); |
| 149 | +#endif |
| 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); |
67 | 155 | #endif
|
| 156 | + return byte; |
| 157 | +} |
| 158 | + |
| 159 | +int BLEStream::read(void) |
| 160 | +{ |
| 161 | +#ifndef _VARIANT_ARDUINO_101_X_ |
| 162 | + BLEPeripheral::poll(); |
| 163 | +#endif |
| 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); |
| 170 | +#endif |
| 171 | + return byte; |
| 172 | +} |
| 173 | + |
| 174 | +void BLEStream::flush(void) |
| 175 | +{ |
| 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(); |
| 181 | + } |
| 182 | +#endif |
| 183 | + this->_txCharacteristic.setValue(this->_txBuffer, this->_txCount); |
| 184 | + this->_flushed = millis(); |
| 185 | + this->_txCount = 0; |
| 186 | +#ifdef BLE_SERIAL_DEBUG |
| 187 | + Serial.println(F("BLEStream::flush()")); |
| 188 | +#endif |
| 189 | +} |
| 190 | + |
| 191 | +size_t BLEStream::write(uint8_t byte) |
| 192 | +{ |
| 193 | +#ifndef _VARIANT_ARDUINO_101_X_ |
| 194 | + BLEPeripheral::poll(); |
| 195 | +#endif |
| 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")); |
| 203 | +#endif |
| 204 | + return 1; |
| 205 | +} |
| 206 | + |
| 207 | +BLEStream::operator bool() |
| 208 | +{ |
| 209 | + bool retval = this->_connected = BLEPeripheral::connected(); |
| 210 | +#ifdef BLE_SERIAL_DEBUG |
| 211 | + Serial.print(F("BLEStream::operator bool() = ")); |
| 212 | + Serial.println(retval); |
| 213 | +#endif |
| 214 | + return retval; |
| 215 | +} |
| 216 | + |
| 217 | +void BLEStream::setFlushInterval(int interval) |
| 218 | +{ |
| 219 | + if (interval > BLESTREAM_MIN_FLUSH_INTERVAL) { |
| 220 | + this->_flushInterval = interval; |
| 221 | + } |
| 222 | +} |
| 223 | + |
| 224 | +void BLEStream::_received(const unsigned char* data, size_t size) |
| 225 | +{ |
| 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]; |
| 229 | + } |
| 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(")")); |
| 234 | +#endif |
| 235 | +} |
| 236 | + |
| 237 | +void BLEStream::_received(BLECentral& /*central*/, BLECharacteristic& rxCharacteristic) |
| 238 | +{ |
| 239 | + BLEStream::_instance->_received(rxCharacteristic.value(), rxCharacteristic.valueLength()); |
| 240 | +} |
| 241 | + |
| 242 | + |
| 243 | +#endif // _BLE_STREAM_H_ |
0 commit comments