parent
f820c20af8
commit
9af989c13c
docs/reference
libs/bluetooth
sim/state
@ -30,6 +30,7 @@ bluetooth.uartReadUntil("");
|
||||
bluetooth.uartWriteString("");
|
||||
bluetooth.uartWriteNumber(0);
|
||||
bluetooth.uartWriteValue("", 0);
|
||||
bluetooth.onUartDataReceived(",", () => {})
|
||||
```
|
||||
|
||||
## Eddystone
|
||||
|
21
docs/reference/bluetooth/on-uart-data-received.md
Normal file
21
docs/reference/bluetooth/on-uart-data-received.md
Normal file
@ -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)))
|
||||
})
|
||||
```
|
@ -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",
|
||||
|
@ -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",
|
||||
|
@ -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
|
||||
|
8
libs/bluetooth/shims.d.ts
vendored
8
libs/bluetooth/shims.d.ts
vendored
@ -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
|
||||
|
@ -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
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user