diff options
author | Pete Johanson <peter@peterjohanson.com> | 2020-06-25 14:39:08 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-06-25 14:39:08 -0400 |
commit | c457d9880d31bf638272495be1891adf172bcaee (patch) | |
tree | 51d57bfea704c616ef9357bfba9bffcaf113d062 /app/include | |
parent | b1cab32c66d19d453b89fcec6f4211367e45827a (diff) | |
parent | f2f9d4502122a332aa6f35ac87d9f6b5e3ff7335 (diff) |
Merge pull request #32 from petejohanson/core/refactor-keymap-to-behaviors
Refactor keymap to behaviors
Diffstat (limited to 'app/include')
-rw-r--r-- | app/include/drivers/behavior.h | 236 | ||||
-rw-r--r-- | app/include/dt-bindings/zmk/keys.h | 236 | ||||
-rw-r--r-- | app/include/zmk/behavior.h | 8 | ||||
-rw-r--r-- | app/include/zmk/endpoints.h | 3 | ||||
-rw-r--r-- | app/include/zmk/events.h | 15 | ||||
-rw-r--r-- | app/include/zmk/hid.h | 57 | ||||
-rw-r--r-- | app/include/zmk/keymap.h | 28 | ||||
-rw-r--r-- | app/include/zmk/matrix.h | 2 | ||||
-rw-r--r-- | app/include/zmk/matrix_transform.h | 3 |
9 files changed, 396 insertions, 192 deletions
diff --git a/app/include/drivers/behavior.h b/app/include/drivers/behavior.h new file mode 100644 index 0000000..cca9272 --- /dev/null +++ b/app/include/drivers/behavior.h @@ -0,0 +1,236 @@ +/* + * Copyright (c) 2020 Peter Johanson + * + * SPDX-License-Identifier: MIT + */ + +#pragma once + +#include <zephyr/types.h> +#include <stddef.h> +#include <device.h> +#include <zmk/keys.h> + +#ifdef __cplusplus +extern "C" { +#endif + +/** + * @cond INTERNAL_HIDDEN + * + * Behavior driver API definition and system call entry points. + * + * (Internal use only.) + */ + +typedef int (*behavior_position_callback_t)(struct device *dev, u32_t position); +typedef int (*behavior_keymap_binding_callback_t)(struct device *dev, u32_t position, u32_t param1, u32_t param2); +typedef int (*behavior_keycode_callback_t)(struct device *dev, u8_t usage_page, u32_t keycode); +typedef int (*behavior_modifiers_callback_t)(struct device *dev, zmk_mod_flags modifiers); + +__subsystem struct behavior_driver_api { + behavior_position_callback_t position_pressed; + behavior_position_callback_t position_released; + behavior_keymap_binding_callback_t binding_pressed; + behavior_keymap_binding_callback_t binding_released; + behavior_keycode_callback_t keycode_pressed; + behavior_keycode_callback_t keycode_released; + behavior_modifiers_callback_t modifiers_pressed; + behavior_modifiers_callback_t modifiers_released; +}; +/** + * @endcond + */ + +/** + * @brief Handle the key position being pressed + * @param dev Pointer to the device structure for the driver instance. + * @param position They key position that was pressed + * + * @retval 0 If successful. + * @retval Negative errno code if failure. + */ +__syscall int behavior_position_pressed(struct device *dev, u32_t position); + +static inline int z_impl_behavior_position_pressed(struct device *dev, u32_t position) +{ + const struct behavior_driver_api *api = + (const struct behavior_driver_api *)dev->driver_api; + + if (api->position_pressed == NULL) { + return -ENOTSUP; + } + + return api->position_pressed(dev, position); +} + +/** + * @brief Handle the key position being released + * @param dev Pointer to the device structure for the driver instance. + * @param position They key position that was released + * + * @retval 0 If successful. + * @retval Negative errno code if failure. + */ +__syscall int behavior_position_released(struct device *dev, u32_t position); + +static inline int z_impl_behavior_position_released(struct device *dev, u32_t position) +{ + const struct behavior_driver_api *api = + (const struct behavior_driver_api *)dev->driver_api; + + if (api->position_released == NULL) { + return -ENOTSUP; + } + + return api->position_released(dev, position); +} + +/** + * @brief Handle the keymap binding being pressed + * @param dev Pointer to the device structure for the driver instance. + * @param param1 User parameter specified at time of behavior binding. + * @param param2 User parameter specified at time of behavior binding. + * + * @retval 0 If successful. + * @retval Negative errno code if failure. + */ +__syscall int behavior_keymap_binding_pressed(struct device *dev, u32_t position, u32_t param1, u32_t param2); + +static inline int z_impl_behavior_keymap_binding_pressed(struct device *dev, u32_t position, u32_t param1, u32_t param2) +{ + const struct behavior_driver_api *api = + (const struct behavior_driver_api *)dev->driver_api; + + if (api->binding_pressed == NULL) { + return -ENOTSUP; + } + + return api->binding_pressed(dev, position, param1, param2); +} + +/** + * @brief Handle the assigned position being pressed + * @param dev Pointer to the device structure for the driver instance. + * @param param1 User parameter specified at time of behavior assignment. + * + * @retval 0 If successful. + * @retval Negative errno code if failure. + */ +__syscall int behavior_keymap_binding_released(struct device *dev, u32_t position, u32_t param1, u32_t param2); + +static inline int z_impl_behavior_keymap_binding_released(struct device *dev, u32_t position, u32_t param1, u32_t param2) +{ + const struct behavior_driver_api *api = + (const struct behavior_driver_api *)dev->driver_api; + + if (api->binding_released == NULL) { + return -ENOTSUP; + } + + return api->binding_released(dev, position, param1, param2); +} + + +/** + * @brief Handle the keycode being pressed + * @param dev Pointer to the device structure for the driver instance. + * @param usage_page The usage page for the keycode. + * @param keycode The keycode that is being pressed. + * + * @retval 0 If successful. + * @retval Negative errno code if failure. + */ +__syscall int behavior_keycode_pressed(struct device *dev, u8_t usage_page, u32_t keycode); + +static inline int z_impl_behavior_keycode_pressed(struct device *dev, u8_t usage_page, u32_t keycode) +{ + const struct behavior_driver_api *api = + (const struct behavior_driver_api *)dev->driver_api; + + if (api->keycode_pressed == NULL) { + return -ENOTSUP; + } + + return api->keycode_pressed(dev, usage_page, keycode); +} + + +/** + * @brief Handle the keycode being released + * @param dev Pointer to the device structure for the driver instance. + * @param usage_page The usage page for the keycode. + * @param keycode The keycode that is being pressed. + * + * @retval 0 If successful. + * @retval Negative errno code if failure. + */ +__syscall int behavior_keycode_released(struct device *dev, u8_t usage_page, u32_t keycode); + +static inline int z_impl_behavior_keycode_released(struct device *dev, u8_t usage_page, u32_t keycode) +{ + const struct behavior_driver_api *api = + (const struct behavior_driver_api *)dev->driver_api; + + if (api->keycode_released == NULL) { + return -ENOTSUP; + } + + return api->keycode_released(dev, usage_page, keycode); +} + + +/** + * @brief Handle the keycode being pressed + * @param dev Pointer to the device structure for the driver instance. + * @param keycode The keycode that is being pressed. + * + * @retval 0 If successful. + * @retval Negative errno code if failure. + */ +__syscall int behavior_modifiers_pressed(struct device *dev, zmk_mod_flags modifiers); + +static inline int z_impl_behavior_modifiers_pressed(struct device *dev, zmk_mod_flags modifiers) +{ + const struct behavior_driver_api *api = + (const struct behavior_driver_api *)dev->driver_api; + + if (api->modifiers_pressed == NULL) { + return -ENOTSUP; + } + + return api->modifiers_pressed(dev, modifiers); +} + + +/** + * @brief Handle the keycode being released + * @param dev Pointer to the device structure for the driver instance. + * @param keycode The keycode that is being pressed. + * + * @retval 0 If successful. + * @retval Negative errno code if failure. + */ +__syscall int behavior_modifiers_released(struct device *dev, zmk_mod_flags modifiers); + +static inline int z_impl_behavior_modifiers_released(struct device *dev, zmk_mod_flags modifiers) +{ + const struct behavior_driver_api *api = + (const struct behavior_driver_api *)dev->driver_api; + + if (api->modifiers_released == NULL) { + return -ENOTSUP; + } + + return api->modifiers_released(dev, modifiers); +} + +#ifdef __cplusplus +} +#endif + +/** + * @} + */ + +#include <syscalls/behavior.h> diff --git a/app/include/dt-bindings/zmk/keys.h b/app/include/dt-bindings/zmk/keys.h index c91d27b..c7003c6 100644 --- a/app/include/dt-bindings/zmk/keys.h +++ b/app/include/dt-bindings/zmk/keys.h @@ -1,122 +1,120 @@ #pragma once -#define KC_A 0x04 -#define KC_B 0x05 -#define KC_C 0x06 -#define KC_D 0x07 -#define KC_E 0x08 -#define KC_F 0x09 -#define KC_G 0x0A -#define KC_H 0x0B -#define KC_I 0x0C -#define KC_J 0x0D -#define KC_K 0x0E -#define KC_L 0x0F -#define KC_M 0x10 -#define KC_N 0x11 -#define KC_O 0x12 -#define KC_P 0x13 -#define KC_Q 0x14 -#define KC_R 0x15 -#define KC_S 0x16 -#define KC_T 0x17 -#define KC_U 0x18 -#define KC_V 0x19 -#define KC_W 0x1A -#define KC_X 0x1B -#define KC_Y 0x1C -#define KC_Z 0x1D -#define KC_1 0x1E -#define KC_2 0x1F -#define KC_3 0x20 -#define KC_4 0x21 -#define KC_5 0x22 -#define KC_6 0x23 -#define KC_7 0x24 -#define KC_8 0x25 -#define KC_9 0x26 -#define KC_0 0x27 -#define KC_RET 0x28 -#define KC_ESC 0x29 -#define KC_DEL 0x2A -#define KC_BKSP KC_DEL -#define KC_TAB 0x2B -#define KC_SPC 0x2C -#define KC_MIN 0x2D -#define KC_EQL 0x2E -#define KC_LBKT 0x2F -#define KC_RBKT 0x30 -#define KC_FSLH 0x31 - -#define KC_SCLN 0x33 -#define KC_QUOT 0x34 -#define KC_GRAV 0x35 -#define KC_CMMA 0x36 -#define KC_DOT 0x37 -#define KC_BSLH 0x38 -#define KC_CLCK 0x39 -#define KC_F1 0x3A -#define KC_F2 0x3B - -#define KC_RARW 0x4F -#define KC_LARW 0x50 -#define KC_DARW 0x51 -#define KC_UARW 0x52 - -#define KC_KDIV 0x54 -#define KC_KMLT 0x55 -#define KC_KMIN 0x56 -#define KC_KPLS 0x57 - -#define KC_APP 0x65 - -#define KC_CURU 0xB4 - -#define KC_LPRN 0xB6 -#define KC_RPRN 0xB7 -#define KC_LCUR 0xB8 -#define KC_RCUR 0xB9 - -#define KC_CRRT 0xC3 -#define KC_PRCT 0xC4 -#define KC_LABT 0xC5 -#define KC_RABT 0xC6 -#define KC_AMPS 0xC7 -#define KC_PIPE 0xC9 -#define KC_COLN 0xCB -#define KC_HASH 0xCC -#define KC_KSPC 0xCD -#define KC_ATSN 0xCE -#define KC_BANG 0xCF - -#define KC_LCTL 0xE0 -#define KC_LSFT 0xE1 -#define KC_LALT 0xE2 -#define KC_LGUI 0xE3 -#define KC_RCTL 0xE4 -#define KC_RSFT 0xE5 -#define KC_RALT 0xE6 -#define KC_RGUI 0xE7 - -#define KC_VOLU 0x80 -#define KC_VOLD 0x81 +#define USAGE_KEYPAD 0x07 +#define USAGE_CONSUMER 0x0C + +#define A 0x04 +#define B 0x05 +#define C 0x06 +#define D 0x07 +#define E 0x08 +#define F 0x09 +#define G 0x0A +#define H 0x0B +#define I 0x0C +#define J 0x0D +#define K 0x0E +#define L 0x0F +#define M 0x10 +#define N 0x11 +#define O 0x12 +#define P 0x13 +#define Q 0x14 +#define R 0x15 +#define S 0x16 +#define T 0x17 +#define U 0x18 +#define V 0x19 +#define W 0x1A +#define X 0x1B +#define Y 0x1C +#define Z 0x1D +#define NUM_1 0x1E +#define NUM_2 0x1F +#define NUM_3 0x20 +#define NUM_4 0x21 +#define NUM_5 0x22 +#define NUM_6 0x23 +#define NUM_7 0x24 +#define NUM_8 0x25 +#define NUM_9 0x26 +#define NUM_0 0x27 +#define RET 0x28 +#define ESC 0x29 +#define DEL 0x2A +#define BKSP DEL +#define TAB 0x2B +#define SPC 0x2C +#define MINUS 0x2D +#define EQL 0x2E +#define LBKT 0x2F +#define RBKT 0x30 +#define FSLH 0x31 + +#define SCLN 0x33 +#define QUOT 0x34 +#define GRAV 0x35 +#define CMMA 0x36 +#define DOT 0x37 +#define BSLH 0x38 +#define CLCK 0x39 +#define F1 0x3A +#define F2 0x3B + +#define RARW 0x4F +#define LARW 0x50 +#define DARW 0x51 +#define UARW 0x52 + +#define KDIV 0x54 +#define KMLT 0x55 +#define KMIN 0x56 +#define KPLS 0x57 + +#define GUI 0x65 + +#define CURU 0xB4 + +#define LPRN 0xB6 +#define RPRN 0xB7 +#define LCUR 0xB8 +#define RCUR 0xB9 + +#define CRRT 0xC3 +#define PRCT 0xC4 +#define LABT 0xC5 +#define RABT 0xC6 +#define AMPS 0xC7 +#define PIPE 0xC9 +#define COLN 0xCB +#define HASH 0xCC +#define KSPC 0xCD +#define ATSN 0xCE +#define BANG 0xCF + +#define LCTL 0xE0 +#define LSFT 0xE1 +#define LALT 0xE2 +#define LGUI 0xE3 +#define RCTL 0xE4 +#define RSFT 0xE5 +#define RALT 0xE6 +#define RGUI 0xE7 + +#define VOLU 0x80 +#define VOLD 0x81 /* The following are select consumer page usages */ -#define KC_MNXT 0x100 -#define KC_MPRV 0x101 -#define KC_MSTP 0x102 -#define KC_MJCT 0x103 -#define KC_MPLY 0x104 -#define KC_MMUT 0x105 -#define KC_MVLU 0x106 -#define KC_MVLD 0x107 - -#define ZC_TRNS (0xFFFF) -#define ZC_NO (0xFFFF - 1) - -#define ZC_CSTM(n) (0xFFF + n) +#define M_NEXT 0xB5 +#define M_PREV 0xB6 +#define M_STOP 0xB7 +#define M_EJCT 0xB8 +#define M_PLAY 0xCD +#define M_MUTE 0xE2 +#define M_VOLU 0xE9 +#define M_VOLD 0xEA #define MOD_LCTL (1 << 0x00) #define MOD_LSFT (1 << 0x01) @@ -127,16 +125,4 @@ #define MOD_RALT (1 << 0x06) #define MOD_RGUI (1 << 0x07) -#define ZK_ACTION(k) (k >> 24) -#define _ACTION(a) (a << 24) -#define _ACTION_MODS(m) (m << 16) -#define ZK_KEY(a) (a & 0xFFFF) -#define ZK_MODS(a) ((a >> 16) & 0xFF) - #define ZK_IS_CONSUMER(k) (ZK_KEY(k) >= 0x100) - -#define ZMK_ACTION_KEY 0x01 -#define ZMK_ACTION_MOD_TAP 0x01 -#define ZMK_ACTION_ONE_SHOT 0x02 - -#define MT(mods, kc) (_ACTION(ZMK_ACTION_MOD_TAP) + _ACTION_MODS(mods) + kc) diff --git a/app/include/zmk/behavior.h b/app/include/zmk/behavior.h new file mode 100644 index 0000000..63edcc9 --- /dev/null +++ b/app/include/zmk/behavior.h @@ -0,0 +1,8 @@ + +#pragma once + +struct zmk_behavior_binding { + char *behavior_dev; + u32_t param1; + u32_t param2; +};
\ No newline at end of file diff --git a/app/include/zmk/endpoints.h b/app/include/zmk/endpoints.h index 255ca54..624bb4e 100644 --- a/app/include/zmk/endpoints.h +++ b/app/include/zmk/endpoints.h @@ -4,5 +4,4 @@ #include <zmk/hid.h> int zmk_endpoints_init(); -int zmk_endpoints_send_report(enum zmk_hid_report_changes changes); -int zmk_endpoints_send_key_event(struct zmk_key_event key_event); +int zmk_endpoints_send_report(u8_t usage_report); diff --git a/app/include/zmk/events.h b/app/include/zmk/events.h new file mode 100644 index 0000000..5be2beb --- /dev/null +++ b/app/include/zmk/events.h @@ -0,0 +1,15 @@ +#pragma once + +#include <zmk/keys.h> + +int zmk_events_position_pressed(u32_t position); +int zmk_events_position_released(u32_t position); +int zmk_events_keycode_pressed(u8_t usage_page, u32_t keycode); +int zmk_events_keycode_released(u8_t usage_page, u32_t keycode); +int zmk_events_modifiers_pressed(zmk_mod_flags modifiers); +int zmk_events_modifiers_released(zmk_mod_flags modifiers); +int zmk_events_consumer_key_pressed(u32_t usage); +int zmk_events_consumer_key_released(u32_t usage); + +// TODO: Encoders? +// TODO: Sensors?
\ No newline at end of file diff --git a/app/include/zmk/hid.h b/app/include/zmk/hid.h index 7e3560d..138606a 100644 --- a/app/include/zmk/hid.h +++ b/app/include/zmk/hid.h @@ -9,7 +9,7 @@ #define COLLECTION_REPORT 0x03 -#define ZMK_HID_MAX_KEYCODE KC_APP +#define ZMK_HID_MAX_KEYCODE GUI static const u8_t zmk_hid_report_desc[] = { /* USAGE_PAGE (Generic Desktop) */ @@ -108,40 +108,21 @@ static const u8_t zmk_hid_report_desc[] = { 0x00, /* LOGICAL_MAXIMUM (1) */ HID_GI_LOGICAL_MAX(1), - 0x01, - /* USAGE (Scan Next Track) */ - HID_LI_USAGE, - 0xB5, - /* USAGE (Scan Previous Track) */ - HID_LI_USAGE, - 0xB6, - /* USAGE (Stop) */ - HID_LI_USAGE, - 0xB7, - /* USAGE (Eject) */ - HID_LI_USAGE, - 0xB8, - /* USAGE (Media Play/Pause) */ - HID_LI_USAGE, - 0xCD, - /* USAGE (Mute) */ - HID_LI_USAGE, - 0xE2, - /* USAGE (Volume Increment) */ - HID_LI_USAGE, - 0xE9, - /* USAGE (Volume Decrement) */ - HID_LI_USAGE, - 0xEA, + 0xFF, + HID_LI_USAGE_MIN(1), + 0x00, + /* USAGE_MAXIMUM (Keyboard Application) */ + HID_LI_USAGE_MAX(1), + 0xFF, /* INPUT (Data,Ary,Abs) */ - /* REPORT_SIZE (1) */ + /* REPORT_SIZE (8) */ HID_GI_REPORT_SIZE, - 0x01, + 0x08, /* REPORT_COUNT (8) */ HID_GI_REPORT_COUNT, - 0x08, + 0x06, HID_MI_INPUT, - 0x02, + 0x00, /* END COLLECTION */ HID_MI_COLLECTION_END, }; @@ -167,7 +148,7 @@ struct zmk_hid_keypad_report struct zmk_hid_consumer_report_body { - u8_t keys; + u8_t keys[6]; } __packed; struct zmk_hid_consumer_report @@ -176,19 +157,15 @@ struct zmk_hid_consumer_report struct zmk_hid_consumer_report_body body; } __packed; -enum zmk_hid_report_changes -{ - None = 0x00, - Keypad = (0x01 << 0x00), - Consumer = (0x01 << 0x01) -}; - int zmk_hid_register_mod(zmk_mod modifier); int zmk_hid_unregister_mod(zmk_mod modifier); int zmk_hid_register_mods(zmk_mod_flags modifiers); int zmk_hid_unregister_mods(zmk_mod_flags modifiers); -enum zmk_hid_report_changes zmk_hid_press_key(zmk_key key); -enum zmk_hid_report_changes zmk_hid_release_key(zmk_key key); +int zmk_hid_keypad_press(zmk_key key); +int zmk_hid_keypad_release(zmk_key key); + +int zmk_hid_consumer_press(zmk_key key); +int zmk_hid_consumer_release(zmk_key key); struct zmk_hid_keypad_report *zmk_hid_get_keypad_report(); struct zmk_hid_consumer_report *zmk_hid_get_consumer_report(); diff --git a/app/include/zmk/keymap.h b/app/include/zmk/keymap.h index 285b071..4a6bb37 100644 --- a/app/include/zmk/keymap.h +++ b/app/include/zmk/keymap.h @@ -1,28 +1,6 @@ #pragma once -#include <devicetree.h> -#include <usb/usb_device.h> -#include <usb/class/usb_hid.h> -#include <dt-bindings/zmk/keys.h> +int zmk_keymap_layer_activate(u8_t layer); +int zmk_keymap_layer_deactivate(u8_t layer); -#include <zmk/matrix.h> -#include <zmk/keys.h> - -#define ZMK_KEYMAP_NODE DT_CHOSEN(zmk_keymap) -#define ZMK_KEYMAP_LAYERS_LEN DT_PROP_LEN(ZMK_KEYMAP_NODE, layers) - -/* TODO: Need to actually be able to get a NODELABEL from a node id -#define _ZMK_KEYMAP_GENERATE_LAYER_CONST(node_id) \ - DT_NODELABEL_FOR_NODE(node_id)_layer, - -enum zmk_keymap_layer -{ - DT_FOREACH_CHILD(DT_INST(0, zmk_layers), _ZMK_KEYMAP_GENERATE_LAYER_CONST) -}; -*/ - -bool zmk_keymap_layer_activate(u8_t layer); -bool zmk_keymap_layer_deactivate(u8_t layer); - -zmk_key -zmk_keymap_keycode_from_position(u32_t row, u32_t column); +int zmk_keymap_position_state_changed(u32_t position, bool pressed); diff --git a/app/include/zmk/matrix.h b/app/include/zmk/matrix.h index 5397cfb..9fe1a85 100644 --- a/app/include/zmk/matrix.h +++ b/app/include/zmk/matrix.h @@ -1,5 +1,7 @@ #pragma once +#include <devicetree.h> + #define ZMK_MATRIX_NODE_ID DT_CHOSEN(zmk_kscan) #if DT_NODE_HAS_PROP(ZMK_MATRIX_NODE_ID,row_gpios) diff --git a/app/include/zmk/matrix_transform.h b/app/include/zmk/matrix_transform.h new file mode 100644 index 0000000..e9e969a --- /dev/null +++ b/app/include/zmk/matrix_transform.h @@ -0,0 +1,3 @@ +#pragma once + +u32_t zmk_matrix_transform_row_column_to_position(u32_t row, u32_t column);
\ No newline at end of file |