Browse Source

Merge pull request #32 from petejohanson/core/refactor-keymap-to-behaviors

Refactor keymap to behaviors
xmkb
Pete Johanson 4 years ago committed by GitHub
parent
commit
c457d9880d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 11
      app/CMakeLists.txt
  2. 26
      app/boards/native_posix.overlay
  3. 34
      app/boards/shields/kyria/keymap/keymap.overlay
  4. 5
      app/boards/shields/kyria/keymaps/default/include/keymap.h
  5. 34
      app/boards/shields/kyria/keymaps/default/keymap.c
  6. 63
      app/boards/shields/kyria/keymaps/default/keymap.overlay
  7. 33
      app/boards/shields/kyria/kyria.overlay
  8. 5
      app/boards/shields/petejohanson_handwire/keymap/include/keymap.h
  9. 34
      app/boards/shields/petejohanson_handwire/keymap/keymap.c
  10. 25
      app/boards/shields/petejohanson_handwire/keymap/keymap.overlay
  11. 9
      app/boards/shields/petejohanson_proton_handwire/keymap/include/keymap.h
  12. 34
      app/boards/shields/petejohanson_proton_handwire/keymap/keymap.c
  13. 27
      app/boards/shields/petejohanson_proton_handwire/keymap/keymap.overlay
  14. 7
      app/dts/behaviors.dtsi
  15. 9
      app/dts/behaviors/hid.dtsi
  16. 19
      app/dts/behaviors/key_press.dtsi
  17. 9
      app/dts/behaviors/keymap.dtsi
  18. 9
      app/dts/behaviors/mod_tap.dtsi
  19. 9
      app/dts/behaviors/momentary_layer.dtsi
  20. 9
      app/dts/behaviors/reset.dtsi
  21. 9
      app/dts/behaviors/transparent.dtsi
  22. 14
      app/dts/bindings/behaviors/one_param.yaml
  23. 15
      app/dts/bindings/behaviors/two_param.yaml
  24. 11
      app/dts/bindings/behaviors/zero_param.yaml
  25. 8
      app/dts/bindings/behaviors/zmk,behavior-hid.yaml
  26. 13
      app/dts/bindings/behaviors/zmk,behavior-key-press.yaml
  27. 8
      app/dts/bindings/behaviors/zmk,behavior-keymap.yaml
  28. 8
      app/dts/bindings/behaviors/zmk,behavior-mod-tap.yaml
  29. 8
      app/dts/bindings/behaviors/zmk,behavior-momentary-layer.yaml
  30. 8
      app/dts/bindings/behaviors/zmk,behavior-reset.yaml
  31. 8
      app/dts/bindings/behaviors/zmk,behavior-transparent.yaml
  32. 7
      app/dts/bindings/zmk,layers.yaml
  33. 236
      app/include/drivers/behavior.h
  34. 236
      app/include/dt-bindings/zmk/keys.h
  35. 8
      app/include/zmk/behavior.h
  36. 3
      app/include/zmk/endpoints.h
  37. 15
      app/include/zmk/events.h
  38. 57
      app/include/zmk/hid.h
  39. 28
      app/include/zmk/keymap.h
  40. 2
      app/include/zmk/matrix.h
  41. 3
      app/include/zmk/matrix_transform.h
  42. 1
      app/prj.conf
  43. 108
      app/src/behaviors/behavior_hid.c
  44. 57
      app/src/behaviors/behavior_key_press.c
  45. 50
      app/src/behaviors/behavior_keymap.c
  46. 88
      app/src/behaviors/behavior_mod_tap.c
  47. 55
      app/src/behaviors/behavior_momentary_layer.c
  48. 45
      app/src/behaviors/behavior_reset.c
  49. 48
      app/src/behaviors/behavior_transparent.c
  50. 4
      app/src/ble.c
  51. 31
      app/src/endpoints.c
  52. 83
      app/src/events.c
  53. 88
      app/src/handlers.c
  54. 78
      app/src/hid.c
  55. 97
      app/src/keymap.c
  56. 16
      app/src/kscan.c
  57. 1
      app/src/kscan_mock.c
  58. 30
      app/src/matrix_transform.c

11
app/CMakeLists.txt

@ -1,6 +1,7 @@ @@ -1,6 +1,7 @@
# Find Zephyr. This also loads Zephyr's build system.
cmake_minimum_required(VERSION 3.13.1)
set(CONFIG_APPLICATION_DEFINED_SYSCALL true)
list(APPEND BOARD_ROOT ${CMAKE_SOURCE_DIR})
list(APPEND DTS_ROOT ${CMAKE_SOURCE_DIR})
@ -27,8 +28,17 @@ endif() @@ -27,8 +28,17 @@ endif()
# find_package(Zephyr) which defines the target.
target_include_directories(app PRIVATE include)
target_sources(app PRIVATE src/kscan.c)
target_sources(app PRIVATE src/matrix_transform.c)
target_sources(app PRIVATE src/events.c)
target_sources(app PRIVATE src/keymap.c)
target_sources(app PRIVATE src/hid.c)
target_sources(app PRIVATE src/behaviors/behavior_keymap.c)
target_sources(app PRIVATE src/behaviors/behavior_hid.c)
target_sources(app PRIVATE src/behaviors/behavior_key_press.c)
target_sources(app PRIVATE src/behaviors/behavior_reset.c)
target_sources(app PRIVATE src/behaviors/behavior_mod_tap.c)
target_sources(app PRIVATE src/behaviors/behavior_momentary_layer.c)
target_sources(app PRIVATE src/behaviors/behavior_transparent.c)
target_sources_ifdef(CONFIG_ZMK_BLE app PRIVATE src/ble.c)
target_sources_ifdef(CONFIG_ZMK_KSCAN_MOCK_DRIVER app PRIVATE src/kscan_mock.c)
target_sources_ifdef(CONFIG_ZMK_KSCAN_COMPOSITE_DRIVER app PRIVATE src/kscan_composite.c)
@ -36,5 +46,4 @@ target_sources_ifdef(CONFIG_ZMK_USB app PRIVATE src/usb_hid.c) @@ -36,5 +46,4 @@ target_sources_ifdef(CONFIG_ZMK_USB app PRIVATE src/usb_hid.c)
target_sources_ifdef(CONFIG_ZMK_BLE app PRIVATE src/hog.c)
target_sources(app PRIVATE src/endpoints.c)
target_sources(app PRIVATE src/main.c)
target_sources(app PRIVATE src/handlers.c)

26
app/boards/native_posix.overlay

@ -1,4 +1,5 @@ @@ -1,4 +1,5 @@
#include <dt-bindings/zmk/keys.h>
#include <behaviors.dtsi>
#include <zmk/kscan-mock.h>
/ {
@ -53,27 +54,26 @@ @@ -53,27 +54,26 @@
default: layer_0 {
label = "DEFAULT";
keys =
<
KC_A MT(MOD_LSFT, KC_B) KC_C KC_D
KC_E KC_F KC_G KC_H
>;
bindings = <
&kp B &mo 1 &kp C &kp E
&kp D &kp G &kp F &kp Q>;
};
lower: layer_1 {
label = "LOWER";
keys = <
KC_A KC_B KC_C KC_D
KC_E KC_F KC_G KC_H
>;
bindings = <
&cp M_NEXT &trans &kp O &kp Q
&kp L &kp J &kp P &kp K>;
};
raise: layer_2 {
label = "RAISE";
keys = <
KC_E KC_F KC_G KC_H
KC_A KC_B KC_C KC_D
>;
bindings = <
&kp W &kp U &kp I &kp N
&kp X &kp M &kp C &kp B>;
};
};
};

