Library thread Blink Led or other processes use RTOS

Dependents:   LedsThreading

Fork of BlinkLed by Satoshi Togawa

Example

https://developer.mbed.org/users/AVELARDEV/code/LedsThreading/

Committer:
togayan
Date:
Sat Sep 01 11:08:44 2012 +0000
Revision:
1:54071e781f77
Parent:
0:a55a3351317d
Child:
2:1d0c09c1a8b4
Use signal to stop blinking

Who changed what in which revision?

UserRevisionLine numberNew contents of line
togayan 0:a55a3351317d 1 #include "BlinkLed.h"
togayan 1:54071e781f77 2
togayan 0:a55a3351317d 3 BlinkLed::BlinkLed(PinName pin, float dutyChangeStep) :
togayan 1:54071e781f77 4 led(pin),
togayan 1:54071e781f77 5 dutyChangeStep(dutyChangeStep),
togayan 1:54071e781f77 6 pause(true),
togayan 1:54071e781f77 7 thread(0)
togayan 0:a55a3351317d 8 {
togayan 0:a55a3351317d 9 }
togayan 0:a55a3351317d 10
togayan 0:a55a3351317d 11 BlinkLed::~BlinkLed()
togayan 0:a55a3351317d 12 {
togayan 0:a55a3351317d 13 }
togayan 0:a55a3351317d 14
togayan 0:a55a3351317d 15 void BlinkLed::startBlink()
togayan 0:a55a3351317d 16 {
togayan 0:a55a3351317d 17 pause = false;
togayan 1:54071e781f77 18 if(!thread)
togayan 0:a55a3351317d 19 {
togayan 0:a55a3351317d 20 thread = new Thread(blink, this, osPriorityNormal, 128, NULL);
togayan 0:a55a3351317d 21 }
togayan 1:54071e781f77 22 thread->signal_set(1);
togayan 0:a55a3351317d 23 }
togayan 0:a55a3351317d 24
togayan 0:a55a3351317d 25 void BlinkLed::finishBlink()
togayan 0:a55a3351317d 26 {
togayan 0:a55a3351317d 27 pause = true;
togayan 0:a55a3351317d 28 }
togayan 0:a55a3351317d 29
togayan 0:a55a3351317d 30 void BlinkLed::blink(void const *argument)
togayan 0:a55a3351317d 31 {
togayan 0:a55a3351317d 32 BlinkLed* self = (BlinkLed*)argument;
togayan 1:54071e781f77 33
togayan 0:a55a3351317d 34 bool sign = false;
togayan 1:54071e781f77 35 while(1)
togayan 0:a55a3351317d 36 {
togayan 0:a55a3351317d 37 if(self->pause)
togayan 0:a55a3351317d 38 {
togayan 0:a55a3351317d 39 self->led = 0.0F;
togayan 1:54071e781f77 40 Thread::signal_wait(1);
togayan 0:a55a3351317d 41 }
togayan 1:54071e781f77 42 float brightness = self->led;
togayan 1:54071e781f77 43 sign = (brightness <= 0.0F) ? true : (1.0F <= brightness) ? false : sign;
togayan 1:54071e781f77 44 self->led = sign ? brightness + self->dutyChangeStep : brightness - self->dutyChangeStep;
togayan 0:a55a3351317d 45 Thread::wait(20);
togayan 0:a55a3351317d 46 }
togayan 0:a55a3351317d 47 }