Thread Sync Example

Dependencies:   C12832

Committer:
bulmecisco
Date:
Thu Mar 21 16:47:54 2019 +0000
Revision:
2:29dca06b0ee0
Parent:
1:a477e0d640e0
Thread Sync Example

Who changed what in which revision?

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