diff --git a/boards/shields/helios/CMakeLists.txt b/boards/shields/helios/CMakeLists.txt new file mode 100644 index 0000000..26ec3b0 --- /dev/null +++ b/boards/shields/helios/CMakeLists.txt @@ -0,0 +1 @@ +target_sources(app PRIVATE leds.c) diff --git a/boards/shields/helios/helios_layout.dtsi b/boards/shields/helios/helios-layout.dtsi similarity index 100% rename from boards/shields/helios/helios_layout.dtsi rename to boards/shields/helios/helios-layout.dtsi diff --git a/boards/shields/helios/helios.conf b/boards/shields/helios/helios.conf index 0947584..0c33fb1 100644 --- a/boards/shields/helios/helios.conf +++ b/boards/shields/helios/helios.conf @@ -1 +1,3 @@ -CONFIG_ZMK_BACKLIGHT=y +CONFIG_ZMK_STUDIO=y +CONFIG_ZMK_HID_INDICATORS=y +CONFIG_LED=y diff --git a/boards/shields/helios/helios.overlay b/boards/shields/helios/helios.overlay index 784a106..4a460b3 100644 --- a/boards/shields/helios/helios.overlay +++ b/boards/shields/helios/helios.overlay @@ -1,7 +1,6 @@ -#include #include -#include "helios_layout.dtsi" +#include "helios-layout.dtsi" &helios_layout { transform = <&default_transform>; @@ -10,23 +9,31 @@ / { chosen { zmk,kscan = &kscan0; - zmk,backlight = &backlight; + zmk,physical-layout = &helios_layout; zephyr,code-partition = &code_partition; }; - backlight: gpioleds { + aliases { + led-caps = &led_0; + led-num = &led_1; + led-scroll = &led_2; + led-layer = &led_3; + }; + + leds { compatible = "gpio-leds"; - gpio_led_0 { - gpios = <&blackpill 46 GPIO_ACTIVE_LOW>; + status = "okay"; + led_0: led_0 { + gpios = <&blackpill 46 (GPIO_ACTIVE_LOW | GPIO_PULL_UP)>; }; - gpio_led_1 { - gpios = <&blackpill 10 GPIO_ACTIVE_LOW>; + led_1: led_1 { + gpios = <&blackpill 10 (GPIO_ACTIVE_LOW | GPIO_PULL_UP)>; }; - gpio_led_2 { - gpios = <&blackpill 11 GPIO_ACTIVE_LOW>; + led_2: led_2 { + gpios = <&blackpill 11 (GPIO_ACTIVE_LOW | GPIO_PULL_UP)>; }; - gpio_led_3 { - gpios = <&blackpill 12 GPIO_ACTIVE_LOW>; + led_3: led_3 { + gpios = <&blackpill 12 (GPIO_ACTIVE_LOW | GPIO_PULL_UP)>; }; }; diff --git a/boards/shields/helios/helios.zmk.yml b/boards/shields/helios/helios.zmk.yml index 60537af..51a7fe9 100644 --- a/boards/shields/helios/helios.zmk.yml +++ b/boards/shields/helios/helios.zmk.yml @@ -8,3 +8,4 @@ requires: features: - keys - backlight + - studio diff --git a/boards/shields/helios/leds.c b/boards/shields/helios/leds.c new file mode 100644 index 0000000..80ab714 --- /dev/null +++ b/boards/shields/helios/leds.c @@ -0,0 +1,71 @@ +#include +#include +#include +#include +#include +#include +#include +#include + +#define LED_GPIO_NODE_ID DT_COMPAT_GET_ANY_STATUS_OKAY(gpio_leds) + +// GPIO-based LED device +static const struct device *led_dev = DEVICE_DT_GET(LED_GPIO_NODE_ID); + +static int led_keylock_listener_cb(const zmk_event_t *eh) { + zmk_hid_indicators_t flags = zmk_hid_indicators_get_current_profile(); + unsigned int capsBit = 1 << (HID_USAGE_LED_CAPS_LOCK - 1); + unsigned int numBit = 1 << (HID_USAGE_LED_NUM_LOCK - 1); + unsigned int scrollBit = 1 << (HID_USAGE_LED_SCROLL_LOCK - 1); + + if (flags & capsBit) { + led_on(led_dev, DT_NODE_CHILD_IDX(DT_ALIAS(led_caps))); + } else { + led_off(led_dev, DT_NODE_CHILD_IDX(DT_ALIAS(led_caps))); + } + + if (flags & numBit) { + led_on(led_dev, DT_NODE_CHILD_IDX(DT_ALIAS(led_num))); + } else { + led_off(led_dev, DT_NODE_CHILD_IDX(DT_ALIAS(led_num))); + } + + if (flags & scrollBit) { + led_on(led_dev, DT_NODE_CHILD_IDX(DT_ALIAS(led_scroll))); + } else { + led_off(led_dev, DT_NODE_CHILD_IDX(DT_ALIAS(led_scroll))); + } + + return 0; +} + +ZMK_LISTENER(led_indicators_listener, led_keylock_listener_cb); +ZMK_SUBSCRIPTION(led_indicators_listener, zmk_hid_indicators_changed); + +// Layer state listener for layer #1 LED +static int led_layer_listener_cb(const zmk_event_t *eh) { + const struct zmk_layer_state_changed *ev = as_zmk_layer_state_changed(eh); + + if (ev->layer == 1) { // Check if the event is for layer #1 + if (ev->state) { + led_on(led_dev, DT_NODE_CHILD_IDX(DT_ALIAS(led_layer))); + } else { + led_off(led_dev, DT_NODE_CHILD_IDX(DT_ALIAS(led_layer))); + } + } + return 0; +} + +ZMK_LISTENER(layer_led_listener, led_layer_listener_cb); +ZMK_SUBSCRIPTION(layer_led_listener, zmk_layer_state_changed); + +static int leds_init(const struct device *device) { + if (!device_is_ready(led_dev)) { + return -ENODEV; + } + + return 0; +} + +// run leds_init on boot +SYS_INIT(leds_init, APPLICATION, CONFIG_APPLICATION_INIT_PRIORITY);