GIU\ZF
Dependencies: MCP23017 WattBob_TextLCD mbed-rtos mbed
Fork of rtos_basic by
tasks/task_group1.cpp@16:0ada6cbd41e2, 2018-03-27 (annotated)
- Committer:
- ihexx
- Date:
- Tue Mar 27 22:03:07 2018 +0000
- Revision:
- 16:0ada6cbd41e2
- Parent:
- 15:524de2b2ef8e
- Child:
- 17:a29ce6fc667c
completed task merging of carSim, getControls, and engine switch;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
ihexx | 13:ab52f46c98ab | 1 | #include "core.h" |
ihexx | 15:524de2b2ef8e | 2 | namespace display{ |
ihexx | 15:524de2b2ef8e | 3 | //Display on MBED text display • odometer value • average speed |
ihexx | 15:524de2b2ef8e | 4 | const float freq = 2.0f; //hz |
ihexx | 15:524de2b2ef8e | 5 | |
ihexx | 15:524de2b2ef8e | 6 | //I/O |
ihexx | 15:524de2b2ef8e | 7 | MCP23017 *port; |
ihexx | 15:524de2b2ef8e | 8 | WattBob_TextLCD *lcd; |
ihexx | 15:524de2b2ef8e | 9 | |
ihexx | 15:524de2b2ef8e | 10 | void init(){ |
ihexx | 15:524de2b2ef8e | 11 | port = new MCP23017(p9, p10, 0x40); |
ihexx | 15:524de2b2ef8e | 12 | lcd = new WattBob_TextLCD(port); |
ihexx | 15:524de2b2ef8e | 13 | port->write_bit(1,BL_BIT); // LCD backlight on. |
ihexx | 15:524de2b2ef8e | 14 | lcd->cls(); |
ihexx | 15:524de2b2ef8e | 15 | } |
ihexx | 15:524de2b2ef8e | 16 | static inline void hotLoop(){ |
ihexx | 15:524de2b2ef8e | 17 | lcd->cls(); |
ihexx | 15:524de2b2ef8e | 18 | |
ihexx | 15:524de2b2ef8e | 19 | runTimeParams::liveAccess.lock(); |
ihexx | 15:524de2b2ef8e | 20 | float odometer = runTimeParams::odometer; |
ihexx | 15:524de2b2ef8e | 21 | float avgSpeed = runTimeParams::avgSpeed; |
ihexx | 15:524de2b2ef8e | 22 | runTimeParams::liveAccess.unlock(); |
ihexx | 15:524de2b2ef8e | 23 | |
ihexx | 15:524de2b2ef8e | 24 | lcd->locate(0,0); //located col, row. |
ihexx | 15:524de2b2ef8e | 25 | lcd->printf("Odo=%.2f",odometer); |
ihexx | 15:524de2b2ef8e | 26 | |
ihexx | 15:524de2b2ef8e | 27 | lcd->locate(1,0); //located col, row. |
ihexx | 15:524de2b2ef8e | 28 | lcd->printf("Speed=%.2f",avgSpeed); |
ihexx | 15:524de2b2ef8e | 29 | } |
ihexx | 15:524de2b2ef8e | 30 | } |
ihexx | 13:ab52f46c98ab | 31 | |
ihexx | 13:ab52f46c98ab | 32 | namespace task_group_1{ |
ihexx | 13:ab52f46c98ab | 33 | Thread thread; |
ihexx | 13:ab52f46c98ab | 34 | const float freq = 2.0f; //hz |
ihexx | 13:ab52f46c98ab | 35 | |
ihexx | 13:ab52f46c98ab | 36 | |
ihexx | 13:ab52f46c98ab | 37 | void runTask(){ |
ihexx | 13:ab52f46c98ab | 38 | Timer executionTimer,sleepTimer; |
ihexx | 13:ab52f46c98ab | 39 | executionTimer.reset(); |
ihexx | 13:ab52f46c98ab | 40 | sleepTimer.reset(); |
ihexx | 13:ab52f46c98ab | 41 | display::init(); |
ihexx | 13:ab52f46c98ab | 42 | |
ihexx | 13:ab52f46c98ab | 43 | const int const_delay = int((1000.0f/freq)+0.5f); |
ihexx | 13:ab52f46c98ab | 44 | int dynamic_delay = const_delay; |
ihexx | 14:8a6c20435523 | 45 | int tick = 0; |
ihexx | 13:ab52f46c98ab | 46 | while(true){ |
ihexx | 13:ab52f46c98ab | 47 | sleepTimer.stop(); |
ihexx | 13:ab52f46c98ab | 48 | executionTimer.start(); |
ihexx | 13:ab52f46c98ab | 49 | |
ihexx | 13:ab52f46c98ab | 50 | |
ihexx | 13:ab52f46c98ab | 51 | int sleepTime = sleepTimer.read_ms(); |
ihexx | 13:ab52f46c98ab | 52 | const int drift = ((sleepTime - dynamic_delay) > 0)? |
ihexx | 13:ab52f46c98ab | 53 | (sleepTime - dynamic_delay) : 0; |
ihexx | 13:ab52f46c98ab | 54 | |
ihexx | 13:ab52f46c98ab | 55 | |
ihexx | 13:ab52f46c98ab | 56 | // Run all tasks |
ihexx | 16:0ada6cbd41e2 | 57 | |
ihexx | 16:0ada6cbd41e2 | 58 | display::hotLoop(); |
ihexx | 16:0ada6cbd41e2 | 59 | |
ihexx | 13:ab52f46c98ab | 60 | |
ihexx | 13:ab52f46c98ab | 61 | |
ihexx | 14:8a6c20435523 | 62 | tick++; |
ihexx | 13:ab52f46c98ab | 63 | executionTimer.stop(); |
ihexx | 13:ab52f46c98ab | 64 | int exec_time = executionTimer.read_ms(); |
ihexx | 13:ab52f46c98ab | 65 | |
ihexx | 13:ab52f46c98ab | 66 | #if DEBUG_MODE |
ihexx | 15:524de2b2ef8e | 67 | static const int tick_interval_debug_log = int((freq/dequeueMail::freq)+0.5f); |
ihexx | 15:524de2b2ef8e | 68 | if (!(tick%tick_interval_debug_log)){ |
ihexx | 14:8a6c20435523 | 69 | runTimeParams::liveAccess.lock(); |
ihexx | 14:8a6c20435523 | 70 | runTimeParams::debugLog += "task_group_1," + to_string(exec_time) + "," |
ihexx | 14:8a6c20435523 | 71 | + to_string(sleepTime) + "," |
ihexx | 14:8a6c20435523 | 72 | + to_string(drift) + "\n\r"; |
ihexx | 14:8a6c20435523 | 73 | runTimeParams::liveAccess.unlock(); |
ihexx | 14:8a6c20435523 | 74 | } |
ihexx | 15:524de2b2ef8e | 75 | if (tick==tick_interval_debug_log*1) tick=0; |
ihexx | 13:ab52f46c98ab | 76 | #endif |
ihexx | 13:ab52f46c98ab | 77 | |
ihexx | 13:ab52f46c98ab | 78 | executionTimer.reset(); |
ihexx | 13:ab52f46c98ab | 79 | sleepTimer.reset(); |
ihexx | 13:ab52f46c98ab | 80 | sleepTimer.start(); |
ihexx | 13:ab52f46c98ab | 81 | dynamic_delay = const_delay - (exec_time + drift); |
ihexx | 13:ab52f46c98ab | 82 | Thread::wait(dynamic_delay); |
ihexx | 13:ab52f46c98ab | 83 | } |
ihexx | 13:ab52f46c98ab | 84 | } |
ihexx | 13:ab52f46c98ab | 85 | } |