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/

BlinkLed.cpp

Committer:
togayan
Date:
2012-12-24
Revision:
2:1d0c09c1a8b4
Parent:
1:54071e781f77
Child:
3:f317d057edde

File content as of revision 2:1d0c09c1a8b4:

#include "BlinkLed.h"

BlinkLed::BlinkLed(PinName pin, float dutyChangeStep) :
    led(pin),
    dutyChangeStep(dutyChangeStep),
    pause(true),
    thread(0)
{
}

BlinkLed::~BlinkLed()
{
}

void BlinkLed::startBlink()
{
    pause = false;
    if(!thread)
    {
        thread = new Thread(blink, this, osPriorityNormal, 128, NULL);
    }
    thread->signal_set(1);
}

void BlinkLed::finishBlink()
{
    pause = true;
}

bool BlinkLed::isBlinking()
{
    return !pause;
}

void BlinkLed::blink(void const *argument)
{
    BlinkLed* self = (BlinkLed*)argument;

    bool sign = false;
    while(1)
    {
        if(self->pause)
        {
            self->led = 0.0F;
            Thread::signal_wait(1);
        }
        float brightness = self->led;
        sign = (brightness <= 0.0F) ? true : (1.0F <= brightness) ? false : sign;
        self->led = sign ? brightness + self->dutyChangeStep : brightness - self->dutyChangeStep;
        Thread::wait(20);
    }
}