add API to disable serial padding. (#2145)
* add API to desiable serial padding. * renamed api
This commit is contained in:
parent
9ca5a22106
commit
9eec76410a
@ -8,6 +8,7 @@ namespace serial {
|
|||||||
* The string used to mark a new line, default is \r\n
|
* The string used to mark a new line, default is \r\n
|
||||||
*/
|
*/
|
||||||
export let NEW_LINE = "\r\n";
|
export let NEW_LINE = "\r\n";
|
||||||
|
let writeLinePadding = 32;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Print a line of text to the serial port
|
* Print a line of text to the serial port
|
||||||
@ -19,15 +20,31 @@ namespace serial {
|
|||||||
//% text.shadowOptions.toString=true
|
//% text.shadowOptions.toString=true
|
||||||
export function writeLine(text: string): void {
|
export function writeLine(text: string): void {
|
||||||
if (!text) text = "";
|
if (!text) text = "";
|
||||||
|
serial.writeString(text);
|
||||||
// pad data to the 32 byte boundary
|
// pad data to the 32 byte boundary
|
||||||
// to ensure apps receive the packet
|
// to ensure apps receive the packet
|
||||||
let r = (32 - (text.length + 2) % 32) % 32;
|
if (writeLinePadding > 0) {
|
||||||
serial.writeString(text);
|
let r = (writeLinePadding - (text.length + NEW_LINE.length) % writeLinePadding) % writeLinePadding;
|
||||||
for (let i = 0; i < r; ++i)
|
for (let i = 0; i < r; ++i)
|
||||||
serial.writeString(" ");
|
serial.writeString(" ");
|
||||||
|
}
|
||||||
serial.writeString(NEW_LINE);
|
serial.writeString(NEW_LINE);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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
|
* Print a numeric value to the serial port
|
||||||
*/
|
*/
|
||||||
|
Loading…
Reference in New Issue
Block a user