34
app/boards/shields/kyria/keymap/keymap.overlay

@ -0,0 +1,34 @@ @@ -0,0 +1,34 @@
#include <behaviors.dtsi>
#include <dt-bindings/zmk/keys.h>
#include <dt-bindings/zmk/matrix-transform.h>
/ {
chosen {
zmk,keymap = &keymap0;
};
keymap0: keymap {
compatible = "zmk,keymap";
label ="Default Kyria Keymap";
layers = <&default>;
};
layers {
compatible = "zmk,layers";
default: layer_0 {
label = "DEFAULT";
// ---------------------------------------------------------------------------------------------------------------------------------
// | ESC | Q | W | E | R | T | | Y | U | I | O | P | "|" |
// | <- | A | S | D | F | G | | H | J | K | L | ; | ' |
// | SHIFT | Z | X | C | V | B | L SHIFT | L SHIFT | | L SHIFT | L SHIFT | N | M | , | . | / | - |
// | GUI | DEL | RET | SPACE | ESC | | RET | SPACE | TAB | BSPC | R-ALT |
bindings = <
&kp ESC &kp Q &kp W &kp E &kp R &kp T &kp Y &kp U &kp I &kp O &kp P &kp PIPE
&kp BKSP &kp A &kp S &kp D &kp F &kp G &kp H &kp J &kp K &kp L &kp SCLN &kp QUOT
&kp LSFT &kp Z &kp X &kp C &kp V &kp B &kp LSFT &kp LSFT &kp LSFT &kp LSFT &kp N &kp M &kp CMMA &kp DOT &kp BSLH &kp MINUS
&kp LGUI &kp DEL &kp RET &kp SPC &kp ESC &kp RET &kp SPC &kp TAB &kp BKSP &kp RALT
>;
};
};
};

5
app/boards/shields/kyria/keymaps/default/include/keymap.h

@ -1,5 +0,0 @@ @@ -1,5 +0,0 @@
#include <dt-bindings/zmk/keys.h>
#define CC_RAIS ZC_CSTM(1)
#define CC_LOWR ZC_CSTM(2)

34
app/boards/shields/kyria/keymaps/default/keymap.c

@ -1,34 +0,0 @@ @@ -1,34 +0,0 @@
#include <zmk/keys.h>
#include <zmk/keymap.h>
#include <keymap.h>
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;
};

63
app/boards/shields/kyria/keymaps/default/keymap.overlay

@ -1,63 +0,0 @@ @@ -1,63 +0,0 @@
#include <dt-bindings/zmk/keys.h>
#include <dt-bindings/zmk/matrix-transform.h>
#include <keymap.h>
/ {
chosen {
zmk,keymap = &keymap0;
};
keymap0: keymap {
compatible = "zmk,keymap";
label ="Default Kyria Keymap";
layers = <&default>;
transform = <&default_transform>;
};
default_transform: keymap_transform_0 {
compatible = "zmk,matrix-transform";
// | MX6 | MX5 | MX4 | MX3 | MX2 | MX1 | | MX1 | MX2 | MX3 | MX4 | MX5 | MX6 |
// | MX12 | MX11 | MX10 | MX9 | MX8 | MX7 | | MX7 | MX8 | MX9 | MX10 | MX11 | MX12 |
// | MX20 | MX19 | MX18 | MX17 | MX16 | MX15 | MX14 | MX13 | | MX13 | MX14 | MX15 | MX16 | MX17 | MX18 | MX19 | MX20 |
// | MX25 | MX24 | MX23 | MX22 | MX21 | | MX21 | MX22 | MX23 | MX24 | MX25 |
map = <
RC(0,0) RC(0,1) RC(0,2) RC(0,3) RC(0,4) RC(0,5) RC(0,10) RC(0,11) RC(0,12) RC(0,13) RC(0,14) RC(0,15)
RC(1,0) RC(1,1) RC(1,2) RC(1,3) RC(1,4) RC(1,5) RC(1,10) RC(1,11) RC(1,12) RC(1,13) RC(1,14) RC(1,15)
RC(2,0) RC(2,1) RC(2,2) RC(2,3) RC(2,4) RC(2,5) RC(2,6) RC(2,7) RC(2,8) RC(2,9) RC(2,10) RC(2,11) RC(2,12) RC(2,13) RC(2,14) RC(2,15)
RC(3,3) RC(3,4) RC(3,5) RC(3,6) RC(3,7) RC(3,8) RC(3,9) RC(3,10) RC(3,11) RC(3,12)
>;
};
// | MX5 | MX4 | MX3 | MX2 | MX1 | | MX1 | MX2 | MX3 | MX4 | MX5 |
// | MX11 | MX10 | MX9 | MX8 | MX7 | | MX7 | MX8 | MX9 | MX10 | MX11 |
// | MX19 | MX18 | MX17 | MX16 | MX15 | MX14 | MX13 | | MX13 | MX14 | MX15 | MX16 | MX17 | MX18 | MX19 |
// | MX25 | MX24 | MX23 | MX22 | MX21 | | MX21 | MX22 | MX23 | MX24 | MX25 |
five_column_transform: keymap_transform_1 {
compatible = "zmk,matrix-transform";
map = <
RC(0,0) RC(0,1) RC(0,2) RC(0,3) RC(0,4) RC(0,9) RC(0,10) RC(0,11) RC(0,12) RC(0,13)
RC(1,0) RC(1,1) RC(1,2) RC(1,3) RC(1,4) RC(1,9) RC(1,10) RC(1,11) RC(1,12) RC(1,13)
RC(2,0) RC(2,1) RC(2,2) RC(2,3) RC(2,4) RC(2,5) RC(2,6) RC(2,7) RC(2,8) RC(2,9) RC(2,10) RC(2,11) RC(2,12) RC(2,13)
RC(3,2) RC(3,3) RC(3,4) RC(3,5) RC(3,6) RC(3,7) RC(3,8) RC(3,9) RC(3,10) RC(3,11)
>;
};
layers {
compatible = "zmk,layers";
default: layer_0 {
label = "DEFAULT";
// ---------------------------------------------------------------------------------------------------------------------------------
// | ESC | Q | W | E | R | T | | Y | U | I | O | P | "|" |
// | <- | A | S | D | F | G | | H | J | K | L | ; | ' |
// | SHIFT | Z | X | C | V | B | L SHIFT | L SHIFT | | L SHIFT | L SHIFT | N | M | , | . | / | - |
// | GUI | DEL | RET | SPACE | ESC | | RET | SPACE | TAB | BSPC | R-ALT |
keys = <
KC_ESC KC_Q KC_W KC_E KC_R KC_T KC_Y KC_U KC_I KC_O KC_P KC_PIPE
KC_BKSP KC_A KC_S KC_D KC_F KC_G KC_H KC_J KC_K KC_L KC_SCLN KC_QUOT
KC_LSFT KC_Z KC_X KC_C KC_V KC_B KC_LSFT KC_LSFT KC_LSFT KC_LSFT KC_N KC_M KC_CMMA KC_DOT KC_BSLH KC_MIN
KC_LGUI KC_DEL KC_RET KC_SPC KC_ESC KC_RET KC_SPC KC_TAB KC_BKSP KC_RALT
>;
};
};
};

