Skip to content

Commit 95e26ef

Browse files
author
burnsed
committed
Add BLE Support. See setting menu to enable
1 parent eb3e4e5 commit 95e26ef

File tree

11 files changed

+550
-15
lines changed

11 files changed

+550
-15
lines changed

Firmware/RTK_Surveyor/Begin.ino

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -338,7 +338,7 @@ void pinUART2Task( void *pvParameters )
338338
vTaskDelete( NULL ); //Delete task once it has run once
339339
}
340340

341-
//Serial Read/Write tasks for the F9P must be started after BT is up and running otherwise SerialBT.available will cause reboot
341+
//Serial Read/Write tasks for the F9P must be started after BT is up and running otherwise SerialBT->available will cause reboot
342342
void startUART2Tasks()
343343
{
344344
//Start the tasks for handling incoming and outgoing BT bytes to/from ZED-F9P

Firmware/RTK_Surveyor/Bluetooth.ino

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,7 @@ Bluetooth States:
2828
//----------------------------------------
2929

3030
#ifdef COMPILE_BT
31-
32-
static BluetoothSerial bluetoothSerial;
31+
BTSerialInterface *bluetoothSerial;
3332
static volatile byte bluetoothState = BT_OFF;
3433

3534
//----------------------------------------
@@ -74,7 +73,7 @@ byte bluetoothGetState()
7473
bool bluetoothIsCongested()
7574
{
7675
#ifdef COMPILE_BT
77-
return bluetoothSerial.isCongested();
76+
return bluetoothSerial->isCongested();
7877
#else //COMPILE_BT
7978
return false;
8079
#endif //COMPILE_BT
@@ -84,7 +83,7 @@ bool bluetoothIsCongested()
8483
int bluetoothReadBytes(uint8_t * buffer, int length)
8584
{
8685
#ifdef COMPILE_BT
87-
return bluetoothSerial.readBytes(buffer, length);
86+
return bluetoothSerial->readBytes(buffer, length);
8887
#else //COMPILE_BT
8988
return 0;
9089
#endif //COMPILE_BT
@@ -94,7 +93,7 @@ int bluetoothReadBytes(uint8_t * buffer, int length)
9493
bool bluetoothRxDataAvailable()
9594
{
9695
#ifdef COMPILE_BT
97-
return bluetoothSerial.available();
96+
return bluetoothSerial->available();
9897
#else //COMPILE_BT
9998
return false;
10099
#endif //COMPILE_BT
@@ -116,7 +115,15 @@ void bluetoothStart()
116115

117116
sprintf(deviceName, "%s %s%02X%02X", platformPrefix, stateName, unitMACAddress[4], unitMACAddress[5]);
118117

119-
if (bluetoothSerial.begin(deviceName, false, settings.sppRxQueueSize, settings.sppTxQueueSize) == false) //localName, isMaster, rxBufferSize, txBufferSize
118+
// BLE vs Bluetooth Classic
119+
if (settings.enableBLE)
120+
bluetoothSerial = new BTLESerial();
121+
else
122+
{
123+
bluetoothSerial = new BTClassicSerial();
124+
}
125+
126+
if (bluetoothSerial->begin(deviceName, false, settings.sppRxQueueSize, settings.sppTxQueueSize) == false) //localName, isMaster, rxBufferSize, txBufferSize
120127
{
121128
Serial.println("An error occurred initializing Bluetooth");
122129

@@ -145,8 +152,8 @@ void bluetoothStart()
145152
esp_bt_gap_set_pin(pin_type, 4, pin_code);
146153
//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
147154

148-
bluetoothSerial.register_callback(bluetoothCallback); //Controls BT Status LED on Surveyor
149-
bluetoothSerial.setTimeout(250);
155+
bluetoothSerial->register_callback(bluetoothCallback); //Controls BT Status LED on Surveyor
156+
bluetoothSerial->setTimeout(250);
150157

151158
Serial.print("Bluetooth broadcasting as: ");
152159
Serial.println(deviceName);
@@ -172,10 +179,10 @@ void bluetoothStop()
172179
#ifdef COMPILE_BT
173180
if (bluetoothState == BT_NOTCONNECTED || bluetoothState == BT_CONNECTED)
174181
{
175-
bluetoothSerial.register_callback(NULL);
176-
bluetoothSerial.flush(); //Complete any transfers
177-
bluetoothSerial.disconnect(); //Drop any clients
178-
bluetoothSerial.end(); //bluetoothSerial.end() will release significant RAM (~100k!) but a bluetoothSerial.start will crash.
182+
bluetoothSerial->register_callback(NULL);
183+
bluetoothSerial->flush(); //Complete any transfers
184+
bluetoothSerial->disconnect(); //Drop any clients
185+
bluetoothSerial->end(); //bluetoothSerial->end() will release significant RAM (~100k!) but a bluetoothSerial->start will crash.
179186

180187
log_d("Bluetooth turned off");
181188

@@ -191,7 +198,7 @@ int bluetoothWriteBytes(const uint8_t * buffer, int length)
191198
{
192199
#ifdef COMPILE_BT
193200
//Push new data to BT SPP
194-
return bluetoothSerial.write(buffer, length);
201+
return bluetoothSerial->write(buffer, length);
195202
#else //COMPILE_BT
196203
return 0;
197204
#endif //COMPILE_BT

Firmware/RTK_Surveyor/NVM.ino

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,7 @@ void recordSystemSettingsToFile(File * settingsFile)
267267
}
268268
settingsFile->printf("%s=%d\n\r", "espnowPeerCount", settings.espnowPeerCount);
269269
settingsFile->printf("%s=%d\n\r", "enableNtripServerMessageParsing", settings.enableNtripServerMessageParsing);
270+
settingsFile->printf("%s=%d\n\r", "enableBLE", settings.enableBLE);
270271

271272
//Record constellation settings
272273
for (int x = 0 ; x < MAX_CONSTELLATIONS ; x++)
@@ -875,6 +876,8 @@ bool parseLine(char* str, Settings *settings)
875876
settings->espnowPeerCount = d;
876877
else if (strcmp(settingName, "enableNtripServerMessageParsing") == 0)
877878
settings->enableNtripServerMessageParsing = d;
879+
else if (strcmp(settingName, "enableBLE") == 0)
880+
settings->enableBLE = d;
878881

879882
//Check for bulk settings (constellations, message rates, ESPNOW Peers)
880883
//Must be last on else list

Firmware/RTK_Surveyor/RTK_Surveyor.ino

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -242,8 +242,16 @@ float battChangeRate = 0.0;
242242
//Hardware serial and BT buffers
243243
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
244244
#ifdef COMPILE_BT
245+
// See bluetoothSelect.h for implemenation
246+
#include "bluetoothSelect.h"
247+
245248
//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
246-
#include "src/BluetoothSerial/BluetoothSerial.h"
249+
// #include "src/BluetoothSerial/BluetoothSerial.h"
250+
// BluetoothSerial SerialBT;
251+
252+
// BLE Support originally from https://github.com/avinabmalla/ESP32_BleSerial/tree/bad5ff841800853a61e431ea751f8ea9d7a1df21
253+
// #include "src/BleSerial/BleSerial.h"
254+
// BleSerial SerialBLE;
247255
#endif
248256

249257
char platformPrefix[55] = "Surveyor"; //Sets the prefix for broadcast names
Lines changed: 161 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,161 @@
1+
#ifdef COMPILE_BT
2+
3+
#include "src/BluetoothSerial/BluetoothSerial.h"
4+
#include "src/BleSerial/BleSerial.h"
5+
6+
class BTSerialInterface
7+
{
8+
public:
9+
virtual bool begin(String deviceName, bool isMaster, uint16_t rxQueueSize, uint16_t txQueueSize) = 0;
10+
virtual void disconnect() = 0;
11+
virtual void end() = 0;
12+
virtual esp_err_t register_callback(esp_spp_cb_t * callback) = 0;
13+
virtual void setTimeout(unsigned long timeout) = 0;
14+
15+
virtual int available() = 0;
16+
virtual size_t readBytes(uint8_t *buffer, size_t bufferSize) = 0;
17+
18+
virtual bool isCongested() = 0;
19+
virtual size_t write(const uint8_t *buffer, size_t size) = 0;
20+
virtual void flush() = 0;
21+
};
22+
23+
24+
class BTClassicSerial : public virtual BTSerialInterface, public BluetoothSerial
25+
{
26+
// Everything is already implemented in BluetoothSerial since the code was
27+
// originally written using that class
28+
public:
29+
bool begin(String deviceName, bool isMaster, uint16_t rxQueueSize, uint16_t txQueueSize)
30+
{
31+
return BluetoothSerial::begin(deviceName, isMaster, rxQueueSize, txQueueSize);
32+
}
33+
34+
void disconnect()
35+
{
36+
BluetoothSerial::disconnect();
37+
}
38+
39+
void end()
40+
{
41+
BluetoothSerial::end();
42+
}
43+
44+
esp_err_t register_callback(esp_spp_cb_t * callback)
45+
{
46+
return BluetoothSerial::register_callback(callback);
47+
}
48+
49+
void setTimeout(unsigned long timeout)
50+
{
51+
BluetoothSerial::setTimeout(timeout);
52+
}
53+
54+
int available()
55+
{
56+
return BluetoothSerial::available();
57+
}
58+
59+
size_t readBytes(uint8_t *buffer, size_t bufferSize)
60+
{
61+
return BluetoothSerial::readBytes(buffer, bufferSize);
62+
}
63+
64+
bool isCongested()
65+
{
66+
return BluetoothSerial::isCongested();
67+
}
68+
69+
size_t write(const uint8_t *buffer, size_t size)
70+
{
71+
return BluetoothSerial::write(buffer, size);
72+
}
73+
74+
void flush()
75+
{
76+
BluetoothSerial::flush();
77+
}
78+
};
79+
80+
81+
class BTLESerial: public virtual BTSerialInterface, public BleSerial
82+
{
83+
public:
84+
// Missing from BleSerial
85+
bool begin(String deviceName, bool isMaster, uint16_t rxQueueSuze, uint16_t txQueueSize)
86+
{
87+
// Curretnly ignoring rxQueueSize
88+
// transmitBufferLength = txQueueSize;
89+
BleSerial::begin(deviceName.c_str());
90+
return true;
91+
}
92+
93+
void disconnect()
94+
{
95+
Server->disconnect(Server->getConnId());
96+
}
97+
98+
void end()
99+
{
100+
BleSerial::end();
101+
}
102+
103+
esp_err_t register_callback(esp_spp_cb_t * callback)
104+
{
105+
connectionCallback = callback;
106+
return ESP_OK;
107+
}
108+
109+
void setTimeout(unsigned long timeout)
110+
{
111+
BleSerial::setTimeout(timeout);
112+
}
113+
114+
int available()
115+
{
116+
return BleSerial::available();
117+
}
118+
119+
size_t readBytes(uint8_t *buffer, size_t bufferSize)
120+
{
121+
return BleSerial::readBytes(buffer, bufferSize);
122+
}
123+
124+
bool isCongested()
125+
{
126+
// not currently supported in this implementation
127+
return false;
128+
}
129+
130+
size_t write(const uint8_t *buffer, size_t size)
131+
{
132+
return BleSerial::write(buffer, size);
133+
}
134+
135+
void flush()
136+
{
137+
BleSerial::flush();
138+
}
139+
140+
// override BLEServerCallbacks
141+
void onConnect(BLEServer *pServer)
142+
{
143+
bleConnected = true;
144+
connectionCallback(ESP_SPP_SRV_OPEN_EVT, nullptr);
145+
}
146+
147+
void onDisconnect(BLEServer *pServer)
148+
{
149+
bleConnected = false;
150+
connectionCallback(ESP_SPP_CLOSE_EVT, nullptr);
151+
Server->startAdvertising();
152+
}
153+
154+
private:
155+
esp_spp_cb_t * connectionCallback;
156+
157+
};
158+
159+
160+
#endif
161+
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
void menuBluetooth()
2+
{
3+
while (1)
4+
{
5+
Serial.println();
6+
Serial.println(F("Menu: Blueooth Menu"));
7+
8+
Serial.println();
9+
Serial.print(F("Current Bluetooth Mode: "));
10+
if (settings.enableBLE == true)
11+
Serial.println(F("BLE"));
12+
else
13+
Serial.println(F("Classic"));
14+
15+
Serial.println();
16+
Serial.println(F("1) Set Bluetooth Mode to Classic"));
17+
Serial.println(F("2) Set Bluetooth Mode to BLE"));
18+
Serial.println(F("x) Exit"));
19+
20+
byte incoming = getByteChoice(menuTimeout);
21+
if (incoming == '1')
22+
{
23+
// Restart Bluetooth
24+
bluetoothStop();
25+
settings.enableBLE = false;
26+
bluetoothStart();
27+
}
28+
else if (incoming == '2')
29+
{
30+
// restart Bluetooth
31+
bluetoothStop();
32+
settings.enableBLE = true;
33+
bluetoothStart();
34+
}
35+
else if (incoming == 'x')
36+
break;
37+
else if (incoming == STATUS_GETBYTE_TIMEOUT)
38+
break;
39+
else
40+
printUnknown(incoming);
41+
}
42+
43+
while (Serial.available()) Serial.read();
44+
}
45+

Firmware/RTK_Surveyor/menuMain.ino

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ void menuMain()
5151

5252
Serial.println("5) Configure Logging");
5353

54+
Serial.println("6) Configure Bluetooth");
55+
5456
Serial.println("p) Configure Profiles");
5557

5658
#ifdef COMPILE_ESPNOW
@@ -81,6 +83,8 @@ void menuMain()
8183
menuPorts();
8284
else if (incoming == '5')
8385
menuLog();
86+
else if (incoming == '6')
87+
menuBluetooth();
8488
else if (incoming == 's')
8589
menuSystem();
8690
else if (incoming == 'p')

Firmware/RTK_Surveyor/settings.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,8 @@ typedef struct {
278278
bool enableExternalHardwareEventLogging = false; //Log when INT/TM2 pin goes low
279279
bool enableMarksFile = false; //Log marks to the marks file
280280

281+
bool enableBLE = false;
282+
281283
ubxMsg ubxMessages[MAX_UBX_MSG] = //Report rates for all known messages
282284
{
283285
//NMEA

0 commit comments

Comments
 (0)