From 6cfe39dac36e199d712f8f2fb46309afeaeb7f6f Mon Sep 17 00:00:00 2001 From: Peli de Halleux Date: Fri, 24 Jun 2016 08:12:52 -0700 Subject: [PATCH] patching up bluetooth uart --- libs/microbit-bluetooth/bluetooth.cpp | 30 +++++++++++++++++++-------- libs/microbit-bluetooth/bluetooth.ts | 12 ----------- libs/microbit-bluetooth/shims.d.ts | 22 ++++++++++---------- package.json | 2 +- 4 files changed, 33 insertions(+), 33 deletions(-) delete mode 100755 libs/microbit-bluetooth/bluetooth.ts diff --git a/libs/microbit-bluetooth/bluetooth.cpp b/libs/microbit-bluetooth/bluetooth.cpp index 49d275bd..81585e9d 100644 --- a/libs/microbit-bluetooth/bluetooth.cpp +++ b/libs/microbit-bluetooth/bluetooth.cpp @@ -1,7 +1,6 @@ #include "pxt.h" #include "MESEvents.h" #include "MicroBitUARTService.h" -MicroBitUARTService *uart; using namespace pxt; @@ -26,6 +25,8 @@ enum Delimiters { */ //% color=#0082FB weight=20 namespace bluetooth { + MicroBitUARTService *uart = NULL; + /** * Starts the Bluetooth IO pin service. */ @@ -83,10 +84,10 @@ namespace bluetooth { /** * Starts the Bluetooth UART service */ - //% help=bluetooth/start-uart-service - //% blockId=bluetooth_start_uart_service block="bluetooth uart service" blockGap=8 - + // help=bluetooth/start-uart-service + // blockId=bluetooth_start_uart_service block="bluetooth uart service" blockGap=8 void startUartService() { + if (uart) return; // 61 octet buffer size is 3 x (MTU - 3) + 1 // MTU on nRF51822 is 23 octets. 3 are used by Attribute Protocol header data leaving 20 octets for payload // So we allow a RX buffer that can contain 3 x max length messages plus one octet for a terminator character @@ -99,24 +100,35 @@ namespace bluetooth { //% help=bluetooth/uart-write //% blockId=bluetooth_uart_write block="bluetooth uart write %data" blockGap=8 void uartWrite(StringData *data) { - uart->send(ManagedString(data)); + startUartService(); + uart->send(ManagedString(data)); } /** * Reads from the Bluetooth UART service buffer, returning its contents when the specified delimiter character is encountered. */ //% help=bluetooth/uart-read - //% blockId=bluetooth_uart_read block="bluetooth uart read %del" blockGap=8 + //% blockId=bluetooth_uart_read block="bluetooth uart read %del=bluetooth_uart_delimiter_conv" blockGap=8 StringData* uartRead(StringData *del) { + startUartService(); return uart->readUntil(ManagedString(del)).leakData(); } + /** + * Returns the delimiter corresponding string + */ + //% blockId="bluetooth_uart_delimiter_conv" block="%del" + //% weight=1 + StringData* delimiters(Delimiters del) { + ManagedString c("\n\n,$:.#"[max(0, min(6, (int)del))]); + return c.leakData(); + } /** * Register code to run when the micro:bit is connected to over Bluetooth * @param body Code to run when a Bluetooth connection is established */ - //% help=bluetooth/on-bluetooth-connected - //% blockId=bluetooth_on_connected block="on bluetooth connected" + //% help=bluetooth/on-bluetooth-connected weight=20 + //% blockId=bluetooth_on_connected block="on bluetooth connected" blockGap=8 void onBluetoothConnected(Action body) { registerWithDal(MICROBIT_ID_BLE, MICROBIT_BLE_EVT_CONNECTED, body); } @@ -125,7 +137,7 @@ namespace bluetooth { * Register code to run when a bluetooth connection to the micro:bit is lost * @param body Code to run when a Bluetooth connection is lost */ - //% help=bluetooth/on-bluetooth-disconnected + //% help=bluetooth/on-bluetooth-disconnected weight=19 //% blockId=bluetooth_on_disconnected block="on bluetooth disconnected" void onBluetoothDisconnected(Action body) { registerWithDal(MICROBIT_ID_BLE, MICROBIT_BLE_EVT_DISCONNECTED, body); diff --git a/libs/microbit-bluetooth/bluetooth.ts b/libs/microbit-bluetooth/bluetooth.ts deleted file mode 100755 index d7c46790..00000000 --- a/libs/microbit-bluetooth/bluetooth.ts +++ /dev/null @@ -1,12 +0,0 @@ -//% blockId="delimiter_conv" block="%del" -export function delimiters(del : Delimiters) : string { - switch(del) { - case Delimiters.NewLine: return "\n"; - case Delimiters.Comma: return ","; - case Delimiters.Dollar: return "$"; - case Delimiters.Colon: return ":"; - case Delimiters.Fullstop: return "."; - case Delimiters.Hash: return "#"; - default: return "\n"; - } -} \ No newline at end of file diff --git a/libs/microbit-bluetooth/shims.d.ts b/libs/microbit-bluetooth/shims.d.ts index ff96c2b5..0606d684 100644 --- a/libs/microbit-bluetooth/shims.d.ts +++ b/libs/microbit-bluetooth/shims.d.ts @@ -49,13 +49,6 @@ declare namespace bluetooth { //% blockId=bluetooth_start_button_service block="bluetooth button service" blockGap=8 shim=bluetooth::startButtonService function startButtonService(): void; - /** - * Starts the Bluetooth UART service - */ - //% help=bluetooth/start-uart-service - //% blockId=bluetooth_start_uart_service block="bluetooth uart service" blockGap=8 shim=bluetooth::startUartService - function startUartService(): void; - /** * Writes to the Bluetooth UART service buffer. From there the data is transmitted over Bluetooth to a connected device. */ @@ -67,22 +60,29 @@ declare namespace bluetooth { * Reads from the Bluetooth UART service buffer, returning its contents when the specified delimiter character is encountered. */ //% help=bluetooth/uart-read - //% blockId=bluetooth_uart_read block="bluetooth uart read %del" blockGap=8 shim=bluetooth::uartRead + //% blockId=bluetooth_uart_read block="bluetooth uart read %del=bluetooth_uart_delimiter_conv" blockGap=8 shim=bluetooth::uartRead function uartRead(del: string): string; + /** + * Returns the delimiter corresponding string + */ + //% blockId="bluetooth_uart_delimiter_conv" block="%del" + //% weight=1 shim=bluetooth::delimiters + function delimiters(del: Delimiters): string; + /** * Register code to run when the micro:bit is connected to over Bluetooth * @param body Code to run when a Bluetooth connection is established */ - //% help=bluetooth/on-bluetooth-connected - //% blockId=bluetooth_on_connected block="on bluetooth connected" shim=bluetooth::onBluetoothConnected + //% help=bluetooth/on-bluetooth-connected weight=20 + //% blockId=bluetooth_on_connected block="on bluetooth connected" blockGap=8 shim=bluetooth::onBluetoothConnected function onBluetoothConnected(body: () => void): void; /** * Register code to run when a bluetooth connection to the micro:bit is lost * @param body Code to run when a Bluetooth connection is lost */ - //% help=bluetooth/on-bluetooth-disconnected + //% help=bluetooth/on-bluetooth-disconnected weight=19 //% blockId=bluetooth_on_disconnected block="on bluetooth disconnected" shim=bluetooth::onBluetoothDisconnected function onBluetoothDisconnected(body: () => void): void; } diff --git a/package.json b/package.json index 88b72e6f..eeaa273c 100644 --- a/package.json +++ b/package.json @@ -29,6 +29,6 @@ "typescript": "^1.8.7" }, "dependencies": { - "pxt-core": "0.2.179" + "pxt-core": "*" } }