33
app/boards/shields/kyria/kyria.overlay

@ -4,9 +4,14 @@ @@ -4,9 +4,14 @@
* SPDX-License-Identifier: MIT
*/
#include <dt-bindings/zmk/matrix-transform.h>
#include <behaviors/keymap.dtsi>
#include <behaviors/hid.dtsi>
/ {
chosen {
zmk,kscan = &kscan0;
zmk,matrix_transform = &default_transform;
};
kscan0: kscan_comp {
@ -28,6 +33,34 @@ @@ -28,6 +33,34 @@
};
};
default_transform: keymap_transform_0 {
compatible = "zmk,matrix-transform";
// | MX6 | MX5 | MX4 | MX3 | MX2 | MX1 | | MX1 | MX2 | MX3 | MX4 | MX5 | MX6 |
// | MX12 | MX11 | MX10 | MX9 | MX8 | MX7 | | MX7 | MX8 | MX9 | MX10 | MX11 | MX12 |
// | MX20 | MX19 | MX18 | MX17 | MX16 | MX15 | MX14 | MX13 | | MX13 | MX14 | MX15 | MX16 | MX17 | MX18 | MX19 | MX20 |
// | MX25 | MX24 | MX23 | MX22 | MX21 | | MX21 | MX22 | MX23 | MX24 | MX25 |
map = <
RC(0,0) RC(0,1) RC(0,2) RC(0,3) RC(0,4) RC(0,5) RC(0,10) RC(0,11) RC(0,12) RC(0,13) RC(0,14) RC(0,15)
RC(1,0) RC(1,1) RC(1,2) RC(1,3) RC(1,4) RC(1,5) RC(1,10) RC(1,11) RC(1,12) RC(1,13) RC(1,14) RC(1,15)
RC(2,0) RC(2,1) RC(2,2) RC(2,3) RC(2,4) RC(2,5) RC(2,6) RC(2,7) RC(2,8) RC(2,9) RC(2,10) RC(2,11) RC(2,12) RC(2,13) RC(2,14) RC(2,15)
RC(3,3) RC(3,4) RC(3,5) RC(3,6) RC(3,7) RC(3,8) RC(3,9) RC(3,10) RC(3,11) RC(3,12)
>;
};
// | MX5 | MX4 | MX3 | MX2 | MX1 | | MX1 | MX2 | MX3 | MX4 | MX5 |
// | MX11 | MX10 | MX9 | MX8 | MX7 | | MX7 | MX8 | MX9 | MX10 | MX11 |
// | MX19 | MX18 | MX17 | MX16 | MX15 | MX14 | MX13 | | MX13 | MX14 | MX15 | MX16 | MX17 | MX18 | MX19 |
// | MX25 | MX24 | MX23 | MX22 | MX21 | | MX21 | MX22 | MX23 | MX24 | MX25 |
five_column_transform: keymap_transform_1 {
compatible = "zmk,matrix-transform";
map = <
RC(0,0) RC(0,1) RC(0,2) RC(0,3) RC(0,4) RC(0,9) RC(0,10) RC(0,11) RC(0,12) RC(0,13)
RC(1,0) RC(1,1) RC(1,2) RC(1,3) RC(1,4) RC(1,9) RC(1,10) RC(1,11) RC(1,12) RC(1,13)
RC(2,0) RC(2,1) RC(2,2) RC(2,3) RC(2,4) RC(2,5) RC(2,6) RC(2,7) RC(2,8) RC(2,9) RC(2,10) RC(2,11) RC(2,12) RC(2,13)
RC(3,2) RC(3,3) RC(3,4) RC(3,5) RC(3,6) RC(3,7) RC(3,8) RC(3,9) RC(3,10) RC(3,11)
>;
};
kscan_left: kscan_left {
compatible = "zmk,kscan-gpio-matrix";
label = "KSCAN_LEFT";

5
app/boards/shields/petejohanson_handwire/keymap/include/keymap.h

@ -1,5 +0,0 @@ @@ -1,5 +0,0 @@
#include <dt-bindings/zmk/keys.h>
#define CC_RAIS ZC_CSTM(1)
#define CC_LOWR ZC_CSTM(2)

34
app/boards/shields/petejohanson_handwire/keymap/keymap.c

@ -1,34 +0,0 @@ @@ -1,34 +0,0 @@
#include <zmk/keys.h>
#include <zmk/keymap.h>
#include <keymap.h>
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;
};

25
app/boards/shields/petejohanson_handwire/keymap/keymap.overlay

@ -1,10 +1,11 @@ @@ -1,10 +1,11 @@
#include <dt-bindings/zmk/keys.h>
#include <keymap.h>
#include <behaviors.dtsi>
/ {
chosen {
zmk,keymap = &keymap0;
};
keymap0: keymap {
compatible = "zmk,keymap";
label ="Default keymap";
@ -16,26 +17,24 @@ @@ -16,26 +17,24 @@
default: layer_0 {
label = "DEFAULT";
keys =
<
KC_A MT(MOD_LSFT, KC_B) ZC_NO ZC_NO
CC_RAIS CC_LOWR ZC_NO ZC_NO
>;
bindings = <
&cp M_NEXT &mt MOD_LSFT B &trans &trans
&mo 1 &mo 2 &trans &trans>;
};
lower: layer_1 {
label = "LOWER";
keys = <
KC_MPLY KC_MNXT ZC_NO ZC_NO
ZC_TRNS ZC_TRNS ZC_NO ZC_NO
>;
bindings = <
&cp M_PLAY &cp M_NEXT &trans &trans
&trans &trans &trans &trans>;
};
raise: layer_2 {
label = "RAISE";
keys = <
KC_C KC_D ZC_NO ZC_NO
ZC_TRNS ZC_TRNS ZC_NO ZC_NO>;
bindings = <
&kp C &kp D &trans &trans
&trans &kp E &trans &trans>;
};
};
};

9
app/boards/shields/petejohanson_proton_handwire/keymap/include/keymap.h

@ -1,9 +0,0 @@ @@ -1,9 +0,0 @@
#include <dt-bindings/zmk/keys.h>
#define DEFAULT 0
#define RAISE 1
#define LOWER 2
#define CC_RAIS ZC_CSTM(RAISE)
#define CC_LOWR ZC_CSTM(LOWER)

34
app/boards/shields/petejohanson_proton_handwire/keymap/keymap.c

