Defer the reading from and printing to the Serial port, to the global event queue.
Diff: main.cpp
- Revision:
- 0:3a7d1a0a9b0b
diff -r 000000000000 -r 3a7d1a0a9b0b main.cpp
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp Fri Nov 10 16:03:09 2017 +0000
@@ -0,0 +1,28 @@
+#include "mbed.h"
+
+DigitalOut led1(LED1);
+DigitalOut led2(LED2);
+
+UARTSerial pc(USBTX, USBRX);
+
+void callback_ex() {
+ while (1) {
+ char c;
+ if (pc.read(&c, 1) != 1) {
+ break;
+ }
+ printf("%c\n", c);
+ led2 = !led2;
+ }
+}
+
+int main() {
+ pc.set_blocking(false);
+ pc.sigio(mbed::mbed_event_queue()->event(callback_ex));
+
+ while (1) {
+ led1 = !led1;
+ wait(0.5);
+ }
+}
+