patching up bluetooth uart
This commit is contained in:
parent
660b22b398
commit
6cfe39dac3
@ -1,7 +1,6 @@
|
|||||||
#include "pxt.h"
|
#include "pxt.h"
|
||||||
#include "MESEvents.h"
|
#include "MESEvents.h"
|
||||||
#include "MicroBitUARTService.h"
|
#include "MicroBitUARTService.h"
|
||||||
MicroBitUARTService *uart;
|
|
||||||
|
|
||||||
using namespace pxt;
|
using namespace pxt;
|
||||||
|
|
||||||
@ -26,6 +25,8 @@ enum Delimiters {
|
|||||||
*/
|
*/
|
||||||
//% color=#0082FB weight=20
|
//% color=#0082FB weight=20
|
||||||
namespace bluetooth {
|
namespace bluetooth {
|
||||||
|
MicroBitUARTService *uart = NULL;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Starts the Bluetooth IO pin service.
|
* Starts the Bluetooth IO pin service.
|
||||||
*/
|
*/
|
||||||
@ -83,10 +84,10 @@ namespace bluetooth {
|
|||||||
/**
|
/**
|
||||||
* Starts the Bluetooth UART service
|
* Starts the Bluetooth UART service
|
||||||
*/
|
*/
|
||||||
//% help=bluetooth/start-uart-service
|
// help=bluetooth/start-uart-service
|
||||||
//% blockId=bluetooth_start_uart_service block="bluetooth uart service" blockGap=8
|
// blockId=bluetooth_start_uart_service block="bluetooth uart service" blockGap=8
|
||||||
|
|
||||||
void startUartService() {
|
void startUartService() {
|
||||||
|
if (uart) return;
|
||||||
// 61 octet buffer size is 3 x (MTU - 3) + 1
|
// 61 octet buffer size is 3 x (MTU - 3) + 1
|
||||||
// MTU on nRF51822 is 23 octets. 3 are used by Attribute Protocol header data leaving 20 octets for payload
|
// MTU on nRF51822 is 23 octets. 3 are used by Attribute Protocol header data leaving 20 octets for payload
|
||||||
// So we allow a RX buffer that can contain 3 x max length messages plus one octet for a terminator character
|
// So we allow a RX buffer that can contain 3 x max length messages plus one octet for a terminator character
|
||||||
@ -99,24 +100,35 @@ namespace bluetooth {
|
|||||||
//% help=bluetooth/uart-write
|
//% 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) {
|
void uartWrite(StringData *data) {
|
||||||
uart->send(ManagedString(data));
|
startUartService();
|
||||||
|
uart->send(ManagedString(data));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Reads from the Bluetooth UART service buffer, returning its contents when the specified delimiter character is encountered.
|
* Reads from the Bluetooth UART service buffer, returning its contents when the specified delimiter character is encountered.
|
||||||
*/
|
*/
|
||||||
//% help=bluetooth/uart-read
|
//% help=bluetooth/uart-read
|
||||||
//% blockId=bluetooth_uart_read block="bluetooth uart read %del" blockGap=8
|
//% blockId=bluetooth_uart_read block="bluetooth uart read %del=bluetooth_uart_delimiter_conv" blockGap=8
|
||||||
StringData* uartRead(StringData *del) {
|
StringData* uartRead(StringData *del) {
|
||||||
|
startUartService();
|
||||||
return uart->readUntil(ManagedString(del)).leakData();
|
return uart->readUntil(ManagedString(del)).leakData();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the delimiter corresponding string
|
||||||
|
*/
|
||||||
|
//% blockId="bluetooth_uart_delimiter_conv" block="%del"
|
||||||
|
//% weight=1
|
||||||
|
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
|
* Register code to run when the micro:bit is connected to over Bluetooth
|
||||||
* @param body Code to run when a Bluetooth connection is established
|
* @param body Code to run when a Bluetooth connection is established
|
||||||
*/
|
*/
|
||||||
//% help=bluetooth/on-bluetooth-connected
|
//% help=bluetooth/on-bluetooth-connected weight=20
|
||||||
//% blockId=bluetooth_on_connected block="on bluetooth connected"
|
//% blockId=bluetooth_on_connected block="on bluetooth connected" blockGap=8
|
||||||
void onBluetoothConnected(Action body) {
|
void onBluetoothConnected(Action body) {
|
||||||
registerWithDal(MICROBIT_ID_BLE, MICROBIT_BLE_EVT_CONNECTED, body);
|
registerWithDal(MICROBIT_ID_BLE, MICROBIT_BLE_EVT_CONNECTED, body);
|
||||||
}
|
}
|
||||||
@ -125,7 +137,7 @@ namespace bluetooth {
|
|||||||
* Register code to run when a bluetooth connection to the micro:bit is lost
|
* Register code to run when a bluetooth connection to the micro:bit is lost
|
||||||
* @param body Code to run when a Bluetooth connection is lost
|
* @param body Code to run when a Bluetooth connection is lost
|
||||||
*/
|
*/
|
||||||
//% help=bluetooth/on-bluetooth-disconnected
|
//% help=bluetooth/on-bluetooth-disconnected weight=19
|
||||||
//% blockId=bluetooth_on_disconnected block="on bluetooth disconnected"
|
//% blockId=bluetooth_on_disconnected block="on bluetooth disconnected"
|
||||||
void onBluetoothDisconnected(Action body) {
|
void onBluetoothDisconnected(Action body) {
|
||||||
registerWithDal(MICROBIT_ID_BLE, MICROBIT_BLE_EVT_DISCONNECTED, body);
|
registerWithDal(MICROBIT_ID_BLE, MICROBIT_BLE_EVT_DISCONNECTED, body);
|
||||||
|
@ -1,12 +0,0 @@
|
|||||||
//% 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";
|
|
||||||
}
|
|
||||||
}
|
|
22
libs/microbit-bluetooth/shims.d.ts
vendored
22
libs/microbit-bluetooth/shims.d.ts
vendored
@ -49,13 +49,6 @@ declare namespace bluetooth {
|
|||||||
//% blockId=bluetooth_start_button_service block="bluetooth button service" blockGap=8 shim=bluetooth::startButtonService
|
//% blockId=bluetooth_start_button_service block="bluetooth button service" blockGap=8 shim=bluetooth::startButtonService
|
||||||
function startButtonService(): void;
|
function startButtonService(): void;
|
||||||
|
|
||||||
/**
|
|
||||||
* Starts the Bluetooth UART service
|
|
||||||
*/
|
|
||||||
//% help=bluetooth/start-uart-service
|
|
||||||
//% blockId=bluetooth_start_uart_service block="bluetooth uart service" blockGap=8 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.
|
* Writes to the Bluetooth UART service buffer. From there the data is transmitted over Bluetooth to a connected device.
|
||||||
*/
|
*/
|
||||||
@ -67,22 +60,29 @@ declare namespace bluetooth {
|
|||||||
* Reads from the Bluetooth UART service buffer, returning its contents when the specified delimiter character is encountered.
|
* Reads from the Bluetooth UART service buffer, returning its contents when the specified delimiter character is encountered.
|
||||||
*/
|
*/
|
||||||
//% help=bluetooth/uart-read
|
//% help=bluetooth/uart-read
|
||||||
//% blockId=bluetooth_uart_read block="bluetooth uart read %del" blockGap=8 shim=bluetooth::uartRead
|
//% blockId=bluetooth_uart_read block="bluetooth uart read %del=bluetooth_uart_delimiter_conv" blockGap=8 shim=bluetooth::uartRead
|
||||||
function uartRead(del: string): string;
|
function uartRead(del: string): string;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the delimiter corresponding string
|
||||||
|
*/
|
||||||
|
//% blockId="bluetooth_uart_delimiter_conv" block="%del"
|
||||||
|
//% weight=1 shim=bluetooth::delimiters
|
||||||
|
function delimiters(del: Delimiters): string;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Register code to run when the micro:bit is connected to over Bluetooth
|
* Register code to run when the micro:bit is connected to over Bluetooth
|
||||||
* @param body Code to run when a Bluetooth connection is established
|
* @param body Code to run when a Bluetooth connection is established
|
||||||
*/
|
*/
|
||||||
//% help=bluetooth/on-bluetooth-connected
|
//% help=bluetooth/on-bluetooth-connected weight=20
|
||||||
//% blockId=bluetooth_on_connected block="on bluetooth connected" shim=bluetooth::onBluetoothConnected
|
//% blockId=bluetooth_on_connected block="on bluetooth connected" blockGap=8 shim=bluetooth::onBluetoothConnected
|
||||||
function onBluetoothConnected(body: () => void): void;
|
function onBluetoothConnected(body: () => void): void;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Register code to run when a bluetooth connection to the micro:bit is lost
|
* Register code to run when a bluetooth connection to the micro:bit is lost
|
||||||
* @param body Code to run when a Bluetooth connection is lost
|
* @param body Code to run when a Bluetooth connection is lost
|
||||||
*/
|
*/
|
||||||
//% help=bluetooth/on-bluetooth-disconnected
|
//% help=bluetooth/on-bluetooth-disconnected weight=19
|
||||||
//% blockId=bluetooth_on_disconnected block="on bluetooth disconnected" shim=bluetooth::onBluetoothDisconnected
|
//% blockId=bluetooth_on_disconnected block="on bluetooth disconnected" shim=bluetooth::onBluetoothDisconnected
|
||||||
function onBluetoothDisconnected(body: () => void): void;
|
function onBluetoothDisconnected(body: () => void): void;
|
||||||
}
|
}
|
||||||
|
@ -29,6 +29,6 @@
|
|||||||
"typescript": "^1.8.7"
|
"typescript": "^1.8.7"
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"pxt-core": "0.2.179"
|
"pxt-core": "*"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user