RGB Thread with synchrinization, such as mutex, semaphore and signal.
siehe auch RTOS-Threads Application Board
Diff: main.cpp
- Revision:
- 0:15d27adc65b3
- Child:
- 1:db53d0623808
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main.cpp Wed Mar 22 15:35:52 2017 +0000 @@ -0,0 +1,181 @@ +// see: [[RTOS: Demonstration Setup]] +// Trace ab 15:00 + +#include "mbed.h" + +class Rgb +{ +private: + DigitalOut _led; + +public: + Rgb(PinName ld) : _led(ld) { _led = 1; }; // Constructor with RgbLed = off + + void LedOn() { + _led = 0; + } + void LedOff(){ + _led.write(1); + } +}; + +class HasA +{ +private: + DigitalOut _led; + +public: + HasA(PinName ld) : _led(ld) {}; // Constructor + + void LedOn() { + _led = 1; + } + void LedOff(){ + _led.write(0); + } +}; + +void Delay_Nonsense(uint32_t * DelayCounter, uint32_t const * TargetCount) +{ + while(*DelayCounter <= *TargetCount) + { + *DelayCounter = *DelayCounter + 1; + } + *DelayCounter = 0; +} + +//M3 r (p23); g (p24); b (p25); +/* +HasA Led1(LED1); +HasA Led2(LED2); +HasA Led3(LED3); +*/ +Rgb Led1(p23); +Rgb Led2(p24); +Rgb Led3(p25); + +Mutex LedMutex; +Semaphore LedSemaphore(3); + +//void Led1_Blink(void *pvParameters) +void Led1_Blink() // red +{ + const int xDelay = 500; // Leuchtdauer + uint32_t BlueDelay = 0; // zufällige Verzögerung von ... + const uint32_t TargetCount = 16000; // ... bis + + for(;;) { +// for(int i = 0; i < 10; i++) { // Zufälligkeit +// wait_ms(xDelay); +// } + + // Obtain the Mutex - block for 0 ticks if the semaphore is not + // immediately available. +// if( xSemaphoreTake( LEDMutex, ( TickType_t ) 10 ) == pdTRUE ) + //LedMutex.lock(); + //LedSemaphore.wait(); + //Thread::signal_wait(0x1); + { + Led1.LedOn(); + Delay_Nonsense(&BlueDelay, &TargetCount); + wait_ms(xDelay); + Led1.LedOff(); + Delay_Nonsense(&BlueDelay, &TargetCount); + wait_ms(xDelay); + } +// xSemaphoreGive(LEDMutex); + //LedMutex.unlock(); + //LedSemaphore.release(); + } +} + +void Led2_Blink() // green +{ + const int xDelay = 250; + uint32_t BlueDelay = 0; + const uint32_t TargetCount = 16000; + + for(;;) { +// for(int i = 0; i < 10; i++) { +// wait_ms(xDelay); +// } + + // Obtain the Mutex - block for 0 ticks if the semaphore is not + // immediately available. +// if( xSemaphoreTake( LEDMutex, ( TickType_t ) 10 ) == pdTRUE ) + //LedMutex.lock(); + //LedSemaphore.wait(); + //Thread::signal_wait(0x1); + { + Led2.LedOn(); + Delay_Nonsense(&BlueDelay, &TargetCount); + wait_ms(xDelay); + Led2.LedOff(); + Delay_Nonsense(&BlueDelay, &TargetCount); + wait_ms(xDelay); +// xSemaphoreGive(LEDMutex); + } + //LedMutex.unlock(); + //LedSemaphore.release(); + + + } +} + +void Led3_Blink() // blue +{ + const int xDelay = 100; + uint32_t BlueDelay = 0; + const uint32_t TargetCount = 16000; + + for(;;) { +// for(int i = 0; i < 10; i++) { // Zufälligkeit +// wait_ms(xDelay); +// } + + // Obtain the Mutex - block for 0 ticks if the semaphore is not + // immediately available. +// if( xSemaphoreTake( LEDMutex, ( TickType_t ) 10 ) == pdTRUE ) + //LedMutex.lock(); // Led leuchten wird nicht mehr unterbrochen !!! + //LedSemaphore.wait(); + //Thread::signal_wait(0x1); + { + Led3.LedOn(); + Delay_Nonsense(&BlueDelay, &TargetCount); + wait_ms(xDelay+100); + Led3.LedOff(); + Delay_Nonsense(&BlueDelay, &TargetCount); + wait_ms(xDelay); +// xSemaphoreGive(LEDMutex); + } + //LedMutex.unlock(); + //LedSemaphore.release(); + //thread.signal_set(0x1); + + } +} + +Thread thread1(osPriorityNormal, DEFAULT_STACK_SIZE, NULL); +//Thread thread1; +Thread thread2; +Thread thread3; + +int main() { + thread1.start(Led1_Blink); + thread2.start(Led2_Blink); + thread3.start(Led3_Blink); + +/* Signals + wait(2); + thread1.signal_set(0x1); + wait(2); + thread2.signal_set(0x1); + wait(2); + thread3.signal_set(0x1); +*/ + thread1.join(); + thread2.join(); + thread3.join(); + while(1) { + } +}