Skip to content

Commit

Permalink
Add header and expose indicate methods
Browse files Browse the repository at this point in the history
  • Loading branch information
caksoylar committed Aug 19, 2024
1 parent f2f6fa9 commit 7dd4ff7
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 54 deletions.
4 changes: 3 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
target_sources_ifdef(CONFIG_RGBLED_WIDGET app PRIVATE leds.c)
target_sources_ifdef(CONFIG_RGBLED_WIDGET app PRIVATE src/widget.c)

zephyr_include_directories(include)
92 changes: 39 additions & 53 deletions leds.c → src/widget.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,12 @@

#include <zephyr/logging/log.h>

#include <zmk_rgbled_widget/widget.h>

LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL);

#define LED_GPIO_NODE_ID DT_COMPAT_GET_ANY_STATUS_OKAY(gpio_leds)

#define SHOW_LAYER_CHANGE \
(IS_ENABLED(CONFIG_RGBLED_WIDGET_SHOW_LAYER_CHANGE)) && \
(!IS_ENABLED(CONFIG_ZMK_SPLIT) || IS_ENABLED(CONFIG_ZMK_SPLIT_ROLE_CENTRAL))

BUILD_ASSERT(DT_NODE_EXISTS(DT_ALIAS(led_red)),
"An alias for a red LED is not found for RGBLED_WIDGET");
BUILD_ASSERT(DT_NODE_EXISTS(DT_ALIAS(led_green)),
Expand All @@ -40,32 +38,12 @@ static const uint8_t rgb_idx[] = {DT_NODE_CHILD_IDX(DT_ALIAS(led_red)),
// flag to indicate whether the initial boot up sequence is complete
static bool initialized = false;

// color values as specified by an RGB bitfield
enum color_t {
LED_BLACK, // 0b000
LED_RED, // 0b001
LED_GREEN, // 0b010
LED_YELLOW, // 0b011
LED_BLUE, // 0b100
LED_MAGENTA, // 0b101
LED_CYAN, // 0b110
LED_WHITE // 0b111
};

// a blink work item as specified by the color and duration
struct blink_item {
enum color_t color;
uint16_t duration_ms;
bool first_item;
uint16_t sleep_ms;
};

// define message queue of blink work items, that will be processed by a
// separate thread
K_MSGQ_DEFINE(led_msgq, sizeof(struct blink_item), 16, 1);

#if IS_ENABLED(CONFIG_ZMK_BLE)
static void output_blink(void) {
void indicate_connectivity(void) {
struct blink_item blink = {.duration_ms = CONFIG_RGBLED_WIDGET_OUTPUT_BLINK_MS};

#if !IS_ENABLED(CONFIG_ZMK_SPLIT) || IS_ENABLED(CONFIG_ZMK_SPLIT_ROLE_CENTRAL)
Expand Down Expand Up @@ -95,7 +73,7 @@ static void output_blink(void) {

static int led_output_listener_cb(const zmk_event_t *eh) {
if (initialized) {
output_blink();
indicate_connectivity();
}
return 0;
}
Expand All @@ -111,6 +89,33 @@ ZMK_SUBSCRIPTION(led_output_listener, zmk_split_peripheral_status_changed);
#endif // IS_ENABLED(CONFIG_ZMK_BLE)

#if IS_ENABLED(CONFIG_ZMK_BATTERY_REPORTING)
void indicate_battery(void) {
struct blink_item blink = {.duration_ms = CONFIG_RGBLED_WIDGET_BATTERY_BLINK_MS,
.first_item = true};
uint8_t battery_level = zmk_battery_state_of_charge();
int retry = 0;
while (battery_level == 0 && retry++ < 10) {
k_sleep(K_MSEC(100));
battery_level = zmk_battery_state_of_charge();
};

if (battery_level == 0) {
LOG_INF("Battery level undetermined (zero), blinking magenta");
blink.color = LED_MAGENTA;
} else if (battery_level >= CONFIG_RGBLED_WIDGET_BATTERY_LEVEL_HIGH) {
LOG_INF("Battery level %d, blinking green", battery_level);
blink.color = LED_GREEN;
} else if (battery_level >= CONFIG_RGBLED_WIDGET_BATTERY_LEVEL_LOW) {
LOG_INF("Battery level %d, blinking yellow", battery_level);
blink.color = LED_YELLOW;
} else {
LOG_INF("Battery level %d, blinking red", battery_level);
blink.color = LED_RED;
}

k_msgq_put(&led_msgq, &blink, K_NO_WAIT);
}

static int led_battery_listener_cb(const zmk_event_t *eh) {
if (!initialized) {
return 0;
Expand Down Expand Up @@ -145,7 +150,11 @@ static int led_layer_listener_cb(const zmk_event_t *eh) {
return 0;
}

static void indicate_layer(struct k_work *work) {
static void indicate_layer_cb(struct k_work *work) {
indicate_layer();
}

void indicate_layer(void) {
uint8_t index = zmk_keymap_highest_layer_active();
static const struct blink_item blink = {.duration_ms = CONFIG_RGBLED_WIDGET_LAYER_BLINK_MS,
.color = LED_CYAN,
Expand All @@ -171,7 +180,7 @@ extern void led_process_thread(void *d0, void *d1, void *d2) {
ARG_UNUSED(d2);

#if SHOW_LAYER_CHANGE
k_work_init_delayable(&layer_indicate_work, indicate_layer);
k_work_init_delayable(&layer_indicate_work, indicate_layer_cb);
#endif

while (true) {
Expand Down Expand Up @@ -221,30 +230,7 @@ extern void led_init_thread(void *d0, void *d1, void *d2) {
// check and indicate battery level on thread start
LOG_INF("Indicating initial battery status");

struct blink_item blink = {.duration_ms = CONFIG_RGBLED_WIDGET_BATTERY_BLINK_MS,
.first_item = true};
uint8_t battery_level = zmk_battery_state_of_charge();
int retry = 0;
while (battery_level == 0 && retry++ < 10) {
k_sleep(K_MSEC(100));
battery_level = zmk_battery_state_of_charge();
};

if (battery_level == 0) {
LOG_INF("Battery level undetermined (zero), blinking magenta");
blink.color = LED_MAGENTA;
} else if (battery_level >= CONFIG_RGBLED_WIDGET_BATTERY_LEVEL_HIGH) {
LOG_INF("Battery level %d, blinking green", battery_level);
blink.color = LED_GREEN;
} else if (battery_level >= CONFIG_RGBLED_WIDGET_BATTERY_LEVEL_LOW) {
LOG_INF("Battery level %d, blinking yellow", battery_level);
blink.color = LED_YELLOW;
} else {
LOG_INF("Battery level %d, blinking red", battery_level);
blink.color = LED_RED;
}

k_msgq_put(&led_msgq, &blink, K_NO_WAIT);
indicate_battery();

// wait until blink should be displayed for further checks
k_sleep(K_MSEC(CONFIG_RGBLED_WIDGET_BATTERY_BLINK_MS + CONFIG_RGBLED_WIDGET_INTERVAL_MS));
Expand All @@ -253,7 +239,7 @@ extern void led_init_thread(void *d0, void *d1, void *d2) {
#if IS_ENABLED(CONFIG_ZMK_BLE)
// check and indicate current profile or peripheral connectivity status
LOG_INF("Indicating initial connectivity status");
output_blink();
indicate_connectivity();
#endif // IS_ENABLED(CONFIG_ZMK_BLE)

initialized = true;
Expand Down
2 changes: 2 additions & 0 deletions zephyr/module.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
name: zmk-rgbled-widget
build:
cmake: .
kconfig: Kconfig
settings:
board_root: .
dts_root: .

0 comments on commit 7dd4ff7

Please sign in to comment.