Added serial->write numbers to support CSV data generation (#699)

* support for serial write numbers

* moving down the list

* formatting

* fixing example typo
This commit is contained in:
Peli de Halleux 2018-03-08 08:33:02 -08:00 committed by GitHub
parent 71e68481ca
commit 29de28a738
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 69 additions and 10 deletions

View File

@ -7,6 +7,7 @@ serial.writeLine("");
serial.writeNumber(0);
serial.writeValue("x", 0);
serial.writeString("");
serial.writeNumbers([0]);
serial.readUntil(",");
serial.readLine();
serial.readString();
@ -25,7 +26,8 @@ serial.readBuffer(64);
## See Also
[writeLine](/reference/serial/write-line), [writeNumber](/reference/serial/write-number), [writeValue](/reference/serial/write-value),
[writeString](/reference/serial/write-string), [readUntil](/reference/serial/read-until), [readLine](/reference/serial/read-line),
[writeString](/reference/serial/write-string),
[writeNumbers](/reference/serial/write-numbers), [readUntil](/reference/serial/read-until), [readLine](/reference/serial/read-line),
[readString](/reference/serial/read-string), [onDataReceived](/reference/serial/on-data-received),
[redirect](/reference/serial/redirect), [writeBuffer](/reference/serial/write-buffer), [readBuffer](/reference/serial/read-buffer),
[redirectToUSB](/reference/serial/redirect-to-usb)

View File

@ -10,13 +10,13 @@ serial.writeNumber(0);
* `value` is the [number](/types/number) to write to the serial port
## Example: one through ten
## Example: one two three
This program repeatedly writes a 10-digit number to the serial port.
This program repeatedly writes a 3-digit number to the serial port.
```blocks
basic.forever(() => {
serial.writeNumber(1234567890);
serial.writeNumber(123);
basic.pause(5000);
});
```
@ -37,5 +37,6 @@ basic.forever(() => {
[serial](/device/serial),
[serial write line](/reference/serial/write-line),
[serial write value](/reference/serial/write-value)
[serial write value](/reference/serial/write-value),
[serial write numbers](/reference/serial/write-numbers)

View File

@ -0,0 +1,39 @@
# Serial Write Numbers
Write an array of numbers to the [serial](/device/serial) port.
```sig
serial.writeNumbers([0, 1, 2]);
```
## Parameters
* `values` is the array of [number](/types/number) to write to the serial port
## Example: one two three
This program repeatedly writes a 3-number array to the serial port.
```blocks
basic.forever(() => {
serial.writeNumbers([1, 2, 3]);
basic.pause(5000);
});
```
## Example: plot temperature and light
```blocks
serial.writeLine("temp,light")
basic.forever(() => {
serial.writeNumbers([input.temperature(), input.lightLevel()])
})
```
## See also
[serial](/device/serial),
[serial write line](/reference/serial/write-line),
[serial write value](/reference/serial/write-value)

View File

@ -154,7 +154,7 @@
"control.eventValue": "Gets the value of the last event executed on the bus",
"control.eventValueId": "Returns the value of a C++ runtime constant",
"control.inBackground": "Schedules code that run in the background.",
"control.onEvent": "Raises an event in the event bus.",
"control.onEvent": "Registers an event handler.",
"control.panic": "Display specified error code and stop the program.",
"control.raiseEvent": "Raises an event in the event bus.",
"control.raiseEvent|param|mode": "optional definition of how the event should be processed after construction (default is CREATE_AND_FIRE).",
@ -426,6 +426,7 @@
"serial.writeBuffer": "Sends a buffer through Serial connection",
"serial.writeLine": "Prints a line of text to the serial",
"serial.writeNumber": "Prints a numeric value to the serial",
"serial.writeNumbers": "Prints an array of numeric values to the serial as CSV",
"serial.writeString": "Sends a piece of text through Serial connection.",
"serial.writeValue": "Writes a ``name: value`` pair line to the serial.",
"serial.writeValue|param|name": "name of the value stream, eg: x",

View File

@ -343,6 +343,7 @@
"serial.redirect|block": "serial|redirect to|TX %tx|RX %rx|at baud rate %rate",
"serial.writeBuffer|block": "serial|write buffer %buffer",
"serial.writeLine|block": "serial|write line %text",
"serial.writeNumbers|block": "serial|write numbers %values",
"serial.writeNumber|block": "serial|write number %value",
"serial.writeString|block": "serial|write string %text",
"serial.writeValue|block": "serial|write value %name|= %value",

View File

@ -101,7 +101,7 @@ namespace serial {
* Sends a piece of text through Serial connection.
*/
//% help=serial/write-string
//% weight=87
//% weight=87 blockGap=8
//% blockId=serial_writestring block="serial|write string %text"
void writeString(StringData *text) {
if (!text) return;

View File

@ -32,6 +32,21 @@ namespace serial {
writeString(value.toString());
}
/**
* Prints an array of numeric values to the serial as CSV
*/
//% help=serial-write-numbers
//% weight=86
//% blockId=serial_writenumbers block="serial|write numbers %values"
export function writeNumbers(values: number[]): void {
if (!values) return;
for(let i = 0; i < values.length; ++i) {
if (i > 0) writeString(",");
writeNumber(values[i]);
}
writeLine("")
}
/**
* Writes a ``name: value`` pair line to the serial.
* @param name name of the value stream, eg: x

View File

@ -404,7 +404,7 @@ declare namespace control {
function raiseEvent(src: number, value: number, mode?: EventCreationMode): void;
/**
* Raises an event in the event bus.
* Registers an event handler.
*/
//% weight=20 blockGap=8 blockId="control_on_event" block="on event|from %src=control_event_source_id|with value %value=control_event_value_id"
//% help=control/on-event
@ -800,7 +800,7 @@ declare namespace serial {
* Sends a piece of text through Serial connection.
*/
//% help=serial/write-string
//% weight=87
//% weight=87 blockGap=8
//% blockId=serial_writestring block="serial|write string %text" shim=serial::writeString
function writeString(text: string): void;

View File

@ -41,6 +41,6 @@
"semantic-ui-less": "^2.2.4"
},
"dependencies": {
"pxt-core": "0.17.13"
"pxt-core": "0.17.14"
}
}