Keypad Project
Dependencies: FPointer keypad mbed-rtos mbed
main.cpp@2:ad4d832c4399, 2015-11-18 (annotated)
- Committer:
- bpp5046
- Date:
- Wed Nov 18 15:40:25 2015 +0000
- Revision:
- 2:ad4d832c4399
- Parent:
- 1:8b931e33f8e3
donesky
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 | 2:ad4d832c4399 | 54 | |
bpp5046 | 2:ad4d832c4399 | 55 | |
bpp5046 | 2:ad4d832c4399 | 56 | PC.printf("\n1. DAC Input\n"); |
bpp5046 | 0:6db970f1f785 | 57 | PC.printf("2. DigiPot Input\n"); |
bpp5046 | 0:6db970f1f785 | 58 | PC.printf("3. ADC Input\n"); |
bpp5046 | 0:6db970f1f785 | 59 | } |
bpp5046 | 0:6db970f1f785 | 60 | |
bpp5046 | 0:6db970f1f785 | 61 | int ADC() |
bpp5046 | 0:6db970f1f785 | 62 | { |
bpp5046 | 0:6db970f1f785 | 63 | break1 = 0; |
bpp5046 | 0:6db970f1f785 | 64 | Index = -1; |
bpp5046 | 2:ad4d832c4399 | 65 | PC.printf("\nSelect Start/Stop\n"); |
bpp5046 | 0:6db970f1f785 | 66 | PC.printf("1. Start\n"); |
bpp5046 | 0:6db970f1f785 | 67 | PC.printf("2. Stop\n"); |
bpp5046 | 0:6db970f1f785 | 68 | PC.printf("Press # to go to main menu\n"); |
bpp5046 | 0:6db970f1f785 | 69 | while (break1 != 1) { |
bpp5046 | 0:6db970f1f785 | 70 | __wfi(); |
bpp5046 | 0:6db970f1f785 | 71 | if (Index > -1) { |
bpp5046 | 2:ad4d832c4399 | 72 | |
bpp5046 | 0:6db970f1f785 | 73 | input = Index + 1; |
bpp5046 | 0:6db970f1f785 | 74 | switch (input){ |
bpp5046 | 0:6db970f1f785 | 75 | case 1: |
bpp5046 | 0:6db970f1f785 | 76 | // start |
bpp5046 | 0:6db970f1f785 | 77 | state = '0'; |
bpp5046 | 0:6db970f1f785 | 78 | PC.printf("Start case 1\n"); |
bpp5046 | 0:6db970f1f785 | 79 | Send_Message(); |
bpp5046 | 0:6db970f1f785 | 80 | break; |
bpp5046 | 0:6db970f1f785 | 81 | case 2: |
bpp5046 | 0:6db970f1f785 | 82 | // stop |
bpp5046 | 0:6db970f1f785 | 83 | state = '1'; |
bpp5046 | 0:6db970f1f785 | 84 | PC.printf("Stop case 2\n"); |
bpp5046 | 0:6db970f1f785 | 85 | Send_Message(); |
bpp5046 | 0:6db970f1f785 | 86 | break; |
bpp5046 | 0:6db970f1f785 | 87 | case 12: |
bpp5046 | 0:6db970f1f785 | 88 | break1 =1; |
bpp5046 | 0:6db970f1f785 | 89 | menu(); |
bpp5046 | 0:6db970f1f785 | 90 | break; |
bpp5046 | 0:6db970f1f785 | 91 | default: |
bpp5046 | 0:6db970f1f785 | 92 | PC.printf("Please Select a Valid Input\n"); |
bpp5046 | 0:6db970f1f785 | 93 | PC.printf("Select Start/Stop\n"); |
bpp5046 | 0:6db970f1f785 | 94 | PC.printf("1. Start\n"); |
bpp5046 | 0:6db970f1f785 | 95 | PC.printf("2. Stop\n"); |
bpp5046 | 0:6db970f1f785 | 96 | PC.printf("Press # to go to main menu\n"); |
bpp5046 | 0:6db970f1f785 | 97 | break; |
bpp5046 | 0:6db970f1f785 | 98 | } |
bpp5046 | 0:6db970f1f785 | 99 | |
bpp5046 | 0:6db970f1f785 | 100 | |
bpp5046 | 0:6db970f1f785 | 101 | Index = -1; |
bpp5046 | 0:6db970f1f785 | 102 | |
bpp5046 | 0:6db970f1f785 | 103 | } |
bpp5046 | 0:6db970f1f785 | 104 | |
bpp5046 | 0:6db970f1f785 | 105 | } |
bpp5046 | 0:6db970f1f785 | 106 | return 0; |
bpp5046 | 0:6db970f1f785 | 107 | } |
bpp5046 | 0:6db970f1f785 | 108 | |
bpp5046 | 0:6db970f1f785 | 109 | |
bpp5046 | 0:6db970f1f785 | 110 | |
bpp5046 | 0:6db970f1f785 | 111 | int DAC() |
bpp5046 | 0:6db970f1f785 | 112 | { |
bpp5046 | 0:6db970f1f785 | 113 | break1 = 0; |
bpp5046 | 0:6db970f1f785 | 114 | Index = -1; |
bpp5046 | 2:ad4d832c4399 | 115 | PC.printf("\nSelect Waveform Type\n"); |
bpp5046 | 0:6db970f1f785 | 116 | PC.printf("1. Turn Off\n"); |
bpp5046 | 0:6db970f1f785 | 117 | PC.printf("2. 1 Hz Square Wave\n"); |
bpp5046 | 0:6db970f1f785 | 118 | PC.printf("3. 10 Hz Square Wave\n"); |
bpp5046 | 0:6db970f1f785 | 119 | PC.printf("4. 1 Hz Sinusoidal Wave\n"); |
bpp5046 | 0:6db970f1f785 | 120 | PC.printf("5. 10 Hz Sinusoidal Wave\n"); |
bpp5046 | 0:6db970f1f785 | 121 | PC.printf("Press # to go to main menu\n"); |
bpp5046 | 0:6db970f1f785 | 122 | |
bpp5046 | 0:6db970f1f785 | 123 | while (break1 != 1) { |
bpp5046 | 0:6db970f1f785 | 124 | __wfi(); |
bpp5046 | 0:6db970f1f785 | 125 | if (Index > -1) { |
bpp5046 | 2:ad4d832c4399 | 126 | |
bpp5046 | 0:6db970f1f785 | 127 | input = Index + 1; |
bpp5046 | 0:6db970f1f785 | 128 | switch (input){ |
bpp5046 | 0:6db970f1f785 | 129 | case 1: |
bpp5046 | 0:6db970f1f785 | 130 | state = '1'; |
bpp5046 | 0:6db970f1f785 | 131 | PC.printf("DAC = OFF\n"); |
bpp5046 | 0:6db970f1f785 | 132 | Send_Message(); |
bpp5046 | 0:6db970f1f785 | 133 | break; |
bpp5046 | 0:6db970f1f785 | 134 | case 2: |
bpp5046 | 0:6db970f1f785 | 135 | state = '2'; |
bpp5046 | 0:6db970f1f785 | 136 | PC.printf("1 Hz Square Wave\n"); |
bpp5046 | 0:6db970f1f785 | 137 | Send_Message(); |
bpp5046 | 0:6db970f1f785 | 138 | break; |
bpp5046 | 0:6db970f1f785 | 139 | case 3: |
bpp5046 | 0:6db970f1f785 | 140 | state = '3'; |
bpp5046 | 0:6db970f1f785 | 141 | PC.printf("10 Hz Square Wave\n"); |
bpp5046 | 0:6db970f1f785 | 142 | Send_Message(); |
bpp5046 | 0:6db970f1f785 | 143 | break; |
bpp5046 | 0:6db970f1f785 | 144 | case 4: |
bpp5046 | 0:6db970f1f785 | 145 | state = '4'; |
bpp5046 | 0:6db970f1f785 | 146 | PC.printf("1 Hz Sinusoidal Wave\n"); |
bpp5046 | 0:6db970f1f785 | 147 | Send_Message(); |
bpp5046 | 0:6db970f1f785 | 148 | break; |
bpp5046 | 0:6db970f1f785 | 149 | case 5: |
bpp5046 | 0:6db970f1f785 | 150 | state = '5'; |
bpp5046 | 0:6db970f1f785 | 151 | PC.printf("10 Hz Sinusoidal Wave\n"); |
bpp5046 | 0:6db970f1f785 | 152 | Send_Message(); |
bpp5046 | 0:6db970f1f785 | 153 | break; |
bpp5046 | 0:6db970f1f785 | 154 | case 12: |
bpp5046 | 0:6db970f1f785 | 155 | break1 =1; |
bpp5046 | 0:6db970f1f785 | 156 | menu(); |
bpp5046 | 0:6db970f1f785 | 157 | break; |
bpp5046 | 0:6db970f1f785 | 158 | default: |
bpp5046 | 0:6db970f1f785 | 159 | PC.printf("Please Input a Valid Input\n"); |
bpp5046 | 0:6db970f1f785 | 160 | PC.printf("Select Waveform Type\n"); |
bpp5046 | 0:6db970f1f785 | 161 | PC.printf("1. Turn Off\n"); |
bpp5046 | 0:6db970f1f785 | 162 | PC.printf("2. 1 Hz Square Wave\n"); |
bpp5046 | 0:6db970f1f785 | 163 | PC.printf("3. 10 Hz Square Wave\n"); |
bpp5046 | 0:6db970f1f785 | 164 | PC.printf("4. 1 Hz Sinusoidal Wave\n"); |
bpp5046 | 0:6db970f1f785 | 165 | PC.printf("5. 10 Hz Sinusoidal Wave\n"); |
bpp5046 | 0:6db970f1f785 | 166 | PC.printf("Press # to go to main menu\n"); |
bpp5046 | 0:6db970f1f785 | 167 | break; |
bpp5046 | 0:6db970f1f785 | 168 | } |
bpp5046 | 0:6db970f1f785 | 169 | |
bpp5046 | 0:6db970f1f785 | 170 | |
bpp5046 | 0:6db970f1f785 | 171 | Index = -1; |
bpp5046 | 0:6db970f1f785 | 172 | |
bpp5046 | 0:6db970f1f785 | 173 | } |
bpp5046 | 0:6db970f1f785 | 174 | |
bpp5046 | 0:6db970f1f785 | 175 | } |
bpp5046 | 0:6db970f1f785 | 176 | return 0; |
bpp5046 | 0:6db970f1f785 | 177 | } |
bpp5046 | 0:6db970f1f785 | 178 | |
bpp5046 | 0:6db970f1f785 | 179 | int DigiPot(){ |
bpp5046 | 0:6db970f1f785 | 180 | break1 = 0; |
bpp5046 | 0:6db970f1f785 | 181 | Index = -1; |
bpp5046 | 2:ad4d832c4399 | 182 | PC.printf("\nSelect Range\n"); |
bpp5046 | 0:6db970f1f785 | 183 | PC.printf("1. Between 0 to 1\n"); |
bpp5046 | 0:6db970f1f785 | 184 | PC.printf("2. Between 0 to 2\n"); |
bpp5046 | 0:6db970f1f785 | 185 | PC.printf("3. Between 0 to 3\n"); |
bpp5046 | 0:6db970f1f785 | 186 | PC.printf("4. Between 0 to 4\n"); |
bpp5046 | 0:6db970f1f785 | 187 | PC.printf("5. Between 0 to 5\n"); |
bpp5046 | 0:6db970f1f785 | 188 | PC.printf("Press # to go to main menu\n"); |
bpp5046 | 0:6db970f1f785 | 189 | while (break1 != 1) { |
bpp5046 | 0:6db970f1f785 | 190 | __wfi(); |
bpp5046 | 0:6db970f1f785 | 191 | if (Index > -1) { |
bpp5046 | 2:ad4d832c4399 | 192 | |
bpp5046 | 0:6db970f1f785 | 193 | input = Index + 1; |
bpp5046 | 0:6db970f1f785 | 194 | switch (input){ |
bpp5046 | 0:6db970f1f785 | 195 | case 1: |
bpp5046 | 0:6db970f1f785 | 196 | state = '1'; |
bpp5046 | 0:6db970f1f785 | 197 | PC.printf("0V to 1V\n"); |
bpp5046 | 0:6db970f1f785 | 198 | Send_Message(); |
bpp5046 | 0:6db970f1f785 | 199 | break; |
bpp5046 | 0:6db970f1f785 | 200 | case 2: |
bpp5046 | 0:6db970f1f785 | 201 | state = '2'; |
bpp5046 | 0:6db970f1f785 | 202 | PC.printf("0V to 2V\n"); |
bpp5046 | 0:6db970f1f785 | 203 | Send_Message(); |
bpp5046 | 0:6db970f1f785 | 204 | break; |
bpp5046 | 0:6db970f1f785 | 205 | case 3: |
bpp5046 | 0:6db970f1f785 | 206 | state = '3'; |
bpp5046 | 0:6db970f1f785 | 207 | PC.printf("0V to 3V\n"); |
bpp5046 | 0:6db970f1f785 | 208 | Send_Message(); |
bpp5046 | 0:6db970f1f785 | 209 | break; |
bpp5046 | 0:6db970f1f785 | 210 | case 4: |
bpp5046 | 0:6db970f1f785 | 211 | state = '4'; |
bpp5046 | 0:6db970f1f785 | 212 | PC.printf("0V to 4V\n"); |
bpp5046 | 0:6db970f1f785 | 213 | Send_Message(); |
bpp5046 | 0:6db970f1f785 | 214 | break; |
bpp5046 | 0:6db970f1f785 | 215 | case 5: |
bpp5046 | 0:6db970f1f785 | 216 | state = '5'; |
bpp5046 | 0:6db970f1f785 | 217 | PC.printf("0V to 5V\n"); |
bpp5046 | 0:6db970f1f785 | 218 | Send_Message(); |
bpp5046 | 0:6db970f1f785 | 219 | break; |
bpp5046 | 0:6db970f1f785 | 220 | case 12: |
bpp5046 | 0:6db970f1f785 | 221 | break1 =1; |
bpp5046 | 0:6db970f1f785 | 222 | menu(); |
bpp5046 | 0:6db970f1f785 | 223 | break; |
bpp5046 | 0:6db970f1f785 | 224 | default: |
bpp5046 | 0:6db970f1f785 | 225 | PC.printf("Please Input a Valid Input\n"); |
bpp5046 | 0:6db970f1f785 | 226 | PC.printf("Select Range\n"); |
bpp5046 | 0:6db970f1f785 | 227 | PC.printf("1. Between 0 to 1\n"); |
bpp5046 | 0:6db970f1f785 | 228 | PC.printf("2. Between 0 to 2\n"); |
bpp5046 | 0:6db970f1f785 | 229 | PC.printf("3. Between 0 to 3\n"); |
bpp5046 | 0:6db970f1f785 | 230 | PC.printf("4. Between 0 to 4\n"); |
bpp5046 | 0:6db970f1f785 | 231 | PC.printf("5. Between 0 to 5\n"); |
bpp5046 | 0:6db970f1f785 | 232 | PC.printf("Press # to go to main menu\n"); |
bpp5046 | 0:6db970f1f785 | 233 | break; |
bpp5046 | 0:6db970f1f785 | 234 | } |
bpp5046 | 0:6db970f1f785 | 235 | |
bpp5046 | 0:6db970f1f785 | 236 | |
bpp5046 | 0:6db970f1f785 | 237 | Index = -1; |
bpp5046 | 0:6db970f1f785 | 238 | |
bpp5046 | 0:6db970f1f785 | 239 | } |
bpp5046 | 0:6db970f1f785 | 240 | |
bpp5046 | 0:6db970f1f785 | 241 | } |
bpp5046 | 0:6db970f1f785 | 242 | return 0; |
bpp5046 | 0:6db970f1f785 | 243 | } |
bpp5046 | 0:6db970f1f785 | 244 | |
bpp5046 | 0:6db970f1f785 | 245 | |
bpp5046 | 0:6db970f1f785 | 246 | int main() |
bpp5046 | 0:6db970f1f785 | 247 | { |
bpp5046 | 2:ad4d832c4399 | 248 | |
bpp5046 | 2:ad4d832c4399 | 249 | PC.printf("\nControl Menu\r\n"); |
bpp5046 | 0:6db970f1f785 | 250 | menu(); |
bpp5046 | 0:6db970f1f785 | 251 | |
bpp5046 | 0:6db970f1f785 | 252 | // r0 r1 r2 r3 c0 c1 c2 c3 |
bpp5046 | 0:6db970f1f785 | 253 | Keypad keypad(p21, p22, p23, p24, p25, p26, p27, NC); |
bpp5046 | 0:6db970f1f785 | 254 | keypad.attach(&cbAfterInput); |
bpp5046 | 0:6db970f1f785 | 255 | keypad.start(); // energize the columns c0-c3 of the keypad |
bpp5046 | 0:6db970f1f785 | 256 | |
bpp5046 | 0:6db970f1f785 | 257 | while (1) { |
bpp5046 | 0:6db970f1f785 | 258 | __wfi(); |
bpp5046 | 0:6db970f1f785 | 259 | |
bpp5046 | 0:6db970f1f785 | 260 | |
bpp5046 | 0:6db970f1f785 | 261 | if (Index > -1) { |
bpp5046 | 2:ad4d832c4399 | 262 | |
bpp5046 | 0:6db970f1f785 | 263 | group_num = Index + 1; |
bpp5046 | 0:6db970f1f785 | 264 | switch (group_num){ |
bpp5046 | 0:6db970f1f785 | 265 | case 1: |
bpp5046 | 0:6db970f1f785 | 266 | PC.printf("Calling DAC()... \n"); |
bpp5046 | 0:6db970f1f785 | 267 | id = '1'; |
bpp5046 | 0:6db970f1f785 | 268 | DAC(); |
bpp5046 | 0:6db970f1f785 | 269 | |
bpp5046 | 0:6db970f1f785 | 270 | break; |
bpp5046 | 0:6db970f1f785 | 271 | case 2: |
bpp5046 | 0:6db970f1f785 | 272 | PC.printf("Calling DigiPot()... \n"); |
bpp5046 | 0:6db970f1f785 | 273 | id = '2'; |
bpp5046 | 0:6db970f1f785 | 274 | DigiPot(); |
bpp5046 | 0:6db970f1f785 | 275 | break; |
bpp5046 | 0:6db970f1f785 | 276 | |
bpp5046 | 0:6db970f1f785 | 277 | case 3: |
bpp5046 | 0:6db970f1f785 | 278 | PC.printf("Calling ADC()... \n"); |
bpp5046 | 0:6db970f1f785 | 279 | id = '3'; |
bpp5046 | 0:6db970f1f785 | 280 | ADC(); |
bpp5046 | 0:6db970f1f785 | 281 | break; |
bpp5046 | 0:6db970f1f785 | 282 | default: |
bpp5046 | 0:6db970f1f785 | 283 | PC.printf("Please Enter a Valid Input between 1 - 3\n"); |
bpp5046 | 0:6db970f1f785 | 284 | menu(); |
bpp5046 | 0:6db970f1f785 | 285 | break; |
bpp5046 | 0:6db970f1f785 | 286 | } |
bpp5046 | 0:6db970f1f785 | 287 | |
bpp5046 | 0:6db970f1f785 | 288 | Index = -1; |
bpp5046 | 0:6db970f1f785 | 289 | |
bpp5046 | 0:6db970f1f785 | 290 | } |
bpp5046 | 0:6db970f1f785 | 291 | } |
bpp5046 | 0:6db970f1f785 | 292 | } |