Skip to content

Commit

Permalink
feat: Add support for switching to previously used bluetooth profile
Browse files Browse the repository at this point in the history
  • Loading branch information
willelind committed Sep 23, 2024
1 parent f992352 commit 6915982
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 4 deletions.
8 changes: 5 additions & 3 deletions app/include/dt-bindings/zmk/bt.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
1 change: 1 addition & 0 deletions app/include/zmk/ble.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
7 changes: 7 additions & 0 deletions app/src/behaviors/behavior_bt.c
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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:
Expand Down
7 changes: 7 additions & 0 deletions app/src/ble.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -274,6 +275,7 @@ int zmk_ble_prof_select(uint8_t index) {
return 0;
}

previous_profile = active_profile;
active_profile = index;
ble_save_profile();

Expand All @@ -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;
Expand Down
9 changes: 8 additions & 1 deletion docs/docs/keymaps/behaviors/bluetooth.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
:::

Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 6915982

Please sign in to comment.