Martin Deng / Mbed 2 deprecated SystemManagement

Dependencies:   CANBuffer mbed SystemManagement mbed-rtos

Dependents:   SystemManagement

Revision:
4:e31528929150
Parent:
3:1345f882d8f3
Child:
5:9258b685fea6
--- a/SysMngmt.cpp	Tue Oct 07 22:42:35 2014 +0000
+++ b/SysMngmt.cpp	Wed Oct 08 00:38:41 2014 +0000
@@ -272,11 +272,11 @@
 CANBuffer rxBuffer(CAN1, MEDIUM);
 XBee250x XbeeTx;
 
-int sysCANId = 0;                   // change this later
+char sys_src_id = 4;                // source address of system management
 
-char fanId = 0;                     // first byte of CANData, last byte states the duty cycle, second-last states which fan to control
+char fan_id = 1;                     // first byte of CANData, last byte states the duty cycle, second-last states which fan to control
 
-Thread *fan_Threads[4] = {NULL, NULL, NULL, NULL};     // Threads currently running on the 4 output pins
+Thread *fan_threads[4] = {NULL, NULL, NULL, NULL};     // Threads currently running on the 4 output pins
 
 /*
     Pins for Fans and Pumps
@@ -294,22 +294,26 @@
 // duty goes from 0 to 100
 void rampFans(void const *arg){
     
-    static int fan1_duty = 0;
-    static int fan2_duty = 0;
-    static int fan3_duty = 0;
-    static int pump_duty = 0;
+    static char fan1_duty = 0;
+    static char fan2_duty = 0;
+    static char fan3_duty = 0;
+    static char pump_duty = 0;
     
-    int *data = (int *)arg;
+    char *data = (char *)arg;
     
-    int duty = data[7];
-    if(duty < 0 || duty > 100)
+    // bounds checking
+    char duty = data[1];
+    if(duty > 100)
         return;
     
-    int pin_ID = data[6];
-    int *cur_duty;
+    char pin_id = data[0];
+    if(pin_id > 3)
+        return;
+    
+    char *cur_duty;
     PwmOut *out_pin;
     
-    switch(pin_ID){
+    switch(pin_id){
         case 0: cur_duty = &pump_duty;
             out_pin = &pump_pwm;
             break;
@@ -346,11 +350,11 @@
     }
 }
 
-char dcCANId = 1;                   // first byte of CANData, last byte states whether to toggle on (1) or off (0)
+char dc_CAN_id = 0;                   // first byte of CANData, last byte states whether to toggle on (1) or off (0)
 DigitalOut dcPin(p20);
+bool dc_on = false;
 
 void toggleDC_DC(bool toggle){
-    static bool dc_on = false;
     
     //dcPin turns on DC_DC converter when 0, off when 1
     
@@ -371,21 +375,36 @@
 
     while(1)
     {
-        if(rxBuffer.rxRead(rx_msg) && rx_msg.id == sysCANId){
-            if(rx_msg.data[0] == fanId){
+        if(rxBuffer.rxRead(rx_msg)){
+            char src_addr = (rx_msg.id & 0x0700) >> 8;      // get bits 10:8
+            
+            if(src_addr == sys_src_id){
+                char cont_id = (rx_msg.id & 0x00FF);        // get bits 7:0
                 
-                int pin_id = (int)rx_msg.data[6];
+                // only control fans of dc_dc converter is on
+                if(cont_id == fan_id && dc_on)
+                {
+                    char duty = rx_msg.data[1];
+                    if(duty > 100)
+                        break;
+                    
+                    char pin_id = rx_msg.data[0];
+                    if(pin_id > 3)
+                        break;
                 
-                if(pin_id < 4 && pin_id >= 0 && fan_Threads[pin_id] != NULL){
-                    fan_Threads[pin_id]->terminate();
-                    free(fan_Threads[pin_id]);
+                    if(fan_threads[pin_id] != NULL){
+                        fan_threads[pin_id]->terminate();
+                        free(fan_threads[pin_id]);   
+                    }
+                
+                    fan_threads[pin_id] = new Thread(rampFans, rx_msg.data);
                 }
                 
-                fan_Threads[pin_id] = new Thread(rampFans, rx_msg.data);
-            }
-            if(rx_msg.data[0] == dcCANId){
-                toggleDC_DC((bool)rx_msg.data[7]);   
-            }
-        }
-    }
+                if(cont_id == dc_CAN_id){
+                    toggleDC_DC(rx_msg.data[0]);
+                }
+                
+            } // check for correct src_addr
+        } // check CANBuffer
+    } // main while loop
 }