diff --git a/app/CMakeLists.txt b/app/CMakeLists.txt index efa349052cf..af0b023c489 100644 --- a/app/CMakeLists.txt +++ b/app/CMakeLists.txt @@ -56,6 +56,7 @@ if ((NOT CONFIG_ZMK_SPLIT) OR CONFIG_ZMK_SPLIT_ROLE_CENTRAL) target_sources_ifdef(CONFIG_ZMK_BEHAVIOR_SENSOR_ROTATE_COMMON app PRIVATE src/behaviors/behavior_sensor_rotate_common.c) target_sources(app PRIVATE src/combo.c) target_sources(app PRIVATE src/behaviors/behavior_tap_dance.c) + target_sources(app PRIVATE src/behaviors/behavior_bottom.c) target_sources(app PRIVATE src/behavior_queue.c) target_sources(app PRIVATE src/conditional_layer.c) target_sources(app PRIVATE src/endpoints.c) diff --git a/app/dts/behaviors.dtsi b/app/dts/behaviors.dtsi index b3502cbbc13..386bc391231 100644 --- a/app/dts/behaviors.dtsi +++ b/app/dts/behaviors.dtsi @@ -18,4 +18,5 @@ #include #include #include -#include \ No newline at end of file +#include +#include diff --git a/app/dts/behaviors/bottom.dtsi b/app/dts/behaviors/bottom.dtsi new file mode 100644 index 00000000000..0336eb3701d --- /dev/null +++ b/app/dts/behaviors/bottom.dtsi @@ -0,0 +1,16 @@ +/* + * Copyright (c) 2021 The ZMK Contributors + * + * SPDX-License-Identifier: MIT + */ + + / { + behaviors { + /omit-if-no-ref/ bottom: behavior_bottom { + compatible = "zmk,behavior-bottom"; + label = "BOTTOM"; + #binding-cells = <0>; + delay_ms = <10>; + }; + }; +}; diff --git a/app/dts/bindings/behaviors/zmk,behavior-bottom.yaml b/app/dts/bindings/behaviors/zmk,behavior-bottom.yaml new file mode 100644 index 00000000000..9f3913e7af1 --- /dev/null +++ b/app/dts/bindings/behaviors/zmk,behavior-bottom.yaml @@ -0,0 +1,13 @@ +# Copyright (c) 2020 The ZMK Contributors +# SPDX-License-Identifier: MIT + +description: Keyboard Keysmash Behavior + +compatible: "zmk,behavior-bottom" + +include: zero_param.yaml + +properties: + delay_ms: + type: int + required: true diff --git a/app/src/behaviors/behavior_bottom.c b/app/src/behaviors/behavior_bottom.c new file mode 100644 index 00000000000..6e4501cc106 --- /dev/null +++ b/app/src/behaviors/behavior_bottom.c @@ -0,0 +1,70 @@ +/* + * Copyright (c) 2021 The ZMK Contributors + * + * SPDX-License-Identifier: MIT + */ + +#define DT_DRV_COMPAT zmk_behavior_bottom + +#include +#include +#include +#include +#include +#include +#include +#include + +LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL); + +#if DT_HAS_COMPAT_STATUS_OKAY(DT_DRV_COMPAT) + +struct behavior_bottom_config { + int delay_ms; +}; + +static void zmk_bottom_tick(struct k_work *work) { + int key = (sys_rand32_get() % 26) + 4; + ZMK_EVENT_RAISE(zmk_keycode_state_changed_from_encoded(key, true, k_uptime_get())); + k_msleep(5); + ZMK_EVENT_RAISE(zmk_keycode_state_changed_from_encoded(key, false, k_uptime_get())); +} + +K_WORK_DEFINE(bottom_work, zmk_bottom_tick); + +static void zmk_bottom_tick_handler(struct k_timer *timer) { k_work_submit(&bottom_work); } + +K_TIMER_DEFINE(bottom_tick, zmk_bottom_tick_handler, NULL); + +static int on_bottom_binding_pressed(struct zmk_behavior_binding *binding, + struct zmk_behavior_binding_event event) { + const struct device *dev = device_get_binding(binding->behavior_dev); + const struct behavior_bottom_config *cfg = dev->config; + + k_timer_start(&bottom_tick, K_NO_WAIT, K_MSEC(cfg->delay_ms)); + + return ZMK_BEHAVIOR_OPAQUE; +} + +static int on_bottom_binding_released(struct zmk_behavior_binding *binding, + struct zmk_behavior_binding_event event) { + k_timer_stop(&bottom_tick); + return ZMK_BEHAVIOR_OPAQUE; +} + +static const struct behavior_driver_api behavior_bottom_driver_api = { + .binding_pressed = on_bottom_binding_pressed, + .binding_released = on_bottom_binding_released, +}; + +static int behavior_bottom_init(const struct device *dev) { return 0; } + +static struct behavior_bottom_config behavior_bottom_config = { + .delay_ms = DT_INST_PROP(0, delay_ms), +}; + +DEVICE_DT_INST_DEFINE(0, behavior_bottom_init, device_pm_control_nop, NULL, &behavior_bottom_config, + APPLICATION, CONFIG_KERNEL_INIT_PRIORITY_DEFAULT, + &behavior_bottom_driver_api); + +#endif