Non-blocking C/C++ library to manage blinking devices (for example, a LED) changing the state in ON-OFF time intervals.
- Tags: library, C, C++, blink, LED, non-blocking, asynchronous, threads, delay
- Downloads: https://github.com/Treboada/AsyncBlinker/releases
- Repository: https://github.com/Treboada/AsyncBlinker
This library is GPL-3.0 licensed. See the LICENSE file.
Bugs, feature requests, contributions are welcome: AsyncBlinker issue tracker.
Examples index:
- SimpleBlink Minimal use of AsyncBlinker to blink the built-in LED in an Arduino Uno board.
- MorseSignals -ToDo-
You must code the blink action with a function that receives the blink state as a boolean parameter:
void blink_my_led(bool enable) {
digitalWrite(PIN_LED, enable ? HIGH : LOW);
}
Declare the instance controlling the blinking and initialize with your callback function:
AsyncBlinker blinker(blink_my_led);
Be sure that your main loop calls to the tickUpdate()
method of the
instance to update the blinker status. It is necessary to specify the elapsed
milliseconds from the last call:
blinker.tickUpdate(elapsed_millis);
See in the SimpleBlink example how to calculate the elapsed time in the main loop.
AsyncBlinker manages blinking intervals as arrays of ON and OFF elements. Each element is the amount of milliseconds (from 0 to 65535) to keep ON or OFF:
// default blink (500ms ON and 500ms OFF):
uint16_t simple_blink[] = { 500, 500 };
// fast blink (200ms ON and 200ms OFF):
uint16_t fast_blink[] = { 200, 200 };
// peaceful blink (20ms ON and 5s OFF):
uint16_t background_signal[] = { 20, 5000 };
The first interval is the milliseconds to keep ON, second interval is to keep OFF, third one to keep OFF, fourth to keep ON and so on...
// Morse 'O' (---) and 'S' (...)
uint16_t morse_o[] = { 900, 300, 900, 300, 900, 300 };
uint16_t morse_s[] = { 300, 300, 300, 300, 300, 300 };
Tip: the first element is always to switch 'ON', but you can use 0 ms to skip it.
Call setIntervals()
method passing the array defining the intervals and
the number of elements in the array:
AsyncBlinker blinker(blink_my_led);
uint16_t fast_blink[] = { 200, 200 };
blinker.setIntervals(fast_blink, 2);
blinker.start();
Tip: use sizeof()
to get the length of an array in bytes and divide by 2 (the
length in bytes of each uint16_t element) to pass the elements count:
blinker.setIntervals(fast_blink, sizeof(fast_blink) / 2);
Call start()
and stop()
methods in any part of your code to control the
blinking:
// something was changed its state:
switch (new_state) {
case: STATE_WIFI_ON: blinker.start(); break;
case: STATE_WIFI_OFF: blinker.stop(); break;
}
Note: the method setIntervals()
always stops the blinker.
You can test if the blinker is stopped with the isStopped()
method:
if (blinker.isStopped()) blinker.start();
By default it cycles intervals endlessly from the first to the last and
restarting with the first again. You can limit to a number of cycles (1-254)
passing it as the first parameter of start()
function:
AsyncBlinker blinker(blink_my_led);
blinker.start(5); // blinks 5 times
The default 'endlessly' mode can be specified as:
AsyncBlinker blinker(blink_my_led);
blinker.start(AsyncBlinker::ENDLESSLY);
Tip: tickUpdate()
method (executing in the main loop) returns if the blinker
was stopped, so you can make any decision there when the blinker stops the
cycles.
-ToDo-