Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add RGB underglow and backlighting events #2706

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions app/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,10 @@ 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)

target_sources_ifdef(CONFIG_ZMK_BATTERY_REPORTING app PRIVATE src/events/battery_state_changed.c)
target_sources_ifdef(CONFIG_ZMK_BATTERY_REPORTING app PRIVATE src/battery.c)
Expand Down
5 changes: 5 additions & 0 deletions app/include/zmk/backlight.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@

#pragma once

struct zmk_backlight_state {
uint8_t brightness;
bool on;
};

int zmk_backlight_on(void);
int zmk_backlight_off(void);
int zmk_backlight_toggle(void);
Expand Down
17 changes: 17 additions & 0 deletions app/include/zmk/events/backlight_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/backlight.h>

struct zmk_backlight_state_changed {
struct zmk_backlight_state state;
};

ZMK_EVENT_DECLARE(zmk_backlight_state_changed);
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
11 changes: 4 additions & 7 deletions app/src/backlight.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include <zmk/event_manager.h>
#include <zmk/events/activity_state_changed.h>
#include <zmk/events/usb_conn_state_changed.h>
#include <zmk/events/backlight_state_changed.h>

LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL);

Expand All @@ -34,13 +35,8 @@ static const struct device *const backlight_dev = DEVICE_DT_GET(DT_CHOSEN(zmk_ba

#define BRT_MAX 100

struct backlight_state {
uint8_t brightness;
bool on;
};

static struct backlight_state state = {.brightness = CONFIG_ZMK_BACKLIGHT_BRT_START,
.on = IS_ENABLED(CONFIG_ZMK_BACKLIGHT_ON_START)};
static struct zmk_backlight_state state = {.brightness = CONFIG_ZMK_BACKLIGHT_BRT_START,
.on = IS_ENABLED(CONFIG_ZMK_BACKLIGHT_ON_START)};

static int zmk_backlight_update(void) {
uint8_t brt = zmk_backlight_get_brt();
Expand All @@ -53,6 +49,7 @@ static int zmk_backlight_update(void) {
return rc;
}
}
raise_zmk_backlight_state_changed((struct zmk_backlight_state_changed){.state = state});
return 0;
}

Expand Down
10 changes: 10 additions & 0 deletions app/src/events/backlight_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/backlight_state_changed.h>

ZMK_EVENT_IMPL(zmk_backlight_state_changed);
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
Loading