Add format to the print
This commit is contained in:
Родитель
d76445f128
Коммит
a257e2cb40
|
@ -10,10 +10,11 @@ namespace dataStreamer {
|
|||
|
||||
/**
|
||||
* Set the USB baud rate connection.
|
||||
* @param rate the new baud rate. eg: 115200
|
||||
* @param rate the new baud rate.
|
||||
*/
|
||||
//% weight=10
|
||||
//% blockId=datastreamer_setBaud block="set baud rate %rate"
|
||||
//% rate.defl=9600
|
||||
|
||||
void setBaudRate(int rate) {
|
||||
uBit.serial.baud(rate);
|
||||
|
|
40
extension.ts
40
extension.ts
|
@ -6,11 +6,32 @@ namespace dataStreamer {
|
|||
/**
|
||||
* Print a numeric value to the serial port
|
||||
* @param value to send over serial
|
||||
* @param decimalDigits (optional) unit of the value
|
||||
*/
|
||||
//% blockId=datastreamer_writeNumber block="write number %value"
|
||||
//% blockId=datastreamer_writeNumber block="write number %value|\\%||format %decimalDigits"
|
||||
//% weight=30
|
||||
export function writeNumber(value: number) {
|
||||
serial.writeNumber(value)
|
||||
//% decimalDigits.defl=2
|
||||
//% expandableArgumentMode=toggle
|
||||
|
||||
export function writeNumber(value: number, decimalDigits: number) {
|
||||
//Format to right number of decimalDigits
|
||||
let text: string = value.toString();
|
||||
let len = 0; //Tracks the subscrtring length
|
||||
|
||||
for (;len < text.length; len++) { //Find the .
|
||||
if (text[len] == ".") { break; }
|
||||
}
|
||||
|
||||
if (decimalDigits != 0) {
|
||||
len++ //Account for .
|
||||
if (len + decimalDigits < text.length) {
|
||||
len = len + decimalDigits;
|
||||
} else {
|
||||
len = text.length;
|
||||
}
|
||||
}
|
||||
|
||||
serial.writeString(text.substr(0, len))
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -36,12 +57,17 @@ namespace dataStreamer {
|
|||
/**
|
||||
* Print an array of numeric values as CSV to the serial port
|
||||
*/
|
||||
//% blockId=datastreamer_writenumbers block="write comma seperated numbers %values"
|
||||
//% blockId=datastreamer_writenumbers block="write array as comma seperated numbers %values|\\%||format %decimalDigits"
|
||||
//% weight=10
|
||||
export function writeNumbers(values: number[]): void {
|
||||
//% values.shadow="lists_create_with"
|
||||
//% decimalDigits.defl=2
|
||||
//% expandableArgumentMode=toggle
|
||||
export function writeNumbers(values: number[], decimalDigits: number): void {
|
||||
for (let i = 0; i < values.length; i++) {
|
||||
serial.writeNumber(values[i])
|
||||
serial.writeLine(",")
|
||||
dataStreamer.writeNumber(values[i], decimalDigits)
|
||||
if ((i + 1) < values.length) {
|
||||
dataStreamer.writeString(",")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -3,11 +3,12 @@ declare namespace dataStreamer {
|
|||
|
||||
/**
|
||||
* Set the USB baud rate connection.
|
||||
* @param rate the new baud rate. eg: 115200
|
||||
* @param rate the new baud rate.
|
||||
*/
|
||||
//% weight=10
|
||||
//% blockId=datastreamer_setBaud block="set baud rate %rate" shim=dataStreamer::setBaudRate
|
||||
function setBaudRate(rate: int32): void;
|
||||
//% blockId=datastreamer_setBaud block="set baud rate %rate"
|
||||
//% rate.defl=9600 shim=dataStreamer::setBaudRate
|
||||
function setBaudRate(rate?: int32): void;
|
||||
}
|
||||
|
||||
// Auto-generated. Do not edit. Really.
|
||||
|
|
20
test.ts
20
test.ts
|
@ -1,18 +1,18 @@
|
|||
dataStreamer.writeNumber(1 / 5);
|
||||
dataStreamer.writeNumber(1 / 5,1);
|
||||
dataStreamer.writeLine();
|
||||
dataStreamer.writeNumber(20 / 3);
|
||||
dataStreamer.writeNumber(20 / 3,2);
|
||||
dataStreamer.writeLine();
|
||||
dataStreamer.writeNumber(400 / 35);
|
||||
dataStreamer.writeNumber(400 / 35,3);
|
||||
dataStreamer.writeLine();
|
||||
dataStreamer.writeNumber(1000 / 2);
|
||||
dataStreamer.writeNumber(1000 / 2,4);
|
||||
dataStreamer.writeLine();
|
||||
dataStreamer.writeNumber(1002);
|
||||
dataStreamer.writeNumber(1002,5);
|
||||
dataStreamer.writeLine();
|
||||
dataStreamer.writeNumber(10000002 / 458);
|
||||
dataStreamer.writeNumber(10000002 / 458, 6);
|
||||
dataStreamer.writeLine();
|
||||
dataStreamer.writeNumber(0);
|
||||
dataStreamer.writeNumber(0,7);
|
||||
dataStreamer.writeLine();
|
||||
dataStreamer.writeNumber(-1);
|
||||
dataStreamer.writeNumber(-1,0);
|
||||
dataStreamer.writeLine();
|
||||
dataStreamer.writeNumber(-1000 / 3);
|
||||
dataStreamer.writeNumbers([5, 2 / 5, 44 / 444, 5 / 5]);
|
||||
dataStreamer.writeNumber(-1000 / 3,1);
|
||||
dataStreamer.writeNumbers([5, 2 / 5, 44 / 444, 5 / 5],1);
|
Загрузка…
Ссылка в новой задаче