Serial I/O program using rtos and ttreads for NMNU Ambient Computing

Dependencies:   SLCD mbed-rtos mbed

Fork of Serial_IO_test_v2 by Stanley Cohen

Files at this revision

API Documentation at this revision

Comitter:
scohennm
Date:
Thu Mar 05 23:59:12 2015 +0000
Parent:
0:60b70ac7ed38
Commit message:
Serial I/O program using rtos and ttreads for NMNU Ambient Computing

Changed in this revision

mbed-rtos.lib Show annotated file Show diff for this revision Revisions of this file
serialO_v2.cpp Show diff for this revision Revisions of this file
serialO_v3.cpp Show annotated file Show diff for this revision Revisions of this file
diff -r 60b70ac7ed38 -r 4942f8201331 mbed-rtos.lib
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed-rtos.lib	Thu Mar 05 23:59:12 2015 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/mbed_official/code/mbed-rtos/#63988a2238f7
diff -r 60b70ac7ed38 -r 4942f8201331 serialO_v2.cpp
--- a/serialO_v2.cpp	Thu Mar 05 03:33:01 2015 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,50 +0,0 @@
-#include "mbed.h"
-#include "SLCD.h"
-
-#define LCDLEN      10
-#define MAXCHAR     4
-#define LEDBLINKTIME 0.2f
-
-
-DigitalOut rLed(LED_RED);
-
- 
-Serial pc(USBTX, USBRX); // tx, rx
-SLCD slcd; //define LCD display
-char rxChar;
-int charIndex = 0;
-char rxString[LCDLEN];
-bool charReady = false;
-
-// set up intterupt for serial port
-
-void serialISR(){
-  rxChar = pc.getc();   // reading clears the buffer
-  pc.printf("%c\n\r", rxChar);
-  rxString[charIndex] = rxChar;
-  charIndex = (charIndex + 1 )% MAXCHAR;
-  charReady = true;
-  return;
-}
-
-void LCDMessNoDwell(char *lMess){
-        slcd.Home();
-        slcd.clear();
-        slcd.printf(lMess);
-}
-
-
-int main()
-{  
- // set up interrupt   
-    pc.attach(&serialISR);
-    
-    while (true) {     
-        rLed = !rLed; // toggle led
-        if (charReady) {
-            LCDMessNoDwell(rxString);
-            charReady = false;
-        }
-        wait(LEDBLINKTIME);
-    }
-}
\ No newline at end of file
diff -r 60b70ac7ed38 -r 4942f8201331 serialO_v3.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/serialO_v3.cpp	Thu Mar 05 23:59:12 2015 +0000
@@ -0,0 +1,62 @@
+#include "mbed.h"
+#include "rtos.h"
+#include "SLCD.h"
+
+#define LCDLEN          10
+#define MAXCHAR         4
+#define ALL8          "8888"
+#define LCDUPDATE       100 //ms
+#define LEDBLINKTIME    300 // ms *** NOTE Change of units ***
+#define SERIALREADTIME  50 //mw
+
+
+DigitalOut rLed(LED_RED);
+
+ 
+Serial pc(USBTX, USBRX); // tx, rx
+SLCD slcd; //define LCD display
+char rxChar;
+char rxString[LCDLEN];
+
+void LCDMessNoDwell(char *lMess){
+        slcd.Home();
+        slcd.clear();
+        slcd.printf(lMess);
+}
+
+// use "thread" in the name to keep things straight 
+// note the use of void constant * args - understand memory resources
+// Thes are "forever loops"
+void LCDdis_thread(void const *args){
+    while(true) {
+        LCDMessNoDwell(rxString);
+        Thread::wait(LCDUPDATE);
+    }
+}
+
+void serial_thread(void const *args){
+   static int charIndex = 0;
+   while(true) {
+      if (pc.readable()) {                // only read from the serial port if there is a character
+            rxChar= pc.getc();              // reading clears the buffer
+            rxString[charIndex] = rxChar;   // construct a 4-digit string for the LCD
+            pc.printf("%s\n\r", rxString); 
+            charIndex = (charIndex + 1 )% MAXCHAR; // Only allow 4 characters then roll over
+      }
+      Thread::wait(SERIALREADTIME);
+    }
+}
+
+int main()
+{  
+ 
+    Thread lthread(LCDdis_thread);
+    Thread serthread(serial_thread);
+    
+    sprintf(rxString,"%s",ALL8);  // just put something on the LCD to show it's working
+
+    while (true) {     
+        rLed = !rLed; // toggle led
+        Thread::wait(LEDBLINKTIME);
+    }
+}
\ No newline at end of file