Yifeng Yuan
/
ese519_lab1_extra1
for extra credit 1
main.cpp@2:7eeefc3bbca4, 2015-02-08 (annotated)
- Committer:
- yifeng021
- Date:
- Sun Feb 08 20:44:21 2015 +0000
- Revision:
- 2:7eeefc3bbca4
- Parent:
- 1:fb9ce7e54c8d
extra1
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
yifeng021 | 0:893e598b3e49 | 1 | /* |
yifeng021 | 0:893e598b3e49 | 2 | #include "mbed.h" |
yifeng021 | 0:893e598b3e49 | 3 | |
yifeng021 | 0:893e598b3e49 | 4 | |
yifeng021 | 0:893e598b3e49 | 5 | int main() { |
yifeng021 | 0:893e598b3e49 | 6 | while(1) { |
yifeng021 | 0:893e598b3e49 | 7 | for (float p = 0.0f; p < 1.0f; p += 0.1f) { |
yifeng021 | 0:893e598b3e49 | 8 | pwm1 = p; |
yifeng021 | 0:893e598b3e49 | 9 | wait(0.4); |
yifeng021 | 0:893e598b3e49 | 10 | } |
yifeng021 | 0:893e598b3e49 | 11 | } |
yifeng021 | 0:893e598b3e49 | 12 | } |
yifeng021 | 0:893e598b3e49 | 13 | */ |
yifeng021 | 0:893e598b3e49 | 14 | |
yifeng021 | 0:893e598b3e49 | 15 | /* |
yifeng021 | 0:893e598b3e49 | 16 | * ESE 519, Spring 2015, Lab 1 |
yifeng021 | 0:893e598b3e49 | 17 | * Part 5 |
yifeng021 | 0:893e598b3e49 | 18 | * Peter Gebhard, pgeb@seas.upenn.edu |
yifeng021 | 0:893e598b3e49 | 19 | * Yifeng Yuan, yifengy@seas.upenn.edu |
yifeng021 | 0:893e598b3e49 | 20 | */ |
yifeng021 | 0:893e598b3e49 | 21 | |
yifeng021 | 0:893e598b3e49 | 22 | #include "mbed.h" |
yifeng021 | 0:893e598b3e49 | 23 | |
yifeng021 | 0:893e598b3e49 | 24 | int global_code; //global variable |
yifeng021 | 0:893e598b3e49 | 25 | //-1 - error |
yifeng021 | 0:893e598b3e49 | 26 | //1 - dot |
yifeng021 | 0:893e598b3e49 | 27 | //0 - dash |
yifeng021 | 0:893e598b3e49 | 28 | //2 - space |
yifeng021 | 0:893e598b3e49 | 29 | int global_buffer; //stores the codes of each letter |
yifeng021 | 0:893e598b3e49 | 30 | int global_length; //stores the length of a coded word |
yifeng021 | 0:893e598b3e49 | 31 | |
yifeng021 | 0:893e598b3e49 | 32 | //Timeout wait_for_timeout; |
yifeng021 | 0:893e598b3e49 | 33 | Timer t; |
yifeng021 | 0:893e598b3e49 | 34 | InterruptIn en1(p21); //declear en1 as interrupt input using pin 21 |
yifeng021 | 0:893e598b3e49 | 35 | DigitalOut led1(LED1); |
yifeng021 | 0:893e598b3e49 | 36 | InterruptIn en2(p22); //declear en2 as interrupt input using pin 22 |
yifeng021 | 0:893e598b3e49 | 37 | DigitalOut led2(LED2); |
yifeng021 | 0:893e598b3e49 | 38 | DigitalOut led3(p23); |
yifeng021 | 0:893e598b3e49 | 39 | DigitalOut led4(p24); |
yifeng021 | 0:893e598b3e49 | 40 | PwmOut pwm1(p25); |
yifeng021 | 0:893e598b3e49 | 41 | |
yifeng021 | 0:893e598b3e49 | 42 | |
yifeng021 | 0:893e598b3e49 | 43 | void released(); |
yifeng021 | 0:893e598b3e49 | 44 | void depressed(); |
yifeng021 | 0:893e598b3e49 | 45 | void speaker(); |
yifeng021 | 0:893e598b3e49 | 46 | void light_7seg(); |
yifeng021 | 0:893e598b3e49 | 47 | char morse2char(int morse, int length); |
yifeng021 | 0:893e598b3e49 | 48 | int hard_limiter(int duration); |
yifeng021 | 0:893e598b3e49 | 49 | |
yifeng021 | 0:893e598b3e49 | 50 | char word[10]; |
yifeng021 | 0:893e598b3e49 | 51 | int letter[5]; |
yifeng021 | 0:893e598b3e49 | 52 | |
yifeng021 | 0:893e598b3e49 | 53 | void depressed() { |
yifeng021 | 0:893e598b3e49 | 54 | t.stop(); |
yifeng021 | 0:893e598b3e49 | 55 | int duration = t.read_ms(); |
yifeng021 | 0:893e598b3e49 | 56 | char letter; |
yifeng021 | 0:893e598b3e49 | 57 | if (duration > 400) { |
yifeng021 | 0:893e598b3e49 | 58 | //convert code into letter |
yifeng021 | 0:893e598b3e49 | 59 | letter = morse2char(global_buffer,global_length); |
yifeng021 | 0:893e598b3e49 | 60 | //display letter |
yifeng021 | 0:893e598b3e49 | 61 | printf("%c\n", letter); |
yifeng021 | 0:893e598b3e49 | 62 | //clear buffer |
yifeng021 | 0:893e598b3e49 | 63 | global_buffer = 0; |
yifeng021 | 0:893e598b3e49 | 64 | //clear length |
yifeng021 | 0:893e598b3e49 | 65 | //printf("%d\n",global_length); |
yifeng021 | 0:893e598b3e49 | 66 | global_length = 0; |
yifeng021 | 0:893e598b3e49 | 67 | |
yifeng021 | 0:893e598b3e49 | 68 | //a space is detected |
yifeng021 | 0:893e598b3e49 | 69 | global_code = 2; |
yifeng021 | 0:893e598b3e49 | 70 | //printf("You pressed the key %d\n", global_code); |
yifeng021 | 0:893e598b3e49 | 71 | letter = ' '; |
yifeng021 | 0:893e598b3e49 | 72 | //print letter |
yifeng021 | 0:893e598b3e49 | 73 | printf("%c\n",letter); |
yifeng021 | 0:893e598b3e49 | 74 | } |
yifeng021 | 0:893e598b3e49 | 75 | t.reset(); |
yifeng021 | 0:893e598b3e49 | 76 | t.start(); |
yifeng021 | 0:893e598b3e49 | 77 | } |
yifeng021 | 0:893e598b3e49 | 78 | |
yifeng021 | 0:893e598b3e49 | 79 | void released() { |
yifeng021 | 0:893e598b3e49 | 80 | t.stop(); |
yifeng021 | 0:893e598b3e49 | 81 | int duration = t.read_ms(); |
yifeng021 | 0:893e598b3e49 | 82 | global_code = hard_limiter(duration); |
yifeng021 | 0:893e598b3e49 | 83 | //printf("You pressed the key %d\n", global_code); |
yifeng021 | 0:893e598b3e49 | 84 | //append the new code into buffer |
yifeng021 | 0:893e598b3e49 | 85 | global_buffer = global_buffer << 1; |
yifeng021 | 0:893e598b3e49 | 86 | global_buffer = global_buffer + global_code; |
yifeng021 | 0:893e598b3e49 | 87 | //printf("%d\n",global_buffer); |
yifeng021 | 0:893e598b3e49 | 88 | //keep tracking length of code |
yifeng021 | 0:893e598b3e49 | 89 | global_length = global_length + 1; |
yifeng021 | 0:893e598b3e49 | 90 | light_7seg(); |
yifeng021 | 0:893e598b3e49 | 91 | speaker(); |
yifeng021 | 0:893e598b3e49 | 92 | t.reset(); |
yifeng021 | 0:893e598b3e49 | 93 | t.start(); |
yifeng021 | 0:893e598b3e49 | 94 | |
yifeng021 | 0:893e598b3e49 | 95 | } |
yifeng021 | 0:893e598b3e49 | 96 | |
yifeng021 | 0:893e598b3e49 | 97 | void speaker() { |
yifeng021 | 0:893e598b3e49 | 98 | if (global_code == 1) { |
yifeng021 | 0:893e598b3e49 | 99 | pwm1 = 0.2f; |
yifeng021 | 0:893e598b3e49 | 100 | wait(0.1); |
yifeng021 | 0:893e598b3e49 | 101 | pwm1 = 0.0f; |
yifeng021 | 0:893e598b3e49 | 102 | } |
yifeng021 | 0:893e598b3e49 | 103 | if (global_code == 0) { |
yifeng021 | 0:893e598b3e49 | 104 | pwm1 = 0.2f; |
yifeng021 | 0:893e598b3e49 | 105 | wait(0.3); |
yifeng021 | 0:893e598b3e49 | 106 | pwm1 = 0.0f; |
yifeng021 | 0:893e598b3e49 | 107 | } |
yifeng021 | 0:893e598b3e49 | 108 | } |
yifeng021 | 0:893e598b3e49 | 109 | |
yifeng021 | 0:893e598b3e49 | 110 | void light_7seg() { |
yifeng021 | 0:893e598b3e49 | 111 | |
yifeng021 | 0:893e598b3e49 | 112 | if (global_code == 1) { |
yifeng021 | 0:893e598b3e49 | 113 | led3 = 0; |
yifeng021 | 0:893e598b3e49 | 114 | wait(0.2); |
yifeng021 | 0:893e598b3e49 | 115 | led3 = 1; |
yifeng021 | 0:893e598b3e49 | 116 | } |
yifeng021 | 0:893e598b3e49 | 117 | if (global_code == 0) { |
yifeng021 | 0:893e598b3e49 | 118 | led4 = 0; |
yifeng021 | 0:893e598b3e49 | 119 | wait(0.2); |
yifeng021 | 0:893e598b3e49 | 120 | led4 = 1; |
yifeng021 | 0:893e598b3e49 | 121 | } |
yifeng021 | 0:893e598b3e49 | 122 | |
yifeng021 | 0:893e598b3e49 | 123 | } |
yifeng021 | 0:893e598b3e49 | 124 | |
yifeng021 | 0:893e598b3e49 | 125 | int hard_limiter(int duration) { |
yifeng021 | 0:893e598b3e49 | 126 | |
yifeng021 | 0:893e598b3e49 | 127 | int symbol; |
yifeng021 | 0:893e598b3e49 | 128 | |
yifeng021 | 0:893e598b3e49 | 129 | if (duration >= 30 && duration <= 200){ |
yifeng021 | 0:893e598b3e49 | 130 | symbol = 1; |
yifeng021 | 0:893e598b3e49 | 131 | } |
yifeng021 | 0:893e598b3e49 | 132 | else if (duration > 200) { |
yifeng021 | 0:893e598b3e49 | 133 | symbol = 0; |
yifeng021 | 0:893e598b3e49 | 134 | } |
yifeng021 | 0:893e598b3e49 | 135 | else { |
yifeng021 | 0:893e598b3e49 | 136 | symbol = -1; |
yifeng021 | 0:893e598b3e49 | 137 | } |
yifeng021 | 0:893e598b3e49 | 138 | return symbol; |
yifeng021 | 0:893e598b3e49 | 139 | |
yifeng021 | 0:893e598b3e49 | 140 | } |
yifeng021 | 0:893e598b3e49 | 141 | /* |
yifeng021 | 0:893e598b3e49 | 142 | int morseStr2int(char* morse) {} |
yifeng021 | 0:893e598b3e49 | 143 | */ |
yifeng021 | 0:893e598b3e49 | 144 | |
yifeng021 | 0:893e598b3e49 | 145 | char morse2char(int morse, int length) { |
yifeng021 | 0:893e598b3e49 | 146 | switch (length) { |
yifeng021 | 0:893e598b3e49 | 147 | case 1: |
yifeng021 | 0:893e598b3e49 | 148 | switch (morse) { |
yifeng021 | 0:893e598b3e49 | 149 | case 0: |
yifeng021 | 0:893e598b3e49 | 150 | return 'T'; |
yifeng021 | 0:893e598b3e49 | 151 | case 1: |
yifeng021 | 0:893e598b3e49 | 152 | return 'E'; |
yifeng021 | 0:893e598b3e49 | 153 | } |
yifeng021 | 0:893e598b3e49 | 154 | case 2: |
yifeng021 | 0:893e598b3e49 | 155 | switch (morse) { |
yifeng021 | 0:893e598b3e49 | 156 | case 0: |
yifeng021 | 0:893e598b3e49 | 157 | return 'M'; |
yifeng021 | 0:893e598b3e49 | 158 | case 1: |
yifeng021 | 0:893e598b3e49 | 159 | return 'N'; |
yifeng021 | 0:893e598b3e49 | 160 | case 2: |
yifeng021 | 0:893e598b3e49 | 161 | return 'A'; |
yifeng021 | 0:893e598b3e49 | 162 | case 3: |
yifeng021 | 0:893e598b3e49 | 163 | return 'I'; |
yifeng021 | 0:893e598b3e49 | 164 | } |
yifeng021 | 0:893e598b3e49 | 165 | case 3: |
yifeng021 | 0:893e598b3e49 | 166 | switch (morse) { |
yifeng021 | 0:893e598b3e49 | 167 | case 0: |
yifeng021 | 0:893e598b3e49 | 168 | return 'O'; |
yifeng021 | 0:893e598b3e49 | 169 | case 1: |
yifeng021 | 0:893e598b3e49 | 170 | return 'G'; |
yifeng021 | 0:893e598b3e49 | 171 | case 2: |
yifeng021 | 0:893e598b3e49 | 172 | return 'K'; |
yifeng021 | 0:893e598b3e49 | 173 | case 3: |
yifeng021 | 0:893e598b3e49 | 174 | return 'D'; |
yifeng021 | 0:893e598b3e49 | 175 | case 4: |
yifeng021 | 0:893e598b3e49 | 176 | return 'W'; |
yifeng021 | 0:893e598b3e49 | 177 | case 5: |
yifeng021 | 0:893e598b3e49 | 178 | return 'R'; |
yifeng021 | 0:893e598b3e49 | 179 | case 6: |
yifeng021 | 0:893e598b3e49 | 180 | return 'U'; |
yifeng021 | 0:893e598b3e49 | 181 | case 7: |
yifeng021 | 0:893e598b3e49 | 182 | return 'S'; |
yifeng021 | 0:893e598b3e49 | 183 | } |
yifeng021 | 0:893e598b3e49 | 184 | case 4: |
yifeng021 | 0:893e598b3e49 | 185 | switch (morse) { |
yifeng021 | 0:893e598b3e49 | 186 | case 2: |
yifeng021 | 0:893e598b3e49 | 187 | return 'Q'; |
yifeng021 | 0:893e598b3e49 | 188 | case 3: |
yifeng021 | 0:893e598b3e49 | 189 | return 'Z'; |
yifeng021 | 0:893e598b3e49 | 190 | case 4: |
yifeng021 | 0:893e598b3e49 | 191 | return 'Y'; |
yifeng021 | 0:893e598b3e49 | 192 | case 5: |
yifeng021 | 0:893e598b3e49 | 193 | return 'C'; |
yifeng021 | 0:893e598b3e49 | 194 | case 6: |
yifeng021 | 0:893e598b3e49 | 195 | return 'X'; |
yifeng021 | 0:893e598b3e49 | 196 | case 7: |
yifeng021 | 0:893e598b3e49 | 197 | return 'B'; |
yifeng021 | 0:893e598b3e49 | 198 | case 8: |
yifeng021 | 0:893e598b3e49 | 199 | return 'J'; |
yifeng021 | 0:893e598b3e49 | 200 | case 9: |
yifeng021 | 0:893e598b3e49 | 201 | return 'P'; |
yifeng021 | 0:893e598b3e49 | 202 | case 11: |
yifeng021 | 0:893e598b3e49 | 203 | return 'L'; |
yifeng021 | 0:893e598b3e49 | 204 | case 13: |
yifeng021 | 0:893e598b3e49 | 205 | return 'F'; |
yifeng021 | 0:893e598b3e49 | 206 | case 14: |
yifeng021 | 0:893e598b3e49 | 207 | return 'V'; |
yifeng021 | 0:893e598b3e49 | 208 | case 15: |
yifeng021 | 0:893e598b3e49 | 209 | return 'H'; |
yifeng021 | 0:893e598b3e49 | 210 | } |
yifeng021 | 0:893e598b3e49 | 211 | case 5: |
yifeng021 | 0:893e598b3e49 | 212 | switch (morse) { |
yifeng021 | 0:893e598b3e49 | 213 | case 0: |
yifeng021 | 0:893e598b3e49 | 214 | return '0'; |
yifeng021 | 0:893e598b3e49 | 215 | case 1: |
yifeng021 | 0:893e598b3e49 | 216 | return '9'; |
yifeng021 | 0:893e598b3e49 | 217 | case 3: |
yifeng021 | 0:893e598b3e49 | 218 | return '8'; |
yifeng021 | 0:893e598b3e49 | 219 | case 7: |
yifeng021 | 0:893e598b3e49 | 220 | return '7'; |
yifeng021 | 0:893e598b3e49 | 221 | case 15: |
yifeng021 | 0:893e598b3e49 | 222 | return '6'; |
yifeng021 | 0:893e598b3e49 | 223 | case 31: |
yifeng021 | 0:893e598b3e49 | 224 | return '5'; |
yifeng021 | 0:893e598b3e49 | 225 | case 30: |
yifeng021 | 0:893e598b3e49 | 226 | return '4'; |
yifeng021 | 0:893e598b3e49 | 227 | case 28: |
yifeng021 | 0:893e598b3e49 | 228 | return '3'; |
yifeng021 | 0:893e598b3e49 | 229 | case 24: |
yifeng021 | 0:893e598b3e49 | 230 | return '2'; |
yifeng021 | 0:893e598b3e49 | 231 | case 16: |
yifeng021 | 0:893e598b3e49 | 232 | return '1'; |
yifeng021 | 0:893e598b3e49 | 233 | } |
yifeng021 | 0:893e598b3e49 | 234 | } |
yifeng021 | 1:fb9ce7e54c8d | 235 | return '\0'; |
yifeng021 | 0:893e598b3e49 | 236 | } |
yifeng021 | 0:893e598b3e49 | 237 | |
yifeng021 | 0:893e598b3e49 | 238 | int main() { |
yifeng021 | 0:893e598b3e49 | 239 | |
yifeng021 | 0:893e598b3e49 | 240 | led3 = 1; |
yifeng021 | 0:893e598b3e49 | 241 | led4 = 1; |
yifeng021 | 1:fb9ce7e54c8d | 242 | pwm1 = 0; |
yifeng021 | 0:893e598b3e49 | 243 | en1.rise(&depressed); |
yifeng021 | 0:893e598b3e49 | 244 | en1.fall(&released); |
yifeng021 | 0:893e598b3e49 | 245 | t.start(); |
yifeng021 | 0:893e598b3e49 | 246 | |
yifeng021 | 0:893e598b3e49 | 247 | while (1) { |
yifeng021 | 0:893e598b3e49 | 248 | wait(0.5); |
yifeng021 | 0:893e598b3e49 | 249 | } |
yifeng021 | 0:893e598b3e49 | 250 | } |