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.
Dependencies: ELEC350-Practicals-FZ429
Fork of Task622Solution-mbedos54 by
main.cpp@3:423191a375dc, 2016-03-09 (annotated)
- Committer:
 - noutram
 - Date:
 - Wed Mar 09 10:12:51 2016 +0000
 - Revision:
 - 3:423191a375dc
 - Parent:
 - 2:70084af839d3
 - Child:
 - 4:dae8898e55fe
 
Now echos state of the buffer as data are added and removed.
Who changed what in which revision?
| User | Revision | Line number | New contents of line | 
|---|---|---|---|
| noutram | 0:f916cefba2f4 | 1 | #include "mbed.h" | 
| noutram | 0:f916cefba2f4 | 2 | #include "rtos.h" | 
| noutram | 2:70084af839d3 | 3 | #include "string.h" | 
| noutram | 2:70084af839d3 | 4 | #include <stdio.h> | 
| noutram | 2:70084af839d3 | 5 | #include <ctype.h> | 
| noutram | 0:f916cefba2f4 | 6 | |
| noutram | 0:f916cefba2f4 | 7 | #define RED_DONE 1 | 
| noutram | 0:f916cefba2f4 | 8 | #define YELLOW_DONE 2 | 
| noutram | 0:f916cefba2f4 | 9 | |
| noutram | 2:70084af839d3 | 10 | //Delays in ms | 
| noutram | 2:70084af839d3 | 11 | #define TUNIT 250 | 
| noutram | 2:70084af839d3 | 12 | #define TDOT TUNIT | 
| noutram | 2:70084af839d3 | 13 | #define TDASH (3*TUNIT) | 
| noutram | 2:70084af839d3 | 14 | #define TGAP TUNIT | 
| noutram | 2:70084af839d3 | 15 | #define TLETTER (3*TUNIT) | 
| noutram | 2:70084af839d3 | 16 | #define TWORD (7*TUNIT) | 
| noutram | 0:f916cefba2f4 | 17 | |
| noutram | 2:70084af839d3 | 18 | //Size of the morse character buffer | 
| noutram | 3:423191a375dc | 19 | #define BUFFERSIZE 100 | 
| noutram | 2:70084af839d3 | 20 | |
| noutram | 2:70084af839d3 | 21 | |
| noutram | 2:70084af839d3 | 22 | /* | 
| noutram | 2:70084af839d3 | 23 | **** Morse Code *** | 
| noutram | 2:70084af839d3 | 24 | |
| noutram | 2:70084af839d3 | 25 | . 1 unit | 
| noutram | 2:70084af839d3 | 26 | - 3 units | 
| noutram | 2:70084af839d3 | 27 | inter-space 1 unit | 
| noutram | 2:70084af839d3 | 28 | |
| noutram | 2:70084af839d3 | 29 | between letters 3 units | 
| noutram | 2:70084af839d3 | 30 | between words 7 units | 
| noutram | 2:70084af839d3 | 31 | */ | 
| noutram | 2:70084af839d3 | 32 | |
| noutram | 2:70084af839d3 | 33 | //Morse code | 
| noutram | 2:70084af839d3 | 34 | const char* const morseAlpha[] = { | 
| noutram | 2:70084af839d3 | 35 | ".-", //A | 
| noutram | 2:70084af839d3 | 36 | "-...", //B | 
| noutram | 2:70084af839d3 | 37 | "-.-.", //C | 
| noutram | 2:70084af839d3 | 38 | "-..", //D | 
| noutram | 2:70084af839d3 | 39 | ".", //E | 
| noutram | 2:70084af839d3 | 40 | "..-.", //F | 
| noutram | 2:70084af839d3 | 41 | "--.", //G | 
| noutram | 2:70084af839d3 | 42 | "....", //H | 
| noutram | 2:70084af839d3 | 43 | "..", //I | 
| noutram | 2:70084af839d3 | 44 | ".---", //J | 
| noutram | 2:70084af839d3 | 45 | "-.-", //K | 
| noutram | 2:70084af839d3 | 46 | ".-..", //L | 
| noutram | 2:70084af839d3 | 47 | "--", //M | 
| noutram | 2:70084af839d3 | 48 | "-.", //N | 
| noutram | 2:70084af839d3 | 49 | "---", //O | 
| noutram | 2:70084af839d3 | 50 | ".--.", //P | 
| noutram | 2:70084af839d3 | 51 | "--.-", //Q | 
| noutram | 2:70084af839d3 | 52 | ".-.", //R | 
| noutram | 2:70084af839d3 | 53 | "...", //S | 
| noutram | 2:70084af839d3 | 54 | "-", //T | 
| noutram | 2:70084af839d3 | 55 | "..-", //U | 
| noutram | 2:70084af839d3 | 56 | "...-", //V | 
| noutram | 2:70084af839d3 | 57 | ".--", //W | 
| noutram | 2:70084af839d3 | 58 | "-..-", //X | 
| noutram | 2:70084af839d3 | 59 | "-.--", //Y | 
| noutram | 2:70084af839d3 | 60 | "--.." //Z | 
| noutram | 2:70084af839d3 | 61 | }; | 
| noutram | 2:70084af839d3 | 62 | |
| noutram | 2:70084af839d3 | 63 | const char* const morseNumeric[] = { | 
| noutram | 2:70084af839d3 | 64 | "-----", //0 | 
| noutram | 2:70084af839d3 | 65 | ".----", //1 | 
| noutram | 2:70084af839d3 | 66 | "..---", //2 | 
| noutram | 2:70084af839d3 | 67 | "...--", //3 | 
| noutram | 2:70084af839d3 | 68 | "....-", //4 | 
| noutram | 2:70084af839d3 | 69 | ".....", //5 | 
| noutram | 2:70084af839d3 | 70 | "-....", //6 | 
| noutram | 2:70084af839d3 | 71 | "--...", //7 | 
| noutram | 2:70084af839d3 | 72 | "---..", //8 | 
| noutram | 2:70084af839d3 | 73 | "----." //9 | 
| noutram | 2:70084af839d3 | 74 | }; | 
| noutram | 0:f916cefba2f4 | 75 | |
| noutram | 0:f916cefba2f4 | 76 | //Digital outputs | 
| noutram | 0:f916cefba2f4 | 77 | DigitalOut onBoardLED(LED1); | 
| noutram | 0:f916cefba2f4 | 78 | DigitalOut redLED(D7); | 
| noutram | 0:f916cefba2f4 | 79 | DigitalOut yellowLED(D6); | 
| noutram | 0:f916cefba2f4 | 80 | DigitalOut greenLED(D5); | 
| noutram | 0:f916cefba2f4 | 81 | |
| noutram | 2:70084af839d3 | 82 | //Serial Interface | 
| noutram | 2:70084af839d3 | 83 | Serial pc(USBTX, USBRX); | 
| noutram | 2:70084af839d3 | 84 | |
| noutram | 0:f916cefba2f4 | 85 | //Digital inputs | 
| noutram | 0:f916cefba2f4 | 86 | DigitalIn onBoardSwitch(USER_BUTTON); | 
| noutram | 0:f916cefba2f4 | 87 | DigitalIn SW1(D4); | 
| noutram | 0:f916cefba2f4 | 88 | DigitalIn SW2(D3); | 
| noutram | 0:f916cefba2f4 | 89 | |
| noutram | 0:f916cefba2f4 | 90 | //Thread ID for the Main function (CMSIS API) | 
| noutram | 0:f916cefba2f4 | 91 | osThreadId tidMain; | 
| noutram | 0:f916cefba2f4 | 92 | |
| noutram | 2:70084af839d3 | 93 | //Thread sychronisation primatives | 
| noutram | 2:70084af839d3 | 94 | Semaphore *spaceAvailable; | 
| noutram | 2:70084af839d3 | 95 | Semaphore *samplesInBuffer; | 
| noutram | 2:70084af839d3 | 96 | Mutex *bufferLock; | 
| noutram | 2:70084af839d3 | 97 | |
| noutram | 2:70084af839d3 | 98 | //Output buffer | 
| noutram | 2:70084af839d3 | 99 | char buffer[BUFFERSIZE]; | 
| noutram | 2:70084af839d3 | 100 | unsigned int newestIndex = BUFFERSIZE-1; //First time it is incremented, it will be 0 | 
| noutram | 2:70084af839d3 | 101 | unsigned int oldestIndex = BUFFERSIZE-1; | 
| noutram | 0:f916cefba2f4 | 102 | |
| noutram | 2:70084af839d3 | 103 | |
| noutram | 2:70084af839d3 | 104 | //Producer | 
| noutram | 2:70084af839d3 | 105 | void addCharacterToQueue(const char c) | 
| noutram | 2:70084af839d3 | 106 | { | 
| noutram | 2:70084af839d3 | 107 | //Is there space? | 
| noutram | 3:423191a375dc | 108 | int32_t Nspaces = spaceAvailable->wait(); | 
| noutram | 2:70084af839d3 | 109 | |
| noutram | 2:70084af839d3 | 110 | //Ok, there is space - take the lock | 
| noutram | 2:70084af839d3 | 111 | bufferLock->lock(); | 
| noutram | 2:70084af839d3 | 112 | redLED = 1; | 
| noutram | 1:4fb27aea76b2 | 113 | |
| noutram | 2:70084af839d3 | 114 | //Update buffer | 
| noutram | 2:70084af839d3 | 115 | newestIndex = (newestIndex+1) % BUFFERSIZE; | 
| noutram | 2:70084af839d3 | 116 | buffer[newestIndex] = c; | 
| noutram | 3:423191a375dc | 117 | pc.printf("\tAdded ASCII Character: %2Xh (%c) to buffer, %d spaces available\n", c, c, Nspaces-1); | 
| noutram | 3:423191a375dc | 118 | |
| noutram | 2:70084af839d3 | 119 | //Release lock | 
| noutram | 2:70084af839d3 | 120 | bufferLock->unlock(); | 
| noutram | 2:70084af839d3 | 121 | redLED = 0; | 
| noutram | 2:70084af839d3 | 122 | |
| noutram | 2:70084af839d3 | 123 | //Signal that a sample has been added | 
| noutram | 2:70084af839d3 | 124 | samplesInBuffer->release(); | 
| noutram | 2:70084af839d3 | 125 | } | 
| noutram | 2:70084af839d3 | 126 | |
| noutram | 2:70084af839d3 | 127 | //Consumer | 
| noutram | 2:70084af839d3 | 128 | char takeCharacterFromQueue() | 
| noutram | 2:70084af839d3 | 129 | { | 
| noutram | 2:70084af839d3 | 130 | //Are thre any samples in the buffer | 
| noutram | 3:423191a375dc | 131 | int32_t Nsamples = samplesInBuffer->wait(); | 
| noutram | 1:4fb27aea76b2 | 132 | |
| noutram | 2:70084af839d3 | 133 | //Ok, there are samples - take the lock | 
| noutram | 2:70084af839d3 | 134 | bufferLock->lock(); | 
| noutram | 2:70084af839d3 | 135 | yellowLED = 1; | 
| noutram | 2:70084af839d3 | 136 | |
| noutram | 2:70084af839d3 | 137 | //Update buffer - remove oldest | 
| noutram | 2:70084af839d3 | 138 | oldestIndex = (oldestIndex+1) % BUFFERSIZE; | 
| noutram | 2:70084af839d3 | 139 | char cc = buffer[oldestIndex]; | 
| noutram | 3:423191a375dc | 140 | pc.printf("\t\tTaking ASCII Character: %2Xh (%c) from buffer, %d bytes remaining\n", cc, cc, Nsamples-1); | 
| noutram | 0:f916cefba2f4 | 141 | |
| noutram | 2:70084af839d3 | 142 | //Release lock | 
| noutram | 2:70084af839d3 | 143 | bufferLock->unlock(); | 
| noutram | 2:70084af839d3 | 144 | yellowLED = 0; | 
| noutram | 2:70084af839d3 | 145 | |
| noutram | 2:70084af839d3 | 146 | //Signal there is space in the buffer | 
| noutram | 2:70084af839d3 | 147 | spaceAvailable->release(); | 
| noutram | 2:70084af839d3 | 148 | |
| noutram | 2:70084af839d3 | 149 | //return a copy of the result | 
| noutram | 2:70084af839d3 | 150 | return cc; | 
| noutram | 0:f916cefba2f4 | 151 | } | 
| noutram | 0:f916cefba2f4 | 152 | |
| noutram | 2:70084af839d3 | 153 | void morseGenerator( const void* arg ) | 
| noutram | 0:f916cefba2f4 | 154 | { | 
| noutram | 2:70084af839d3 | 155 | while (true) { | 
| noutram | 2:70084af839d3 | 156 | |
| noutram | 2:70084af839d3 | 157 | //Are there samples available? | 
| noutram | 2:70084af839d3 | 158 | char nextChar = takeCharacterFromQueue(); | 
| noutram | 2:70084af839d3 | 159 | nextChar = tolower(nextChar); | 
| noutram | 2:70084af839d3 | 160 | |
| noutram | 2:70084af839d3 | 161 | //Look up morse code | 
| noutram | 2:70084af839d3 | 162 | |
| noutram | 2:70084af839d3 | 163 | //Space between words - assumes only one | 
| noutram | 2:70084af839d3 | 164 | if (nextChar == ' ') { | 
| noutram | 2:70084af839d3 | 165 | Thread::wait(TWORD-TLETTER); | 
| noutram | 2:70084af839d3 | 166 | continue; | 
| noutram | 2:70084af839d3 | 167 | } | 
| noutram | 2:70084af839d3 | 168 | |
| noutram | 2:70084af839d3 | 169 | //Number? | 
| noutram | 2:70084af839d3 | 170 | const char *nextString; | 
| noutram | 2:70084af839d3 | 171 | if ((nextChar >= '0') && (nextChar <= '9')) { | 
| noutram | 2:70084af839d3 | 172 | nextString = morseNumeric[nextChar - '0']; | 
| noutram | 2:70084af839d3 | 173 | } | 
| noutram | 2:70084af839d3 | 174 | //Character? | 
| noutram | 2:70084af839d3 | 175 | else if ((nextChar >='a') && (nextChar <= 'z')) { | 
| noutram | 2:70084af839d3 | 176 | nextString = morseAlpha[nextChar - 'a']; | 
| noutram | 2:70084af839d3 | 177 | } else { | 
| noutram | 2:70084af839d3 | 178 | //Invalid | 
| noutram | 2:70084af839d3 | 179 | continue; | 
| noutram | 2:70084af839d3 | 180 | } | 
| noutram | 1:4fb27aea76b2 | 181 | |
| noutram | 2:70084af839d3 | 182 | //Flash morse for this letter | 
| noutram | 2:70084af839d3 | 183 | for (unsigned int n=0; n<strlen(nextString); n++) { | 
| noutram | 2:70084af839d3 | 184 | char symb = nextString[n]; | 
| noutram | 2:70084af839d3 | 185 | greenLED = 1; | 
| noutram | 2:70084af839d3 | 186 | switch (symb) { | 
| noutram | 2:70084af839d3 | 187 | case '.': | 
| noutram | 2:70084af839d3 | 188 | Thread::wait(TDOT); | 
| noutram | 2:70084af839d3 | 189 | break; | 
| noutram | 2:70084af839d3 | 190 | case '-': | 
| noutram | 2:70084af839d3 | 191 | Thread::wait(TDASH); | 
| noutram | 2:70084af839d3 | 192 | break; | 
| noutram | 2:70084af839d3 | 193 | default: | 
| noutram | 2:70084af839d3 | 194 | break; | 
| noutram | 2:70084af839d3 | 195 | } | 
| noutram | 2:70084af839d3 | 196 | greenLED = 0; | 
| noutram | 2:70084af839d3 | 197 | |
| noutram | 2:70084af839d3 | 198 | //A gap between symbols | 
| noutram | 2:70084af839d3 | 199 | Thread::wait(TGAP); | 
| noutram | 2:70084af839d3 | 200 | } | 
| noutram | 1:4fb27aea76b2 | 201 | |
| noutram | 2:70084af839d3 | 202 | //Gap between letters | 
| noutram | 2:70084af839d3 | 203 | Thread::wait(TLETTER-TGAP); | 
| noutram | 2:70084af839d3 | 204 | |
| noutram | 2:70084af839d3 | 205 | } | 
| noutram | 0:f916cefba2f4 | 206 | } | 
| noutram | 0:f916cefba2f4 | 207 | |
| noutram | 0:f916cefba2f4 | 208 | //Main thread | 
| noutram | 0:f916cefba2f4 | 209 | int main() { | 
| noutram | 0:f916cefba2f4 | 210 | redLED = 0; | 
| noutram | 0:f916cefba2f4 | 211 | yellowLED = 0; | 
| noutram | 2:70084af839d3 | 212 | greenLED = 0; | 
| noutram | 2:70084af839d3 | 213 | |
| noutram | 2:70084af839d3 | 214 | //Semaphores | 
| noutram | 2:70084af839d3 | 215 | bufferLock = new Mutex(); | 
| noutram | 2:70084af839d3 | 216 | spaceAvailable = new Semaphore(BUFFERSIZE); | 
| noutram | 2:70084af839d3 | 217 | samplesInBuffer = new Semaphore(0); | 
| noutram | 2:70084af839d3 | 218 | |
| noutram | 0:f916cefba2f4 | 219 | |
| noutram | 0:f916cefba2f4 | 220 | //Main thread ID | 
| noutram | 0:f916cefba2f4 | 221 | tidMain = Thread::gettid(); | 
| noutram | 0:f916cefba2f4 | 222 | |
| noutram | 2:70084af839d3 | 223 | |
| noutram | 2:70084af839d3 | 224 | //Thread for outputting mors | 
| noutram | 2:70084af839d3 | 225 | Thread writer(morseGenerator); | 
| noutram | 1:4fb27aea76b2 | 226 | |
| noutram | 2:70084af839d3 | 227 | pc.printf("Type characters to send\n"); | 
| noutram | 2:70084af839d3 | 228 | while (true) { | 
| noutram | 2:70084af839d3 | 229 | //Read keyboard (serial port) | 
| noutram | 2:70084af839d3 | 230 | char c = pc.getc(); | 
| noutram | 2:70084af839d3 | 231 | addCharacterToQueue(c); | 
| noutram | 0:f916cefba2f4 | 232 | } | 
| noutram | 0:f916cefba2f4 | 233 | |
| noutram | 0:f916cefba2f4 | 234 | } | 
| noutram | 2:70084af839d3 | 235 | |
| noutram | 2:70084af839d3 | 236 | 
