diff --git a/libs/microbit-bluetooth/bluetooth.cpp b/libs/microbit-bluetooth/bluetooth.cpp index 23462190..49d275bd 100644 --- a/libs/microbit-bluetooth/bluetooth.cpp +++ b/libs/microbit-bluetooth/bluetooth.cpp @@ -97,11 +97,20 @@ namespace bluetooth { * 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 + //% blockId=bluetooth_uart_write block="bluetooth uart write %data" blockGap=8 void uartWrite(StringData *data) { 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 + StringData* uartRead(StringData *del) { + return uart->readUntil(ManagedString(del)).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 100755 index 00000000..d7c46790 --- /dev/null +++ b/libs/microbit-bluetooth/bluetooth.ts @@ -0,0 +1,12 @@ +//% 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 4dba2871..ff96c2b5 100644 --- a/libs/microbit-bluetooth/shims.d.ts +++ b/libs/microbit-bluetooth/shims.d.ts @@ -60,9 +60,16 @@ declare namespace bluetooth { * 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 shim=bluetooth::uartWrite + //% blockId=bluetooth_uart_write block="bluetooth uart write %data" blockGap=8 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" blockGap=8 shim=bluetooth::uartRead + function uartRead(del: string): 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