pxt-calliope/libs/core/serial.ts
Peli de Halleux 3477d40ce0
uart write sim support (#1933)
* pass data to uart without prefix

* added NEW_LINE support
2019-03-21 10:22:24 -07:00

98 lines
3.1 KiB
TypeScript

/**
* Reading and writing data over a serial connection.
*/
//% weight=2 color=#002050 icon="\uf287"
//% advanced=true
namespace serial {
/**
* The string used to mark a new line, default is \r\n
*/
export let NEW_LINE = "\r\n";
/**
* Print a line of text to the serial port
* @param value to send over serial
*/
//% weight=90
//% help=serial/write-line blockGap=8
//% blockId=serial_writeline block="serial|write line %text"
//% text.shadowOptions.toString=true
export function writeLine(text: string): void {
if (!text) text = "";
// pad data to the 32 byte boundary
// to ensure apps receive the packet
let r = (32 - (text.length + 2) % 32) % 32;
serial.writeString(text);
for (let i = 0; i < r; ++i)
serial.writeString(" ");
serial.writeString(NEW_LINE);
}
/**
* Print a numeric value to the serial port
*/
//% help=serial/write-number
//% weight=89 blockGap=8
//% blockId=serial_writenumber block="serial|write number %value"
export function writeNumber(value: number): void {
writeString(value.toString());
}
/**
* Print an array of numeric values as CSV to the serial port
*/
//% help=serial/write-numbers
//% weight=86
//% blockId=serial_writenumbers block="serial|write numbers %values"
export function writeNumbers(values: number[]): void {
if (!values) return;
for(let i = 0; i < values.length; ++i) {
if (i > 0) writeString(",");
writeNumber(values[i]);
}
writeLine("")
}
/**
* Write a name:value pair as a line to the serial port.
* @param name name of the value stream, eg: x
* @param value to write
*/
//% weight=88 blockGap=8
//% help=serial/write-value
//% blockId=serial_writevalue block="serial|write value %name|= %value"
export function writeValue(name: string, value: number): void {
writeLine((name ? name + ":" : "") + value);
}
/**
* Read a line of text from the serial port.
*/
//% help=serial/read-line
//% blockId=serial_read_line block="serial|read line"
//% weight=20 blockGap=8
export function readLine(): string {
return serial.readUntil(delimiters(Delimiters.NewLine));
}
/**
* Return the corresponding delimiter string
*/
//% blockId="serial_delimiter_conv" block="%del"
//% weight=1 blockHidden=true
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"
}
}
}