diff options
Diffstat (limited to 'app/src')
-rw-r--r-- | app/src/behaviors/behavior_none.c | 48 | ||||
-rw-r--r-- | app/src/behaviors/behavior_reset.c | 29 | ||||
-rw-r--r-- | app/src/behaviors/behavior_toggle_layer.c | 49 | ||||
-rw-r--r-- | app/src/ble.c | 42 | ||||
-rw-r--r-- | app/src/ble_unpair_combo.c | 83 | ||||
-rw-r--r-- | app/src/keymap.c | 15 | ||||
-rw-r--r-- | app/src/usb_hid.c | 36 |
7 files changed, 264 insertions, 38 deletions
diff --git a/app/src/behaviors/behavior_none.c b/app/src/behaviors/behavior_none.c new file mode 100644 index 0000000..7e77e54 --- /dev/null +++ b/app/src/behaviors/behavior_none.c @@ -0,0 +1,48 @@ +/* + * Copyright (c) 2020 Peter Johanson <peter@peterjohanson.com> + * + * SPDX-License-Identifier: MIT + */ + +#define DT_DRV_COMPAT zmk_behavior_none + +#include <device.h> +#include <power/reboot.h> +#include <drivers/behavior.h> +#include <logging/log.h> + +LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL); + +struct behavior_none_config { }; +struct behavior_none_data { }; + +static int behavior_none_init(struct device *dev) +{ + return 0; +}; + +static int on_keymap_binding_pressed(struct device *dev, u32_t position, u32_t _param1, u32_t _param2) +{ + return 1; +} + +static int on_keymap_binding_released(struct device *dev, u32_t position, u32_t _param1, u32_t _param2) +{ + return 1; +} + +static const struct behavior_driver_api behavior_none_driver_api = { + .binding_pressed = on_keymap_binding_pressed, + .binding_released = on_keymap_binding_released, +}; + + +static const struct behavior_none_config behavior_none_config = {}; + +static struct behavior_none_data behavior_none_data; + +DEVICE_AND_API_INIT(behavior_none, DT_INST_LABEL(0), behavior_none_init, + &behavior_none_data, + &behavior_none_config, + APPLICATION, CONFIG_KERNEL_INIT_PRIORITY_DEFAULT, + &behavior_none_driver_api);
\ No newline at end of file diff --git a/app/src/behaviors/behavior_reset.c b/app/src/behaviors/behavior_reset.c index 44cbc21..30a96ea 100644 --- a/app/src/behaviors/behavior_reset.c +++ b/app/src/behaviors/behavior_reset.c @@ -13,8 +13,9 @@ LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL); -struct behavior_reset_config { }; -struct behavior_reset_data { }; +struct behavior_reset_config { + int type; +}; static int behavior_reset_init(struct device *dev) { @@ -23,9 +24,11 @@ static int behavior_reset_init(struct device *dev) static int on_keymap_binding_pressed(struct device *dev, u32_t position, u32_t _param1, u32_t _param2) { + const struct behavior_reset_config *cfg = dev->config_info; + // TODO: Correct magic code for going into DFU? // See https://github.com/adafruit/Adafruit_nRF52_Bootloader/blob/d6b28e66053eea467166f44875e3c7ec741cb471/src/main.c#L107 - sys_reboot(0); + sys_reboot(cfg->type); return 0; } @@ -34,12 +37,14 @@ static const struct behavior_driver_api behavior_reset_driver_api = { }; -static const struct behavior_reset_config behavior_reset_config = {}; - -static struct behavior_reset_data behavior_reset_data; - -DEVICE_AND_API_INIT(behavior_reset, DT_INST_LABEL(0), behavior_reset_init, - &behavior_reset_data, - &behavior_reset_config, - APPLICATION, CONFIG_KERNEL_INIT_PRIORITY_DEFAULT, - &behavior_reset_driver_api);
\ No newline at end of file +#define RST_INST(n) \ + static const struct behavior_reset_config behavior_reset_config_##n = { \ + .type = DT_INST_PROP(n, type) \ + }; \ + DEVICE_AND_API_INIT(behavior_reset_##n, DT_INST_LABEL(n), behavior_reset_init, \ + NULL, \ + &behavior_reset_config_##n, \ + APPLICATION, CONFIG_KERNEL_INIT_PRIORITY_DEFAULT, \ + &behavior_reset_driver_api); + +DT_INST_FOREACH_STATUS_OKAY(RST_INST)
\ No newline at end of file diff --git a/app/src/behaviors/behavior_toggle_layer.c b/app/src/behaviors/behavior_toggle_layer.c new file mode 100644 index 0000000..13f4a29 --- /dev/null +++ b/app/src/behaviors/behavior_toggle_layer.c @@ -0,0 +1,49 @@ +/* + * Copyright (c) 2020 Cody McGinnis <brainwart@gmail.com> + * + * SPDX-License-Identifier: MIT + */ + +#define DT_DRV_COMPAT zmk_behavior_toggle_layer + +#include <device.h> +#include <drivers/behavior.h> +#include <logging/log.h> + +#include <zmk/keymap.h> + +LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL); + +struct behavior_tog_config { }; +struct behavior_tog_data { }; + +static int behavior_tog_init(struct device *dev) +{ + return 0; +}; + + +static int tog_keymap_binding_pressed(struct device *dev, u32_t position, u32_t layer, u32_t _) +{ + return zmk_keymap_layer_toggle(layer); +} + +static int tog_keymap_binding_released(struct device *dev, u32_t position, u32_t layer, u32_t _) +{ + return 0; +} + +static const struct behavior_driver_api behavior_tog_driver_api = { + .binding_pressed = tog_keymap_binding_pressed, + .binding_released = tog_keymap_binding_released, +}; + +static const struct behavior_tog_config behavior_tog_config = {}; + +static struct behavior_tog_data behavior_tog_data; + +DEVICE_AND_API_INIT(behavior_tog, DT_INST_LABEL(0), behavior_tog_init, + &behavior_tog_data, + &behavior_tog_config, + APPLICATION, CONFIG_KERNEL_INIT_PRIORITY_DEFAULT, + &behavior_tog_driver_api); diff --git a/app/src/ble.c b/app/src/ble.c index 488491c..0e96d16 100644 --- a/app/src/ble.c +++ b/app/src/ble.c @@ -1,3 +1,8 @@ +/* + * Copyright (c) 2020 Peter Johanson + * + * SPDX-License-Identifier: MIT + */ #include <device.h> #include <init.h> @@ -23,6 +28,16 @@ static struct bt_conn *auth_passkey_entry_conn; static u8_t passkey_entries[6] = {0, 0, 0, 0, 0, 0}; static u8_t passkey_digit = 0; +#if IS_ENABLED(CONFIG_ZMK_SPLIT_BLE_ROLE_PERIPHERAL) +#define ZMK_ADV_PARAMS BT_LE_ADV_PARAM(BT_LE_ADV_OPT_CONNECTABLE | \ + BT_LE_ADV_OPT_USE_NAME | \ + BT_LE_ADV_OPT_ONE_TIME, \ + BT_GAP_ADV_FAST_INT_MIN_2, \ + BT_GAP_ADV_FAST_INT_MAX_2, NULL) +#else +#define ZMK_ADV_PARAMS BT_LE_ADV_CONN_NAME +#endif + static void connected(struct bt_conn *conn, u8_t err) { char addr[BT_ADDR_LE_STR_LEN]; @@ -76,29 +91,10 @@ static void security_changed(struct bt_conn *conn, bt_security_t level, } } -#if !IS_ENABLED(CONFIG_ZMK_SPLIT_BLE_ROLE_PERIPHERAL) -static bool le_param_req(struct bt_conn *conn, struct bt_le_conn_param *param) { - static struct bt_conn_info info; - - bt_conn_get_info(conn, &info); - - /* This captures a param change from central half of a split board connection - to stop default params from getting set over the top of our preferred ones */ - if (info.role == BT_CONN_ROLE_MASTER && (param->interval_min != 6 || param->latency != 30)) { - return false; - } - - return true; -} -#endif - static struct bt_conn_cb conn_callbacks = { .connected = connected, .disconnected = disconnected, .security_changed = security_changed, -#if !IS_ENABLED(CONFIG_ZMK_SPLIT_BLE_ROLE_PERIPHERAL) - .le_param_req = le_param_req, -#endif }; static void auth_passkey_display(struct bt_conn *conn, unsigned int passkey) @@ -173,7 +169,7 @@ static void zmk_ble_ready(int err) return; } - err = bt_le_adv_start(BT_LE_ADV_CONN_NAME, zmk_ble_ad, ARRAY_SIZE(zmk_ble_ad), NULL, 0); + err = bt_le_adv_start(ZMK_ADV_PARAMS, zmk_ble_ad, ARRAY_SIZE(zmk_ble_ad), NULL, 0); if (err) { LOG_ERR("Advertising failed to start (err %d)", err); @@ -204,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..82fa834 --- /dev/null +++ b/app/src/ble_unpair_combo.c @@ -0,0 +1,83 @@ +/* + * Copyright (c) 2020 Peter Johanson + * + * SPDX-License-Identifier: MIT + */ + +#include <device.h> +#include <init.h> + +#define DT_DRV_COMPAT zmk_bt_unpair_combo + +#if DT_HAS_COMPAT_STATUS_OKAY(DT_DRV_COMPAT) + +#include <logging/log.h> +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); + +#endif /* DT_HAS_COMPAT_STATUS_OKAY(DT_DRV_COMPAT) */ diff --git a/app/src/keymap.c b/app/src/keymap.c index ff494f7..ee6e370 100644 --- a/app/src/keymap.c +++ b/app/src/keymap.c @@ -76,6 +76,11 @@ static struct zmk_behavior_binding zmk_sensor_keymap[ZMK_KEYMAP_LAYERS_LEN][ZMK_ WRITE_BIT(zmk_keymap_layer_state, layer, state); \ return 0; +bool zmk_keymap_layer_active(u8_t layer) +{ + return (zmk_keymap_layer_state & (BIT(layer))) == (BIT(layer)); +}; + int zmk_keymap_layer_activate(u8_t layer) { SET_LAYER_STATE(layer, true); @@ -86,6 +91,16 @@ int zmk_keymap_layer_deactivate(u8_t layer) SET_LAYER_STATE(layer, false); }; +int zmk_keymap_layer_toggle(u8_t layer) +{ + if (zmk_keymap_layer_active(layer)) + { + return zmk_keymap_layer_deactivate(layer); + } + + return zmk_keymap_layer_activate(layer); +}; + bool is_active_position(u32_t position, u8_t layer) { return (zmk_keymap_layer_state & BIT(layer)) == BIT(layer) diff --git a/app/src/usb_hid.c b/app/src/usb_hid.c index 4c6dd4b..784fc25 100644 --- a/app/src/usb_hid.c +++ b/app/src/usb_hid.c @@ -11,18 +11,42 @@ LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL); -static enum usb_dc_status_code usb_status; +static enum usb_dc_status_code usb_status = USB_DC_UNKNOWN; static struct device *hid_dev; +static K_SEM_DEFINE(hid_sem, 1, 1); + +static void in_ready_cb(void) +{ + k_sem_give(&hid_sem); +} + +static const struct hid_ops ops = +{ + .int_in_ready = in_ready_cb, +}; + int zmk_usb_hid_send_report(const u8_t *report, size_t len) { - if (usb_status == USB_DC_SUSPEND) - { + switch(usb_status) { + case USB_DC_SUSPEND: return usb_wakeup_request(); + case USB_DC_ERROR: + case USB_DC_RESET: + case USB_DC_DISCONNECTED: + case USB_DC_UNKNOWN: + return -ENODEV; + default: + k_sem_take(&hid_sem, K_MSEC(30)); + int err = hid_int_ep_write(hid_dev, report, len, NULL); + + if (err) { + k_sem_give(&hid_sem); + } + + return err; } - - return hid_int_ep_write(hid_dev, report, len, NULL); } void usb_hid_status_cb(enum usb_dc_status_code status, const u8_t *params) @@ -43,7 +67,7 @@ static int zmk_usb_hid_init(struct device *_arg) usb_hid_register_device(hid_dev, zmk_hid_report_desc, sizeof(zmk_hid_report_desc), - NULL); + &ops); usb_hid_init(hid_dev); |