Skip to content

Treboada/AsyncBlinker

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

19 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

AsyncBlinker library

Travis status

Non-blocking C/C++ library to manage blinking devices (for example, a LED) changing the state in ON-OFF time intervals.

License

This library is GPL-3.0 licensed. See the LICENSE file.

Support

Bugs, feature requests, contributions are welcome: AsyncBlinker issue tracker.

Library usage

Examples index:

  1. SimpleBlink Minimal use of AsyncBlinker to blink the built-in LED in an Arduino Uno board.
  2. MorseSignals -ToDo-

Define a callback function to switch ON or OFF

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);
    }

AsyncBlinker instance

Declare the instance controlling the blinking and initialize with your callback function:

    AsyncBlinker blinker(blink_my_led);

Call the non-blocking update

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.

Define blinking intervals

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.

Set the blinking intervals

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);

Start and stop the blinker

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();

Limit the cycles

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.

Use sub-intervals

-ToDo-

About

Non-blocking C/C++ library to manage blinking devices (for example, a LED) changing the state in ON-OFF time intervals.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages