Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of Task616-mbedos54 by
main.cpp
00001 #include "mbed.h" 00002 00003 #include "string.h" 00004 #include <stdio.h> 00005 #include <ctype.h> 00006 00007 #define RED_DONE 1 00008 #define YELLOW_DONE 2 00009 00010 //Delays in ms 00011 #define TUNIT 250 00012 #define TDOT TUNIT 00013 #define TDASH (3*TUNIT) 00014 #define TGAP TUNIT 00015 #define TLETTER (3*TUNIT) 00016 #define TWORD (7*TUNIT) 00017 00018 //Size of the morse character buffer 00019 #define BUFFERSIZE 100 00020 00021 00022 /* 00023 **** Morse Code *** 00024 00025 . 1 unit 00026 - 3 units 00027 inter-space 1 unit 00028 00029 between letters 3 units 00030 between words 7 units 00031 */ 00032 00033 //Morse code 00034 const char* const morseAlpha[] = { 00035 ".-", //A 00036 "-...", //B 00037 "-.-.", //C 00038 "-..", //D 00039 ".", //E 00040 "..-.", //F 00041 "--.", //G 00042 "....", //H 00043 "..", //I 00044 ".---", //J 00045 "-.-", //K 00046 ".-..", //L 00047 "--", //M 00048 "-.", //N 00049 "---", //O 00050 ".--.", //P 00051 "--.-", //Q 00052 ".-.", //R 00053 "...", //S 00054 "-", //T 00055 "..-", //U 00056 "...-", //V 00057 ".--", //W 00058 "-..-", //X 00059 "-.--", //Y 00060 "--.." //Z 00061 }; 00062 00063 const char* const morseNumeric[] = { 00064 "-----", //0 00065 ".----", //1 00066 "..---", //2 00067 "...--", //3 00068 "....-", //4 00069 ".....", //5 00070 "-....", //6 00071 "--...", //7 00072 "---..", //8 00073 "----." //9 00074 }; 00075 00076 //Digital outputs 00077 DigitalOut onBoardLED(LED1); 00078 DigitalOut redLED(D7); 00079 DigitalOut yellowLED(D6); 00080 DigitalOut greenLED(D5); 00081 00082 //Serial Interface 00083 Serial pc(USBTX, USBRX); 00084 00085 //Digital inputs 00086 DigitalIn onBoardSwitch(USER_BUTTON); 00087 DigitalIn SW1(D4); 00088 DigitalIn SW2(D3); 00089 00090 //Thread ID for the Main function (CMSIS API) 00091 osThreadId tidMain; 00092 00093 //Thread sychronisation primatives 00094 Semaphore *spaceAvailable; 00095 Semaphore *samplesInBuffer; 00096 Mutex *bufferLock; 00097 00098 //Output buffer 00099 char buffer[BUFFERSIZE]; 00100 unsigned int newestIndex = BUFFERSIZE-1; //First time it is incremented, it will be 0 00101 unsigned int oldestIndex = BUFFERSIZE-1; 00102 00103 00104 //Producer 00105 void addCharacterToQueue(const char c) 00106 { 00107 //Is there space? 00108 int32_t Nspaces = spaceAvailable->wait(); 00109 00110 //Ok, there is space - take the lock 00111 bufferLock->lock(); 00112 redLED = 1; 00113 00114 //Update buffer 00115 newestIndex = (newestIndex+1) % BUFFERSIZE; 00116 buffer[newestIndex] = c; 00117 pc.printf("\tAdded ASCII Character: %2Xh (%c) to buffer, %d spaces available\n", c, c, Nspaces-1); 00118 00119 //Release lock 00120 bufferLock->unlock(); 00121 redLED = 0; 00122 00123 //Signal that a sample has been added 00124 samplesInBuffer->release(); 00125 } 00126 00127 //Consumer 00128 char takeCharacterFromQueue() 00129 { 00130 //Are thre any samples in the buffer 00131 int32_t Nsamples = samplesInBuffer->wait(); 00132 00133 //Ok, there are samples - take the lock 00134 bufferLock->lock(); 00135 yellowLED = 1; 00136 00137 //Update buffer - remove oldest 00138 oldestIndex = (oldestIndex+1) % BUFFERSIZE; 00139 char cc = buffer[oldestIndex]; 00140 pc.printf("\t\tTaking ASCII Character: %2Xh (%c) from buffer, %d bytes remaining\n", cc, cc, Nsamples-1); 00141 00142 //Release lock 00143 bufferLock->unlock(); 00144 yellowLED = 0; 00145 00146 //Signal there is space in the buffer 00147 spaceAvailable->release(); 00148 00149 //return a copy of the result 00150 return cc; 00151 } 00152 00153 void morseGenerator() 00154 { 00155 while (true) { 00156 00157 //Are there samples available? 00158 char nextChar = takeCharacterFromQueue(); 00159 nextChar = tolower(nextChar); 00160 00161 //Look up morse code 00162 00163 //Space between words - assumes only one 00164 if (nextChar == ' ') { 00165 Thread::wait(TWORD-TLETTER); 00166 continue; 00167 } 00168 00169 //Number? 00170 const char *nextString; 00171 if ((nextChar >= '0') && (nextChar <= '9')) { 00172 nextString = morseNumeric[nextChar - '0']; 00173 } 00174 //Character? 00175 else if ((nextChar >='a') && (nextChar <= 'z')) { 00176 nextString = morseAlpha[nextChar - 'a']; 00177 } else { 00178 //Invalid 00179 continue; 00180 } 00181 00182 //Flash morse for this letter 00183 for (unsigned int n=0; n<strlen(nextString); n++) { 00184 char symb = nextString[n]; 00185 greenLED = 1; 00186 switch (symb) { 00187 case '.': 00188 Thread::wait(TDOT); 00189 break; 00190 case '-': 00191 Thread::wait(TDASH); 00192 break; 00193 default: 00194 break; 00195 } 00196 greenLED = 0; 00197 00198 //A gap between symbols 00199 Thread::wait(TGAP); 00200 } 00201 00202 //Gap between letters 00203 Thread::wait(TLETTER-TGAP); 00204 00205 } 00206 } 00207 00208 //Main thread 00209 int main() { 00210 Thread writer; 00211 00212 redLED = 0; 00213 yellowLED = 0; 00214 greenLED = 0; 00215 00216 //Semaphores 00217 bufferLock = new Mutex(); 00218 spaceAvailable = new Semaphore(BUFFERSIZE); 00219 samplesInBuffer = new Semaphore(0); 00220 00221 00222 //Main thread ID 00223 tidMain = Thread::gettid(); 00224 00225 00226 //Thread for outputting mors 00227 writer.start(morseGenerator); 00228 00229 pc.printf("Type characters to send\n"); 00230 while (true) { 00231 //Read keyboard (serial port) 00232 char c = pc.getc(); 00233 addCharacterToQueue(c); 00234 } 00235 00236 } 00237 00238
Generated on Mon Jul 18 2022 01:25:45 by
1.7.2