Wenrui Meng
/
ese519-hw2-part4
ese519 hw2 part 4
main.cpp@1:e004fff01d0d, 2015-02-24 (annotated)
- Committer:
- hlekkala
- Date:
- Tue Feb 24 19:02:54 2015 +0000
- Revision:
- 1:e004fff01d0d
- Parent:
- 0:cfa060762ad7
final
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
wenrui | 0:cfa060762ad7 | 1 | #include "mbed.h" |
wenrui | 0:cfa060762ad7 | 2 | |
hlekkala | 1:e004fff01d0d | 3 | //Defining the keypad rows as outputs and columns as inputs |
hlekkala | 1:e004fff01d0d | 4 | Serial pc(USBTX, USBRX); |
hlekkala | 1:e004fff01d0d | 5 | DigitalOut row2(p17); |
hlekkala | 1:e004fff01d0d | 6 | DigitalOut row3(p12); |
hlekkala | 1:e004fff01d0d | 7 | DigitalOut row4(p15); |
hlekkala | 1:e004fff01d0d | 8 | DigitalOut row1(p18); |
hlekkala | 1:e004fff01d0d | 9 | |
hlekkala | 1:e004fff01d0d | 10 | DigitalIn column1(p16); |
hlekkala | 1:e004fff01d0d | 11 | DigitalIn column2(p14); |
hlekkala | 1:e004fff01d0d | 12 | DigitalIn column3(p13); |
hlekkala | 1:e004fff01d0d | 13 | DigitalIn column4(p12); |
wenrui | 0:cfa060762ad7 | 14 | |
hlekkala | 1:e004fff01d0d | 15 | DigitalOut buzzer(p22); |
hlekkala | 1:e004fff01d0d | 16 | |
hlekkala | 1:e004fff01d0d | 17 | //defining tickers |
hlekkala | 1:e004fff01d0d | 18 | Ticker rowsTick; |
hlekkala | 1:e004fff01d0d | 19 | Ticker tick; |
hlekkala | 1:e004fff01d0d | 20 | |
hlekkala | 1:e004fff01d0d | 21 | //function to flip the buzzer value for generating sounds |
hlekkala | 1:e004fff01d0d | 22 | int i = 0; |
hlekkala | 1:e004fff01d0d | 23 | |
hlekkala | 1:e004fff01d0d | 24 | //function to flip the buzzer value |
hlekkala | 1:e004fff01d0d | 25 | void outFreq(){ |
hlekkala | 1:e004fff01d0d | 26 | buzzer =! buzzer; |
hlekkala | 1:e004fff01d0d | 27 | } |
wenrui | 0:cfa060762ad7 | 28 | |
hlekkala | 1:e004fff01d0d | 29 | //defining class for sound |
hlekkala | 1:e004fff01d0d | 30 | class Speaker |
hlekkala | 1:e004fff01d0d | 31 | { |
hlekkala | 1:e004fff01d0d | 32 | public: |
hlekkala | 1:e004fff01d0d | 33 | Speaker(PinName pin) : inpPin(pin) { |
hlekkala | 1:e004fff01d0d | 34 | } |
hlekkala | 1:e004fff01d0d | 35 | //function to play the sound for according to the key pressed |
hlekkala | 1:e004fff01d0d | 36 | void Output(float frequency, float sound) { |
hlekkala | 1:e004fff01d0d | 37 | inpPin.period(1.0/frequency); |
hlekkala | 1:e004fff01d0d | 38 | inpPin = sound; |
hlekkala | 1:e004fff01d0d | 39 | } |
hlekkala | 1:e004fff01d0d | 40 | //defining the inpPin as PwmOut so that microcontroller recoginses it |
hlekkala | 1:e004fff01d0d | 41 | PwmOut inpPin; |
hlekkala | 1:e004fff01d0d | 42 | }; |
hlekkala | 1:e004fff01d0d | 43 | |
hlekkala | 1:e004fff01d0d | 44 | //iterating over the rows in a cycle |
hlekkala | 1:e004fff01d0d | 45 | void row_press() |
hlekkala | 1:e004fff01d0d | 46 | { |
hlekkala | 1:e004fff01d0d | 47 | if(i < 4) |
hlekkala | 1:e004fff01d0d | 48 | { |
hlekkala | 1:e004fff01d0d | 49 | i++; |
wenrui | 0:cfa060762ad7 | 50 | } |
hlekkala | 1:e004fff01d0d | 51 | else |
hlekkala | 1:e004fff01d0d | 52 | { |
hlekkala | 1:e004fff01d0d | 53 | i = 1; |
hlekkala | 1:e004fff01d0d | 54 | } |
wenrui | 0:cfa060762ad7 | 55 | } |
wenrui | 0:cfa060762ad7 | 56 | |
hlekkala | 1:e004fff01d0d | 57 | int main(){ |
wenrui | 0:cfa060762ad7 | 58 | |
hlekkala | 1:e004fff01d0d | 59 | //initializing all rows to zero |
hlekkala | 1:e004fff01d0d | 60 | Speaker speaker1(p21); |
hlekkala | 1:e004fff01d0d | 61 | row1 = 0; |
hlekkala | 1:e004fff01d0d | 62 | row2 = 0; |
hlekkala | 1:e004fff01d0d | 63 | row3 = 0; |
hlekkala | 1:e004fff01d0d | 64 | row4 = 0; |
hlekkala | 1:e004fff01d0d | 65 | |
hlekkala | 1:e004fff01d0d | 66 | //calling a ticker once every 4ms |
hlekkala | 1:e004fff01d0d | 67 | |
hlekkala | 1:e004fff01d0d | 68 | rowsTick.attach_us(&row_press, 4000); |
wenrui | 0:cfa060762ad7 | 69 | |
hlekkala | 1:e004fff01d0d | 70 | while(1){ |
hlekkala | 1:e004fff01d0d | 71 | //checking each row and creating the sound accordingly |
hlekkala | 1:e004fff01d0d | 72 | // Check for Row 1 |
hlekkala | 1:e004fff01d0d | 73 | if(i == 1){ |
hlekkala | 1:e004fff01d0d | 74 | row1 = 1; |
hlekkala | 1:e004fff01d0d | 75 | if(column1 == 1){ |
hlekkala | 1:e004fff01d0d | 76 | |
hlekkala | 1:e004fff01d0d | 77 | speaker1.Output(1209, 0.5); |
hlekkala | 1:e004fff01d0d | 78 | buzzer = 0; |
hlekkala | 1:e004fff01d0d | 79 | tick.attach(&outFreq, 0.5/697.0); |
hlekkala | 1:e004fff01d0d | 80 | wait_ms(16); |
hlekkala | 1:e004fff01d0d | 81 | speaker1.Output(0, 0.0); |
hlekkala | 1:e004fff01d0d | 82 | pc.printf("1"); |
hlekkala | 1:e004fff01d0d | 83 | tick.detach(); |
hlekkala | 1:e004fff01d0d | 84 | } |
hlekkala | 1:e004fff01d0d | 85 | else if(column2 == 1){ |
hlekkala | 1:e004fff01d0d | 86 | speaker1.Output(1336, 0.5); |
hlekkala | 1:e004fff01d0d | 87 | buzzer = 0; |
hlekkala | 1:e004fff01d0d | 88 | tick.attach(&outFreq, 0.5/697.0); |
hlekkala | 1:e004fff01d0d | 89 | wait_ms(16); |
hlekkala | 1:e004fff01d0d | 90 | speaker1.Output(0, 0.0); |
hlekkala | 1:e004fff01d0d | 91 | tick.detach(); |
hlekkala | 1:e004fff01d0d | 92 | pc.printf("2"); |
hlekkala | 1:e004fff01d0d | 93 | } |
hlekkala | 1:e004fff01d0d | 94 | else if(column3 == 1){ |
hlekkala | 1:e004fff01d0d | 95 | speaker1.Output(1477, 0.5); |
hlekkala | 1:e004fff01d0d | 96 | buzzer = 0; |
hlekkala | 1:e004fff01d0d | 97 | tick.attach(&outFreq, 0.5/697.0); |
hlekkala | 1:e004fff01d0d | 98 | wait_ms(16); |
hlekkala | 1:e004fff01d0d | 99 | speaker1.Output(0, 0.0); |
hlekkala | 1:e004fff01d0d | 100 | tick.detach(); |
hlekkala | 1:e004fff01d0d | 101 | pc.printf("3"); |
hlekkala | 1:e004fff01d0d | 102 | } |
hlekkala | 1:e004fff01d0d | 103 | else if(column4 == 1){ |
hlekkala | 1:e004fff01d0d | 104 | pc.printf("A"); |
hlekkala | 1:e004fff01d0d | 105 | speaker1.Output(1633, 0.5); |
hlekkala | 1:e004fff01d0d | 106 | buzzer = 0; |
hlekkala | 1:e004fff01d0d | 107 | tick.attach(&outFreq, 0.5/697.0); |
hlekkala | 1:e004fff01d0d | 108 | wait_ms(16); |
hlekkala | 1:e004fff01d0d | 109 | speaker1.Output(0, 0.0); |
hlekkala | 1:e004fff01d0d | 110 | tick.detach(); |
hlekkala | 1:e004fff01d0d | 111 | } |
hlekkala | 1:e004fff01d0d | 112 | row1 = 0; |
hlekkala | 1:e004fff01d0d | 113 | } |
hlekkala | 1:e004fff01d0d | 114 | // Check for Row 2 |
hlekkala | 1:e004fff01d0d | 115 | if(i == 2){ |
hlekkala | 1:e004fff01d0d | 116 | |
hlekkala | 1:e004fff01d0d | 117 | row2 = 1; |
hlekkala | 1:e004fff01d0d | 118 | |
hlekkala | 1:e004fff01d0d | 119 | if(column1 == 1){ |
hlekkala | 1:e004fff01d0d | 120 | pc.printf("4"); |
hlekkala | 1:e004fff01d0d | 121 | speaker1.Output(1209, 0.5); |
hlekkala | 1:e004fff01d0d | 122 | buzzer = 0; |
hlekkala | 1:e004fff01d0d | 123 | tick.attach(&outFreq, 0.5/770.0); |
hlekkala | 1:e004fff01d0d | 124 | wait_ms(16); |
hlekkala | 1:e004fff01d0d | 125 | speaker1.Output(0, 0.0); |
hlekkala | 1:e004fff01d0d | 126 | tick.detach(); |
hlekkala | 1:e004fff01d0d | 127 | } |
hlekkala | 1:e004fff01d0d | 128 | else if(column2 == 1){ |
hlekkala | 1:e004fff01d0d | 129 | pc.printf("5"); |
hlekkala | 1:e004fff01d0d | 130 | speaker1.Output(1336, 0.5); |
hlekkala | 1:e004fff01d0d | 131 | buzzer = 0; |
hlekkala | 1:e004fff01d0d | 132 | tick.attach(&outFreq, 0.5/770.0); |
hlekkala | 1:e004fff01d0d | 133 | wait_ms(16); |
hlekkala | 1:e004fff01d0d | 134 | speaker1.Output(0, 0.0); |
hlekkala | 1:e004fff01d0d | 135 | tick.detach(); |
hlekkala | 1:e004fff01d0d | 136 | } |
hlekkala | 1:e004fff01d0d | 137 | else if(column3 == 1){ |
hlekkala | 1:e004fff01d0d | 138 | pc.printf("6"); |
hlekkala | 1:e004fff01d0d | 139 | speaker1.Output(1477, 0.5); |
hlekkala | 1:e004fff01d0d | 140 | buzzer = 0; |
hlekkala | 1:e004fff01d0d | 141 | tick.attach(&outFreq, 0.5/770.0); |
hlekkala | 1:e004fff01d0d | 142 | wait_ms(16); |
hlekkala | 1:e004fff01d0d | 143 | speaker1.Output(0, 0.0); |
hlekkala | 1:e004fff01d0d | 144 | tick.detach(); |
hlekkala | 1:e004fff01d0d | 145 | } |
hlekkala | 1:e004fff01d0d | 146 | else if(column4 == 1){ |
hlekkala | 1:e004fff01d0d | 147 | pc.printf("B"); |
hlekkala | 1:e004fff01d0d | 148 | speaker1.Output(1633, 0.5); |
hlekkala | 1:e004fff01d0d | 149 | buzzer = 0; |
hlekkala | 1:e004fff01d0d | 150 | tick.attach(&outFreq, 0.5/770.0); |
hlekkala | 1:e004fff01d0d | 151 | wait_ms(16); |
hlekkala | 1:e004fff01d0d | 152 | speaker1.Output(0, 0.0); |
hlekkala | 1:e004fff01d0d | 153 | tick.detach(); |
hlekkala | 1:e004fff01d0d | 154 | } |
hlekkala | 1:e004fff01d0d | 155 | row2 = 0; |
hlekkala | 1:e004fff01d0d | 156 | } |
hlekkala | 1:e004fff01d0d | 157 | // Check for Row 3 |
hlekkala | 1:e004fff01d0d | 158 | if(i == 3){ |
hlekkala | 1:e004fff01d0d | 159 | |
hlekkala | 1:e004fff01d0d | 160 | row3 = 1; |
hlekkala | 1:e004fff01d0d | 161 | |
hlekkala | 1:e004fff01d0d | 162 | if(column1 == 1){ |
hlekkala | 1:e004fff01d0d | 163 | pc.printf("7"); |
hlekkala | 1:e004fff01d0d | 164 | speaker1.Output(1209, 0.5); |
hlekkala | 1:e004fff01d0d | 165 | buzzer = 0; |
hlekkala | 1:e004fff01d0d | 166 | tick.attach(&outFreq, 0.5/852.0); |
hlekkala | 1:e004fff01d0d | 167 | wait_ms(16); |
hlekkala | 1:e004fff01d0d | 168 | speaker1.Output(0, 0.0); |
hlekkala | 1:e004fff01d0d | 169 | tick.detach(); |
hlekkala | 1:e004fff01d0d | 170 | } |
hlekkala | 1:e004fff01d0d | 171 | else if(column2 == 1){ |
hlekkala | 1:e004fff01d0d | 172 | pc.printf("8"); |
hlekkala | 1:e004fff01d0d | 173 | speaker1.Output(1336, 0.5); |
hlekkala | 1:e004fff01d0d | 174 | buzzer = 0; |
hlekkala | 1:e004fff01d0d | 175 | tick.attach(&outFreq, 0.5/852.0); |
hlekkala | 1:e004fff01d0d | 176 | wait_ms(16); |
hlekkala | 1:e004fff01d0d | 177 | speaker1.Output(0, 0.0); |
hlekkala | 1:e004fff01d0d | 178 | tick.detach(); |
hlekkala | 1:e004fff01d0d | 179 | } |
hlekkala | 1:e004fff01d0d | 180 | else if(column3 == 1){ |
hlekkala | 1:e004fff01d0d | 181 | pc.printf("9"); |
hlekkala | 1:e004fff01d0d | 182 | speaker1.Output(1477, 0.5); |
hlekkala | 1:e004fff01d0d | 183 | buzzer = 0; |
hlekkala | 1:e004fff01d0d | 184 | tick.attach(&outFreq, 0.5/852.0); |
hlekkala | 1:e004fff01d0d | 185 | wait_ms(16); |
hlekkala | 1:e004fff01d0d | 186 | speaker1.Output(0, 0.0); |
hlekkala | 1:e004fff01d0d | 187 | tick.detach(); |
hlekkala | 1:e004fff01d0d | 188 | } |
hlekkala | 1:e004fff01d0d | 189 | else if(column4 == 1){ |
hlekkala | 1:e004fff01d0d | 190 | pc.printf("C"); |
hlekkala | 1:e004fff01d0d | 191 | speaker1.Output(1633, 0.5); |
hlekkala | 1:e004fff01d0d | 192 | buzzer = 0; |
hlekkala | 1:e004fff01d0d | 193 | tick.attach(&outFreq, 0.5/852.0); |
hlekkala | 1:e004fff01d0d | 194 | wait_ms(16); |
hlekkala | 1:e004fff01d0d | 195 | speaker1.Output(0, 0.0); |
hlekkala | 1:e004fff01d0d | 196 | tick.detach(); |
hlekkala | 1:e004fff01d0d | 197 | } |
hlekkala | 1:e004fff01d0d | 198 | row3 = 0; |
hlekkala | 1:e004fff01d0d | 199 | } |
hlekkala | 1:e004fff01d0d | 200 | |
hlekkala | 1:e004fff01d0d | 201 | // Check for Row 4 |
hlekkala | 1:e004fff01d0d | 202 | if(i == 4){ |
hlekkala | 1:e004fff01d0d | 203 | |
hlekkala | 1:e004fff01d0d | 204 | row4 = 1; |
hlekkala | 1:e004fff01d0d | 205 | |
hlekkala | 1:e004fff01d0d | 206 | if(column1 == 1){ |
hlekkala | 1:e004fff01d0d | 207 | pc.printf("*"); |
hlekkala | 1:e004fff01d0d | 208 | speaker1.Output(1209, 0.5); |
hlekkala | 1:e004fff01d0d | 209 | buzzer = 0; |
hlekkala | 1:e004fff01d0d | 210 | tick.attach(&outFreq, 0.5/941.0); |
hlekkala | 1:e004fff01d0d | 211 | wait_ms(16); |
hlekkala | 1:e004fff01d0d | 212 | speaker1.Output(0, 0.0); |
hlekkala | 1:e004fff01d0d | 213 | tick.detach(); |
hlekkala | 1:e004fff01d0d | 214 | } |
hlekkala | 1:e004fff01d0d | 215 | else if(column2 == 1){ |
hlekkala | 1:e004fff01d0d | 216 | pc.printf("0"); |
hlekkala | 1:e004fff01d0d | 217 | speaker1.Output(1336, 0.5); |
hlekkala | 1:e004fff01d0d | 218 | buzzer = 0; |
hlekkala | 1:e004fff01d0d | 219 | tick.attach(&outFreq, 0.5/941.0); |
hlekkala | 1:e004fff01d0d | 220 | wait_ms(16); |
hlekkala | 1:e004fff01d0d | 221 | speaker1.Output(0, 0.0); |
hlekkala | 1:e004fff01d0d | 222 | tick.detach(); |
hlekkala | 1:e004fff01d0d | 223 | } |
hlekkala | 1:e004fff01d0d | 224 | else if(column3 == 1){ |
hlekkala | 1:e004fff01d0d | 225 | pc.printf("#"); |
hlekkala | 1:e004fff01d0d | 226 | speaker1.Output(1477, 0.5); |
hlekkala | 1:e004fff01d0d | 227 | buzzer = 0; |
hlekkala | 1:e004fff01d0d | 228 | tick.attach(&outFreq, 0.5/941.0); |
hlekkala | 1:e004fff01d0d | 229 | wait_ms(16); |
hlekkala | 1:e004fff01d0d | 230 | speaker1.Output(0, 0.0); |
hlekkala | 1:e004fff01d0d | 231 | tick.detach(); |
hlekkala | 1:e004fff01d0d | 232 | } |
hlekkala | 1:e004fff01d0d | 233 | else if(column4 == 1){ |
hlekkala | 1:e004fff01d0d | 234 | pc.printf("D"); |
hlekkala | 1:e004fff01d0d | 235 | speaker1.Output(1633, 0.5); |
hlekkala | 1:e004fff01d0d | 236 | buzzer = 0; |
hlekkala | 1:e004fff01d0d | 237 | tick.attach(&outFreq, 0.5/941.0); |
hlekkala | 1:e004fff01d0d | 238 | wait_ms(16); |
hlekkala | 1:e004fff01d0d | 239 | speaker1.Output(0, 0.0); |
hlekkala | 1:e004fff01d0d | 240 | tick.detach(); |
hlekkala | 1:e004fff01d0d | 241 | } |
hlekkala | 1:e004fff01d0d | 242 | row4 = 0; |
hlekkala | 1:e004fff01d0d | 243 | } |
wenrui | 0:cfa060762ad7 | 244 | } |
hlekkala | 1:e004fff01d0d | 245 | |
hlekkala | 1:e004fff01d0d | 246 | } |