thread pointers

Dependencies:   TextLCD mbed-rtos mbed

Fork of myStopwatch_threads by CIS541 Xueli Jon

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 TextLCD lcd(p15, p16, p17, p18, p19, p20, TextLCD::LCD16x2);
00006 Serial pc (USBTX, USBRX);
00007 DigitalOut myled(LED1);
00008  
00009  
00010 // global vars 
00011 int mm [] = {0, 0};
00012 int ss [] = {0, 0};
00013 int MM [] = {0, 0};
00014  
00015 // functions
00016 void update_display(void const *args);
00017 void update_mm(void const *args);
00018 void update_ss(void const *args);
00019 void update_MM(void const *args);
00020  
00021 int main() {
00022     
00023     Thread thread_display(update_display);
00024     Thread * mmptr;
00025     Thread * ssptr; 
00026     Thread * MMptr;    
00027  
00028     char input = 'z';
00029     int run_status = 0;
00030     
00031     while(1) {
00032         
00033         if ( input == 's') { 
00034             if (run_status==0) {
00035                 mmptr = new Thread(update_mm);
00036                 ssptr = new Thread(update_ss);
00037                 MMptr = new Thread(update_MM);
00038                 run_status = 1;
00039             }
00040         }
00041         if ( input == 'p') {
00042             if (run_status==1) {
00043                 delete mmptr;
00044                 delete ssptr;
00045                 delete MMptr;
00046                 run_status = 0;
00047             }
00048         }
00049         if ( input == 'r') {
00050             if (run_status == 0) {
00051                 mm[0] = mm[1] = 0;
00052                 ss[0] = ss[1] = 0;
00053                 MM[0] = MM[1] = 0;
00054             }
00055         }
00056         
00057         input = pc.getc();
00058 
00059     }
00060     
00061 }
00062  
00063 void update_display(void const *args) {
00064     while (1) {
00065         lcd.printf("%d%d:%d%d:%d%d\n\n", MM[1], MM[0],ss[1],ss[0],mm[1],mm[0]);
00066         Thread::wait(10);
00067     }
00068 }
00069  
00070 void update_mm(void const *args) {
00071     while (1) {
00072         Thread::wait(10);
00073         mm[0]++;
00074         if (mm[0] >= 10) {
00075             mm[0] = 0;
00076             mm[1]++;
00077         }
00078         if (mm[1] >= 10) {
00079             mm[0] = 0;
00080             mm[1] = 0;
00081         }
00082     }
00083 }
00084  
00085 void update_ss(void const *args) {
00086     while (1) {
00087         Thread::wait(1000);
00088         ss[0]++;
00089         if (ss[0] >= 10) {
00090             ss[0] = 0;
00091             ss[1]++;
00092         }
00093         if (ss[1] >= 6) {
00094             ss[0] = 0;
00095             ss[1] = 0;
00096         }
00097     }
00098 }
00099  
00100 void update_MM(void const *args) {
00101     while (1) {
00102         Thread::wait(60000);
00103         MM[0]++;
00104         if (MM[0] >= 10) {
00105             MM[0] = 0;
00106             MM[1]++;
00107         }
00108         if (MM[1] >= 6) {
00109             MM[0] = 0;
00110             MM[1] = 0;
00111         }
00112     }
00113 }
00114