-
Notifications
You must be signed in to change notification settings - Fork 10
/
bt_serial.cpp
117 lines (107 loc) · 3.1 KB
/
bt_serial.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
#include "setup.h"
BluetoothSerial SerialBT;
/** Max number of messages in buffer */
#define sppMsgBufferSize 10
/** Bluetooth Serial device name */
String btSppName = "ESP32 Weather";
void btSppTask(void *pvParameters);
/** Structure for Bluetooth Serial message Buffer */
struct btSppMsgStruct {
String payload = "";
};
/** Queued messages for Bluetooth Serial */
btSppMsgStruct btSppMsg[sppMsgBufferSize];
/** Task handle for the Bluetooth Serial sender task */
TaskHandle_t btSppTaskHandle = NULL;
bool initBtSerial() {
if (!SerialBT.begin(apName)) {
sendDebug(debugLabel, errorLabel + digitalTimeDisplaySec() + " Failed to start BTSerial", false);
return false;
}
// Clear message structure
for (int index = 0; index < sppMsgBufferSize; index++) {
btSppMsg[index].payload = "";
}
// Start task for MEEO publishing
xTaskCreatePinnedToCore(
btSppTask, /* Function to implement the task */
"BtSpp ", /* Name of the task */
2000, /* Stack size in words */
NULL, /* Task input parameter */
5, /* Priority of the task */
&btSppTaskHandle, /* Task handle. */
1); /* Core where the task should run */
return true;
}
void stopBtSerial() {
// Stop Bluetooth Serial sending task
vTaskDelete(btSppTaskHandle);
// Stop Bluetooth Serial
SerialBT.end();
}
bool sendBtSerial(String message) {
bool queueResults = false;
// Check if we have a client
if (SerialBT.hasClient()) {
for (byte index = 0; index < sppMsgBufferSize; index ++) {
if (btSppMsg[index].payload.length() == 0) { // found an empty slot?
btSppMsg[index].payload = message;
queueResults = true;
break;
}
}
if (tasksEnabled) {
vTaskResume(btSppTaskHandle);
}
}
return queueResults;
}
/**
* Task to send data from btSppMsg buffer over Bluetooth Serial
* @param pvParameters
* pointer to task parameters
*/
void btSppTask(void *pvParameters) {
// Serial.println("btSppTask loop started");
while (1) // btSppTask loop
{
if (otaRunning) {
vTaskDelete(NULL);
}
for (byte index = 0; index < sppMsgBufferSize; index ++) {
if (btSppMsg[index].payload.length() != 0) {
// Check if we have a client
if (SerialBT.hasClient()) {
// Try to send the message
// int result = SerialBT.println(btSppMsg[index].payload);
int result = SerialBT.write(btSppMsg[index].payload);
if (result == -1) {
// Sending failed, try to reinitialize the Bluetooth Serial
SerialBT.end();
delay(1000);
SerialBT.begin(btSppName);
// Retry to send the message
// SerialBT.println(btSppMsg[index].payload);
int result = SerialBT.write(btSppMsg[index].payload);
}
SerialBT.write("\n");
// Flush the send buffer
SerialBT.flush();
delay(250);
// Discard the message
btSppMsg[index].payload = "";
}
}
}
bool queueIsEmpty = true;
for (byte index = 0; index < sppMsgBufferSize; index ++) {
if (btSppMsg[index].payload.length() != 0) {
queueIsEmpty = false;
}
}
if (queueIsEmpty) {
vTaskSuspend(NULL);
}
delay(500);
}
}