Skip to content

Commit 4682aa1

Browse files
LeeLeahy2nseidle
authored andcommitted
Move Bluetooth support into Bluetooth.ino
1 parent f7b639b commit 4682aa1

File tree

12 files changed

+224
-142
lines changed

12 files changed

+224
-142
lines changed
Lines changed: 201 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,201 @@
1+
/*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
2+
Bluetooth State Values:
3+
BT_OFF = 0,
4+
BT_NOTCONNECTED,
5+
BT_CONNECTED,
6+
7+
Bluetooth States:
8+
9+
BT_OFF (Using WiFi)
10+
| ^
11+
Use Bluetooth | | Use WiFi
12+
bluetoothStart | | bluetoothStop
13+
v |
14+
BT_NOTCONNECTED
15+
| ^
16+
Client connected | | Client disconnected
17+
v |
18+
BT_CONNECTED
19+
20+
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
21+
22+
//----------------------------------------
23+
// Constants
24+
//----------------------------------------
25+
26+
//----------------------------------------
27+
// Locals - compiled out
28+
//----------------------------------------
29+
30+
#ifdef COMPILE_BT
31+
32+
static BluetoothSerial bluetoothSerial;
33+
static volatile byte bluetoothState = BT_OFF;
34+
35+
//----------------------------------------
36+
// Bluetooth Routines - compiled out
37+
//----------------------------------------
38+
39+
//Call back for when BT connection event happens (connected/disconnect)
40+
//Used for updating the bluetoothState state machine
41+
void bluetoothCallback(esp_spp_cb_event_t event, esp_spp_cb_param_t *param) {
42+
if (event == ESP_SPP_SRV_OPEN_EVT) {
43+
Serial.println(F("Client Connected"));
44+
bluetoothState = BT_CONNECTED;
45+
if (productVariant == RTK_SURVEYOR)
46+
digitalWrite(pin_bluetoothStatusLED, HIGH);
47+
}
48+
49+
if (event == ESP_SPP_CLOSE_EVT ) {
50+
Serial.println(F("Client disconnected"));
51+
bluetoothState = BT_NOTCONNECTED;
52+
if (productVariant == RTK_SURVEYOR)
53+
digitalWrite(pin_bluetoothStatusLED, LOW);
54+
}
55+
}
56+
57+
#endif //COMPILE_BT
58+
59+
//----------------------------------------
60+
// Global Bluetooth Routines
61+
//----------------------------------------
62+
63+
//Return the Bluetooth state
64+
byte bluetoothGetState()
65+
{
66+
#ifdef COMPILE_BT
67+
return bluetoothState;
68+
#else //COMPILE_BT
69+
return BT_OFF;
70+
#endif //COMPILE_BT
71+
}
72+
73+
//Determine if the Bluetooth link is congested
74+
bool bluetoothIsCongested()
75+
{
76+
#ifdef COMPILE_BT
77+
return bluetoothSerial.isCongested();
78+
#else //COMPILE_BT
79+
return false;
80+
#endif //COMPILE_BT
81+
}
82+
83+
//Read data from the Bluetooth device
84+
int bluetoothReadBytes(uint8_t * buffer, int length)
85+
{
86+
#ifdef COMPILE_BT
87+
return bluetoothSerial.readBytes(buffer, length);
88+
#else //COMPILE_BT
89+
return 0;
90+
#endif //COMPILE_BT
91+
}
92+
93+
//Determine if data is available
94+
bool bluetoothRxDataAvailable()
95+
{
96+
#ifdef COMPILE_BT
97+
return bluetoothSerial.available();
98+
#else //COMPILE_BT
99+
return false;
100+
#endif //COMPILE_BT
101+
}
102+
103+
//Get MAC, start radio
104+
//Tack device's MAC address to end of friendly broadcast name
105+
//This allows multiple units to be on at same time
106+
void bluetoothStart()
107+
{
108+
ntripClientStop(true);
109+
ntripServerStop(true);
110+
wifiStop();
111+
#ifdef COMPILE_BT
112+
if (bluetoothState == BT_OFF)
113+
{
114+
char stateName[10];
115+
if (buttonPreviousState == BUTTON_ROVER)
116+
strcpy(stateName, "Rover");
117+
else
118+
strcpy(stateName, "Base");
119+
120+
sprintf(deviceName, "%s %s-%02X%02X", platformPrefix, stateName, unitMACAddress[4], unitMACAddress[5]); //Base mode
121+
122+
if (bluetoothSerial.begin(deviceName, false, settings.sppRxQueueSize, settings.sppTxQueueSize) == false) //localName, isMaster, rxBufferSize, txBufferSize
123+
{
124+
Serial.println(F("An error occurred initializing Bluetooth"));
125+
126+
if (productVariant == RTK_SURVEYOR)
127+
digitalWrite(pin_bluetoothStatusLED, LOW);
128+
return;
129+
}
130+
131+
//Set PIN to 1234 so we can connect to older BT devices, but not require a PIN for modern device pairing
132+
//See issue: https://github.com/sparkfun/SparkFun_RTK_Firmware/issues/5
133+
//https://github.com/espressif/esp-idf/issues/1541
134+
//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
135+
esp_bt_sp_param_t param_type = ESP_BT_SP_IOCAP_MODE;
136+
137+
esp_bt_io_cap_t iocap = ESP_BT_IO_CAP_NONE; //Requires pin 1234 on old BT dongle, No prompt on new BT dongle
138+
//esp_bt_io_cap_t iocap = ESP_BT_IO_CAP_OUT; //Works but prompts for either pin (old) or 'Does this 6 pin appear on the device?' (new)
139+
140+
esp_bt_gap_set_security_param(param_type, &iocap, sizeof(uint8_t));
141+
142+
esp_bt_pin_type_t pin_type = ESP_BT_PIN_TYPE_FIXED;
143+
esp_bt_pin_code_t pin_code;
144+
pin_code[0] = '1';
145+
pin_code[1] = '2';
146+
pin_code[2] = '3';
147+
pin_code[3] = '4';
148+
esp_bt_gap_set_pin(pin_type, 4, pin_code);
149+
//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
150+
151+
bluetoothSerial.register_callback(bluetoothCallback); //Controls BT Status LED on Surveyor
152+
bluetoothSerial.setTimeout(250);
153+
154+
Serial.print(F("Bluetooth broadcasting as: "));
155+
Serial.println(deviceName);
156+
157+
//Start task for controlling Bluetooth pair LED
158+
if (productVariant == RTK_SURVEYOR)
159+
{
160+
ledcWrite(ledBTChannel, 255); //Turn on BT LED
161+
btLEDTask.detach(); //Slow down the BT LED blinker task
162+
btLEDTask.attach(btLEDTaskPace2Hz, updateBTled); //Rate in seconds, callback
163+
}
164+
165+
bluetoothState = BT_NOTCONNECTED;
166+
reportHeapNow();
167+
}
168+
#endif //COMPILE_BT
169+
}
170+
171+
//This function stops BT so that it can be restarted later
172+
//It also releases as much system resources as possible so that WiFi/caster is more stable
173+
void bluetoothStop()
174+
{
175+
#ifdef COMPILE_BT
176+
if (bluetoothState == BT_NOTCONNECTED || bluetoothState == BT_CONNECTED)
177+
{
178+
bluetoothSerial.register_callback(NULL);
179+
bluetoothSerial.flush(); //Complete any transfers
180+
bluetoothSerial.disconnect(); //Drop any clients
181+
bluetoothSerial.end(); //bluetoothSerial.end() will release significant RAM (~100k!) but a bluetoothSerial.start will crash.
182+
183+
log_d("Bluetooth turned off");
184+
185+
bluetoothState = BT_OFF;
186+
reportHeapNow();
187+
}
188+
#endif //COMPILE_BT
189+
online.rxRtcmCorrectionData = false;
190+
}
191+
192+
//Write data to the Bluetooth device
193+
int bluetoothWriteBytes(const uint8_t * buffer, int length)
194+
{
195+
#ifdef COMPILE_BT
196+
//Push new data to BT SPP
197+
return bluetoothSerial.write(buffer, length);
198+
#else //COMPILE_BT
199+
return 0;
200+
#endif //COMPILE_BT
201+
}

Firmware/RTK_Surveyor/Display.ino

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -330,7 +330,7 @@ void updateDisplay()
330330
else if (icons & ICON_BT_SYMBOL)
331331
{
332332
displayBitmap(4, 0, BT_Symbol_Width, BT_Symbol_Height, BT_Symbol);
333-
if (btState == BT_CONNECTED)
333+
if (bluetoothGetState() == BT_CONNECTED)
334334
{
335335
if (icons & ICON_DOWN_ARROW)
336336
{
@@ -568,7 +568,7 @@ uint32_t paintWirelessIcon()
568568
if (online.display == true)
569569
{
570570
//Bluetooth icon if paired, or Bluetooth MAC address if not paired
571-
if (btState == BT_CONNECTED)
571+
if (bluetoothGetState() == BT_CONNECTED)
572572
{
573573
icons = ICON_BT_SYMBOL;
574574
if (systemState <= STATE_BASE_NOT_STARTED)
@@ -913,7 +913,7 @@ void paintRTCM()
913913
int textY = 17;
914914
int textKerning = 8;
915915
oled.setFont(QW_FONT_8X16);
916-
if (btState != BT_OFF)
916+
if (bluetoothGetState() != BT_OFF)
917917
{
918918
int textX = 1;
919919
printTextwithKerning("Xmitting", textX, textY, textKerning); //via Bluetooth

Firmware/RTK_Surveyor/NtripClient.ino

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ void ntripClientSwitchToBluetooth()
189189
ntripClientStop(true);
190190

191191
//Turn on Bluetooth with 'Rover' name
192-
startBluetooth();
192+
bluetoothStart();
193193
}
194194

195195
//Update the state of the NTRIP client state machine

Firmware/RTK_Surveyor/NtripServer.ino

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,7 @@ void ntripServerSwitchToBluetooth()
303303
ntripServerStop(true);
304304

305305
//Turn on Bluetooth with 'Rover' name
306-
startBluetooth();
306+
bluetoothStart();
307307
}
308308

309309
#endif // COMPILE_WIFI

Firmware/RTK_Surveyor/RTK_Surveyor.ino

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,6 @@ float battChangeRate = 0.0;
221221
#ifdef COMPILE_BT
222222
//We use a local copy of the BluetoothSerial library so that we can increase the RX buffer. See issue: https://github.com/sparkfun/SparkFun_RTK_Firmware/issues/23
223223
#include "src/BluetoothSerial/BluetoothSerial.h"
224-
BluetoothSerial SerialBT;
225224
#endif
226225

227226
char platformPrefix[40] = "Surveyor"; //Sets the prefix for broadcast names

Firmware/RTK_Surveyor/States.ino

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ void updateSystemState()
116116

117117
i2cGNSS.enableRTCMmessage(UBX_RTCM_1230, COM_PORT_UART2, 0); //Disable RTCM sentences
118118

119-
startBluetooth(); //Turn on Bluetooth with 'Rover' name
119+
bluetoothStart(); //Turn on Bluetooth with 'Rover' name
120120
startUART2Tasks(); //Start monitoring the UART1 from ZED for NMEA and UBX data (enables logging)
121121

122122
settings.updateZEDSettings = false; //On the next boot, no need to update the ZED on this profile
@@ -235,7 +235,7 @@ void updateSystemState()
235235

236236
//Stop all WiFi and BT. Re-enable in each specific base start state.
237237
wifiStop();
238-
stopBluetooth();
238+
bluetoothStop();
239239
startUART2Tasks(); //Start monitoring the UART1 from ZED for NMEA and UBX data (enables logging)
240240

241241
if (configureUbloxModuleBase() == true)
@@ -253,7 +253,7 @@ void updateSystemState()
253253
if (settings.ntripServer_StartAtSurveyIn)
254254
ntripServerStart();
255255
else
256-
startBluetooth();
256+
bluetoothStart();
257257
changeState(STATE_BASE_TEMP_SETTLE);
258258
}
259259
else if (settings.fixedBase == true)
@@ -597,7 +597,7 @@ void updateSystemState()
597597

598598
displayWiFiConfigNotStarted(); //Display immediately during SD cluster pause
599599

600-
stopBluetooth();
600+
bluetoothStop();
601601
stopUART2Tasks(); //Delete F9 serial tasks if running
602602
startWebServer(); //Start in AP mode and show config html page
603603

0 commit comments

Comments
 (0)