support for serial read/write buffer (#384)

This commit is contained in:
Peli de Halleux 2017-04-21 09:02:29 -07:00 committed by GitHub
parent 43bad888dc
commit 17c1abe86a
5 changed files with 76 additions and 1 deletions

View File

@ -10,10 +10,17 @@ serial.writeString("");
serial.readUntil(","); serial.readUntil(",");
serial.readLine(); serial.readLine();
serial.readString(); serial.readString();
serial.redirect(SerialPin.P0, SerialPin.P0, BaudRate.BaudRate115200);
serial.onDataReceived(",", () => {}) serial.onDataReceived(",", () => {})
``` ```
### Advanced
```cards
serial.redirect(SerialPin.P0, SerialPin.P0, BaudRate.BaudRate115200);
serial.writeBuffer(pins.createBuffer(0));
serial.readBuffer(64);
```
### See Also ### See Also
[writeLine](/reference/serial/write-line), [writeNumber](/reference/serial/write-number), [writeValue](/reference/serial/write-value), [writeString](/reference/serial/write-string), [readLine](/reference/serial/read-line), [redirect](/reference/serial/redirect-to) [writeLine](/reference/serial/write-line), [writeNumber](/reference/serial/write-number), [writeValue](/reference/serial/write-value), [writeString](/reference/serial/write-string), [readLine](/reference/serial/read-line), [redirect](/reference/serial/redirect-to)

View File

@ -0,0 +1,12 @@
# Serial Read Buffer
Read the buffered serial data as a buffer
```sig
serial.readBuffer(64);
```
### Returns
* a Buffer containing input from the serial port. Empty if no data available.

View File

@ -0,0 +1,11 @@
# Serial Write Buffer
Write a buffer to the [serial](/device/serial) port.
```sig
serial.writeBuffer(pins.createBuffer(0));
```
### Parameters
* `buffer` is the buffer to write to the serial port

View File

@ -1,5 +1,7 @@
#include "pxt.h" #include "pxt.h"
#define MICROBIT_SERIAL_READ_BUFFER_LENGTH 64
enum SerialPin { enum SerialPin {
P0 = MICROBIT_ID_IO_P0, P0 = MICROBIT_ID_IO_P0,
P1 = MICROBIT_ID_IO_P1, P1 = MICROBIT_ID_IO_P1,
@ -101,9 +103,39 @@ namespace serial {
//% weight=87 //% weight=87
//% blockId=serial_writestring block="serial|write string %text" //% blockId=serial_writestring block="serial|write string %text"
void writeString(StringData *text) { void writeString(StringData *text) {
if (!text) return;
uBit.serial.send(ManagedString(text)); uBit.serial.send(ManagedString(text));
} }
/**
* Sends a buffer through Serial connection
*/
//% help=serial/write-buffer advanced=true weight=6
void writeBuffer(Buffer buffer) {
if (!buffer) return;
ManagedBuffer buf(buffer);
uBit.serial.send(buf.getBytes(), buf.length());
}
/**
* Reads multiple characters from the rxBuff and fills a user buffer.
* @param length default buffer length, eg: 64
*/
//% help=serial/read-buffer advanced=true weight=5
Buffer readBuffer(int length) {
if (length <= 0)
length = MICROBIT_SERIAL_READ_BUFFER_LENGTH;
ManagedBuffer buf(length);
int read = uBit.serial.read(buf.getBytes(), buf.length());
if (read != buf.length())
buf = buf.slice(read);
return buf.leakData();
}
/** /**
* Dynamically configuring the serial instance to use pins other than USBTX and USBRX. * Dynamically configuring the serial instance to use pins other than USBTX and USBRX.
* @param tx the new transmission pins, eg: SerialPin.P0 * @param tx the new transmission pins, eg: SerialPin.P0

13
libs/core/shims.d.ts vendored
View File

@ -714,6 +714,19 @@ declare namespace serial {
//% blockId=serial_writestring block="serial|write string %text" shim=serial::writeString //% blockId=serial_writestring block="serial|write string %text" shim=serial::writeString
function writeString(text: string): void; function writeString(text: string): void;
/**
* Sends a buffer through Serial connection
*/
//% help=serial/write-buffer advanced=true weight=6 shim=serial::writeBuffer
function writeBuffer(buffer: Buffer): void;
/**
* Reads multiple characters from the rxBuff and fills a user buffer.
* @param length default buffer length, eg: 64
*/
//% help=serial/read-buffer advanced=true weight=5 shim=serial::readBuffer
function readBuffer(length: number): Buffer;
/** /**
* Dynamically configuring the serial instance to use pins other than USBTX and USBRX. * Dynamically configuring the serial instance to use pins other than USBTX and USBRX.
* @param tx the new transmission pins, eg: SerialPin.P0 * @param tx the new transmission pins, eg: SerialPin.P0