Callum and Adel's changes on 12/02/19

Dependencies:   Crypto

Revision:
29:c96439a60184
Parent:
28:4f02ac845e5d
Child:
30:fbae0e5f200d
--- a/main.cpp	Sat Mar 16 18:16:39 2019 +0000
+++ b/main.cpp	Mon Mar 18 15:37:20 2019 +0000
@@ -47,39 +47,23 @@
 7       -   -   -
 */
 
-/*//Declare and start threads
-class T_{
-
-    // protected:
-    public:
+//Status LED
+DigitalOut led1(LED1);
 
-        Thread *p_comm_in;
-        Thread *p_comm_out;
-        Thread *p_motor_ctrl;
-
-
+//Photointerrupter inputs
+InterruptIn I1(I1pin);
+InterruptIn I2(I2pin);
+InterruptIn I3(I3pin);
 
-        T_(){
-            //(priority, stack size,
-            Thread comm_in(osPriorityAboveNormal, 1024);
-            Thread comm_out(osPriorityAboveNormal, 1024);
-            Thread motor_ctrl(osPriorityAboveNormal, 1024);
-
-            p_comm_in = &comm_in;
-            p_comm_out = &comm_out;
-            p_motor_ctrl = &motor_ctrl;
+//Motor Drive outputs
+DigitalOut L1L(L1Lpin);
+DigitalOut L1H(L1Hpin);
+DigitalOut L2L(L2Lpin);
+DigitalOut L2H(L2Hpin);
+DigitalOut L3L(L3Lpin);
+DigitalOut L3H(L3Hpin);
 
-        }
-
-        ~T_(){
-            if (p_comm_in->get_state() == 2)
-                p_comm_in->terminate();
-            if (p_comm_out->get_state() == 2)
-                p_comm_out->terminate();
-            if (p_motor_ctrl->get_state() == 2)
-                p_motor_ctrl->terminate();
-        }
-};*/
+PwmOut pwmCtrl(PWMpin);
 
 //Drive state to output table
 const int8_t driveTable[] = {0x12,0x18,0x09,0x21,0x24,0x06,0x00,0x00};
@@ -408,36 +392,12 @@
     
     int8_t* params[2];
 
-    //Status LED
-    DigitalOut led1(LED1);
-
-    //Photointerrupter inputs
-    InterruptIn I1(I1pin);
-    InterruptIn I2(I2pin);
-    InterruptIn I3(I3pin);
-
-    //Motor Drive outputs
-    DigitalOut L1L(L1Lpin);
-    DigitalOut L1H(L1Hpin);
-    DigitalOut L2L(L2Lpin);
-    DigitalOut L2H(L2Hpin);
-    DigitalOut L3L(L3Lpin);
-    DigitalOut L3H(L3Hpin);
-
-    PwmOut pwmCtrl(PWMpin);
-
-
-
+    float dutyC; // 1 = 100%
+    float mtrPeriod; // motor period
+    int stateCount;
 
 public:
 
-
-
-    float dutyC = 1; // 100%
-    float mtrPeriod = 2e-3; // motor period
-
-
-
     Motor(){
 
         orState = motorHome();;    //Rotot offset at motor state 0
@@ -447,21 +407,23 @@
         motorOut((currentState-orState+lead+6)%6); // We push it digitally
 
         dutyC = 0.8;
+        mtrPeriod = 2e-3; // motor period
         pwmCtrl.pulsewidth(mtrPeriod*dutyC);
 
         params[0] = &currentState;
         params[1] = &orState;
 
-        I1.fall(callback(&stateUpdate,params));
-        I2.fall(callback(&stateUpdate,params));
-        I3.fall(callback(&stateUpdate,params));
-
-        I1.rise(callback(&stateUpdate,params));
-        I2.rise(callback(&stateUpdate,params));
-        I3.rise(callback(&stateUpdate,params));
+        I1.fall(callback(this, &Motor::stateUpdate));
+        I2.fall(callback(this, &Motor::stateUpdate));
+        I3.fall(callback(this, &Motor::stateUpdate));
+        I1.rise(callback(this, &Motor::stateUpdate));
+        I2.rise(callback(this, &Motor::stateUpdate));
+        I3.rise(callback(this, &Motor::stateUpdate));
 
         pwmCtrl.period(mtrPeriod);
         pwmCtrl.pulsewidth(mtrPeriod*dutyC);
+        
+        stateCount = 0;
 
     }
 
@@ -505,15 +467,29 @@
     }
 
 
-    void stateUpdate(int8_t *params[]) { // () { // **params
+    void stateUpdate() { // () { // **params
         *params[0] = readRotorState();
         int8_t currentState = *params[0];
         int8_t offset = *params[1];
 
         motorOut((currentState - offset + lead + 6) % 6);
+        
+        if (stateCount<6){
+            stateList[stateCount] = currentState;
+            stateCount++;
+        }
+        else {
+            //pc.printf("states");
+            //for(int i = 0; i < 6; ++i)
+            //pc.printf("%02i,", stateList[i]);
+            //pc.printf("\n\r");
+            stateCount = 0;
+        }
+        
+        
     }
 
-}
+};
 
 
 
@@ -558,7 +534,7 @@
 
     // Keep the program running indefinitely
     timer.start();          // start timer
-    int stateCount = 0;
+    
     while (1) {
         // pc.printf("Current:%d \t Next:%d \n\r", currentState, (currentState-orState+lead+6)%6);
         comm_plz.newKey_mutex.lock();
@@ -573,18 +549,6 @@
         // Try a new nonce
         (*nonce)++;
 
-        if (stateCount<6){
-            stateList[stateCount] = currentState;
-            stateCount++;
-        }
-        else {
-            //pc.printf("states");
-            //for(int i = 0; i < 6; ++i)
-            //pc.printf("%02i,", stateList[i]);
-            //pc.printf("\n\r");
-            stateCount = 0;
-        }
-
         // Per Second i.e. when greater or equal to 1
         if (timer.read() >= 1){
             comm_plz.putMessage((Comm::msgType)5, hashCounter);