Skip to content

Commit 727f691

Browse files
committed
Handle all BT radio settings
1 parent ddcfe1a commit 727f691

File tree

1 file changed

+95
-47
lines changed

1 file changed

+95
-47
lines changed

Firmware/RTK_Everywhere/Bluetooth.ino

Lines changed: 95 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@
3030
static volatile BTState bluetoothState = BT_OFF;
3131

3232
#ifdef COMPILE_BT
33-
BTSerialInterface *bluetoothSerial;
3433
BTSerialInterface *bluetoothSerialSpp;
3534
BTSerialInterface *bluetoothSerialBle;
3635

@@ -46,7 +45,7 @@ void bluetoothCallback(esp_spp_cb_event_t event, esp_spp_cb_param_t *param)
4645
{
4746
systemPrintln("BT client Connected");
4847
bluetoothState = BT_CONNECTED;
49-
//LED is controlled by tickerBluetoothLedUpdate()
48+
// LED is controlled by tickerBluetoothLedUpdate()
5049
}
5150

5251
if (event == ESP_SPP_CLOSE_EVT)
@@ -95,8 +94,10 @@ int bluetoothRead(uint8_t *buffer, int length)
9594

9695
return (bytesRead);
9796
}
98-
else
99-
return bluetoothSerial->readBytes(buffer, length);
97+
else if (settings.bluetoothRadioType == BLUETOOTH_RADIO_SPP)
98+
return bluetoothSerialSpp->readBytes(buffer, length);
99+
else if (settings.bluetoothRadioType == BLUETOOTH_RADIO_BLE)
100+
return bluetoothSerialBle->readBytes(buffer, length);
100101

101102
return 0;
102103

@@ -117,8 +118,12 @@ uint8_t bluetoothRead()
117118

118119
return (bluetoothSerialSpp->read());
119120
}
120-
else
121-
return bluetoothSerial->read();
121+
else if (settings.bluetoothRadioType == BLUETOOTH_RADIO_SPP)
122+
return bluetoothSerialSpp->read();
123+
else if (settings.bluetoothRadioType == BLUETOOTH_RADIO_BLE)
124+
return bluetoothSerialBle->read();
125+
126+
return 0;
122127
#else // COMPILE_BT
123128
return 0;
124129
#endif // COMPILE_BT
@@ -136,8 +141,12 @@ bool bluetoothRxDataAvailable()
136141

