uses Rtostimer separate threads check for reset
Dependencies: TextLCD mbed-rtos mbed
main.cpp
00001 #include "mbed.h" 00002 #include "rtos.h" 00003 #include "TextLCD.h" 00004 00005 #define RUN 0x1 00006 00007 TextLCD lcd(p15, p16, p17, p18, p19, p20, TextLCD::LCD16x2); 00008 Serial pc (USBTX, USBRX); 00009 DigitalOut myled(LED1); 00010 00011 // mutexes 00012 Mutex mm_mutex; // protects mm[] 00013 Mutex ss_mutex; // protects ss[] 00014 Mutex MM_mutex; // protects MM[] 00015 Mutex input_mutex; // protects input 00016 00017 // global vars 00018 int mm [] = {0, 0}; // protected by mm_mutex 00019 int ss [] = {0, 0}; // protected by ss_mutex 00020 int MM [] = {0, 0}; // protected by MM_mutex 00021 int mm_wait = 10; // doesn't need mutex 00022 int ss_wait = 1000; // doesn't need mutex 00023 int MM_wait = 60000; // doesn't need mutex 00024 int run_status = 0; // doesn't need mutex 00025 char input = 'z'; // protected by input_mutex 00026 int need_reset_ss = 0; 00027 int need_reset_MM = 0; 00028 00029 // functions 00030 void update_display(void const *args); 00031 void update_mm(void const *args); 00032 void update_ss(void const *args); 00033 void update_MM(void const *args); 00034 void process_commands(void const *args); 00035 void ss_reset_func(void const *args); 00036 void MM_reset_func(void const *args); 00037 void disp0(); 00038 00039 // init timer threads 00040 RtosTimer disp_thread(update_display, osTimerPeriodic); 00041 RtosTimer mm_thread(update_mm, osTimerPeriodic); 00042 RtosTimer ss_thread(update_ss, osTimerPeriodic); 00043 RtosTimer MM_thread(update_MM, osTimerPeriodic); 00044 00045 // init threads 00046 Thread ss_reset_thread(ss_reset_func); 00047 Thread MM_reset_thread(MM_reset_func); 00048 Thread cmds(process_commands); // listen for keyboard inputs 00049 00050 int main() { 00051 00052 // set initial time to 00:00:00 00053 disp0(); 00054 00055 while (1) { 00056 00057 input_mutex.lock(); // lock input mutex 00058 00059 // start 00060 if (input == 's' && !run_status) { 00061 run_status = 1; 00062 disp_thread.start(10); 00063 mm_thread.start(mm_wait); 00064 ss_thread.start(ss_wait); 00065 MM_thread.start(MM_wait); 00066 } 00067 00068 // pause 00069 if (input == 'p' && run_status) { 00070 run_status = 0; 00071 disp_thread.stop(); 00072 mm_thread.stop(); 00073 ss_thread.stop(); 00074 MM_thread.stop(); 00075 ss_wait = ss_wait - 100*mm[1] - 10*mm[0]; 00076 MM_wait = MM_wait - 10000*ss[1] - 1000*ss[0] - 100*mm[1] - 10*mm[0]; 00077 need_reset_ss = 1; 00078 need_reset_MM = 1; 00079 } 00080 00081 // reset 00082 if (input == 'r' && !run_status) { 00083 for (int i=0;i<2;i++) { 00084 mm[i] = 0; 00085 ss[i] = 0; 00086 MM[i] = 0; 00087 } 00088 disp0(); 00089 ss_wait = 1000; 00090 MM_wait = 60000; 00091 need_reset_ss = 0; 00092 need_reset_MM = 0; 00093 } 00094 input_mutex.unlock(); // unlock input mutex 00095 cmds.signal_set(RUN); 00096 } 00097 } 00098 00099 void disp0() { 00100 lcd.printf("%d%d:%d%d:%d%d\n\n", MM[1], MM[0],ss[1],ss[0],mm[1],mm[0]); 00101 } 00102 00103 void process_commands(void const *args) { 00104 while (1) { 00105 input_mutex.lock(); 00106 input = pc.getc(); 00107 input_mutex.unlock(); 00108 Thread::signal_wait(RUN,osWaitForever); 00109 } 00110 } 00111 00112 void update_display(void const *args) { 00113 00114 mm_mutex.lock(); 00115 ss_mutex.lock(); 00116 MM_mutex.lock(); 00117 00118 lcd.printf("%d%d:%d%d:%d%d\n\n", MM[1], MM[0],ss[1],ss[0],mm[1],mm[0]); 00119 00120 MM_mutex.unlock(); 00121 ss_mutex.unlock(); 00122 mm_mutex.unlock(); 00123 00124 } 00125 00126 void update_mm(void const *args) { 00127 00128 mm_mutex.lock(); 00129 mm[0]++; 00130 if (mm[0] > 9) { 00131 mm[0] = 0; 00132 mm[1]++; 00133 } 00134 if (mm[1] > 9) { 00135 mm[0] = 0; 00136 mm[1] = 0; 00137 } 00138 mm_mutex.unlock(); 00139 00140 } 00141 00142 void update_ss(void const *args) { 00143 00144 ss_mutex.lock(); 00145 ss[0]++; 00146 if (ss[0] > 9) { 00147 ss[0] = 0; 00148 ss[1]++; 00149 } 00150 if (ss[1] > 5) { 00151 ss[0] = 0; 00152 ss[1] = 0; 00153 } 00154 ss_mutex.unlock(); 00155 if (need_reset_ss) ss_reset_thread.signal_set(RUN); 00156 } 00157 00158 void update_MM(void const *args) { 00159 00160 MM_mutex.lock(); 00161 MM[0]++; 00162 if (MM[0] > 9) { 00163 MM[0] = 0; 00164 MM[1]++; 00165 } 00166 if (MM[1] > 5) { 00167 MM[0] = 0; 00168 MM[1] = 0; 00169 } 00170 MM_mutex.unlock(); 00171 if (need_reset_MM) MM_reset_thread.signal_set(RUN); 00172 } 00173 00174 void ss_reset_func(void const *args) { 00175 Thread::signal_wait(RUN,osWaitForever); 00176 ss_thread.stop(); 00177 need_reset_ss = 0; 00178 ss_wait = 10000; 00179 ss_thread.start(ss_wait); 00180 } 00181 00182 void MM_reset_func(void const *args) { 00183 Thread::signal_wait(RUN,osWaitForever); 00184 MM_thread.stop(); 00185 need_reset_MM = 0; 00186 MM_wait = 60000; 00187 MM_thread.start(MM_wait); 00188 }
Generated on Sat Jul 30 2022 10:05:51 by
1.7.2