BlinkLed automatically. This library requires RTOS.

Dependents:   mpod_nhk_english mpod_picasa_photoframe mpod_nhk_english_spxml

This is a library that automatically blink LED

RTOS has been used. However, you do not need to be aware of the RTOS.

As with PwmOut, there are restrictions on the LED that you can use. See also PwmOut.

LEDを自動的に点滅させるライブラリです。

内部にRTOSを利用していますが、ユーザがそのことを意識する必要はありません。

使用できるLEDは、PwmOutと同様の制約があります。詳しくは、PwmOutを参照ください。

main.cpp

#include "mbed.h"
#include "BlinkLed.h"

BlinkLed led1(LED1, 0.02);
BlinkLed led2(LED2, 0.04);
BlinkLed led3(LED3, 0.06);
BlinkLed led4(LED4, 0.08);

int main()
{
    while(1) {
        led1.startBlink();
        Thread::wait(1000);

        led2.startBlink();
        Thread::wait(1000);

        led3.startBlink();
        Thread::wait(1000);

        led4.startBlink();
        Thread::wait(10000);

        led1.finishBlink();
        Thread::wait(1000);

        led2.finishBlink();
        Thread::wait(1000);

        led3.finishBlink();
        Thread::wait(1000);

        led4.finishBlink();
        Thread::wait(1000);
    }
}

Import library

Public Member Functions

BlinkLed (PinName pin, float dutyChangeStep)
Constructor.
~BlinkLed ()
Destructor.
void startBlink ()
Start biinking.
void finishBlink ()
Finish biinking.
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 }