-
Notifications
You must be signed in to change notification settings - Fork 8
/
extension.ts
90 lines (80 loc) · 2.61 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
83
84
85
86
87
88
89
90
/**
* Writing data for hacking stem experiments
*/
//% weight=94 color=#e69100 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) {
if (isNaN(value)) {
serial.writeString(" ")
return; // don't write anything if the number is not defined
}
//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) {
if (text)
serial.writeString(text)
}
/**
* Print a new line to the serial port
*/
//% blockId=datastreamer_writeline block="write line"
//% weight=20
export function writeLine() {
serial.writeLine("")
}
/**
* Configures the serial communication to use USB and 9600 bauds
*/
export function setup(baudRate = BaudRate.BaudRate9600) {
serial.redirectToUSB()
if (baudRate)
serial.setBaudRate(baudRate)
}
/**
* 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(",")
}
}
}
setup()
}