display letter on 7-segment

Dependencies:   mbed

Committer:
yifeng021
Date:
Tue Feb 03 21:52:22 2015 +0000
Revision:
0:6ebe4f2554ad
Child:
1:ad5433d86676
display letter on 7 segment

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 0:6ebe4f2554ad 13 //0 - dash
yifeng021 0:6ebe4f2554ad 14 //2 - 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 //Timeout wait_for_timeout;
yifeng021 0:6ebe4f2554ad 19 Timer t;
yifeng021 0:6ebe4f2554ad 20 Timeout timeout_function;
yifeng021 0:6ebe4f2554ad 21 InterruptIn en1(p21); //declear en1 as interrupt input using pin 21
yifeng021 0:6ebe4f2554ad 22 //DigitalOut led1(LED1);
yifeng021 0:6ebe4f2554ad 23 InterruptIn en2(p22); //declear en2 as interrupt input using pin 22
yifeng021 0:6ebe4f2554ad 24 //DigitalOut led2(LED2);
yifeng021 0:6ebe4f2554ad 25 DigitalOut dp2(p23);
yifeng021 0:6ebe4f2554ad 26 DigitalOut d(p24);
yifeng021 0:6ebe4f2554ad 27 DigitalOut c(p25);
yifeng021 0:6ebe4f2554ad 28 DigitalOut g(p26);
yifeng021 0:6ebe4f2554ad 29 DigitalOut b(p27);
yifeng021 0:6ebe4f2554ad 30 DigitalOut e(p28);
yifeng021 0:6ebe4f2554ad 31 DigitalOut f(p29);
yifeng021 0:6ebe4f2554ad 32 DigitalOut a(p30);
yifeng021 0:6ebe4f2554ad 33 //PwmOut pwm1(p05);
yifeng021 0:6ebe4f2554ad 34
yifeng021 0:6ebe4f2554ad 35 void released();
yifeng021 0:6ebe4f2554ad 36 void depressed();
yifeng021 0:6ebe4f2554ad 37 void light_7seg();
yifeng021 0:6ebe4f2554ad 38 char morse2char(int morse, int length);
yifeng021 0:6ebe4f2554ad 39 int hard_limiter(int duration);
yifeng021 0:6ebe4f2554ad 40 void print_symbol();
yifeng021 0:6ebe4f2554ad 41 void print_word();
yifeng021 0:6ebe4f2554ad 42 void show_letter_7seg(char letter);
yifeng021 0:6ebe4f2554ad 43
yifeng021 0:6ebe4f2554ad 44 char word[10];
yifeng021 0:6ebe4f2554ad 45 int letter[5];
yifeng021 0:6ebe4f2554ad 46
yifeng021 0:6ebe4f2554ad 47 void depressed() {
yifeng021 0:6ebe4f2554ad 48 timeout_function.detach();
yifeng021 0:6ebe4f2554ad 49 t.stop();
yifeng021 0:6ebe4f2554ad 50 t.reset();
yifeng021 0:6ebe4f2554ad 51 t.start();
yifeng021 0:6ebe4f2554ad 52
yifeng021 0:6ebe4f2554ad 53 }
yifeng021 0:6ebe4f2554ad 54
yifeng021 0:6ebe4f2554ad 55 void released() {
yifeng021 0:6ebe4f2554ad 56 t.stop();
yifeng021 0:6ebe4f2554ad 57 int duration = t.read_ms();
yifeng021 0:6ebe4f2554ad 58 global_code = hard_limiter(duration);
yifeng021 0:6ebe4f2554ad 59 //printf("You pressed the key %d\n", global_code);
yifeng021 0:6ebe4f2554ad 60 //append the new code into buffer
yifeng021 0:6ebe4f2554ad 61 global_buffer = global_buffer << 1;
yifeng021 0:6ebe4f2554ad 62 global_buffer = global_buffer + global_code;
yifeng021 0:6ebe4f2554ad 63 //printf("%d\n",global_buffer);
yifeng021 0:6ebe4f2554ad 64 //keep tracking length of code
yifeng021 0:6ebe4f2554ad 65 global_length = global_length + 1;
yifeng021 0:6ebe4f2554ad 66 light_7seg();
yifeng021 0:6ebe4f2554ad 67 t.reset();
yifeng021 0:6ebe4f2554ad 68 t.start();
yifeng021 0:6ebe4f2554ad 69 //add timeout
yifeng021 0:6ebe4f2554ad 70 timeout_function.attach(&print_symbol,0.5);
yifeng021 0:6ebe4f2554ad 71
yifeng021 0:6ebe4f2554ad 72 }
yifeng021 0:6ebe4f2554ad 73
yifeng021 0:6ebe4f2554ad 74 void print_symbol() {
yifeng021 0:6ebe4f2554ad 75
yifeng021 0:6ebe4f2554ad 76 char letter;
yifeng021 0:6ebe4f2554ad 77
yifeng021 0:6ebe4f2554ad 78 //convert code into letter
yifeng021 0:6ebe4f2554ad 79 letter = morse2char(global_buffer,global_length);
yifeng021 0:6ebe4f2554ad 80 //display letter
yifeng021 0:6ebe4f2554ad 81 printf("%c", letter);
yifeng021 0:6ebe4f2554ad 82 show_letter_7seg(letter);
yifeng021 0:6ebe4f2554ad 83 //clear buffer
yifeng021 0:6ebe4f2554ad 84 global_buffer = 0;
yifeng021 0:6ebe4f2554ad 85 //clear length
yifeng021 0:6ebe4f2554ad 86 //printf("%d\n",global_length);
yifeng021 0:6ebe4f2554ad 87 global_length = 0;
yifeng021 0:6ebe4f2554ad 88
yifeng021 0:6ebe4f2554ad 89 //a space is detected
yifeng021 0:6ebe4f2554ad 90 global_code = 2;
yifeng021 0:6ebe4f2554ad 91 //printf("You pressed the key %d\n", global_code);
yifeng021 0:6ebe4f2554ad 92 //letter = '_';
yifeng021 0:6ebe4f2554ad 93 //print letter
yifeng021 0:6ebe4f2554ad 94 //printf("_");
yifeng021 0:6ebe4f2554ad 95
yifeng021 0:6ebe4f2554ad 96 timeout_function.detach();
yifeng021 0:6ebe4f2554ad 97 timeout_function.attach(&print_word,1.5);
yifeng021 0:6ebe4f2554ad 98
yifeng021 0:6ebe4f2554ad 99
yifeng021 0:6ebe4f2554ad 100 }
yifeng021 0:6ebe4f2554ad 101
yifeng021 0:6ebe4f2554ad 102 void print_word() {
yifeng021 0:6ebe4f2554ad 103
yifeng021 0:6ebe4f2554ad 104 printf(" ");
yifeng021 0:6ebe4f2554ad 105
yifeng021 0:6ebe4f2554ad 106 }
yifeng021 0:6ebe4f2554ad 107
yifeng021 0:6ebe4f2554ad 108 void light_7seg() {
yifeng021 0:6ebe4f2554ad 109
yifeng021 0:6ebe4f2554ad 110 if (global_code == 1) {
yifeng021 0:6ebe4f2554ad 111 dp2 = 0;
yifeng021 0:6ebe4f2554ad 112 wait(0.2);
yifeng021 0:6ebe4f2554ad 113 dp2 = 1;
yifeng021 0:6ebe4f2554ad 114 }
yifeng021 0:6ebe4f2554ad 115 if (global_code == 0) {
yifeng021 0:6ebe4f2554ad 116 d = 0;
yifeng021 0:6ebe4f2554ad 117 wait(0.2);
yifeng021 0:6ebe4f2554ad 118 d = 1;
yifeng021 0:6ebe4f2554ad 119 }
yifeng021 0:6ebe4f2554ad 120
yifeng021 0:6ebe4f2554ad 121 }
yifeng021 0:6ebe4f2554ad 122
yifeng021 0:6ebe4f2554ad 123 void show_letter_7seg(char letter) {
yifeng021 0:6ebe4f2554ad 124
yifeng021 0:6ebe4f2554ad 125 float wait_time = 0.2;
yifeng021 0:6ebe4f2554ad 126 switch(letter) {
yifeng021 0:6ebe4f2554ad 127 case 'A':
yifeng021 0:6ebe4f2554ad 128 e = f = a = b = c = g = 0;
yifeng021 0:6ebe4f2554ad 129 wait(wait_time);
yifeng021 0:6ebe4f2554ad 130 e = f = a = b = c = g = 1;
yifeng021 0:6ebe4f2554ad 131 break;
yifeng021 0:6ebe4f2554ad 132 case 'C':
yifeng021 0:6ebe4f2554ad 133 a = f = e = d = 0;
yifeng021 0:6ebe4f2554ad 134 wait(wait_time);
yifeng021 0:6ebe4f2554ad 135 a = f = e = d = 1;
yifeng021 0:6ebe4f2554ad 136 break;
yifeng021 0:6ebe4f2554ad 137 case 'E':
yifeng021 0:6ebe4f2554ad 138 a = f = g = e = d = 0;
yifeng021 0:6ebe4f2554ad 139 wait(wait_time);
yifeng021 0:6ebe4f2554ad 140 a = f = g = e = d = 1;
yifeng021 0:6ebe4f2554ad 141 break;
yifeng021 0:6ebe4f2554ad 142 case 'F':
yifeng021 0:6ebe4f2554ad 143 a = f = g = e = 0;
yifeng021 0:6ebe4f2554ad 144 wait(wait_time);
yifeng021 0:6ebe4f2554ad 145 a = f = g = e = 1;
yifeng021 0:6ebe4f2554ad 146 break;
yifeng021 0:6ebe4f2554ad 147 case 'G':
yifeng021 0:6ebe4f2554ad 148 a = f = e = d = c = 0;
yifeng021 0:6ebe4f2554ad 149 wait(wait_time);
yifeng021 0:6ebe4f2554ad 150 a = f = e = d = c = 1;
yifeng021 0:6ebe4f2554ad 151 break;
yifeng021 0:6ebe4f2554ad 152 case 'H':
yifeng021 0:6ebe4f2554ad 153 f = b = g = e = c = 0;
yifeng021 0:6ebe4f2554ad 154 wait(wait_time);
yifeng021 0:6ebe4f2554ad 155 f = b = g = e = c = 1;
yifeng021 0:6ebe4f2554ad 156 break;
yifeng021 0:6ebe4f2554ad 157 case 'I':
yifeng021 0:6ebe4f2554ad 158 b = c = 0;
yifeng021 0:6ebe4f2554ad 159 wait(wait_time);
yifeng021 0:6ebe4f2554ad 160 b = c = 1;
yifeng021 0:6ebe4f2554ad 161 break;
yifeng021 0:6ebe4f2554ad 162 case 'J':
yifeng021 0:6ebe4f2554ad 163 b = c = d = 0;
yifeng021 0:6ebe4f2554ad 164 wait(wait_time);
yifeng021 0:6ebe4f2554ad 165 b = c = d = 1;
yifeng021 0:6ebe4f2554ad 166 break;
yifeng021 0:6ebe4f2554ad 167 case 'L':
yifeng021 0:6ebe4f2554ad 168 f = e = d = 0;
yifeng021 0:6ebe4f2554ad 169 wait(wait_time);
yifeng021 0:6ebe4f2554ad 170 f = e = d = 1;
yifeng021 0:6ebe4f2554ad 171 break;
yifeng021 0:6ebe4f2554ad 172 case 'O':
yifeng021 0:6ebe4f2554ad 173 a = f = e = d = c = b = 0;
yifeng021 0:6ebe4f2554ad 174 wait(wait_time);
yifeng021 0:6ebe4f2554ad 175 a = f = e = d = c = b = 1;
yifeng021 0:6ebe4f2554ad 176 break;
yifeng021 0:6ebe4f2554ad 177 case 'P':
yifeng021 0:6ebe4f2554ad 178 a = b = g = f = e = 0;
yifeng021 0:6ebe4f2554ad 179 wait(wait_time);
yifeng021 0:6ebe4f2554ad 180 a = b = g = f = e = 1;
yifeng021 0:6ebe4f2554ad 181 break;
yifeng021 0:6ebe4f2554ad 182 case 'U':
yifeng021 0:6ebe4f2554ad 183 f = e = d = c = b = 0;
yifeng021 0:6ebe4f2554ad 184 wait(wait_time);
yifeng021 0:6ebe4f2554ad 185 f = e = d = c = b = 1;
yifeng021 0:6ebe4f2554ad 186 break;
yifeng021 0:6ebe4f2554ad 187 default:
yifeng021 0:6ebe4f2554ad 188 a = b = c = d = e = f = g = 1;
yifeng021 0:6ebe4f2554ad 189 break;
yifeng021 0:6ebe4f2554ad 190 }
yifeng021 0:6ebe4f2554ad 191 }
yifeng021 0:6ebe4f2554ad 192
yifeng021 0:6ebe4f2554ad 193 int hard_limiter(int duration) {
yifeng021 0:6ebe4f2554ad 194
yifeng021 0:6ebe4f2554ad 195 int symbol;
yifeng021 0:6ebe4f2554ad 196
yifeng021 0:6ebe4f2554ad 197 if (duration >= 30 && duration <= 200){
yifeng021 0:6ebe4f2554ad 198 symbol = 1;
yifeng021 0:6ebe4f2554ad 199 }
yifeng021 0:6ebe4f2554ad 200 else if (duration > 200) {
yifeng021 0:6ebe4f2554ad 201 symbol = 0;
yifeng021 0:6ebe4f2554ad 202 }
yifeng021 0:6ebe4f2554ad 203 else {
yifeng021 0:6ebe4f2554ad 204 symbol = -1;
yifeng021 0:6ebe4f2554ad 205 }
yifeng021 0:6ebe4f2554ad 206 return symbol;
yifeng021 0:6ebe4f2554ad 207
yifeng021 0:6ebe4f2554ad 208 }
yifeng021 0:6ebe4f2554ad 209
yifeng021 0:6ebe4f2554ad 210
yifeng021 0:6ebe4f2554ad 211 char morse2char(int morse, int length) {
yifeng021 0:6ebe4f2554ad 212 switch (length) {
yifeng021 0:6ebe4f2554ad 213 case 1:
yifeng021 0:6ebe4f2554ad 214 switch (morse) {
yifeng021 0:6ebe4f2554ad 215 case 0:
yifeng021 0:6ebe4f2554ad 216 return 'T';
yifeng021 0:6ebe4f2554ad 217 case 1:
yifeng021 0:6ebe4f2554ad 218 return 'E';
yifeng021 0:6ebe4f2554ad 219 }
yifeng021 0:6ebe4f2554ad 220 case 2:
yifeng021 0:6ebe4f2554ad 221 switch (morse) {
yifeng021 0:6ebe4f2554ad 222 case 0:
yifeng021 0:6ebe4f2554ad 223 return 'M';
yifeng021 0:6ebe4f2554ad 224 case 1:
yifeng021 0:6ebe4f2554ad 225 return 'N';
yifeng021 0:6ebe4f2554ad 226 case 2:
yifeng021 0:6ebe4f2554ad 227 return 'A';
yifeng021 0:6ebe4f2554ad 228 case 3:
yifeng021 0:6ebe4f2554ad 229 return 'I';
yifeng021 0:6ebe4f2554ad 230 }
yifeng021 0:6ebe4f2554ad 231 case 3:
yifeng021 0:6ebe4f2554ad 232 switch (morse) {
yifeng021 0:6ebe4f2554ad 233 case 0:
yifeng021 0:6ebe4f2554ad 234 return 'O';
yifeng021 0:6ebe4f2554ad 235 case 1:
yifeng021 0:6ebe4f2554ad 236 return 'G';
yifeng021 0:6ebe4f2554ad 237 case 2:
yifeng021 0:6ebe4f2554ad 238 return 'K';
yifeng021 0:6ebe4f2554ad 239 case 3:
yifeng021 0:6ebe4f2554ad 240 return 'D';
yifeng021 0:6ebe4f2554ad 241 case 4:
yifeng021 0:6ebe4f2554ad 242 return 'W';
yifeng021 0:6ebe4f2554ad 243 case 5:
yifeng021 0:6ebe4f2554ad 244 return 'R';
yifeng021 0:6ebe4f2554ad 245 case 6:
yifeng021 0:6ebe4f2554ad 246 return 'U';
yifeng021 0:6ebe4f2554ad 247 case 7:
yifeng021 0:6ebe4f2554ad 248 return 'S';
yifeng021 0:6ebe4f2554ad 249 }
yifeng021 0:6ebe4f2554ad 250 case 4:
yifeng021 0:6ebe4f2554ad 251 switch (morse) {
yifeng021 0:6ebe4f2554ad 252 case 2:
yifeng021 0:6ebe4f2554ad 253 return 'Q';
yifeng021 0:6ebe4f2554ad 254 case 3:
yifeng021 0:6ebe4f2554ad 255 return 'Z';
yifeng021 0:6ebe4f2554ad 256 case 4:
yifeng021 0:6ebe4f2554ad 257 return 'Y';
yifeng021 0:6ebe4f2554ad 258 case 5:
yifeng021 0:6ebe4f2554ad 259 return 'C';
yifeng021 0:6ebe4f2554ad 260 case 6:
yifeng021 0:6ebe4f2554ad 261 return 'X';
yifeng021 0:6ebe4f2554ad 262 case 7:
yifeng021 0:6ebe4f2554ad 263 return 'B';
yifeng021 0:6ebe4f2554ad 264 case 8:
yifeng021 0:6ebe4f2554ad 265 return 'J';
yifeng021 0:6ebe4f2554ad 266 case 9:
yifeng021 0:6ebe4f2554ad 267 return 'P';
yifeng021 0:6ebe4f2554ad 268 case 11:
yifeng021 0:6ebe4f2554ad 269 return 'L';
yifeng021 0:6ebe4f2554ad 270 case 13:
yifeng021 0:6ebe4f2554ad 271 return 'F';
yifeng021 0:6ebe4f2554ad 272 case 14:
yifeng021 0:6ebe4f2554ad 273 return 'V';
yifeng021 0:6ebe4f2554ad 274 case 15:
yifeng021 0:6ebe4f2554ad 275 return 'H';
yifeng021 0:6ebe4f2554ad 276 }
yifeng021 0:6ebe4f2554ad 277 case 5:
yifeng021 0:6ebe4f2554ad 278 switch (morse) {
yifeng021 0:6ebe4f2554ad 279 case 0:
yifeng021 0:6ebe4f2554ad 280 return '0';
yifeng021 0:6ebe4f2554ad 281 case 1:
yifeng021 0:6ebe4f2554ad 282 return '9';
yifeng021 0:6ebe4f2554ad 283 case 3:
yifeng021 0:6ebe4f2554ad 284 return '8';
yifeng021 0:6ebe4f2554ad 285 case 7:
yifeng021 0:6ebe4f2554ad 286 return '7';
yifeng021 0:6ebe4f2554ad 287 case 15:
yifeng021 0:6ebe4f2554ad 288 return '6';
yifeng021 0:6ebe4f2554ad 289 case 31:
yifeng021 0:6ebe4f2554ad 290 return '5';
yifeng021 0:6ebe4f2554ad 291 case 30:
yifeng021 0:6ebe4f2554ad 292 return '4';
yifeng021 0:6ebe4f2554ad 293 case 28:
yifeng021 0:6ebe4f2554ad 294 return '3';
yifeng021 0:6ebe4f2554ad 295 case 24:
yifeng021 0:6ebe4f2554ad 296 return '2';
yifeng021 0:6ebe4f2554ad 297 case 16:
yifeng021 0:6ebe4f2554ad 298 return '1';
yifeng021 0:6ebe4f2554ad 299 }
yifeng021 0:6ebe4f2554ad 300 }
yifeng021 0:6ebe4f2554ad 301 return '\0';
yifeng021 0:6ebe4f2554ad 302 }
yifeng021 0:6ebe4f2554ad 303
yifeng021 0:6ebe4f2554ad 304 int main() {
yifeng021 0:6ebe4f2554ad 305
yifeng021 0:6ebe4f2554ad 306 //setbuf(stdout,NULL);
yifeng021 0:6ebe4f2554ad 307 setvbuf(stdout,NULL,_IONBF,0);
yifeng021 0:6ebe4f2554ad 308 //pwm1 = 0.0f;
yifeng021 0:6ebe4f2554ad 309 a = b = c = d = e = f = g = dp2 = 1;
yifeng021 0:6ebe4f2554ad 310 en1.rise(&depressed);
yifeng021 0:6ebe4f2554ad 311 en1.fall(&released);
yifeng021 0:6ebe4f2554ad 312 t.start();
yifeng021 0:6ebe4f2554ad 313
yifeng021 0:6ebe4f2554ad 314 while (1) {
yifeng021 0:6ebe4f2554ad 315 wait(0.5);
yifeng021 0:6ebe4f2554ad 316 }
yifeng021 0:6ebe4f2554ad 317 }