Oscillators
/
Lab3
yup
Fork of Lab3 by
main.cpp@6:7ed6688e3caf, 2016-11-08 (annotated)
- Committer:
- Dorsh
- Date:
- Tue Nov 08 21:12:57 2016 +0000
- Revision:
- 6:7ed6688e3caf
- Parent:
- 5:d27422e874af
- Child:
- 7:31184fd43707
Got rid of the class, used a static integer instead of the object state variable. Works the same, just kinda better this way instead of using a handler
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Dorsh | 4:a10c7172b4f6 | 1 | #include "mbed.h" |
Dorsh | 4:a10c7172b4f6 | 2 | #include "Keypad.h" //Open Source Library for adaFruit KeyPad |
Dorsh | 4:a10c7172b4f6 | 3 | //Modified to work with 4 row 3 col keypad |
Dorsh | 4:a10c7172b4f6 | 4 | #include "CAN.h" //Open Source Library for CAN transmitter |
simon | 0:fb6bbc10ffa0 | 5 | |
simon | 0:fb6bbc10ffa0 | 6 | DigitalOut myled(LED1); |
Dorsh | 2:022b41ff9719 | 7 | CAN can1(p30,p29); |
Dorsh | 2:022b41ff9719 | 8 | |
Dorsh | 2:022b41ff9719 | 9 | |
Dorsh | 4:a10c7172b4f6 | 10 | //Keypad Values |
Dorsh | 4:a10c7172b4f6 | 11 | char Keytable[] = { '1', '2', '3', // r0 |
Dorsh | 4:a10c7172b4f6 | 12 | '4', '5', '6', // r1 |
Dorsh | 4:a10c7172b4f6 | 13 | '7', '8', '9', // r2 |
Dorsh | 4:a10c7172b4f6 | 14 | '*', '0', '#', // r3 |
Dorsh | 4:a10c7172b4f6 | 15 | }; |
Dorsh | 4:a10c7172b4f6 | 16 | // c0 c1 c2 |
Dorsh | 2:022b41ff9719 | 17 | |
Dorsh | 4:a10c7172b4f6 | 18 | //This is what happens when a key is pressed |
Dorsh | 4:a10c7172b4f6 | 19 | //Assigns Index to the value generated by the keypad |
Dorsh | 4:a10c7172b4f6 | 20 | //This lets us use the keypad number |
Dorsh | 4:a10c7172b4f6 | 21 | uint32_t Index; |
Dorsh | 4:a10c7172b4f6 | 22 | uint32_t cbAfterInput(uint32_t index) { |
Dorsh | 4:a10c7172b4f6 | 23 | Index = index; |
Dorsh | 4:a10c7172b4f6 | 24 | return 0; |
Dorsh | 4:a10c7172b4f6 | 25 | } |
Dorsh | 2:022b41ff9719 | 26 | |
Dorsh | 4:a10c7172b4f6 | 27 | void send(char *str) { |
Dorsh | 4:a10c7172b4f6 | 28 | printf("send(%s)\n",str); |
Dorsh | 2:022b41ff9719 | 29 | if(can1.write(CANMessage(1, str, 1))) { |
Dorsh | 2:022b41ff9719 | 30 | printf("wloop()\n"); |
Dorsh | 2:022b41ff9719 | 31 | printf("Message sent: \n"); |
Dorsh | 4:a10c7172b4f6 | 32 | } |
Dorsh | 4:a10c7172b4f6 | 33 | } |
Dorsh | 6:7ed6688e3caf | 34 | |
Dorsh | 6:7ed6688e3caf | 35 | //Returns a specific message to be sent to the CAN Bus |
Dorsh | 6:7ed6688e3caf | 36 | const char *createMessage(char *key) { |
Dorsh | 6:7ed6688e3caf | 37 | |
Dorsh | 6:7ed6688e3caf | 38 | static int subsys = 0; //0 = DAC, 1 = DigiPot, 2 = ADC |
Dorsh | 6:7ed6688e3caf | 39 | //static will keep the state of subsys after each call |
Dorsh | 6:7ed6688e3caf | 40 | const char* sysName[] = {"DAC", "DigiPot", "ADC"}; |
Dorsh | 6:7ed6688e3caf | 41 | const int numOfSubsys = 3; |
Dorsh | 6:7ed6688e3caf | 42 | \ |
Dorsh | 6:7ed6688e3caf | 43 | //cycles between subsystems when the '#' key is pressed |
Dorsh | 6:7ed6688e3caf | 44 | if(key == '#') { |
Dorsh | 6:7ed6688e3caf | 45 | subsys = (subsys + 1) % (numOfSubsys); //cycles from 0,1,2,0,1,2,etc |
Dorsh | 6:7ed6688e3caf | 46 | printf("Now working with Subsystem %s\n", sysName[subsys]); |
Dorsh | 6:7ed6688e3caf | 47 | return "switched"; |
Dorsh | 6:7ed6688e3caf | 48 | } |
Dorsh | 4:a10c7172b4f6 | 49 | |
Dorsh | 6:7ed6688e3caf | 50 | //This handles all message creation |
Dorsh | 6:7ed6688e3caf | 51 | //Work with the other teams to determine what they need |
Dorsh | 6:7ed6688e3caf | 52 | //Stick to 8 characters (8 bytes) max |
Dorsh | 6:7ed6688e3caf | 53 | //Probably better to use a code (like hex or something) |
Dorsh | 6:7ed6688e3caf | 54 | else { |
Dorsh | 6:7ed6688e3caf | 55 | switch(subsys) { |
Dorsh | 6:7ed6688e3caf | 56 | case 0: |
Dorsh | 6:7ed6688e3caf | 57 | if(key == '*') |
Dorsh | 6:7ed6688e3caf | 58 | return "DACswitch"; |
Dorsh | 6:7ed6688e3caf | 59 | else if(key == '1') |
Dorsh | 6:7ed6688e3caf | 60 | return "omsg"; |
Dorsh | 6:7ed6688e3caf | 61 | break; |
Dorsh | 6:7ed6688e3caf | 62 | case 1: |
Dorsh | 6:7ed6688e3caf | 63 | if(key == '*') |
Dorsh | 6:7ed6688e3caf | 64 | return "msg"; |
Dorsh | 6:7ed6688e3caf | 65 | else if(key == '1') |
Dorsh | 6:7ed6688e3caf | 66 | return "omsg"; |
Dorsh | 6:7ed6688e3caf | 67 | break; |
Dorsh | 6:7ed6688e3caf | 68 | case 2: |
Dorsh | 6:7ed6688e3caf | 69 | if(key == '*') |
Dorsh | 6:7ed6688e3caf | 70 | return "msg"; |
Dorsh | 6:7ed6688e3caf | 71 | else if(key == '1') |
Dorsh | 6:7ed6688e3caf | 72 | return "omsg; |
Dorsh | 6:7ed6688e3caf | 73 | break; |
Dorsh | 4:a10c7172b4f6 | 74 | } |
Dorsh | 6:7ed6688e3caf | 75 | return "null"; //no message |
Dorsh | 4:a10c7172b4f6 | 76 | } |
Dorsh | 6:7ed6688e3caf | 77 | } |
Dorsh | 4:a10c7172b4f6 | 78 | |
Dorsh | 4:a10c7172b4f6 | 79 | int main() { |
Dorsh | 4:a10c7172b4f6 | 80 | // r0 r1 r2 r3 c0 c1 c2 |
Dorsh | 2:022b41ff9719 | 81 | Keypad keypad(p21, p22, p23, p24, p25, p26, p27); |
Dorsh | 2:022b41ff9719 | 82 | keypad.attach(&cbAfterInput); |
Dorsh | 2:022b41ff9719 | 83 | keypad.start(); // energize the keypad via c0-c3 |
Dorsh | 5:d27422e874af | 84 | char *msg = "a"; //initialize to something |
Dorsh | 4:a10c7172b4f6 | 85 | while (1) { |
Dorsh | 6:7ed6688e3caf | 86 | __wfi(); //waits for input |
Dorsh | 4:a10c7172b4f6 | 87 | printf("Interrupted\r\n"); |
Dorsh | 2:022b41ff9719 | 88 | wait(0.5); |
Dorsh | 6:7ed6688e3caf | 89 | *msg = createMessage( Keytable[Index] ); |
Dorsh | 2:022b41ff9719 | 90 | send(msg); |
Dorsh | 4:a10c7172b4f6 | 91 | printf("Index:%d => Key:%c\r\n", Index, Keytable[Index]); |
Dorsh | 2:022b41ff9719 | 92 | } |
Dorsh | 2:022b41ff9719 | 93 | } |