Dr. Pucher
/
8ABELI_LCD_Thread3
Thread Sync Example
main.cpp@1:a477e0d640e0, 2017-03-22 (annotated)
- Committer:
- fpucher
- Date:
- Wed Mar 22 15:31:42 2017 +0000
- Revision:
- 1:a477e0d640e0
- Parent:
- 0:567c690c073a
- Child:
- 2:29dca06b0ee0
Different thread functions added
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
fpucher | 0:567c690c073a | 1 | // see: [[RTOS: Demonstration Setup]] |
fpucher | 0:567c690c073a | 2 | // Trace ab 15:00 |
fpucher | 0:567c690c073a | 3 | |
fpucher | 0:567c690c073a | 4 | #include "mbed.h" |
fpucher | 0:567c690c073a | 5 | |
fpucher | 0:567c690c073a | 6 | class Rgb |
fpucher | 0:567c690c073a | 7 | { |
fpucher | 0:567c690c073a | 8 | private: |
fpucher | 0:567c690c073a | 9 | DigitalOut _led; |
fpucher | 0:567c690c073a | 10 | |
fpucher | 0:567c690c073a | 11 | public: |
fpucher | 1:a477e0d640e0 | 12 | Rgb(PinName ld) : _led(ld) { _led = 1;}; // Constructor |
fpucher | 0:567c690c073a | 13 | |
fpucher | 0:567c690c073a | 14 | void LedOn() { |
fpucher | 0:567c690c073a | 15 | _led = 0; |
fpucher | 0:567c690c073a | 16 | } |
fpucher | 0:567c690c073a | 17 | void LedOff(){ |
fpucher | 0:567c690c073a | 18 | _led.write(1); |
fpucher | 0:567c690c073a | 19 | } |
fpucher | 0:567c690c073a | 20 | }; |
fpucher | 0:567c690c073a | 21 | |
fpucher | 0:567c690c073a | 22 | class HasA |
fpucher | 0:567c690c073a | 23 | { |
fpucher | 0:567c690c073a | 24 | private: |
fpucher | 0:567c690c073a | 25 | DigitalOut _led; |
fpucher | 0:567c690c073a | 26 | |
fpucher | 0:567c690c073a | 27 | public: |
fpucher | 0:567c690c073a | 28 | HasA(PinName ld) : _led(ld) {}; // Constructor |
fpucher | 0:567c690c073a | 29 | |
fpucher | 0:567c690c073a | 30 | void LedOn() { |
fpucher | 0:567c690c073a | 31 | _led = 1; |
fpucher | 0:567c690c073a | 32 | } |
fpucher | 0:567c690c073a | 33 | void LedOff(){ |
fpucher | 0:567c690c073a | 34 | _led.write(0); |
fpucher | 0:567c690c073a | 35 | } |
fpucher | 0:567c690c073a | 36 | }; |
fpucher | 0:567c690c073a | 37 | |
fpucher | 0:567c690c073a | 38 | void Delay_Nonsense(uint32_t * DelayCounter, uint32_t const * TargetCount) |
fpucher | 0:567c690c073a | 39 | { |
fpucher | 0:567c690c073a | 40 | while(*DelayCounter <= *TargetCount) |
fpucher | 0:567c690c073a | 41 | { |
fpucher | 0:567c690c073a | 42 | *DelayCounter = *DelayCounter + 1; |
fpucher | 0:567c690c073a | 43 | } |
fpucher | 0:567c690c073a | 44 | |
fpucher | 0:567c690c073a | 45 | *DelayCounter = 0; |
fpucher | 0:567c690c073a | 46 | } |
fpucher | 0:567c690c073a | 47 | |
fpucher | 0:567c690c073a | 48 | //M3 r (p23); g (p24); b (p25); |
fpucher | 0:567c690c073a | 49 | /* |
fpucher | 0:567c690c073a | 50 | HasA Led1(LED1); |
fpucher | 0:567c690c073a | 51 | HasA Led2(LED2); |
fpucher | 0:567c690c073a | 52 | HasA Led3(LED3); |
fpucher | 0:567c690c073a | 53 | */ |
fpucher | 0:567c690c073a | 54 | Rgb Led1(p23); |
fpucher | 0:567c690c073a | 55 | Rgb Led2(p24); |
fpucher | 0:567c690c073a | 56 | Rgb Led3(p25); |
fpucher | 0:567c690c073a | 57 | |
fpucher | 0:567c690c073a | 58 | //void Led1_Blink(void *pvParameters) |
fpucher | 0:567c690c073a | 59 | void Led1_Blink() // red long |
fpucher | 0:567c690c073a | 60 | { |
fpucher | 0:567c690c073a | 61 | const int xDelay = 500; |
fpucher | 0:567c690c073a | 62 | uint32_t BlueDelay = 0; |
fpucher | 0:567c690c073a | 63 | const uint32_t TargetCount = 16000; |
fpucher | 0:567c690c073a | 64 | |
fpucher | 0:567c690c073a | 65 | for(;;) { |
fpucher | 0:567c690c073a | 66 | for(int i = 0; i < 10; i++) { // randomnes |
fpucher | 0:567c690c073a | 67 | wait_ms(xDelay); |
fpucher | 0:567c690c073a | 68 | } |
fpucher | 0:567c690c073a | 69 | |
fpucher | 0:567c690c073a | 70 | { |
fpucher | 0:567c690c073a | 71 | Led1.LedOn(); |
fpucher | 0:567c690c073a | 72 | Delay_Nonsense(&BlueDelay, &TargetCount); |
fpucher | 0:567c690c073a | 73 | wait_ms(xDelay); |
fpucher | 0:567c690c073a | 74 | Led1.LedOff(); |
fpucher | 0:567c690c073a | 75 | Delay_Nonsense(&BlueDelay, &TargetCount); |
fpucher | 0:567c690c073a | 76 | wait_ms(xDelay); |
fpucher | 0:567c690c073a | 77 | } |
fpucher | 0:567c690c073a | 78 | } |
fpucher | 0:567c690c073a | 79 | } |
fpucher | 0:567c690c073a | 80 | |
fpucher | 0:567c690c073a | 81 | void Led2_Blink() // green |
fpucher | 0:567c690c073a | 82 | { |
fpucher | 0:567c690c073a | 83 | const int xDelay = 250; |
fpucher | 0:567c690c073a | 84 | uint32_t BlueDelay = 0; |
fpucher | 0:567c690c073a | 85 | const uint32_t TargetCount = 16000; |
fpucher | 0:567c690c073a | 86 | |
fpucher | 0:567c690c073a | 87 | for(;;) { |
fpucher | 0:567c690c073a | 88 | for(int i = 0; i < 10; i++) { // randomnes |
fpucher | 0:567c690c073a | 89 | wait_ms(xDelay); |
fpucher | 0:567c690c073a | 90 | } |
fpucher | 0:567c690c073a | 91 | { |
fpucher | 0:567c690c073a | 92 | Led2.LedOn(); |
fpucher | 0:567c690c073a | 93 | Delay_Nonsense(&BlueDelay, &TargetCount); |
fpucher | 0:567c690c073a | 94 | wait_ms(xDelay); |
fpucher | 0:567c690c073a | 95 | Led2.LedOff(); |
fpucher | 0:567c690c073a | 96 | Delay_Nonsense(&BlueDelay, &TargetCount); |
fpucher | 0:567c690c073a | 97 | wait_ms(xDelay); |
fpucher | 0:567c690c073a | 98 | } |
fpucher | 0:567c690c073a | 99 | } |
fpucher | 0:567c690c073a | 100 | } |
fpucher | 0:567c690c073a | 101 | |
fpucher | 0:567c690c073a | 102 | void Led3_Blink() // blue very short |
fpucher | 0:567c690c073a | 103 | { |
fpucher | 0:567c690c073a | 104 | const int xDelay = 100; |
fpucher | 0:567c690c073a | 105 | uint32_t BlueDelay = 0; |
fpucher | 0:567c690c073a | 106 | const uint32_t TargetCount = 16000; |
fpucher | 0:567c690c073a | 107 | |
fpucher | 0:567c690c073a | 108 | for(;;) { |
fpucher | 0:567c690c073a | 109 | for(int i = 0; i < 10; i++) { // randomnes |
fpucher | 0:567c690c073a | 110 | wait_ms(xDelay); |
fpucher | 0:567c690c073a | 111 | } |
fpucher | 0:567c690c073a | 112 | { |
fpucher | 0:567c690c073a | 113 | Led3.LedOn(); |
fpucher | 0:567c690c073a | 114 | Delay_Nonsense(&BlueDelay, &TargetCount); |
fpucher | 0:567c690c073a | 115 | wait_ms(xDelay); |
fpucher | 0:567c690c073a | 116 | Led3.LedOff(); |
fpucher | 0:567c690c073a | 117 | Delay_Nonsense(&BlueDelay, &TargetCount); |
fpucher | 0:567c690c073a | 118 | wait_ms(xDelay); |
fpucher | 0:567c690c073a | 119 | } |
fpucher | 0:567c690c073a | 120 | } |
fpucher | 0:567c690c073a | 121 | } |
fpucher | 0:567c690c073a | 122 | |
fpucher | 1:a477e0d640e0 | 123 | DigitalOut Led4(LED4); |
fpucher | 1:a477e0d640e0 | 124 | volatile bool running = true; |
fpucher | 1:a477e0d640e0 | 125 | |
fpucher | 1:a477e0d640e0 | 126 | // Callback function to pass arguments to params |
fpucher | 1:a477e0d640e0 | 127 | void blink(DigitalOut *led) { |
fpucher | 1:a477e0d640e0 | 128 | osThreadSetPriority(osThreadGetId(), osPriorityIdle); |
fpucher | 1:a477e0d640e0 | 129 | while (running) { |
fpucher | 1:a477e0d640e0 | 130 | *led = !*led; |
fpucher | 1:a477e0d640e0 | 131 | wait(1); |
fpucher | 1:a477e0d640e0 | 132 | } |
fpucher | 1:a477e0d640e0 | 133 | } |
fpucher | 1:a477e0d640e0 | 134 | |
fpucher | 1:a477e0d640e0 | 135 | // https://docs.mbed.com/docs/mbed-os-api-reference/en/latest/APIs/tasks/rtos/#thread |
fpucher | 1:a477e0d640e0 | 136 | |
fpucher | 1:a477e0d640e0 | 137 | Thread thread1(osPriorityNormal, DEFAULT_STACK_SIZE, NULL); |
fpucher | 1:a477e0d640e0 | 138 | // Thread thread1; |
fpucher | 0:567c690c073a | 139 | Thread thread2; |
fpucher | 0:567c690c073a | 140 | Thread thread3; |
fpucher | 1:a477e0d640e0 | 141 | // parametrisierter Thread |
fpucher | 1:a477e0d640e0 | 142 | Thread pthread (osPriorityNormal, DEFAULT_STACK_SIZE, NULL); |
fpucher | 0:567c690c073a | 143 | |
fpucher | 0:567c690c073a | 144 | int main() { |
fpucher | 0:567c690c073a | 145 | thread1.start(Led1_Blink); |
fpucher | 0:567c690c073a | 146 | thread2.start(Led2_Blink); |
fpucher | 1:a477e0d640e0 | 147 | thread3.start(Led3_Blink); |
fpucher | 1:a477e0d640e0 | 148 | // Start parametrisierter Thread mit Callback Funktion |
fpucher | 1:a477e0d640e0 | 149 | pthread.start(callback(blink, &Led4)); |
fpucher | 0:567c690c073a | 150 | |
fpucher | 1:a477e0d640e0 | 151 | osThreadSetPriority(osThreadGetId(), osPriorityIdle); // osPriorityHigh, |
fpucher | 1:a477e0d640e0 | 152 | // osPriorityIdle |
fpucher | 1:a477e0d640e0 | 153 | Thread::yield(); |
fpucher | 1:a477e0d640e0 | 154 | printf("Priority is %i\r\n", thread1.get_priority()); |
fpucher | 1:a477e0d640e0 | 155 | Thread::wait(1); |
fpucher | 1:a477e0d640e0 | 156 | |
fpucher | 1:a477e0d640e0 | 157 | thread1.join(); |
fpucher | 1:a477e0d640e0 | 158 | thread2.join(); |
fpucher | 1:a477e0d640e0 | 159 | thread3.join(); |
fpucher | 1:a477e0d640e0 | 160 | |
fpucher | 0:567c690c073a | 161 | while(1) { |
fpucher | 0:567c690c073a | 162 | } |
fpucher | 1:a477e0d640e0 | 163 | thread1.terminate(); |
fpucher | 0:567c690c073a | 164 | } |