pxt-hacking-stem/extension.ts

91 строка
2.6 KiB
TypeScript
Исходник Постоянная ссылка Обычный вид История

2018-10-16 01:19:04 +03:00
/**
* Writing data for hacking stem experiments
*/
2021-11-11 21:39:46 +03:00
//% weight=94 color=#e69100 icon="\uf287"
2018-10-16 01:19:04 +03:00
namespace dataStreamer {
/**
* Print a numeric value to the serial port
* @param value to send over serial
2018-10-19 03:15:10 +03:00
* @param decimalDigits (optional) unit of the value
2018-10-16 01:19:04 +03:00
*/
2018-10-19 03:27:59 +03:00
//% blockId=datastreamer_writeNumber block="write number %value||decimal digits %decimalDigits"
2018-10-16 02:42:30 +03:00
//% weight=30
2018-10-19 03:43:05 +03:00
//% decimalDigits.defl=2
//% expandableArgumentMode=toggle
2018-10-19 03:45:33 +03:00
export function writeNumber(value: number, decimalDigits: number = 2) {
2021-11-11 22:03:48 +03:00
if (isNaN(value)) {
serial.writeString(" ")
return; // don't write anything if the number is not defined
}
2018-10-19 03:15:10 +03:00
//Format to right number of decimalDigits
let text: string = value.toString();
let len = 0; //Tracks the subscrtring length
2018-10-19 03:43:05 +03:00
for (; len < text.length; len++) { //Find the .
2018-10-19 03:15:10 +03:00
if (text[len] == ".") { break; }
}
2018-10-19 03:43:05 +03:00
if (decimalDigits != 0) {
2018-10-19 03:15:10 +03:00
len++ //Account for .
2018-10-19 03:43:05 +03:00
if (len + decimalDigits < text.length) {
2018-10-19 03:15:10 +03:00
len = len + decimalDigits;
} else {
len = text.length;
}
}
2018-10-19 03:43:05 +03:00
serial.writeString(text.substr(0, len))
2018-05-23 08:49:42 +03:00
}
/**
2018-10-16 02:42:30 +03:00
* Print a text to the serial port
2018-10-16 01:19:04 +03:00
* @param text to send over serial
*/
2018-10-16 02:46:06 +03:00
//% blockId=datastreamer_writestring block="write string %text"
2018-10-16 02:42:30 +03:00
//% weight=20
//% text.defl=","
2018-10-16 02:42:30 +03:00
export function writeString(text: string) {
2021-11-11 22:03:48 +03:00
if (text)
serial.writeString(text)
2018-10-16 02:42:30 +03:00
}
/**
* Print a new line to the serial port
2018-10-16 02:42:30 +03:00
*/
//% blockId=datastreamer_writeline block="write line"
2018-10-16 02:42:30 +03:00
//% weight=20
export function writeLine() {
serial.writeLine("")
2018-10-16 02:42:30 +03:00
}
2018-10-16 01:19:04 +03:00
/**
* Configures the serial communication to use USB and 9600 bauds
*/
export function setup(baudRate = BaudRate.BaudRate9600) {
serial.redirectToUSB()
if (baudRate)
serial.setBaudRate(baudRate)
}
2018-10-16 01:19:04 +03:00
/**
* Print an array of numeric values as CSV to the serial port
2018-05-23 08:49:42 +03:00
*/
//% blockId=datastreamer_writenumberarray block="write number array %values||decimal digits %decimalDigits"
2018-10-16 02:42:30 +03:00
//% weight=10
2018-10-19 03:15:10 +03:00
//% values.shadow="lists_create_with"
2018-10-19 03:43:05 +03:00
//% decimalDigits.defl=2
//% expandableArgumentMode=toggle
export function writeNumberArray(values: number[], decimalDigits: number = 2): void {
2018-10-16 02:42:30 +03:00
for (let i = 0; i < values.length; i++) {
2018-10-19 03:15:10 +03:00
dataStreamer.writeNumber(values[i], decimalDigits)
if ((i + 1) < values.length) {
dataStreamer.writeString(",")
}
2018-10-16 02:42:30 +03:00
}
2018-05-23 08:49:42 +03:00
}
setup()
}