Uartevent (#393)

* support for uart data event

* updated shims
This commit is contained in:
Peli de Halleux 2017-04-28 01:05:04 -07:00 committed by GitHub
parent f820c20af8
commit 9af989c13c
7 changed files with 49 additions and 0 deletions

View File

@ -30,6 +30,7 @@ bluetooth.uartReadUntil("");
bluetooth.uartWriteString("");
bluetooth.uartWriteNumber(0);
bluetooth.uartWriteValue("", 0);
bluetooth.onUartDataReceived(",", () => {})
```
## Eddystone

View 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)))
})
```

View File

@ -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",

View File

@ -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",

View File

@ -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

View File

@ -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

View File

@ -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
}