diff --git a/libs/microbit/control.cpp b/libs/microbit/control.cpp index 07fa9188..ed8479a5 100644 --- a/libs/microbit/control.cpp +++ b/libs/microbit/control.cpp @@ -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; } diff --git a/libs/microbit/serial.cpp b/libs/microbit/serial.cpp index bdd15adb..a891d36a 100644 --- a/libs/microbit/serial.cpp +++ b/libs/microbit/serial.cpp @@ -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); + } } diff --git a/libs/microbit/serial.ts b/libs/microbit/serial.ts index 39e86ac2..53211ed2 100644 --- a/libs/microbit/serial.ts +++ b/libs/microbit/serial.ts @@ -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); + } } diff --git a/libs/microbit/shims.d.ts b/libs/microbit/shims.d.ts index 0a89784a..4b0d11bf 100644 --- a/libs/microbit/shims.d.ts +++ b/libs/microbit/shims.d.ts @@ -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; } diff --git a/sim/libmbit.ts b/sim/libmbit.ts index 3c11352d..8075906d 100644 --- a/sim/libmbit.ts +++ b/sim/libmbit.ts @@ -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); + } }