Skip to content

Commit

Permalink
underglow-layer: use behaviors to control RGBs
Browse files Browse the repository at this point in the history
  • Loading branch information
darknao committed Jan 5, 2025
1 parent b742e72 commit 15505c8
Show file tree
Hide file tree
Showing 20 changed files with 319 additions and 26 deletions.
3 changes: 3 additions & 0 deletions app/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,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_RGB_UNDERGLOW app PRIVATE src/behaviors/behavior_underglow_color.c)
target_sources_ifdef(CONFIG_ZMK_RGB_UNDERGLOW app PRIVATE src/behaviors/behavior_underglow_indicators.c)
target_sources_ifdef(CONFIG_ZMK_RGB_UNDERGLOW app PRIVATE src/events/underglow_color_changed.c)
target_sources_ifdef(CONFIG_ZMK_BACKLIGHT app PRIVATE src/behaviors/behavior_backlight.c)

target_sources_ifdef(CONFIG_ZMK_BATTERY_REPORTING app PRIVATE src/events/battery_state_changed.c)
Expand Down
8 changes: 8 additions & 0 deletions app/boards/arm/glove80/glove80_lh.dts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,14 @@
zmk,underglow-indicators = &underglow_indicators;
};

underglow-layer {
compatible = "zmk,underglow-layer";
pixel-lookup = <52>, <53>, <54>, <69>, <70>, <71>, <15>, <27>, <39>, <51>, <4>, <14>, <26>, <38>,
<50>, <68>, <3>, <13>, <25>, <37>, <49>, <67>, <2>, <12>, <24>, <36>, <48>, <66>,
<1>, <11>, <23>, <35>, <47>, <65>, <0>, <10>, <22>, <34>, <46>, <64>;
};


