From f478438f01e081514f255ce67f7b75935ee7b51a Mon Sep 17 00:00:00 2001 From: Pete Johanson Date: Mon, 18 May 2020 13:42:41 -0400 Subject: [PATCH] Implement raise/lower for default keymap. * Properly allow includes for keymaps to allow defining custom keycodes, and then adding them to the keymap.overlay file. * Fix keymap.c loading after build refactor. * Fix for deactivating a layer working properly. * Fix default keymap to put transparent in proper spots to make raise/lower keycodes work as expected. * Add custom raise/lower keycode handling to default keymap. --- CMakeLists.txt | 4 +-- .../keymaps/default/include/keymap.h | 5 ++++ .../keymaps/default/keymap.c | 27 +++++++++++++++++++ .../keymaps/default/keymap.overlay | 13 ++++----- cmake/keymap.cmake | 14 +++++++--- include/dt-bindings/zmk/keys.h | 6 ++--- include/zmk/keymap.h | 10 +++++++ src/keymap.c | 12 ++++----- 8 files changed, 69 insertions(+), 22 deletions(-) create mode 100644 boards/shields/petejohanson_handwire/keymaps/default/include/keymap.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 9348b9ce..3bc1b289 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -9,9 +9,9 @@ include(cmake/keymap.cmake) find_package(Zephyr) project(zmk) -if(EXISTS ${KEYMAP_DIR}/${KEYMAP}/keymap.c) +if(EXISTS ${KEYMAP_DIR}/keymap.c) message(STATUS "ADDING THE KEYMAP SOURCE") - target_sources(app PRIVATE ${KEYMAP_DIR}/${KEYMAP}/keymap.c) + target_sources(app PRIVATE ${KEYMAP_DIR}/keymap.c) endif() diff --git a/boards/shields/petejohanson_handwire/keymaps/default/include/keymap.h b/boards/shields/petejohanson_handwire/keymaps/default/include/keymap.h new file mode 100644 index 00000000..01cd9e5b --- /dev/null +++ b/boards/shields/petejohanson_handwire/keymaps/default/include/keymap.h @@ -0,0 +1,5 @@ + +#include + +#define CC_RAIS ZC_CSTM(1) +#define CC_LOWR ZC_CSTM(2) \ No newline at end of file diff --git a/boards/shields/petejohanson_handwire/keymaps/default/keymap.c b/boards/shields/petejohanson_handwire/keymaps/default/keymap.c index 349d7557..067cd08e 100644 --- a/boards/shields/petejohanson_handwire/keymaps/default/keymap.c +++ b/boards/shields/petejohanson_handwire/keymaps/default/keymap.c @@ -1,7 +1,34 @@ #include +#include +#include bool zmk_handle_key_user(struct zmk_key_event *key_event) { + switch (key_event->key) + { + case CC_LOWR: + if (key_event->pressed) + { + zmk_keymap_layer_activate(1); + } + else + { + zmk_keymap_layer_deactivate(1); + } + + return false; + case CC_RAIS: + if (key_event->pressed) + { + zmk_keymap_layer_activate(2); + } + else + { + zmk_keymap_layer_deactivate(2); + } + return false; + } + return true; }; diff --git a/boards/shields/petejohanson_handwire/keymaps/default/keymap.overlay b/boards/shields/petejohanson_handwire/keymaps/default/keymap.overlay index 9e7d3d80..ccc42303 100644 --- a/boards/shields/petejohanson_handwire/keymaps/default/keymap.overlay +++ b/boards/shields/petejohanson_handwire/keymaps/default/keymap.overlay @@ -1,4 +1,5 @@ #include +#include / { chosen { @@ -14,22 +15,22 @@ compatible = "zmk,layers"; default: layer_0 { - label = "Default"; + label = "DEFAULT"; keys = < KC_A KC_B - KC_C ZC_CSTM(1) + CC_RAIS CC_LOWR >; }; lower: layer_1 { - label = "Lower"; - keys = ; + label = "LOWER"; + keys = ; }; raise: layer_2 { - label = "Raise"; - keys = ; + label = "RAISE"; + keys = ; }; }; }; diff --git a/cmake/keymap.cmake b/cmake/keymap.cmake index e580e1ea..2d3c6292 100644 --- a/cmake/keymap.cmake +++ b/cmake/keymap.cmake @@ -76,13 +76,19 @@ foreach(root ${BOARD_ROOT}) endif() endforeach() -find_path(KEYMAP_DIR +find_path(BASE_KEYMAPS_DIR NAMES ${KEYMAP}/keymap.overlay PATHS ${KEYMAP_DIRS} NO_DEFAULT_PATH ) -if (KEYMAP_DIR) - message(STATUS "Using keymap directory: ${KEYMAP_DIR}/${KEYMAP}/") - set(DTC_OVERLAY_FILE ${KEYMAP_DIR}/${KEYMAP}/keymap.overlay) +if (BASE_KEYMAPS_DIR) + set(KEYMAP_DIR "${BASE_KEYMAPS_DIR}/${KEYMAP}" CACHE STRING "Selected keymap directory") + message(STATUS "Using keymap directory: ${KEYMAP_DIR}/") + # Used to let local imports of custom keycodes work as expected + list(APPEND DTS_ROOT ${KEYMAP_DIR}) + if (EXISTS "${KEYMAP_DIR}/include") + include_directories("${KEYMAP_DIR}/include") + endif() + set(DTC_OVERLAY_FILE ${KEYMAP_DIR}/keymap.overlay) endif() diff --git a/include/dt-bindings/zmk/keys.h b/include/dt-bindings/zmk/keys.h index bf3fcc08..4c975dbe 100644 --- a/include/dt-bindings/zmk/keys.h +++ b/include/dt-bindings/zmk/keys.h @@ -66,9 +66,7 @@ #define KC_ALT 0x02 #define KC_CTRL 0x03 -#define ZC_RAIS 0xF0 -#define ZC_LOWR 0xF1 -#define ZC_TRNS 0xF2 -#define ZC_NOOP 0xF3 +#define ZC_TRNS 0xF0 +#define ZC_NOOP 0xF1 #define ZC_CSTM(n) (0xFF + n) \ No newline at end of file diff --git a/include/zmk/keymap.h b/include/zmk/keymap.h index 9b58cc0f..e575774d 100644 --- a/include/zmk/keymap.h +++ b/include/zmk/keymap.h @@ -11,6 +11,16 @@ #define ZMK_KEYMAP_NODE DT_CHOSEN(zmk_keymap) #define ZMK_KEYMAP_LAYERS_LEN DT_PROP_LEN(ZMK_KEYMAP_NODE, layers) +#define _ZMK_LAYER_ENUM_ITEM(label) DT_CAT(label, _layer) + +#define _ZMK_KEYMAP_GENERATE_LAYER_CONST(node_id) \ + _ZMK_LAYER_ENUM_ITEM(DT_NODELABEL(node_id)), + +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); diff --git a/src/keymap.c b/src/keymap.c index b47809b7..b8dfb6f1 100644 --- a/src/keymap.c +++ b/src/keymap.c @@ -31,12 +31,12 @@ static zmk_key zmk_keymap[ZMK_KEYMAP_LAYERS_LEN][ZMK_MATRIX_ROWS * ZMK_MATRIX_CO #endif }; -#define SET_LAYER_STATE(layer, state) \ - if (layer >= 32) \ - { \ - return false; \ - } \ - WRITE_BIT(zmk_keymap_layer_state, layer, true); \ +#define SET_LAYER_STATE(layer, state) \ + if (layer >= 32) \ + { \ + return false; \ + } \ + WRITE_BIT(zmk_keymap_layer_state, layer, state); \ return true; bool zmk_keymap_layer_activate(u8_t layer)