GIU\ZF

Dependencies:   MCP23017 WattBob_TextLCD mbed-rtos mbed

Fork of rtos_basic by mbed official

tasks/task_group1.cpp

Committer:
ihexx
Date:
2018-03-27
Revision:
15:524de2b2ef8e
Parent:
14:8a6c20435523
Child:
16:0ada6cbd41e2

File content as of revision 15:524de2b2ef8e:

#include "core.h"
namespace display{
    //Display on MBED text display • odometer value • average speed
    const float freq = 2.0f; //hz
    
    //I/O
    MCP23017 *port;
    WattBob_TextLCD *lcd;
    
    void init(){
        port = new MCP23017(p9, p10, 0x40); 
        lcd = new WattBob_TextLCD(port);
        port->write_bit(1,BL_BIT); // LCD backlight on.
        lcd->cls();
    }
    static inline void hotLoop(){
        lcd->cls();
        
        runTimeParams::liveAccess.lock();
        float odometer = runTimeParams::odometer;
        float avgSpeed = runTimeParams::avgSpeed;
        runTimeParams::liveAccess.unlock();
        
        lcd->locate(0,0);   //located col, row.
        lcd->printf("Odo=%.2f",odometer);
        
        lcd->locate(1,0);   //located col, row.
        lcd->printf("Speed=%.2f",avgSpeed);
    }
}

namespace task_group_1{
    Thread thread;
    const float freq = 2.0f; //hz
    

    void runTask(){
        Timer executionTimer,sleepTimer;
        executionTimer.reset();
        sleepTimer.reset();
        display::init();
        
        const int const_delay = int((1000.0f/freq)+0.5f);
        int dynamic_delay = const_delay;
        int tick = 0;
        while(true){
            sleepTimer.stop();
            executionTimer.start();
            
            
            int sleepTime = sleepTimer.read_ms();
            const int drift = ((sleepTime - dynamic_delay) > 0)?
                                        (sleepTime - dynamic_delay) : 0;
            
            
            // Run all tasks
            static const int tick_interval_display = int((freq/display::freq)+0.5f);
            if (!(tick%tick_interval_display )) display::hotLoop();
            
            
            tick++;
            executionTimer.stop();
            int exec_time = executionTimer.read_ms();
            
            #if DEBUG_MODE
            runTimeParams::liveAccess.lock();
            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) + ","
                            + to_string(sleepTime) + ","
                            + to_string(drift) + "\n\r";
                runTimeParams::liveAccess.unlock();
                
            }
            if (tick==tick_interval_debug_log*1) tick=0;
            runTimeParams::liveAccess.unlock();
            #endif
            
            executionTimer.reset();
            sleepTimer.reset();
            sleepTimer.start();
            dynamic_delay = const_delay - (exec_time + drift);
            Thread::wait(dynamic_delay);
        }   
    }
}