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:
Mon Mar 09 00:33:25 2015 +0000
Revision:
2:8196b4cc681b
Precursor top to simple parser multi threaded use of c++ basic string library.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
scohennm 2:8196b4cc681b 1 #include "mbed.h"
scohennm 2:8196b4cc681b 2 #include <iostream>
scohennm 2:8196b4cc681b 3 #include <stdio.h>
scohennm 2:8196b4cc681b 4 #include <ctype.h>
scohennm 2:8196b4cc681b 5 #include <string.h>
scohennm 2:8196b4cc681b 6 #include <locale.h>
scohennm 2:8196b4cc681b 7 #include "rtos.h"
scohennm 2:8196b4cc681b 8 #include "SLCD.h"
scohennm 2:8196b4cc681b 9
scohennm 2:8196b4cc681b 10 #define LCDLEN 10
scohennm 2:8196b4cc681b 11 #define MAXCHAR 4
scohennm 2:8196b4cc681b 12 #define ALL8 "8888"
scohennm 2:8196b4cc681b 13 #define LCDUPDATE 100 //ms
scohennm 2:8196b4cc681b 14 #define LEDBLINKTIME 300 // ms *** NOTE Change of units ***
scohennm 2:8196b4cc681b 15 #define SERIALREADTIME 50 //ms
scohennm 2:8196b4cc681b 16 // ASCII constants
scohennm 2:8196b4cc681b 17 #define CR '\r'
scohennm 2:8196b4cc681b 18
scohennm 2:8196b4cc681b 19
scohennm 2:8196b4cc681b 20
scohennm 2:8196b4cc681b 21
scohennm 2:8196b4cc681b 22 DigitalOut rLed(LED_RED);
scohennm 2:8196b4cc681b 23
scohennm 2:8196b4cc681b 24
scohennm 2:8196b4cc681b 25 Serial pc(USBTX, USBRX); // tx, rx
scohennm 2:8196b4cc681b 26 SLCD slcd; //define LCD display
scohennm 2:8196b4cc681b 27 char rxChar;
scohennm 2:8196b4cc681b 28 char rxString[LCDLEN];
scohennm 2:8196b4cc681b 29 string rxRealString;
scohennm 2:8196b4cc681b 30
scohennm 2:8196b4cc681b 31 void clearString (char * aString){
scohennm 2:8196b4cc681b 32 int i;
scohennm 2:8196b4cc681b 33 int sSize = sizeof(aString);
scohennm 2:8196b4cc681b 34
scohennm 2:8196b4cc681b 35 for (i=0; i< sSize; i++){
scohennm 2:8196b4cc681b 36 aString[i] = NULL;
scohennm 2:8196b4cc681b 37 }
scohennm 2:8196b4cc681b 38 return;
scohennm 2:8196b4cc681b 39 }
scohennm 2:8196b4cc681b 40
scohennm 2:8196b4cc681b 41
scohennm 2:8196b4cc681b 42
scohennm 2:8196b4cc681b 43 void LCDMessNoDwell(char *lMess){
scohennm 2:8196b4cc681b 44 slcd.Home();
scohennm 2:8196b4cc681b 45 slcd.clear();
scohennm 2:8196b4cc681b 46 slcd.printf(lMess);
scohennm 2:8196b4cc681b 47 }
scohennm 2:8196b4cc681b 48
scohennm 2:8196b4cc681b 49 // use "thread" in the name to keep things straight
scohennm 2:8196b4cc681b 50 // note the use of void constant * args - understand memory resources
scohennm 2:8196b4cc681b 51 // Thes are "forever loops"
scohennm 2:8196b4cc681b 52 void LCDdis_thread(void const *args){
scohennm 2:8196b4cc681b 53 while(true) {
scohennm 2:8196b4cc681b 54 LCDMessNoDwell(rxString);
scohennm 2:8196b4cc681b 55 Thread::wait(LCDUPDATE);
scohennm 2:8196b4cc681b 56 }
scohennm 2:8196b4cc681b 57 }
scohennm 2:8196b4cc681b 58
scohennm 2:8196b4cc681b 59 void serial_thread(void const *args){
scohennm 2:8196b4cc681b 60 static int charIndex =0;
scohennm 2:8196b4cc681b 61 int rxStringLen;
scohennm 2:8196b4cc681b 62 while(true) {
scohennm 2:8196b4cc681b 63 if (pc.readable()) { // only read from the serial port if there is a character
scohennm 2:8196b4cc681b 64 rxChar= toupper(pc.getc()); // reading clears the buffer
scohennm 2:8196b4cc681b 65 // construct a 4-digit string for the LCD
scohennm 2:8196b4cc681b 66 // check for carriage return
scohennm 2:8196b4cc681b 67 if (rxChar == CR) {
scohennm 2:8196b4cc681b 68 rxRealString.assign(rxString);
scohennm 2:8196b4cc681b 69 rxStringLen = rxRealString.length();
scohennm 2:8196b4cc681b 70 pc.printf("%s - StringLen = %d OK \n\r", rxString, rxStringLen);
scohennm 2:8196b4cc681b 71 charIndex = 0; // start building string from position zero next time around
scohennm 2:8196b4cc681b 72 } else {
scohennm 2:8196b4cc681b 73 if(charIndex == 0) clearString(rxString);
scohennm 2:8196b4cc681b 74 rxString[charIndex] = rxChar;
scohennm 2:8196b4cc681b 75 rxRealString.assign(rxString);
scohennm 2:8196b4cc681b 76 pc.printf("%s\n\r", rxString);
scohennm 2:8196b4cc681b 77 charIndex = (charIndex + 1)% (MAXCHAR); // Only allow 4 characters then roll over
scohennm 2:8196b4cc681b 78
scohennm 2:8196b4cc681b 79 }
scohennm 2:8196b4cc681b 80 }
scohennm 2:8196b4cc681b 81 Thread::wait(SERIALREADTIME);
scohennm 2:8196b4cc681b 82 }
scohennm 2:8196b4cc681b 83 }
scohennm 2:8196b4cc681b 84
scohennm 2:8196b4cc681b 85 int main()
scohennm 2:8196b4cc681b 86 {
scohennm 2:8196b4cc681b 87
scohennm 2:8196b4cc681b 88 Thread lthread(LCDdis_thread);
scohennm 2:8196b4cc681b 89 Thread serthread(serial_thread);
scohennm 2:8196b4cc681b 90
scohennm 2:8196b4cc681b 91 sprintf(rxString,"%s",ALL8); // just put something on the LCD to show it's working
scohennm 2:8196b4cc681b 92
scohennm 2:8196b4cc681b 93 while (true) {
scohennm 2:8196b4cc681b 94 rLed = !rLed; // toggle led
scohennm 2:8196b4cc681b 95 Thread::wait(LEDBLINKTIME);
scohennm 2:8196b4cc681b 96 }
scohennm 2:8196b4cc681b 97 }