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
Revision 2:8196b4cc681b, committed 2015-03-09
- Comitter:
- scohennm
- Date:
- Mon Mar 09 00:33:25 2015 +0000
- Parent:
- 1:4942f8201331
- Commit message:
- Precursor top to simple parser multi threaded use of c++ basic string library.
Changed in this revision
serialO_v3.cpp | Show diff for this revision Revisions of this file |
serialO_v5.cpp | Show annotated file Show diff for this revision Revisions of this file |
diff -r 4942f8201331 -r 8196b4cc681b serialO_v3.cpp --- a/serialO_v3.cpp Thu Mar 05 23:59:12 2015 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,62 +0,0 @@ -#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
diff -r 4942f8201331 -r 8196b4cc681b serialO_v5.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/serialO_v5.cpp Mon Mar 09 00:33:25 2015 +0000 @@ -0,0 +1,97 @@ +#include "mbed.h" +#include <iostream> +#include <stdio.h> +#include <ctype.h> +#include <string.h> +#include <locale.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 //ms +// ASCII constants +#define CR '\r' + + + + +DigitalOut rLed(LED_RED); + + +Serial pc(USBTX, USBRX); // tx, rx +SLCD slcd; //define LCD display +char rxChar; +char rxString[LCDLEN]; +string rxRealString; + +void clearString (char * aString){ + int i; + int sSize = sizeof(aString); + + for (i=0; i< sSize; i++){ + aString[i] = NULL; + } + return; +} + + + +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; + int rxStringLen; + while(true) { + if (pc.readable()) { // only read from the serial port if there is a character + rxChar= toupper(pc.getc()); // reading clears the buffer + // construct a 4-digit string for the LCD + // check for carriage return + if (rxChar == CR) { + rxRealString.assign(rxString); + rxStringLen = rxRealString.length(); + pc.printf("%s - StringLen = %d OK \n\r", rxString, rxStringLen); + charIndex = 0; // start building string from position zero next time around + } else { + if(charIndex == 0) clearString(rxString); + rxString[charIndex] = rxChar; + rxRealString.assign(rxString); + 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