Skip to content

Commit

Permalink
BLE: add ability to not use the new baudrate
Browse files Browse the repository at this point in the history
  • Loading branch information
chipaudette committed Sep 3, 2021
1 parent 629b2f9 commit 5b21332
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 11 deletions.
19 changes: 10 additions & 9 deletions src/BLE/ble.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ const int faster_baudrate = 115200; //here is the other possible starting baudra

int BLE::begin(int doFactoryReset) //0 is no, 1 = hardware reset
{
bool switch_to_faster_baud_rate = true;
int ret_val = 0;

//clear the incoming serial buffer
Expand All @@ -24,9 +23,11 @@ int BLE::begin(int doFactoryReset) //0 is no, 1 = hardware reset
}

//switch BC127 to listen to the serial link from the Tympan to a faster baud rate
if (switch_to_faster_baud_rate) {
if (useFasterBaudRateUponBegin) {
Serial.println("BLE: begin: setting BC127 baudrate to " + String(faster_baudrate) + ".");
switchToFasterBaudRate(faster_baudrate);
switchToNewBaudRate(faster_baudrate);
} else {
Serial.println("BLE: begin: keeping baudrate at default.");
}

//enable BT_Classic connectable and discoverable, always
Expand Down Expand Up @@ -126,7 +127,7 @@ void BLE::echoBTreply(const bool printDebug) {
if (printDebug && if_any_received) { if (BC127_firmware_ver >= 7) Serial.println(); }
}

void BLE::switchToFasterBaudRate(int new_baudrate) {
void BLE::switchToNewBaudRate(int new_baudrate) {
const bool printDebug = false;

//Send the command to increase the baud rate. Takes effect immediately
Expand All @@ -136,25 +137,25 @@ void BLE::switchToFasterBaudRate(int new_baudrate) {
//to match. So, we need to manually send the command, then swap the Tympan's baud rate, then listen
//new replies.
if (BC127_firmware_ver >= 7) {
if (printDebug) Serial.println("BLE: switchToFasterBaudRate: V7: changing BC127 to " + String(new_baudrate));
if (printDebug) Serial.println("BLE: switchToNewBaudRate: V7: changing BC127 to " + String(new_baudrate));
_serialPort->print("SET UART_CONFIG=" + String(new_baudrate) + " OFF 0" + EOC);
} else {
if (printDebug) Serial.println("BLE: switchToFasterBaudRate: V5: changing BC127 to " + String(new_baudrate));
if (printDebug) Serial.println("BLE: switchToNewBaudRate: V5: changing BC127 to " + String(new_baudrate));
_serialPort->print("SET BAUD=" + String(new_baudrate) + EOC);
}

//Switch the serial link to the BC127 to the faster baud rate
setSerialBaudRate(new_baudrate);
if (printDebug) Serial.println("BLE: switchToFasterBaudRate: setting Serial link to BC127 to " + String(new_baudrate));
if (printDebug) Serial.println("BLE: switchToNewBaudRate: setting Serial link to BC127 to " + String(new_baudrate));


//give time for any replies from the module
delay(500); //500 seems to work on V5
if (printDebug) Serial.println("BLE: switchToFasterBaudRate: Reply from BC127 (if any)...");
if (printDebug) Serial.println("BLE: switchToNewBaudRate: Reply from BC127 (if any)...");
echoBTreply(printDebug);

//clear the serial link by sending a CR...will return an error
if (printDebug) Serial.println("BLE: switchToFasterBaudRate: Confirming asking status");
if (printDebug) Serial.println("BLE: switchToNewBaudRate: Confirming asking status");
_serialPort->print("STATUS" + EOC);
delay(100);
echoBTreply(printDebug); //should cause response of "ERROR"
Expand Down
6 changes: 4 additions & 2 deletions src/BLE/ble.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class BLE : public BC127
public:
BLE(HardwareSerial *sp) : BC127(sp) {}
BLE(TympanBase *tympan) : BC127(tympan->BT_Serial) { setPins(tympan->getPin_BT_PIO0(),tympan->getPin_BT_RST()); };
int begin(int doFactoryReset = 1); //0 = no reset, 1 = hardware reset, 2 = software reset
int begin(int doFactoryReset = 1);
void setupBLE(int BT_firmware = 7, bool printDebug = true); //to be called from the Arduino sketch's setup() routine. Includes factory reset.
void setupBLE_noFactoryReset(int BT_firmware = 7, bool printDebug = true); //to be called from the Arduino sketch's setup() routine. Excludes factory reset.
void setupBLE(int BT_firmware, bool printDebug, int doFactoryReset); //to be called from the Arduino sketch's setup() routine. Must define all params
Expand All @@ -29,10 +29,12 @@ class BLE : public BC127
void updateAdvertising(unsigned long curTime_millis, unsigned long updatePeriod_millis = 5000, bool printDebugMsgs=false);

void echoBTreply(bool printDebug = false);
bool setUseFasterBaudRateUponBegin(bool enable = true) { return useFasterBaudRateUponBegin = enable; }
protected:
bool useFasterBaudRateUponBegin = true;
void setSerialBaudRate(int new_baud);
int hardwareFactoryReset(bool printDebug = false);
void switchToFasterBaudRate(int new_baudrate);
void switchToNewBaudRate(int new_baudrate);

};

Expand Down

0 comments on commit 5b21332

Please sign in to comment.