99#include <zmk/endpoints.h>
1010#include <zmk/events/battery_state_changed.h>
1111#include <zmk/events/ble_active_profile_changed.h>
12+ #include <zmk/events/endpoint_changed.h>
1213#include <zmk/events/layer_state_changed.h>
1314#include <zmk/events/split_peripheral_status_changed.h>
1415#include <zmk/events/activity_state_changed.h>
@@ -118,23 +119,34 @@ static void set_rgb_leds(uint8_t color, uint16_t duration_ms) {
118119// separate thread
119120K_MSGQ_DEFINE (led_msgq , sizeof (struct blink_item ), 16 , 1 );
120121
121- #if IS_ENABLED (CONFIG_ZMK_BLE )
122- void indicate_connectivity (void ) {
122+ static void indicate_connectivity_internal (void ) {
123123 struct blink_item blink = {.duration_ms = CONFIG_RGBLED_WIDGET_CONN_BLINK_MS };
124124
125125#if !IS_ENABLED (CONFIG_ZMK_SPLIT ) || IS_ENABLED (CONFIG_ZMK_SPLIT_ROLE_CENTRAL )
126- uint8_t profile_index = zmk_ble_active_profile_index ();
127- if (zmk_ble_active_profile_is_connected ()) {
128- LOG_CONN_CENTRAL (profile_index , "connected" , CONNECTED );
129- blink .color = CONFIG_RGBLED_WIDGET_CONN_COLOR_CONNECTED ;
130- } else if (zmk_ble_active_profile_is_open ()) {
131- LOG_CONN_CENTRAL (profile_index , "open" , ADVERTISING );
132- blink .color = CONFIG_RGBLED_WIDGET_CONN_COLOR_ADVERTISING ;
133- } else {
134- LOG_CONN_CENTRAL (profile_index , "not connected" , DISCONNECTED );
135- blink .color = CONFIG_RGBLED_WIDGET_CONN_COLOR_DISCONNECTED ;
126+ switch (zmk_endpoints_selected ().transport ) {
127+ case ZMK_TRANSPORT_USB :
128+ #if IS_ENABLED (CONFIG_RGBLED_WIDGET_CONN_SHOW_USB )
129+ LOG_INF ("USB connected, blinking %s" , color_names [CONFIG_RGBLED_WIDGET_CONN_COLOR_USB ]);
130+ blink .color = CONFIG_RGBLED_WIDGET_CONN_COLOR_USB ;
131+ break ;
132+ #endif
133+ default : // ZMK_TRANSPORT_BLE
134+ #if IS_ENABLED (CONFIG_ZMK_BLE )
135+ uint8_t profile_index = zmk_ble_active_profile_index ();
136+ if (zmk_ble_active_profile_is_connected ()) {
137+ LOG_CONN_CENTRAL (profile_index , "connected" , CONNECTED );
138+ blink .color = CONFIG_RGBLED_WIDGET_CONN_COLOR_CONNECTED ;
139+ } else if (zmk_ble_active_profile_is_open ()) {
140+ LOG_CONN_CENTRAL (profile_index , "open" , ADVERTISING );
141+ blink .color = CONFIG_RGBLED_WIDGET_CONN_COLOR_ADVERTISING ;
142+ } else {
143+ LOG_CONN_CENTRAL (profile_index , "not connected" , DISCONNECTED );
144+ blink .color = CONFIG_RGBLED_WIDGET_CONN_COLOR_DISCONNECTED ;
145+ }
146+ #endif
147+ break ;
136148 }
137- #else
149+ #elif IS_ENABLED ( CONFIG_ZMK_SPLIT_BLE )
138150 if (zmk_split_bt_peripheral_is_connected ()) {
139151 LOG_CONN_PERIPHERAL ("connected" , CONNECTED );
140152 blink .color = CONFIG_RGBLED_WIDGET_CONN_COLOR_CONNECTED ;
@@ -154,15 +166,22 @@ static int led_output_listener_cb(const zmk_event_t *eh) {
154166 return 0 ;
155167}
156168
169+ // debouncing to ignore all but last connectivity event, to prevent repeat blinks
170+ static struct k_work_delayable indicate_connectivity_work ;
171+ static void indicate_connectivity_cb (struct k_work * work ) { indicate_connectivity_internal (); }
172+ void indicate_connectivity () { k_work_reschedule (& indicate_connectivity_work , K_MSEC (16 )); }
173+
157174ZMK_LISTENER (led_output_listener , led_output_listener_cb );
158175#if !IS_ENABLED (CONFIG_ZMK_SPLIT ) || IS_ENABLED (CONFIG_ZMK_SPLIT_ROLE_CENTRAL )
159- // run led_output_listener_cb on BLE profile change (on central)
176+ // run led_output_listener_cb on endpoint and BLE profile change (on central)
177+ ZMK_SUBSCRIPTION (led_output_listener , zmk_endpoint_changed );
178+ #if IS_ENABLED (CONFIG_ZMK_BLE )
160179ZMK_SUBSCRIPTION (led_output_listener , zmk_ble_active_profile_changed );
161- #else
180+ #endif // IS_ENABLED(CONFIG_ZMK_BLE)
181+ #elif IS_ENABLED (CONFIG_ZMK_SPLIT_BLE )
162182// run led_output_listener_cb on peripheral status change event
163183ZMK_SUBSCRIPTION (led_output_listener , zmk_split_peripheral_status_changed );
164184#endif
165- #endif // IS_ENABLED(CONFIG_ZMK_BLE)
166185
167186#if IS_ENABLED (CONFIG_ZMK_BATTERY_REPORTING )
168187static inline uint8_t get_battery_color (uint8_t battery_level ) {
@@ -277,7 +296,7 @@ static int led_layer_color_listener_cb(const zmk_event_t *eh) {
277296 LOG_INF ("Detected sleep activity state, turn off LED" );
278297 set_rgb_leds (0 , 0 );
279298 break ;
280- default : // not handling IDLE and ACTIVE yet
299+ default : // not handling IDLE and ACTIVE yet
281300 break ;
282301 }
283302 return 0 ;
@@ -339,6 +358,8 @@ extern void led_process_thread(void *d0, void *d1, void *d2) {
339358 ARG_UNUSED (d1 );
340359 ARG_UNUSED (d2 );
341360
361+ k_work_init_delayable (& indicate_connectivity_work , indicate_connectivity_cb );
362+
342363#if SHOW_LAYER_CHANGE
343364 k_work_init_delayable (& layer_indicate_work , indicate_layer_cb );
344365#endif
@@ -390,11 +411,9 @@ extern void led_init_thread(void *d0, void *d1, void *d2) {
390411 k_sleep (K_MSEC (CONFIG_RGBLED_WIDGET_BATTERY_BLINK_MS + CONFIG_RGBLED_WIDGET_INTERVAL_MS ));
391412#endif // IS_ENABLED(CONFIG_ZMK_BATTERY_REPORTING)
392413
393- #if IS_ENABLED (CONFIG_ZMK_BLE )
394414 // check and indicate current profile or peripheral connectivity status
395415 LOG_INF ("Indicating initial connectivity status" );
396416 indicate_connectivity ();
397- #endif // IS_ENABLED(CONFIG_ZMK_BLE)
398417
399418#if SHOW_LAYER_COLORS
400419 LOG_INF ("Setting initial layer color" );
0 commit comments