| 1 | #include "BLESerial.h" |
| 2 | |
| 3 | // #define BLE_SERIAL_DEBUG |
| 4 | |
| 5 | BLESerial *BLESerial::_instance = NULL; |
| 6 | |
| 7 | BLESerial::BLESerial(unsigned char req, unsigned char rdy, unsigned char rst) |
| 8 | : BLEPeripheral(req, rdy, rst) { |
| 9 | this->_txCount = 0; |
| 10 | this->_rxHead = this->_rxTail = 0; |
| 11 | this->_flushed = 0; |
| 12 | BLESerial::_instance = this; |
| 13 | |
| 14 | addAttribute(this->_uartService); |
| 15 | addAttribute(this->_uartNameDescriptor); |
| 16 | setAdvertisedServiceUuid(this->_uartService.uuid()); |
| 17 | addAttribute(this->_rxCharacteristic); |
| 18 | addAttribute(this->_rxNameDescriptor); |
| 19 | this->_rxCharacteristic.setEventHandler(BLEWritten, BLESerial::_received); |
| 20 | addAttribute(this->_txCharacteristic); |
| 21 | addAttribute(this->_txNameDescriptor); |
| 22 | } |
| 23 | |
| 24 | void BLESerial::begin(...) { |
| 25 | BLEPeripheral::begin(); |
| 26 | #ifdef BLE_SERIAL_DEBUG |
| 27 | Serial.println(F("BLESerial::begin()")); |
| 28 | #endif |
| 29 | } |
| 30 | |
| 31 | void BLESerial::poll() { |
| 32 | if (millis() < this->_flushed + 100) { |
| 33 | BLEPeripheral::poll(); |
| 34 | } else { |
| 35 | flush(); |
| 36 | } |
| 37 | } |
| 38 | |
| 39 | void BLESerial::end() { |
| 40 | this->_rxCharacteristic.setEventHandler(BLEWritten, NULL); |
| 41 | this->_rxHead = this->_rxTail = 0; |
| 42 | flush(); |
| 43 | BLEPeripheral::disconnect(); |
| 44 | } |
| 45 | |
| 46 | int BLESerial::available(void) { |
| 47 | BLEPeripheral::poll(); |
| 48 | int retval = (this->_rxHead - this->_rxTail + sizeof(this->_rxBuffer)) % |
| 49 | sizeof(this->_rxBuffer); |
| 50 | #ifdef BLE_SERIAL_DEBUG |
| 51 | Serial.print(F("BLESerial::available() = ")); |
| 52 | Serial.println(retval); |
| 53 | #endif |
| 54 | return retval; |
| 55 | } |
| 56 | |
| 57 | int BLESerial::peek(void) { |
| 58 | BLEPeripheral::poll(); |
| 59 | if (this->_rxTail == this->_rxHead) |
| 60 | return -1; |
| 61 | uint8_t byte = this->_rxBuffer[this->_rxTail]; |
| 62 | #ifdef BLE_SERIAL_DEBUG |
| 63 | Serial.print(F("BLESerial::peek() = ")); |
| 64 | Serial.print((char)byte); |
| 65 | Serial.print(F(" 0x")); |
| 66 | Serial.println(byte, HEX); |
| 67 | #endif |
| 68 | return byte; |
| 69 | } |
| 70 | |
| 71 | int BLESerial::read(void) { |
| 72 | BLEPeripheral::poll(); |
| 73 | if (this->_rxTail == this->_rxHead) |
| 74 | return -1; |
| 75 | this->_rxTail = (this->_rxTail + 1) % sizeof(this->_rxBuffer); |
| 76 | uint8_t byte = this->_rxBuffer[this->_rxTail]; |
| 77 | #ifdef BLE_SERIAL_DEBUG |
| 78 | Serial.print(F("BLESerial::read() = ")); |
| 79 | Serial.print((char)byte); |
| 80 | Serial.print(F(" 0x")); |
| 81 | Serial.println(byte, HEX); |
| 82 | #endif |
| 83 | return byte; |
| 84 | } |
| 85 | |
| 86 | void BLESerial::flush(void) { |
| 87 | if (this->_txCount == 0) |
| 88 | return; |
| 89 | this->_txCharacteristic.setValue(this->_txBuffer, this->_txCount); |
| 90 | this->_flushed = millis(); |
| 91 | this->_txCount = 0; |
| 92 | BLEPeripheral::poll(); |
| 93 | #ifdef BLE_SERIAL_DEBUG |
| 94 | Serial.println(F("BLESerial::flush()")); |
| 95 | #endif |
| 96 | } |
| 97 | |
| 98 | size_t BLESerial::write(uint8_t byte) { |
| 99 | BLEPeripheral::poll(); |
| 100 | if (this->_txCharacteristic.subscribed() == false) |
| 101 | return 0; |
| 102 | this->_txBuffer[this->_txCount++] = byte; |
| 103 | if (this->_txCount == sizeof(this->_txBuffer)) |
| 104 | flush(); |
| 105 | #ifdef BLE_SERIAL_DEBUG |
| 106 | Serial.print(F("BLESerial::write(")); |
| 107 | Serial.print((char)byte); |
| 108 | Serial.print(F(" 0x")); |
| 109 | Serial.print(byte, HEX); |
| 110 | Serial.println(F(") = 1")); |
| 111 | #endif |
| 112 | return 1; |
| 113 | } |
| 114 | |
| 115 | BLESerial::operator bool() { |
| 116 | bool retval = BLEPeripheral::connected(); |
| 117 | #ifdef BLE_SERIAL_DEBUG |
| 118 | Serial.print(F("BLESerial::operator bool() = ")); |
| 119 | Serial.println(retval); |
| 120 | #endif |
| 121 | return retval; |
| 122 | } |
| 123 | |
| 124 | void BLESerial::_received(const uint8_t *data, size_t size) { |
| 125 | for (int i = 0; i < size; i++) { |
| 126 | this->_rxHead = (this->_rxHead + 1) % sizeof(this->_rxBuffer); |
| 127 | this->_rxBuffer[this->_rxHead] = data[i]; |
| 128 | } |
| 129 | #ifdef BLE_SERIAL_DEBUG |
| 130 | Serial.print(F("BLESerial::received(")); |
| 131 | for (int i = 0; i < size; i++) |
| 132 | Serial.print((char)data[i]); |
| 133 | Serial.println(F(")")); |
| 134 | #endif |
| 135 | } |
| 136 | |
| 137 | void BLESerial::_received(BLECentral & /*central*/, |
| 138 | BLECharacteristic &rxCharacteristic) { |
| 139 | BLESerial::_instance->_received(rxCharacteristic.value(), |
| 140 | rxCharacteristic.valueLength()); |
| 141 | } |