Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Firmware/RTK_Surveyor/Begin.ino
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,7 @@ void pinUART2Task( void *pvParameters )
vTaskDelete( NULL ); //Delete task once it has run once
}

//Serial Read/Write tasks for the F9P must be started after BT is up and running otherwise SerialBT.available will cause reboot
//Serial Read/Write tasks for the F9P must be started after BT is up and running otherwise SerialBT->available will cause reboot
void startUART2Tasks()
{
//Start the tasks for handling incoming and outgoing BT bytes to/from ZED-F9P
Expand Down
33 changes: 20 additions & 13 deletions Firmware/RTK_Surveyor/Bluetooth.ino
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,7 @@ Bluetooth States:
//----------------------------------------

#ifdef COMPILE_BT

static BluetoothSerial bluetoothSerial;
BTSerialInterface *bluetoothSerial;
static volatile byte bluetoothState = BT_OFF;

//----------------------------------------
Expand Down Expand Up @@ -74,7 +73,7 @@ byte bluetoothGetState()
bool bluetoothIsCongested()
{
#ifdef COMPILE_BT
return bluetoothSerial.isCongested();
return bluetoothSerial->isCongested();
#else //COMPILE_BT
return false;
#endif //COMPILE_BT
Expand All @@ -84,7 +83,7 @@ bool bluetoothIsCongested()
int bluetoothReadBytes(uint8_t * buffer, int length)
{
#ifdef COMPILE_BT
return bluetoothSerial.readBytes(buffer, length);
return bluetoothSerial->readBytes(buffer, length);
#else //COMPILE_BT
return 0;
#endif //COMPILE_BT
Expand All @@ -94,7 +93,7 @@ int bluetoothReadBytes(uint8_t * buffer, int length)
bool bluetoothRxDataAvailable()
{
#ifdef COMPILE_BT
return bluetoothSerial.available();
return bluetoothSerial->available();
#else //COMPILE_BT
return false;
#endif //COMPILE_BT
Expand All @@ -116,7 +115,15 @@ void bluetoothStart()

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

if (bluetoothSerial.begin(deviceName, false, settings.sppRxQueueSize, settings.sppTxQueueSize) == false) //localName, isMaster, rxBufferSize, txBufferSize
// BLE vs Bluetooth Classic
if (settings.enableBLE)
bluetoothSerial = new BTLESerial();
else
{
bluetoothSerial = new BTClassicSerial();
}

if (bluetoothSerial->begin(deviceName, false, settings.sppRxQueueSize, settings.sppTxQueueSize) == false) //localName, isMaster, rxBufferSize, txBufferSize
{
Serial.println("An error occurred initializing Bluetooth");

Expand Down Expand Up @@ -145,8 +152,8 @@ void bluetoothStart()
esp_bt_gap_set_pin(pin_type, 4, pin_code);
//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-

bluetoothSerial.register_callback(bluetoothCallback); //Controls BT Status LED on Surveyor
bluetoothSerial.setTimeout(250);
bluetoothSerial->register_callback(bluetoothCallback); //Controls BT Status LED on Surveyor
bluetoothSerial->setTimeout(250);

Serial.print("Bluetooth broadcasting as: ");
Serial.println(deviceName);
Expand All @@ -172,10 +179,10 @@ void bluetoothStop()
#ifdef COMPILE_BT
if (bluetoothState == BT_NOTCONNECTED || bluetoothState == BT_CONNECTED)
{
bluetoothSerial.register_callback(NULL);
bluetoothSerial.flush(); //Complete any transfers
bluetoothSerial.disconnect(); //Drop any clients
bluetoothSerial.end(); //bluetoothSerial.end() will release significant RAM (~100k!) but a bluetoothSerial.start will crash.
bluetoothSerial->register_callback(NULL);
bluetoothSerial->flush(); //Complete any transfers
bluetoothSerial->disconnect(); //Drop any clients
bluetoothSerial->end(); //bluetoothSerial->end() will release significant RAM (~100k!) but a bluetoothSerial->start will crash.

log_d("Bluetooth turned off");

Expand All @@ -191,7 +198,7 @@ int bluetoothWriteBytes(const uint8_t * buffer, int length)
{
#ifdef COMPILE_BT
//Push new data to BT SPP
return bluetoothSerial.write(buffer, length);
return bluetoothSerial->write(buffer, length);
#else //COMPILE_BT
return 0;
#endif //COMPILE_BT
Expand Down
3 changes: 3 additions & 0 deletions Firmware/RTK_Surveyor/NVM.ino
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,7 @@ void recordSystemSettingsToFile(File * settingsFile)
}
settingsFile->printf("%s=%d\n\r", "espnowPeerCount", settings.espnowPeerCount);
settingsFile->printf("%s=%d\n\r", "enableNtripServerMessageParsing", settings.enableNtripServerMessageParsing);
settingsFile->printf("%s=%d\n\r", "enableBLE", settings.enableBLE);

//Record constellation settings
for (int x = 0 ; x < MAX_CONSTELLATIONS ; x++)
Expand Down Expand Up @@ -875,6 +876,8 @@ bool parseLine(char* str, Settings *settings)
settings->espnowPeerCount = d;
else if (strcmp(settingName, "enableNtripServerMessageParsing") == 0)
settings->enableNtripServerMessageParsing = d;
else if (strcmp(settingName, "enableBLE") == 0)
settings->enableBLE = d;

//Check for bulk settings (constellations, message rates, ESPNOW Peers)
//Must be last on else list
Expand Down
10 changes: 9 additions & 1 deletion Firmware/RTK_Surveyor/RTK_Surveyor.ino
Original file line number Diff line number Diff line change
Expand Up @@ -242,8 +242,16 @@ float battChangeRate = 0.0;
//Hardware serial and BT buffers
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
#ifdef COMPILE_BT
// See bluetoothSelect.h for implemenation
#include "bluetoothSelect.h"

//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
#include "src/BluetoothSerial/BluetoothSerial.h"
// #include "src/BluetoothSerial/BluetoothSerial.h"
// BluetoothSerial SerialBT;

// BLE Support originally from https://github.com/avinabmalla/ESP32_BleSerial/tree/bad5ff841800853a61e431ea751f8ea9d7a1df21
// #include "src/BleSerial/BleSerial.h"
// BleSerial SerialBLE;
#endif

char platformPrefix[55] = "Surveyor"; //Sets the prefix for broadcast names
Expand Down
161 changes: 161 additions & 0 deletions Firmware/RTK_Surveyor/bluetoothSelect.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
#ifdef COMPILE_BT

#include "src/BluetoothSerial/BluetoothSerial.h"
#include "src/BleSerial/BleSerial.h"

class BTSerialInterface
{
public:
virtual bool begin(String deviceName, bool isMaster, uint16_t rxQueueSize, uint16_t txQueueSize) = 0;
virtual void disconnect() = 0;
virtual void end() = 0;
virtual esp_err_t register_callback(esp_spp_cb_t * callback) = 0;
virtual void setTimeout(unsigned long timeout) = 0;

virtual int available() = 0;
virtual size_t readBytes(uint8_t *buffer, size_t bufferSize) = 0;

virtual bool isCongested() = 0;
virtual size_t write(const uint8_t *buffer, size_t size) = 0;
virtual void flush() = 0;
};


class BTClassicSerial : public virtual BTSerialInterface, public BluetoothSerial
{
// Everything is already implemented in BluetoothSerial since the code was
// originally written using that class
public:
bool begin(String deviceName, bool isMaster, uint16_t rxQueueSize, uint16_t txQueueSize)
{
return BluetoothSerial::begin(deviceName, isMaster, rxQueueSize, txQueueSize);
}

void disconnect()
{
BluetoothSerial::disconnect();
}

void end()
{
BluetoothSerial::end();
}

esp_err_t register_callback(esp_spp_cb_t * callback)
{
return BluetoothSerial::register_callback(callback);
}

void setTimeout(unsigned long timeout)
{
BluetoothSerial::setTimeout(timeout);
}

int available()
{
return BluetoothSerial::available();
}

size_t readBytes(uint8_t *buffer, size_t bufferSize)
{
return BluetoothSerial::readBytes(buffer, bufferSize);
}

bool isCongested()
{
return BluetoothSerial::isCongested();
}

size_t write(const uint8_t *buffer, size_t size)
{
return BluetoothSerial::write(buffer, size);
}

void flush()
{
BluetoothSerial::flush();
}
};


class BTLESerial: public virtual BTSerialInterface, public BleSerial
{
public:
// Missing from BleSerial
bool begin(String deviceName, bool isMaster, uint16_t rxQueueSuze, uint16_t txQueueSize)
{
// Curretnly ignoring rxQueueSize
// transmitBufferLength = txQueueSize;
BleSerial::begin(deviceName.c_str());
return true;
}

void disconnect()
{
Server->disconnect(Server->getConnId());
}

void end()
{
BleSerial::end();
}

esp_err_t register_callback(esp_spp_cb_t * callback)
{
connectionCallback = callback;
return ESP_OK;
}

void setTimeout(unsigned long timeout)
{
BleSerial::setTimeout(timeout);
}

int available()
{
return BleSerial::available();
}

size_t readBytes(uint8_t *buffer, size_t bufferSize)
{
return BleSerial::readBytes(buffer, bufferSize);
}

bool isCongested()
{
// not currently supported in this implementation
return false;
}

size_t write(const uint8_t *buffer, size_t size)
{
return BleSerial::write(buffer, size);
}

void flush()
{
BleSerial::flush();
}

// override BLEServerCallbacks
void onConnect(BLEServer *pServer)
{
bleConnected = true;
connectionCallback(ESP_SPP_SRV_OPEN_EVT, nullptr);
}

void onDisconnect(BLEServer *pServer)
{
bleConnected = false;
connectionCallback(ESP_SPP_CLOSE_EVT, nullptr);
Server->startAdvertising();
}

private:
esp_spp_cb_t * connectionCallback;

};


#endif

45 changes: 45 additions & 0 deletions Firmware/RTK_Surveyor/menuBluetooth.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
void menuBluetooth()
{
while (1)
{
Serial.println();
Serial.println(F("Menu: Blueooth Menu"));

Serial.println();
Serial.print(F("Current Bluetooth Mode: "));
if (settings.enableBLE == true)
Serial.println(F("BLE"));
else
Serial.println(F("Classic"));

Serial.println();
Serial.println(F("1) Set Bluetooth Mode to Classic"));
Serial.println(F("2) Set Bluetooth Mode to BLE"));
Serial.println(F("x) Exit"));

byte incoming = getByteChoice(menuTimeout);
if (incoming == '1')
{
// Restart Bluetooth
bluetoothStop();
settings.enableBLE = false;
bluetoothStart();
}
else if (incoming == '2')
{
// restart Bluetooth
bluetoothStop();
settings.enableBLE = true;
bluetoothStart();
}
else if (incoming == 'x')
break;
else if (incoming == STATUS_GETBYTE_TIMEOUT)
break;
else
printUnknown(incoming);
}

while (Serial.available()) Serial.read();
}

4 changes: 4 additions & 0 deletions Firmware/RTK_Surveyor/menuMain.ino
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ void menuMain()

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

Serial.println("6) Configure Bluetooth");

Serial.println("p) Configure Profiles");

#ifdef COMPILE_ESPNOW
Expand Down Expand Up @@ -81,6 +83,8 @@ void menuMain()
menuPorts();
else if (incoming == '5')
menuLog();
else if (incoming == '6')
menuBluetooth();
else if (incoming == 's')
menuSystem();
else if (incoming == 'p')
Expand Down
2 changes: 2 additions & 0 deletions Firmware/RTK_Surveyor/settings.h
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,8 @@ typedef struct {
bool enableExternalHardwareEventLogging = false; //Log when INT/TM2 pin goes low
bool enableMarksFile = false; //Log marks to the marks file

bool enableBLE = false;

ubxMsg ubxMessages[MAX_UBX_MSG] = //Report rates for all known messages
{
//NMEA
Expand Down
Loading