Defer the reading from and printing to the Serial port, to the global event queue.

Files at this revision

API Documentation at this revision

Comitter:
AnnaBridge
Date:
Fri Nov 10 16:03:09 2017 +0000
Commit message:
Additional example for UARTSerial

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
mbed-os.lib Show annotated file Show diff for this revision Revisions of this file
--- /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);
+    }
+}
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed-os.lib	Fri Nov 10 16:03:09 2017 +0000
@@ -0,0 +1,1 @@
+https://github.com/ARMmbed/mbed-os/#78474a5129e18e136cc7e872adbaa5b74fbb8f6a