PlatformIO package of the Teensy core framework compatible with GCC 10 & C++20
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

134 lines
3.6KB

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