added serial events

This commit is contained in:
Peli de Halleux 2016-05-17 09:36:01 -07:00
parent 7704ad9f8d
commit 2e3d875b7d
5 changed files with 58 additions and 13 deletions

View File

@ -169,7 +169,7 @@ namespace control {
* Gets the timestamp of the last event executed on the bus
*/
//% blockId=control_event_timestamp" block="event timestamp"
//% weight=19 blockGap-8
//% weight=19 blockGap=8
int eventTimestamp() {
return pxt::lastEvent.timestamp;
}

View File

@ -7,16 +7,31 @@ namespace serial {
/**
* Reads a line of text from the serial port.
*/
//%
StringData* readString() {
return uBit.serial.readUntil(ManagedString("\r\n")).leakData();
//% help=serial/read-line
//% blockId=serial_read_line block="serial read line"
//% weight=20
StringData* readLine() {
return uBit.serial.readUntil(ManagedString("\n")).leakData();
}
/**
* Sends a piece of text through Serial connection.
*/
//% blockId=serial_writestring block="serial write %text"
//% help=serial/write-string
//% weight=87
//% blockId=serial_writestring block="serial write string %text"
void writeString(StringData *text) {
uBit.serial.send(ManagedString(text));
}
/**
* Registers an event to be fired when one of the delimiter is matched
* @param delimiters the characters to match received characters against. eg:"\n"
*/
//% help=serial/on-data-received
//% weight=19
void onDataReceived(StringData* delimiters, Action body) {
uBit.serial.eventOn(ManagedString(delimiters));
registerWithDal(MICROBIT_ID_SERIAL, MICROBIT_SERIAL_EVT_DELIM_MATCH, body);
}
}

View File

@ -7,7 +7,8 @@ namespace serial {
* Prints a line of text to the serial
* @param value to send over serial
*/
//% help=serial/write-line
//% weight=90
//% help=serial/write-line blockGap=8
//% blockId=serial_writeline block="serial|write line %text"
export function writeLine(text: string): void {
writeString(text);
@ -17,6 +18,8 @@ namespace serial {
/**
* Prints a numeric value to the serial
*/
//% help=serial/write-number
//% weight=89 blockGap=8
//% blockId=serial_writenumber block="serial|write number %value"
export function writeNumber(value: number): void {
writeString(value.toString());
@ -27,13 +30,23 @@ namespace serial {
* @param name name of the value stream, eg: x
* @param value to write
*/
//% weight=80
//% weight=88 blockGap=8
//% help=serial/write-value
//% blockId=serial_writevalue block="serial|write line %name|= %value"
//% blockId=serial_writevalue block="serial|write value %name|= %value"
export function writeValue(name: string, value: number): void {
writeString(name);
writeString(": ");
writeString(":");
writeNumber(value);
writeLine("");
}
/**
* Registers an event to be fired when a line has been received
*/
//% help=serial/on-line-received
//% blockId=serial_on_line_received block="serial on line received"
//% weight=21 blockGap=8
export function onLineReceived(body: Action): void {
serial.onDataReceived("\n", body);
}
}

View File

@ -354,7 +354,7 @@ declare namespace control {
* Gets the timestamp of the last event executed on the bus
*/
//% blockId=control_event_timestamp" block="event timestamp"
//% weight=19 blockGap-8 shim=control::eventTimestamp
//% weight=19 blockGap=8 shim=control::eventTimestamp
function eventTimestamp(): number;
/**
@ -563,14 +563,26 @@ declare namespace serial {
/**
* Reads a line of text from the serial port.
*/
//% shim=serial::readString
function readString(): string;
//% help=serial/read-line
//% blockId=serial_read_line block="serial read line"
//% weight=20 shim=serial::readLine
function readLine(): string;
/**
* Sends a piece of text through Serial connection.
*/
//% blockId=serial_writestring block="serial write %text" shim=serial::writeString
//% help=serial/write-string
//% weight=87
//% blockId=serial_writestring block="serial write string %text" shim=serial::writeString
function writeString(text: string): void;
/**
* Registers an event to be fired when one of the delimiter is matched
* @param delimiters the characters to match received characters against. eg:"\n"
*/
//% help=serial/on-data-received
//% weight=19 shim=serial::onDataReceived
function onDataReceived(delimiters: string, body: () => void): void;
}

View File

@ -429,6 +429,11 @@ namespace pxsim.serial {
export function readString(): string {
return board().readSerial();
}
export function onDataReceived(delimiters: string, handler: RefAction) {
let b = board();
b.bus.listen(DAL.MICROBIT_ID_SERIAL, DAL.MICROBIT_SERIAL_EVT_DELIM_MATCH, handler);
}
}