Add dummy implementations for some bluetooth functions to avoid crashes

This commit is contained in:
Michal Moskal 2016-09-02 16:03:55 +01:00
parent 15fecb77c4
commit 9a2367cf8e
4 changed files with 46 additions and 46 deletions

View File

@ -101,38 +101,18 @@ namespace bluetooth {
uart = new MicroBitUARTService(*uBit.ble, 61, 60); uart = new MicroBitUARTService(*uBit.ble, 61, 60);
} }
/** //%
* 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
//% parts="bluetooth"
void uartWrite(StringData *data) { void uartWrite(StringData *data) {
startUartService(); startUartService();
uart->send(ManagedString(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=bluetooth_uart_delimiter_conv" blockGap=8
//% parts="bluetooth"
StringData* uartRead(StringData *del) { StringData* uartRead(StringData *del) {
startUartService(); 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
//% parts="bluetooth"
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

View File

@ -0,0 +1,43 @@
namespace bluetooth {
/**
* Returns the delimiter corresponding string
*/
//% blockId="bluetooth_uart_delimiter_conv" block="%del"
//% weight=1 parts="bluetooth"
export function delimiters(del: Delimiters): string {
// even though it might not look like, this is more
// (memory) efficient than the C++ implementation, because the
// strings are statically allocated and take no RAM
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"
}
}
/**
* 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
//% parts="bluetooth" shim=bluetooth::uartWrite
export function uartWrite(data: string): void {
// dummy implementation for simulator
console.log("UART Write: " + 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=bluetooth_uart_delimiter_conv" blockGap=8
//% parts="bluetooth" shim=bluetooth::uartRead
export function uartRead(del: string): string {
// dummy implementation for simulator
return "???"
}
}

View File

@ -5,6 +5,7 @@
"README.md", "README.md",
"enums.d.ts", "enums.d.ts",
"shims.d.ts", "shims.d.ts",
"bluetooth.ts",
"bluetooth.cpp" "bluetooth.cpp"
], ],
"public": true, "public": true,

View File

@ -63,30 +63,6 @@ declare namespace bluetooth {
//% parts="bluetooth" shim=bluetooth::startUartService //% parts="bluetooth" shim=bluetooth::startUartService
function startUartService(): void; function startUartService(): void;
/**
* 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
//% parts="bluetooth" 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=bluetooth_uart_delimiter_conv" blockGap=8
//% parts="bluetooth" shim=bluetooth::uartRead
function uartRead(del: string): string;
/**
* Returns the delimiter corresponding string
*/
//% blockId="bluetooth_uart_delimiter_conv" block="%del"
//% weight=1
//% parts="bluetooth" 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