@ -1,34 +0,0 @@ @@ -1,34 +0,0 @@
#include <zmk/keys.h>
#include <zmk/keymap.h>
#include <keymap.h>
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(LOWER);
}
else
{
zmk_keymap_layer_deactivate(LOWER);
}
return false;
case CC_RAIS:
if (key_event->pressed)
{
zmk_keymap_layer_activate(RAISE);
}
else
{
zmk_keymap_layer_deactivate(RAISE);
}
return false;
}
return true;
};

27
app/boards/shields/petejohanson_proton_handwire/keymap/keymap.overlay

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
#include <behaviors.dtsi>
#include <dt-bindings/zmk/keys.h>
#include <keymap.h>
/ {
chosen {
@ -16,27 +16,26 @@ @@ -16,27 +16,26 @@
default: layer_0 {
label = "DEFAULT";
keys =
<
KC_A MT(MOD_LSFT, KC_B)
CC_RAIS CC_LOWR
>;
bindings = <
&mo 1 &kp A
&mo 2 &kp C>;
};
lower: layer_1 {
label = "LOWER";
keys = <
KC_MPLY KC_MNXT
ZC_TRNS ZC_TRNS
>;
bindings = <
&trans &kp D
&trans &kp E>;
};
raise: layer_2 {
label = "RAISE";
keys = <
KC_C KC_D
ZC_TRNS ZC_TRNS
>;
bindings = <
&trans &kp I
&trans &kp H>;
};
};
};

7
app/dts/behaviors.dtsi

@ -0,0 +1,7 @@ @@ -0,0 +1,7 @@
#include <behaviors/key_press.dtsi>
#include <behaviors/transparent.dtsi>
#include <behaviors/mod_tap.dtsi>
#include <behaviors/momentary_layer.dtsi>
#include <behaviors/reset.dtsi>
#include <behaviors/keymap.dtsi>
#include <behaviors/hid.dtsi>

9
app/dts/behaviors/hid.dtsi

@ -0,0 +1,9 @@ @@ -0,0 +1,9 @@
/ {
behaviors {
hid_behavior: behavior_hid {
compatible = "zmk,behavior-hid", "zmk,behavior-global";
label = "HID";
#binding-cells = <0>;
};
};
};

19
app/dts/behaviors/key_press.dtsi

@ -0,0 +1,19 @@ @@ -0,0 +1,19 @@
#include <dt-bindings/zmk/keys.h>
/ {
behaviors {
kp: behavior_key_press {
compatible = "zmk,behavior-key-press";
label = "KEY_PRESS";
usage_page = <USAGE_KEYPAD>;
#binding-cells = <1>;
};
cp: behavior_consumer_press {
compatible = "zmk,behavior-key-press";
label = "CONSUMER_PRESS";
usage_page = <USAGE_CONSUMER>;
#binding-cells = <1>;
};
};
};

9
app/dts/behaviors/keymap.dtsi

@ -0,0 +1,9 @@ @@ -0,0 +1,9 @@
/ {
behaviors {
keymap_behavior: behavior_keymap {
compatible = "zmk,behavior-keymap", "zmk,behavior-global";
label = "KEYMAP";
#binding-cells = <0>;
};
};
};

9
app/dts/behaviors/mod_tap.dtsi

@ -0,0 +1,9 @@ @@ -0,0 +1,9 @@
/ {
behaviors {
mt: behavior_mod_tap {
compatible = "zmk,behavior-mod-tap", "zmk,behavior-global";
label = "MOD_TAP";
#binding-cells = <2>;
};
};
};

9
app/dts/behaviors/momentary_layer.dtsi

@ -0,0 +1,9 @@ @@ -0,0 +1,9 @@
/ {
behaviors {
mo: behavior_momentary_layer {
compatible = "zmk,behavior-momentary-layer";
label = "MO";
#binding-cells = <1>;
};
};
};

9
app/dts/behaviors/reset.dtsi

@ -0,0 +1,9 @@ @@ -0,0 +1,9 @@
/ {
behaviors {
reset: behavior_reset {
compatible = "zmk,behavior-reset";
label = "RESET";
#binding-cells = <0>;
};
};
};

9
app/dts/behaviors/transparent.dtsi

@ -0,0 +1,9 @@ @@ -0,0 +1,9 @@
/ {
behaviors {
trans: behavior_transparent {
compatible = "zmk,behavior-transparent";
label = "TRANS";
#binding-cells = <0>;
};
};
};

14
app/dts/bindings/behaviors/one_param.yaml

@ -0,0 +1,14 @@ @@ -0,0 +1,14 @@
# Copyright (c) 2020, Pete Johanson
# SPDX-License-Identifier: MIT
properties:
label:
type: string
required: true
"#binding-cells":
type: int
required: true
const: 1
binding-cells:
- param1

15
app/dts/bindings/behaviors/two_param.yaml

@ -0,0 +1,15 @@ @@ -0,0 +1,15 @@
# Copyright (c) 2020, Pete Johanson
# SPDX-License-Identifier: MIT
properties:
label:
type: string
required: true
"#binding-cells":
type: int
required: true
const: 2
binding-cells:
- param1
- param2

11
app/dts/bindings/behaviors/zero_param.yaml

@ -0,0 +1,11 @@ @@ -0,0 +1,11 @@
# Copyright (c) 2020, Pete Johanson
# SPDX-License-Identifier: MIT
properties:
label:
type: string
required: true
"#binding-cells":
type: int
required: true
const: 0

8
app/dts/bindings/behaviors/zmk,behavior-hid.yaml

@ -0,0 +1,8 @@ @@ -0,0 +1,8 @@
# Copyright (c) 2020, Pete Johanson
# SPDX-License-Identifier: MIT
description: HID Report Behavior
compatible: "zmk,behavior-hid"
include: zero_param.yaml

13
app/dts/bindings/behaviors/zmk,behavior-key-press.yaml

@ -0,0 +1,13 @@ @@ -0,0 +1,13 @@
# Copyright (c) 2020, Pete Johanson
# SPDX-License-Identifier: MIT
description: Key press/release behavior
compatible: "zmk,behavior-key-press"
include: one_param.yaml
properties:
usage_page:
type: int
default: 0

8
app/dts/bindings/behaviors/zmk,behavior-keymap.yaml

@ -0,0 +1,8 @@ @@ -0,0 +1,8 @@
# Copyright (c) 2020, Pete Johanson
# SPDX-License-Identifier: MIT
description: Keymap Behavior
compatible: "zmk,behavior-keymap"
include: zero_param.yaml

8
app/dts/bindings/behaviors/zmk,behavior-mod-tap.yaml

@ -0,0 +1,8 @@ @@ -0,0 +1,8 @@
# Copyright (c) 2020, Pete Johanson
# SPDX-License-Identifier: MIT
description: Mod-Tap Beavhior
compatible: "zmk,behavior-mod-tap"
include: two_param.yaml

8
app/dts/bindings/behaviors/zmk,behavior-momentary-layer.yaml

@ -0,0 +1,8 @@ @@ -0,0 +1,8 @@
# Copyright (c) 2020, Pete Johanson
# SPDX-License-Identifier: MIT
description: Momentary layer on press/release behavior
compatible: "zmk,behavior-momentary-layer"
include: one_param.yaml

8
app/dts/bindings/behaviors/zmk,behavior-reset.yaml

