5#ifndef esp32bledumbdisplay_h
6#define esp32bledumbdisplay_h
17 #error DDBLESerialIO is for ESP32
28#include "dumbdisplay.h"
30#define DD_SERVICE_UUID "6E400001-B5A3-F393-E0A9-E50E24DCCA9E"
31#define DD_CHARACTERISTIC_UUID_RX "6E400002-B5A3-F393-E0A9-E50E24DCCA9E"
32#define DD_CHARACTERISTIC_UUID_TX "6E400003-B5A3-F393-E0A9-E50E24DCCA9E"
34#define DD_RECEIVE_BUFFER_SIZE 256
35#define DD_SEND_BUFFER_SIZE 20
53 DDBLESerialIO(
const String& deviceName,
bool enableSerial =
false,
54 unsigned long serialBaud = DD_SERIAL_BAUD):
56 this->deviceName = deviceName;
57 this->initialized =
false;
62 const char* getWhat() {
66 return pCallbacks->available();
69 return pCallbacks->read();
71 void print(
const String &s) {
72 pCallbacks->print(s.c_str());
74 void print(
const char *p) {
77 void write(uint8_t b) {
80 bool preConnect(
bool firstCall) {
82 if (!setupForSerial) {
86 DDInputOutput::preConnect(firstCall);
90 if (!willUseSerial()) {
92 if (!Serial) Serial.begin(DD_SERIAL_BAUD);
94 Serial.println(
"ble address: " + address);
103 if (pServerCallbacks->isDisconnected()) {
104 pServerCallbacks->restartAdvertising();
111 void validConnection() {
112 if (pServerCallbacks->isDisconnected()) {
114 pServerCallbacks->restartAdvertising();
116 Serial.println(
"... connection lost ... waiting for re-connection ...");
120 bool canConnectPassive() {
123 bool canUseBuffer() {
127 class ServerCallbacks:
public BLEServerCallbacks {
129 ServerCallbacks(BLEServer* pServer) {
130 this->pServer = pServer;
131 this->connectionState = 0;
134 void restartAdvertising() {
135 pServer->startAdvertising();
138 Serial.println(
"restarted advertising");
141 bool isDisconnected() {
return connectionState == -1; }
142 bool isConnected() {
return connectionState == 1; }
143 bool isConnecting() {
return connectionState == 0; }
145 void onConnect(BLEServer* pServer) {
148 Serial.println(
"BLE connected");
151 void onDisconnect(BLEServer* pServer) {
152 connectionState = -1;
154 Serial.println(
"BLE disconnected");
161 class Callbacks:
public BLECharacteristicCallbacks {
163 Callbacks(BLECharacteristic *pTx, ServerCallbacks* pServerCallbacks) {
165 this->pServerCallbacks = pServerCallbacks;
166 this->buffering =
false;
167 this->bufferStart = 0;
169#ifdef DD_SEND_BUFFER_SIZE
170 this->sendBufferSize = 0;
174 void print(std::string s) {
175#ifdef DD_SEND_BUFFER_SIZE
176 int len = s.length();
177 if ((len + sendBufferSize) > DD_SEND_BUFFER_SIZE) {
179 if (len > DD_SEND_BUFFER_SIZE) {
184 for (
int i = 0; i < len; i++) {
185 sendBuffer[sendBufferSize++] = s[i];
187 if (sendBufferSize > 0 && sendBuffer[sendBufferSize - 1] ==
'\n') {
194 void write(uint8_t b) {
206 return bufferStart != bufferEnd;
214 char c = buffer[bufferStart];
215 if (++bufferStart >= DD_RECEIVE_BUFFER_SIZE) {
224#ifdef DD_SEND_BUFFER_SIZE
225 void _flushSendBuffer() {
226 if (sendBufferSize > 0) {
227 sendBuffer[sendBufferSize] = 0;
233 void _print(std::string s) {
234#if defined(DD_DEBUG_BLE_SEND)
235 if (pServerCallbacks->isConnected()) {
236 }
else if (pServerCallbacks->isConnecting()) {
237 Serial.print(
"[connecting] ");
239 Serial.print(
"[not connected] ");
241 Serial.print(
"BLE sent ... [");
242 Serial.print(s.c_str());
245 int len = s.length();
256 pTx->setValue(s.substr(0, 20));
268 void onWrite(BLECharacteristic *pCharacteristic) {
270 std::string rxValue = pCharacteristic->getValue();
271 int count = rxValue.size();
272 for (
int i = 0; i < count; i++) {
273 int nextBufferEnd = bufferEnd + 1;
274 if (nextBufferEnd >= DD_RECEIVE_BUFFER_SIZE) {
277 if (nextBufferEnd == bufferStart) {
280 buffer[bufferEnd] = rxValue[i];
281 bufferEnd = nextBufferEnd;
283#ifdef DD_DEBUG_BLE_RECEIVE
284 Serial.print(
"BLE received ... ");
286 Serial.print(
" ... [");
287 Serial.print(rxValue.c_str());
288 Serial.print(
"] ==> ");
289 Serial.print(bufferStart);
291 Serial.print(bufferEnd);
293 Serial.print(DD_RECEIVE_BUFFER_SIZE);
299 BLECharacteristic *pTx;
300 ServerCallbacks* pServerCallbacks;
301 volatile bool buffering;
304 char buffer[DD_RECEIVE_BUFFER_SIZE];
305#ifdef DD_SEND_BUFFER_SIZE
306 uint8_t sendBufferSize;
307 char sendBuffer[DD_SEND_BUFFER_SIZE + 1];
313 Serial.println(
"initialized BLE ...");
315 BLEDevice::init(deviceName.c_str());
316 BLEServer *pServer = BLEDevice::createServer();
317 pServerCallbacks =
new ServerCallbacks(pServer);
318 pServer->setCallbacks(pServerCallbacks);
319 BLEService *pService = pServer->createService(DD_SERVICE_UUID);
320 BLECharacteristic *pTx = pService->createCharacteristic(
321 DD_CHARACTERISTIC_UUID_TX,
323 BLECharacteristic::PROPERTY_NOTIFY
325 BLECharacteristic::PROPERTY_INDICATE
328 pTx->addDescriptor(
new BLE2902());
329 BLECharacteristic *pRx = pService->createCharacteristic(
330 DD_CHARACTERISTIC_UUID_RX,
331 BLECharacteristic::PROPERTY_WRITE
333 pCallbacks =
new Callbacks(pTx, pServerCallbacks);
334 pRx->setCallbacks(pCallbacks);
336 pServer->getAdvertising()->start();
338 address = BLEDevice::getAddress().toString().c_str();
339 address.toUpperCase();
342 Serial.println(
"... done initialized BLE ... waiting for connection ...");
349 ServerCallbacks* pServerCallbacks;
350 Callbacks* pCallbacks;
Subclass of DDInputOutput.
Definition: esp32bledumbdisplay.h:47