for extra credit 1

Dependencies:   mbed

Committer:
yifeng021
Date:
Fri Jan 30 23:41:05 2015 +0000
Revision:
0:893e598b3e49
Child:
1:fb9ce7e54c8d
Buzz feature added!

Who changed what in which revision?

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