yup

Dependencies:   keypad mbed

Fork of Lab3 by Matthew Dorshimer

Revision:
10:337f8b757d8d
Parent:
9:192484f373fd
Child:
11:3cc46e37f97f
--- a/main.cpp	Fri Nov 18 19:12:57 2016 +0000
+++ b/main.cpp	Mon Nov 28 17:47:24 2016 +0000
@@ -3,11 +3,31 @@
                      //Modified to work with 4 row 3 col keypad
 #include "CAN.h"     //Open Source Library for CAN transmitter
 
-DigitalOut myled(LED1);
+/*
+ * 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);
-
-//Keypad Values
+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
@@ -15,90 +35,89 @@
                  };
                  // c0   c1   c2   
  
-//This is what happens when a key is pressed
+//Runs whenever a key is pressed
 //Assigns Index to the value generated by the keypad
-//This lets us use the keypad number
+//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;
     }
   
-void send(char *str) {
-    printf("send(%s)\n",str);
-    if(can1.write(CANMessage(1337, str, 8))) {
-        printf("wrote successfully\n");
-        } 
-        
-    }
-    
-//Returns a specific message to be sent to the CAN Bus
-void createMessage(char key, char msg[]) {
-    
-    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;
-    char message[8];
-    //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(message, '\0', sizeof(message));
-        strcpy(message, "switch");
-        memset(msg, '\0', sizeof(msg));
-        strcpy(msg,message);
-        printf("this is the solution\n");
-    }
-    
-    //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 == '*')
-                    strcpy(message, "D_");
-                else if(key == '1')
-                    strcpy(message, "D_");
-                break;
-            case 1:
-                if(key == '*')
-                    strcpy(message, "DACswitc");
-                else if(key == '1')
-                    strcpy(message, "DACswitc");
-                break;
-            case 2:
-                if(key == '*')
-                    strcpy(message, "A_switch");
-                break;
-            default:
-                strcpy(message, "null"); //no message
-                break;
-            
-            }
-        strcpy(msg,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[8];//initialize to something 8 characters
+      
+      char msg[msgLength];//initialize to something 8 characters
       CANMessage recievedMessage;
     while (1) {
         __wfi(); //waits for input
         printf("Interrupted\r\n");
         wait(0.5);
-        createMessage( Keytable[Index], msg ); //problem
         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
       }
-  }
\ No newline at end of file
+  }
+  
+  
+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;
+            
+            }
+    }
+}
\ No newline at end of file