From b538e605321791568aa25f0bd534ebcb30da92c2 Mon Sep 17 00:00:00 2001 From: Joel Spadin Date: Sat, 24 Oct 2020 13:39:02 -0500 Subject: feat(endpoints): update on BLE profile change Added zmk_ble_active_profile_is_connected() to allow code outside ble.c to check the status of the active profile, and changed the ble_active_profile_changed event to also notify when the active profile connects or disconnects. Changed endpoint selection to to also update when the active profile changes, connects, or disconnects. --- app/src/endpoints.c | 24 ++++++++++++++++++------ 1 file changed, 18 insertions(+), 6 deletions(-) (limited to 'app/src/endpoints.c') diff --git a/app/src/endpoints.c b/app/src/endpoints.c index 3acaba6..56204a4 100644 --- a/app/src/endpoints.c +++ b/app/src/endpoints.c @@ -4,11 +4,13 @@ * SPDX-License-Identifier: MIT */ +#include #include #include #include #include #include +#include #include #include @@ -19,8 +21,9 @@ enum endpoint { ENDPOINT_BLE, }; -static enum endpoint current_endpoint = - COND_CODE_1(IS_ENABLED(CONFIG_ZMK_BLE), (ENDPOINT_BLE), (ENDPOINT_USB)); +#define DEFAULT_ENDPOINT COND_CODE_1(IS_ENABLED(CONFIG_ZMK_BLE), (ENDPOINT_BLE), (ENDPOINT_USB)) + +static enum endpoint current_endpoint = DEFAULT_ENDPOINT; static int send_keypad_report() { struct zmk_hid_keypad_report *keypad_report = zmk_hid_get_keypad_report(); @@ -115,15 +118,22 @@ static bool is_ble_ready() { static enum endpoint get_selected_endpoint() { if (is_ble_ready()) { if (is_usb_ready()) { - LOG_DBG("Both endpoints ready. Selecting USB"); + LOG_DBG("Both endpoints are ready."); // TODO: add user setting to control this return ENDPOINT_USB; } + LOG_DBG("Only BLE is ready."); return ENDPOINT_BLE; } - return ENDPOINT_USB; + if (is_usb_ready()) { + LOG_DBG("Only USB is ready."); + return ENDPOINT_USB; + } + + LOG_DBG("No endpoints are ready."); + return DEFAULT_ENDPOINT; } static void disconnect_current_endpoint() { @@ -149,7 +159,9 @@ static int endpoint_listener(const struct zmk_event_header *eh) { } ZMK_LISTENER(endpoint_listener, endpoint_listener); -#if IS_ENABLED(CONFIG_USB) +#if IS_ENABLED(CONFIG_ZMK_USB) ZMK_SUBSCRIPTION(endpoint_listener, usb_conn_state_changed); #endif -// TODO: add BLE state subscription \ No newline at end of file +#if IS_ENABLED(CONFIG_ZMK_BLE) +ZMK_SUBSCRIPTION(endpoint_listener, ble_active_profile_changed); +#endif \ No newline at end of file -- cgit v1.2.3