Skip to content

Commit

Permalink
indicator_app: Fix wrong color on start up
Browse files Browse the repository at this point in the history
  • Loading branch information
xingrz committed Feb 12, 2023
1 parent 7937f84 commit b0cb678
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 12 deletions.
25 changes: 18 additions & 7 deletions config/boards/arm/hw75_dynamic/app/indicator_app.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ static const struct device *led_strip;
static const struct led_rgb color_red = { RGB(0xFF, 0x00, 0x00) };
static const struct led_rgb color_green = { RGB(0x00, 0xFF, 0x00) };

static struct k_mutex lock;

static struct led_rgb current;
static bool active = true;

Expand All @@ -48,11 +50,11 @@ static inline void apply_brightness(struct led_rgb *c_out, const struct led_rgb
c_out->b = (uint8_t)((int)c_in->b * bri / 255);
}

static int indicator_update()
static void indicator_update(struct k_work *work)
{
if (!led_strip) {
LOG_ERR("LED strip device %s not found", STRIP_LABEL);
return -ENODEV;
return;
}

apply_color(&current, state ? &color_red : &color_green);
Expand All @@ -66,26 +68,34 @@ static int indicator_update()
LOG_DBG("Update indicator, color: %02X%02X%02X, brightness: %d -> %02X%02X%02X", current.r,
current.g, current.b, bri, color.r, color.g, color.b);

return led_strip_remap_set(led_strip, "STATUS", &color);
led_strip_remap_set(led_strip, "STATUS", &color);
}

K_WORK_DEFINE(indicator_update_work, indicator_update);

static int indicator_app_init(const struct device *dev)
{
ARG_UNUSED(dev);
led_strip = device_get_binding(STRIP_LABEL);
return indicator_update();
k_mutex_init(&lock);
k_work_submit_to_queue(&k_sys_work_q, &indicator_update_work);
return 0;
}

void indicator_set_bits(uint32_t bits)
{
k_mutex_lock(&lock, K_FOREVER);
state |= bits;
indicator_update();
k_mutex_unlock(&lock);
k_work_submit_to_queue(&k_sys_work_q, &indicator_update_work);
}

void indicator_clear_bits(uint32_t bits)
{
k_mutex_lock(&lock, K_FOREVER);
state &= ~bits;
indicator_update();
k_mutex_unlock(&lock);
k_work_submit_to_queue(&k_sys_work_q, &indicator_update_work);
}

static int indicator_app_event_listener(const zmk_event_t *eh)
Expand All @@ -95,7 +105,8 @@ static int indicator_app_event_listener(const zmk_event_t *eh)

if ((activity_ev = as_zmk_activity_state_changed(eh)) != NULL) {
active = activity_ev->state == ZMK_ACTIVITY_ACTIVE;
return indicator_update();
k_work_submit_to_queue(&k_sys_work_q, &indicator_update_work);
return 0;
} else if ((knob_ev = as_app_knob_state_changed(eh)) != NULL) {
if (knob_ev->calibration == KNOB_CALIBRATE_OK) {
indicator_clear_bits(INDICATOR_KNOB_CALIBRATING);
Expand Down
14 changes: 9 additions & 5 deletions config/boards/arm/hw75_keyboard/app/indicator_app.c
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,11 @@ static inline void apply_brightness(struct led_rgb *c_out, const struct led_rgb
c_out->b = (uint8_t)((int)c_in->b * bri / 255);
}

static int indicator_update()
static void indicator_update(struct k_work *work)
{
if (!led_strip) {
LOG_ERR("LED strip device %s not found", STRIP_LABEL);
return -EINVAL;
return;
}

apply_color(&current, &color_green); // TODO
Expand All @@ -60,14 +60,17 @@ static int indicator_update()
LOG_DBG("Update indicator, color: %02X%02X%02X, brightness: %d -> %02X%02X%02X", current.r,
current.g, current.b, bri, color.r, color.g, color.b);

return led_strip_remap_set(led_strip, "STATUS", &color);
led_strip_remap_set(led_strip, "STATUS", &color);
}

K_WORK_DEFINE(indicator_update_work, indicator_update);

static int indicator_app_init(const struct device *dev)
{
ARG_UNUSED(dev);
led_strip = device_get_binding(STRIP_LABEL);
return indicator_update();
k_work_submit_to_queue(&k_sys_work_q, &indicator_update_work);
return 0;
}

static int indicator_app_event_listener(const zmk_event_t *eh)
Expand All @@ -76,7 +79,8 @@ static int indicator_app_event_listener(const zmk_event_t *eh)

if ((activity_ev = as_zmk_activity_state_changed(eh)) != NULL) {
active = activity_ev->state == ZMK_ACTIVITY_ACTIVE;
return indicator_update();
k_work_submit_to_queue(&k_sys_work_q, &indicator_update_work);
return 0;
}

return -ENOTSUP;
Expand Down

0 comments on commit b0cb678

Please sign in to comment.