Oscillators
/
Lab3
yup
Fork of Lab3 by
main.cpp
- Committer:
- Dorsh
- Date:
- 2016-11-28
- Revision:
- 10:337f8b757d8d
- Parent:
- 9:192484f373fd
- Child:
- 11:3cc46e37f97f
File content as of revision 10:337f8b757d8d:
#include "mbed.h" #include "Keypad.h" //Open Source Library for adaFruit KeyPad //Modified to work with 4 row 3 col keypad #include "CAN.h" //Open Source Library for CAN transmitter /* * Written By: * Matthew Dorshimer * Ian * Mohammed * * A keypad with 12 buttons (0-9,*,#) controls the Digital-Analog Conveter, * DigiPot, and Analog-Digial Converter Subsystems * This program controls the subsystems by sending messages to the CAN Bus * * Only one subsystem can be controlled at a time. Pressing '#' switches the active * subsystem. This gives each subsystem complete control of the keypad * * Left to Do: * We have not successfully read any messages. Once we can successfully * read messages, the keypad subsystem is complete */ void createMessage(char* msg); //Creates message based off Keytable[Index] press void send(char* msg); //Takes the created message and sends to the CAN Bus //DigitalOut myled(LED1); CAN can2(p30,p29); CAN can1(p9,p10); const int msgLength = 8; //Length of message sent to CAN Bus char Keytable[] = { '1', '2', '3', // r0 '4', '5', '6', // r1 '7', '8', '9', // r2 '*', '0', '#', // r3 }; // c0 c1 c2 //Runs whenever a key is pressed //Assigns Index to the value generated by the keypad //The value generated by the keypad is the index of the key pressed uint32_t Index; uint32_t cbAfterInput(uint32_t index) { Index = index; return 0; } int main() { // r0 r1 r2 r3 c0 c1 c2 Keypad keypad(p21, p22, p23, p24, p25, p26, p27); keypad.attach(&cbAfterInput); keypad.start(); // energize the keypad via c0-c3 char msg[msgLength];//initialize to something 8 characters CANMessage recievedMessage; while (1) { __wfi(); //waits for input printf("Interrupted\r\n"); wait(0.5); send(msg); printf("Index:%d => Key:%c\r\n", Index, Keytable[Index]); if(can2.read(recievedMessage)) { printf("Message received: %s\n", recievedMessage.data); } //This never runs. Message can never be received for some reason } } void send(char *str) { createMessage(str); printf("send(%s)\n",str); if(can1.write(CANMessage(1337, str, msgLength))) { printf("wrote successfully\n"); } } //Returns a specific message to be sent to the CAN Bus void createMessage(char msg[]) { char key = Keytable[Index]; static int subsys = 0; //0 = DAC, 1 = DigiPot, 2 = ADC //static will keep the state of subsys after each call const char* sysName[] = {"DAC", "DigiPot", "ADC"}; const int numOfSubsys = 3; //Cycles between subsystems when the '#' key is pressed if(key == '#') { subsys = (subsys + 1) % (numOfSubsys); //cycles from 0,1,2,0,1,2,etc printf("Now working with Subsystem %s\n", sysName[subsys]); memset(msg, '\0', sizeof(msg)); strcpy(msg,"switch"); printf("this is the solution\n"); } //Handles all message creation //Work with the other teams to determine what they need //Stick to 8 characters (8 bytes) max //Probably better to use a code (like hex or something) else { switch(subsys) { case 0: if(key == '*') strcpy(msg, "D_"); else if(key == '1') strcpy(msg, "D_"); break; case 1: if(key == '*') strcpy(msg, "DACswitc"); else if(key == '1') strcpy(msg, "DACswitc"); break; case 2: if(key == '*') strcpy(msg, "A_switch"); break; default: strcpy(msg, "null"); //no message break; } } }