137142
return (bluetoothSerialSpp->available());
138143
}
139-
else
140-
return bluetoothSerial->available();
144+
else if (settings.bluetoothRadioType == BLUETOOTH_RADIO_SPP)
145+
return bluetoothSerialSpp->available();
146+
else if (settings.bluetoothRadioType == BLUETOOTH_RADIO_BLE)
147+
return bluetoothSerialBle->available();
148+
149+
return (0);
141150
#else // COMPILE_BT
142151
return false;
143152
#endif // COMPILE_BT
@@ -147,8 +156,6 @@ bool bluetoothRxDataAvailable()
147156
int bluetoothWrite(const uint8_t *buffer, int length)
148157
{
149158
#ifdef COMPILE_BT
150-
// BLE write does not handle 0 length requests correctly
151-
152159
if (settings.bluetoothRadioType == BLUETOOTH_RADIO_SPP_AND_BLE)
153160
{
154161
// Write to both interfaces
@@ -162,13 +169,19 @@ int bluetoothWrite(const uint8_t *buffer, int length)
162169
return (bleWrite);
163170
return (sppWrite);
164171
}
165-
else
172+
else if (settings.bluetoothRadioType == BLUETOOTH_RADIO_SPP)
173+
{
174+
return bluetoothSerialSpp->write(buffer, length);
175+
}
176+
else if (settings.bluetoothRadioType == BLUETOOTH_RADIO_BLE)
166177
{
178+
// BLE write does not handle 0 length requests correctly
167179
if (length > 0)
168-
return bluetoothSerial->write(buffer, length);
169-
else
170-
return 0;
180+
return bluetoothSerialBle->write(buffer, length);
181+
return 0;
171182
}
183+
184+
return (0);
172185
#else // COMPILE_BT
173186
return 0;
174187
#endif // COMPILE_BT
@@ -191,10 +204,16 @@ int bluetoothWrite(uint8_t value)
191204
return (bleWrite);
192205
return (sppWrite);
193206
}
194-
else
207+
else if (settings.bluetoothRadioType == BLUETOOTH_RADIO_SPP)
195208
{
196-
return bluetoothSerial->write(value);
209+
return bluetoothSerialSpp->write(value);
197210
}
211+
else if (settings.bluetoothRadioType == BLUETOOTH_RADIO_BLE)
212+
{
213+
return bluetoothSerialBle->write(value);
214+
}
215+
216+
return (0);
198217
#else // COMPILE_BT
199218
return 0;
200219
#endif // COMPILE_BT
@@ -209,8 +228,10 @@ void bluetoothFlush()
209228
bluetoothSerialBle->flush();
210229
bluetoothSerialSpp->flush();
211230
}
212-
else
213-
bluetoothSerial->flush();
231+
else if (settings.bluetoothRadioType == BLUETOOTH_RADIO_SPP)
232+
bluetoothSerialSpp->flush();
233+
else if (settings.bluetoothRadioType == BLUETOOTH_RADIO_BLE)
234+
bluetoothSerialBle->flush();
214235
#else // COMPILE_BT
215236
return;
216237
#endif // COMPILE_BT
@@ -221,6 +242,9 @@ void bluetoothFlush()
221242
// This allows multiple units to be on at same time
222243
void bluetoothStart()
223244
{
245+
if (settings.bluetoothRadioType == BLUETOOTH_RADIO_OFF)
246+
return;
247+
224248
#ifdef COMPILE_BT
225249
if (!online.bluetooth)
226250
{
@@ -269,9 +293,9 @@ void bluetoothStart()
269293
bluetoothSerialBle = new BTLESerial();
270294
}
271295
else if (settings.bluetoothRadioType == BLUETOOTH_RADIO_SPP)
272-
bluetoothSerial = new BTClassicSerial();
296+
bluetoothSerialSpp = new BTClassicSerial();
273297
else if (settings.bluetoothRadioType == BLUETOOTH_RADIO_BLE)
274-
bluetoothSerial = new BTLESerial();
298+
bluetoothSerialBle = new BTLESerial();
275299

276300
// Not yet implemented
277301
// if (pinBluetoothTaskHandle == nullptr)
@@ -290,18 +314,26 @@ void bluetoothStart()
290314
bool beginSuccess = true;
291315
if (settings.bluetoothRadioType == BLUETOOTH_RADIO_SPP_AND_BLE)
292316
{
293-
beginSuccess &=
294-
bluetoothSerialBle->begin(deviceName, false, false, settings.sppRxQueueSize,
295-
settings.sppTxQueueSize); // localName, isMaster, disableBLE, rxBufferSize, txBufferSize
296-
beginSuccess &=
297-
bluetoothSerialSpp->begin(deviceName, false, false, settings.sppRxQueueSize,
298-
settings.sppTxQueueSize); // localName, isMaster, disableBLE, rxBufferSize, txBufferSize
317+
beginSuccess &= bluetoothSerialBle->begin(
318+
deviceName, false, false, settings.sppRxQueueSize,
319+
settings.sppTxQueueSize); // localName, isMaster, disableBLE, rxBufferSize, txBufferSize
320+
beginSuccess &= bluetoothSerialSpp->begin(
321+
deviceName, false, false, settings.sppRxQueueSize,
322+
settings.sppTxQueueSize); // localName, isMaster, disableBLE, rxBufferSize, txBufferSize
299323
}
300-
else
324+
else if (settings.bluetoothRadioType == BLUETOOTH_RADIO_SPP)
301325
{
302-
beginSuccess &=
303-
bluetoothSerial->begin(deviceName, false, true, settings.sppRxQueueSize,
304-
settings.sppTxQueueSize); // localName, isMaster, disableBLE, rxBufferSize, txBufferSize
326+
// Disable BLE
327+
beginSuccess &= bluetoothSerialSpp->begin(
328+
deviceName, false, true, settings.sppRxQueueSize,
329+
settings.sppTxQueueSize); // localName, isMaster, disableBLE, rxBufferSize, txBufferSize
330+
}
331+
else if (settings.bluetoothRadioType == BLUETOOTH_RADIO_BLE)
332+
{
333+
// Don't disable BLE
334+
beginSuccess &= bluetoothSerialBle->begin(
335+
deviceName, false, false, settings.sppRxQueueSize,
336+
settings.sppTxQueueSize); // localName, isMaster, disableBLE, rxBufferSize, txBufferSize
305337
}
306338

307339
if (beginSuccess == false)
@@ -315,8 +347,8 @@ void bluetoothStart()
315347
// https://github.com/espressif/esp-idf/issues/1541
316348
//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
317349
/*
318-
// Note: Since version 3.0.0 this library does not support legacy pairing (using fixed PIN consisting of 4 digits).
319-
esp_bt_sp_param_t param_type = ESP_BT_SP_IOCAP_MODE;
350+
// Note: Since version 3.0.0 this library does not support legacy pairing (using fixed PIN consisting of 4
351+
digits). esp_bt_sp_param_t param_type = ESP_BT_SP_IOCAP_MODE;
320352
321353
esp_bt_io_cap_t iocap = ESP_BT_IO_CAP_NONE; // Requires pin 1234 on old BT dongle, No prompt on new BT dongle
322354
// esp_bt_io_cap_t iocap = ESP_BT_IO_CAP_OUT; //Works but prompts for either pin (old) or 'Does this 6 pin
@@ -342,10 +374,15 @@ void bluetoothStart()
342374
bluetoothSerialSpp->register_callback(bluetoothCallback); // Controls BT state and LED
343375
bluetoothSerialSpp->setTimeout(250);
344376
}
345-
else
377+
else if (settings.bluetoothRadioType == BLUETOOTH_RADIO_SPP)
378+
{
379+
bluetoothSerialSpp->register_callback(bluetoothCallback); // Controls BT state and LED
380+
bluetoothSerialSpp->setTimeout(250);
381+
}
382+
else if (settings.bluetoothRadioType == BLUETOOTH_RADIO_BLE)
346383
{
347-
bluetoothSerial->register_callback(bluetoothCallback); // Controls BT state and LED
348-
bluetoothSerial->setTimeout(250);
384+
bluetoothSerialBle->register_callback(bluetoothCallback); // Controls BT state and LED
385+
bluetoothSerialBle->setTimeout(250);
349386
}
350387

351388
if (settings.bluetoothRadioType == BLUETOOTH_RADIO_SPP_AND_BLE)
@@ -390,7 +427,7 @@ void bluetoothStart()
390427
// }
391428

392429
// This function stops BT so that it can be restarted later
393-
// It also releases as much system resources as possible so that WiFi/caster is more stable
430+
// It also releases as many system resources as possible so that WiFi/caster is more stable
394431
void bluetoothStop()
395432
{
396433
#ifdef COMPILE_BT
@@ -399,22 +436,28 @@ void bluetoothStop()
399436
if (settings.bluetoothRadioType == BLUETOOTH_RADIO_SPP_AND_BLE)
400437
{
401438
bluetoothSerialBle->register_callback(nullptr);
402-
bluetoothSerialBle->flush(); // Complete any transfers
439+
bluetoothSerialBle->flush(); // Complete any transfers
403440
bluetoothSerialBle->disconnect(); // Drop any clients
404-
bluetoothSerialBle->end(); // Release resources
441+
bluetoothSerialBle->end(); // Release resources
405442

406443
bluetoothSerialSpp->register_callback(nullptr);
407-
bluetoothSerialSpp->flush(); // Complete any transfers
444+
bluetoothSerialSpp->flush(); // Complete any transfers
408445
bluetoothSerialSpp->disconnect(); // Drop any clients
409-
bluetoothSerialSpp->end(); // Release resources
446+
bluetoothSerialSpp->end(); // Release resources
410447
}
411-
else
448+
else if (settings.bluetoothRadioType == BLUETOOTH_RADIO_SPP)
412449
{
413-
bluetoothSerial->register_callback(nullptr);
414-
bluetoothSerial->flush(); // Complete any transfers
415-
bluetoothSerial->disconnect(); // Drop any clients
416-
bluetoothSerial->end(); // bluetoothSerial->end() will release significant RAM (~100k!) but a
417-
// bluetoothSerial->start will crash.
450+
bluetoothSerialSpp->register_callback(nullptr);
451+
bluetoothSerialSpp->flush(); // Complete any transfers
452+
bluetoothSerialSpp->disconnect(); // Drop any clients
453+
bluetoothSerialSpp->end(); // Release resources
454+
}
455+
else if (settings.bluetoothRadioType == BLUETOOTH_RADIO_BLE)
456+
{
457+
bluetoothSerialBle->register_callback(nullptr);
458+
bluetoothSerialBle->flush(); // Complete any transfers
459+
bluetoothSerialBle->disconnect(); // Drop any clients
460+
bluetoothSerialBle->end(); // Release resources
418461
}
419462

420463
log_d("Bluetooth turned off");
@@ -473,9 +516,14 @@ void bluetoothTest(bool runTest)
473516
snprintf(macAddress, sizeof(macAddress), "%02X%02X", btMACAddress[4], btMACAddress[5]);
474517
systemPrint("Bluetooth ");
475518
if (settings.bluetoothRadioType == BLUETOOTH_RADIO_SPP_AND_BLE)
476-
systemPrint("Low Energy ");
519+
systemPrint("SPP and Low Energy ");
520+
else if (settings.bluetoothRadioType == BLUETOOTH_RADIO_SPP)
521+
systemPrint("SPP ");
477522
else if (settings.bluetoothRadioType == BLUETOOTH_RADIO_BLE)
478523
systemPrint("Low Energy ");
524+
else if (settings.bluetoothRadioType == BLUETOOTH_RADIO_OFF)
525+
systemPrint("Off ");
526+
479527
systemPrint("(");
480528
systemPrint(macAddress);
481529
systemPrint("): ");

0 commit comments

Comments
 (0)