Browse Source
* Add `bt` behavior that can be used to perform certain actions, such as next/prev identity, reset identity, etc. NOTE: Multiple identities is only supported for non-split shields, due to missing Zephyr identity functionality for dual central/peripheral devices. * Proper bond reset tied to action, that honors peripheral bonds, so folks can reset and pair to other hosts, without breaking bonds between splt halves.xmkb
Pete Johanson
4 years ago
12 changed files with 563 additions and 52 deletions
@ -0,0 +1,9 @@ |
|||||||
|
/ { |
||||||
|
behaviors { |
||||||
|
bt: behavior_bluetooth { |
||||||
|
compatible = "zmk,behavior-bluetooth"; |
||||||
|
label = "BLUETOOTH"; |
||||||
|
#binding-cells = <2>; |
||||||
|
}; |
||||||
|
}; |
||||||
|
}; |
@ -0,0 +1,8 @@ |
|||||||
|
# Copyright (c) 2020, Peter Johanson |
||||||
|
# SPDX-License-Identifier: MIT |
||||||
|
|
||||||
|
description: Bluetooth Behavior |
||||||
|
|
||||||
|
compatible: "zmk,behavior-bluetooth" |
||||||
|
|
||||||
|
include: two_param.yaml |
@ -0,0 +1,17 @@ |
|||||||
|
/*
|
||||||
|
* Copyright (c) 2020 Peter Johanson <peter@peterjohanson.com> |
||||||
|
* |
||||||
|
* SPDX-License-Identifier: MIT |
||||||
|
*/ |
||||||
|
|
||||||
|
#define BT_RST_CMD 0 |
||||||
|
#define BT_IDENT_NEXT_CMD 1 |
||||||
|
#define BT_IDENT_PREV_CMD 2 |
||||||
|
#define BT_IDENT_SEL_CMD 3 |
||||||
|
#define BT_IDENT_CLR_CMD 4 |
||||||
|
|
||||||
|
#define BT_RST BT_RST_CMD 0 |
||||||
|
#define BT_IDENT_NEXT BT_IDENT_NEXT_CMD 0 |
||||||
|
#define BT_IDENT_PREV BT_IDENT_PREV_CMD 0 |
||||||
|
#define BT_IDENT_SEL BT_IDENT_SEL_CMD |
||||||
|
#define BT_IDENT_CLR BT_IDENT_CLR_CMD 0 |
@ -0,0 +1,62 @@ |
|||||||
|
/*
|
||||||
|
* Copyright (c) 2020 Peter Johanson <peter@peterjohanson.com> |
||||||
|
* |
||||||
|
* SPDX-License-Identifier: MIT |
||||||
|
*/ |
||||||
|
|
||||||
|
#define DT_DRV_COMPAT zmk_behavior_bluetooth |
||||||
|
|
||||||
|
#include <device.h> |
||||||
|
#include <drivers/behavior.h> |
||||||
|
|
||||||
|
#include <dt-bindings/zmk/bt.h> |
||||||
|
|
||||||
|
#include <bluetooth/conn.h> |
||||||
|
|
||||||
|
#include <logging/log.h> |
||||||
|
LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL); |
||||||
|
|
||||||
|
#include <zmk/ble.h> |
||||||
|
|
||||||
|
static int on_keymap_binding_pressed(struct device *dev, u32_t position, u32_t command, u32_t arg) |
||||||
|
{ |
||||||
|
switch (command) |
||||||
|
{ |
||||||
|
case BT_RST_CMD: |
||||||
|
return zmk_ble_unpair_all(); |
||||||
|
case BT_IDENT_CLR_CMD: |
||||||
|
return zmk_ble_identity_clear(); |
||||||
|
#if CONFIG_BT_ID_MAX != 1 |
||||||
|
case BT_IDENT_NEXT_CMD: |
||||||
|
return zmk_ble_identity_next(); |
||||||
|
case BT_IDENT_PREV_CMD: |
||||||
|
return zmk_ble_identity_prev(); |
||||||
|
case BT_IDENT_SEL_CMD: |
||||||
|
return zmk_ble_identity_select(arg); |
||||||
|
#endif /* BT_ID_MAX != 1 */ |
||||||
|
} |
||||||
|
|
||||||
|
return -ENOTSUP; |
||||||
|
} |
||||||
|
|
||||||
|
static int behavior_bt_init(struct device *dev) |
||||||
|
{ |
||||||
|
return 0; |
||||||
|
}; |
||||||
|
|
||||||
|
static int on_keymap_binding_released(struct device *dev, u32_t position, u32_t command, u32_t arg) |
||||||
|
{ |
||||||
|
return 0; |
||||||
|
} |
||||||
|
|
||||||
|
static const struct behavior_driver_api behavior_bt_driver_api = { |
||||||
|
.binding_pressed = on_keymap_binding_pressed, |
||||||
|
.binding_released = on_keymap_binding_released, |
||||||
|
}; |
||||||
|
|
||||||
|
DEVICE_AND_API_INIT(behavior_bt, DT_INST_LABEL(0), |
||||||
|
behavior_bt_init, |
||||||
|
NULL, |
||||||
|
NULL, |
||||||
|
APPLICATION, CONFIG_KERNEL_INIT_PRIORITY_DEFAULT, |
||||||
|
&behavior_bt_driver_api); |
@ -0,0 +1,82 @@ |
|||||||
|
--- |
||||||
|
title: Bluetooth Behavior |
||||||
|
sidebar_label: Bluetooth |
||||||
|
--- |
||||||
|
|
||||||
|
## Summary |
||||||
|
|
||||||
|
The bluetooth behavior allows management of various settings and states related to the bluetooth connection(s) |
||||||
|
between the keyboard and the host. |
||||||
|
|
||||||
|
## Bluetooth Command Defines |
||||||
|
|
||||||
|
Bluetooth command defines are provided through the [`dt-bindings/zmk/bt.h`](https://github.com/zmkfirmware/zmk/blob/main/app/include/dt-bindings/zmk/bt.h) header, |
||||||
|
which is added at the top of the keymap file: |
||||||
|
|
||||||
|
``` |
||||||
|
#include <dt-bindings/zmk/bt.h> |
||||||
|
``` |
||||||
|
|
||||||
|
This will allow you to reference the actions defined in this header such as `BT_IDENT_CLR_CMD`. |
||||||
|
|
||||||
|
Here is a table describing the command for each define: |
||||||
|
|
||||||
|
| Define | Action | |
||||||
|
| ------------------- | ----------------------------------------------------- | |
||||||
|
| `BT_IDENT_CLR_CMD` | Clear paired keyboards (for the current identity)[^1] | |
||||||
|
| `BT_IDENT_NEXT_CMD` | Switch to the next identity[^1] | |
||||||
|
| `BT_IDENT_PREV_CMD` | Switch to the previous identity | |
||||||
|
| `BT_IDENT_SEL_CMD` | Switch to a specific numbered identity | |
||||||
|
| `BT_RST_CMD` | Hard reset of all bluetooth bonds | |
||||||
|
|
||||||
|
Because the `BT_IDENT_SEL_CMD` command takes an additional parameter, the numeric index of the identity to select, _all_ the commands for the bluetooth behavior must take that additional parameter, and ignore the value. To make this easier, |
||||||
|
there are alias defines that add those default parameters for you: |
||||||
|
|
||||||
|
| Define | Action | |
||||||
|
| --------------- | --------------------------------------------------------------------------------------- | |
||||||
|
| `BT_IDENT_CLR` | Alias for `BT_IDENT_CLR_CMD 0` to clear paired keyboards (for the current identity)[^1] | |
||||||
|
| `BT_IDENT_NEXT` | Alias for `BT_IDENT_NEXT_CMD 0` to switch to the next identity[^1] | |
||||||
|
| `BT_IDENT_PREV` | Alias for `BT_IDENT_PREV_CMD 0` to switch to the previous identity | |
||||||
|
| `BT_IDENT_SEL` | Alias for `BT_IDENT_SEL_CMD` to switch to a specific numbered identity | |
||||||
|
| `BT_RST` | Alias for `BT_RST_CMD 0` to reset all bonds[^2] | |
||||||
|
|
||||||
|
[^1]: Multiple keyboard identities/profiles is only support on non-split keyboards as of this time. |
||||||
|
[^2]: This may interrupt pairing between both halves of a split keyboard. For split keyboards, it is preferred to use the [/docs/bond-reset] combo to clear bonds on both devices |
||||||
|
|
||||||
|
## Bluetooth Behavior |
||||||
|
|
||||||
|
The bluetooth behavior completes an bluetooth action given on press. |
||||||
|
|
||||||
|
### Behavior Binding |
||||||
|
|
||||||
|
- Reference: `&bt` |
||||||
|
- Parameter #1: The bluetooth command define, e.g. `BT_IDENT_CLR_CMD` or `BT_RST_CMD` |
||||||
|
- Parameter #2: The parameter for the command, when used to identify an identity/profile to select, e.g. `0` |
||||||
|
|
||||||
|
### Examples |
||||||
|
|
||||||
|
1. Behavior to clear the paired host: |
||||||
|
|
||||||
|
``` |
||||||
|
&bt BT_IDENT_CLR |
||||||
|
``` |
||||||
|
|
||||||
|
1. Behavior to hard reset all bonded devices[^2]: |
||||||
|
|
||||||
|
``` |
||||||
|
&bt BT_IDENT_CLR |
||||||
|
``` |
||||||
|
|
||||||
|
Examples for non-split keyboards with multiple identities: |
||||||
|
|
||||||
|
1. Behavior to switch to the next identity: |
||||||
|
|
||||||
|
``` |
||||||
|
&bt BT_IDENT_NEXT |
||||||
|
``` |
||||||
|
|
||||||
|
1. Behavior to switch to the specific numbered identity: |
||||||
|
|
||||||
|
``` |
||||||
|
&bt BT_IDENT_SEL 1 |
||||||
|
``` |
Loading…
Reference in new issue