Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(usb): detect USB power mode to fallback to BLE #2458

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions app/include/zmk/usb.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,4 @@ enum zmk_usb_conn_state zmk_usb_get_conn_state(void);
static inline bool zmk_usb_is_powered(void) {
return zmk_usb_get_conn_state() != ZMK_USB_CONN_NONE;
}
static inline bool zmk_usb_is_hid_ready(void) {
return zmk_usb_get_conn_state() == ZMK_USB_CONN_HID;
}
bool zmk_usb_is_hid_ready(void);
10 changes: 10 additions & 0 deletions app/src/usb.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL);

static enum usb_dc_status_code usb_status = USB_DC_UNKNOWN;
static bool is_configured;

static void raise_usb_status_changed_event(struct k_work *_work) {
raise_zmk_usb_conn_state_changed(
Expand Down Expand Up @@ -49,6 +50,10 @@ enum zmk_usb_conn_state zmk_usb_get_conn_state(void) {
}
}

bool zmk_usb_is_hid_ready(void) {
return zmk_usb_get_conn_state() == ZMK_USB_CONN_HID && is_configured;
}

void usb_status_cb(enum usb_dc_status_code status, const uint8_t *params) {
// Start-of-frame events are too frequent and noisy to notify, and they're
// not used within ZMK
Expand All @@ -62,6 +67,11 @@ void usb_status_cb(enum usb_dc_status_code status, const uint8_t *params) {
}
#endif
usb_status = status;
if (zmk_usb_get_conn_state() == ZMK_USB_CONN_HID) {
is_configured |= usb_status == USB_DC_CONFIGURED;
} else {
is_configured = false;
}
k_work_submit(&usb_status_notifier_work);
};

Expand Down