forked from microsoft/pxt-data-streamer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
extension.ts
82 lines (72 loc) · 2.31 KB
/
extension.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
/**
* Writing data for hacking stem experiments
*/
//% weight=94 color=#ffab19 icon="\uf287"
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||decimal digits %decimalDigits"
//% weight=30
//% decimalDigits.defl=2
//% expandableArgumentMode=toggle
export function writeNumber(value: number, decimalDigits: number = 2) {
//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))
}
/**
* Print a text to the serial port
* @param text to send over serial
*/
//% blockId=datastreamer_writestring block="write string %text"
//% weight=20
//% text.defl=","
export function writeString(text: string) {
serial.writeString(text)
}
/**
* Print a new line to the serial port
*/
//% blockId=datastreamer_writeline block="write line"
//% weight=20
export function writeLine() {
serial.writeLine("")
}
/**
* Print an array of numeric values as CSV to the serial port
*/
//% blockId=datastreamer_writenumberarray block="write number array %values||decimal digits %decimalDigits"
//% weight=10
//% values.shadow="lists_create_with"
//% decimalDigits.defl=2
//% expandableArgumentMode=toggle
export function writeNumberArray(values: number[], decimalDigits: number = 2): void {
for (let i = 0; i < values.length; i++) {
dataStreamer.writeNumber(values[i], decimalDigits)
if ((i + 1) < values.length) {
dataStreamer.writeString(",")
}
}
}
//% shim=dataStreamer::init
function init() {
// keep this statement
return;
}
init();
}