GIU\ZF

Dependencies:   MCP23017 WattBob_TextLCD mbed-rtos mbed

Fork of rtos_basic by mbed official

Revision:
17:a29ce6fc667c
Parent:
16:0ada6cbd41e2
Child:
19:2044bb5d7f29
--- a/tasks/task_group1.cpp	Tue Mar 27 22:03:07 2018 +0000
+++ b/tasks/task_group1.cpp	Wed Mar 28 00:26:55 2018 +0000
@@ -28,6 +28,78 @@
         lcd->printf("Speed=%.2f",avgSpeed);
     }
 }
+namespace brakeIndicator{
+    //Show use of the brake on a LED on the RedBox Unit
+    const float freq = 2.0f; //hz
+    PwmOut led2(PORT_REDBOX_LED1);
+    static inline void init(){
+        led2.period_ms(50);
+    }
+    static inline void hotLoop(){
+            runTimeParams::liveAccess.lock();
+            led2.write(runTimeParams::brakeForce);
+            runTimeParams::liveAccess.unlock();
+
+    }
+}
+namespace speedIndicator{
+    //Monitor speed and if it goes over 88 mph switch on a LED on
+        //the RedBox unit
+    static const float freq = 1; //hz
+    DigitalOut led(PORT_REDBOX_LED1);
+    static inline void init(){
+        led = 0;
+    }
+    static inline void hotLoop(){
+            runTimeParams::liveAccess.lock();
+            led = (runTimeParams::avgSpeed>88);
+            runTimeParams::liveAccess.unlock();
+            
+    }
+}
+namespace sideLights{
+    //Read a single side light switch and set side lights accordingly
+    static const float freq = 1; //hz
+    DigitalIn lightSwitch(PORT_BRAKE);
+    DigitalOut led(PORT_SIDE_LIGHTS);
+    static inline void hotLoop(){
+        led = lightSwitch;
+        }
+        
+    }
+namespace turnSignal{
+    //Read the two turn indicator switches and flash appropriate
+        //indicator LEDs at a rate of 1Hz. If both switches are switched on
+        //then flash both indicator LEDs at a rate of 2Hz (hazard mode).
+    static const float freq = 0.5; //hz
+    
+    //I/O
+    DigitalIn lSwitch(PORT_TURN_SIGNAL_SWITCH_LEFT);
+    DigitalIn rSwitch(PORT_TURN_SIGNAL_SWITCH_RIGHT);
+    
+    PwmOut lLed(PORT_TURN_SIGNAL_LED_LEFT);
+    PwmOut rLed(PORT_TURN_SIGNAL_LED_RIGHT);
+    
+    static inline void hotLoop(){
+        int a = lSwitch.read();
+        int b = rSwitch.read();
+        if(a&&b){
+             lLed.period(2.0f);
+             rLed.period(2.0f);
+        }
+        else{
+            lLed.period(1.0f);
+            rLed.period(1.0f);
+        }
+        a ? lLed.write(0.5f) : lLed.write(0.0f);
+        b ? rLed.write(0.5f) : rLed.write(0.0f);
+    }
+    
+}
+
+
+
+
 
 namespace task_group_1{
     Thread thread;
@@ -38,16 +110,19 @@
         Timer executionTimer,sleepTimer;
         executionTimer.reset();
         sleepTimer.reset();
+        
         display::init();
+        brakeIndicator::init();
+        speedIndicator::init();
         
         const int const_delay = int((1000.0f/freq)+0.5f);
         int dynamic_delay = const_delay;
         int tick = 0;
+        int max_exec_time = 0;
+        
         while(true){
             sleepTimer.stop();
             executionTimer.start();
-            
-            
             int sleepTime = sleepTimer.read_ms();
             const int drift = ((sleepTime - dynamic_delay) > 0)?
                                         (sleepTime - dynamic_delay) : 0;
@@ -55,26 +130,45 @@
             
             // Run all tasks
            
-            display::hotLoop();
+            brakeIndicator::hotLoop();
             
+            static const int tick_interval_sIndicator = int((freq/speedIndicator::freq)+0.5f);
+            if (!(tick%tick_interval_sIndicator)){
+                speedIndicator::hotLoop();
+                sideLights::hotLoop();
+            }
+            static const int tick_interval_tSignal = int((freq/turnSignal::freq)+0.5f);
+            if (!(tick%tick_interval_tSignal)){
+                turnSignal::hotLoop();
+            }
             
+            display::hotLoop();
             
             tick++;
             executionTimer.stop();
             int exec_time = executionTimer.read_ms();
+            if (exec_time > max_exec_time) max_exec_time=exec_time;
             
             #if DEBUG_MODE
             static const int tick_interval_debug_log = int((freq/dequeueMail::freq)+0.5f);
             if (!(tick%tick_interval_debug_log)){
-                runTimeParams::liveAccess.lock();
-                runTimeParams::debugLog += "task_group_1," + to_string(exec_time) + ","
+                runTimeParams::debugAccess.lock();
+                *runTimeParams::debugLog += "task_group_1," + to_string(max_exec_time) + ","
                             + to_string(sleepTime) + ","
                             + to_string(drift) + "\n\r";
-                runTimeParams::liveAccess.unlock();
+                runTimeParams::debugAccess.unlock();
             }
-            if (tick==tick_interval_debug_log*1) tick=0;
+            #else
+            static const int tick_interval_debug_log = 1;
             #endif
             
+            static const int tick_LCM = 
+                tick_interval_debug_log*
+                tick_interval_sIndicator*
+                tick_interval_tSignal;
+                
+            if (tick==tick_LCM) tick=0;
+            
             executionTimer.reset();
             sleepTimer.reset();
             sleepTimer.start();