diff options
author | Pete Johanson <peter@peterjohanson.com> | 2020-05-03 15:49:33 -0400 |
---|---|---|
committer | Pete Johanson <peter@peterjohanson.com> | 2020-05-03 15:49:33 -0400 |
commit | fe961d54a351a793c9037f78a42ae07eddec7d69 (patch) | |
tree | 32a2c446adb875c4027d6935e1e27b0e7774c8a0 | |
parent | 7b4394b3e775e4be06dd80a3f967b0c28ba74d14 (diff) |
Start to get layers + keymaps pulled in from DTS.
-rw-r--r-- | CMakeLists.txt | 1 | ||||
-rw-r--r-- | boards/shields/petejohanson_handwire/petejohanson_handwire.overlay | 30 | ||||
-rw-r--r-- | dts/bindings/zmk,keymap.yaml | 13 | ||||
-rw-r--r-- | dts/bindings/zmk,layers.yaml | 14 | ||||
-rw-r--r-- | src/keymap.c | 34 | ||||
-rw-r--r-- | src/keymap.h | 15 | ||||
-rw-r--r-- | src/kscan.c | 6 | ||||
-rw-r--r-- | src/main.c | 5 | ||||
-rw-r--r-- | src/zmk.h | 8 |
9 files changed, 113 insertions, 13 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt index b6e66ba..7c2e065 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -9,5 +9,6 @@ project(zmk) # Add your source file to the "app" target. This must come after # find_package(Zephyr) which defines the target. target_sources(app PRIVATE src/kscan.c) +target_sources(app PRIVATE src/keymap.c) target_sources(app PRIVATE src/main.c) diff --git a/boards/shields/petejohanson_handwire/petejohanson_handwire.overlay b/boards/shields/petejohanson_handwire/petejohanson_handwire.overlay index 678070e..222762e 100644 --- a/boards/shields/petejohanson_handwire/petejohanson_handwire.overlay +++ b/boards/shields/petejohanson_handwire/petejohanson_handwire.overlay @@ -1,5 +1,35 @@ / { + chosen { + zmk,keymap = &keymap0; + }; + + layers { + compatible = "zmk,layers"; + + default: layer_0 { + label = "Default"; + keys = <10 2 3 8>; + }; + + lower: layer_1 { + label = "Default"; + keys = <9 2 3 5>; + }; + + raise: layer_2 { + label = "Default"; + keys = <8 1 2 3>; + }; + }; + + keymap0: keymap { + compatible = "zmk,keymap"; + + label ="Default keymap"; + layers = <&default &lower &raise>; + }; + kscan { compatible = "gpio-kscan"; label = "Handwired GPIO KSCAN matrix"; diff --git a/dts/bindings/zmk,keymap.yaml b/dts/bindings/zmk,keymap.yaml new file mode 100644 index 0000000..8c56f93 --- /dev/null +++ b/dts/bindings/zmk,keymap.yaml @@ -0,0 +1,13 @@ +description: | + Allows defining a keymap composed of multiple layers + +compatible: "zmk,keymap" + +properties: + label: + type: string + required: true + + layers: + type: phandles + required: true diff --git a/dts/bindings/zmk,layers.yaml b/dts/bindings/zmk,layers.yaml new file mode 100644 index 0000000..c9b462a --- /dev/null +++ b/dts/bindings/zmk,layers.yaml @@ -0,0 +1,14 @@ +description: | + Allows defining the various keymap layers for use. + +compatible: "zmk,layers" + +child-binding: + description: "A layer to be used in a keymap" + + properties: + label: + type: string + keys: + type: array + diff --git a/src/keymap.c b/src/keymap.c new file mode 100644 index 0000000..ecfce59 --- /dev/null +++ b/src/keymap.c @@ -0,0 +1,34 @@ + +#include "keymap.h" + +static enum hid_kbd_code zmk_keymap[ZMK_KEYMAP_LAYERS_LEN][ZMK_MATRIX_ROWS * ZMK_MATRIX_COLS] = { +#if DT_PROP_HAS_IDX(ZMK_KEYMAP_NODE,layers,0) + DT_PROP_BY_PHANDLE_IDX(ZMK_KEYMAP_NODE,layers,0,keys), +#endif +#if DT_PROP_HAS_IDX(ZMK_KEYMAP_NODE,layers,1) + DT_PROP_BY_PHANDLE_IDX(ZMK_KEYMAP_NODE,layers,1,keys), +#endif +#if DT_PROP_HAS_IDX(ZMK_KEYMAP_NODE,layers,2) + DT_PROP_BY_PHANDLE_IDX(ZMK_KEYMAP_NODE,layers,2,keys), +#endif +#if DT_PROP_HAS_IDX(ZMK_KEYMAP_NODE,layers,3) + DT_PROP_BY_PHANDLE_IDX(ZMK_KEYMAP_NODE,layers,3,keys), +#endif +#if DT_PROP_HAS_IDX(ZMK_KEYMAP_NODE,layers,4) + DT_PROP_BY_PHANDLE_IDX(ZMK_KEYMAP_NODE,layers,4,keys), +#endif +#if DT_PROP_HAS_IDX(ZMK_KEYMAP_NODE,layers,5) + DT_PROP_BY_PHANDLE_IDX(ZMK_KEYMAP_NODE,layers,5,keys), +#endif +#if DT_PROP_HAS_IDX(ZMK_KEYMAP_NODE,layers,6) + DT_PROP_BY_PHANDLE_IDX(ZMK_KEYMAP_NODE,layers,6,keys), +#endif +#if DT_PROP_HAS_IDX(ZMK_KEYMAP_NODE,layers,7) + DT_PROP_BY_PHANDLE_IDX(ZMK_KEYMAP_NODE,layers,7,keys), +#endif +}; + +enum hid_kbd_code zmk_keymap_keycode_from_position(u32_t row, u32_t column) +{ + return zmk_keymap[0][(row * ZMK_MATRIX_ROWS) + column]; +} diff --git a/src/keymap.h b/src/keymap.h index 4456d08..1fa606f 100644 --- a/src/keymap.h +++ b/src/keymap.h @@ -5,17 +5,12 @@ #include <usb/usb_device.h> #include <usb/class/usb_hid.h> -// TODO: Pull these in fro a kscan_gpio.h file from Zephyr! -// -#define MATRIX_NODE_ID DT_PATH(kscan) -#define MATRIX_ROWS DT_PROP_LEN(MATRIX_NODE_ID,row_gpios) -#define MATRIX_COLS DT_PROP_LEN(MATRIX_NODE_ID,col_gpios) +#include "zmk.h" -enum hid_kbd_code keymap[MATRIX_ROWS][MATRIX_COLS] = { - { HID_KEY_A, HID_KEY_B }, - { HID_KEY_C, HID_KEY_D } -}; +#define ZMK_KEYMAP_NODE DT_CHOSEN(zmk_keymap) +#define ZMK_KEYMAP_LAYERS_LEN DT_PROP_LEN(ZMK_KEYMAP_NODE,layers) -#define zmk_keymap_keycode_from_position(row, column) keymap[row][column] + +enum hid_kbd_code zmk_keymap_keycode_from_position(u32_t row, u32_t column); #endif diff --git a/src/kscan.c b/src/kscan.c index 0239e76..6815aa2 100644 --- a/src/kscan.c +++ b/src/kscan.c @@ -35,6 +35,7 @@ static void zmk_kscan_callback(struct device *dev, u32_t row, u32_t column, bool .column = column, .state = (pressed ? ZMK_KSCAN_EVENT_STATE_PRESSED : ZMK_KSCAN_EVENT_STATE_RELEASED) }; + printk("Row: %d, col: %d, pressed: %s\n", ev.row, ev.column, (pressed ? "true" : "false")); k_msgq_put(&zmk_kscan_msgq, &ev, K_NO_WAIT); k_work_submit(&msg_processor.work); @@ -49,7 +50,7 @@ void zmk_kscan_process_msgq(struct k_work *item) bool pressed = (ev.state == ZMK_KSCAN_EVENT_STATE_PRESSED); // TODO: More than basic mapping, layers, etc. enum hid_kbd_code code = zmk_keymap_keycode_from_position(ev.row, ev.column); - printk("Row: %d, col: %d, pressed: %s\n", ev.row, ev.column, (pressed ? "true" : "false")); + printk("Row: %d, col: %d, code: %d, pressed: %s\n", ev.row, ev.column, code, (pressed ? "true" : "false")); } } @@ -61,10 +62,11 @@ int zmk_kscan_init(char* name) return -EINVAL; } - return 0; k_work_init(&msg_processor.work, zmk_kscan_process_msgq); kscan_config(dev, zmk_kscan_callback); kscan_enable_callback(dev); + + return 0; } @@ -14,5 +14,8 @@ void main(void) { printk("Welcome to ZMK!\n"); - zmk_kscan_init(CONFIG_KSCAN_MATRIX_DEV_NAME); + if (zmk_kscan_init(CONFIG_KSCAN_MATRIX_DEV_NAME) != 0) { + printk("Keyboard Scan Init Failed\n"); + return; + } } diff --git a/src/zmk.h b/src/zmk.h new file mode 100644 index 0000000..a5a5ff1 --- /dev/null +++ b/src/zmk.h @@ -0,0 +1,8 @@ +#ifndef ZMK_H +#define ZMK_H + +#define __ZMK_MATRIX_NODE_ID DT_PATH(kscan) +#define ZMK_MATRIX_ROWS DT_PROP_LEN(__ZMK_MATRIX_NODE_ID,row_gpios) +#define ZMK_MATRIX_COLS DT_PROP_LEN(__ZMK_MATRIX_NODE_ID,col_gpios) + +#endif |