![](/media/cache/group/default_image.jpg.50x50_q85.jpg)
thread pointers
Dependencies: TextLCD mbed-rtos mbed
Fork of myStopwatch_threads by
main.cpp@2:9386c6f541bd, 2014-10-03 (annotated)
- Committer:
- jfields
- Date:
- Fri Oct 03 20:18:22 2014 +0000
- Revision:
- 2:9386c6f541bd
- Parent:
- 1:38e744366b97
pointer threads!
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
jfields | 0:c204b3ecd227 | 1 | #include "mbed.h" |
jfields | 0:c204b3ecd227 | 2 | #include "rtos.h" |
jfields | 0:c204b3ecd227 | 3 | #include "TextLCD.h" |
jfields | 2:9386c6f541bd | 4 | |
jfields | 0:c204b3ecd227 | 5 | TextLCD lcd(p15, p16, p17, p18, p19, p20, TextLCD::LCD16x2); |
jfields | 0:c204b3ecd227 | 6 | Serial pc (USBTX, USBRX); |
jfields | 0:c204b3ecd227 | 7 | DigitalOut myled(LED1); |
jfields | 0:c204b3ecd227 | 8 | |
jfields | 2:9386c6f541bd | 9 | |
jfields | 0:c204b3ecd227 | 10 | // global vars |
jfields | 0:c204b3ecd227 | 11 | int mm [] = {0, 0}; |
jfields | 0:c204b3ecd227 | 12 | int ss [] = {0, 0}; |
jfields | 0:c204b3ecd227 | 13 | int MM [] = {0, 0}; |
jfields | 2:9386c6f541bd | 14 | |
jfields | 0:c204b3ecd227 | 15 | // functions |
jfields | 0:c204b3ecd227 | 16 | void update_display(void const *args); |
jfields | 0:c204b3ecd227 | 17 | void update_mm(void const *args); |
jfields | 0:c204b3ecd227 | 18 | void update_ss(void const *args); |
jfields | 0:c204b3ecd227 | 19 | void update_MM(void const *args); |
jfields | 2:9386c6f541bd | 20 | |
jfields | 0:c204b3ecd227 | 21 | int main() { |
jfields | 0:c204b3ecd227 | 22 | |
jfields | 0:c204b3ecd227 | 23 | Thread thread_display(update_display); |
jfields | 2:9386c6f541bd | 24 | Thread * mmptr; |
jfields | 2:9386c6f541bd | 25 | Thread * ssptr; |
jfields | 2:9386c6f541bd | 26 | Thread * MMptr; |
jfields | 2:9386c6f541bd | 27 | |
jfields | 2:9386c6f541bd | 28 | char input = 'z'; |
jfields | 2:9386c6f541bd | 29 | int run_status = 0; |
jfields | 0:c204b3ecd227 | 30 | |
jfields | 2:9386c6f541bd | 31 | while(1) { |
jfields | 2:9386c6f541bd | 32 | |
jfields | 2:9386c6f541bd | 33 | if ( input == 's') { |
jfields | 2:9386c6f541bd | 34 | if (run_status==0) { |
jfields | 2:9386c6f541bd | 35 | mmptr = new Thread(update_mm); |
jfields | 2:9386c6f541bd | 36 | ssptr = new Thread(update_ss); |
jfields | 2:9386c6f541bd | 37 | MMptr = new Thread(update_MM); |
jfields | 2:9386c6f541bd | 38 | run_status = 1; |
jfields | 2:9386c6f541bd | 39 | } |
jfields | 2:9386c6f541bd | 40 | } |
jfields | 2:9386c6f541bd | 41 | if ( input == 'p') { |
jfields | 2:9386c6f541bd | 42 | if (run_status==1) { |
jfields | 2:9386c6f541bd | 43 | delete mmptr; |
jfields | 2:9386c6f541bd | 44 | delete ssptr; |
jfields | 2:9386c6f541bd | 45 | delete MMptr; |
jfields | 2:9386c6f541bd | 46 | run_status = 0; |
jfields | 2:9386c6f541bd | 47 | } |
jfields | 2:9386c6f541bd | 48 | } |
jfields | 2:9386c6f541bd | 49 | if ( input == 'r') { |
jfields | 2:9386c6f541bd | 50 | if (run_status == 0) { |
jfields | 2:9386c6f541bd | 51 | mm[0] = mm[1] = 0; |
jfields | 2:9386c6f541bd | 52 | ss[0] = ss[1] = 0; |
jfields | 2:9386c6f541bd | 53 | MM[0] = MM[1] = 0; |
jfields | 2:9386c6f541bd | 54 | } |
jfields | 2:9386c6f541bd | 55 | } |
jfields | 2:9386c6f541bd | 56 | |
jfields | 2:9386c6f541bd | 57 | input = pc.getc(); |
jfields | 0:c204b3ecd227 | 58 | |
jfields | 0:c204b3ecd227 | 59 | } |
jfields | 2:9386c6f541bd | 60 | |
jfields | 0:c204b3ecd227 | 61 | } |
jfields | 2:9386c6f541bd | 62 | |
jfields | 0:c204b3ecd227 | 63 | void update_display(void const *args) { |
jfields | 0:c204b3ecd227 | 64 | while (1) { |
jfields | 0:c204b3ecd227 | 65 | lcd.printf("%d%d:%d%d:%d%d\n\n", MM[1], MM[0],ss[1],ss[0],mm[1],mm[0]); |
jfields | 1:38e744366b97 | 66 | Thread::wait(10); |
jfields | 0:c204b3ecd227 | 67 | } |
jfields | 0:c204b3ecd227 | 68 | } |
jfields | 2:9386c6f541bd | 69 | |
jfields | 0:c204b3ecd227 | 70 | void update_mm(void const *args) { |
jfields | 1:38e744366b97 | 71 | while (1) { |
jfields | 1:38e744366b97 | 72 | Thread::wait(10); |
jfields | 1:38e744366b97 | 73 | mm[0]++; |
jfields | 1:38e744366b97 | 74 | if (mm[0] >= 10) { |
jfields | 1:38e744366b97 | 75 | mm[0] = 0; |
jfields | 1:38e744366b97 | 76 | mm[1]++; |
jfields | 1:38e744366b97 | 77 | } |
jfields | 1:38e744366b97 | 78 | if (mm[1] >= 10) { |
jfields | 1:38e744366b97 | 79 | mm[0] = 0; |
jfields | 1:38e744366b97 | 80 | mm[1] = 0; |
jfields | 1:38e744366b97 | 81 | } |
jfields | 0:c204b3ecd227 | 82 | } |
jfields | 0:c204b3ecd227 | 83 | } |
jfields | 2:9386c6f541bd | 84 | |
jfields | 0:c204b3ecd227 | 85 | void update_ss(void const *args) { |
jfields | 1:38e744366b97 | 86 | while (1) { |
jfields | 1:38e744366b97 | 87 | Thread::wait(1000); |
jfields | 1:38e744366b97 | 88 | ss[0]++; |
jfields | 1:38e744366b97 | 89 | if (ss[0] >= 10) { |
jfields | 1:38e744366b97 | 90 | ss[0] = 0; |
jfields | 1:38e744366b97 | 91 | ss[1]++; |
jfields | 1:38e744366b97 | 92 | } |
jfields | 1:38e744366b97 | 93 | if (ss[1] >= 6) { |
jfields | 1:38e744366b97 | 94 | ss[0] = 0; |
jfields | 1:38e744366b97 | 95 | ss[1] = 0; |
jfields | 1:38e744366b97 | 96 | } |
jfields | 0:c204b3ecd227 | 97 | } |
jfields | 0:c204b3ecd227 | 98 | } |
jfields | 2:9386c6f541bd | 99 | |
jfields | 0:c204b3ecd227 | 100 | void update_MM(void const *args) { |
jfields | 1:38e744366b97 | 101 | while (1) { |
jfields | 1:38e744366b97 | 102 | Thread::wait(60000); |
jfields | 0:c204b3ecd227 | 103 | MM[0]++; |
jfields | 1:38e744366b97 | 104 | if (MM[0] >= 10) { |
jfields | 1:38e744366b97 | 105 | MM[0] = 0; |
jfields | 1:38e744366b97 | 106 | MM[1]++; |
jfields | 1:38e744366b97 | 107 | } |
jfields | 1:38e744366b97 | 108 | if (MM[1] >= 6) { |
jfields | 1:38e744366b97 | 109 | MM[0] = 0; |
jfields | 1:38e744366b97 | 110 | MM[1] = 0; |
jfields | 1:38e744366b97 | 111 | } |
jfields | 0:c204b3ecd227 | 112 | } |
jfields | 0:c204b3ecd227 | 113 | } |
jfields | 2:9386c6f541bd | 114 |