2020-09-08 11:04:25 +02:00
|
|
|
const enum Delimiters {
|
|
|
|
//% block="new line (\n)"
|
|
|
|
NewLine = 10,
|
|
|
|
//% block=","
|
|
|
|
Comma = 44,
|
|
|
|
//% block="$"
|
|
|
|
Dollar = 36,
|
|
|
|
//% block=":"
|
|
|
|
Colon = 58,
|
|
|
|
//% block="."
|
|
|
|
Fullstop = 46,
|
|
|
|
//% block="#"
|
|
|
|
Hash = 35,
|
|
|
|
//% block="carriage return (\r)"
|
|
|
|
CarriageReturn = 13,
|
|
|
|
//% block="space"
|
|
|
|
Space = 32,
|
|
|
|
//% block="tab (\t)"
|
|
|
|
Tab = 9,
|
|
|
|
//% block="|"
|
|
|
|
Pipe = 124,
|
|
|
|
//% block=";"
|
|
|
|
SemiColon = 59,
|
|
|
|
}
|
|
|
|
|
2016-04-06 00:59:25 +02:00
|
|
|
/**
|
|
|
|
* Reading and writing data over a serial connection.
|
|
|
|
*/
|
2017-01-20 05:55:31 +01:00
|
|
|
//% weight=2 color=#002050 icon="\uf287"
|
2016-10-11 22:48:25 +02:00
|
|
|
//% advanced=true
|
2016-03-10 23:01:04 +01:00
|
|
|
namespace serial {
|
|
|
|
/**
|
2019-12-02 05:58:26 +01:00
|
|
|
* The string used to mark a new line, default is \r\n
|
|
|
|
*/
|
|
|
|
export let NEW_LINE = "\r\n";
|
2020-09-08 11:04:25 +02:00
|
|
|
export let NEW_LINE_DELIMITER: Delimiters = Delimiters.NewLine;
|
2019-12-02 05:58:26 +01:00
|
|
|
let writeLinePadding = 32;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Print a line of text to the serial port
|
2016-03-10 23:01:04 +01:00
|
|
|
* @param value to send over serial
|
|
|
|
*/
|
2016-05-17 18:36:01 +02:00
|
|
|
//% weight=90
|
|
|
|
//% help=serial/write-line blockGap=8
|
2016-04-26 19:29:05 +02:00
|
|
|
//% blockId=serial_writeline block="serial|write line %text"
|
2019-12-02 05:58:26 +01:00
|
|
|
//% text.shadowOptions.toString=true
|
2016-03-10 23:01:04 +01:00
|
|
|
export function writeLine(text: string): void {
|
2019-12-02 05:58:26 +01:00
|
|
|
if (!text) text = "";
|
|
|
|
serial.writeString(text);
|
|
|
|
// pad data to the 32 byte boundary
|
|
|
|
// to ensure apps receive the packet
|
|
|
|
if (writeLinePadding > 0) {
|
|
|
|
let r = (writeLinePadding - (text.length + NEW_LINE.length) % writeLinePadding) % writeLinePadding;
|
|
|
|
for (let i = 0; i < r; ++i)
|
|
|
|
serial.writeString(" ");
|
|
|
|
}
|
|
|
|
serial.writeString(NEW_LINE);
|
2016-03-10 23:01:04 +01:00
|
|
|
}
|
|
|
|
|
2016-04-16 01:40:51 +02:00
|
|
|
/**
|
2019-12-02 05:58:26 +01:00
|
|
|
* Sets the padding length for lines sent with "write line".
|
|
|
|
* @param length the number of bytes alignment, eg: 0
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
//% weight=1
|
|
|
|
//% help=serial/set-write-line-padding
|
|
|
|
//% blockId=serialWriteNewLinePadding block="serial set write line padding to $length"
|
|
|
|
//% advanced=true
|
|
|
|
//% length.min=0 length.max=128
|
|
|
|
export function setWriteLinePadding(length: number) {
|
|
|
|
writeLinePadding = length | 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Print a numeric value to the serial port
|
2016-04-16 01:40:51 +02:00
|
|
|
*/
|
2016-05-17 18:36:01 +02:00
|
|
|
//% help=serial/write-number
|
|
|
|
//% weight=89 blockGap=8
|
2016-04-26 19:29:05 +02:00
|
|
|
//% blockId=serial_writenumber block="serial|write number %value"
|
2016-04-16 01:40:51 +02:00
|
|
|
export function writeNumber(value: number): void {
|
|
|
|
writeString(value.toString());
|
|
|
|
}
|
|
|
|
|
2016-03-10 23:01:04 +01:00
|
|
|
/**
|
2019-12-02 05:58:26 +01:00
|
|
|
* 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.
|
2016-03-10 23:01:04 +01:00
|
|
|
* @param name name of the value stream, eg: x
|
|
|
|
* @param value to write
|
|
|
|
*/
|
2016-05-17 18:36:01 +02:00
|
|
|
//% weight=88 blockGap=8
|
2016-04-22 21:37:47 +02:00
|
|
|
//% help=serial/write-value
|
2016-05-17 18:36:01 +02:00
|
|
|
//% blockId=serial_writevalue block="serial|write value %name|= %value"
|
2016-03-10 23:01:04 +01:00
|
|
|
export function writeValue(name: string, value: number): void {
|
2019-12-02 05:58:26 +01:00
|
|
|
writeLine((name ? name + ":" : "") + value);
|
2016-03-10 23:01:04 +01:00
|
|
|
}
|
2016-05-17 18:36:01 +02:00
|
|
|
|
|
|
|
/**
|
2019-12-02 05:58:26 +01:00
|
|
|
* Read a line of text from the serial port.
|
2017-01-10 19:26:44 +01:00
|
|
|
*/
|
|
|
|
//% help=serial/read-line
|
|
|
|
//% blockId=serial_read_line block="serial|read line"
|
|
|
|
//% weight=20 blockGap=8
|
|
|
|
export function readLine(): string {
|
2020-09-08 11:04:25 +02:00
|
|
|
return serial.readUntil(delimiters(NEW_LINE_DELIMITER));
|
2016-05-17 18:36:01 +02:00
|
|
|
}
|
2016-10-19 06:36:42 +02:00
|
|
|
|
|
|
|
/**
|
2019-12-02 05:58:26 +01:00
|
|
|
* Return the corresponding delimiter string
|
2016-10-19 06:36:42 +02:00
|
|
|
*/
|
|
|
|
//% blockId="serial_delimiter_conv" block="%del"
|
2017-01-10 19:26:44 +01:00
|
|
|
//% weight=1 blockHidden=true
|
2016-10-19 06:36:42 +02:00
|
|
|
export function delimiters(del: Delimiters): string {
|
2020-09-08 11:04:25 +02:00
|
|
|
return String.fromCharCode(del as number);
|
2016-10-19 06:36:42 +02:00
|
|
|
}
|
2016-03-10 23:01:04 +01:00
|
|
|
}
|