yup

Dependencies:   keypad mbed

Fork of Lab3 by Matthew Dorshimer

Committer:
Dorsh
Date:
Fri Dec 02 18:15:45 2016 +0000
Revision:
11:3cc46e37f97f
Parent:
10:337f8b757d8d
ye

Who changed what in which revision?

UserRevisionLine numberNew 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 11:3cc46e37f97f 24 char createMessage(); //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 4:a10c7172b4f6 30 char Keytable[] = { '1', '2', '3', // r0
Dorsh 4:a10c7172b4f6 31 '4', '5', '6', // r1
Dorsh 4:a10c7172b4f6 32 '7', '8', '9', // r2
Dorsh 4:a10c7172b4f6 33 '*', '0', '#', // r3
Dorsh 4:a10c7172b4f6 34 };
Dorsh 4:a10c7172b4f6 35 // c0 c1 c2
Dorsh 2:022b41ff9719 36
Dorsh 10:337f8b757d8d 37 //Runs whenever a key is pressed
Dorsh 4:a10c7172b4f6 38 //Assigns Index to the value generated by the keypad
Dorsh 10:337f8b757d8d 39 //The value generated by the keypad is the index of the key pressed
Dorsh 4:a10c7172b4f6 40 uint32_t Index;
Dorsh 4:a10c7172b4f6 41 uint32_t cbAfterInput(uint32_t index) {
Dorsh 4:a10c7172b4f6 42 Index = index;
Dorsh 4:a10c7172b4f6 43 return 0;
Dorsh 4:a10c7172b4f6 44 }
Dorsh 2:022b41ff9719 45
Dorsh 4:a10c7172b4f6 46
Dorsh 4:a10c7172b4f6 47 int main() {
Dorsh 4:a10c7172b4f6 48 // r0 r1 r2 r3 c0 c1 c2
Dorsh 2:022b41ff9719 49 Keypad keypad(p21, p22, p23, p24, p25, p26, p27);
Dorsh 2:022b41ff9719 50 keypad.attach(&cbAfterInput);
Dorsh 2:022b41ff9719 51 keypad.start(); // energize the keypad via c0-c3
Dorsh 10:337f8b757d8d 52
Dorsh 11:3cc46e37f97f 53 char msg = 'z';
Dorsh 9:192484f373fd 54 CANMessage recievedMessage;
Dorsh 4:a10c7172b4f6 55 while (1) {
Dorsh 6:7ed6688e3caf 56 __wfi(); //waits for input
Dorsh 4:a10c7172b4f6 57 printf("Interrupted\r\n");
Dorsh 2:022b41ff9719 58 wait(0.5);
Dorsh 11:3cc46e37f97f 59 send(&msg);
Dorsh 4:a10c7172b4f6 60 printf("Index:%d => Key:%c\r\n", Index, Keytable[Index]);
Dorsh 9:192484f373fd 61 if(can2.read(recievedMessage)) {
Dorsh 9:192484f373fd 62 printf("Message received: %s\n", recievedMessage.data);
Dorsh 10:337f8b757d8d 63 } //This never runs. Message can never be received for some reason
Dorsh 2:022b41ff9719 64 }
Dorsh 10:337f8b757d8d 65 }
Dorsh 10:337f8b757d8d 66
Dorsh 10:337f8b757d8d 67
Dorsh 10:337f8b757d8d 68 void send(char *str) {
Dorsh 11:3cc46e37f97f 69 *str = createMessage();
Dorsh 11:3cc46e37f97f 70 printf("send(%c)\n",str[0]);
Dorsh 11:3cc46e37f97f 71 if(can1.write(CANMessage(1337, str, 1))) {
Dorsh 10:337f8b757d8d 72 printf("wrote successfully\n");
Dorsh 10:337f8b757d8d 73 }
Dorsh 10:337f8b757d8d 74 }
Dorsh 10:337f8b757d8d 75
Dorsh 10:337f8b757d8d 76 //Returns a specific message to be sent to the CAN Bus
Dorsh 11:3cc46e37f97f 77 char createMessage() {
Dorsh 10:337f8b757d8d 78
Dorsh 10:337f8b757d8d 79 char key = Keytable[Index];
Dorsh 10:337f8b757d8d 80 static int subsys = 0; //0 = DAC, 1 = DigiPot, 2 = ADC
Dorsh 10:337f8b757d8d 81 //static will keep the state of subsys after each call
Dorsh 10:337f8b757d8d 82 const char* sysName[] = {"DAC", "DigiPot", "ADC"};
Dorsh 10:337f8b757d8d 83 const int numOfSubsys = 3;
Dorsh 10:337f8b757d8d 84
Dorsh 10:337f8b757d8d 85 //Cycles between subsystems when the '#' key is pressed
Dorsh 10:337f8b757d8d 86 if(key == '#') {
Dorsh 10:337f8b757d8d 87 subsys = (subsys + 1) % (numOfSubsys); //cycles from 0,1,2,0,1,2,etc
Dorsh 10:337f8b757d8d 88 printf("Now working with Subsystem %s\n", sysName[subsys]);
Dorsh 10:337f8b757d8d 89 }
Dorsh 10:337f8b757d8d 90
Dorsh 10:337f8b757d8d 91 //Handles all message creation
Dorsh 10:337f8b757d8d 92 //Work with the other teams to determine what they need
Dorsh 10:337f8b757d8d 93 //Stick to 8 characters (8 bytes) max
Dorsh 10:337f8b757d8d 94 //Probably better to use a code (like hex or something)
Dorsh 10:337f8b757d8d 95 else {
Dorsh 10:337f8b757d8d 96 switch(subsys) {
Dorsh 10:337f8b757d8d 97 case 0:
Dorsh 11:3cc46e37f97f 98 if(key == '2')
Dorsh 11:3cc46e37f97f 99 return 'a';
Dorsh 10:337f8b757d8d 100 else if(key == '1')
Dorsh 11:3cc46e37f97f 101 return 'b';
Dorsh 11:3cc46e37f97f 102 else if(key == '3')
Dorsh 11:3cc46e37f97f 103 return 'c';
Dorsh 11:3cc46e37f97f 104 else if(key == '4')
Dorsh 11:3cc46e37f97f 105 return 'd';
Dorsh 11:3cc46e37f97f 106 else if(key == '5')
Dorsh 11:3cc46e37f97f 107 return 'e';
Dorsh 11:3cc46e37f97f 108 else if(key == '6')
Dorsh 11:3cc46e37f97f 109 return 'f';
Dorsh 10:337f8b757d8d 110 break;
Dorsh 10:337f8b757d8d 111 case 1:
Dorsh 11:3cc46e37f97f 112 if(key == '1')
Dorsh 11:3cc46e37f97f 113 return '1';
Dorsh 11:3cc46e37f97f 114 else if(key == '2')
Dorsh 11:3cc46e37f97f 115 return '2';
Dorsh 11:3cc46e37f97f 116 else if(key == '3')
Dorsh 11:3cc46e37f97f 117 return '3';
Dorsh 11:3cc46e37f97f 118 else if(key == '4')
Dorsh 11:3cc46e37f97f 119 return '4';
Dorsh 11:3cc46e37f97f 120 else if(key == '5')
Dorsh 11:3cc46e37f97f 121 return '5';
Dorsh 10:337f8b757d8d 122 break;
Dorsh 10:337f8b757d8d 123 case 2:
Dorsh 10:337f8b757d8d 124 if(key == '*')
Dorsh 11:3cc46e37f97f 125 return 'A';
Dorsh 10:337f8b757d8d 126 break;
Dorsh 10:337f8b757d8d 127 default:
Dorsh 11:3cc46e37f97f 128 return '0';
Dorsh 10:337f8b757d8d 129 break;
Dorsh 10:337f8b757d8d 130
Dorsh 10:337f8b757d8d 131 }
Dorsh 10:337f8b757d8d 132 }
Dorsh 10:337f8b757d8d 133 }