Skip to content

Commit 0efe481

Browse files
committed
Add flag to indicate USB endpoint instead of BLE status if it is active
Fixes #13
1 parent c12cf5c commit 0efe481

4 files changed

Lines changed: 56 additions & 26 deletions

File tree

Kconfig

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,9 @@ config RGBLED_WIDGET_CONN_BLINK_MS
8686
int "Duration of BLE connection status blink in ms"
8787
default RGBLED_WIDGET_OUTPUT_BLINK_MS
8888

89+
config RGBLED_WIDGET_CONN_SHOW_USB
90+
bool "Show USB indicator instead of BLE status if it has priority"
91+
8992
config RGBLED_WIDGET_OUTPUT_BLINK_MS # DEPRECATED, do not use
9093
int
9194
default 1000
@@ -105,6 +108,11 @@ config RGBLED_WIDGET_CONN_COLOR_DISCONNECTED
105108
range 0 7
106109
default $(COLOR_RED)
107110

111+
config RGBLED_WIDGET_CONN_COLOR_USB
112+
int "Color for USB endpoint active"
113+
range 0 7
114+
default $(COLOR_CYAN)
115+
108116
# Layer indicator settings
109117
config RGBLED_WIDGET_SHOW_LAYER_CHANGE
110118
bool "Indicate highest active layer on each layer change with a sequence of blinks"

README.md

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ It is used to indicate battery level and BLE connection status in a minimalist w
2121
### Connection status
2222

2323
- Blink 🔵 for connected, 🟡 for open (advertising), 🔴 for disconnected profiles on boot after the battery blink, and following every BT profile switch (only on central side for splits)
24+
- Enable `CONFIG_RGBLED_WIDGET_CONN_SHOW_USB` to blink cyan whenever USB has priority over BLE, instead of above
2425
- Blink 🔵 for connected, 🔴 for disconnected on peripheral side of splits
2526

2627
### Layer state
@@ -156,12 +157,14 @@ The non-default ones (second and third below) only work on central parts of spli
156157
<details>
157158
<summary>Connectivity-related</summary>
158159

159-
| Name | Description | Default |
160-
| ---------------------------------------------- | --------------------------------------------- | ------------ |
161-
| `CONFIG_RGBLED_WIDGET_CONN_BLINK_MS` | Duration of BLE connection status blink in ms | 1000 |
162-
| `CONFIG_RGBLED_WIDGET_CONN_COLOR_CONNECTED` | Color for connected BLE connection status | Blue (`4`) |
163-
| `CONFIG_RGBLED_WIDGET_CONN_COLOR_ADVERTISING` | Color for advertising BLE connection status | Yellow (`3`) |
164-
| `CONFIG_RGBLED_WIDGET_CONN_COLOR_DISCONNECTED` | Color for disconnected BLE connection status | Red (`1`) |
160+
| Name | Description | Default |
161+
| ---------------------------------------------- | ----------------------------------------------------------- | ------------ |
162+
| `CONFIG_RGBLED_WIDGET_CONN_BLINK_MS` | Duration of BLE connection status blink in ms | 1000 |
163+
| `CONFIG_RGBLED_WIDGET_CONN_SHOW_USB` | Show USB indicator instead of BLE status if it has priority | `n` |
164+
| `CONFIG_RGBLED_WIDGET_CONN_COLOR_CONNECTED` | Color for connected BLE connection status | Blue (`4`) |
165+
| `CONFIG_RGBLED_WIDGET_CONN_COLOR_ADVERTISING` | Color for advertising BLE connection status | Yellow (`3`) |
166+
| `CONFIG_RGBLED_WIDGET_CONN_COLOR_DISCONNECTED` | Color for disconnected BLE connection status | Red (`1`) |
167+
| `CONFIG_RGBLED_WIDGET_CONN_COLOR_USB` | Color for USB endpoint active | Cyan (`6`) |
165168

166169
</details>
167170

src/behaviors/behavior_rgbled_widget.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ static int on_keymap_binding_pressed(struct zmk_behavior_binding *binding,
2929
indicate_battery();
3030
}
3131
#endif
32-
#if IS_ENABLED(CONFIG_ZMK_BLE)
32+
#if IS_ENABLED(CONFIG_ZMK_USB) || IS_ENABLED(CONFIG_ZMK_BLE)
3333
if (cfg->indicate_connectivity) {
3434
indicate_connectivity();
3535
}

src/widget.c

Lines changed: 38 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
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
119120
K_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+
157174
ZMK_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)
160179
ZMK_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
163183
ZMK_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)
168187
static 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

Comments
 (0)