diff options
author | Pete Johanson <peter@peterjohanson.com> | 2020-08-18 19:21:43 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-08-18 19:21:43 -0400 |
commit | 0d3cfa859636a43e77ebad66f048452320d65d8b (patch) | |
tree | 53104ca199c0974e02e6ffa96997c176a9e8ce56 /app/src | |
parent | 1ae9e9d81786ce3ee2a1fbff4d1fd7108bc5d45c (diff) | |
parent | 4402e4fbc7bc79206589d3006fde802c4ba70ec7 (diff) |
Merge pull request #99 from petejohanson/core/bluetooth-unpair-magic-combo
Add magic combo for unpairing BT devices on start.
Diffstat (limited to 'app/src')
-rw-r--r-- | app/src/ble.c | 6 | ||||
-rw-r--r-- | app/src/ble_unpair_combo.c | 80 |
2 files changed, 86 insertions, 0 deletions
diff --git a/app/src/ble.c b/app/src/ble.c index bf1dee7..0e96d16 100644 --- a/app/src/ble.c +++ b/app/src/ble.c @@ -200,6 +200,12 @@ static int zmk_ble_init(struct device *_arg) return 0; } +int zmk_ble_unpair_all() +{ + LOG_DBG(""); + return bt_unpair(BT_ID_DEFAULT, NULL); +}; + bool zmk_ble_handle_key_user(struct zmk_key_event *key_event) { zmk_key key = key_event->key; diff --git a/app/src/ble_unpair_combo.c b/app/src/ble_unpair_combo.c new file mode 100644 index 0000000..a33a8e2 --- /dev/null +++ b/app/src/ble_unpair_combo.c @@ -0,0 +1,80 @@ +/* + * Copyright (c) 2020 Peter Johanson + * + * SPDX-License-Identifier: MIT + */ + +#include <device.h> +#include <init.h> + +#include <logging/log.h> + +#define DT_DRV_COMPAT zmk_bt_unpair_combo + +LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL); + +#include <zmk/ble.h> +#include <zmk/event-manager.h> +#include <zmk/events/position-state-changed.h> + + +static u8_t combo_state; + +const u32_t key_positions[] = DT_INST_PROP(0, key_positions); +#define KP_LEN DT_INST_PROP_LEN(0, key_positions) + +int index_for_key_position(u32_t kp) +{ + for (int i = 0; i < KP_LEN; i++) { + if (key_positions[i] == kp) { + return i; + } + } + + return -1; +} + +int unpair_combo_listener(const struct zmk_event_header *eh) +{ + if (is_position_state_changed(eh)) { + const struct position_state_changed *psc = cast_position_state_changed(eh); + + int kp_index = index_for_key_position(psc->position); + if (kp_index < 0) { + return 0; + } + + WRITE_BIT(combo_state, kp_index, psc->state); + } + + return 0; +}; + +void unpair_combo_work_handler(struct k_work *work) +{ + for (int i = 0; i < KP_LEN; i++) { + if (!(combo_state & BIT(i))) { + LOG_DBG("Key position %d not held, skipping unpair combo", key_positions[i]); + return; + } + } + + zmk_ble_unpair_all(); +}; + +struct k_delayed_work unpair_combo_work; + +int zmk_ble_unpair_combo_init(struct device *_unused) +{ + k_delayed_work_init(&unpair_combo_work, unpair_combo_work_handler); + k_delayed_work_submit(&unpair_combo_work, K_SECONDS(2)); + + return 0; +}; + +ZMK_LISTENER(zmk_ble_unpair_combo, unpair_combo_listener); +ZMK_SUBSCRIPTION(zmk_ble_unpair_combo, position_state_changed); + +SYS_INIT(zmk_ble_unpair_combo_init, + APPLICATION, + CONFIG_APPLICATION_INIT_PRIORITY); |