Keypad Project
Dependencies: FPointer keypad mbed-rtos mbed
main.cpp@1:8b931e33f8e3, 2015-11-18 (annotated)
- Committer:
- bpp5046
- Date:
- Wed Nov 18 14:42:03 2015 +0000
- Revision:
- 1:8b931e33f8e3
- Parent:
- 0:6db970f1f785
- Child:
- 2:ad4d832c4399
hi
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
bpp5046 | 0:6db970f1f785 | 1 | #include "mbed.h" |
bpp5046 | 0:6db970f1f785 | 2 | #include "rtos.h" |
bpp5046 | 0:6db970f1f785 | 3 | |
bpp5046 | 0:6db970f1f785 | 4 | #include "Keypad.h" |
bpp5046 | 0:6db970f1f785 | 5 | |
bpp5046 | 0:6db970f1f785 | 6 | Serial PC(USBTX, USBRX); |
bpp5046 | 0:6db970f1f785 | 7 | RtosTimer *LedTimer; |
bpp5046 | 0:6db970f1f785 | 8 | DigitalOut Led1(LED1); |
bpp5046 | 0:6db970f1f785 | 9 | DigitalOut Led2(LED2); |
bpp5046 | 0:6db970f1f785 | 10 | int input; |
bpp5046 | 0:6db970f1f785 | 11 | int break1; |
bpp5046 | 0:6db970f1f785 | 12 | bool start = false; |
bpp5046 | 0:6db970f1f785 | 13 | int freq = 1; |
bpp5046 | 0:6db970f1f785 | 14 | int wave = 1; |
bpp5046 | 0:6db970f1f785 | 15 | int range = 1; |
bpp5046 | 0:6db970f1f785 | 16 | int group_num = 0; |
bpp5046 | 0:6db970f1f785 | 17 | char id = '0'; |
bpp5046 | 0:6db970f1f785 | 18 | CAN can1(p9,p10); |
bpp5046 | 0:6db970f1f785 | 19 | CANMessage talk; |
bpp5046 | 0:6db970f1f785 | 20 | // Define your own keypad values |
bpp5046 | 0:6db970f1f785 | 21 | char Keytable[] = { |
bpp5046 | 0:6db970f1f785 | 22 | '1', '2', '3', // r0 |
bpp5046 | 0:6db970f1f785 | 23 | '4', '5', '6', // r1 |
bpp5046 | 0:6db970f1f785 | 24 | '7', '8', '9', // r2 |
bpp5046 | 0:6db970f1f785 | 25 | '*', '0', '#' // r3 |
bpp5046 | 0:6db970f1f785 | 26 | // c0 c1 c2 |
bpp5046 | 0:6db970f1f785 | 27 | }; |
bpp5046 | 0:6db970f1f785 | 28 | |
bpp5046 | 0:6db970f1f785 | 29 | int32_t Index = -1; |
bpp5046 | 0:6db970f1f785 | 30 | char state='0'; |
bpp5046 | 0:6db970f1f785 | 31 | |
bpp5046 | 0:6db970f1f785 | 32 | uint32_t cbAfterInput(uint32_t _index) |
bpp5046 | 0:6db970f1f785 | 33 | { |
bpp5046 | 0:6db970f1f785 | 34 | Index = _index; |
bpp5046 | 0:6db970f1f785 | 35 | return 0; |
bpp5046 | 0:6db970f1f785 | 36 | } |
bpp5046 | 0:6db970f1f785 | 37 | |
bpp5046 | 0:6db970f1f785 | 38 | void Send_Message() { |
bpp5046 | 0:6db970f1f785 | 39 | |
bpp5046 | 0:6db970f1f785 | 40 | PC.printf("\n Message Sending...\n"); |
bpp5046 | 0:6db970f1f785 | 41 | talk.data[0] = id; |
bpp5046 | 0:6db970f1f785 | 42 | talk.data[1] = state; |
bpp5046 | 0:6db970f1f785 | 43 | can1.write(talk); |
bpp5046 | 0:6db970f1f785 | 44 | |
bpp5046 | 0:6db970f1f785 | 45 | if(can1.write(talk)) |
bpp5046 | 0:6db970f1f785 | 46 | { |
bpp5046 | 0:6db970f1f785 | 47 | PC.printf("Sent to id = %c with state = %c\n", talk.data[0], talk.data[1]); |
bpp5046 | 0:6db970f1f785 | 48 | } |
bpp5046 | 1:8b931e33f8e3 | 49 | |
bpp5046 | 1:8b931e33f8e3 | 50 | PC.printf("Press # to go to main menu\n"); |
bpp5046 | 0:6db970f1f785 | 51 | } |
bpp5046 | 0:6db970f1f785 | 52 | |
bpp5046 | 0:6db970f1f785 | 53 | void menu(){ |
bpp5046 | 0:6db970f1f785 | 54 | PC.printf("1. DAC Input\n"); |
bpp5046 | 0:6db970f1f785 | 55 | PC.printf("2. DigiPot Input\n"); |
bpp5046 | 0:6db970f1f785 | 56 | PC.printf("3. ADC Input\n"); |
bpp5046 | 0:6db970f1f785 | 57 | } |
bpp5046 | 0:6db970f1f785 | 58 | |
bpp5046 | 0:6db970f1f785 | 59 | int ADC() |
bpp5046 | 0:6db970f1f785 | 60 | { |
bpp5046 | 0:6db970f1f785 | 61 | break1 = 0; |
bpp5046 | 0:6db970f1f785 | 62 | Index = -1; |
bpp5046 | 0:6db970f1f785 | 63 | PC.printf("Select Start/Stop\n"); |
bpp5046 | 0:6db970f1f785 | 64 | PC.printf("1. Start\n"); |
bpp5046 | 0:6db970f1f785 | 65 | PC.printf("2. Stop\n"); |
bpp5046 | 0:6db970f1f785 | 66 | PC.printf("Press # to go to main menu\n"); |
bpp5046 | 0:6db970f1f785 | 67 | while (break1 != 1) { |
bpp5046 | 0:6db970f1f785 | 68 | __wfi(); |
bpp5046 | 0:6db970f1f785 | 69 | if (Index > -1) { |
bpp5046 | 0:6db970f1f785 | 70 | PC.printf("Index:%d => Key:%c\r\n", Index, Keytable[Index]); |
bpp5046 | 0:6db970f1f785 | 71 | input = Index + 1; |
bpp5046 | 0:6db970f1f785 | 72 | switch (input){ |
bpp5046 | 0:6db970f1f785 | 73 | case 1: |
bpp5046 | 0:6db970f1f785 | 74 | // start |
bpp5046 | 0:6db970f1f785 | 75 | state = '0'; |
bpp5046 | 0:6db970f1f785 | 76 | PC.printf("Start case 1\n"); |
bpp5046 | 0:6db970f1f785 | 77 | Send_Message(); |
bpp5046 | 0:6db970f1f785 | 78 | break; |
bpp5046 | 0:6db970f1f785 | 79 | case 2: |
bpp5046 | 0:6db970f1f785 | 80 | // stop |
bpp5046 | 0:6db970f1f785 | 81 | state = '1'; |
bpp5046 | 0:6db970f1f785 | 82 | PC.printf("Stop case 2\n"); |
bpp5046 | 0:6db970f1f785 | 83 | Send_Message(); |
bpp5046 | 0:6db970f1f785 | 84 | break; |
bpp5046 | 0:6db970f1f785 | 85 | case 12: |
bpp5046 | 0:6db970f1f785 | 86 | break1 =1; |
bpp5046 | 0:6db970f1f785 | 87 | menu(); |
bpp5046 | 0:6db970f1f785 | 88 | break; |
bpp5046 | 0:6db970f1f785 | 89 | default: |
bpp5046 | 0:6db970f1f785 | 90 | PC.printf("Please Select a Valid Input\n"); |
bpp5046 | 0:6db970f1f785 | 91 | PC.printf("Select Start/Stop\n"); |
bpp5046 | 0:6db970f1f785 | 92 | PC.printf("1. Start\n"); |
bpp5046 | 0:6db970f1f785 | 93 | PC.printf("2. Stop\n"); |
bpp5046 | 0:6db970f1f785 | 94 | PC.printf("Press # to go to main menu\n"); |
bpp5046 | 0:6db970f1f785 | 95 | break; |
bpp5046 | 0:6db970f1f785 | 96 | } |
bpp5046 | 0:6db970f1f785 | 97 | |
bpp5046 | 0:6db970f1f785 | 98 | |
bpp5046 | 0:6db970f1f785 | 99 | Index = -1; |
bpp5046 | 0:6db970f1f785 | 100 | |
bpp5046 | 0:6db970f1f785 | 101 | } |
bpp5046 | 0:6db970f1f785 | 102 | |
bpp5046 | 0:6db970f1f785 | 103 | } |
bpp5046 | 0:6db970f1f785 | 104 | return 0; |
bpp5046 | 0:6db970f1f785 | 105 | } |
bpp5046 | 0:6db970f1f785 | 106 | |
bpp5046 | 0:6db970f1f785 | 107 | |
bpp5046 | 0:6db970f1f785 | 108 | |
bpp5046 | 0:6db970f1f785 | 109 | int DAC() |
bpp5046 | 0:6db970f1f785 | 110 | { |
bpp5046 | 0:6db970f1f785 | 111 | break1 = 0; |
bpp5046 | 0:6db970f1f785 | 112 | Index = -1; |
bpp5046 | 0:6db970f1f785 | 113 | PC.printf("Select Waveform Type\n"); |
bpp5046 | 0:6db970f1f785 | 114 | PC.printf("1. Turn Off\n"); |
bpp5046 | 0:6db970f1f785 | 115 | PC.printf("2. 1 Hz Square Wave\n"); |
bpp5046 | 0:6db970f1f785 | 116 | PC.printf("3. 10 Hz Square Wave\n"); |
bpp5046 | 0:6db970f1f785 | 117 | PC.printf("4. 1 Hz Sinusoidal Wave\n"); |
bpp5046 | 0:6db970f1f785 | 118 | PC.printf("5. 10 Hz Sinusoidal Wave\n"); |
bpp5046 | 0:6db970f1f785 | 119 | PC.printf("Press # to go to main menu\n"); |
bpp5046 | 0:6db970f1f785 | 120 | |
bpp5046 | 0:6db970f1f785 | 121 | while (break1 != 1) { |
bpp5046 | 0:6db970f1f785 | 122 | __wfi(); |
bpp5046 | 0:6db970f1f785 | 123 | if (Index > -1) { |
bpp5046 | 0:6db970f1f785 | 124 | PC.printf("Index:%d => Key:%c\r\n", Index, Keytable[Index]); |
bpp5046 | 0:6db970f1f785 | 125 | input = Index + 1; |
bpp5046 | 0:6db970f1f785 | 126 | switch (input){ |
bpp5046 | 0:6db970f1f785 | 127 | case 1: |
bpp5046 | 0:6db970f1f785 | 128 | state = '1'; |
bpp5046 | 0:6db970f1f785 | 129 | PC.printf("DAC = OFF\n"); |
bpp5046 | 0:6db970f1f785 | 130 | Send_Message(); |
bpp5046 | 0:6db970f1f785 | 131 | break; |
bpp5046 | 0:6db970f1f785 | 132 | case 2: |
bpp5046 | 0:6db970f1f785 | 133 | state = '2'; |
bpp5046 | 0:6db970f1f785 | 134 | PC.printf("1 Hz Square Wave\n"); |
bpp5046 | 0:6db970f1f785 | 135 | Send_Message(); |
bpp5046 | 0:6db970f1f785 | 136 | break; |
bpp5046 | 0:6db970f1f785 | 137 | case 3: |
bpp5046 | 0:6db970f1f785 | 138 | state = '3'; |
bpp5046 | 0:6db970f1f785 | 139 | PC.printf("10 Hz Square Wave\n"); |
bpp5046 | 0:6db970f1f785 | 140 | Send_Message(); |
bpp5046 | 0:6db970f1f785 | 141 | break; |
bpp5046 | 0:6db970f1f785 | 142 | case 4: |
bpp5046 | 0:6db970f1f785 | 143 | state = '4'; |
bpp5046 | 0:6db970f1f785 | 144 | PC.printf("1 Hz Sinusoidal Wave\n"); |
bpp5046 | 0:6db970f1f785 | 145 | Send_Message(); |
bpp5046 | 0:6db970f1f785 | 146 | break; |
bpp5046 | 0:6db970f1f785 | 147 | case 5: |
bpp5046 | 0:6db970f1f785 | 148 | state = '5'; |
bpp5046 | 0:6db970f1f785 | 149 | PC.printf("10 Hz Sinusoidal Wave\n"); |
bpp5046 | 0:6db970f1f785 | 150 | Send_Message(); |
bpp5046 | 0:6db970f1f785 | 151 | break; |
bpp5046 | 0:6db970f1f785 | 152 | case 12: |
bpp5046 | 0:6db970f1f785 | 153 | break1 =1; |
bpp5046 | 0:6db970f1f785 | 154 | menu(); |
bpp5046 | 0:6db970f1f785 | 155 | break; |
bpp5046 | 0:6db970f1f785 | 156 | default: |
bpp5046 | 0:6db970f1f785 | 157 | PC.printf("Please Input a Valid Input\n"); |
bpp5046 | 0:6db970f1f785 | 158 | PC.printf("Select Waveform Type\n"); |
bpp5046 | 0:6db970f1f785 | 159 | PC.printf("1. Turn Off\n"); |
bpp5046 | 0:6db970f1f785 | 160 | PC.printf("2. 1 Hz Square Wave\n"); |
bpp5046 | 0:6db970f1f785 | 161 | PC.printf("3. 10 Hz Square Wave\n"); |
bpp5046 | 0:6db970f1f785 | 162 | PC.printf("4. 1 Hz Sinusoidal Wave\n"); |
bpp5046 | 0:6db970f1f785 | 163 | PC.printf("5. 10 Hz Sinusoidal Wave\n"); |
bpp5046 | 0:6db970f1f785 | 164 | PC.printf("Press # to go to main menu\n"); |
bpp5046 | 0:6db970f1f785 | 165 | break; |
bpp5046 | 0:6db970f1f785 | 166 | } |
bpp5046 | 0:6db970f1f785 | 167 | |
bpp5046 | 0:6db970f1f785 | 168 | |
bpp5046 | 0:6db970f1f785 | 169 | Index = -1; |
bpp5046 | 0:6db970f1f785 | 170 | |
bpp5046 | 0:6db970f1f785 | 171 | } |
bpp5046 | 0:6db970f1f785 | 172 | |
bpp5046 | 0:6db970f1f785 | 173 | } |
bpp5046 | 0:6db970f1f785 | 174 | return 0; |
bpp5046 | 0:6db970f1f785 | 175 | } |
bpp5046 | 0:6db970f1f785 | 176 | |
bpp5046 | 0:6db970f1f785 | 177 | int DigiPot(){ |
bpp5046 | 0:6db970f1f785 | 178 | break1 = 0; |
bpp5046 | 0:6db970f1f785 | 179 | Index = -1; |
bpp5046 | 0:6db970f1f785 | 180 | PC.printf("Select Range\n"); |
bpp5046 | 0:6db970f1f785 | 181 | PC.printf("1. Between 0 to 1\n"); |
bpp5046 | 0:6db970f1f785 | 182 | PC.printf("2. Between 0 to 2\n"); |
bpp5046 | 0:6db970f1f785 | 183 | PC.printf("3. Between 0 to 3\n"); |
bpp5046 | 0:6db970f1f785 | 184 | PC.printf("4. Between 0 to 4\n"); |
bpp5046 | 0:6db970f1f785 | 185 | PC.printf("5. Between 0 to 5\n"); |
bpp5046 | 0:6db970f1f785 | 186 | PC.printf("Press # to go to main menu\n"); |
bpp5046 | 0:6db970f1f785 | 187 | while (break1 != 1) { |
bpp5046 | 0:6db970f1f785 | 188 | __wfi(); |
bpp5046 | 0:6db970f1f785 | 189 | if (Index > -1) { |
bpp5046 | 0:6db970f1f785 | 190 | PC.printf("Index:%d => Key:%c\r\n", Index, Keytable[Index]); |
bpp5046 | 0:6db970f1f785 | 191 | input = Index + 1; |
bpp5046 | 0:6db970f1f785 | 192 | switch (input){ |
bpp5046 | 0:6db970f1f785 | 193 | case 1: |
bpp5046 | 0:6db970f1f785 | 194 | state = '1'; |
bpp5046 | 0:6db970f1f785 | 195 | PC.printf("0V to 1V\n"); |
bpp5046 | 0:6db970f1f785 | 196 | Send_Message(); |
bpp5046 | 0:6db970f1f785 | 197 | break; |
bpp5046 | 0:6db970f1f785 | 198 | case 2: |
bpp5046 | 0:6db970f1f785 | 199 | state = '2'; |
bpp5046 | 0:6db970f1f785 | 200 | PC.printf("0V to 2V\n"); |
bpp5046 | 0:6db970f1f785 | 201 | Send_Message(); |
bpp5046 | 0:6db970f1f785 | 202 | break; |
bpp5046 | 0:6db970f1f785 | 203 | case 3: |
bpp5046 | 0:6db970f1f785 | 204 | state = '3'; |
bpp5046 | 0:6db970f1f785 | 205 | PC.printf("0V to 3V\n"); |
bpp5046 | 0:6db970f1f785 | 206 | Send_Message(); |
bpp5046 | 0:6db970f1f785 | 207 | break; |
bpp5046 | 0:6db970f1f785 | 208 | case 4: |
bpp5046 | 0:6db970f1f785 | 209 | state = '4'; |
bpp5046 | 0:6db970f1f785 | 210 | PC.printf("0V to 4V\n"); |
bpp5046 | 0:6db970f1f785 | 211 | Send_Message(); |
bpp5046 | 0:6db970f1f785 | 212 | break; |
bpp5046 | 0:6db970f1f785 | 213 | case 5: |
bpp5046 | 0:6db970f1f785 | 214 | state = '5'; |
bpp5046 | 0:6db970f1f785 | 215 | PC.printf("0V to 5V\n"); |
bpp5046 | 0:6db970f1f785 | 216 | Send_Message(); |
bpp5046 | 0:6db970f1f785 | 217 | break; |
bpp5046 | 0:6db970f1f785 | 218 | case 12: |
bpp5046 | 0:6db970f1f785 | 219 | break1 =1; |
bpp5046 | 0:6db970f1f785 | 220 | menu(); |
bpp5046 | 0:6db970f1f785 | 221 | break; |
bpp5046 | 0:6db970f1f785 | 222 | default: |
bpp5046 | 0:6db970f1f785 | 223 | PC.printf("Please Input a Valid Input\n"); |
bpp5046 | 0:6db970f1f785 | 224 | PC.printf("Select Range\n"); |
bpp5046 | 0:6db970f1f785 | 225 | PC.printf("1. Between 0 to 1\n"); |
bpp5046 | 0:6db970f1f785 | 226 | PC.printf("2. Between 0 to 2\n"); |
bpp5046 | 0:6db970f1f785 | 227 | PC.printf("3. Between 0 to 3\n"); |
bpp5046 | 0:6db970f1f785 | 228 | PC.printf("4. Between 0 to 4\n"); |
bpp5046 | 0:6db970f1f785 | 229 | PC.printf("5. Between 0 to 5\n"); |
bpp5046 | 0:6db970f1f785 | 230 | PC.printf("Press # to go to main menu\n"); |
bpp5046 | 0:6db970f1f785 | 231 | break; |
bpp5046 | 0:6db970f1f785 | 232 | } |
bpp5046 | 0:6db970f1f785 | 233 | |
bpp5046 | 0:6db970f1f785 | 234 | |
bpp5046 | 0:6db970f1f785 | 235 | Index = -1; |
bpp5046 | 0:6db970f1f785 | 236 | |
bpp5046 | 0:6db970f1f785 | 237 | } |
bpp5046 | 0:6db970f1f785 | 238 | |
bpp5046 | 0:6db970f1f785 | 239 | } |
bpp5046 | 0:6db970f1f785 | 240 | return 0; |
bpp5046 | 0:6db970f1f785 | 241 | } |
bpp5046 | 0:6db970f1f785 | 242 | |
bpp5046 | 0:6db970f1f785 | 243 | |
bpp5046 | 0:6db970f1f785 | 244 | int main() |
bpp5046 | 0:6db970f1f785 | 245 | { |
bpp5046 | 0:6db970f1f785 | 246 | PC.printf("Control Menu\r\n"); |
bpp5046 | 0:6db970f1f785 | 247 | menu(); |
bpp5046 | 0:6db970f1f785 | 248 | |
bpp5046 | 0:6db970f1f785 | 249 | // r0 r1 r2 r3 c0 c1 c2 c3 |
bpp5046 | 0:6db970f1f785 | 250 | Keypad keypad(p21, p22, p23, p24, p25, p26, p27, NC); |
bpp5046 | 0:6db970f1f785 | 251 | keypad.attach(&cbAfterInput); |
bpp5046 | 0:6db970f1f785 | 252 | keypad.start(); // energize the columns c0-c3 of the keypad |
bpp5046 | 0:6db970f1f785 | 253 | |
bpp5046 | 0:6db970f1f785 | 254 | while (1) { |
bpp5046 | 0:6db970f1f785 | 255 | __wfi(); |
bpp5046 | 0:6db970f1f785 | 256 | |
bpp5046 | 0:6db970f1f785 | 257 | |
bpp5046 | 0:6db970f1f785 | 258 | if (Index > -1) { |
bpp5046 | 0:6db970f1f785 | 259 | PC.printf("Index:%d => Key:%c\r\n", Index, Keytable[Index]); |
bpp5046 | 0:6db970f1f785 | 260 | group_num = Index + 1; |
bpp5046 | 0:6db970f1f785 | 261 | switch (group_num){ |
bpp5046 | 0:6db970f1f785 | 262 | case 1: |
bpp5046 | 0:6db970f1f785 | 263 | PC.printf("Calling DAC()... \n"); |
bpp5046 | 0:6db970f1f785 | 264 | id = '1'; |
bpp5046 | 0:6db970f1f785 | 265 | DAC(); |
bpp5046 | 0:6db970f1f785 | 266 | |
bpp5046 | 0:6db970f1f785 | 267 | break; |
bpp5046 | 0:6db970f1f785 | 268 | case 2: |
bpp5046 | 0:6db970f1f785 | 269 | PC.printf("Calling DigiPot()... \n"); |
bpp5046 | 0:6db970f1f785 | 270 | id = '2'; |
bpp5046 | 0:6db970f1f785 | 271 | DigiPot(); |
bpp5046 | 0:6db970f1f785 | 272 | break; |
bpp5046 | 0:6db970f1f785 | 273 | |
bpp5046 | 0:6db970f1f785 | 274 | case 3: |
bpp5046 | 0:6db970f1f785 | 275 | PC.printf("Calling ADC()... \n"); |
bpp5046 | 0:6db970f1f785 | 276 | id = '3'; |
bpp5046 | 0:6db970f1f785 | 277 | ADC(); |
bpp5046 | 0:6db970f1f785 | 278 | break; |
bpp5046 | 0:6db970f1f785 | 279 | default: |
bpp5046 | 0:6db970f1f785 | 280 | PC.printf("Please Enter a Valid Input between 1 - 3\n"); |
bpp5046 | 0:6db970f1f785 | 281 | menu(); |
bpp5046 | 0:6db970f1f785 | 282 | break; |
bpp5046 | 0:6db970f1f785 | 283 | } |
bpp5046 | 0:6db970f1f785 | 284 | |
bpp5046 | 0:6db970f1f785 | 285 | Index = -1; |
bpp5046 | 0:6db970f1f785 | 286 | |
bpp5046 | 0:6db970f1f785 | 287 | } |
bpp5046 | 0:6db970f1f785 | 288 | } |
bpp5046 | 0:6db970f1f785 | 289 | } |