Oscillators
/
Lab3
yup
Fork of Lab3 by
main.cpp@10:337f8b757d8d, 2016-11-28 (annotated)
- Committer:
- Dorsh
- Date:
- Mon Nov 28 17:47:24 2016 +0000
- Revision:
- 10:337f8b757d8d
- Parent:
- 9:192484f373fd
- Child:
- 11:3cc46e37f97f
Some organizational changes + added comments to the code
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 | |
Dorsh | 10:337f8b757d8d | 6 | /* |
Dorsh | 10:337f8b757d8d | 7 | * Written By: |
Dorsh | 10:337f8b757d8d | 8 | * Matthew Dorshimer |
Dorsh | 10:337f8b757d8d | 9 | * Ian |
Dorsh | 10:337f8b757d8d | 10 | * Mohammed |
Dorsh | 10:337f8b757d8d | 11 | * |
Dorsh | 10:337f8b757d8d | 12 | * A keypad with 12 buttons (0-9,*,#) controls the Digital-Analog Conveter, |
Dorsh | 10:337f8b757d8d | 13 | * DigiPot, and Analog-Digial Converter Subsystems |
Dorsh | 10:337f8b757d8d | 14 | * This program controls the subsystems by sending messages to the CAN Bus |
Dorsh | 10:337f8b757d8d | 15 | * |
Dorsh | 10:337f8b757d8d | 16 | * Only one subsystem can be controlled at a time. Pressing '#' switches the active |
Dorsh | 10:337f8b757d8d | 17 | * subsystem. This gives each subsystem complete control of the keypad |
Dorsh | 10:337f8b757d8d | 18 | * |
Dorsh | 10:337f8b757d8d | 19 | * Left to Do: |
Dorsh | 10:337f8b757d8d | 20 | * We have not successfully read any messages. Once we can successfully |
Dorsh | 10:337f8b757d8d | 21 | * read messages, the keypad subsystem is complete |
Dorsh | 10:337f8b757d8d | 22 | */ |
Dorsh | 10:337f8b757d8d | 23 | |
Dorsh | 10:337f8b757d8d | 24 | void createMessage(char* msg); //Creates message based off Keytable[Index] press |
Dorsh | 10:337f8b757d8d | 25 | void send(char* msg); //Takes the created message and sends to the CAN Bus |
Dorsh | 10:337f8b757d8d | 26 | |
Dorsh | 10:337f8b757d8d | 27 | //DigitalOut myled(LED1); |
Dorsh | 8:9af374c3b559 | 28 | CAN can2(p30,p29); |
Dorsh | 9:192484f373fd | 29 | CAN can1(p9,p10); |
Dorsh | 10:337f8b757d8d | 30 | const int msgLength = 8; //Length of message sent to CAN Bus |
Dorsh | 4:a10c7172b4f6 | 31 | char Keytable[] = { '1', '2', '3', // r0 |
Dorsh | 4:a10c7172b4f6 | 32 | '4', '5', '6', // r1 |
Dorsh | 4:a10c7172b4f6 | 33 | '7', '8', '9', // r2 |
Dorsh | 4:a10c7172b4f6 | 34 | '*', '0', '#', // r3 |
Dorsh | 4:a10c7172b4f6 | 35 | }; |
Dorsh | 4:a10c7172b4f6 | 36 | // c0 c1 c2 |
Dorsh | 2:022b41ff9719 | 37 | |
Dorsh | 10:337f8b757d8d | 38 | //Runs whenever a key is pressed |
Dorsh | 4:a10c7172b4f6 | 39 | //Assigns Index to the value generated by the keypad |
Dorsh | 10:337f8b757d8d | 40 | //The value generated by the keypad is the index of the key pressed |
Dorsh | 4:a10c7172b4f6 | 41 | uint32_t Index; |
Dorsh | 4:a10c7172b4f6 | 42 | uint32_t cbAfterInput(uint32_t index) { |
Dorsh | 4:a10c7172b4f6 | 43 | Index = index; |
Dorsh | 4:a10c7172b4f6 | 44 | return 0; |
Dorsh | 4:a10c7172b4f6 | 45 | } |
Dorsh | 2:022b41ff9719 | 46 | |
Dorsh | 4:a10c7172b4f6 | 47 | |
Dorsh | 4:a10c7172b4f6 | 48 | int main() { |
Dorsh | 4:a10c7172b4f6 | 49 | // r0 r1 r2 r3 c0 c1 c2 |
Dorsh | 2:022b41ff9719 | 50 | Keypad keypad(p21, p22, p23, p24, p25, p26, p27); |
Dorsh | 2:022b41ff9719 | 51 | keypad.attach(&cbAfterInput); |
Dorsh | 2:022b41ff9719 | 52 | keypad.start(); // energize the keypad via c0-c3 |
Dorsh | 10:337f8b757d8d | 53 | |
Dorsh | 10:337f8b757d8d | 54 | char msg[msgLength];//initialize to something 8 characters |
Dorsh | 9:192484f373fd | 55 | CANMessage recievedMessage; |
Dorsh | 4:a10c7172b4f6 | 56 | while (1) { |
Dorsh | 6:7ed6688e3caf | 57 | __wfi(); //waits for input |
Dorsh | 4:a10c7172b4f6 | 58 | printf("Interrupted\r\n"); |
Dorsh | 2:022b41ff9719 | 59 | wait(0.5); |
Dorsh | 2:022b41ff9719 | 60 | send(msg); |
Dorsh | 4:a10c7172b4f6 | 61 | printf("Index:%d => Key:%c\r\n", Index, Keytable[Index]); |
Dorsh | 9:192484f373fd | 62 | if(can2.read(recievedMessage)) { |
Dorsh | 9:192484f373fd | 63 | printf("Message received: %s\n", recievedMessage.data); |
Dorsh | 10:337f8b757d8d | 64 | } //This never runs. Message can never be received for some reason |
Dorsh | 2:022b41ff9719 | 65 | } |
Dorsh | 10:337f8b757d8d | 66 | } |
Dorsh | 10:337f8b757d8d | 67 | |
Dorsh | 10:337f8b757d8d | 68 | |
Dorsh | 10:337f8b757d8d | 69 | void send(char *str) { |
Dorsh | 10:337f8b757d8d | 70 | createMessage(str); |
Dorsh | 10:337f8b757d8d | 71 | printf("send(%s)\n",str); |
Dorsh | 10:337f8b757d8d | 72 | if(can1.write(CANMessage(1337, str, msgLength))) { |
Dorsh | 10:337f8b757d8d | 73 | printf("wrote successfully\n"); |
Dorsh | 10:337f8b757d8d | 74 | } |
Dorsh | 10:337f8b757d8d | 75 | } |
Dorsh | 10:337f8b757d8d | 76 | |
Dorsh | 10:337f8b757d8d | 77 | //Returns a specific message to be sent to the CAN Bus |
Dorsh | 10:337f8b757d8d | 78 | void createMessage(char msg[]) { |
Dorsh | 10:337f8b757d8d | 79 | |
Dorsh | 10:337f8b757d8d | 80 | char key = Keytable[Index]; |
Dorsh | 10:337f8b757d8d | 81 | static int subsys = 0; //0 = DAC, 1 = DigiPot, 2 = ADC |
Dorsh | 10:337f8b757d8d | 82 | //static will keep the state of subsys after each call |
Dorsh | 10:337f8b757d8d | 83 | const char* sysName[] = {"DAC", "DigiPot", "ADC"}; |
Dorsh | 10:337f8b757d8d | 84 | const int numOfSubsys = 3; |
Dorsh | 10:337f8b757d8d | 85 | |
Dorsh | 10:337f8b757d8d | 86 | //Cycles between subsystems when the '#' key is pressed |
Dorsh | 10:337f8b757d8d | 87 | if(key == '#') { |
Dorsh | 10:337f8b757d8d | 88 | subsys = (subsys + 1) % (numOfSubsys); //cycles from 0,1,2,0,1,2,etc |
Dorsh | 10:337f8b757d8d | 89 | printf("Now working with Subsystem %s\n", sysName[subsys]); |
Dorsh | 10:337f8b757d8d | 90 | memset(msg, '\0', sizeof(msg)); |
Dorsh | 10:337f8b757d8d | 91 | strcpy(msg,"switch"); |
Dorsh | 10:337f8b757d8d | 92 | printf("this is the solution\n"); |
Dorsh | 10:337f8b757d8d | 93 | } |
Dorsh | 10:337f8b757d8d | 94 | |
Dorsh | 10:337f8b757d8d | 95 | //Handles all message creation |
Dorsh | 10:337f8b757d8d | 96 | //Work with the other teams to determine what they need |
Dorsh | 10:337f8b757d8d | 97 | //Stick to 8 characters (8 bytes) max |
Dorsh | 10:337f8b757d8d | 98 | //Probably better to use a code (like hex or something) |
Dorsh | 10:337f8b757d8d | 99 | else { |
Dorsh | 10:337f8b757d8d | 100 | switch(subsys) { |
Dorsh | 10:337f8b757d8d | 101 | case 0: |
Dorsh | 10:337f8b757d8d | 102 | if(key == '*') |
Dorsh | 10:337f8b757d8d | 103 | strcpy(msg, "D_"); |
Dorsh | 10:337f8b757d8d | 104 | else if(key == '1') |
Dorsh | 10:337f8b757d8d | 105 | strcpy(msg, "D_"); |
Dorsh | 10:337f8b757d8d | 106 | break; |
Dorsh | 10:337f8b757d8d | 107 | case 1: |
Dorsh | 10:337f8b757d8d | 108 | if(key == '*') |
Dorsh | 10:337f8b757d8d | 109 | strcpy(msg, "DACswitc"); |
Dorsh | 10:337f8b757d8d | 110 | else if(key == '1') |
Dorsh | 10:337f8b757d8d | 111 | strcpy(msg, "DACswitc"); |
Dorsh | 10:337f8b757d8d | 112 | break; |
Dorsh | 10:337f8b757d8d | 113 | case 2: |
Dorsh | 10:337f8b757d8d | 114 | if(key == '*') |
Dorsh | 10:337f8b757d8d | 115 | strcpy(msg, "A_switch"); |
Dorsh | 10:337f8b757d8d | 116 | break; |
Dorsh | 10:337f8b757d8d | 117 | default: |
Dorsh | 10:337f8b757d8d | 118 | strcpy(msg, "null"); //no message |
Dorsh | 10:337f8b757d8d | 119 | break; |
Dorsh | 10:337f8b757d8d | 120 | |
Dorsh | 10:337f8b757d8d | 121 | } |
Dorsh | 10:337f8b757d8d | 122 | } |
Dorsh | 10:337f8b757d8d | 123 | } |