diff options
author | Jakob Stendahl <jakob.stendahl@outlook.com> | 2021-02-24 11:33:03 +0100 |
---|---|---|
committer | Jakob Stendahl <jakob.stendahl@outlook.com> | 2021-02-24 11:33:03 +0100 |
commit | f8aa93d62c5ca5dc2f32d9ace7249c4bbbb50519 (patch) | |
tree | 9e1ed04ff68e476e529c3e264ca45239345fdfbb | |
parent | 8cc460085e75a2cda1bb03cc51786c776da6ebcd (diff) | |
download | hoverbit-ble-f8aa93d62c5ca5dc2f32d9ace7249c4bbbb50519.tar.gz hoverbit-ble-f8aa93d62c5ca5dc2f32d9ace7249c4bbbb50519.zip |
:bug: Try fix #2
-rw-r--r-- | source/main.cpp | 103 |
1 files changed, 52 insertions, 51 deletions
diff --git a/source/main.cpp b/source/main.cpp index eaf3f30..815d560 100644 --- a/source/main.cpp +++ b/source/main.cpp @@ -27,6 +27,8 @@ DEALINGS IN THE SOFTWARE. #include "HoverBitController.h" #include "Screen.h" +#define BLE_UART_DELIM ":" + MicroBit uBit; MicroBitUARTService *uart; HoverBitController controller; @@ -44,68 +46,64 @@ DisplayMainScreenMode displayMainScreenMode = GRAPHS; void onConnected(MicroBitEvent) { bConnected = 1; uBit.audio.soundExpressions.play(ManagedString("giggle")); +} - // mobile app will send ASCII strings terminated with the colon character - ManagedString eom(":"); +void onDisconnected(MicroBitEvent) { + bConnected = 0; + uBit.audio.soundExpressions.play(ManagedString("sad")); +} - while (bConnected) { - ManagedString msg = uart->readUntil(eom); - int length = msg.length(); - const char* command = msg.toCharArray(); +void onDelim(MicroBitEvent) { + ManagedString msg = uart->readUntil(BLE_UART_DELIM); - ManagedString accString("ACC:"); + int length = msg.length(); + const char* command = msg.toCharArray(); - char cCommand = command[0]; - char cChar; - int startI = 1; - bool bEOC = false; - int valLength = 0; + ManagedString accString("ACC:"); - for (int i = 1; i < length; i++) { - cChar = command[i]; + char cCommand = command[0]; + char cChar; + int startI = 1; + bool bEOC = false; + int valLength = 0; - if (i >= length - 1) { - bEOC = true; - valLength = i - startI + 1; - } else if (cChar == 'R' || cChar == 'T' || cChar == 'A' || cChar == 'S') { - bEOC = true; - valLength = i - startI; - } + for (int i = 1; i < length; i++) { + cChar = command[i]; - if (bEOC) { - /* We will just assume that we end up with a valid integer here */ - int value = atoi(msg.substring(startI, startI + valLength).toCharArray()); - - if (cCommand == 'R') { - controller.Rudder(value); - accString = accString + ManagedString("R") + ManagedString(controller.Rudder()); - } else if (cCommand == 'T') { - controller.Throttle(value); - accString = accString + ManagedString("T") + ManagedString(controller.Throttle()); - } else if (cCommand == 'A') { - controller.Arm(value == 1); - accString = accString + ManagedString("A") + ManagedString(controller.Arm()); - } - } else { - // We ignore it :) - } + if (i >= length - 1) { + bEOC = true; + valLength = i - startI + 1; + } else if (cChar == 'R' || cChar == 'T' || cChar == 'A' || cChar == 'S') { + bEOC = true; + valLength = i - startI; + } - cCommand = cChar; - startI = i+1; - bEOC = false; + if (bEOC) { + /* We will just assume that we end up with a valid integer here */ + int value = atoi(msg.substring(startI, startI + valLength).toCharArray()); + + if (cCommand == 'R') { + controller.Rudder(value); + accString = accString + ManagedString("R") + ManagedString(controller.Rudder()); + } else if (cCommand == 'T') { + controller.Throttle(value); + accString = accString + ManagedString("T") + ManagedString(controller.Throttle()); + } else if (cCommand == 'A') { + controller.Arm(value == 1); + accString = accString + ManagedString("A") + ManagedString(controller.Arm()); + } else { + // We ignore it :) } + + cCommand = cChar; + startI = i+1; + bEOC = false; } - // @TODO: Move this to the hoverControl module, we would rather like to have that there, or in the main loop. - // it could also be in the same clause as the batttery sending, but we might want to have it more - // dependent on actual received values. - uart->send(accString); } - -} - -void onDisconnected(MicroBitEvent) { - bConnected = 0; - uBit.audio.soundExpressions.play(ManagedString("sad")); + // @TODO: Move this to the hoverControl module, we would rather like to have that there, or in the main loop. + // it could also be in the same clause as the batttery sending, but we might want to have it more + // dependent on actual received values. + uart->send(accString); } void iconBatteryDead() { @@ -250,6 +248,8 @@ int main() { // Setup listeners uBit.messageBus.listen(MICROBIT_ID_BLE, MICROBIT_BLE_EVT_CONNECTED, onConnected); uBit.messageBus.listen(MICROBIT_ID_BLE, MICROBIT_BLE_EVT_DISCONNECTED, onDisconnected); + uBit.messageBus.listen(MICROBIT_ID_BLE_UART, MICROBIT_UART_S_EVT_DELIM_MATCH, onDelim); + uBit.messageBus.listen(MICROBIT_ID_BUTTON_A, MICROBIT_BUTTON_EVT_CLICK, onButtonA_press); uBit.messageBus.listen(MICROBIT_ID_BUTTON_B, MICROBIT_BUTTON_EVT_CLICK, onButtonB_press); @@ -257,6 +257,7 @@ int main() { // Note GATT table size increased from default in MicroBitConfig.h // #define MICROBIT_SD_GATT_TABLE_SIZE 0x500 uart = new MicroBitUARTService(*uBit.ble, 32, 32); + uart->eventOn(BLE_UART_DELIM); uBit.audio.soundExpressions.play(ManagedString("hello")); |