diff --git a/libs/microbit-bluetooth/bluetooth.cpp b/libs/microbit-bluetooth/bluetooth.cpp index da9b4eeb..890f2f2c 100644 --- a/libs/microbit-bluetooth/bluetooth.cpp +++ b/libs/microbit-bluetooth/bluetooth.cpp @@ -101,38 +101,18 @@ namespace bluetooth { uart = new MicroBitUARTService(*uBit.ble, 61, 60); } - /** - * Writes to the Bluetooth UART service buffer. From there the data is transmitted over Bluetooth to a connected device. - */ - //% help=bluetooth/uart-write - //% blockId=bluetooth_uart_write block="bluetooth uart write %data" blockGap=8 - //% parts="bluetooth" + //% void uartWrite(StringData *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=bluetooth_uart_delimiter_conv" blockGap=8 - //% parts="bluetooth" + //% 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 - //% parts="bluetooth" - 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 diff --git a/libs/microbit-bluetooth/bluetooth.ts b/libs/microbit-bluetooth/bluetooth.ts new file mode 100644 index 00000000..05144086 --- /dev/null +++ b/libs/microbit-bluetooth/bluetooth.ts @@ -0,0 +1,43 @@ +namespace bluetooth { + /** + * Returns the delimiter corresponding string + */ + //% blockId="bluetooth_uart_delimiter_conv" block="%del" + //% weight=1 parts="bluetooth" + export function delimiters(del: Delimiters): string { + // even though it might not look like, this is more + // (memory) efficient than the C++ implementation, because the + // strings are statically allocated and take no RAM + 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" + } + } + + /** + * Writes to the Bluetooth UART service buffer. From there the data is transmitted over Bluetooth to a connected device. + */ + //% help=bluetooth/uart-write + //% blockId=bluetooth_uart_write block="bluetooth uart write %data" blockGap=8 + //% parts="bluetooth" shim=bluetooth::uartWrite + export function uartWrite(data: string): void { + // dummy implementation for simulator + console.log("UART Write: " + 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=bluetooth_uart_delimiter_conv" blockGap=8 + //% parts="bluetooth" shim=bluetooth::uartRead + export function uartRead(del: string): string { + // dummy implementation for simulator + return "???" + } +} diff --git a/libs/microbit-bluetooth/pxt.json b/libs/microbit-bluetooth/pxt.json index bfa289f0..e486d262 100644 --- a/libs/microbit-bluetooth/pxt.json +++ b/libs/microbit-bluetooth/pxt.json @@ -5,6 +5,7 @@ "README.md", "enums.d.ts", "shims.d.ts", + "bluetooth.ts", "bluetooth.cpp" ], "public": true, diff --git a/libs/microbit-bluetooth/shims.d.ts b/libs/microbit-bluetooth/shims.d.ts index f76076f1..31c8e34b 100644 --- a/libs/microbit-bluetooth/shims.d.ts +++ b/libs/microbit-bluetooth/shims.d.ts @@ -63,30 +63,6 @@ declare namespace bluetooth { //% parts="bluetooth" 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. - */ - //% help=bluetooth/uart-write - //% blockId=bluetooth_uart_write block="bluetooth uart write %data" blockGap=8 - //% parts="bluetooth" shim=bluetooth::uartWrite - function uartWrite(data: string): void; - - /** - * 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=bluetooth_uart_delimiter_conv" blockGap=8 - //% parts="bluetooth" shim=bluetooth::uartRead - function uartRead(del: string): string; - - /** - * Returns the delimiter corresponding string - */ - //% blockId="bluetooth_uart_delimiter_conv" block="%del" - //% weight=1 - //% parts="bluetooth" 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