From c23d752917774bc740c791e2f7eeef6f8f9e1033 Mon Sep 17 00:00:00 2001 From: Pete Johanson Date: Fri, 19 Jun 2020 15:32:33 -0400 Subject: Some initial work on behavior bindings for keymaps --- app/src/behaviors/behavior_key_press.c | 66 ++++++++++++++++++++++++++++++++++ app/src/behaviors/behavior_reset.c | 51 ++++++++++++++++++++++++++ 2 files changed, 117 insertions(+) create mode 100644 app/src/behaviors/behavior_key_press.c create mode 100644 app/src/behaviors/behavior_reset.c (limited to 'app/src/behaviors') diff --git a/app/src/behaviors/behavior_key_press.c b/app/src/behaviors/behavior_key_press.c new file mode 100644 index 0000000..b9d0ff0 --- /dev/null +++ b/app/src/behaviors/behavior_key_press.c @@ -0,0 +1,66 @@ +/* + * Copyright (c) 2020 Peter Johanson + * + * SPDX-License-Identifier: MIT + */ + +#define DT_DRV_COMPAT zmk_behavior_key_press + +#include +#include +#include + +LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL); + +struct behavior_key_press_config { }; +struct behavior_key_press_data { }; + +static int behavior_key_press_init(struct device *dev) +{ + return 0; +}; + + +// They keycode is passed by the "keymap" based on the parameter created as part of the assignment. +// Other drivers instead might activate a layer, update the consumer page state, or update the RGB state, etc. +// Returns: +// * > 0 - indicate successful processing, and halt further handling, +// * 0 - Indicate successful processing, and continue propagation. +// * < 0 - Indicate error processing, report and halt further propagation. +static int on_position_pressed(struct device *dev, u32_t keycode, u32_t _) +{ + // Invoking this triggers a *new* event, that can be linked to other behaviours. + //return zmk_key_state_press(u32_t keycode); + return 0; +} + + +// They keycode is passed by the "keymap" based on the parameter created as part of the assignment. +static int on_position_released(struct device *dev, u32_t keycode, u32_t _) +{ + // Invoking this triggers a *new* event, that can will be handled by other behaviors + // This is the "command" piece. Which could be better/richer, but captures essence here. + // return zmk_key_state_release(u32_t keycode); + return 0; +} + +static const struct behavior_driver_api behavior_key_press_driver_api = { + // These callbacks are all optional, and define which kinds of events the behavior can handle. + // They can reference local functions defined here, or shared event handlers. + .position_pressed = on_position_pressed, + .position_released = on_position_released + // Other optional callbacks a behavior can implement + // .on_mouse_moved + // .on_sensor_data - Any behaviour that wants to be linked to a censor can implement this behavior +}; + + +static const struct behavior_key_press_config behavior_key_press_config = {}; + +static struct behavior_key_press_data behavior_key_press_data; + +DEVICE_AND_API_INIT(behavior_key_press, DT_INST_LABEL(0), behavior_key_press_init, + &behavior_key_press_data, + &behavior_key_press_config, + APPLICATION, CONFIG_KERNEL_INIT_PRIORITY_DEFAULT, + &behavior_key_press_driver_api); diff --git a/app/src/behaviors/behavior_reset.c b/app/src/behaviors/behavior_reset.c new file mode 100644 index 0000000..1609fad --- /dev/null +++ b/app/src/behaviors/behavior_reset.c @@ -0,0 +1,51 @@ +/* + * Copyright (c) 2020 Peter Johanson + * + * SPDX-License-Identifier: MIT + */ + +#define DT_DRV_COMPAT zmk_behavior_reset + +#include +#include +#include +#include + +LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL); + +struct behavior_reset_config { }; +struct behavior_reset_data { }; + +static int behavior_reset_init(struct device *dev) +{ + return 0; +}; + +static int on_position_pressed(struct device *dev, u32_t _param1, u32_t _param2) +{ + // 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); + return 0; +} + +static int on_position_released(struct device *dev, u32_t _param1, u32_t _param2) +{ + return 0; +} + +static const struct behavior_driver_api behavior_reset_driver_api = { + .position_pressed = on_position_pressed, + .position_released = on_position_released +}; + + +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 -- cgit v1.2.3 From d65629b9a0b79b6e294419fe9a4118fb09491c91 Mon Sep 17 00:00:00 2001 From: Pete Johanson Date: Sat, 20 Jun 2020 00:11:39 -0400 Subject: Lots more pieces toward HID working again. --- app/src/behaviors/behavior_hid.c | 59 ++++++++++++++++++++++++++++++++++ app/src/behaviors/behavior_key_press.c | 13 ++++---- app/src/behaviors/behavior_keymap.c | 50 ++++++++++++++++++++++++++++ 3 files changed, 115 insertions(+), 7 deletions(-) create mode 100644 app/src/behaviors/behavior_hid.c create mode 100644 app/src/behaviors/behavior_keymap.c (limited to 'app/src/behaviors') diff --git a/app/src/behaviors/behavior_hid.c b/app/src/behaviors/behavior_hid.c new file mode 100644 index 0000000..2779568 --- /dev/null +++ b/app/src/behaviors/behavior_hid.c @@ -0,0 +1,59 @@ +/* + * Copyright (c) 2020 Peter Johanson + * + * SPDX-License-Identifier: MIT + */ + +#define DT_DRV_COMPAT zmk_behavior_hid + +#include +#include +#include +#include + +LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL); + +#include +#include + +struct behavior_hid_config { }; +struct behavior_hid_data { }; + +static int behavior_hid_init(struct device *dev) +{ + return 0; +}; + +static int on_keycode_pressed(struct device *dev, u32_t keycode) +{ + enum zmk_hid_report_changes changes; + LOG_DBG("keycode %d", keycode); + + changes = zmk_hid_press_key(keycode); + return zmk_endpoints_send_report(changes); +} + +static int on_keycode_released(struct device *dev, u32_t keycode) +{ + enum zmk_hid_report_changes changes; + LOG_DBG("keycode %d", keycode); + + changes = zmk_hid_release_key(keycode); + return zmk_endpoints_send_report(changes); +} + +static const struct behavior_driver_api behavior_hid_driver_api = { + .keycode_pressed = on_keycode_pressed, + .keycode_released = on_keycode_released +}; + + +static const struct behavior_hid_config behavior_hid_config = {}; + +static struct behavior_hid_data behavior_hid_data; + +DEVICE_AND_API_INIT(behavior_hid, DT_INST_LABEL(0), behavior_hid_init, + &behavior_hid_data, + &behavior_hid_config, + APPLICATION, CONFIG_KERNEL_INIT_PRIORITY_DEFAULT, + &behavior_hid_driver_api); \ No newline at end of file diff --git a/app/src/behaviors/behavior_key_press.c b/app/src/behaviors/behavior_key_press.c index b9d0ff0..3c57c51 100644 --- a/app/src/behaviors/behavior_key_press.c +++ b/app/src/behaviors/behavior_key_press.c @@ -10,6 +10,8 @@ #include #include +#include + LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL); struct behavior_key_press_config { }; @@ -29,19 +31,16 @@ static int behavior_key_press_init(struct device *dev) // * < 0 - Indicate error processing, report and halt further propagation. static int on_position_pressed(struct device *dev, u32_t keycode, u32_t _) { - // Invoking this triggers a *new* event, that can be linked to other behaviours. - //return zmk_key_state_press(u32_t keycode); - return 0; + LOG_DBG("pressing: %d", keycode); + return zmk_events_keycode_pressed(keycode); } // They keycode is passed by the "keymap" based on the parameter created as part of the assignment. static int on_position_released(struct device *dev, u32_t keycode, u32_t _) { - // Invoking this triggers a *new* event, that can will be handled by other behaviors - // This is the "command" piece. Which could be better/richer, but captures essence here. - // return zmk_key_state_release(u32_t keycode); - return 0; + LOG_DBG("releasing: %d", keycode); + return zmk_events_keycode_released(keycode); } static const struct behavior_driver_api behavior_key_press_driver_api = { diff --git a/app/src/behaviors/behavior_keymap.c b/app/src/behaviors/behavior_keymap.c new file mode 100644 index 0000000..48f8547 --- /dev/null +++ b/app/src/behaviors/behavior_keymap.c @@ -0,0 +1,50 @@ +/* + * Copyright (c) 2020 Peter Johanson + * + * SPDX-License-Identifier: MIT + */ + +#define DT_DRV_COMPAT zmk_behavior_keymap + +#include +#include +#include +#include + +LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL); + +#include + +struct behavior_keymap_config { }; +struct behavior_keymap_data { }; + +static int behavior_keymap_init(struct device *dev) +{ + return 0; +}; + +static int on_position_pressed(struct device *dev, u32_t row, u32_t column) +{ + return zmk_keymap_position_state_changed(row, column, true); +} + +static int on_position_released(struct device *dev, u32_t row, u32_t column) +{ + return zmk_keymap_position_state_changed(row, column, false); +} + +static const struct behavior_driver_api behavior_keymap_driver_api = { + .position_pressed = on_position_pressed, + .position_released = on_position_released +}; + + +static const struct behavior_keymap_config behavior_keymap_config = {}; + +static struct behavior_keymap_data behavior_keymap_data; + +DEVICE_AND_API_INIT(behavior_keymap, DT_INST_LABEL(0), behavior_keymap_init, + &behavior_keymap_data, + &behavior_keymap_config, + APPLICATION, CONFIG_KERNEL_INIT_PRIORITY_DEFAULT, + &behavior_keymap_driver_api); \ No newline at end of file -- cgit v1.2.3 From 7e659851c80bf2819554d93ad9971f56aa5b225b Mon Sep 17 00:00:00 2001 From: Pete Johanson Date: Sat, 20 Jun 2020 17:54:52 -0400 Subject: Refactor matrix transform, positions pervasively * Do mapping to positions right in kscan handler, and then surface positions throughout the API. --- app/src/behaviors/behavior_keymap.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'app/src/behaviors') diff --git a/app/src/behaviors/behavior_keymap.c b/app/src/behaviors/behavior_keymap.c index 48f8547..9369b40 100644 --- a/app/src/behaviors/behavior_keymap.c +++ b/app/src/behaviors/behavior_keymap.c @@ -23,14 +23,14 @@ static int behavior_keymap_init(struct device *dev) return 0; }; -static int on_position_pressed(struct device *dev, u32_t row, u32_t column) +static int on_position_pressed(struct device *dev, u32_t position, u32_t _) { - return zmk_keymap_position_state_changed(row, column, true); + return zmk_keymap_position_state_changed(position, true); } -static int on_position_released(struct device *dev, u32_t row, u32_t column) +static int on_position_released(struct device *dev, u32_t position, u32_t _) { - return zmk_keymap_position_state_changed(row, column, false); + return zmk_keymap_position_state_changed(position, false); } static const struct behavior_driver_api behavior_keymap_driver_api = { -- cgit v1.2.3 From 223edf05ad08938b066f9187668ebfae43c5e91a Mon Sep 17 00:00:00 2001 From: Pete Johanson Date: Sun, 21 Jun 2020 21:43:44 -0400 Subject: Refactor global bindings, implement mod-tap. * Use extra comptible = "zmk,behavior-global" to add behaviors to global bindings for event notification. * Implement mod-tap, as a keymap binding and global one to skip tap if other keycode pressed while held. --- app/src/behaviors/behavior_hid.c | 20 +++++++- app/src/behaviors/behavior_key_press.c | 12 ++--- app/src/behaviors/behavior_keymap.c | 8 +-- app/src/behaviors/behavior_mod_tap.c | 89 ++++++++++++++++++++++++++++++++++ app/src/behaviors/behavior_reset.c | 10 +--- 5 files changed, 120 insertions(+), 19 deletions(-) create mode 100644 app/src/behaviors/behavior_mod_tap.c (limited to 'app/src/behaviors') diff --git a/app/src/behaviors/behavior_hid.c b/app/src/behaviors/behavior_hid.c index 2779568..0aeade5 100644 --- a/app/src/behaviors/behavior_hid.c +++ b/app/src/behaviors/behavior_hid.c @@ -42,9 +42,27 @@ static int on_keycode_released(struct device *dev, u32_t keycode) return zmk_endpoints_send_report(changes); } +static int on_modifiers_pressed(struct device *dev, zmk_mod_flags modifiers) +{ + LOG_DBG("modifiers %d", modifiers); + + zmk_hid_register_mods(modifiers); + return zmk_endpoints_send_report(Keypad); +} + +static int on_modifiers_released(struct device *dev, zmk_mod_flags modifiers) +{ + LOG_DBG("modifiers %d", modifiers); + + zmk_hid_unregister_mods(modifiers); + return zmk_endpoints_send_report(Keypad); +} + static const struct behavior_driver_api behavior_hid_driver_api = { .keycode_pressed = on_keycode_pressed, - .keycode_released = on_keycode_released + .keycode_released = on_keycode_released, + .modifiers_pressed = on_modifiers_pressed, + .modifiers_released = on_modifiers_released }; diff --git a/app/src/behaviors/behavior_key_press.c b/app/src/behaviors/behavior_key_press.c index 3c57c51..1b3e670 100644 --- a/app/src/behaviors/behavior_key_press.c +++ b/app/src/behaviors/behavior_key_press.c @@ -29,25 +29,25 @@ static int behavior_key_press_init(struct device *dev) // * > 0 - indicate successful processing, and halt further handling, // * 0 - Indicate successful processing, and continue propagation. // * < 0 - Indicate error processing, report and halt further propagation. -static int on_position_pressed(struct device *dev, u32_t keycode, u32_t _) +static int on_keymap_binding_pressed(struct device *dev, u32_t position, u32_t keycode, u32_t _) { - LOG_DBG("pressing: %d", keycode); + LOG_DBG("position %d keycode %d", position, keycode); return zmk_events_keycode_pressed(keycode); } // They keycode is passed by the "keymap" based on the parameter created as part of the assignment. -static int on_position_released(struct device *dev, u32_t keycode, u32_t _) +static int on_keymap_binding_released(struct device *dev, u32_t position, u32_t keycode, u32_t _) { - LOG_DBG("releasing: %d", keycode); + LOG_DBG("position %d keycode %d", position, keycode); return zmk_events_keycode_released(keycode); } static const struct behavior_driver_api behavior_key_press_driver_api = { // These callbacks are all optional, and define which kinds of events the behavior can handle. // They can reference local functions defined here, or shared event handlers. - .position_pressed = on_position_pressed, - .position_released = on_position_released + .binding_pressed = on_keymap_binding_pressed, + .binding_released = on_keymap_binding_released // Other optional callbacks a behavior can implement // .on_mouse_moved // .on_sensor_data - Any behaviour that wants to be linked to a censor can implement this behavior diff --git a/app/src/behaviors/behavior_keymap.c b/app/src/behaviors/behavior_keymap.c index 9369b40..7a06b2f 100644 --- a/app/src/behaviors/behavior_keymap.c +++ b/app/src/behaviors/behavior_keymap.c @@ -23,19 +23,19 @@ static int behavior_keymap_init(struct device *dev) return 0; }; -static int on_position_pressed(struct device *dev, u32_t position, u32_t _) +static int on_position_pressed(struct device *dev, u32_t position) { return zmk_keymap_position_state_changed(position, true); } -static int on_position_released(struct device *dev, u32_t position, u32_t _) +static int on_position_released(struct device *dev, u32_t position) { return zmk_keymap_position_state_changed(position, false); } static const struct behavior_driver_api behavior_keymap_driver_api = { .position_pressed = on_position_pressed, - .position_released = on_position_released + .position_released = on_position_released, }; @@ -47,4 +47,4 @@ DEVICE_AND_API_INIT(behavior_keymap, DT_INST_LABEL(0), behavior_keymap_init, &behavior_keymap_data, &behavior_keymap_config, APPLICATION, CONFIG_KERNEL_INIT_PRIORITY_DEFAULT, - &behavior_keymap_driver_api); \ No newline at end of file + &behavior_keymap_driver_api); diff --git a/app/src/behaviors/behavior_mod_tap.c b/app/src/behaviors/behavior_mod_tap.c new file mode 100644 index 0000000..7684c29 --- /dev/null +++ b/app/src/behaviors/behavior_mod_tap.c @@ -0,0 +1,89 @@ +/* + * Copyright (c) 2020 Peter Johanson + * + * SPDX-License-Identifier: MIT + */ + +#define DT_DRV_COMPAT zmk_behavior_mod_tap + +#include +#include +#include + +#include + +LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL); + +struct behavior_mod_tap_config { }; +struct behavior_mod_tap_data { + u16_t pending_press_positions; +}; + +static int behavior_mod_tap_init(struct device *dev) +{ + return 0; +}; + + +static int on_keymap_binding_pressed(struct device *dev, u32_t position, u32_t mods, u32_t keycode) +{ + struct behavior_mod_tap_data *data = dev->driver_data; + LOG_DBG("mods: %d, keycode: %d", mods, keycode); + WRITE_BIT(data->pending_press_positions, position, true); + return zmk_events_modifiers_pressed(mods); +} + + +// They keycode is passed by the "keymap" based on the parameter created as part of the assignment. +static int on_keymap_binding_released(struct device *dev, u32_t position, u32_t mods, u32_t keycode) +{ + struct behavior_mod_tap_data *data = dev->driver_data; + LOG_DBG("mods: %d, keycode: %d", mods, keycode); + + zmk_events_modifiers_released(mods); + if (data->pending_press_positions & BIT(position)) { + zmk_events_keycode_pressed(keycode); + k_msleep(10); + zmk_events_keycode_released(keycode); + } + + return 0; +} + +static int on_keycode_pressed(struct device *dev, u32_t keycode) +{ + struct behavior_mod_tap_data *data = dev->driver_data; + data->pending_press_positions = 0; + LOG_DBG("pressing: %d", keycode); + return 0; +} + + +static int on_keycode_released(struct device *dev, u32_t keycode) +{ + LOG_DBG("releasing: %d", keycode); + return 0; +} + +static const struct behavior_driver_api behavior_mod_tap_driver_api = { + // These callbacks are all optional, and define which kinds of events the behavior can handle. + // They can reference local functions defined here, or shared event handlers. + .binding_pressed = on_keymap_binding_pressed, + .binding_released = on_keymap_binding_released, + .keycode_pressed = on_keycode_pressed, + .keycode_released = on_keycode_released + // Other optional callbacks a behavior can implement + // .on_mouse_moved + // .on_sensor_data - Any behaviour that wants to be linked to a censor can implement this behavior +}; + + +static const struct behavior_mod_tap_config behavior_mod_tap_config = {}; + +static struct behavior_mod_tap_data behavior_mod_tap_data; + +DEVICE_AND_API_INIT(behavior_key_press, DT_INST_LABEL(0), behavior_mod_tap_init, + &behavior_mod_tap_data, + &behavior_mod_tap_config, + APPLICATION, CONFIG_KERNEL_INIT_PRIORITY_DEFAULT, + &behavior_mod_tap_driver_api); diff --git a/app/src/behaviors/behavior_reset.c b/app/src/behaviors/behavior_reset.c index 1609fad..44cbc21 100644 --- a/app/src/behaviors/behavior_reset.c +++ b/app/src/behaviors/behavior_reset.c @@ -21,7 +21,7 @@ static int behavior_reset_init(struct device *dev) return 0; }; -static int on_position_pressed(struct device *dev, u32_t _param1, u32_t _param2) +static int on_keymap_binding_pressed(struct device *dev, u32_t position, u32_t _param1, u32_t _param2) { // TODO: Correct magic code for going into DFU? // See https://github.com/adafruit/Adafruit_nRF52_Bootloader/blob/d6b28e66053eea467166f44875e3c7ec741cb471/src/main.c#L107 @@ -29,14 +29,8 @@ static int on_position_pressed(struct device *dev, u32_t _param1, u32_t _param2) return 0; } -static int on_position_released(struct device *dev, u32_t _param1, u32_t _param2) -{ - return 0; -} - static const struct behavior_driver_api behavior_reset_driver_api = { - .position_pressed = on_position_pressed, - .position_released = on_position_released + .binding_pressed = on_keymap_binding_pressed, }; -- cgit v1.2.3 From 93635077e600e269aa905a0558c07acf0efebffb Mon Sep 17 00:00:00 2001 From: Pete Johanson Date: Sun, 21 Jun 2020 21:56:49 -0400 Subject: Implement momentary layer + transparent behaviors * Implement "momentary layer" behavior with `&mo` reference. * Implement basic "transparent" behavior with `&trans` reference. --- app/src/behaviors/behavior_momentary_layer.c | 55 ++++++++++++++++++++++++++++ app/src/behaviors/behavior_transparent.c | 48 ++++++++++++++++++++++++ 2 files changed, 103 insertions(+) create mode 100644 app/src/behaviors/behavior_momentary_layer.c create mode 100644 app/src/behaviors/behavior_transparent.c (limited to 'app/src/behaviors') diff --git a/app/src/behaviors/behavior_momentary_layer.c b/app/src/behaviors/behavior_momentary_layer.c new file mode 100644 index 0000000..f85d33d --- /dev/null +++ b/app/src/behaviors/behavior_momentary_layer.c @@ -0,0 +1,55 @@ +/* + * Copyright (c) 2020 Peter Johanson + * + * SPDX-License-Identifier: MIT + */ + +#define DT_DRV_COMPAT zmk_behavior_momentary_layer + +#include +#include +#include + +#include +#include + +LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL); + +struct behavior_mo_config { }; +struct behavior_mo_data { }; + +static int behavior_mo_init(struct device *dev) +{ + return 0; +}; + + +static int on_keymap_binding_pressed(struct device *dev, u32_t position, u32_t layer, u32_t _) +{ + LOG_DBG("position %d layer %d", position, layer); + + return zmk_keymap_layer_activate(layer); +} + +static int on_keymap_binding_released(struct device *dev, u32_t position, u32_t layer, u32_t _) +{ + LOG_DBG("position %d layer %d", position, layer); + + return zmk_keymap_layer_deactivate(layer); +} + +static const struct behavior_driver_api behavior_mo_driver_api = { + .binding_pressed = on_keymap_binding_pressed, + .binding_released = on_keymap_binding_released +}; + + +static const struct behavior_mo_config behavior_mo_config = {}; + +static struct behavior_mo_data behavior_mo_data; + +DEVICE_AND_API_INIT(behavior_mo, DT_INST_LABEL(0), behavior_mo_init, + &behavior_mo_data, + &behavior_mo_config, + APPLICATION, CONFIG_KERNEL_INIT_PRIORITY_DEFAULT, + &behavior_mo_driver_api); diff --git a/app/src/behaviors/behavior_transparent.c b/app/src/behaviors/behavior_transparent.c new file mode 100644 index 0000000..5109264 --- /dev/null +++ b/app/src/behaviors/behavior_transparent.c @@ -0,0 +1,48 @@ +/* + * Copyright (c) 2020 Peter Johanson + * + * SPDX-License-Identifier: MIT + */ + +#define DT_DRV_COMPAT zmk_behavior_transparent + +#include +#include +#include +#include + +LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL); + +struct behavior_transparent_config { }; +struct behavior_transparent_data { }; + +static int behavior_transparent_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_transparent_driver_api = { + .binding_pressed = on_keymap_binding_pressed, + .binding_released = on_keymap_binding_released, +}; + + +static const struct behavior_transparent_config behavior_transparent_config = {}; + +static struct behavior_transparent_data behavior_transparent_data; + +DEVICE_AND_API_INIT(behavior_transparent, DT_INST_LABEL(0), behavior_transparent_init, + &behavior_transparent_data, + &behavior_transparent_config, + APPLICATION, CONFIG_KERNEL_INIT_PRIORITY_DEFAULT, + &behavior_transparent_driver_api); \ No newline at end of file -- cgit v1.2.3 From 55cf9db564e66e2804f2d3f2201c55c3c86a90d7 Mon Sep 17 00:00:00 2001 From: Pete Johanson Date: Mon, 22 Jun 2020 11:06:01 -0400 Subject: Fix consumer keys w/ refactored behaviors. --- app/src/behaviors/behavior_hid.c | 51 ++++++++++++++++++++++------ app/src/behaviors/behavior_key_press.c | 50 ++++++++++++--------------- app/src/behaviors/behavior_mod_tap.c | 11 +++--- app/src/behaviors/behavior_momentary_layer.c | 8 ++--- 4 files changed, 71 insertions(+), 49 deletions(-) (limited to 'app/src/behaviors') diff --git a/app/src/behaviors/behavior_hid.c b/app/src/behaviors/behavior_hid.c index 0aeade5..1ac7404 100644 --- a/app/src/behaviors/behavior_hid.c +++ b/app/src/behaviors/behavior_hid.c @@ -24,22 +24,53 @@ static int behavior_hid_init(struct device *dev) return 0; }; -static int on_keycode_pressed(struct device *dev, u32_t keycode) +static int on_keycode_pressed(struct device *dev, u8_t usage_page, u32_t keycode) { - enum zmk_hid_report_changes changes; + int err; LOG_DBG("keycode %d", keycode); - changes = zmk_hid_press_key(keycode); - return zmk_endpoints_send_report(changes); + switch (usage_page) { + case USAGE_KEYPAD: + err = zmk_hid_keypad_press(keycode); + if (err) { + LOG_ERR("Unable to press keycode"); + return err; + } + break; + case USAGE_CONSUMER: + err = zmk_hid_consumer_press(keycode); + if (err) { + LOG_ERR("Unable to press keycode"); + return err; + } + break; + } + + return zmk_endpoints_send_report(usage_page); } -static int on_keycode_released(struct device *dev, u32_t keycode) +static int on_keycode_released(struct device *dev, u8_t usage_page, u32_t keycode) { - enum zmk_hid_report_changes changes; + int err; LOG_DBG("keycode %d", keycode); - changes = zmk_hid_release_key(keycode); - return zmk_endpoints_send_report(changes); + switch (usage_page) { + case USAGE_KEYPAD: + err = zmk_hid_keypad_release(keycode); + if (err) { + LOG_ERR("Unable to press keycode"); + return err; + } + break; + case USAGE_CONSUMER: + err = zmk_hid_consumer_release(keycode); + if (err) { + LOG_ERR("Unable to press keycode"); + return err; + } + break; + } + return zmk_endpoints_send_report(usage_page); } static int on_modifiers_pressed(struct device *dev, zmk_mod_flags modifiers) @@ -47,7 +78,7 @@ static int on_modifiers_pressed(struct device *dev, zmk_mod_flags modifiers) LOG_DBG("modifiers %d", modifiers); zmk_hid_register_mods(modifiers); - return zmk_endpoints_send_report(Keypad); + return zmk_endpoints_send_report(USAGE_KEYPAD); } static int on_modifiers_released(struct device *dev, zmk_mod_flags modifiers) @@ -55,7 +86,7 @@ static int on_modifiers_released(struct device *dev, zmk_mod_flags modifiers) LOG_DBG("modifiers %d", modifiers); zmk_hid_unregister_mods(modifiers); - return zmk_endpoints_send_report(Keypad); + return zmk_endpoints_send_report(USAGE_KEYPAD); } static const struct behavior_driver_api behavior_hid_driver_api = { diff --git a/app/src/behaviors/behavior_key_press.c b/app/src/behaviors/behavior_key_press.c index 1b3e670..7213b3c 100644 --- a/app/src/behaviors/behavior_key_press.c +++ b/app/src/behaviors/behavior_key_press.c @@ -14,7 +14,9 @@ LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL); -struct behavior_key_press_config { }; +struct behavior_key_press_config { + u8_t usage_page; +}; struct behavior_key_press_data { }; static int behavior_key_press_init(struct device *dev) @@ -22,44 +24,34 @@ static int behavior_key_press_init(struct device *dev) return 0; }; - -// They keycode is passed by the "keymap" based on the parameter created as part of the assignment. -// Other drivers instead might activate a layer, update the consumer page state, or update the RGB state, etc. -// Returns: -// * > 0 - indicate successful processing, and halt further handling, -// * 0 - Indicate successful processing, and continue propagation. -// * < 0 - Indicate error processing, report and halt further propagation. static int on_keymap_binding_pressed(struct device *dev, u32_t position, u32_t keycode, u32_t _) { - LOG_DBG("position %d keycode %d", position, keycode); - return zmk_events_keycode_pressed(keycode); + const struct behavior_key_press_config *cfg = dev->config_info; + LOG_DBG("position %d usage_page 0x%02X keycode 0x%02X", position, cfg->usage_page, keycode); + return zmk_events_keycode_pressed(cfg->usage_page, keycode); } - -// They keycode is passed by the "keymap" based on the parameter created as part of the assignment. static int on_keymap_binding_released(struct device *dev, u32_t position, u32_t keycode, u32_t _) { - LOG_DBG("position %d keycode %d", position, keycode); - return zmk_events_keycode_released(keycode); + const struct behavior_key_press_config *cfg = dev->config_info; + LOG_DBG("position %d usage_page 0x%02X keycode 0x%02X", position, cfg->usage_page, keycode); + return zmk_events_keycode_released(cfg->usage_page, keycode); } static const struct behavior_driver_api behavior_key_press_driver_api = { - // These callbacks are all optional, and define which kinds of events the behavior can handle. - // They can reference local functions defined here, or shared event handlers. .binding_pressed = on_keymap_binding_pressed, .binding_released = on_keymap_binding_released - // Other optional callbacks a behavior can implement - // .on_mouse_moved - // .on_sensor_data - Any behaviour that wants to be linked to a censor can implement this behavior }; - -static const struct behavior_key_press_config behavior_key_press_config = {}; - -static struct behavior_key_press_data behavior_key_press_data; - -DEVICE_AND_API_INIT(behavior_key_press, DT_INST_LABEL(0), behavior_key_press_init, - &behavior_key_press_data, - &behavior_key_press_config, - APPLICATION, CONFIG_KERNEL_INIT_PRIORITY_DEFAULT, - &behavior_key_press_driver_api); +#define KP_INST(n) \ + static const struct behavior_key_press_config behavior_key_press_config_##n = { \ + .usage_page = DT_INST_PROP(n, usage_page) \ + }; \ + static struct behavior_key_press_data behavior_key_press_data_##n; \ + DEVICE_AND_API_INIT(behavior_key_press_##n, DT_INST_LABEL(n), behavior_key_press_init, \ + &behavior_key_press_data_##n, \ + &behavior_key_press_config_##n, \ + APPLICATION, CONFIG_KERNEL_INIT_PRIORITY_DEFAULT, \ + &behavior_key_press_driver_api); + +DT_INST_FOREACH_STATUS_OKAY(KP_INST) \ No newline at end of file diff --git a/app/src/behaviors/behavior_mod_tap.c b/app/src/behaviors/behavior_mod_tap.c index 7684c29..2e6e339 100644 --- a/app/src/behaviors/behavior_mod_tap.c +++ b/app/src/behaviors/behavior_mod_tap.c @@ -40,17 +40,17 @@ static int on_keymap_binding_released(struct device *dev, u32_t position, u32_t struct behavior_mod_tap_data *data = dev->driver_data; LOG_DBG("mods: %d, keycode: %d", mods, keycode); - zmk_events_modifiers_released(mods); +zmk_events_modifiers_released(mods); if (data->pending_press_positions & BIT(position)) { - zmk_events_keycode_pressed(keycode); + zmk_events_keycode_pressed(USAGE_KEYPAD, keycode); k_msleep(10); - zmk_events_keycode_released(keycode); + zmk_events_keycode_released(USAGE_KEYPAD, keycode); } return 0; } -static int on_keycode_pressed(struct device *dev, u32_t keycode) +static int on_keycode_pressed(struct device *dev, u8_t usage_page, u32_t keycode) { struct behavior_mod_tap_data *data = dev->driver_data; data->pending_press_positions = 0; @@ -58,8 +58,7 @@ static int on_keycode_pressed(struct device *dev, u32_t keycode) return 0; } - -static int on_keycode_released(struct device *dev, u32_t keycode) +static int on_keycode_released(struct device *dev, u8_t usage_page, u32_t keycode) { LOG_DBG("releasing: %d", keycode); return 0; diff --git a/app/src/behaviors/behavior_momentary_layer.c b/app/src/behaviors/behavior_momentary_layer.c index f85d33d..904051e 100644 --- a/app/src/behaviors/behavior_momentary_layer.c +++ b/app/src/behaviors/behavior_momentary_layer.c @@ -24,14 +24,14 @@ static int behavior_mo_init(struct device *dev) }; -static int on_keymap_binding_pressed(struct device *dev, u32_t position, u32_t layer, u32_t _) +static int mo_keymap_binding_pressed(struct device *dev, u32_t position, u32_t layer, u32_t _) { LOG_DBG("position %d layer %d", position, layer); return zmk_keymap_layer_activate(layer); } -static int on_keymap_binding_released(struct device *dev, u32_t position, u32_t layer, u32_t _) +static int mo_keymap_binding_released(struct device *dev, u32_t position, u32_t layer, u32_t _) { LOG_DBG("position %d layer %d", position, layer); @@ -39,8 +39,8 @@ static int on_keymap_binding_released(struct device *dev, u32_t position, u32_t } static const struct behavior_driver_api behavior_mo_driver_api = { - .binding_pressed = on_keymap_binding_pressed, - .binding_released = on_keymap_binding_released + .binding_pressed = mo_keymap_binding_pressed, + .binding_released = mo_keymap_binding_released }; -- cgit v1.2.3