yup

Dependencies:   keypad mbed

Fork of Lab3 by Matthew Dorshimer

main.cpp

Committer:
Dorsh
Date:
2016-11-08
Revision:
7:31184fd43707
Parent:
6:7ed6688e3caf
Child:
8:9af374c3b559

File content as of revision 7:31184fd43707:

#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

DigitalOut myled(LED1);
CAN can1(p30,p29);


//Keypad Values
char Keytable[] = { '1', '2', '3',    // r0
                    '4', '5', '6',    // r1
                    '7', '8', '9',    // r2
                    '*', '0', '#',    // r3
                 };
                 // c0   c1   c2   
 
//This is what happens when a key is pressed
//Assigns Index to the value generated by the keypad
//This lets us use the keypad number
uint32_t Index;
uint32_t cbAfterInput(uint32_t index) {
    Index = index;
    return 0;
    }
  
void send(char *str) {
    printf("send(%s)\n",str);
    if(can1.write(CANMessage(1, str, 1))) {
        printf("wloop()\n");
        printf("Message sent: \n");
        } 
    }
    
//Returns a specific message to be sent to the CAN Bus
const char *createMessage(char *key) {
    
    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]);
        return "switched";
    }
    
    //This 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 == '*')
                    return "DACswitch";
                else if(key == '1')
                    return "omsg";
                break;
            case 1:
                if(key == '*')
                    return "msg";
                else if(key == '1')
                    return "omsg";
                break;
            case 2:
                if(key == '*')
                    return "msg";
                else if(key == '1')
                    return "omsg;
                break;
            }
        return "null"; //no message
    }
}
    
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 = "a"; //initialize to something
    while (1) {
        __wfi(); //waits for input
        printf("Interrupted\r\n");
        wait(0.5);
        *msg = createMessage( Keytable[Index] );
        send(msg);
        printf("Index:%d => Key:%c\r\n", Index, Keytable[Index]);
      }
  }