new
Dependencies: mbed
main.cpp@1:461a4f04db9b, 2015-02-19 (annotated)
- Committer:
- jaredwil
- Date:
- Thu Feb 19 18:33:11 2015 +0000
- Revision:
- 1:461a4f04db9b
- Parent:
- 0:89cc1300ce7e
- Child:
- 2:94a34bcf8f09
new;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
jaredwil | 0:89cc1300ce7e | 1 | #include "mbed.h" |
jaredwil | 0:89cc1300ce7e | 2 | #include <map> |
jaredwil | 0:89cc1300ce7e | 3 | #include <string> |
jaredwil | 0:89cc1300ce7e | 4 | |
jaredwil | 0:89cc1300ce7e | 5 | //Initialize serial comms |
jaredwil | 0:89cc1300ce7e | 6 | Serial pc(USBTX, USBRX); |
jaredwil | 0:89cc1300ce7e | 7 | |
jaredwil | 1:461a4f04db9b | 8 | PwmOut PWMh(p22); |
jaredwil | 1:461a4f04db9b | 9 | //because both use same ticker make a new manual pmw |
jaredwil | 1:461a4f04db9b | 10 | DigitalOut PWMl(p21); |
jaredwil | 1:461a4f04db9b | 11 | Ticker PWMcont; |
jaredwil | 1:461a4f04db9b | 12 | //PwmOut PWMl(p21); |
jaredwil | 0:89cc1300ce7e | 13 | |
jaredwil | 1:461a4f04db9b | 14 | DigitalOut row1(p26); |
jaredwil | 1:461a4f04db9b | 15 | DigitalOut row2(p23); |
jaredwil | 1:461a4f04db9b | 16 | DigitalOut row3(p24); |
jaredwil | 1:461a4f04db9b | 17 | DigitalOut row4(p25); |
jaredwil | 0:89cc1300ce7e | 18 | |
jaredwil | 0:89cc1300ce7e | 19 | DigitalIn col1(p20); |
jaredwil | 0:89cc1300ce7e | 20 | DigitalIn col2(p19); |
jaredwil | 0:89cc1300ce7e | 21 | DigitalIn col3(p18); |
jaredwil | 0:89cc1300ce7e | 22 | DigitalIn col4(p17); |
jaredwil | 0:89cc1300ce7e | 23 | Timer t1; |
jaredwil | 0:89cc1300ce7e | 24 | |
jaredwil | 0:89cc1300ce7e | 25 | //map |
jaredwil | 0:89cc1300ce7e | 26 | map<int, char> keypadMap; |
jaredwil | 0:89cc1300ce7e | 27 | map<int, char *> dtmfMap; |
jaredwil | 0:89cc1300ce7e | 28 | |
jaredwil | 1:461a4f04db9b | 29 | //global |
jaredwil | 1:461a4f04db9b | 30 | int on = 0; |
jaredwil | 1:461a4f04db9b | 31 | |
jaredwil | 0:89cc1300ce7e | 32 | //Mores Code -> Character Map |
jaredwil | 0:89cc1300ce7e | 33 | void init(){ |
jaredwil | 0:89cc1300ce7e | 34 | keypadMap[0x11]='1'; |
jaredwil | 0:89cc1300ce7e | 35 | keypadMap[0x12]='2'; |
jaredwil | 0:89cc1300ce7e | 36 | keypadMap[0x14]='3'; |
jaredwil | 0:89cc1300ce7e | 37 | keypadMap[0x18]='A'; |
jaredwil | 0:89cc1300ce7e | 38 | keypadMap[0x21]='4'; |
jaredwil | 0:89cc1300ce7e | 39 | keypadMap[0x22]='5'; |
jaredwil | 0:89cc1300ce7e | 40 | keypadMap[0x24]='6'; |
jaredwil | 0:89cc1300ce7e | 41 | keypadMap[0x28]='B'; |
jaredwil | 0:89cc1300ce7e | 42 | keypadMap[0x41]='7'; |
jaredwil | 0:89cc1300ce7e | 43 | keypadMap[0x42]='8'; |
jaredwil | 0:89cc1300ce7e | 44 | keypadMap[0x44]='9'; |
jaredwil | 0:89cc1300ce7e | 45 | keypadMap[0x48]='C'; |
jaredwil | 0:89cc1300ce7e | 46 | keypadMap[0x81]='*'; |
jaredwil | 0:89cc1300ce7e | 47 | keypadMap[0x82]='0'; |
jaredwil | 0:89cc1300ce7e | 48 | keypadMap[0x84]='#'; |
jaredwil | 0:89cc1300ce7e | 49 | keypadMap[0x88]='D'; |
jaredwil | 0:89cc1300ce7e | 50 | |
jaredwil | 0:89cc1300ce7e | 51 | dtmfMap[0x11]="1029,697"; //1 |
jaredwil | 0:89cc1300ce7e | 52 | dtmfMap[0x12]="1336,697"; //2 |
jaredwil | 0:89cc1300ce7e | 53 | dtmfMap[0x14]="1477,697"; //3 |
jaredwil | 0:89cc1300ce7e | 54 | dtmfMap[0x18]="1633,697"; //A |
jaredwil | 0:89cc1300ce7e | 55 | dtmfMap[0x21]="1209,770"; //4 |
jaredwil | 0:89cc1300ce7e | 56 | dtmfMap[0x22]="1336,770"; //5 |
jaredwil | 0:89cc1300ce7e | 57 | dtmfMap[0x24]="1477,770"; //6 |
jaredwil | 0:89cc1300ce7e | 58 | dtmfMap[0x28]="1633,770"; //B |
jaredwil | 0:89cc1300ce7e | 59 | dtmfMap[0x41]="1209,852"; //7 |
jaredwil | 0:89cc1300ce7e | 60 | dtmfMap[0x42]="1336,852"; //8 |
jaredwil | 0:89cc1300ce7e | 61 | dtmfMap[0x44]="1477,852"; //9 |
jaredwil | 0:89cc1300ce7e | 62 | dtmfMap[0x48]="1633,852"; //C |
jaredwil | 0:89cc1300ce7e | 63 | dtmfMap[0x81]="1209,941"; //* |
jaredwil | 0:89cc1300ce7e | 64 | dtmfMap[0x82]="1336,941"; //0 |
jaredwil | 0:89cc1300ce7e | 65 | dtmfMap[0x84]="1477,941"; //# |
jaredwil | 0:89cc1300ce7e | 66 | dtmfMap[0x88]="1633,941"; //D |
jaredwil | 0:89cc1300ce7e | 67 | } |
jaredwil | 0:89cc1300ce7e | 68 | |
jaredwil | 1:461a4f04db9b | 69 | void flip() { |
jaredwil | 1:461a4f04db9b | 70 | if(on == 1) |
jaredwil | 1:461a4f04db9b | 71 | PWMl = !PWMl; |
jaredwil | 1:461a4f04db9b | 72 | else |
jaredwil | 1:461a4f04db9b | 73 | PWMl = 0; |
jaredwil | 1:461a4f04db9b | 74 | } |
jaredwil | 1:461a4f04db9b | 75 | |
jaredwil | 0:89cc1300ce7e | 76 | int main() { |
jaredwil | 0:89cc1300ce7e | 77 | init(); |
jaredwil | 0:89cc1300ce7e | 78 | //Initialize rows to 0 |
jaredwil | 0:89cc1300ce7e | 79 | row1 = 0; |
jaredwil | 0:89cc1300ce7e | 80 | row2 = 0; |
jaredwil | 0:89cc1300ce7e | 81 | row3 = 0; |
jaredwil | 0:89cc1300ce7e | 82 | row4 = 0; |
jaredwil | 1:461a4f04db9b | 83 | PWMl = 0; |
jaredwil | 0:89cc1300ce7e | 84 | t1.start(); |
jaredwil | 0:89cc1300ce7e | 85 | int cur_input = 0x00; |
jaredwil | 0:89cc1300ce7e | 86 | int prev_input = 0x00; |
jaredwil | 1:461a4f04db9b | 87 | |
jaredwil | 1:461a4f04db9b | 88 | //main loop |
jaredwil | 0:89cc1300ce7e | 89 | while(1) { |
jaredwil | 1:461a4f04db9b | 90 | |
jaredwil | 1:461a4f04db9b | 91 | |
jaredwil | 1:461a4f04db9b | 92 | //If there was no previous input than switch rows |
jaredwil | 1:461a4f04db9b | 93 | if(prev_input == 0){ |
jaredwil | 1:461a4f04db9b | 94 | //make all rows 0 to add stability |
jaredwil | 1:461a4f04db9b | 95 | row1 = 0; |
jaredwil | 1:461a4f04db9b | 96 | row2 = 0; |
jaredwil | 1:461a4f04db9b | 97 | row3 = 0; |
jaredwil | 1:461a4f04db9b | 98 | row4 = 0; |
jaredwil | 1:461a4f04db9b | 99 | //Keep each pin on for 4ms |
jaredwil | 1:461a4f04db9b | 100 | //"turn on" appropriat bit in map when on. |
jaredwil | 1:461a4f04db9b | 101 | switch(t1.read_ms()%4){ |
jaredwil | 1:461a4f04db9b | 102 | case 0: |
jaredwil | 1:461a4f04db9b | 103 | row1 = 1; |
jaredwil | 1:461a4f04db9b | 104 | cur_input |= 0x10; |
jaredwil | 1:461a4f04db9b | 105 | break; |
jaredwil | 1:461a4f04db9b | 106 | case 1: |
jaredwil | 1:461a4f04db9b | 107 | row2 = 1; |
jaredwil | 1:461a4f04db9b | 108 | cur_input |= 0x20; |
jaredwil | 1:461a4f04db9b | 109 | break; |
jaredwil | 1:461a4f04db9b | 110 | case 2: |
jaredwil | 1:461a4f04db9b | 111 | row3 = 1; |
jaredwil | 1:461a4f04db9b | 112 | cur_input |= 0x40; |
jaredwil | 1:461a4f04db9b | 113 | break; |
jaredwil | 1:461a4f04db9b | 114 | case 3: |
jaredwil | 1:461a4f04db9b | 115 | row4 = 1; |
jaredwil | 1:461a4f04db9b | 116 | cur_input |= 0x80; |
jaredwil | 1:461a4f04db9b | 117 | break; |
jaredwil | 1:461a4f04db9b | 118 | } |
jaredwil | 1:461a4f04db9b | 119 | } |
jaredwil | 1:461a4f04db9b | 120 | |
jaredwil | 1:461a4f04db9b | 121 | |
jaredwil | 0:89cc1300ce7e | 122 | |
jaredwil | 1:461a4f04db9b | 123 | |
jaredwil | 1:461a4f04db9b | 124 | //Check each colum to see if it is high |
jaredwil | 1:461a4f04db9b | 125 | //if it is "turn on" that respective bit |
jaredwil | 1:461a4f04db9b | 126 | if(col1 == 1) |
jaredwil | 1:461a4f04db9b | 127 | cur_input |= 0x01; |
jaredwil | 1:461a4f04db9b | 128 | else if(col2 == 1) |
jaredwil | 1:461a4f04db9b | 129 | cur_input |= 0x02; |
jaredwil | 1:461a4f04db9b | 130 | else if(col3 == 1) |
jaredwil | 1:461a4f04db9b | 131 | cur_input |= 0x04; |
jaredwil | 1:461a4f04db9b | 132 | else if(col4 == 1) |
jaredwil | 1:461a4f04db9b | 133 | cur_input |= 0x08; |
jaredwil | 1:461a4f04db9b | 134 | else cur_input = 0; //Detect button release or no input set input to 0 |
jaredwil | 1:461a4f04db9b | 135 | |
jaredwil | 1:461a4f04db9b | 136 | //If curr input is non zero continue |
jaredwil | 1:461a4f04db9b | 137 | if(cur_input) { |
jaredwil | 1:461a4f04db9b | 138 | if(cur_input != prev_input) { |
jaredwil | 1:461a4f04db9b | 139 | char c = keypadMap[cur_input]; //assign charcter based on input |
jaredwil | 1:461a4f04db9b | 140 | if(c != 0){ |
jaredwil | 1:461a4f04db9b | 141 | pc.printf("%c\r",c); //print input and char |
jaredwil | 1:461a4f04db9b | 142 | } |
jaredwil | 1:461a4f04db9b | 143 | float freqh,freql; |
jaredwil | 1:461a4f04db9b | 144 | char *freq = dtmfMap[cur_input]; |
jaredwil | 1:461a4f04db9b | 145 | sscanf(freq,"%f,%f",&freqh,&freql); |
jaredwil | 1:461a4f04db9b | 146 | pc.printf("High:%f, Low:%f\r",freqh,freql); |
jaredwil | 1:461a4f04db9b | 147 | |
jaredwil | 1:461a4f04db9b | 148 | |
jaredwil | 1:461a4f04db9b | 149 | |
jaredwil | 1:461a4f04db9b | 150 | //PWMl.period(1/freql); // set PWM period to user specified for low tone |
jaredwil | 1:461a4f04db9b | 151 | //PWMl=0.5; // set duty cycle to 50% |
jaredwil | 1:461a4f04db9b | 152 | on = 1; |
jaredwil | 1:461a4f04db9b | 153 | PWMcont.attach(&flip, (1/freql)); // the address of the function to be attached (flip) and the interval (1/freql) |
jaredwil | 1:461a4f04db9b | 154 | |
jaredwil | 1:461a4f04db9b | 155 | PWMh.period(1/freqh); // set PWM period to user specified for high tone |
jaredwil | 1:461a4f04db9b | 156 | PWMh=0.5; // set duty cycle to 50% |
jaredwil | 1:461a4f04db9b | 157 | |
jaredwil | 1:461a4f04db9b | 158 | } |
jaredwil | 1:461a4f04db9b | 159 | |
jaredwil | 0:89cc1300ce7e | 160 | } |
jaredwil | 1:461a4f04db9b | 161 | //Turn off pulse width modulation if curr input is 0 |
jaredwil | 1:461a4f04db9b | 162 | else { |
jaredwil | 1:461a4f04db9b | 163 | PWMh = 0.0; |
jaredwil | 1:461a4f04db9b | 164 | //PWMl = 0.0; |
jaredwil | 1:461a4f04db9b | 165 | on = 0; |
jaredwil | 1:461a4f04db9b | 166 | } |
jaredwil | 1:461a4f04db9b | 167 | |
jaredwil | 1:461a4f04db9b | 168 | |
jaredwil | 1:461a4f04db9b | 169 | //Maintain the past input |
jaredwil | 1:461a4f04db9b | 170 | prev_input = cur_input; |
jaredwil | 1:461a4f04db9b | 171 | |
jaredwil | 1:461a4f04db9b | 172 | |
jaredwil | 0:89cc1300ce7e | 173 | } |
jaredwil | 1:461a4f04db9b | 174 | |
jaredwil | 0:89cc1300ce7e | 175 | } |