@ -0,0 +1,8 @@ @@ -0,0 +1,8 @@
# Copyright (c) 2020, Pete Johanson
# SPDX-License-Identifier: MIT
description: Keyboard Reset Behavior
compatible: "zmk,behavior-reset"
include: zero_param.yaml

8
app/dts/bindings/behaviors/zmk,behavior-transparent.yaml

@ -0,0 +1,8 @@ @@ -0,0 +1,8 @@
# Copyright (c) 2020, Pete Johanson
# SPDX-License-Identifier: MIT
description: Transparent Binding Behavior
compatible: "zmk,behavior-transparent"
include: zero_param.yaml

7
app/dts/bindings/zmk,layers.yaml

@ -9,6 +9,7 @@ child-binding: @@ -9,6 +9,7 @@ child-binding:
properties:
label:
type: string
keys:
type: array
required: true
bindings:
type: phandle-array
required: true

236
app/include/drivers/behavior.h

@ -0,0 +1,236 @@ @@ -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>

236
app/include/dt-bindings/zmk/keys.h

@ -1,122 +1,120 @@ @@ -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 @@ @@ -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)

8
app/include/zmk/behavior.h

@ -0,0 +1,8 @@ @@ -0,0 +1,8 @@
#pragma once
struct zmk_behavior_binding {
char *behavior_dev;
u32_t param1;
u32_t param2;
};

3
app/include/zmk/endpoints.h

@ -4,5 +4,4 @@ @@ -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);

15
app/include/zmk/events.h

@ -0,0 +1,15 @@ @@ -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?

57
app/include/zmk/hid.h

@ -9,7 +9,7 @@ @@ -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[] = { @@ -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 @@ -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 @@ -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();

28
app/include/zmk/keymap.h

@ -1,28 +1,6 @@ @@ -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);

2
app/include/zmk/matrix.h

@ -1,5 +1,7 @@ @@ -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)

3
app/include/zmk/matrix_transform.h

@ -0,0 +1,3 @@ @@ -0,0 +1,3 @@
#pragma once
u32_t zmk_matrix_transform_row_column_to_position(u32_t row, u32_t column);

1
app/prj.conf

@ -1,3 +1,4 @@ @@ -1,3 +1,4 @@
# CONFIG_LOG=y
# CONFIG_ZMK_LOG_LEVEL_DBG=y
CONFIG_KERNEL_BIN_NAME="zmk"
CONFIG_REBOOT=y

108
app/src/behaviors/behavior_hid.c

@ -0,0 +1,108 @@ @@ -0,0 +1,108 @@
/*
* Copyright (c) 2020 Peter Johanson <peter@peterjohanson.com>
*
* SPDX-License-Identifier: MIT
*/
#define DT_DRV_COMPAT zmk_behavior_hid
#include <device.h>
#include <power/reboot.h>
#include <drivers/behavior.h>
#include <logging/log.h>
LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL);
#include <zmk/hid.h>
#include <zmk/endpoints.h>
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, u8_t usage_page, u32_t keycode)
{
int err;
LOG_DBG("keycode %d", keycode);
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, u8_t usage_page, u32_t keycode)
{
int err;
LOG_DBG("keycode %d", keycode);
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)
{
LOG_DBG("modifiers %d", modifiers);
zmk_hid_register_mods(modifiers);
return zmk_endpoints_send_report(USAGE_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(USAGE_KEYPAD);
}
static const struct behavior_driver_api behavior_hid_driver_api = {
.keycode_pressed = on_keycode_pressed,
.keycode_released = on_keycode_released,
.modifiers_pressed = on_modifiers_pressed,
.modifiers_released = on_modifiers_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);

57
app/src/behaviors/behavior_key_press.c

@ -0,0 +1,57 @@ @@ -0,0 +1,57 @@
/*
* Copyright (c) 2020 Peter Johanson <peter@peterjohanson.com>
*
* SPDX-License-Identifier: MIT
*/
#define DT_DRV_COMPAT zmk_behavior_key_press
#include <device.h>
#include <drivers/behavior.h>
#include <logging/log.h>
#include <zmk/events.h>
LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL);
struct behavior_key_press_config {
u8_t usage_page;
};
struct behavior_key_press_data { };
static int behavior_key_press_init(struct device *dev)
{
return 0;
};
static int on_keymap_binding_pressed(struct device *dev, u32_t position, u32_t keycode, u32_t _)
{
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);
}
static int on_keymap_binding_released(struct device *dev, u32_t position, u32_t keycode, u32_t _)
{
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 = {
.binding_pressed = on_keymap_binding_pressed,
.binding_released = on_keymap_binding_released
};
#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)

50
app/src/behaviors/behavior_keymap.c

@ -0,0 +1,50 @@ @@ -0,0 +1,50 @@
/*
* Copyright (c) 2020 Peter Johanson <peter@peterjohanson.com>
*
* SPDX-License-Identifier: MIT
*/
#define DT_DRV_COMPAT zmk_behavior_keymap
#include <device.h>
#include <power/reboot.h>
#include <drivers/behavior.h>
#include <logging/log.h>
LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL);
#include <zmk/keymap.h>
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 position)
{
return zmk_keymap_position_state_changed(position, true);
}
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,
};
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);

88
app/src/behaviors/behavior_mod_tap.c

@ -0,0 +1,88 @@ @@ -0,0 +1,88 @@
/*
* Copyright (c) 2020 Peter Johanson <peter@peterjohanson.com>
*
* SPDX-License-Identifier: MIT
*/
#define DT_DRV_COMPAT zmk_behavior_mod_tap
#include <device.h>
#include <drivers/behavior.h>
#include <logging/log.h>
#include <zmk/events.h>
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(USAGE_KEYPAD, keycode);
k_msleep(10);
zmk_events_keycode_released(USAGE_KEYPAD, keycode);
}
return 0;
}
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;
LOG_DBG("pressing: %d", keycode);
return 0;
}
static int on_keycode_released(struct device *dev, u8_t usage_page, 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);

55
app/src/behaviors/behavior_momentary_layer.c

@ -0,0 +1,55 @@ @@ -0,0 +1,55 @@
/*
* Copyright (c) 2020 Peter Johanson <peter@peterjohanson.com>
*
* SPDX-License-Identifier: MIT
*/
#define DT_DRV_COMPAT zmk_behavior_momentary_layer
#include <device.h>
#include <drivers/behavior.h>
#include <logging/log.h>
#include <zmk/events.h>
#include <zmk/keymap.h>
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 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 mo_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 = mo_keymap_binding_pressed,
.binding_released = mo_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);

45
app/src/behaviors/behavior_reset.c

@ -0,0 +1,45 @@ @@ -0,0 +1,45 @@
/*
* Copyright (c) 2020 Peter Johanson <peter@peterjohanson.com>
*
* SPDX-License-Identifier: MIT
*/
#define DT_DRV_COMPAT zmk_behavior_reset
#include <device.h>
#include <power/reboot.h>
#include <drivers/behavior.h>
#include <logging/log.h>
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_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
sys_reboot(0);
return 0;
}
static const struct behavior_driver_api behavior_reset_driver_api = {
.binding_pressed = on_keymap_binding_pressed,
};
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);

