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
serialO_v5.cpp
00001 #include "mbed.h" 00002 #include <iostream> 00003 #include <stdio.h> 00004 #include <ctype.h> 00005 #include <string.h> 00006 #include <locale.h> 00007 #include "rtos.h" 00008 #include "SLCD.h" 00009 00010 #define LCDLEN 10 00011 #define MAXCHAR 4 00012 #define ALL8 "8888" 00013 #define LCDUPDATE 100 //ms 00014 #define LEDBLINKTIME 300 // ms *** NOTE Change of units *** 00015 #define SERIALREADTIME 50 //ms 00016 // ASCII constants 00017 #define CR '\r' 00018 00019 00020 00021 00022 DigitalOut rLed(LED_RED); 00023 00024 00025 Serial pc(USBTX, USBRX); // tx, rx 00026 SLCD slcd; //define LCD display 00027 char rxChar; 00028 char rxString[LCDLEN]; 00029 string rxRealString; 00030 00031 void clearString (char * aString){ 00032 int i; 00033 int sSize = sizeof(aString); 00034 00035 for (i=0; i< sSize; i++){ 00036 aString[i] = NULL; 00037 } 00038 return; 00039 } 00040 00041 00042 00043 void LCDMessNoDwell(char *lMess){ 00044 slcd.Home(); 00045 slcd.clear(); 00046 slcd.printf(lMess); 00047 } 00048 00049 // use "thread" in the name to keep things straight 00050 // note the use of void constant * args - understand memory resources 00051 // Thes are "forever loops" 00052 void LCDdis_thread(void const *args){ 00053 while(true) { 00054 LCDMessNoDwell(rxString); 00055 Thread::wait(LCDUPDATE); 00056 } 00057 } 00058 00059 void serial_thread(void const *args){ 00060 static int charIndex =0; 00061 int rxStringLen; 00062 while(true) { 00063 if (pc.readable()) { // only read from the serial port if there is a character 00064 rxChar= toupper(pc.getc()); // reading clears the buffer 00065 // construct a 4-digit string for the LCD 00066 // check for carriage return 00067 if (rxChar == CR) { 00068 rxRealString.assign(rxString); 00069 rxStringLen = rxRealString.length(); 00070 pc.printf("%s - StringLen = %d OK \n\r", rxString, rxStringLen); 00071 charIndex = 0; // start building string from position zero next time around 00072 } else { 00073 if(charIndex == 0) clearString(rxString); 00074 rxString[charIndex] = rxChar; 00075 rxRealString.assign(rxString); 00076 pc.printf("%s\n\r", rxString); 00077 charIndex = (charIndex + 1)% (MAXCHAR); // Only allow 4 characters then roll over 00078 00079 } 00080 } 00081 Thread::wait(SERIALREADTIME); 00082 } 00083 } 00084 00085 int main() 00086 { 00087 00088 Thread lthread(LCDdis_thread); 00089 Thread serthread(serial_thread); 00090 00091 sprintf(rxString,"%s",ALL8); // just put something on the LCD to show it's working 00092 00093 while (true) { 00094 rLed = !rLed; // toggle led 00095 Thread::wait(LEDBLINKTIME); 00096 } 00097 }
Generated on Sat Jul 16 2022 02:55:46 by 1.7.2