Precursor top to simple parser multi threaded use of c++ basic string library. For Ambient computing NMNU Spring 2015

Dependencies:   SLCD mbed-rtos mbed

Fork of Serial_IO_test_v3 by Stanley Cohen

Committer:
scohennm
Date:
Thu Mar 05 23:59:12 2015 +0000
Revision:
1:4942f8201331
Parent:
serialO_v2.cpp@0:60b70ac7ed38
Serial I/O program using rtos and ttreads for NMNU Ambient Computing

Who changed what in which revision?

UserRevisionLine numberNew contents of line
scohennm 0:60b70ac7ed38 1 #include "mbed.h"
scohennm 1:4942f8201331 2 #include "rtos.h"
scohennm 0:60b70ac7ed38 3 #include "SLCD.h"
scohennm 0:60b70ac7ed38 4
scohennm 1:4942f8201331 5 #define LCDLEN 10
scohennm 1:4942f8201331 6 #define MAXCHAR 4
scohennm 1:4942f8201331 7 #define ALL8 "8888"
scohennm 1:4942f8201331 8 #define LCDUPDATE 100 //ms
scohennm 1:4942f8201331 9 #define LEDBLINKTIME 300 // ms *** NOTE Change of units ***
scohennm 1:4942f8201331 10 #define SERIALREADTIME 50 //mw
scohennm 0:60b70ac7ed38 11
scohennm 0:60b70ac7ed38 12
scohennm 0:60b70ac7ed38 13 DigitalOut rLed(LED_RED);
scohennm 0:60b70ac7ed38 14
scohennm 0:60b70ac7ed38 15
scohennm 0:60b70ac7ed38 16 Serial pc(USBTX, USBRX); // tx, rx
scohennm 0:60b70ac7ed38 17 SLCD slcd; //define LCD display
scohennm 0:60b70ac7ed38 18 char rxChar;
scohennm 0:60b70ac7ed38 19 char rxString[LCDLEN];
scohennm 0:60b70ac7ed38 20
scohennm 0:60b70ac7ed38 21 void LCDMessNoDwell(char *lMess){
scohennm 0:60b70ac7ed38 22 slcd.Home();
scohennm 0:60b70ac7ed38 23 slcd.clear();
scohennm 0:60b70ac7ed38 24 slcd.printf(lMess);
scohennm 0:60b70ac7ed38 25 }
scohennm 0:60b70ac7ed38 26
scohennm 1:4942f8201331 27 // use "thread" in the name to keep things straight
scohennm 1:4942f8201331 28 // note the use of void constant * args - understand memory resources
scohennm 1:4942f8201331 29 // Thes are "forever loops"
scohennm 1:4942f8201331 30 void LCDdis_thread(void const *args){
scohennm 1:4942f8201331 31 while(true) {
scohennm 1:4942f8201331 32 LCDMessNoDwell(rxString);
scohennm 1:4942f8201331 33 Thread::wait(LCDUPDATE);
scohennm 1:4942f8201331 34 }
scohennm 1:4942f8201331 35 }
scohennm 1:4942f8201331 36
scohennm 1:4942f8201331 37 void serial_thread(void const *args){
scohennm 1:4942f8201331 38 static int charIndex = 0;
scohennm 1:4942f8201331 39 while(true) {
scohennm 1:4942f8201331 40 if (pc.readable()) { // only read from the serial port if there is a character
scohennm 1:4942f8201331 41 rxChar= pc.getc(); // reading clears the buffer
scohennm 1:4942f8201331 42 rxString[charIndex] = rxChar; // construct a 4-digit string for the LCD
scohennm 1:4942f8201331 43 pc.printf("%s\n\r", rxString);
scohennm 1:4942f8201331 44 charIndex = (charIndex + 1 )% MAXCHAR; // Only allow 4 characters then roll over
scohennm 1:4942f8201331 45 }
scohennm 1:4942f8201331 46 Thread::wait(SERIALREADTIME);
scohennm 1:4942f8201331 47 }
scohennm 1:4942f8201331 48 }
scohennm 0:60b70ac7ed38 49
scohennm 0:60b70ac7ed38 50 int main()
scohennm 0:60b70ac7ed38 51 {
scohennm 1:4942f8201331 52
scohennm 1:4942f8201331 53 Thread lthread(LCDdis_thread);
scohennm 1:4942f8201331 54 Thread serthread(serial_thread);
scohennm 0:60b70ac7ed38 55
scohennm 1:4942f8201331 56 sprintf(rxString,"%s",ALL8); // just put something on the LCD to show it's working
scohennm 1:4942f8201331 57
scohennm 0:60b70ac7ed38 58 while (true) {
scohennm 0:60b70ac7ed38 59 rLed = !rLed; // toggle led
scohennm 1:4942f8201331 60 Thread::wait(LEDBLINKTIME);
scohennm 0:60b70ac7ed38 61 }
scohennm 0:60b70ac7ed38 62 }