48
app/src/behaviors/behavior_transparent.c

@ -0,0 +1,48 @@ @@ -0,0 +1,48 @@
/*
* Copyright (c) 2020 Peter Johanson <peter@peterjohanson.com>
*
* SPDX-License-Identifier: MIT
*/
#define DT_DRV_COMPAT zmk_behavior_transparent
#include <device.h>
#include <power/reboot.h>
#include <drivers/behavior.h>
#include <logging/log.h>
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);

4
app/src/ble.c

@ -168,12 +168,12 @@ bool zmk_ble_handle_key_user(struct zmk_key_event *key_event) @@ -168,12 +168,12 @@ bool zmk_ble_handle_key_user(struct zmk_key_event *key_event)
return true;
}
if (key < KC_1 || key > KC_0)
if (key < NUM_1 || key > NUM_0)
{
return true;
}
u32_t val = (key == KC_0) ? 0 : (key - KC_1 + 1);
u32_t val = (key == NUM_0) ? 0 : (key - NUM_1 + 1);
passkey_entries[passkey_digit++] = val;

31
app/src/endpoints.c

@ -3,6 +3,7 @@ @@ -3,6 +3,7 @@
#include <zmk/hid.h>
#include <zmk/usb_hid.h>
#include <zmk/hog.h>
#
#include <logging/log.h>
LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL);
@ -35,14 +36,15 @@ int zmk_endpoints_init() @@ -35,14 +36,15 @@ int zmk_endpoints_init()
return 0;
}
int zmk_endpoints_send_report(enum zmk_hid_report_changes report_type)
int zmk_endpoints_send_report(u8_t usage_page)
{
int err;
struct zmk_hid_keypad_report *keypad_report;
struct zmk_hid_consumer_report *consumer_report;
switch (report_type)
LOG_DBG("usage page 0x%02X", usage_page);
switch (usage_page)
{
case Keypad:
case USAGE_KEYPAD:
keypad_report = zmk_hid_get_keypad_report();
#ifdef CONFIG_ZMK_USB
if (zmk_usb_hid_send_report((u8_t *)keypad_report, sizeof(struct zmk_hid_keypad_report)) != 0)
@ -60,7 +62,7 @@ int zmk_endpoints_send_report(enum zmk_hid_report_changes report_type) @@ -60,7 +62,7 @@ int zmk_endpoints_send_report(enum zmk_hid_report_changes report_type)
#endif /* CONFIG_ZMK_BLE */
break;
case Consumer:
case USAGE_CONSUMER:
consumer_report = zmk_hid_get_consumer_report();
#ifdef CONFIG_ZMK_USB
if (zmk_usb_hid_send_report((u8_t *)consumer_report, sizeof(struct zmk_hid_consumer_report)) != 0)
@ -79,27 +81,10 @@ int zmk_endpoints_send_report(enum zmk_hid_report_changes report_type) @@ -79,27 +81,10 @@ int zmk_endpoints_send_report(enum zmk_hid_report_changes report_type)
break;
default:
LOG_ERR("Unknown report change type %d", report_type);
return -EINVAL;
LOG_ERR("Unsupported usage page %d", usage_page);
return -ENOTSUP;
}
return 0;
}
int zmk_endpoints_send_key_event(struct zmk_key_event key_event)
{
enum zmk_hid_report_changes changes;
LOG_DBG("key %d, state %d\n", key_event.key, key_event.pressed);
if (key_event.pressed)
{
changes = zmk_hid_press_key(key_event.key);
}
else
{
changes = zmk_hid_release_key(key_event.key);
}
return zmk_endpoints_send_report(changes);
}

83
app/src/events.c

@ -0,0 +1,83 @@ @@ -0,0 +1,83 @@
#include <zephyr.h>
#include <drivers/behavior.h>
#include <zmk/behavior.h>
#include <zmk/events.h>
#include <sys/util.h>
#define DT_DRV_COMPAT zmk_behavior_global
#define GLOBAL_BEHAVIOR_LEN DT_NUM_INST_STATUS_OKAY(DT_DRV_COMPAT)
#define LABEL_ENTRY(i) DT_INST_LABEL(i),
static const char *global_behaviors[] = {
DT_INST_FOREACH_STATUS_OKAY(LABEL_ENTRY)
};
int zmk_events_position_pressed(u32_t position)
{
for (int i = 0; i < GLOBAL_BEHAVIOR_LEN; i++) {
const char* label = global_behaviors[i];
struct device *dev = device_get_binding(label);
behavior_position_pressed(dev, position);
}
return 0;
};
int zmk_events_position_released(u32_t position)
{
for (int i = 0; i < GLOBAL_BEHAVIOR_LEN; i++) {
const char* label = global_behaviors[i];
struct device *dev = device_get_binding(label);
behavior_position_released(dev, position);
}
return 0;
};
int zmk_events_keycode_pressed(u8_t usage_page, u32_t keycode)
{
for (int i = 0; i < GLOBAL_BEHAVIOR_LEN; i++) {
const char* label = global_behaviors[i];
struct device *dev = device_get_binding(label);
behavior_keycode_pressed(dev, usage_page, keycode);
}
return 0;
};
int zmk_events_keycode_released(u8_t usage_page, u32_t keycode)
{
for (int i = 0; i < GLOBAL_BEHAVIOR_LEN; i++) {
const char* label = global_behaviors[i];
struct device *dev = device_get_binding(label);
behavior_keycode_released(dev, usage_page, keycode);
}
return 0;
};
int zmk_events_modifiers_pressed(zmk_mod_flags modifiers)
{
for (int i = 0; i < GLOBAL_BEHAVIOR_LEN; i++) {
const char* label = global_behaviors[i];
struct device *dev = device_get_binding(label);
behavior_modifiers_pressed(dev, modifiers);
}
return 0;
};
int zmk_events_modifiers_released(zmk_mod_flags modifiers)
{
for (int i = 0; i < GLOBAL_BEHAVIOR_LEN; i++) {
const char* label = global_behaviors[i];
struct device *dev = device_get_binding(label);
behavior_modifiers_released(dev, modifiers);
}
return 0;
};
int zmk_events_consumer_key_pressed(u32_t usage)
{
return 0;
};
int zmk_events_consumer_key_released(u32_t usage)
{
return 0;
};

88
app/src/handlers.c

