Team Kryptonite EE300 Keypad Subsystem

Dependencies:   keypad mbed

Fork of mbed_can_bus_test1 by Kryptonite

Revision:
3:e019f1954b2a
Parent:
2:02021bd6b07e
--- a/main.cpp	Fri Dec 01 16:06:09 2017 +0000
+++ b/main.cpp	Thu Dec 07 23:17:35 2017 +0000
@@ -1,4 +1,8 @@
 #include "mbed.h"
+#include <iostream>
+#include <cmath>
+#include <iomanip>
+
 #include "Keypad.h"
 
 Ticker ticker;
@@ -27,30 +31,35 @@
                     //p5, p6, p7
                     //mbedpin25, mbedpin26, mbedpin27
 
+void functionone(int& Index, int& count);
+void functiontwo();
+void functionthree();
 
-int32_t       Index =-1;
-int           State;
- 
-uint32_t cbAfterInput(uint32_t _index)
+using namespace std;
+
+int32_t Index;
+int32_t count = 0;
+
+uint32_t cbAfterInput(uint32_t index)
 {
-    Index = _index;
+    Index = index;
     return 0;
-} 
+}
 
 //Universal ID: 1337
 //CAN Message Info
-char counter        = 0;    //keypad system CAN test variable
+char counter        = '0';    //keypad system CAN test variable
 char start          = '*';  //Start
 char stop           = '#';  //Stop / Reset
-char amp5           = 5;    //Digipot
-char amp4           = 4;    //Digipot
-char amp3           = 3;    //Digipot
-char amp2           = 2;    //Digipot
-char amp1           = 1;    //Digipot
-char sin10Hz        = 'q';  //DAC -6
-char sin1Hz         = 'w';  //DAC -7
-char sqr10Hz        = 'o';  //DAC -8
-char sqr1Hz         = 't';  //DAC -9
+char amp5           = '5';    //Digipot
+char amp4           = '4';    //Digipot
+char amp3           = '3';    //Digipot
+char amp2           = '2';    //Digipot
+char amp1           = '1';    //Digipot
+char sin10Hz        = 'q';  //DAC -
+char sin1Hz         = 'w';  //DAC -
+char sqr10Hz        = 'o';  //DAC -
+char sqr1Hz         = 't';  //DAC -
 char saveUSB        = 'u';  //ADC -0
 char saveLocal      = 'l';  //ADC That's an l as in labeorphily
 char keypress;
