display letter on 7-segment

Dependencies:   mbed

Committer:
yifeng021
Date:
Mon Feb 09 02:16:36 2015 +0000
Revision:
3:42fb220c3e1d
Parent:
1:ad5433d86676
clean up, add comments

Who changed what in which revision?

UserRevisionLine numberNew contents of line
yifeng021 0:6ebe4f2554ad 1 /*
yifeng021 0:6ebe4f2554ad 2 * ESE 519, Spring 2015, Lab 1
yifeng021 0:6ebe4f2554ad 3 * Part 5
yifeng021 0:6ebe4f2554ad 4 * Peter Gebhard, pgeb@seas.upenn.edu
yifeng021 0:6ebe4f2554ad 5 * Yifeng Yuan, yifengy@seas.upenn.edu
yifeng021 0:6ebe4f2554ad 6 */
yifeng021 0:6ebe4f2554ad 7
yifeng021 0:6ebe4f2554ad 8 #include "mbed.h"
yifeng021 0:6ebe4f2554ad 9
yifeng021 0:6ebe4f2554ad 10 int global_code; //global variable
yifeng021 0:6ebe4f2554ad 11 //-1 - error
yifeng021 0:6ebe4f2554ad 12 //1 - dot
yifeng021 3:42fb220c3e1d 13 //2 - dash
yifeng021 3:42fb220c3e1d 14 //3 - space
yifeng021 0:6ebe4f2554ad 15 int global_buffer; //stores the codes of each letter
yifeng021 0:6ebe4f2554ad 16 int global_length; //stores the length of a coded word
yifeng021 0:6ebe4f2554ad 17
yifeng021 0:6ebe4f2554ad 18
yifeng021 3:42fb220c3e1d 19 Timer t; // Adding timer to track timing between key presses
yifeng021 3:42fb220c3e1d 20 Timeout timeout_function; //
yifeng021 3:42fb220c3e1d 21 InterruptIn en1(p21); //declare en1 as interrupt input using pin 21
yifeng021 3:42fb220c3e1d 22 InterruptIn en2(p22); //declare en2 as interrupt input using pin 22
yifeng021 3:42fb220c3e1d 23 DigitalOut dp2(p23); //7seg decimal point
yifeng021 3:42fb220c3e1d 24 DigitalOut d(p24); //7seg
yifeng021 3:42fb220c3e1d 25 DigitalOut c(p15); //7seg
yifeng021 3:42fb220c3e1d 26 DigitalOut g(p16); //7seg
yifeng021 3:42fb220c3e1d 27 DigitalOut b(p17); //7seg
yifeng021 3:42fb220c3e1d 28 DigitalOut e(p18); //7seg
yifeng021 3:42fb220c3e1d 29 DigitalOut f(p19); //7seg
yifeng021 3:42fb220c3e1d 30 DigitalOut a(p8); //7seg
yifeng021 3:42fb220c3e1d 31 PwmOut pwm1(p25); //declare pwm1 as digital output pin that provides pwm wave
yifeng021 3:42fb220c3e1d 32
yifeng021 3:42fb220c3e1d 33 // Functions declarations
yifeng021 0:6ebe4f2554ad 34 void released();
yifeng021 0:6ebe4f2554ad 35 void depressed();
yifeng021 0:6ebe4f2554ad 36 void light_7seg();
yifeng021 0:6ebe4f2554ad 37 char morse2char(int morse, int length);
yifeng021 0:6ebe4f2554ad 38 int hard_limiter(int duration);
yifeng021 0:6ebe4f2554ad 39 void print_symbol();
yifeng021 0:6ebe4f2554ad 40 void print_word();
yifeng021 0:6ebe4f2554ad 41 void show_letter_7seg(char letter);
yifeng021 0:6ebe4f2554ad 42
yifeng021 3:42fb220c3e1d 43 // This function handles what the device should do when a key is pressed
yifeng021 0:6ebe4f2554ad 44 void depressed() {
yifeng021 0:6ebe4f2554ad 45 timeout_function.detach();
yifeng021 0:6ebe4f2554ad 46 t.stop();
yifeng021 0:6ebe4f2554ad 47 t.reset();
yifeng021 0:6ebe4f2554ad 48 t.start();
yifeng021 0:6ebe4f2554ad 49 }
yifeng021 0:6ebe4f2554ad 50
yifeng021 3:42fb220c3e1d 51 // This function handles what the device should do when a key is released
yifeng021 0:6ebe4f2554ad 52 void released() {
yifeng021 0:6ebe4f2554ad 53 t.stop();
yifeng021 0:6ebe4f2554ad 54 int duration = t.read_ms();
yifeng021 0:6ebe4f2554ad 55 global_code = hard_limiter(duration);
yifeng021 0:6ebe4f2554ad 56 //printf("You pressed the key %d\n", global_code);
yifeng021 0:6ebe4f2554ad 57 //append the new code into buffer
yifeng021 0:6ebe4f2554ad 58 global_buffer = global_buffer << 1;
yifeng021 0:6ebe4f2554ad 59 global_buffer = global_buffer + global_code;
yifeng021 0:6ebe4f2554ad 60 //printf("%d\n",global_buffer);
yifeng021 0:6ebe4f2554ad 61 //keep tracking length of code
yifeng021 0:6ebe4f2554ad 62 global_length = global_length + 1;
yifeng021 0:6ebe4f2554ad 63 light_7seg();
yifeng021 0:6ebe4f2554ad 64 t.reset();
yifeng021 0:6ebe4f2554ad 65 t.start();
yifeng021 0:6ebe4f2554ad 66 //add timeout
yifeng021 0:6ebe4f2554ad 67 timeout_function.attach(&print_symbol,0.5);
yifeng021 3:42fb220c3e1d 68
yifeng021 0:6ebe4f2554ad 69 }
yifeng021 0:6ebe4f2554ad 70
yifeng021 0:6ebe4f2554ad 71 void print_symbol() {
yifeng021 0:6ebe4f2554ad 72
yifeng021 0:6ebe4f2554ad 73 char letter;
yifeng021 0:6ebe4f2554ad 74
yifeng021 3:42fb220c3e1d 75 //convert code into letter
yifeng021 3:42fb220c3e1d 76 letter = morse2char(global_buffer,global_length);
yifeng021 3:42fb220c3e1d 77 //display letter
yifeng021 3:42fb220c3e1d 78 printf("%c", letter);
yifeng021 3:42fb220c3e1d 79 show_letter_7seg(letter);
yifeng021 3:42fb220c3e1d 80 //clear buffer
yifeng021 3:42fb220c3e1d 81 global_buffer = 0;
yifeng021 3:42fb220c3e1d 82 //clear length
yifeng021 3:42fb220c3e1d 83 //printf("%d\n",global_length);
yifeng021 3:42fb220c3e1d 84 global_length = 0;
yifeng021 3:42fb220c3e1d 85 //a space is detected
yifeng021 3:42fb220c3e1d 86 global_code = 2;
yifeng021 3:42fb220c3e1d 87
yifeng021 3:42fb220c3e1d 88 timeout_function.detach();
yifeng021 3:42fb220c3e1d 89 timeout_function.attach(&print_word,1.5);
yifeng021 0:6ebe4f2554ad 90 }
yifeng021 0:6ebe4f2554ad 91
yifeng021 3:42fb220c3e1d 92 // This function prints a [space] to signal the end of a word
yifeng021 0:6ebe4f2554ad 93 void print_word() {
yifeng021 0:6ebe4f2554ad 94 printf(" ");
yifeng021 0:6ebe4f2554ad 95 }
yifeng021 0:6ebe4f2554ad 96
yifeng021 3:42fb220c3e1d 97 // Light the 7-Segment Display for a dot or dash (depending on the global_code)
yifeng021 0:6ebe4f2554ad 98 void light_7seg() {
yifeng021 0:6ebe4f2554ad 99
yifeng021 0:6ebe4f2554ad 100 if (global_code == 1) {
yifeng021 0:6ebe4f2554ad 101 dp2 = 0;
yifeng021 0:6ebe4f2554ad 102 wait(0.2);
yifeng021 0:6ebe4f2554ad 103 dp2 = 1;
yifeng021 0:6ebe4f2554ad 104 }
yifeng021 0:6ebe4f2554ad 105 if (global_code == 0) {
yifeng021 0:6ebe4f2554ad 106 d = 0;
yifeng021 0:6ebe4f2554ad 107 wait(0.2);
yifeng021 0:6ebe4f2554ad 108 d = 1;
yifeng021 0:6ebe4f2554ad 109 }
yifeng021 0:6ebe4f2554ad 110 }
yifeng021 0:6ebe4f2554ad 111
yifeng021 3:42fb220c3e1d 112 // This function displays letters on 7-segment LEDs
yifeng021 0:6ebe4f2554ad 113 void show_letter_7seg(char letter) {
yifeng021 0:6ebe4f2554ad 114
yifeng021 0:6ebe4f2554ad 115 float wait_time = 0.2;
yifeng021 0:6ebe4f2554ad 116 switch(letter) {
yifeng021 0:6ebe4f2554ad 117 case 'A':
yifeng021 0:6ebe4f2554ad 118 e = f = a = b = c = g = 0;
yifeng021 0:6ebe4f2554ad 119 wait(wait_time);
yifeng021 0:6ebe4f2554ad 120 e = f = a = b = c = g = 1;
yifeng021 0:6ebe4f2554ad 121 break;
yifeng021 0:6ebe4f2554ad 122 case 'C':
yifeng021 0:6ebe4f2554ad 123 a = f = e = d = 0;
yifeng021 0:6ebe4f2554ad 124 wait(wait_time);
yifeng021 0:6ebe4f2554ad 125 a = f = e = d = 1;
yifeng021 0:6ebe4f2554ad 126 break;
yifeng021 0:6ebe4f2554ad 127 case 'E':
yifeng021 0:6ebe4f2554ad 128 a = f = g = e = d = 0;
yifeng021 0:6ebe4f2554ad 129 wait(wait_time);
yifeng021 0:6ebe4f2554ad 130 a = f = g = e = d = 1;
yifeng021 0:6ebe4f2554ad 131 break;
yifeng021 0:6ebe4f2554ad 132 case 'F':
yifeng021 0:6ebe4f2554ad 133 a = f = g = e = 0;
yifeng021 0:6ebe4f2554ad 134 wait(wait_time);
yifeng021 0:6ebe4f2554ad 135 a = f = g = e = 1;
yifeng021 0:6ebe4f2554ad 136 break;
yifeng021 0:6ebe4f2554ad 137 case 'G':
yifeng021 0:6ebe4f2554ad 138 a = f = e = d = c = 0;
yifeng021 0:6ebe4f2554ad 139 wait(wait_time);
yifeng021 0:6ebe4f2554ad 140 a = f = e = d = c = 1;
yifeng021 0:6ebe4f2554ad 141 break;
yifeng021 0:6ebe4f2554ad 142 case 'H':
yifeng021 0:6ebe4f2554ad 143 f = b = g = e = c = 0;
yifeng021 0:6ebe4f2554ad 144 wait(wait_time);
yifeng021 0:6ebe4f2554ad 145 f = b = g = e = c = 1;
yifeng021 0:6ebe4f2554ad 146 break;
yifeng021 0:6ebe4f2554ad 147 case 'I':
yifeng021 0:6ebe4f2554ad 148 b = c = 0;
yifeng021 0:6ebe4f2554ad 149 wait(wait_time);
yifeng021 0:6ebe4f2554ad 150 b = c = 1;
yifeng021 0:6ebe4f2554ad 151 break;
yifeng021 0:6ebe4f2554ad 152 case 'J':
yifeng021 0:6ebe4f2554ad 153 b = c = d = 0;
yifeng021 0:6ebe4f2554ad 154 wait(wait_time);
yifeng021 0:6ebe4f2554ad 155 b = c = d = 1;
yifeng021 0:6ebe4f2554ad 156 break;
yifeng021 0:6ebe4f2554ad 157 case 'L':
yifeng021 0:6ebe4f2554ad 158 f = e = d = 0;
yifeng021 0:6ebe4f2554ad 159 wait(wait_time);
yifeng021 0:6ebe4f2554ad 160 f = e = d = 1;
yifeng021 0:6ebe4f2554ad 161 break;
yifeng021 0:6ebe4f2554ad 162 case 'O':
yifeng021 0:6ebe4f2554ad 163 a = f = e = d = c = b = 0;
yifeng021 0:6ebe4f2554ad 164 wait(wait_time);
yifeng021 0:6ebe4f2554ad 165 a = f = e = d = c = b = 1;
yifeng021 0:6ebe4f2554ad 166 break;
yifeng021 0:6ebe4f2554ad 167 case 'P':
yifeng021 0:6ebe4f2554ad 168 a = b = g = f = e = 0;
yifeng021 0:6ebe4f2554ad 169 wait(wait_time);
yifeng021 0:6ebe4f2554ad 170 a = b = g = f = e = 1;
yifeng021 0:6ebe4f2554ad 171 break;
yifeng021 0:6ebe4f2554ad 172 case 'U':
yifeng021 0:6ebe4f2554ad 173 f = e = d = c = b = 0;
yifeng021 0:6ebe4f2554ad 174 wait(wait_time);
yifeng021 0:6ebe4f2554ad 175 f = e = d = c = b = 1;
yifeng021 0:6ebe4f2554ad 176 break;
yifeng021 0:6ebe4f2554ad 177 default:
yifeng021 0:6ebe4f2554ad 178 a = b = c = d = e = f = g = 1;
yifeng021 0:6ebe4f2554ad 179 break;
yifeng021 0:6ebe4f2554ad 180 }
yifeng021 0:6ebe4f2554ad 181 }
yifeng021 0:6ebe4f2554ad 182
yifeng021 3:42fb220c3e1d 183 // This function handles interpretation of the key press duration to a Morse Code symbol (ie. dot, dash, space)
yifeng021 0:6ebe4f2554ad 184 int hard_limiter(int duration) {
yifeng021 0:6ebe4f2554ad 185
yifeng021 0:6ebe4f2554ad 186 int symbol;
yifeng021 0:6ebe4f2554ad 187
yifeng021 3:42fb220c3e1d 188 // Interpret a dot
yifeng021 1:ad5433d86676 189 if (duration >= 30 && duration <= 250){
yifeng021 0:6ebe4f2554ad 190 symbol = 1;
yifeng021 0:6ebe4f2554ad 191 }
yifeng021 3:42fb220c3e1d 192 // Interpret a dash
yifeng021 1:ad5433d86676 193 else if (duration > 250) {
yifeng021 0:6ebe4f2554ad 194 symbol = 0;
yifeng021 0:6ebe4f2554ad 195 }
yifeng021 3:42fb220c3e1d 196 // Interpret a space
yifeng021 0:6ebe4f2554ad 197 else {
yifeng021 0:6ebe4f2554ad 198 symbol = -1;
yifeng021 0:6ebe4f2554ad 199 }
yifeng021 0:6ebe4f2554ad 200 return symbol;
yifeng021 0:6ebe4f2554ad 201 }
yifeng021 0:6ebe4f2554ad 202
yifeng021 3:42fb220c3e1d 203 // This function takes in the integer value of morse code and the code word length as inputs, and determines the
yifeng021 3:42fb220c3e1d 204 // letter or number they represent for
yifeng021 0:6ebe4f2554ad 205 char morse2char(int morse, int length) {
yifeng021 0:6ebe4f2554ad 206 switch (length) {
yifeng021 0:6ebe4f2554ad 207 case 1:
yifeng021 0:6ebe4f2554ad 208 switch (morse) {
yifeng021 0:6ebe4f2554ad 209 case 0:
yifeng021 0:6ebe4f2554ad 210 return 'T';
yifeng021 0:6ebe4f2554ad 211 case 1:
yifeng021 0:6ebe4f2554ad 212 return 'E';
yifeng021 0:6ebe4f2554ad 213 }
yifeng021 0:6ebe4f2554ad 214 case 2:
yifeng021 0:6ebe4f2554ad 215 switch (morse) {
yifeng021 0:6ebe4f2554ad 216 case 0:
yifeng021 0:6ebe4f2554ad 217 return 'M';
yifeng021 0:6ebe4f2554ad 218 case 1:
yifeng021 0:6ebe4f2554ad 219 return 'N';
yifeng021 0:6ebe4f2554ad 220 case 2:
yifeng021 0:6ebe4f2554ad 221 return 'A';
yifeng021 0:6ebe4f2554ad 222 case 3:
yifeng021 0:6ebe4f2554ad 223 return 'I';
yifeng021 0:6ebe4f2554ad 224 }
yifeng021 0:6ebe4f2554ad 225 case 3:
yifeng021 0:6ebe4f2554ad 226 switch (morse) {
yifeng021 0:6ebe4f2554ad 227 case 0:
yifeng021 0:6ebe4f2554ad 228 return 'O';
yifeng021 0:6ebe4f2554ad 229 case 1:
yifeng021 0:6ebe4f2554ad 230 return 'G';
yifeng021 0:6ebe4f2554ad 231 case 2:
yifeng021 0:6ebe4f2554ad 232 return 'K';
yifeng021 0:6ebe4f2554ad 233 case 3:
yifeng021 0:6ebe4f2554ad 234 return 'D';
yifeng021 0:6ebe4f2554ad 235 case 4:
yifeng021 0:6ebe4f2554ad 236 return 'W';
yifeng021 0:6ebe4f2554ad 237 case 5:
yifeng021 0:6ebe4f2554ad 238 return 'R';
yifeng021 0:6ebe4f2554ad 239 case 6:
yifeng021 0:6ebe4f2554ad 240 return 'U';
yifeng021 0:6ebe4f2554ad 241 case 7:
yifeng021 0:6ebe4f2554ad 242 return 'S';
yifeng021 0:6ebe4f2554ad 243 }
yifeng021 0:6ebe4f2554ad 244 case 4:
yifeng021 0:6ebe4f2554ad 245 switch (morse) {
yifeng021 0:6ebe4f2554ad 246 case 2:
yifeng021 0:6ebe4f2554ad 247 return 'Q';
yifeng021 0:6ebe4f2554ad 248 case 3:
yifeng021 0:6ebe4f2554ad 249 return 'Z';
yifeng021 0:6ebe4f2554ad 250 case 4:
yifeng021 0:6ebe4f2554ad 251 return 'Y';
yifeng021 0:6ebe4f2554ad 252 case 5:
yifeng021 0:6ebe4f2554ad 253 return 'C';
yifeng021 0:6ebe4f2554ad 254 case 6:
yifeng021 0:6ebe4f2554ad 255 return 'X';
yifeng021 0:6ebe4f2554ad 256 case 7:
yifeng021 0:6ebe4f2554ad 257 return 'B';
yifeng021 0:6ebe4f2554ad 258 case 8:
yifeng021 0:6ebe4f2554ad 259 return 'J';
yifeng021 0:6ebe4f2554ad 260 case 9:
yifeng021 0:6ebe4f2554ad 261 return 'P';
yifeng021 0:6ebe4f2554ad 262 case 11:
yifeng021 0:6ebe4f2554ad 263 return 'L';
yifeng021 0:6ebe4f2554ad 264 case 13:
yifeng021 0:6ebe4f2554ad 265 return 'F';
yifeng021 0:6ebe4f2554ad 266 case 14:
yifeng021 0:6ebe4f2554ad 267 return 'V';
yifeng021 0:6ebe4f2554ad 268 case 15:
yifeng021 0:6ebe4f2554ad 269 return 'H';
yifeng021 0:6ebe4f2554ad 270 }
yifeng021 0:6ebe4f2554ad 271 case 5:
yifeng021 0:6ebe4f2554ad 272 switch (morse) {
yifeng021 0:6ebe4f2554ad 273 case 0:
yifeng021 0:6ebe4f2554ad 274 return '0';
yifeng021 0:6ebe4f2554ad 275 case 1:
yifeng021 0:6ebe4f2554ad 276 return '9';
yifeng021 0:6ebe4f2554ad 277 case 3:
yifeng021 0:6ebe4f2554ad 278 return '8';
yifeng021 0:6ebe4f2554ad 279 case 7:
yifeng021 0:6ebe4f2554ad 280 return '7';
yifeng021 0:6ebe4f2554ad 281 case 15:
yifeng021 0:6ebe4f2554ad 282 return '6';
yifeng021 0:6ebe4f2554ad 283 case 31:
yifeng021 0:6ebe4f2554ad 284 return '5';
yifeng021 0:6ebe4f2554ad 285 case 30:
yifeng021 0:6ebe4f2554ad 286 return '4';
yifeng021 0:6ebe4f2554ad 287 case 28:
yifeng021 0:6ebe4f2554ad 288 return '3';
yifeng021 0:6ebe4f2554ad 289 case 24:
yifeng021 0:6ebe4f2554ad 290 return '2';
yifeng021 0:6ebe4f2554ad 291 case 16:
yifeng021 0:6ebe4f2554ad 292 return '1';
yifeng021 0:6ebe4f2554ad 293 }
yifeng021 0:6ebe4f2554ad 294 }
yifeng021 0:6ebe4f2554ad 295 return '\0';
yifeng021 0:6ebe4f2554ad 296 }
yifeng021 0:6ebe4f2554ad 297
yifeng021 0:6ebe4f2554ad 298 int main() {
yifeng021 0:6ebe4f2554ad 299
yifeng021 0:6ebe4f2554ad 300 //setbuf(stdout,NULL);
yifeng021 0:6ebe4f2554ad 301 setvbuf(stdout,NULL,_IONBF,0);
yifeng021 0:6ebe4f2554ad 302 //pwm1 = 0.0f;
yifeng021 0:6ebe4f2554ad 303 a = b = c = d = e = f = g = dp2 = 1;
yifeng021 0:6ebe4f2554ad 304 en1.rise(&depressed);
yifeng021 0:6ebe4f2554ad 305 en1.fall(&released);
yifeng021 0:6ebe4f2554ad 306 t.start();
yifeng021 0:6ebe4f2554ad 307
yifeng021 0:6ebe4f2554ad 308 while (1) {
yifeng021 0:6ebe4f2554ad 309 wait(0.5);
yifeng021 0:6ebe4f2554ad 310 }
yifeng021 0:6ebe4f2554ad 311 }