diff --git a/docs/reference/bluetooth.md b/docs/reference/bluetooth.md index d14a59c9..e7ee8dce 100644 --- a/docs/reference/bluetooth.md +++ b/docs/reference/bluetooth.md @@ -30,6 +30,7 @@ bluetooth.uartReadUntil(""); bluetooth.uartWriteString(""); bluetooth.uartWriteNumber(0); bluetooth.uartWriteValue("", 0); +bluetooth.onUartDataReceived(",", () => {}) ``` ## Eddystone diff --git a/docs/reference/bluetooth/on-uart-data-received.md b/docs/reference/bluetooth/on-uart-data-received.md new file mode 100644 index 00000000..b58df0ba --- /dev/null +++ b/docs/reference/bluetooth/on-uart-data-received.md @@ -0,0 +1,21 @@ +# Bluetooth On UART Data Received + +Registers an event to be fired when one of the delimiter is matched. + +```sig +bluetooth.onUartDataReceived(",", () => {}) +``` + +### Parameters + +* `delimiters` is a [string](/types/string) containing any of the character to match + +### Example + +Read values separated by `,`: + +```blocks +bluetooth.onUartDataReceived(serial.delimiters(Delimiters.Comma), () => { + basic.showString(serial.readUntil(serial.delimiters(Delimiters.Comma))) +}) +``` diff --git a/libs/bluetooth/_locales/bluetooth-jsdoc-strings.json b/libs/bluetooth/_locales/bluetooth-jsdoc-strings.json index 258674fa..861244b8 100644 --- a/libs/bluetooth/_locales/bluetooth-jsdoc-strings.json +++ b/libs/bluetooth/_locales/bluetooth-jsdoc-strings.json @@ -17,6 +17,8 @@ "bluetooth.onBluetoothConnected|param|body": "Code to run when a Bluetooth connection is established", "bluetooth.onBluetoothDisconnected": "Register code to run when a bluetooth connection to the micro:bit is lost", "bluetooth.onBluetoothDisconnected|param|body": "Code to run when a Bluetooth connection is lost", + "bluetooth.onUartDataReceived": "Registers an event to be fired when one of the delimiter is matched.", + "bluetooth.onUartDataReceived|param|delimiters": "the characters to match received characters against.", "bluetooth.setTransmitPower": "Sets the bluetooth transmit power between 0 (minimal) and 7 (maximum).", "bluetooth.setTransmitPower|param|power": "power level between 0 (minimal) and 7 (maximum), eg: 7.", "bluetooth.startAccelerometerService": "Starts the Bluetooth accelerometer service", diff --git a/libs/bluetooth/_locales/bluetooth-strings.json b/libs/bluetooth/_locales/bluetooth-strings.json index cde5e714..bf68a2dc 100644 --- a/libs/bluetooth/_locales/bluetooth-strings.json +++ b/libs/bluetooth/_locales/bluetooth-strings.json @@ -3,6 +3,7 @@ "bluetooth.advertiseUrl|block": "bluetooth advertise url %url|with power %power|connectable %connectable", "bluetooth.onBluetoothConnected|block": "on bluetooth connected", "bluetooth.onBluetoothDisconnected|block": "on bluetooth disconnected", + "bluetooth.onUartDataReceived|block": "bluetooth|on data received %delimiters=serial_delimiter_conv", "bluetooth.setTransmitPower|block": "bluetooth set transmit power %power", "bluetooth.startAccelerometerService|block": "bluetooth accelerometer service", "bluetooth.startButtonService|block": "bluetooth button service", diff --git a/libs/bluetooth/bluetooth.cpp b/libs/bluetooth/bluetooth.cpp index f04d6243..effd4f2a 100644 --- a/libs/bluetooth/bluetooth.cpp +++ b/libs/bluetooth/bluetooth.cpp @@ -98,6 +98,18 @@ namespace bluetooth { return uart->readUntil(ManagedString(del)).leakData(); } + /** + * Registers an event to be fired when one of the delimiter is matched. + * @param delimiters the characters to match received characters against. + */ + //% help=bluetooth/on-uart-data-received + //% weight=18 blockId=bluetooth_on_data_received block="bluetooth|on data received %delimiters=serial_delimiter_conv" + void onUartDataReceived(StringData* delimiters, Action body) { + startUartService(); + uart->eventOn(ManagedString(delimiters)); + registerWithDal(MICROBIT_ID_BLE_UART, MICROBIT_UART_S_EVT_DELIM_MATCH, body); + } + /** * 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/bluetooth/shims.d.ts b/libs/bluetooth/shims.d.ts index 3dfcf726..5e35e3b7 100644 --- a/libs/bluetooth/shims.d.ts +++ b/libs/bluetooth/shims.d.ts @@ -63,6 +63,14 @@ declare namespace bluetooth { //% parts="bluetooth" advanced=true shim=bluetooth::startUartService function startUartService(): void; + /** + * Registers an event to be fired when one of the delimiter is matched. + * @param delimiters the characters to match received characters against. + */ + //% help=bluetooth/on-uart-data-received + //% weight=18 blockId=bluetooth_on_data_received block="bluetooth|on data received %delimiters=serial_delimiter_conv" shim=bluetooth::onUartDataReceived + function onUartDataReceived(delimiters: string, body: () => void): void; + /** * 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/sim/state/misc.ts b/sim/state/misc.ts index a1014af3..18b51805 100644 --- a/sim/state/misc.ts +++ b/sim/state/misc.ts @@ -175,6 +175,10 @@ namespace pxsim.bluetooth { export function uartReadUntil(del: string): string { return serial.readUntil(del); } + export function onDataReceived(delimiters: string, handler: RefAction) { + let b = board(); + b.bus.listen(DAL.MICROBIT_ID_BLE_UART, DAL.MICROBIT_UART_S_EVT_DELIM_MATCH, handler); + } export function onBluetoothConnected(a: RefAction) { // TODO }