Skip to content

Commit

Permalink
Add on state to the rgb underglow transmission
Browse files Browse the repository at this point in the history
  • Loading branch information
ReFil committed Jan 14, 2025
1 parent d1a0660 commit 178f2b2
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 7 deletions.
1 change: 1 addition & 0 deletions app/include/zmk/rgb_underglow.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ struct zmk_led_hsb {
struct zmk_periph_led {
uint8_t layer;
zmk_hid_indicators_t indicators;
bool on;
};

int zmk_rgb_underglow_toggle(void);
Expand Down
1 change: 1 addition & 0 deletions app/include/zmk/split/bluetooth/service.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ struct zmk_split_input_event_payload {
struct zmk_split_update_led_data {
uint8_t layer;
uint8_t indicators;
bool on;
} __packed;
#endif

Expand Down
2 changes: 1 addition & 1 deletion app/src/behaviors/behavior_rgb_underglow.c
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ static const struct behavior_driver_api behavior_rgb_underglow_driver_api = {
on_keymap_binding_convert_central_state_dependent_params,
.binding_pressed = on_keymap_binding_pressed,
.binding_released = on_keymap_binding_released,
.locality = BEHAVIOR_LOCALITY_GLOBAL,
.locality = BEHAVIOR_LOCALITY_CENTRAL,
#if IS_ENABLED(CONFIG_ZMK_BEHAVIOR_METADATA)
.parameter_metadata = &metadata,
#endif
Expand Down
23 changes: 20 additions & 3 deletions app/src/rgb_underglow.c
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,12 @@ static struct led_rgb hsb_to_rgb(struct zmk_led_hsb hsb) {

int zmk_rgb_underglow_set_periph(struct zmk_periph_led periph) {
led_data = periph;
LOG_DBG("Update led_data %d %d", led_data.layer, led_data.indicators);
if (!state.on && led_data.on)
zmk_rgb_underglow_on();
else if (state.on && !led_data.on)
zmk_rgb_underglow_off();

LOG_DBG("Update led_data %d %d %d", led_data.layer, led_data.indicators, led_data.on);
return 0;
}

Expand Down Expand Up @@ -476,15 +481,18 @@ static int zmk_rgb_underglow_init(void) {
on : IS_ENABLED(CONFIG_ZMK_RGB_UNDERGLOW_ON_START)
};
led_data.indicators = 0;
led_data.on = IS_ENABLED(CONFIG_ZMK_RGB_UNDERGLOW_ON_START);

#if ZMK_BLE_IS_CENTRAL
k_work_init_delayable(&led_update_work, zmk_rgb_underglow_central_send);
#endif

k_work_submit_to_queue(zmk_workqueue_lowprio_work_q(), &underglow_tick_work);
zmk_rgb_underglow_off();
if (IS_ENABLED(CONFIG_ZMK_RGB_UNDERGLOW_ON_START))
if (IS_ENABLED(CONFIG_ZMK_RGB_UNDERGLOW_ON_START)) {
ext_power_enable(ext_power);
zmk_rgb_underglow_on();
}
triggered = false;
return 0;
}
Expand Down Expand Up @@ -514,6 +522,11 @@ int zmk_rgb_underglow_on(void) {
state.animation_step = 0;
k_timer_start(&underglow_tick, K_NO_WAIT, K_MSEC(50));

#if ZMK_BLE_IS_CENTRAL
led_data.on = true;
zmk_rgb_underglow_central_send();
#endif

return 0;
}

Expand Down Expand Up @@ -544,7 +557,10 @@ int zmk_rgb_underglow_off(void) {

k_timer_stop(&underglow_tick);
state.on = false;

#if ZMK_BLE_IS_CENTRAL
led_data.on = false;
zmk_rgb_underglow_central_send();
#endif
return 0;
}

Expand Down Expand Up @@ -703,6 +719,7 @@ static int rgb_underglow_event_listener(const zmk_event_t *eh) {
if (as_zmk_usb_conn_state_changed(eh)) {
led_data.indicators = zmk_hid_indicators_get_current_profile();
led_data.layer = zmk_keymap_highest_layer_active();
led_data.on = state.on;
int err = zmk_split_bt_update_led(&led_data);
if (err) {
LOG_ERR("send failed (err %d)", err);
Expand Down
4 changes: 2 additions & 2 deletions app/src/split/bluetooth/central.c
Original file line number Diff line number Diff line change
Expand Up @@ -1242,8 +1242,8 @@ static int split_bt_update_led_payload(struct zmk_split_update_led_data payload)
};

int zmk_split_bt_update_led(struct zmk_periph_led *periph) {
struct zmk_split_update_led_data payload = {.layer = periph->layer,
.indicators = periph->indicators};
struct zmk_split_update_led_data payload = {
.layer = periph->layer, .indicators = periph->indicators, .on = periph->on};

return split_bt_update_led_payload(payload);
}
Expand Down
3 changes: 2 additions & 1 deletion app/src/split/bluetooth/service.c
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,8 @@ static ssize_t split_svc_update_led(struct bt_conn *conn, const struct bt_gatt_a
// 1: We've gotten all the position/state/param data.
// 2: We have a null terminated string for the behavior device label.
if ((end_addr == sizeof(struct zmk_split_update_led_data))) {
struct zmk_periph_led periph = {.layer = payload->layer, .indicators = payload->indicators};
struct zmk_periph_led periph = {
.layer = payload->layer, .indicators = payload->indicators, .on = payload->on};
zmk_rgb_underglow_set_periph(periph);
LOG_DBG("Update leds with params %d and %d", periph.layer, periph.indicators);
}
Expand Down

0 comments on commit 178f2b2

Please sign in to comment.