Skip to content

Commit

Permalink
feat(underglow): Add underglow state changed event
Browse files Browse the repository at this point in the history
Add event triggered when underglow state is saved
  • Loading branch information
ReFil committed Dec 12, 2024
1 parent ca2ea82 commit b1534ef
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 11 deletions.
1 change: 1 addition & 0 deletions app/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ if ((NOT CONFIG_ZMK_SPLIT) OR CONFIG_ZMK_SPLIT_ROLE_CENTRAL)
endif()

target_sources_ifdef(CONFIG_ZMK_RGB_UNDERGLOW app PRIVATE src/behaviors/behavior_rgb_underglow.c)
target_sources_ifdef(CONFIG_ZMK_RGB_UNDERGLOW app PRIVATE src/events/underglow_state_changed.c)

target_sources_ifdef(CONFIG_ZMK_BACKLIGHT app PRIVATE src/behaviors/behavior_backlight.c)
target_sources_ifdef(CONFIG_ZMK_BACKLIGHT app PRIVATE src/events/backlight_state_changed.c)
Expand Down
17 changes: 17 additions & 0 deletions app/include/zmk/events/underglow_state_changed.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/*
* Copyright (c) 2024 The ZMK Contributors
*
* SPDX-License-Identifier: MIT
*/

#pragma once

#include <zephyr/kernel.h>
#include <zmk/event_manager.h>
#include <zmk/rgb_underglow.h>

struct zmk_underglow_state_changed {
struct zmk_underglow_state state;
};

ZMK_EVENT_DECLARE(zmk_underglow_state_changed);
8 changes: 8 additions & 0 deletions app/include/zmk/rgb_underglow.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,14 @@ struct zmk_led_hsb {
uint8_t b;
};

struct zmk_underglow_state {
struct zmk_led_hsb color;
uint8_t animation_speed;
uint8_t current_effect;
uint16_t animation_step;
bool on;
};

int zmk_rgb_underglow_toggle(void);
int zmk_rgb_underglow_get_state(bool *state);
int zmk_rgb_underglow_on(void);
Expand Down
10 changes: 10 additions & 0 deletions app/src/events/underglow_state_changed.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/*
* Copyright (c) 2024 The ZMK Contributors
*
* SPDX-License-Identifier: MIT
*/

#include <zephyr/kernel.h>
#include <zmk/events/underglow_state_changed.h>

ZMK_EVENT_IMPL(zmk_underglow_state_changed);
19 changes: 8 additions & 11 deletions app/src/rgb_underglow.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include <zmk/event_manager.h>
#include <zmk/events/activity_state_changed.h>
#include <zmk/events/usb_conn_state_changed.h>
#include <zmk/events/underglow_state_changed.h>
#include <zmk/workqueue.h>

LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL);
Expand Down Expand Up @@ -52,19 +53,11 @@ enum rgb_underglow_effect {
UNDERGLOW_EFFECT_NUMBER // Used to track number of underglow effects
};

struct rgb_underglow_state {
struct zmk_led_hsb color;
uint8_t animation_speed;
uint8_t current_effect;
uint16_t animation_step;
bool on;
};

static const struct device *led_strip;

static struct led_rgb pixels[STRIP_NUM_PIXELS];

static struct rgb_underglow_state state;
static struct zmk_underglow_state state;

#if IS_ENABLED(CONFIG_ZMK_RGB_UNDERGLOW_EXT_POWER)
static const struct device *const ext_power = DEVICE_DT_GET(DT_INST(0, zmk_ext_power_generic));
Expand Down Expand Up @@ -190,7 +183,8 @@ static void zmk_rgb_underglow_tick(struct k_work *work) {
zmk_rgb_underglow_effect_swirl();
break;
}

if (state.animation_step == 0)
raise_zmk_underglow_state_changed((struct zmk_underglow_state_changed){.state = state});
int err = led_strip_update_rgb(led_strip, pixels, STRIP_NUM_PIXELS);
if (err < 0) {
LOG_ERR("Failed to update the RGB strip (%d)", err);
Expand Down Expand Up @@ -253,7 +247,7 @@ static int zmk_rgb_underglow_init(void) {
}
#endif

state = (struct rgb_underglow_state){
state = (struct zmk_underglow_state){
color : {
h : CONFIG_ZMK_RGB_UNDERGLOW_HUE_START,
s : CONFIG_ZMK_RGB_UNDERGLOW_SAT_START,
Expand All @@ -277,10 +271,13 @@ static int zmk_rgb_underglow_init(void) {
k_timer_start(&underglow_tick, K_NO_WAIT, K_MSEC(50));
}

raise_zmk_underglow_state_changed((struct zmk_underglow_state_changed){.state = state});

return 0;
}

int zmk_rgb_underglow_save_state(void) {
raise_zmk_underglow_state_changed((struct zmk_underglow_state_changed){.state = state});
#if IS_ENABLED(CONFIG_SETTINGS)
int ret = k_work_reschedule(&underglow_save_work, K_MSEC(CONFIG_ZMK_SETTINGS_SAVE_DEBOUNCE));
return MIN(ret, 0);
Expand Down

0 comments on commit b1534ef

Please sign in to comment.