Wenrui Meng
/
ese519-hw2-part4
ese519 hw2 part 4
Revision 1:e004fff01d0d, committed 2015-02-24
- Comitter:
- hlekkala
- Date:
- Tue Feb 24 19:02:54 2015 +0000
- Parent:
- 0:cfa060762ad7
- Commit message:
- final
Changed in this revision
main.cpp | Show annotated file Show diff for this revision Revisions of this file |
diff -r cfa060762ad7 -r e004fff01d0d main.cpp --- a/main.cpp Tue Feb 17 20:35:43 2015 +0000 +++ b/main.cpp Tue Feb 24 19:02:54 2015 +0000 @@ -1,72 +1,246 @@ #include "mbed.h" -PwmOut buzzer(p21); -PwmOut speaker(p26); +//Defining the keypad rows as outputs and columns as inputs +Serial pc(USBTX, USBRX); +DigitalOut row2(p17); +DigitalOut row3(p12); +DigitalOut row4(p15); +DigitalOut row1(p18); + +DigitalIn column1(p16); +DigitalIn column2(p14); +DigitalIn column3(p13); +DigitalIn column4(p12); -Serial pc(USBTX, USBRX); -DigitalOut cols[4]= {p20,p19,p18,p17}; -InterruptIn rows[4]={p25,p22,p23,p24}; +DigitalOut buzzer(p22); + +//defining tickers +Ticker rowsTick; +Ticker tick; + +//function to flip the buzzer value for generating sounds +int i = 0; + +//function to flip the buzzer value +void outFreq(){ + buzzer =! buzzer; + } -double HZc[] = { 1209.0, 1336.0, 1477.0, 1633.0}; // Colum -double HZr[] = {697.0, 770.0, 852.0, 941.0}; //Row -char Keytable[] = { '1', '2', '3', 'A', - '4', '5', '6', 'B', - '7', '8', '9', 'C', - '*', '0', '#', 'D' - }; - -void start(int row){ - double high = 0, low = 0; - if(rows[row].read() == 0) return; - for(int i = 0; i < 4; ++i){ - cols[i].write(0); - if(rows[row].read() == 0){ - high = HZc[i]; - low = HZr[row]; - pc.printf("input row %d col %d %c \n", row, i, Keytable[row * 4 + i]); - break; +//defining class for sound +class Speaker +{ +public: + Speaker(PinName pin) : inpPin(pin) { + } +//function to play the sound for according to the key pressed + void Output(float frequency, float sound) { + inpPin.period(1.0/frequency); + inpPin = sound; + } + //defining the inpPin as PwmOut so that microcontroller recoginses it + PwmOut inpPin; +}; + +//iterating over the rows in a cycle +void row_press() + { + if(i < 4) + { + i++; } - } - buzzer.period(low); - speaker.period(high); - buzzer = 0.5; - speaker = 0.5; -} - -void stop(){ - buzzer.period(0); - speaker.period(0); - buzzer = 0; - speaker = 0; - for(int i = 0; i < 4; ++i) cols[i] = 1; + else + { + i = 1; + } } - +int main(){ -void row0Rise(){ - start(0); -} -void row1Rise(){ - start(1); -} -void row2Rise(){ - start(2); -} -void row3Rise(){ - start(3); -} +//initializing all rows to zero + Speaker speaker1(p21); + row1 = 0; + row2 = 0; + row3 = 0; + row4 = 0; + +//calling a ticker once every 4ms + + rowsTick.attach_us(&row_press, 4000); -void rowFall(){ - stop(); -} - -int main() { - rows[0].rise(&row0Rise); - rows[1].rise(&row1Rise); - rows[2].rise(&row2Rise); - rows[3].rise(&row3Rise); - for(int i = 0; i < 4; ++i) rows[i].fall(&rowFall); - while(1) { - ; + while(1){ + //checking each row and creating the sound accordingly + // Check for Row 1 + if(i == 1){ + row1 = 1; + if(column1 == 1){ + + speaker1.Output(1209, 0.5); + buzzer = 0; + tick.attach(&outFreq, 0.5/697.0); + wait_ms(16); + speaker1.Output(0, 0.0); + pc.printf("1"); + tick.detach(); + } + else if(column2 == 1){ + speaker1.Output(1336, 0.5); + buzzer = 0; + tick.attach(&outFreq, 0.5/697.0); + wait_ms(16); + speaker1.Output(0, 0.0); + tick.detach(); + pc.printf("2"); + } + else if(column3 == 1){ + speaker1.Output(1477, 0.5); + buzzer = 0; + tick.attach(&outFreq, 0.5/697.0); + wait_ms(16); + speaker1.Output(0, 0.0); + tick.detach(); + pc.printf("3"); + } + else if(column4 == 1){ + pc.printf("A"); + speaker1.Output(1633, 0.5); + buzzer = 0; + tick.attach(&outFreq, 0.5/697.0); + wait_ms(16); + speaker1.Output(0, 0.0); + tick.detach(); + } + row1 = 0; + } + // Check for Row 2 + if(i == 2){ + + row2 = 1; + + if(column1 == 1){ + pc.printf("4"); + speaker1.Output(1209, 0.5); + buzzer = 0; + tick.attach(&outFreq, 0.5/770.0); + wait_ms(16); + speaker1.Output(0, 0.0); + tick.detach(); + } + else if(column2 == 1){ + pc.printf("5"); + speaker1.Output(1336, 0.5); + buzzer = 0; + tick.attach(&outFreq, 0.5/770.0); + wait_ms(16); + speaker1.Output(0, 0.0); + tick.detach(); + } + else if(column3 == 1){ + pc.printf("6"); + speaker1.Output(1477, 0.5); + buzzer = 0; + tick.attach(&outFreq, 0.5/770.0); + wait_ms(16); + speaker1.Output(0, 0.0); + tick.detach(); + } + else if(column4 == 1){ + pc.printf("B"); + speaker1.Output(1633, 0.5); + buzzer = 0; + tick.attach(&outFreq, 0.5/770.0); + wait_ms(16); + speaker1.Output(0, 0.0); + tick.detach(); + } + row2 = 0; + } + // Check for Row 3 + if(i == 3){ + + row3 = 1; + + if(column1 == 1){ + pc.printf("7"); + speaker1.Output(1209, 0.5); + buzzer = 0; + tick.attach(&outFreq, 0.5/852.0); + wait_ms(16); + speaker1.Output(0, 0.0); + tick.detach(); + } + else if(column2 == 1){ + pc.printf("8"); + speaker1.Output(1336, 0.5); + buzzer = 0; + tick.attach(&outFreq, 0.5/852.0); + wait_ms(16); + speaker1.Output(0, 0.0); + tick.detach(); + } + else if(column3 == 1){ + pc.printf("9"); + speaker1.Output(1477, 0.5); + buzzer = 0; + tick.attach(&outFreq, 0.5/852.0); + wait_ms(16); + speaker1.Output(0, 0.0); + tick.detach(); + } + else if(column4 == 1){ + pc.printf("C"); + speaker1.Output(1633, 0.5); + buzzer = 0; + tick.attach(&outFreq, 0.5/852.0); + wait_ms(16); + speaker1.Output(0, 0.0); + tick.detach(); + } + row3 = 0; + } + + // Check for Row 4 + if(i == 4){ + + row4 = 1; + + if(column1 == 1){ + pc.printf("*"); + speaker1.Output(1209, 0.5); + buzzer = 0; + tick.attach(&outFreq, 0.5/941.0); + wait_ms(16); + speaker1.Output(0, 0.0); + tick.detach(); + } + else if(column2 == 1){ + pc.printf("0"); + speaker1.Output(1336, 0.5); + buzzer = 0; + tick.attach(&outFreq, 0.5/941.0); + wait_ms(16); + speaker1.Output(0, 0.0); + tick.detach(); + } + else if(column3 == 1){ + pc.printf("#"); + speaker1.Output(1477, 0.5); + buzzer = 0; + tick.attach(&outFreq, 0.5/941.0); + wait_ms(16); + speaker1.Output(0, 0.0); + tick.detach(); + } + else if(column4 == 1){ + pc.printf("D"); + speaker1.Output(1633, 0.5); + buzzer = 0; + tick.attach(&outFreq, 0.5/941.0); + wait_ms(16); + speaker1.Output(0, 0.0); + tick.detach(); + } + row4 = 0; + } } -} + +} \ No newline at end of file