@ -1,88 +0,0 @@ @@ -1,88 +0,0 @@
#include <zmk/ble.h>
#include <zmk/handlers.h>
#include <zmk/endpoints.h>
#include <zmk/hid.h>
#include <zmk/matrix.h>
#ifdef CONFIG_ZMK_ACTION_MOD_TAP
u16_t action_effect_pending = 0;
#endif
__attribute__((weak)) bool zmk_handle_key_user(struct zmk_key_event *key_event)
{
return true;
};
bool zmk_handle_action(zmk_action action, struct zmk_key_event *key_event)
{
zmk_mod mods = ZK_MODS(key_event->key);
u8_t flattened_index = (key_event->row * ZMK_MATRIX_COLS) + key_event->column;
switch (action)
{
#ifdef CONFIG_ZMK_ACTION_MOD_TAP
case ZMK_ACTION_MOD_TAP:
if (key_event->pressed)
{
WRITE_BIT(action_effect_pending, flattened_index, true);
zmk_hid_register_mods(mods);
}
else
{
zmk_hid_unregister_mods(mods);
if (action_effect_pending & BIT(flattened_index))
{
struct zmk_key_event non_mod_event =
{
.row = key_event->row,
.column = key_event->column,
.key = ZK_KEY(key_event->key),
.pressed = true};
zmk_handle_key(non_mod_event);
// A small sleep is needed to ensure device layer sends initial
// key, before we send the release.
k_msleep(10);
non_mod_event.pressed = false;
zmk_handle_key(non_mod_event);
}
else
{
// Since not sending a keycode, at least send the report w/ the mod removed
zmk_endpoints_send_report(Keypad);
}
}
break;
#endif
}
return false;
};
void zmk_handle_key(struct zmk_key_event key_event)
{
zmk_action action = ZK_ACTION(key_event.key);
if (!zmk_handle_key_user(&key_event))
{
return;
}
if (action && !zmk_handle_action(action, &key_event))
{
return;
}
#ifdef CONFIG_ZMK_ACTION_MOD_TAP
action_effect_pending = 0;
#endif
#ifdef CONFIG_ZMK_BLE
/* Used for intercepting key presses when doing passkey verification */
if (!zmk_ble_handle_key_user(&key_event))
{
return;
}
#endif /* CONFIG_ZMK_BLE */
zmk_endpoints_send_key_event(key_event);
};

78
app/src/hid.c

@ -12,7 +12,7 @@ static struct zmk_hid_keypad_report kp_report = { @@ -12,7 +12,7 @@ static struct zmk_hid_keypad_report kp_report = {
static struct zmk_hid_consumer_report consumer_report = {
.report_id = 2,
.body = {
.keys = 0x00}};
.keys = {0,0,0,0,0,0}}};
#define _TOGGLE_MOD(mod, state) \
if (modifier > MOD_RGUI) \
@ -61,62 +61,68 @@ int zmk_hid_unregister_mods(zmk_mod_flags modifiers) @@ -61,62 +61,68 @@ int zmk_hid_unregister_mods(zmk_mod_flags modifiers)
#define TOGGLE_KEY(code, val) WRITE_BIT(kp_report.body.keys[code / 8], code % 8, val)
#define TOGGLE_CONSUMER(key, state) \
WRITE_BIT(consumer_report.body.keys, (key - 0x100), state);
#define TOGGLE_CONSUMER(match, val) \
for (int idx = 0; idx < MAX_KEYS; idx++) \
{ \
if (consumer_report.body.keys[idx] != match) \
{ \
continue; \
} \
consumer_report.body.keys[idx] = val; \
break; \
}
enum zmk_hid_report_changes zmk_hid_press_key(zmk_key code)
int zmk_hid_keypad_press(zmk_key code)
{
if (code >= KC_LCTL && code <= KC_RGUI)
if (code >= LCTL && code <= RGUI)
{
return zmk_hid_register_mod(code - KC_LCTL);
return zmk_hid_register_mod(code - LCTL);
}
if (ZK_IS_CONSUMER(code))
if (code > ZMK_HID_MAX_KEYCODE)
{
LOG_DBG("Toggling a consumer key!");
TOGGLE_CONSUMER(code, true);
return Consumer;
return -EINVAL;
}
else
{
if (code > ZMK_HID_MAX_KEYCODE)
{
return -EINVAL;
}
// TOGGLE_BOOT_KEY(0U, code);
// TOGGLE_BOOT_KEY(0U, code);
TOGGLE_KEY(code, true);
TOGGLE_KEY(code, true);
return Keypad;
}
return 0;
};
enum zmk_hid_report_changes zmk_hid_release_key(zmk_key code)
int zmk_hid_keypad_release(zmk_key code)
{
if (code >= KC_LCTL && code <= KC_RGUI)
if (code >= LCTL && code <= RGUI)
{
return zmk_hid_unregister_mod(code - KC_LCTL);
return zmk_hid_unregister_mod(code - LCTL);
}
if (ZK_IS_CONSUMER(code))
if (code > ZMK_HID_MAX_KEYCODE)
{
TOGGLE_CONSUMER(code, false);
return Consumer;
return -EINVAL;
}
else
{
if (code > ZMK_HID_MAX_KEYCODE)
{
return -EINVAL;
}
// TOGGLE_BOOT_KEY(0U, code);
// TOGGLE_BOOT_KEY(0U, code);
TOGGLE_KEY(code, false);
TOGGLE_KEY(code, false);
return Keypad;
}
return 0;
};
int zmk_hid_consumer_press(zmk_key code)
{
TOGGLE_CONSUMER(0U, code);
return 0;
};
int zmk_hid_consumer_release(zmk_key code)
{
TOGGLE_CONSUMER(code, 0U);
return 0;
};
struct zmk_hid_keypad_report *zmk_hid_get_keypad_report()

97
app/src/keymap.c

