LedLib is a repository for LED and RGB light scenes. Currently, it only contains rgb_scene
.
rgb_scene
supports both monochrome LED lights and RGB tri-color lights. By defining different LED scenes and running priorities, it can meet various indicator light needs.
An Action is the smallest execution unit of rgb_scene
. rgb_scene
supports three types of Actions.
typedef struct
{
uint16_t rgb_mask; /* RGB mask */
uint16_t cycle; /* Action cycle count, 0xFFFF means forever */
action_type_t type; /* Action type */
union
{
action_onoff_t onoff; /* ON/OFF */
action_blink_t blink; /* Blink */
action_fade_t fade; /* Fade */
} sub;
} action_t;
typedef struct
{
rgb_value_t value; /* RGB value */
uint16_t lifetime; /* Duration */
} action_onoff_t;
typedef struct
{
action_onoff_t action1; /* First half of the blink action */
action_onoff_t action2; /* Second half of the blink action */
} action_blink_t;
typedef struct
{
rgb_value_t start; /* RGB value before fading */
rgb_value_t end; /* RGB value after fading */
uint8_t step; /* Number of fade steps */
uint16_t interval; /* Time interval */
} action_fade_t;
A Scene is composed of Actions. By combining different Actions, complex application scenarios can be implemented.
For example: A red light fades from 0% brightness to 100% brightness, stays for 400 milliseconds, then fades from 100% brightness back to 0%, stays for 1 second, and continues in this cycle.
const rgb_scene_t c_sys_pairing_fail = {
.cycle = 5,
.num = 4,
.action[0] = {
.cycle = 1,
.type = ACTION_FADE,
.sub.fade.start.r = 0,
.sub.fade.start.g = 0,
.sub.fade.start.b = 0,
.sub.fade.end.r = 0xFF,
.sub.fade.end.g = 0,
.sub.fade.end.b = 0,
.sub.fade.step = 100,
.sub.fade.interval = 10*RGB_MSEC,
},
.action[1] = {
.cycle = 1,
.type = ACTION_ONOFF,
.sub.onoff.value.r = 0xFF,
.sub.onoff.value.g = 0,
.sub.onoff.value.b = 0,
.sub.onoff.lifetime = 400*RGB_MSEC,
},
.action[2] = {
.cycle = 1,
.type = ACTION_FADE,
.sub.fade.start.r = 0xFF,
.sub.fade.start.g = 0,
.sub.fade.start.b = 0,
.sub.fade.end.r = 0,
.sub.fade.end.g = 0,
.sub.fade.end.b = 0,
.sub.fade.step = 100,
.sub.fade.interval = 10*RGB_MSEC,
},
.action[3] = {
.cycle = 1,
.type = ACTION_ONOFF,
.sub.onoff.value.r = 0,
.sub.onoff.value.g = 0,
.sub.onoff.value.b = 0,
.sub.onoff.lifetime = 1*RGB_SEC,
},
};
rgb_scene
uses priority to regulate the execution order of scenes. Each scene must specify a priority, and multiple Scenes can share the same priority.
Compilation and running environment: ubuntu 18
- cd /demo/linux
- make
- ./build/rgb_scene_demo
- For each independent LED, create a corresponding
rgb_scene_obj_t
object. - Each
rgb_scene_obj_t
object needs to define the corresponding enumeration -> priority and scene ID. - Based on the priority and scene ID, define the scene list
rgb_scene_tab_t
. - Mutually exclusive scenes are set to the same priority so that they will replace each other.
- High-priority scenes preempt low-priority scenes. After a high-priority scene finishes, the low-priority scene will continue to run.
- Design principle: For urgent and brief events, set them as high priority.
- Copy the LedLib folder to your project’s source directory.
- In the IDE or makefile, add the
rgb_scene.c
source file and include thergb_scene.h
header file path. - Define the scene table
rgb_scene_tab_t
. - Call
RgbSceneObjCreate
to create LED/RGB objects. - Periodically call
RgbSceneTick
. - Use
RgbSceneStart
to run a specified scene.
p.s. For details, refer to the demo.