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:
Mon Dec 24 06:38:42 2012 +0000
Revision:
2:1d0c09c1a8b4
Parent:
1:54071e781f77
Child:
3:f317d057edde
Add "isBlinking()" method.

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 2:1d0c09c1a8b4 30 bool BlinkLed::isBlinking()
togayan 2:1d0c09c1a8b4 31 {
togayan 2:1d0c09c1a8b4 32 return !pause;
togayan 2:1d0c09c1a8b4 33 }
togayan 2:1d0c09c1a8b4 34
togayan 0:a55a3351317d 35 void BlinkLed::blink(void const *argument)
togayan 0:a55a3351317d 36 {
togayan 0:a55a3351317d 37 BlinkLed* self = (BlinkLed*)argument;
togayan 1:54071e781f77 38
togayan 0:a55a3351317d 39 bool sign = false;
togayan 1:54071e781f77 40 while(1)
togayan 0:a55a3351317d 41 {
togayan 0:a55a3351317d 42 if(self->pause)
togayan 0:a55a3351317d 43 {
togayan 0:a55a3351317d 44 self->led = 0.0F;
togayan 1:54071e781f77 45 Thread::signal_wait(1);
togayan 0:a55a3351317d 46 }
togayan 1:54071e781f77 47 float brightness = self->led;
togayan 1:54071e781f77 48 sign = (brightness <= 0.0F) ? true : (1.0F <= brightness) ? false : sign;
togayan 1:54071e781f77 49 self->led = sign ? brightness + self->dutyChangeStep : brightness - self->dutyChangeStep;
togayan 0:a55a3351317d 50 Thread::wait(20);
togayan 0:a55a3351317d 51 }
togayan 0:a55a3351317d 52 }