Keypad Project

Dependencies:   FPointer keypad mbed-rtos mbed

Committer:
bpp5046
Date:
Wed Nov 18 14:35:29 2015 +0000
Revision:
0:6db970f1f785
Child:
1:8b931e33f8e3
1hourlaterKeypadProject;

Who changed what in which revision?

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