Example of checking for a valid note:
Dependencies: SLCD mbed-rtos mbed
Fork of Serial_IO_test_v5 by
serialO_v6.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 // Debug printout control 00010 #define PRINTDBUG 00011 #define PROGNAME "SerialIO v6\n\r" 00012 00013 #define LCDLEN 10 00014 #define MAXCHAR 4 00015 #define ALL8 "8888" 00016 #define LCDUPDATE 100 //ms 00017 #define LEDBLINKTIME 300 // ms *** NOTE Change of units *** 00018 #define SERIALREADTIME 50 //ms 00019 // ASCII constants 00020 #define CR '\r' 00021 #define ALLNOTES "CDEFGAB" // Note only 7 unique notes. 00022 #define NUMTONES 10 00023 #define NONOTE 8 00024 #define NOTEOFFSET 1 00025 00026 00027 00028 float diatonicScale[NUMTONES] = {246.94, 261.63,293.66,329.63,349.23,392.00,440.00,493.88,523.25,587.33}; 00029 00030 DigitalOut rLed(LED_RED); 00031 00032 00033 Serial pc(USBTX, USBRX); // tx, rx 00034 SLCD slcd; //define LCD display 00035 char rxChar; 00036 char rxString[LCDLEN]; 00037 string rxRealString; 00038 00039 void clearString (char * aString){ 00040 int i; 00041 int sSize = sizeof(aString); 00042 00043 for (i=0; i< sSize; i++){ 00044 aString[i] = NULL; 00045 } 00046 return; 00047 } 00048 00049 00050 int isNote(char theNote){ //check for valid onte 00051 int noteListLen = sizeof(ALLNOTES); 00052 int pos = NONOTE; 00053 int i; 00054 for (i=0; i<noteListLen; i++){ 00055 if (ALLNOTES[i] == theNote){ 00056 pos = i; 00057 break; 00058 } 00059 } 00060 return (pos); 00061 } 00062 00063 00064 void LCDMessNoDwell(char *lMess){ 00065 slcd.Home(); 00066 slcd.clear(); 00067 slcd.printf(lMess); 00068 } 00069 00070 // use "thread" in the name to keep things straight 00071 // note the use of void constant * args - understand memory resources 00072 // Thes are "forever loops" 00073 void LCDdis_thread(void const *args){ 00074 while(true) { 00075 LCDMessNoDwell(rxString); 00076 Thread::wait(LCDUPDATE); 00077 } 00078 } 00079 00080 void serial_thread(void const *args){ 00081 static int charIndex =0; 00082 int rxStringLen; 00083 int noteOK; 00084 while(true) { 00085 if (pc.readable()) { // only read from the serial port if there is a character 00086 rxChar= toupper(pc.getc()); // reading clears the buffer 00087 noteOK = isNote(rxChar); 00088 // construct a 4-digit string for the LCD 00089 // check for carriage return 00090 if (rxChar == CR) { 00091 rxRealString.assign(rxString); 00092 rxStringLen = rxRealString.length(); 00093 #ifdef PRINTDBUG 00094 pc.printf("%s - StringLen = %d OK \n\r", rxString, rxStringLen); 00095 #endif 00096 charIndex = 0; // start building string from position zero next time around 00097 } else { 00098 if(charIndex == 0) clearString(rxString); 00099 rxString[charIndex] = rxChar; 00100 rxRealString.assign(rxString); 00101 #ifdef PRINTDBUG 00102 if(noteOK != NONOTE){ 00103 pc.printf("%s - IsNote - %4.2f\n\r", rxString, diatonicScale[noteOK + NOTEOFFSET]); 00104 } else { 00105 pc.printf("%s - Not a Note\n\r", rxString); 00106 } 00107 #endif 00108 charIndex = (charIndex + 1)% (MAXCHAR); // Only allow 4 characters then roll over 00109 00110 } 00111 } 00112 Thread::wait(SERIALREADTIME); 00113 } 00114 } 00115 00116 int main() 00117 { 00118 00119 Thread lthread(LCDdis_thread); 00120 Thread serthread(serial_thread); 00121 00122 sprintf(rxString,"%s",ALL8); // just put something on the LCD to show it's working 00123 00124 #ifdef PRINTDBUG 00125 pc.printf(PROGNAME); 00126 #endif 00127 00128 while (true) { 00129 rLed = !rLed; // toggle led 00130 Thread::wait(LEDBLINKTIME); 00131 } 00132 }
Generated on Wed Aug 3 2022 08:13:54 by 1.7.2