From f2435df722315c2fbc8f791dab0a4a2039a88c4f Mon Sep 17 00:00:00 2001 From: Peli de Halleux Date: Fri, 5 Oct 2018 15:25:18 -0700 Subject: [PATCH] BLE UART Buffer methods (#1353) * adding simulator * attempt at handling size * correct ref counting * defense is on * handle read < 0 --- .../_locales/bluetooth-jsdoc-strings.json | 2 ++ libs/bluetooth/bluetooth.cpp | 33 +++++++++++++++++++ libs/bluetooth/shims.d.ts | 12 +++++++ sim/state/misc.ts | 9 +++++ 4 files changed, 56 insertions(+) diff --git a/libs/bluetooth/_locales/bluetooth-jsdoc-strings.json b/libs/bluetooth/_locales/bluetooth-jsdoc-strings.json index 861244b8..2b11b49c 100644 --- a/libs/bluetooth/_locales/bluetooth-jsdoc-strings.json +++ b/libs/bluetooth/_locales/bluetooth-jsdoc-strings.json @@ -29,7 +29,9 @@ "bluetooth.startTemperatureService": "Starts the Bluetooth temperature service", "bluetooth.startUartService": "Starts the Bluetooth UART service", "bluetooth.stopAdvertising": "Stops advertising Eddystone end points", + "bluetooth.uartReadBuffer": "Reads buffered UART data into a buffer", "bluetooth.uartReadUntil": "Reads from the Bluetooth UART service buffer, returning its contents when the specified delimiter character is encountered.", + "bluetooth.uartWriteBuffer": "Sends a buffer of data via Bluetooth UART", "bluetooth.uartWriteNumber": "Prints a numeric value to the serial", "bluetooth.uartWriteString": "Writes to the Bluetooth UART service buffer. From there the data is transmitted over Bluetooth to a connected device.", "bluetooth.uartWriteValue": "Writes a ``name: value`` pair line to the serial.", diff --git a/libs/bluetooth/bluetooth.cpp b/libs/bluetooth/bluetooth.cpp index aabb2e56..aa666e83 100644 --- a/libs/bluetooth/bluetooth.cpp +++ b/libs/bluetooth/bluetooth.cpp @@ -98,6 +98,39 @@ namespace bluetooth { return PSTR(uart->readUntil(MSTR(del))); } + + /** + * Sends a buffer of data via Bluetooth UART + */ + //% + void uartWriteBuffer(Buffer buffer) { + startUartService(); + uart->send(buffer->data, buffer->length); + } + + /** + * Reads buffered UART data into a buffer + */ + //% + Buffer uartReadBuffer() { + startUartService(); + int bytes = uart->rxBufferedSize(); + auto buffer = mkBuffer(NULL, bytes); + int read = uart->read(buffer->data, buffer->length); + // read failed + if (read < 0) { + decrRC(buffer); + return mkBuffer(NULL, 0); + } + // could not fill the buffer + if (read != buffer->length) { + auto tmp = mkBuffer(buffer->data, read); + decrRC(buffer); + buffer = tmp; + } + return buffer; + } + /** * Registers an event to be fired when one of the delimiter is matched. * @param delimiters the characters to match received characters against. diff --git a/libs/bluetooth/shims.d.ts b/libs/bluetooth/shims.d.ts index 1c8a9a9c..46f84bf0 100644 --- a/libs/bluetooth/shims.d.ts +++ b/libs/bluetooth/shims.d.ts @@ -63,6 +63,18 @@ declare namespace bluetooth { //% parts="bluetooth" advanced=true shim=bluetooth::startUartService function startUartService(): void; + /** + * Sends a buffer of data via Bluetooth UART + */ + //% shim=bluetooth::uartWriteBuffer + function uartWriteBuffer(buffer: Buffer): void; + + /** + * Reads buffered UART data into a buffer + */ + //% shim=bluetooth::uartReadBuffer + function uartReadBuffer(): Buffer; + /** * Registers an event to be fired when one of the delimiter is matched. * @param delimiters the characters to match received characters against. diff --git a/sim/state/misc.ts b/sim/state/misc.ts index c58f80f3..c1e4a099 100644 --- a/sim/state/misc.ts +++ b/sim/state/misc.ts @@ -208,6 +208,15 @@ namespace pxsim.bluetooth { export function uartWriteString(s: string): void { serial.writeString(s) } + + export function uartWriteBuffer(b: RefBuffer): void { + serial.writeBuffer(b); + } + + export function uartReadBuffer(): RefBuffer { + return pins.createBuffer(0); + } + export function uartReadUntil(del: string): string { return serial.readUntil(del); }