Skip to content

Commit

Permalink
feat(backlight): Add backlight state changed event
Browse files Browse the repository at this point in the history
Add a backlight state changed event whenever the backlight is updated
  • Loading branch information
ReFil committed Dec 12, 2024
1 parent 7013158 commit ca2ea82
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 7 deletions.
2 changes: 2 additions & 0 deletions app/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,9 @@ 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_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);
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);

0 comments on commit ca2ea82

Please sign in to comment.