uses to rtos to send run signals to update threads. there are 5 rtos timers - 2 single shots and 3 periodic.

Dependencies:   TextLCD mbed-rtos mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

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 int first_run_ss = 1, first_run_MM = 1;
00026 char input = 'z';           // protected by input_mutex
00027 
00028 // functions
00029 void manage_signals(void const *i);
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 
00036 // create RtosTimer manager 
00037 RtosTimer * disp_signal;
00038 RtosTimer * mm_signal;
00039 RtosTimer * ss_init;
00040 RtosTimer * MM_init; 
00041 RtosTimer * ss_signal;
00042 RtosTimer * MM_signal;
00043 
00044 // create thread pointers
00045 Thread * mm_thread;
00046 Thread * ss_thread;
00047 Thread * MM_thread;
00048 Thread * disp_thread;
00049 
00050 int main() {
00051     
00052     // init threads
00053     mm_thread = new Thread(update_mm);                // update mm[]
00054     ss_thread = new Thread(update_ss);                // update ss[]
00055     MM_thread = new Thread(update_MM);                // update MM[]
00056     disp_thread = new Thread(update_display);         // display LCD
00057 
00058     // rtos manager
00059     disp_signal = new RtosTimer(manage_signals, osTimerPeriodic, (void *)0);
00060     mm_signal = new RtosTimer(manage_signals, osTimerPeriodic, (void *)1);
00061     ss_init = new RtosTimer(manage_signals, osTimerOnce, (void *)2);
00062     MM_init = new RtosTimer(manage_signals, osTimerOnce, (void *)3);
00063     ss_signal = new RtosTimer(manage_signals, osTimerPeriodic, (void *)12);
00064     MM_signal = new RtosTimer(manage_signals, osTimerPeriodic, (void *)13);
00065     
00066     // set initial time to 00:00:00
00067     disp_thread->signal_set(RUN);
00068 
00069     // listen for commands
00070     Thread cmds(process_commands);                    
00071         
00072     while (1) {
00073 
00074         input_mutex.lock(); // lock input mutex
00075         
00076         // start
00077         if (input == 's' && !run_status) {            
00078             disp_signal->start(10);
00079             mm_signal->start(mm_wait);
00080             ss_init->start(ss_wait);
00081             MM_init->start(MM_wait);
00082             first_run_ss = 1;
00083             first_run_MM = 1;
00084             run_status = 1;
00085         }
00086         
00087         // pause
00088         if (input == 'p' && run_status) {
00089             run_status = 0;
00090             ss_wait = 1000 - 100*mm[1] - 10*mm[0];
00091             MM_wait = 60000 - 10000*ss[1] - 1000*ss[0] - 100*mm[1] - 10*mm[0];
00092             disp_signal->stop();
00093             mm_signal->stop();
00094             ss_init->stop();
00095             MM_init->stop();
00096             ss_signal->stop();
00097             MM_signal->stop();
00098         }
00099         
00100         // reset
00101         if (input == 'r' && !run_status) {
00102             for (int i=0;i<2;i++) {
00103                 mm[i] = 0;
00104                 ss[i] = 0;
00105                 MM[i] = 0;
00106             }
00107             disp_thread->signal_set(RUN);
00108             ss_wait = 1000;
00109             MM_wait = 60000;
00110         }
00111         input_mutex.unlock(); // unlock input mutex
00112         cmds.signal_set(RUN);
00113     }
00114 }
00115 
00116 void process_commands(void const *args) {
00117     while (1) {
00118         input_mutex.lock();
00119         input = pc.getc();
00120         input_mutex.unlock();
00121         Thread::signal_wait(RUN,osWaitForever);
00122     }
00123 }
00124 
00125 void update_display(void const *args) {
00126     while (1) {
00127         Thread::signal_wait(RUN,osWaitForever);    
00128         mm_mutex.lock();
00129         ss_mutex.lock();
00130         MM_mutex.lock();
00131         lcd.printf("%d%d:%d%d:%d%d\n\n", MM[1], MM[0],ss[1],ss[0],mm[1],mm[0]);
00132         MM_mutex.unlock();
00133         ss_mutex.unlock();
00134         mm_mutex.unlock();
00135     }
00136 }
00137 
00138 void update_mm(void const *args) {
00139     while (1) {
00140         Thread::signal_wait(RUN,osWaitForever);    
00141         mm_mutex.lock();
00142         mm[0]++;
00143         if (mm[0] > 9) {
00144             mm[0] = 0;
00145             mm[1]++;
00146         }
00147         if (mm[1] > 9) {
00148             mm[0] = 0;
00149             mm[1] = 0;
00150         }
00151         mm_mutex.unlock();
00152     }
00153 }
00154 
00155 void update_ss(void const *args) {
00156     while (1) {
00157         Thread::signal_wait(RUN,osWaitForever);
00158         if (first_run_ss) {
00159             ss_wait = 1000;
00160             ss_signal->start(ss_wait);
00161             first_run_ss = 0;
00162         }
00163         ss_mutex.lock();
00164         ss[0]++;
00165         if (ss[0] > 9) {
00166             ss[0] = 0;
00167             ss[1]++;
00168         }
00169         if (ss[1] > 5) {
00170             ss[0] = 0;
00171             ss[1] = 0;
00172         }
00173         ss_mutex.unlock();
00174     }
00175 }
00176 
00177 void update_MM(void const *args) {
00178     while (1) {
00179         Thread::signal_wait(RUN,osWaitForever);
00180         if (first_run_MM) {
00181             MM_wait = 60000;
00182             MM_signal->start(MM_wait);
00183             first_run_MM = 0;
00184         }
00185         MM_mutex.lock();
00186         MM[0]++;
00187         if (MM[0] > 9) {
00188             MM[0] = 0;
00189             MM[1]++;
00190         }
00191         if (MM[1] > 5) {
00192             MM[0] = 0;
00193             MM[1] = 0;
00194         }
00195         MM_mutex.unlock();
00196     }
00197 }
00198 
00199 void manage_signals(void const *i) {
00200     if (!(int)i) disp_thread->signal_set(RUN);
00201     if ((int)i==1) mm_thread->signal_set(RUN);
00202     if ((int)i%10==2) ss_thread->signal_set(RUN);
00203     if ((int)i%10==3) MM_thread->signal_set(RUN);
00204 }