From 0648e80131f6e8098eb238a034e04852fae0f13f Mon Sep 17 00:00:00 2001 From: Peli de Halleux Date: Thu, 26 May 2016 11:07:09 -0700 Subject: [PATCH] added serial.redirect --- .gitignore | 2 ++ libs/microbit/enums.d.ts | 21 +++++++++++++++++++++ libs/microbit/serial.cpp | 36 +++++++++++++++++++++++++++++++++++- libs/microbit/shims.d.ts | 12 ++++++++++++ sim/libmbit.ts | 4 ++++ 5 files changed, 74 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index 0a4c2d25..46028121 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,6 @@ node_modules +yotta_modules +yotta_targets built typings tmp diff --git a/libs/microbit/enums.d.ts b/libs/microbit/enums.d.ts index c24c9e32..c0b922f1 100644 --- a/libs/microbit/enums.d.ts +++ b/libs/microbit/enums.d.ts @@ -280,6 +280,27 @@ declare namespace led { } declare namespace pins { } + + + declare enum SerialPin { + P0 = 7, // MICROBIT_ID_IO_P0 + P1 = 8, // MICROBIT_ID_IO_P1 + P2 = 9, // MICROBIT_ID_IO_P2 + P8 = 15, // MICROBIT_ID_IO_P8 + P12 = 19, // MICROBIT_ID_IO_P12 + P13 = 20, // MICROBIT_ID_IO_P13 + P14 = 21, // MICROBIT_ID_IO_P14 + P15 = 22, // MICROBIT_ID_IO_P15 + P16 = 23, // MICROBIT_ID_IO_P16 + } + + + declare enum BaudRate { + //% block=115200 + BaudRate115200 = 115200, + //% block=9600 + BaudRate9600 = 9600, + } declare namespace serial { } diff --git a/libs/microbit/serial.cpp b/libs/microbit/serial.cpp index ff027c7e..e08e0b99 100644 --- a/libs/microbit/serial.cpp +++ b/libs/microbit/serial.cpp @@ -1,5 +1,24 @@ #include "ksbit.h" +enum class SerialPin { + P0 = MICROBIT_ID_IO_P0, + P1 = MICROBIT_ID_IO_P1, + P2 = MICROBIT_ID_IO_P2, + P8 = MICROBIT_ID_IO_P8, + P12 = MICROBIT_ID_IO_P12, + P13 = MICROBIT_ID_IO_P13, + P14 = MICROBIT_ID_IO_P14, + P15 = MICROBIT_ID_IO_P15, + P16 = MICROBIT_ID_IO_P16 +}; + +enum class BaudRate { + //% block=115200 + BaudRate115200 = 115200, + //% block=9600 + BaudRate9600 = 9600 +}; + //% weight=2 color=30 namespace serial { // note that at least one // followed by % is needed per declaration! @@ -34,4 +53,19 @@ namespace serial { uBit.serial.eventOn(ManagedString(delimiters)); registerWithDal(MICROBIT_ID_SERIAL, MICROBIT_SERIAL_EVT_DELIM_MATCH, body); } -} + + /** + * Dynamically configuring the serial instance to use pins other than USBTX and USBRX. + * @param tx the new transmission pins + * @param rx the new reception pin + * @param baud the new baud rate. eg: 115200 + */ + //% weight=10 + //% help=serial/redirect + //% blockId=serial_redirect block="serial redirect to|TX %tx|RX %rx|at baud rate %rate" + //% blockExternalInputs=1 + void redirect(SerialPin tx, SerialPin rx, BaudRate rate) { + uBit.serial.redirect((PinName)tx, (PinName)rx); + uBit.serial.baud((int)rate); + } +} \ No newline at end of file diff --git a/libs/microbit/shims.d.ts b/libs/microbit/shims.d.ts index 523a9b98..aff85e66 100644 --- a/libs/microbit/shims.d.ts +++ b/libs/microbit/shims.d.ts @@ -575,6 +575,18 @@ declare namespace serial { //% weight=87 //% blockId=serial_writestring block="serial write string %text" shim=serial::writeString function writeString(text: string): void; + + /** + * Dynamically configuring the serial instance to use pins other than USBTX and USBRX. + * @param tx the new transmission pins + * @param rx the new reception pin + * @param baud the new baud rate. eg: 115200 + */ + //% weight=10 + //% help=serial/redirect + //% blockId=serial_redirect block="serial redirect to|TX %tx|RX %rx|at baud rate %rate" + //% blockExternalInputs=1 shim=serial::redirect + function redirect(tx: SerialPin, rx: SerialPin, rate: BaudRate): void; } diff --git a/sim/libmbit.ts b/sim/libmbit.ts index 916bdc3a..0ddbecab 100644 --- a/sim/libmbit.ts +++ b/sim/libmbit.ts @@ -438,6 +438,10 @@ namespace pxsim.serial { let b = board(); b.bus.listen(DAL.MICROBIT_ID_SERIAL, DAL.MICROBIT_SERIAL_EVT_DELIM_MATCH, handler); } + + export function redirect(tx: number, rx: number, rate: number) { + // TODO? + } }