@ -1,24 +1,32 @@ @@ -1,24 +1,32 @@
#include <sys/util.h>
#include <logging/log.h>
LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL);
#include <zmk/matrix.h>
#include <zmk/keymap.h>
#include <dt-bindings/zmk/matrix-transform.h>
#include <sys/util.h>
#include <drivers/behavior.h>
#include <zmk/behavior.h>
static u32_t zmk_keymap_layer_state = 0;
static u8_t zmk_keymap_layer_default = 0;
#if DT_NODE_HAS_PROP(ZMK_KEYMAP_NODE, transform)
#define ZMK_KEYMAP_TRANSFORM_NODE DT_PHANDLE(ZMK_KEYMAP_NODE, transform)
#define ZMK_KEYMAP_LEN DT_PROP_LEN(ZMK_KEYMAP_TRANSFORM_NODE, map)
#define ZMK_KEYMAP_NODE DT_CHOSEN(zmk_keymap)
#define ZMK_KEYMAP_LAYERS_LEN DT_PROP_LEN(ZMK_KEYMAP_NODE, layers)
#define LAYER_NODE(l) DT_PHANDLE_BY_IDX(ZMK_KEYMAP_NODE, layers, l)
#define _TRANSFORM_ENTRY(i, l) \
[(KT_ROW(DT_PROP_BY_IDX(ZMK_KEYMAP_TRANSFORM_NODE, map, i)) * ZMK_MATRIX_COLS) + KT_COL(DT_PROP_BY_IDX(ZMK_KEYMAP_TRANSFORM_NODE, map, i))] = DT_PROP_BY_IDX(DT_PHANDLE_BY_IDX(ZMK_KEYMAP_NODE, layers, l), keys, i),
#define _TRANSFORM_ENTRY(idx, layer) \
{ .behavior_dev = DT_LABEL(DT_PHANDLE_BY_IDX(DT_PHANDLE_BY_IDX(ZMK_KEYMAP_NODE, layers, layer), bindings, idx)), \
.param1 = COND_CODE_0(DT_PHA_HAS_CELL_AT_IDX(LAYER_NODE(layer), bindings, idx, param1), (0), (DT_PHA_BY_IDX(LAYER_NODE(layer), bindings, idx, param1))), \
.param2 = COND_CODE_0(DT_PHA_HAS_CELL_AT_IDX(LAYER_NODE(layer), bindings, idx, param2), (0), (DT_PHA_BY_IDX(LAYER_NODE(layer), bindings, idx, param2))), \
},
#define TRANSFORMED_LAYER(idx) \
{ UTIL_LISTIFY(ZMK_KEYMAP_LEN, _TRANSFORM_ENTRY, idx) }
{ UTIL_LISTIFY(DT_PROP_LEN(DT_PHANDLE_BY_IDX(ZMK_KEYMAP_NODE, layers, idx), bindings), _TRANSFORM_ENTRY, idx) }
static zmk_key zmk_keymap[ZMK_KEYMAP_LAYERS_LEN][ZMK_MATRIX_ROWS * ZMK_MATRIX_COLS] = {
static struct zmk_behavior_binding zmk_keymap[ZMK_KEYMAP_LAYERS_LEN][ZMK_MATRIX_ROWS * ZMK_MATRIX_COLS] = {
#if DT_PROP_HAS_IDX(ZMK_KEYMAP_NODE, layers, 0)
TRANSFORMED_LAYER(0),
#endif
@ -51,73 +59,60 @@ static zmk_key zmk_keymap[ZMK_KEYMAP_LAYERS_LEN][ZMK_MATRIX_ROWS * ZMK_MATRIX_CO @@ -51,73 +59,60 @@ static zmk_key zmk_keymap[ZMK_KEYMAP_LAYERS_LEN][ZMK_MATRIX_ROWS * ZMK_MATRIX_CO
#endif
};
#else
static zmk_key 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
};
#endif
#define SET_LAYER_STATE(layer, state) \
if (layer >= 32) \
{ \
return false; \
return -EINVAL; \
} \
WRITE_BIT(zmk_keymap_layer_state, layer, state); \
return true;
return 0;
bool zmk_keymap_layer_activate(u8_t layer)
int zmk_keymap_layer_activate(u8_t layer)
{
SET_LAYER_STATE(layer, true);
};
bool zmk_keymap_layer_deactivate(u8_t layer)
int zmk_keymap_layer_deactivate(u8_t layer)
{
SET_LAYER_STATE(layer, false);
};
zmk_key zmk_keymap_keycode_from_position(u32_t row, u32_t column)
int zmk_keymap_position_state_changed(u32_t position, bool pressed)
{
for (int layer = ZMK_KEYMAP_LAYERS_LEN - 1; layer >= zmk_keymap_layer_default; layer--)
{
if ((zmk_keymap_layer_state & BIT(layer)) == BIT(layer) || layer == zmk_keymap_layer_default)
{
u8_t key_index = (row * ZMK_MATRIX_COLS) + column;
LOG_DBG("Getting key at index %d", key_index);
struct zmk_behavior_binding *binding = &zmk_keymap[layer][position];
struct device *behavior;
int ret;
LOG_DBG("layer: %d position: %d, binding name: %s", layer, position, binding->behavior_dev);
zmk_key key = zmk_keymap[layer][key_index];
if (key == ZC_TRNS)
{
behavior = device_get_binding(binding->behavior_dev);
if (!behavior) {
LOG_DBG("No behavior assigned to %d on layer %d", position, layer);
continue;
}
if (pressed) {
ret = behavior_keymap_binding_pressed(behavior, position, binding->param1, binding->param2);
} else {
ret = behavior_keymap_binding_released(behavior, position, binding->param1, binding->param2);
}
return key;
if (ret > 0) {
LOG_DBG("behavior processing to continue to next layer");
continue;
} else if (ret < 0) {
LOG_DBG("Behavior returned error: %d", ret);
return ret;
} else {
return ret;
}
}
}
return ZC_NO;
return -ENOTSUP;
}

16
app/src/kscan.c

@ -11,8 +11,8 @@ @@ -11,8 +11,8 @@
LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL);
#include <zmk/keymap.h>
#include <zmk/handlers.h>
#include <zmk/matrix_transform.h>
#include <zmk/events.h>
#define ZMK_KSCAN_EVENT_STATE_PRESSED 0
#define ZMK_KSCAN_EVENT_STATE_RELEASED 1
@ -49,11 +49,13 @@ void zmk_kscan_process_msgq(struct k_work *item) @@ -49,11 +49,13 @@ void zmk_kscan_process_msgq(struct k_work *item)
while (k_msgq_get(&zmk_kscan_msgq, &ev, K_NO_WAIT) == 0)
{
bool pressed = (ev.state == ZMK_KSCAN_EVENT_STATE_PRESSED);
zmk_key key = zmk_keymap_keycode_from_position(ev.row, ev.column);
struct zmk_key_event kev = (struct zmk_key_event){.row = ev.row, .column = ev.column, .key = key, .pressed = pressed};
LOG_DBG("Row: %d, col: %d, key: %d, pressed: %s\n", ev.row, ev.column, key, (pressed ? "true" : "false"));
zmk_handle_key(kev);
u32_t position = zmk_matrix_transform_row_column_to_position(ev.row, ev.column);
LOG_DBG("Row: %d, col: %d, position: %d, pressed: %s\n", ev.row, ev.column, position, (pressed ? "true" : "false"));
if (pressed) {
zmk_events_position_pressed(position);
} else {
zmk_events_position_released(position);
}
}
}

1
app/src/kscan_mock.c

@ -9,6 +9,7 @@ @@ -9,6 +9,7 @@
#include <device.h>
#include <drivers/kscan.h>
#include <logging/log.h>
LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL);
#include <zmk/kscan-mock.h>

30
app/src/matrix_transform.c

@ -0,0 +1,30 @@ @@ -0,0 +1,30 @@
#include <zephyr.h>
#include <zmk/matrix_transform.h>
#include <zmk/matrix.h>
#include <dt-bindings/zmk/matrix-transform.h>
#define HAS_TRANSFORM DT_HAS_CHOSEN(zmk_matrix_transform)
#if HAS_TRANSFORM
#define ZMK_KEYMAP_TRANSFORM_NODE DT_CHOSEN(zmk_matrix_transform)
#define ZMK_KEYMAP_LEN DT_PROP_LEN(ZMK_KEYMAP_TRANSFORM_NODE, map)
#define _TRANSFORM_ENTRY(i, _) \
[(KT_ROW(DT_PROP_BY_IDX(ZMK_KEYMAP_TRANSFORM_NODE, map, i)) * ZMK_MATRIX_COLS) + KT_COL(DT_PROP_BY_IDX(ZMK_KEYMAP_TRANSFORM_NODE, map, i))] = i,
static u32_t transform[] =
{ UTIL_LISTIFY(ZMK_KEYMAP_LEN, _TRANSFORM_ENTRY, 0) };
#endif
u32_t zmk_matrix_transform_row_column_to_position(u32_t row, u32_t column)
{
u32_t matrix_index = (row * ZMK_MATRIX_COLS) + column;
#if HAS_TRANSFORM
return transform[matrix_index];
#else
return matrix_index;
#endif /* HAS_TRANSFORM */
};
Loading…
Cancel
Save