From 691598206460deebc94f956e3da152341d8c46a4 Mon Sep 17 00:00:00 2001 From: William Lindholm Date: Mon, 23 Sep 2024 17:59:08 +0200 Subject: [PATCH] feat: Add support for switching to previously used bluetooth profile --- app/include/dt-bindings/zmk/bt.h | 8 +++++--- app/include/zmk/ble.h | 1 + app/src/behaviors/behavior_bt.c | 7 +++++++ app/src/ble.c | 7 +++++++ docs/docs/keymaps/behaviors/bluetooth.md | 9 ++++++++- 5 files changed, 28 insertions(+), 4 deletions(-) diff --git a/app/include/dt-bindings/zmk/bt.h b/app/include/dt-bindings/zmk/bt.h index aaad4dc5b8b..74c9bbb3547 100644 --- a/app/include/dt-bindings/zmk/bt.h +++ b/app/include/dt-bindings/zmk/bt.h @@ -7,9 +7,10 @@ #define BT_CLR_CMD 0 #define BT_NXT_CMD 1 #define BT_PRV_CMD 2 -#define BT_SEL_CMD 3 -#define BT_CLR_ALL_CMD 4 -#define BT_DISC_CMD 5 +#define BT_PRV_DEV_CMD 3 +#define BT_SEL_CMD 4 +#define BT_CLR_ALL_CMD 5 +#define BT_DISC_CMD 6 /* Note: Some future commands will include additional parameters, so we @@ -19,6 +20,7 @@ defines these aliases up front. #define BT_CLR BT_CLR_CMD 0 #define BT_NXT BT_NXT_CMD 0 #define BT_PRV BT_PRV_CMD 0 +#define BT_PRV_DEV BT_PRV_DEV_CMD 0 #define BT_SEL BT_SEL_CMD #define BT_CLR_ALL BT_CLR_ALL_CMD 0 #define BT_DISC BT_DISC_CMD diff --git a/app/include/zmk/ble.h b/app/include/zmk/ble.h index cc55a6ce142..c4f6739e5a3 100644 --- a/app/include/zmk/ble.h +++ b/app/include/zmk/ble.h @@ -23,6 +23,7 @@ void zmk_ble_clear_bonds(void); int zmk_ble_prof_next(void); int zmk_ble_prof_prev(void); +int zmk_ble_prof_prev_dev(void); int zmk_ble_prof_select(uint8_t index); void zmk_ble_clear_all_bonds(void); int zmk_ble_prof_disconnect(uint8_t index); diff --git a/app/src/behaviors/behavior_bt.c b/app/src/behaviors/behavior_bt.c index f439e49b1cf..bfc3266417c 100644 --- a/app/src/behaviors/behavior_bt.c +++ b/app/src/behaviors/behavior_bt.c @@ -33,6 +33,11 @@ static const struct behavior_parameter_value_metadata no_arg_values[] = { .type = BEHAVIOR_PARAMETER_VALUE_TYPE_VALUE, .value = BT_PRV_CMD, }, + { + .display_name = "Previously Used Profile", + .type = BEHAVIOR_PARAMETER_VALUE_TYPE_VALUE, + .value = BT_PRV_DEV_CMD, + }, { .display_name = "Clear All Profiles", .type = BEHAVIOR_PARAMETER_VALUE_TYPE_VALUE, @@ -98,6 +103,8 @@ static int on_keymap_binding_pressed(struct zmk_behavior_binding *binding, return zmk_ble_prof_next(); case BT_PRV_CMD: return zmk_ble_prof_prev(); + case BT_PRV_DEV_CMD: + return zmk_ble_prof_prev_dev(); case BT_SEL_CMD: return zmk_ble_prof_select(binding->param2); case BT_CLR_ALL_CMD: diff --git a/app/src/ble.c b/app/src/ble.c index 9ecfb7ec394..faba9c8e7db 100644 --- a/app/src/ble.c +++ b/app/src/ble.c @@ -61,6 +61,7 @@ enum advertising_type { static struct zmk_ble_profile profiles[ZMK_BLE_PROFILE_COUNT]; static uint8_t active_profile; +static uint8_t previous_profile = 0; #define DEVICE_NAME CONFIG_BT_DEVICE_NAME #define DEVICE_NAME_LEN (sizeof(DEVICE_NAME) - 1) @@ -274,6 +275,7 @@ int zmk_ble_prof_select(uint8_t index) { return 0; } + previous_profile = active_profile; active_profile = index; ble_save_profile(); @@ -295,6 +297,11 @@ int zmk_ble_prof_prev(void) { ZMK_BLE_PROFILE_COUNT); }; +int zmk_ble_prof_prev_dev(void) { + LOG_DBG(""); + return zmk_ble_prof_select(previous_profile); +}; + int zmk_ble_prof_disconnect(uint8_t index) { if (index >= ZMK_BLE_PROFILE_COUNT) return -ERANGE; diff --git a/docs/docs/keymaps/behaviors/bluetooth.md b/docs/docs/keymaps/behaviors/bluetooth.md index 93d0842814a..1e094400f92 100644 --- a/docs/docs/keymaps/behaviors/bluetooth.md +++ b/docs/docs/keymaps/behaviors/bluetooth.md @@ -44,11 +44,12 @@ Here is a table describing the command for each define: | `BT_CLR_ALL` | Clear bond information between the keyboard and host for all profiles. | | `BT_NXT` | Switch to the next profile, cycling through to the first one when the end is reached. | | `BT_PRV` | Switch to the previous profile, cycling through to the last one when the beginning is reached. | +| `BT_PRV_DEV` | Switch to the previously used profile. | | `BT_SEL` | Select the 0-indexed profile by number; must include a number as an argument in the keymap to work correctly, e.g. `BT_SEL 0`. | | `BT_DISC` | Disconnect from the 0-indexed profile by number, if it's currently connected and inactive; must include a number as an argument in the keymap to work correctly, e.g. `BT_DISC 0`. | :::note[Selected profile persistence] -The profile that is selected by the `BT_SEL`/`BT_PRV`/`BT_NXT` actions will be saved to flash storage and hence persist across restarts and firmware flashes. +The profile that is selected by the `BT_SEL`/`BT_PRV`/`BT_PRV_DEV`/`BT_NXT` actions will be saved to flash storage and hence persist across restarts and firmware flashes. However it will only be saved after [`CONFIG_ZMK_SETTINGS_SAVE_DEBOUNCE`](../../config/system.md#general) milliseconds in order to reduce potential wear on the flash memory. ::: @@ -82,6 +83,12 @@ The bluetooth behavior completes an bluetooth action given on press. &bt BT_PRV ``` +1. Behavior binding to select the previously used profile: + + ```dts + &bt BT_PRV_DEV + ``` + 1. Behavior binding to select the 2nd profile (passed parameters are [zero based](https://en.wikipedia.org/wiki/Zero-based_numbering)): ```dts