2016-04-06 00:59:25 +02:00
|
|
|
/**
|
|
|
|
* Reading and writing data over a serial connection.
|
|
|
|
*/
|
2016-05-19 20:59:57 +02:00
|
|
|
//% weight=2 color=#002050
|
2016-10-11 22:48:25 +02:00
|
|
|
//% advanced=true
|
2016-03-10 23:01:04 +01:00
|
|
|
namespace serial {
|
|
|
|
/**
|
|
|
|
* Prints a line of text to the serial
|
|
|
|
* @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"
|
2016-03-10 23:01:04 +01:00
|
|
|
export function writeLine(text: string): void {
|
2016-05-18 20:57:10 +02:00
|
|
|
writeString(text + "\r\n");
|
2016-03-10 23:01:04 +01:00
|
|
|
}
|
|
|
|
|
2016-04-16 01:40:51 +02:00
|
|
|
/**
|
|
|
|
* Prints a numeric value to the serial
|
|
|
|
*/
|
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
|
|
|
/**
|
|
|
|
* Writes a ``name: value`` pair line to the serial.
|
|
|
|
* @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 {
|
2016-05-18 20:57:10 +02:00
|
|
|
writeString(name + ":" + value + "\r\n");
|
2016-03-10 23:01:04 +01:00
|
|
|
}
|
2016-05-17 18:36:01 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Registers an event to be fired when a line has been received
|
|
|
|
*/
|
2016-05-19 22:39:28 +02:00
|
|
|
// help=serial/on-line-received
|
|
|
|
// blockId=serial_on_line_received block="serial on line received"
|
|
|
|
// weight=21 blockGap=8
|
2016-05-17 18:36:01 +02:00
|
|
|
export function onLineReceived(body: Action): void {
|
2016-05-19 22:39:28 +02:00
|
|
|
// serial.onDataReceived("\n", body);
|
2016-05-17 18:36:01 +02:00
|
|
|
}
|
2016-10-19 06:36:42 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the delimiter corresponding string
|
|
|
|
*/
|
|
|
|
//% blockId="serial_delimiter_conv" block="%del"
|
|
|
|
//% weight=1
|
|
|
|
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"
|
|
|
|
}
|
|
|
|
}
|
2016-03-10 23:01:04 +01:00
|
|
|
}
|