Skip to content

Commit

Permalink
indicator_app: Dim indicators on idle
Browse files Browse the repository at this point in the history
  • Loading branch information
xingrz committed Jan 29, 2023
1 parent 44994c3 commit 0079aef
Show file tree
Hide file tree
Showing 4 changed files with 133 additions and 64 deletions.
47 changes: 43 additions & 4 deletions config/boards/arm/hw75_dynamic/app/indicator_app.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,27 +12,61 @@ LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL);
#include <drivers/led_strip_remap.h>

#include <zmk/event_manager.h>
#include <zmk/events/activity_state_changed.h>
#include <app/events/knob_state_changed.h>

#include "indicator_app.h"

#define STRIP_LABEL DT_LABEL(DT_CHOSEN(zmk_underglow))

#define BRI_ACTIVE (5)
#define BRI_INACTIVE (1)

#define RGB(R, G, B) .r = (R), .g = (G), .b = (B)

static const struct device *led_strip;

static struct led_rgb color_red = { .r = 0x04, .g = 0x00, .b = 0x00 };
static struct led_rgb color_green = { .r = 0x00, .g = 0x04, .b = 0x00 };
static const struct led_rgb color_red = { RGB(0xFF, 0x00, 0x00) };
static const struct led_rgb color_green = { RGB(0x00, 0xFF, 0x00) };

static struct led_rgb current;
static bool active = true;

static uint32_t state = 0;

static inline void apply_color(struct led_rgb *c_out, const struct led_rgb *c_in)
{
c_out->r = c_in->r;
c_out->g = c_in->g;
c_out->b = c_in->b;
}

static inline void apply_brightness(struct led_rgb *c_out, const struct led_rgb *c_in, uint8_t bri)
{
c_out->r = (uint8_t)((int)c_in->r * bri / 255);
c_out->g = (uint8_t)((int)c_in->g * bri / 255);
c_out->b = (uint8_t)((int)c_in->b * bri / 255);
}

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

return led_strip_remap_set(led_strip, "STATUS", state ? &color_red : &color_green);
apply_color(&current, state ? &color_red : &color_green);

uint8_t bri = active ? BRI_ACTIVE : BRI_INACTIVE;

struct led_rgb color;
apply_color(&color, &current);
apply_brightness(&color, &current, bri);

LOG_DBG("Update indicator, color: %02X%02X%02X, brightness: %d -> %02X%02X%02X", current.r,
current.b, current.b, bri, color.r, color.g, color.b);

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

static int indicator_app_init(const struct device *dev)
Expand All @@ -56,9 +90,13 @@ void indicator_clear_bits(uint32_t bits)

static int indicator_app_event_listener(const zmk_event_t *eh)
{
struct zmk_activity_state_changed *activity_ev;
struct app_knob_state_changed *knob_ev;

if ((knob_ev = as_app_knob_state_changed(eh)) != NULL) {
if ((activity_ev = as_zmk_activity_state_changed(eh)) != NULL) {
active = activity_ev->state == ZMK_ACTIVITY_ACTIVE;
return indicator_update();
} else if ((knob_ev = as_app_knob_state_changed(eh)) != NULL) {
if (knob_ev->calibration == KNOB_CALIBRATE_OK) {
indicator_clear_bits(INDICATOR_KNOB_CALIBRATING);
} else {
Expand All @@ -71,6 +109,7 @@ static int indicator_app_event_listener(const zmk_event_t *eh)
}

ZMK_LISTENER(indicator_app, indicator_app_event_listener);
ZMK_SUBSCRIPTION(indicator_app, zmk_activity_state_changed);
ZMK_SUBSCRIPTION(indicator_app, app_knob_state_changed);

SYS_INIT(indicator_app_init, APPLICATION, CONFIG_APPLICATION_INIT_PRIORITY);
4 changes: 2 additions & 2 deletions config/boards/arm/hw75_keyboard/app/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# Copyright (c) 2022-2023 XiNGRZ
# SPDX-License-Identifier: MIT

zephyr_library_sources(led_indicator_usb.c)

zephyr_library_include_directories(${CMAKE_SOURCE_DIR}/include)

zephyr_library_sources(indicator_app.c)

add_subdirectory_ifdef(CONFIG_HW75_UART_COMM uart_comm)
88 changes: 88 additions & 0 deletions config/boards/arm/hw75_keyboard/app/indicator_app.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
/*
* Copyright (c) 2022-2023 XiNGRZ
* SPDX-License-Identifier: MIT
*/

#include <kernel.h>
#include <device.h>

#include <logging/log.h>
LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL);

#include <drivers/led_strip_remap.h>

#include <zmk/event_manager.h>
#include <zmk/events/activity_state_changed.h>

#define STRIP_LABEL DT_LABEL(DT_CHOSEN(zmk_underglow))

#define BRI_ACTIVE (255)
#define BRI_INACTIVE (10)

#define RGB(R, G, B) .r = (R), .g = (G), .b = (B)

static const struct device *led_strip;

static const struct led_rgb color_green = { RGB(0x00, 0xFF, 0x00) };

static struct led_rgb current;
static bool active = true;

static inline void apply_color(struct led_rgb *c_out, const struct led_rgb *c_in)
{
c_out->r = c_in->r;
c_out->g = c_in->g;
c_out->b = c_in->b;
}

static inline void apply_brightness(struct led_rgb *c_out, const struct led_rgb *c_in, uint8_t bri)
{
c_out->r = (uint8_t)((int)c_in->r * bri / 255);
c_out->g = (uint8_t)((int)c_in->g * bri / 255);
c_out->b = (uint8_t)((int)c_in->b * bri / 255);
}

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

apply_color(&current, &color_green); // TODO

uint8_t bri = active ? BRI_ACTIVE : BRI_INACTIVE;

struct led_rgb color;
apply_color(&color, &current);
apply_brightness(&color, &current, bri);

LOG_DBG("Update indicator, color: %02X%02X%02X, brightness: %d -> %02X%02X%02X", current.r,
current.b, current.b, bri, color.r, color.g, color.b);

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

static int indicator_app_init(const struct device *dev)
{
ARG_UNUSED(dev);
led_strip = device_get_binding(STRIP_LABEL);
return indicator_update();
}

static int indicator_app_event_listener(const zmk_event_t *eh)
{
struct zmk_activity_state_changed *activity_ev;

if ((activity_ev = as_zmk_activity_state_changed(eh)) != NULL) {
active = activity_ev->state == ZMK_ACTIVITY_ACTIVE;
return indicator_update();
}

return -ENOTSUP;
}

ZMK_LISTENER(indicator_app, indicator_app_event_listener);
ZMK_SUBSCRIPTION(indicator_app, zmk_activity_state_changed);

SYS_INIT(indicator_app_init, APPLICATION, CONFIG_APPLICATION_INIT_PRIORITY);
58 changes: 0 additions & 58 deletions config/boards/arm/hw75_keyboard/app/led_indicator_usb.c

This file was deleted.

0 comments on commit 0079aef

Please sign in to comment.