back_led_backlight: pwmleds {
compatible = "pwm-leds";
pwm_led_0 {
Expand Down
3 changes: 3 additions & 0 deletions app/boards/arm/glove80/glove80_lh_defconfig
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,9 @@ CONFIG_ZMK_HID_CONSUMER_REPORT_USAGES_BASIC=y
CONFIG_ZMK_USB_BOOT=y
CONFIG_ZMK_HID_INDICATORS=y

# Send HID indicator to peripherals
CONFIG_ZMK_SPLIT_PERIPHERAL_HID_INDICATORS=y

# Turn on debugging to disable optimization. Debug messages can result in larger
# stacks, so enable stack protection and particularly a larger BLE peripheral stack.
# CONFIG_DEBUG=y
Expand Down
8 changes: 8 additions & 0 deletions app/boards/arm/glove80/glove80_rh.dts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,14 @@
zmk,battery = &vbatt;
};

underglow-layer {
compatible = "zmk,underglow-layer";
pixel-lookup = <57>, <56>, <55>, <74>, <73>, <72>, <16>, <28>, <40>, <58>, <5>, <17>, <29>, <41>,
<59>, <75>, <6>, <18>, <30>, <42>, <60>, <76>, <7>, <19>, <31>, <43>, <61>, <77>,
<8>, <20>, <32>, <44>, <62>, <78>, <9>, <21>, <33>, <45>, <63>, <79>;
};


back_led_backlight: pwmleds {
compatible = "pwm-leds";
pwm_led_0 {
Expand Down
4 changes: 4 additions & 0 deletions app/boards/arm/glove80/glove80_rh_defconfig
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,10 @@ CONFIG_ZMK_RGB_UNDERGLOW_HUE_START=285
CONFIG_ZMK_RGB_UNDERGLOW_SAT_START=75
CONFIG_ZMK_RGB_UNDERGLOW_BRT_START=16

# Enable HID indicators on peripheral
CONFIG_ZMK_HID_INDICATORS=y
CONFIG_ZMK_SPLIT_PERIPHERAL_HID_INDICATORS=y

# The power LED is implemented as a backlight
# For now, the power LED is acting as a "USB connected" indicator
CONFIG_ZMK_BACKLIGHT=y
Expand Down
2 changes: 2 additions & 0 deletions app/dts/behaviors.dtsi
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,5 @@
#include <behaviors/soft_off.dtsi>
#include <behaviors/studio_unlock.dtsi>
#include <behaviors/mouse_keys.dtsi>
#include <behaviors/ug_color.dtsi>
#include <behaviors/ug_indicators.dtsi>
15 changes: 15 additions & 0 deletions app/dts/behaviors/ug_color.dtsi
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/*
* Copyright (c) 2024 The ZMK Contributors
*
* SPDX-License-Identifier: MIT
*/

/ {
behaviors {
ug: ugcolor {
compatible = "zmk,behavior-underglow-color";
#binding-cells = <1>;
display-name = "Underglow Color";
};
};
};
33 changes: 33 additions & 0 deletions app/dts/behaviors/ug_indicators.dtsi
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* Copyright (c) 2024 The ZMK Contributors
*
* SPDX-License-Identifier: MIT
*/

#include <dt-bindings/zmk/hid_indicators.h>

/ {
behaviors {
ug_nl: ugnumlk {
compatible = "zmk,behavior-underglow-indicators";
indicator = <NUM_LOCK>;
#binding-cells = <2>;
display-name = "Underglow NumLock indicator";
};

ug_cl: ugcapslk {
compatible = "zmk,behavior-underglow-indicators";
indicator = <CAPS_LOCK>;
#binding-cells = <2>;
display-name = "Underglow CapsLock indicator";
};

ug_sl: ugscrllk {
compatible = "zmk,behavior-underglow-indicators";
indicator = <SCROLL_LOCK>;
#binding-cells = <2>;
display-name = "Underglow ScrollLock indicator";
};

};
};
8 changes: 8 additions & 0 deletions app/dts/bindings/behaviors/zmk,behavior-underglow-color.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Copyright (c) 2024, The ZMK Contributors
# SPDX-License-Identifier: MIT

description: Set underglow to specified color

compatible: "zmk,behavior-underglow-color"

include: one_param.yaml
13 changes: 13 additions & 0 deletions app/dts/bindings/behaviors/zmk,behavior-underglow-indicators.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Copyright (c) 2024, The ZMK Contributors
# SPDX-License-Identifier: MIT

description: Set underglow for num lock indicator

compatible: "zmk,behavior-underglow-indicators"

include: two_param.yaml

properties:
indicator:
type: int
default: 0
7 changes: 6 additions & 1 deletion app/dts/bindings/zmk,underglow-layer.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,17 @@ description: |
compatible: "zmk,underglow-layer"

properties:
pixel-lookup:
type: array
required: true

child-binding:
description: "A layer to be used in a rgbmap"

properties:
bindings:
type: array
type: phandle-array
required: true
layer-id:
type: int
Expand Down
9 changes: 9 additions & 0 deletions app/include/dt-bindings/zmk/hid_indicators.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/*
* Copyright (c) 2020 The ZMK Contributors
*
* SPDX-License-Identifier: MIT
*/

#define NUM_LOCK 0
#define CAPS_LOCK 1
#define SCROLL_LOCK 2
3 changes: 2 additions & 1 deletion app/include/dt-bindings/zmk/rgb_colors.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,5 @@
#define PURPLE 0x800080
#define PINK 0xffc0cb
#define WHITE 0xffffff
#define ______ 0x000000
#define ___ 0x000000
#define BLACK 0x000000
13 changes: 13 additions & 0 deletions app/include/zmk/events/underglow_color_changed.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/*
* Copyright (c) 2024 The ZMK Contributors
*
* SPDX-License-Identifier: MIT
*/

#pragma once

#include <zmk/event_manager.h>

struct zmk_underglow_color_changed {};

ZMK_EVENT_DECLARE(zmk_underglow_color_changed);
13 changes: 12 additions & 1 deletion app/include/zmk/rgb_underglow_layer.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,18 @@
#define ZMK_RGBMAP_LAYERS_LEN \
(DT_FOREACH_CHILD(DT_INST(0, zmk_underglow_layer), ZMK_RGB_CHILD_LEN_PLUS_ONE) 0)

#define ZMK_RGBMAP_EXTRACT_BINDING(idx, drv_inst) \
{ \
.behavior_dev = DEVICE_DT_NAME(DT_PHANDLE_BY_IDX(drv_inst, bindings, idx)), \
.param1 = COND_CODE_0(DT_PHA_HAS_CELL_AT_IDX(drv_inst, bindings, idx, param1), (0), \
(DT_PHA_BY_IDX(drv_inst, bindings, idx, param1))), \
.param2 = COND_CODE_0(DT_PHA_HAS_CELL_AT_IDX(drv_inst, bindings, idx, param2), (0), \
(DT_PHA_BY_IDX(drv_inst, bindings, idx, param2))), \
}

const int rgb_pixel_lookup(int idx);
const int zmk_rgbmap_id(uint8_t layer);
uint32_t *rgb_underglow_get_bindings(uint8_t layer);
const struct zmk_behavior_binding *rgb_underglow_get_bindings(uint8_t layer);

uint8_t rgb_underglow_top_layer_with_state(uint32_t state_to_test);
uint8_t rgb_underglow_top_layer(void);
39 changes: 39 additions & 0 deletions app/src/behaviors/behavior_underglow_color.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* Copyright (c) 2024 The ZMK Contributors
*
* SPDX-License-Identifier: MIT
*/

#define DT_DRV_COMPAT zmk_behavior_underglow_color

// Dependencies
#include <zephyr/device.h>
#include <drivers/behavior.h>
#include <zephyr/logging/log.h>

LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL);

#if DT_HAS_COMPAT_STATUS_OKAY(DT_DRV_COMPAT)

// Initialization Function
static int underglow_color_init(const struct device *dev) { return 0; };

static int underglow_color_process(struct zmk_behavior_binding *binding,
struct zmk_behavior_binding_event event) {
return binding->param1;
}

// API Structure
static const struct behavior_driver_api underglow_color_driver_api = {
.binding_pressed = underglow_color_process,
.locality = BEHAVIOR_LOCALITY_GLOBAL,
#if IS_ENABLED(CONFIG_ZMK_BEHAVIOR_METADATA)
.get_parameter_metadata = zmk_behavior_get_empty_param_metadata,
#endif // IS_ENABLED(CONFIG_ZMK_BEHAVIOR_METADATA)

};

BEHAVIOR_DT_INST_DEFINE(0, underglow_color_init, NULL, NULL, NULL, POST_KERNEL,
CONFIG_KERNEL_INIT_PRIORITY_DEFAULT, &underglow_color_driver_api);

#endif /* DT_HAS_COMPAT_STATUS_OKAY(DT_DRV_COMPAT) */
77 changes: 77 additions & 0 deletions app/src/behaviors/behavior_underglow_indicators.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/*
* Copyright (c) 2024 The ZMK Contributors
*
* SPDX-License-Identifier: MIT
*/

#define DT_DRV_COMPAT zmk_behavior_underglow_indicators

// Dependencies
#include <zephyr/device.h>
#include <drivers/behavior.h>
#include <zephyr/logging/log.h>
#include <zmk/hid_indicators.h>
#include <zmk/event_manager.h>
#include <zmk/events/hid_indicators_changed.h>
#include <zmk/events/underglow_color_changed.h>

LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL);

#if DT_HAS_COMPAT_STATUS_OKAY(DT_DRV_COMPAT)

struct underglow_indicators_data {
zmk_hid_indicators_t indicators;
};

struct underglow_indicators_config {
int indicator;
};

static int underglow_indicators_init(const struct device *dev) { return 0; };

static int underglow_indicators_process(struct zmk_behavior_binding *binding,
struct zmk_behavior_binding_event event) {
const struct device *dev = zmk_behavior_get_binding(binding->behavior_dev);
struct underglow_indicators_data *data = dev->data;
struct underglow_indicators_config *config = dev->config;

if (data->indicators & BIT(config->indicator))
return binding->param2;
else
return binding->param1;
}

static const struct behavior_driver_api underglow_indicators_driver_api = {
.binding_pressed = underglow_indicators_process,
.locality = BEHAVIOR_LOCALITY_GLOBAL,
#if IS_ENABLED(CONFIG_ZMK_BEHAVIOR_METADATA)
.get_parameter_metadata = zmk_behavior_get_empty_param_metadata,
#endif // IS_ENABLED(CONFIG_ZMK_BEHAVIOR_METADATA)
};

static int underglow_indicators_listener(const zmk_event_t *eh);

ZMK_LISTENER(behavior_underglow_indicators, underglow_indicators_listener);
ZMK_SUBSCRIPTION(behavior_underglow_indicators, zmk_hid_indicators_changed);

static struct underglow_indicators_data underglow_indicators_data = {.indicators = 0};

static int underglow_indicators_listener(const zmk_event_t *eh) {
const struct zmk_hid_indicators_changed *ev = as_zmk_hid_indicators_changed(eh);
underglow_indicators_data.indicators = ev->indicators;
raise_zmk_underglow_color_changed((struct zmk_underglow_color_changed){});

return ZMK_EV_EVENT_BUBBLE;
}

#define KP_INST(n) \
static struct underglow_indicators_config underglow_indicators_config_##n = { \
.indicator = DT_INST_PROP(n, indicator)}; \
BEHAVIOR_DT_INST_DEFINE(n, underglow_indicators_init, NULL, &underglow_indicators_data, \
&underglow_indicators_config_##n, POST_KERNEL, \
CONFIG_KERNEL_INIT_PRIORITY_DEFAULT, \
&underglow_indicators_driver_api);

DT_INST_FOREACH_STATUS_OKAY(KP_INST)

#endif /* DT_HAS_COMPAT_STATUS_OKAY(DT_DRV_COMPAT) */
10 changes: 10 additions & 0 deletions app/src/events/underglow_color_changed.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/*
* Copyright (c) 2020 The ZMK Contributors
*
* SPDX-License-Identifier: MIT
*/

#include <zephyr/kernel.h>
#include <zmk/events/underglow_color_changed.h>

ZMK_EVENT_IMPL(zmk_underglow_color_changed);
Loading

0 comments on commit 15505c8

Please sign in to comment.