Mbed code for KeyPad

Dependencies:   KeyPad2 mbed

Files at this revision

API Documentation at this revision

Comitter:
darksonic56
Date:
Sat Dec 09 21:45:18 2017 +0000
Parent:
7:630372bb8c15
Commit message:
final code for the report guys!

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
diff -r 630372bb8c15 -r 8155a917d4ec main.cpp
--- a/main.cpp	Wed Dec 06 01:38:47 2017 +0000
+++ b/main.cpp	Sat Dec 09 21:45:18 2017 +0000
@@ -5,20 +5,11 @@
 //Ticker ticker;
 DigitalOut led1(LED1);
 DigitalOut led2(LED2);
-CAN can1(p30,p29); //p30(rx) and p29(tx)
-CAN can2(p9, p10); //p9 (rx) and p10(tx)
+CAN can2(p30,p29); //p30(rx) and p29(tx)
+CAN can1(p9, p10); //p9 (rx) and p10(tx)
 CANMessage msg;
 char counter = 0;
 
-// function to send the CAN message 
-void send()
-{
-    if(can1.write(CANMessage(1337, &counter, 5))) {
-        counter++;
-        printf("Message sent: %d\n\r", counter);
-        }
-        led1 =!led1;
-}
 char Keytable[] = { '1', '2', '3', 'A', 
                     '4', '5', '6', 'B',   
                     '7', '8', '9', 'C',   
@@ -33,7 +24,8 @@
 int main() {
     //variables declared
     char mainLvl, partLvl;
-    int toggle = 1;
+    int toggle = 0;
+    int squareToggle = 0;
     int digpotAmp = 0;
     Keypad::Keypad keypad(p21, p22, p23, p24, p25, p26, p27, p28, 20);
     keypad.attach(&cbAfterInput);
@@ -48,17 +40,10 @@
         pc.printf("2. Digital Potentiometer Setup\r\n");
         pc.printf("3. ADC Setup\r\n");
         pc.printf("\r\n");
-       
-       
-        //pc.printf("Input a key\r\n");
-        // pc.printf("Key %c was pressed\r\n", key); 
-        //reads the keypad input
         
         __wfi();
         mainLvl = Keytable[Index];
       
-               
-       
         switch(mainLvl)
         {
             case '1': // DAC Setup
@@ -75,61 +60,54 @@
                  pc.printf("\r\n");
                  __wfi();
                  partLvl = Keytable[Index];
-               //  pc.printf("\r\n\r\n Key %c was pressed\r\n\r\n", partLvl);
-                 send(); // sends the CAN message
+               
                 switch(partLvl)
                 {
                     case '1':
+                    counter = 5;
+                    can1.write(CANMessage(1337, &counter, 1));
                     pc.printf("DAC Enabled\r\n");
                     pc.printf("\r\n");
                 
                     break;
                     
                     case '2':
+                    counter = 0;
+                    can1.write(CANMessage(1337, &counter, 1));
                     pc.printf("DAC Disabled\r\n");
                     pc.printf("\r\n");
                  
                     break;
                     
                     case '3':
-                    if(toggle == 1)
-                    {
                      pc.printf("Sinusoid wave 1 HZ wave selected\r\n");
+                     counter = 1;
+                     can1.write(CANMessage(1337, &counter, 1));
                      toggle = 1;
-                     }
                      break;
                     case '4':
-                    if(toggle == 10)
-                    {
                      pc.printf("Sinusoid wave 10 HZ wave selected\r\n");
+                     counter = 3;
+                     can1.write(CANMessage(1337, &counter, 1));
                      toggle = 10;
-                     }
                      break;
                     case '5': 
-                    if(toggle == 1)
-                    {
                      pc.printf("Square wave 1 HZ wave selected\r\n");
-                     toggle = 1;
-                     }
+                     counter = 2;
+                     can1.write(CANMessage(1337, &counter, 1));
+                     squareToggle = 1;
                      break;
                     case '6': 
-                    if(toggle == 10)
-                    {
                      pc.printf("Square wave 10 HZ wave selected\r\n");
-                     toggle = 10;
-                     }
+                     counter = 4;
+                     can1.write(CANMessage(1337, &counter, 1));
+                     squareToggle = 10;
+                     
                      break;
                      default:
                      break;
                     }         
             }
-            /*
-            if(can2.read(msg))
-            {
-                printf("Message received: %d\n\r", msg.data[0]);
-                led2 != led2;
-            }
-            */
             partLvl = '0';
             break;
             
@@ -149,7 +127,7 @@
                  pc.printf("\r\n");
                  __wfi();
                  partLvl = Keytable[Index];
-                 send(); // sends the CAN message 
+                 //send(); // sends the CAN message 
                  switch(partLvl)
                 {
                   case '0':
@@ -213,13 +191,6 @@
                 }
             }
             
-             /*
-            if(can2.read(msg))
-            {
-                printf("Message received: %d\n\r", msg.data[0]);
-                led2 != led2;
-            }
-            */
               partLvl = '0';
               break;
               
@@ -235,16 +206,20 @@
                     pc.printf("\r\n");
                     __wfi(); 
                     partLvl = Keytable[Index];
-                    send(); // sends the CAN message
+                    //send(); // sends the CAN message
                     switch(partLvl)
                     {
                     
                     case '1':
+                    counter = 1;
+                    can1.write(CANMessage(1337, &counter, 1));
                     pc.printf("ADC Enabled\r\n");
                     pc.printf("\r\n");
                     break;
                     
                     case '2':
+                    counter = 0;
+                    can1.write(CANMessage(1337, &counter, 1));
                     pc.printf("ADC Disabled\r\n");
                     pc.printf("\r\n");
                     break;
@@ -253,16 +228,9 @@
                     break;
                     }
              } 
-               /*
-            if(can2.read(msg))
-            {
-                printf("Message received: %d\n\r", msg.data[0]);
-                led2 != led2;
-            }
-            */
                 partLvl = '0';
                 break; 
             }
         }         
     }
-}
+}
\ No newline at end of file