pxt-calliope/libs/microbit/serial.cpp

38 lines
1.1 KiB
C++
Raw Normal View History

2016-04-02 07:00:42 +02:00
#include "ksbit.h"
//% weight=2 color=30
namespace serial {
// note that at least one // followed by % is needed per declaration!
/**
* Reads a line of text from the serial port.
*/
2016-05-17 18:36:01 +02:00
//% help=serial/read-line
//% blockId=serial_read_line block="serial read line"
//% weight=20
StringData* readLine() {
return uBit.serial.readUntil(ManagedString("\n")).leakData();
2016-04-02 07:00:42 +02:00
}
/**
* Sends a piece of text through Serial connection.
*/
2016-05-17 18:36:01 +02:00
//% help=serial/write-string
//% weight=87
//% blockId=serial_writestring block="serial write string %text"
2016-04-02 07:00:42 +02:00
void writeString(StringData *text) {
2016-04-14 23:29:28 +02:00
uBit.serial.send(ManagedString(text));
2016-04-02 07:00:42 +02:00
}
2016-05-17 18:36:01 +02:00
/**
* 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
2016-05-17 18:36:01 +02:00
void onDataReceived(StringData* delimiters, Action body) {
uBit.serial.eventOn(ManagedString(delimiters));
registerWithDal(MICROBIT_ID_SERIAL, MICROBIT_SERIAL_EVT_DELIM_MATCH, body);
}
2016-04-02 07:00:42 +02:00
}