StripEvents - Event driven LED strip control
Our local community has been flooded with an abundance of LED strips equipped with WS2811 RGB addressable LEDs. Cool stuff has been build and is still in the making. Some people went as far as repurposing a FTDI-Friend to directly adress the strips, other are using Arduinos or smaller ICs.
During a discussion the issue arose having different “animations” coexisting on the same strip. Especially on the arduino platform the codes organization wasn’t nice. As consequence this small two file library was written to showcast an idea on how to improve the code structure.
The difference to a normal event scheduler is that the event triggered is applied to an interval. This interval translates to a range of LEDs the function may work on. The scheduler itself only has a concept of ticks. It can run in a loop, where the delay comes from a user supplied callback or via a timer interrupt.
/*
* Initialises the event handler
* Takes as argument a callback to a function that may be called
* at the end of each tick
*/
void initEvent(void (*callback)());
/*
* Invokes the event loop. Does not return
* The delay is managed by the callback
*/
void eventLoop();
/*
* Performs all actions for one tick.
* Can be used for a timer/interrupt driven schedule
*/
void eventLoopInterrupt();
/*
* Registers an event in the handler. The arguments from and to describe the intervall the callback
* should work on. The callback get these as argument in the same order. The argument invoke
* describes in how many ticks the event is to be called.
*
* If the event is overlapping with an event that is already registered the event is removed.
* Registration is done at the end of each tick in the order the events were added.
*/
void registerEvent(void (*call)(unsigned,unsigned), unsigned from, unsigned to, unsigned invoke);
/*
* This function may be called inside an event callback to reschedule the current event.
*/
void recallEvent(unsigned ticks);
An example and the code can be found here: https://github.com/Gnoxter/StripEvents