@@ -58,117 +67,222 @@
 
 //CAN Send (Parameter = message data) 
 void send(char sendMsg) {
-    if(can1.write(CANMessage(1337, &sendMsg, 1))) {
-        printf("Message sent: %d\n", sendMsg);       
+    if(can1.write(CANMessage(1337, &sendMsg, 1))) 
+    {
+        printf("Command sent: %c\n\n", sendMsg);       
     }
-    led1 = !led1;
+    led1 = !led1;  
 }
 
+
 /* write Switch statements*/
 //Setup Choice Switch Statements
 
+void functionone(int& Index, int& count)// Get Keypad Input to change Index
+{
+    while(count == Index)
+        {
+            Keypad keypad(p21, p22,  p23,  p24, p25, p26,  p27,  NC);
+            keypad.attach(&cbAfterInput);
+            keypad.start();
+        }
+    count = Index;
+
+}
+
+void functiontwo() //DAC
+{
+
+    int reset = -1;
+    
+    while(reset == -1)
+    {
+        cout << "-------------------------------------------------\n\r" << endl;
+        cout << "For 10Hz Sine Wave select: 3\n\r" << endl;
+        cout << "For 1Hz Sine Wave select: 4\n\r" << endl;
+        cout << "For 10Hz Square Wave select: 5\n\r" << endl;
+        cout << "For 1Hz Square Wave select: 6\n\r" << endl;
+    
+    functionone(Index, count);
+    
+    if (Index == 2)
+    {
+        cout << "10Hz Sine Wave has been selected \n\r" << endl;
+        send(sin10Hz);
+        reset = 1; 
+    }
+    else if (Index == 3)
+    {
+        cout << "1Hz Sine Wave has been selected\n\r" << endl;
+        send(sin1Hz);
+        reset = 1;
+    }
+    else if (Index == 4)
+    {
+        cout << "10Hz Square Wave has been selected \n\r" << endl;
+        send(sqr10Hz);
+        reset = 1;
+    }
+    else if (Index == 5)
+    {
+        cout << "1Hz Square Wave has been selected \n\r" << endl;
+        send(sqr1Hz);
+        reset = 1;
+    }
+    else
+    {
+        reset = -1;
+    }
+}
+}
+
+void functionthree()//Digipot Config (select amplitude)
+{
+    int flag = -1;
+    
+    while (flag == -1)
+    {
+        cout << "-------------------------------------------------\n\r" << endl;
+        cout << "Select Waveform Amplitude\n\r";
+        cout << "1V = 1,\n2V = 2,\n3V = 3,\n4V = 4,\n5v = 5\n\r" << endl;
+        
+        functionone(Index, count);
+        
+        if (Index == 0)
+        {
+            send(amp1);
+            flag =1;
+        }
+        else if (Index == 1)
+        {
+            send(amp2);
+            flag = 1;
+        }
+        else if (Index == 2)
+        {
+            send(amp3);
+            flag = 1;
+        }
+        else if (Index == 3)
+        {
+            send(amp4);
+            flag = 1;
+        }
+        else if (Index == 4)
+        {
+            send(amp5);
+            flag = 1;
+        }
+        else 
+        {
+            flag = -1;
+        }
+    }
+}
+
+void functionfour() //ADC Config (Save Location Settings)
+{
+    int flag = -1;
+    
+    while(flag == -1)
+    {
+        cout << "-------------------------------------------------\n\r" << endl;
+        cout << "Select Save Location:\n\r" << endl;
+        cout << "Save Locally (default) = 5, Save to USB = 6\n\r" << endl;
+        
+        functionone(Index, count);
+        
+        if (Index == 4)
+        {
+            cout << "Save locally has been selected\n\r" << endl;
+            send(saveLocal);
+            flag = 1;
+        }
+        else if (Index == 5)
+        {
+            cout << "Save to USB has been selected\n\r" << endl;
+            send(saveUSB);
+            flag = 1;
+        }
+        else
+        {
+            flag =-1;
+        }
+    }
+}
+
+void functionfive()//Start Stop Waveform Command Menu
+{
+    int flag = -1;
+
+    while(flag == -1)
+    {
+        cout << "Start/Stop waveform: start = 7, stop = 8 \n\r" << endl;
+
+        functionone(Index, count);
+
+        if (Index ==6)
+        {
+            cout << "Wave has been started\n\r" << endl;
+            send(start);
+            flag = 1;
+        }
+        else if (Index == 7)
+        {
+            cout << "Wave has been stopped\n\r" << endl;
+            send(stop);
+            flag = 1;
+        }
+        else
+        {
+            flag = -1;
+        }
+    }
+}
+
 
 
 
 
-int main() { 
-
-
-    State = 1;
-
-
-    //             r0   r1   r2   r3   c0   c1   c2   c3
-    Keypad keypad(p21, p22,  p23,  p24, p25, p26,  p27,  NC);
-    keypad.attach(&cbAfterInput);
-    keypad.start();  // energize the columns c0-c3 of the keypad
- 
-    while (1) {
-        __wfi();
-        if (Index > -1) {
-            PC.printf("Interrupted");
-            PC.printf("Index:%d => Key:%c\r\n", Index, Keytable[Index]);
-            Index = -1;
-            printf("here2");
-        }
-    
- 
- printf("Please choose waveform:\n1: 1V \n2: 2V \n3: 3V \n4: 4V \n5: 5V \n6: 10Hz, Sine wave");
- printf("\n7: 1Hz, Sine Wave\n8: 10Hz, Square Wave \n9: 1Hz, Square wave \n0: Save to USB \n*: Start Waveform Generation \n#: Stop/Reset Waveform\n");
-   
+int main()
+{
+    cout << "EE 300 Embedded System Black Box Transfer Function Data Acquisition\n\r" << endl;
     while(1)
     {
-        keypress = Keytable[Index];
-    switch(keypress) {
-   case 1  :
-    send(amp1);
-    Index=-1;
-    printf("here1");
-      break; //optional
-      
-   case 2  :
-      send(amp2);
-      Index=-1;
-      break; //optional
-      
-   case 3  :
-      send(amp3);
-     Index=-1;
-      break; //Optional   
-   
-   case 4 :
-      send(amp4);
-     Index=-1;
-      break;
-   
-    case 5 : 
-      send(amp5);
-      Index=-1;
-      break;
-    
-    case 6 :
-      send(sin10Hz);
-     Index=-1;
-      break;
-    
-    case 7 :
-        send(sin1Hz);
-      Index=-1;
-        break;
-    
-    case 8 :
-        send(sqr10Hz);
-        Index=-1;
-        break;
-    
-    case 9 :
-        send(sqr1Hz);
-       Index=-1;
-        break;
-    
-    case 0 :
-        send(saveUSB);
-      Index=-1;
-        break;
-    
-    case '*' :
-        send(start);
-        Index=-1;
-        break;
-    
-    case '#' :
-        send(stop);
-       Index=-1;
-        break;
+        int flag2 = -1;
+        
+        while(flag2 == -1)
+        {
+            cout << "-------------------------------------------------\n\r" << endl; 
+            cout << "Select Wave Type = 9,\nSelect Wave Amplitude = *,\n\r";
+            cout << "Select Save Location =0,\nStart/Stop Wave = #\n\r" << endl;
+            functionone(Index, count);
             
-            };
+            if (Index == 8)//9
+            {
+                functiontwo();//DAC Config Function
+                flag2 = 1;
+            }
+            else if (Index == 9)//*
+            {
+                functionthree();//Digipot Config
+                flag2 = 1;
             }
-    /*CANMessage msg; 
-    while(1) {
-        if(can2.read(msg)) {
-            printf("Message Recieved: %d\n\n", msg.data[0]);
-            led2 = !led2;
+            else if (Index == 10) //0
+            {
+                functionfour();//ADC Config
+                flag2 = 1;
+            }
+            else if (Index == 11)//#
+            {
+                functionfive();//Start/Stop Waveform Function
+                flag2 = 1;
+            }
+            else
+            {
+                flag2 = -1;
+            }
         }
-    wait(0.2);
-    }*/
+       
+    }
 }
-}