Skip to main content

Tick

The tick is a constant event that refreshes all time based actions on the LED strip. It is executed in the main loop of the application therefore the core has no idea about the refresh rate. It receives the elapsed time information when it's called. The refresh rate is a tradeoff between the animation smoothness of the segments and the computing resource needs of the software. It kind of works like the frames in a movie or in a game. The more times the tick is executed in a second the higher the details are in transitions like moving or dimming. However it demands more CPU resource from the hardware it runs on.

A lot of stuff happens in a tick cycle. The below process diagram shows these steps:

img

Execute any timer based actions that is due

When an action need to be executed after a certain delay(timer actions, music rhythms) a new thread is created and a callback function is called after the specified delay. The callback function pushes the action in a queue that is executed in the next tick. To avoid any complication caused by possible data race this queue is protected by a mutex that is locked by the tick and by the callback function as well.

Update the time based properties on all segments of all LED strips

Properties like moving and dimming should take effect gradually and the tick event makes sure that it's continuously updated to make it seamless.

Remove all not visible segments

The segments could become invisible in the following scenarios:

  • It moves out of the strip in any direction
  • It dims to 0 intensity.
  • It shrinks to 0 length.

Since these segments are not useful anymore they are deleted.

Determine all LEDs the segments occupy

This is the step where each segment is projected onto an array of colors that represents an LED strip. For example:

If a segment's head position is 50 and it's length is 5, it occupies LED #50,#49, #48, #47 and #46.

Calculate the color of LEDs where segments overlap

An LED could be occupied by multiple segments at the same time. For now, the solution is to add their colors together in an additive(Sum the rgb components and maximize them in 255) manner. A future plan is that the method of aggregation is configurable and there will be an option where the priority attribute of the segment is used to determine which one hides the other.

Fill the buffer that the application that uses the core provides with colors

Ultimately the tick function is what generates the output for the application. It expects the color for each LED on the strips. So the tick function calls a callback function for each index of each LED strip with an uint8_t array of size 3. Each element of this array represents one of the components of an RGB color. Each component is a number in the 0-255 range, and it represents the brightness of a color of an LED. After this the tick has no more responsibility, the application converts the array into the necessary format.