diff --git a/.github/workflows/build-user-config.yml b/.github/workflows/build-user-config.yml index b1e0602d..5891ddc1 100644 --- a/.github/workflows/build-user-config.yml +++ b/.github/workflows/build-user-config.yml @@ -39,8 +39,8 @@ jobs: - name: Fetch Build Matrix run: | - echo "build_matrix=$(yaml2json ${{ inputs.build_matrix_path }} | jq -c .)" >> $GITHUB_ENV - yaml2json ${{ inputs.build_matrix_path }} | jq + echo "build_matrix=$(yaml2json '${{ inputs.build_matrix_path }}' | jq -c .)" >> $GITHUB_ENV + yaml2json "${{ inputs.build_matrix_path }}" | jq build: runs-on: ubuntu-latest @@ -54,18 +54,13 @@ jobs: steps: - name: Prepare variables shell: sh -x {0} + env: + shield: ${{ matrix.shield }} run: | - if [ -n "${{ matrix.shield }}" ] - then - echo "extra_cmake_args=-DSHIELD=\"${{ matrix.shield }}\"" >> $GITHUB_ENV - echo "artifact_name=${{ matrix.shield }}-${{ matrix.board }}-zmk" >> $GITHUB_ENV - echo "display_name=${{ matrix.shield }} - ${{ matrix.board }}" >> $GITHUB_ENV - else - echo "extra_cmake_args=" >> $GITHUB_ENV - echo "artifact_name=${{ matrix.board }}-zmk" >> $GITHUB_ENV - echo "display_name=${{ matrix.board }}" >> $GITHUB_ENV - fi echo "zephyr_version=${ZEPHYR_VERSION}" >> $GITHUB_ENV + echo "extra_cmake_args=${shield:+-DSHIELD=\"$shield\"}" >> $GITHUB_ENV + echo "display_name=${shield:+$shield - }${{ matrix.board }}" >> $GITHUB_ENV + echo "artifact_name=${shield:+$shield-}${{ matrix.board }}-zmk" >> $GITHUB_ENV - name: Checkout uses: actions/checkout@v3 @@ -89,7 +84,7 @@ jobs: ${{ runner.os }}- - name: West Init - run: west init -l ${{ inputs.config_path }} + run: west init -l "${{ inputs.config_path }}" - name: West Update run: west update @@ -99,7 +94,7 @@ jobs: - name: West Build (${{ env.display_name }}) shell: sh -x {0} - run: west build -s zmk/app -b ${{ matrix.board }} -- -DZMK_CONFIG=${GITHUB_WORKSPACE}/${{ inputs.config_path }} ${{ env.extra_cmake_args }} ${{ matrix.cmake-args }} + run: west build -s zmk/app -b "${{ matrix.board }}" -- -DZMK_CONFIG="${GITHUB_WORKSPACE}/${{ inputs.config_path }}" ${{ env.extra_cmake_args }} ${{ matrix.cmake-args }} - name: ${{ env.display_name }} Kconfig file run: grep -v -e "^#" -e "^$" build/zephyr/.config | sort @@ -113,7 +108,7 @@ jobs: cp build/zephyr/zmk.uf2 "build/artifacts/${{ env.artifact_name }}.uf2" elif [ -f build/zephyr/zmk.${{ inputs.fallback_binary }} ] then - cp build/zephyr/zmk.${{ inputs.fallback_binary }} "build/artifacts/${{ env.artifact_name }}.${{ inputs.fallback_binary }}" + cp "build/zephyr/zmk.${{ inputs.fallback_binary }}" "build/artifacts/${{ env.artifact_name }}.${{ inputs.fallback_binary }}" fi - name: Archive (${{ env.display_name }}) diff --git a/.vscode/extensions.json b/.vscode/extensions.json new file mode 100644 index 00000000..0819f71e --- /dev/null +++ b/.vscode/extensions.json @@ -0,0 +1,9 @@ +{ + "recommendations": [ + "esbenp.prettier-vscode", + "ms-python.python", + "ms-vscode.cpptools", + "plorefice.devicetree", + "twxs.cmake" + ] +} diff --git a/.vscode/settings.json b/.vscode/settings.json index aea29cf0..924d83b1 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -3,5 +3,20 @@ "*.overlay": "dts", "*.keymap": "dts" }, - "python.formatting.provider": "black" + "python.formatting.provider": "black", + "[c]": { + "editor.formatOnSave": true + }, + "[javascript][javascriptreact][typescript][typescriptreact]": { + "editor.formatOnSave": true, + "editor.defaultFormatter": "esbenp.prettier-vscode" + }, + "[python]": { + "editor.formatOnSave": true, + "editor.defaultFormatter": "ms-python.python" + }, + "[css][json][jsonc][html][markdown][yaml]": { + "editor.formatOnSave": true, + "editor.defaultFormatter": "esbenp.prettier-vscode" + } } diff --git a/app/boards/arm/bluemicro840/bluemicro840_v1.dts b/app/boards/arm/bluemicro840/bluemicro840_v1.dts index f1952601..05849001 100644 --- a/app/boards/arm/bluemicro840/bluemicro840_v1.dts +++ b/app/boards/arm/bluemicro840/bluemicro840_v1.dts @@ -63,7 +63,7 @@ }; &i2c0 { - compatible = "nordic,nrf-twim"; + compatible = "nordic,nrf-twi"; pinctrl-0 = <&i2c0_default>; pinctrl-1 = <&i2c0_sleep>; pinctrl-names = "default", "sleep"; @@ -71,6 +71,7 @@ &uart0 { compatible = "nordic,nrf-uarte"; + current-speed = <115200>; pinctrl-0 = <&uart0_default>; pinctrl-1 = <&uart0_sleep>; pinctrl-names = "default", "sleep"; diff --git a/app/boards/arm/kbdfans_tofu65/Kconfig.board b/app/boards/arm/kbdfans_tofu65/Kconfig.board new file mode 100644 index 00000000..954166b7 --- /dev/null +++ b/app/boards/arm/kbdfans_tofu65/Kconfig.board @@ -0,0 +1,6 @@ +# Copyright (c) 2023 The ZMK Contributors +# SPDX-License-Identifier: Apache-2.0 + +config BOARD_KBDFANS_TOFU65_V2 + bool "KBDfans Tofu65 2.0" + depends on SOC_RP2040 diff --git a/app/boards/arm/kbdfans_tofu65/Kconfig.defconfig b/app/boards/arm/kbdfans_tofu65/Kconfig.defconfig new file mode 100644 index 00000000..993d5142 --- /dev/null +++ b/app/boards/arm/kbdfans_tofu65/Kconfig.defconfig @@ -0,0 +1,15 @@ +# Copyright (c) 2023 The ZMK Contributors +# SPDX-License-Identifier: MIT + +if BOARD_KBDFANS_TOFU65_V2 + +config ZMK_KEYBOARD_NAME + default "kbdfans tofu65" + +config RP2_FLASH_W25Q080 + default y + +config ZMK_USB + default y + +endif # BOARD_KBDFANS_TOFU65_V2 diff --git a/app/boards/arm/kbdfans_tofu65/kbdfans_tofu65_v2.dts b/app/boards/arm/kbdfans_tofu65/kbdfans_tofu65_v2.dts new file mode 100644 index 00000000..261ffbf4 --- /dev/null +++ b/app/boards/arm/kbdfans_tofu65/kbdfans_tofu65_v2.dts @@ -0,0 +1,125 @@ +/* + * Copyright (c) 2023 The ZMK Contributors + * SPDX-License-Identifier: MIT + */ + +/dts-v1/; + +#include +#include + +/ { + + chosen { + zephyr,sram = &sram0; + zephyr,flash = &flash0; + zephyr,console = &cdc_acm_uart; + zephyr,shell-uart = &cdc_acm_uart; + zephyr,code-partition = &code_partition; + zmk,kscan = &kscan0; + zmk,matrix_transform = &default_transform; + }; + + xtal_clk: xtal-clk { + compatible = "fixed-clock"; + clock-frequency = <12000000>; + #clock-cells = <0>; + }; + + default_transform: keymap_transform_0 { + compatible = "zmk,matrix-transform"; + columns = <15>; + rows = <5>; + +// ------- Switch Matrix ---------- +// +// Column 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | +// ========================================================================================== +// Row 0 || S0 | S1 | S2 | S3 | S4 | S5 | S6 | S7 | S8 | S9 | S10 | S11 | S12 | S13 | S14 | +// Row 1 || S0 | S1 | S2 | S3 | S4 | S5 | S6 | S7 | S8 | S9 | S10 | S11 | S12 | S13 | S14 | +// Row 2 || S0 | S1 | S2 | S3 | S4 | S5 | S6 | S7 | S8 | S9 | S10 | S11 | S12 | | S13 | +// Row 3 || S0 | S1 | S2 | S3 | S4 | S5 | S6 | S7 | S8 | S9 | S10 | S11 | | S12 | S13 | +// Row 4 || S0 | S1 | S2 | | | | S3 | | S4 | S5 | S6 | | S7 | S8 | S9 | +// ----------------------------------------------------------------------------------- +// + map = < +RC(0,0) RC(0,1) RC(0,2) RC(0,3) RC(0,4) RC(0,5) RC(0,6) RC(0,7) RC(0,8) RC(0,9) RC(0,10) RC(0,11) RC(0,12) RC(0,13) RC(0,14) +RC(1,0) RC(1,1) RC(1,2) RC(1,3) RC(1,4) RC(1,5) RC(1,6) RC(1,7) RC(1,8) RC(1,9) RC(1,10) RC(1,11) RC(1,12) RC(1,13) RC(1,14) +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,14) +RC(3,0) RC(3,1) 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) RC(3,13) RC(3,14) +RC(4,0) RC(4,1) RC(4,2) RC(4,6) RC(4,8) RC(4,9) RC(4,10) RC(4,12) RC(4,13) RC(4,14) + >; + }; + + kscan0: kscan { + compatible = "zmk,kscan-gpio-matrix"; + label = "KSCAN"; + + diode-direction = "col2row"; + row-gpios + = <&gpio0 29 (GPIO_ACTIVE_HIGH | GPIO_PULL_DOWN)> + , <&gpio0 28 (GPIO_ACTIVE_HIGH | GPIO_PULL_DOWN)> + , <&gpio0 27 (GPIO_ACTIVE_HIGH | GPIO_PULL_DOWN)> + , <&gpio0 26 (GPIO_ACTIVE_HIGH | GPIO_PULL_DOWN)> + , <&gpio0 22 (GPIO_ACTIVE_HIGH | GPIO_PULL_DOWN)> + ; + col-gpios + = <&gpio0 25 GPIO_ACTIVE_HIGH> + , <&gpio0 24 GPIO_ACTIVE_HIGH> + , <&gpio0 23 GPIO_ACTIVE_HIGH> + , <&gpio0 1 GPIO_ACTIVE_HIGH> + , <&gpio0 7 GPIO_ACTIVE_HIGH> + , <&gpio0 21 GPIO_ACTIVE_HIGH> + , <&gpio0 20 GPIO_ACTIVE_HIGH> + , <&gpio0 19 GPIO_ACTIVE_HIGH> + , <&gpio0 18 GPIO_ACTIVE_HIGH> + , <&gpio0 17 GPIO_ACTIVE_HIGH> + , <&gpio0 16 GPIO_ACTIVE_HIGH> + , <&gpio0 15 GPIO_ACTIVE_HIGH> + , <&gpio0 14 GPIO_ACTIVE_HIGH> + , <&gpio0 13 GPIO_ACTIVE_HIGH> + , <&gpio0 12 GPIO_ACTIVE_HIGH> + ; + }; +}; + +&flash0 { + reg = <0x10000000 DT_SIZE_M(16)>; + + partitions { + compatible = "fixed-partitions"; + #address-cells = <1>; + #size-cells = <1>; + + /* Reserved memory for the second stage bootloader */ + second_stage_bootloader: partition@0 { + label = "second_stage_bootloader"; + reg = <0x00000000 0x100>; + read-only; + }; + + /* + * Usable flash. Starts at 0x100, after the bootloader. The partition + * size is 16MB minus the 0x100 bytes taken by the bootloader. + */ + code_partition: partition@100 { + label = "code"; + reg = <0x100 (DT_SIZE_M(16) - 0x100)>; + read-only; + }; + }; +}; + + +&usbd { + status = "okay"; + cdc_acm_uart: cdc_acm_uart { + compatible = "zephyr,cdc-acm-uart"; + }; +}; + + +&gpio0 { + status = "okay"; +}; + diff --git a/app/boards/arm/kbdfans_tofu65/kbdfans_tofu65_v2.keymap b/app/boards/arm/kbdfans_tofu65/kbdfans_tofu65_v2.keymap new file mode 100644 index 00000000..7eca7919 --- /dev/null +++ b/app/boards/arm/kbdfans_tofu65/kbdfans_tofu65_v2.keymap @@ -0,0 +1,97 @@ +// Copyright (c) 2023 The ZMK Contributors +// SPDX-License-Identifier: MIT + +#include +#include + +#define BASE 0 +#define FUNC 1 + +// +// ---------- Tofu65 2.0 key switch positions ---------- +// +// ------------------------------------------------------------------------------------------------- +// | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | +// ------------------------------------------------------------------------------------------------- +// | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 24 | 26 | 27 | 28 | 29 | +// ------------------------------------------------------------------------------------------------- +// | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | +// ------------------------------------------------------------------------------------------------- +// | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | +// ------------------------------------------------------------------------------------------------- +// | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | +// ------------------------------------------------------------------------------------------------- +// + + +/ { + combos { + compatible = "zmk,combos"; + + // BACKSPACE + LCTRL + LALT = &sys_reset + combo_bootloader { + timeout-ms = <100>; + key-positions = <13 58 60>; + bindings = <&sys_reset>; + }; + + // RETURN + LCTRL + LALT = &bootloader + combo_sys_reset { + timeout-ms = <100>; + key-positions = <42 58 60>; + bindings = <&bootloader>; + }; + }; + + + keymap { + compatible = "zmk,keymap"; + + base { + +// --------- Default QWERTY Layout --------- +// Layer 0 BASE +// ------------------------------------------------------------------------------------------------- +// | ESC | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 0 | - | = | BSPC | HME | +// ------------------------------------------------------------------------------------------------- +// | TAB | Q | W | E | R | T | Y | U | I | O | P | [ | ] | \ | PGU | +// ------------------------------------------------------------------------------------------------- +// | CAPS | A | S | D | F | G | H | J | K | L | ; | ' | ENTER | PGD | +// ------------------------------------------------------------------------------------------------- +// | LSHIFT | Z | X | C | V | B | N | M | , | . | / | RSHFT | ↑ | END | +// ------------------------------------------------------------------------------------------------- +// | LCTL | LGUI | LALT | SPACE | RALT | RGUI | RCTL | <- | ↓ | -> | +// ------------------------------------------------------------------------------------------------- + bindings = < +&kp ESC &kp N1 &kp N2 &kp N3 &kp N4 &kp N5 &kp N6 &kp N7 &kp N8 &kp N9 &kp N0 &kp MINUS &kp EQUAL &kp BSPC &kp HOME +&kp TAB &kp Q &kp W &kp E &kp R &kp T &kp Y &kp U &kp I &kp O &kp P &kp LBKT &kp RBKT &kp BSLH &kp PG_UP +&kp CLCK &kp A &kp S &kp D &kp F &kp G &kp H &kp J &kp K &kp L &kp SEMI &kp SQT &kp ENTER &kp PG_DN +&kp LSHFT &kp Z &kp X &kp C &kp V &kp B &kp N &kp M &kp COMMA &kp DOT &kp FSLH &kp RSHFT &kp UP &kp END +&kp LCTRL &kp LGUI &kp LALT &kp SPACE &kp RALT < FUNC K_APP &kp RCTRL &kp LEFT &kp DOWN &kp RIGHT + >; + }; + + func { +// --------- Default QWERTY Layout --------- +// Layer 1 FUNC +// --------------------------------------------------------------------------------------------------- +// | ESC | F1 | F2 | F3 | F4 | F5 | F6 | F7 | F8 | F9 | F10 | F11 | F12 | DEL | HME | +// --------------------------------------------------------------------------------------------------- +// | --- | -- | -- | -- | -- | -- | -- | -- | -- | -- | -- | scroll lock | pause | --- | PGU | +// --------------------------------------------------------------------------------------------------- +// | CAPS | -- | -- | -- | -- | -- | -- | -- | -- | -- | -- | -- | --- | PGD | +// --------------------------------------------------------------------------------------------------- +// | LSHIFT | -- | -- | -- | -- | -- | -- | -- | -- | -- | -- | ----- | VOL UP | MUTE | +// --------------------------------------------------------------------------------------------------- +// | ---- | ---- | ---- | ---- | -- | MO 1 | -- | PREV | VOL DN | NEXT | +// --------------------------------------------------------------------------------------------------- + bindings = < +&kp GRAVE &kp F1 &kp F2 &kp F3 &kp F4 &kp F5 &kp F6 &kp F7 &kp F8 &kp F9 &kp F10 &kp F11 &kp F12 &kp DEL &trans +&trans &trans &trans &trans &trans &trans &trans &trans &trans &trans &trans &kp SLCK &kp PAUSE_BREAK &trans &trans +&trans &trans &trans &trans &trans &trans &trans &trans &trans &trans &trans &trans &trans &trans +&trans &trans &trans &trans &trans &trans &trans &trans &trans &trans &trans &trans &kp C_VOL_UP &kp C_MUTE +&trans &trans &trans &trans &trans &trans &trans &kp C_PREV &kp C_VOL_DN &kp C_NEXT + >; + }; + }; +}; diff --git a/app/boards/arm/kbdfans_tofu65/kbdfans_tofu65_v2.yaml b/app/boards/arm/kbdfans_tofu65/kbdfans_tofu65_v2.yaml new file mode 100644 index 00000000..e1089766 --- /dev/null +++ b/app/boards/arm/kbdfans_tofu65/kbdfans_tofu65_v2.yaml @@ -0,0 +1,15 @@ +identifier: kbdfans_tofu65_v2 +name: KBDfans Tofu65 2.0 +type: mcu +arch: arm +flash: 16384 +ram: 264 +toolchain: + - zephyr + - gnuarmemb + - xtools +supported: + - gpio + - usb_device + - hwinfo + - pwm diff --git a/app/boards/arm/kbdfans_tofu65/kbdfans_tofu65_v2.zmk.yml b/app/boards/arm/kbdfans_tofu65/kbdfans_tofu65_v2.zmk.yml new file mode 100644 index 00000000..382e7dd3 --- /dev/null +++ b/app/boards/arm/kbdfans_tofu65/kbdfans_tofu65_v2.zmk.yml @@ -0,0 +1,10 @@ +file_format: "1" +id: kbdfans_tofu65_v2 +name: KBDfans Tofu65 2.0 +type: board +arch: arm +features: + - keys +outputs: + - usb +url: https://kbdfans.com/collections/tofu65-2-0/products/tofu65-2-0 diff --git a/app/boards/arm/kbdfans_tofu65/kbdfans_tofu65_v2_defconfig b/app/boards/arm/kbdfans_tofu65/kbdfans_tofu65_v2_defconfig new file mode 100644 index 00000000..cf546683 --- /dev/null +++ b/app/boards/arm/kbdfans_tofu65/kbdfans_tofu65_v2_defconfig @@ -0,0 +1,20 @@ +# Copyright (c) 2023 The ZMK Contributors +# SPDX-License-Identifier: MIT + +CONFIG_SOC_SERIES_RP2XXX=y +CONFIG_SOC_RP2040=y +CONFIG_BOARD_KBDFANS_TOFU65_V2=y + +CONFIG_SYS_CLOCK_HW_CYCLES_PER_SEC=125000000 + +# Enable USB CDC ACM logging for debugging +# CONFIG_ZMK_USB_LOGGING=y + +# Enable reset by default +CONFIG_RESET=y + +# Code partition needed to target the correct flash range +CONFIG_USE_DT_CODE_PARTITION=y + +# Output UF2 by default, native bootloader supports it. +CONFIG_BUILD_OUTPUT_UF2=y diff --git a/app/boards/arm/nrfmicro/nrfmicro_11.dts b/app/boards/arm/nrfmicro/nrfmicro_11.dts index 68331edc..14b9adb9 100644 --- a/app/boards/arm/nrfmicro/nrfmicro_11.dts +++ b/app/boards/arm/nrfmicro/nrfmicro_11.dts @@ -48,7 +48,7 @@ }; &i2c0 { - compatible = "nordic,nrf-twim"; + compatible = "nordic,nrf-twi"; pinctrl-0 = <&i2c0_default>; pinctrl-1 = <&i2c0_sleep>; pinctrl-names = "default", "sleep"; @@ -56,6 +56,7 @@ &uart0 { compatible = "nordic,nrf-uarte"; + current-speed = <115200>; pinctrl-0 = <&uart0_default>; pinctrl-1 = <&uart0_sleep>; pinctrl-names = "default", "sleep"; diff --git a/app/boards/arm/nrfmicro/nrfmicro_11_flipped.dts b/app/boards/arm/nrfmicro/nrfmicro_11_flipped.dts index 9977617c..874b67e2 100644 --- a/app/boards/arm/nrfmicro/nrfmicro_11_flipped.dts +++ b/app/boards/arm/nrfmicro/nrfmicro_11_flipped.dts @@ -48,7 +48,7 @@ }; &i2c0 { - compatible = "nordic,nrf-twim"; + compatible = "nordic,nrf-twi"; pinctrl-0 = <&i2c0_default>; pinctrl-1 = <&i2c0_sleep>; pinctrl-names = "default", "sleep"; @@ -56,6 +56,7 @@ &uart0 { compatible = "nordic,nrf-uarte"; + current-speed = <115200>; pinctrl-0 = <&uart0_default>; pinctrl-1 = <&uart0_sleep>; pinctrl-names = "default", "sleep"; diff --git a/app/boards/arm/nrfmicro/nrfmicro_13.dts b/app/boards/arm/nrfmicro/nrfmicro_13.dts index 300838ff..65674132 100644 --- a/app/boards/arm/nrfmicro/nrfmicro_13.dts +++ b/app/boards/arm/nrfmicro/nrfmicro_13.dts @@ -61,7 +61,7 @@ }; &i2c0 { - compatible = "nordic,nrf-twim"; + compatible = "nordic,nrf-twi"; pinctrl-0 = <&i2c0_default>; pinctrl-1 = <&i2c0_sleep>; pinctrl-names = "default", "sleep"; @@ -69,6 +69,7 @@ &uart0 { compatible = "nordic,nrf-uarte"; + current-speed = <115200>; pinctrl-0 = <&uart0_default>; pinctrl-1 = <&uart0_sleep>; pinctrl-names = "default", "sleep"; diff --git a/app/boards/arm/nrfmicro/nrfmicro_13_52833.dts b/app/boards/arm/nrfmicro/nrfmicro_13_52833.dts index 86bcb5e5..cfe77e37 100644 --- a/app/boards/arm/nrfmicro/nrfmicro_13_52833.dts +++ b/app/boards/arm/nrfmicro/nrfmicro_13_52833.dts @@ -61,7 +61,7 @@ }; &i2c0 { - compatible = "nordic,nrf-twim"; + compatible = "nordic,nrf-twi"; pinctrl-0 = <&i2c0_default>; pinctrl-1 = <&i2c0_sleep>; pinctrl-names = "default", "sleep"; @@ -69,6 +69,7 @@ &uart0 { compatible = "nordic,nrf-uarte"; + current-speed = <115200>; pinctrl-0 = <&uart0_default>; pinctrl-1 = <&uart0_sleep>; pinctrl-names = "default", "sleep"; diff --git a/app/drivers/kscan/CMakeLists.txt b/app/drivers/kscan/CMakeLists.txt index ced31e6f..8fc7ed58 100644 --- a/app/drivers/kscan/CMakeLists.txt +++ b/app/drivers/kscan/CMakeLists.txt @@ -5,6 +5,7 @@ zephyr_library_named(zmk__drivers__kscan) zephyr_library_include_directories(${CMAKE_SOURCE_DIR}/include) zephyr_library_sources_ifdef(CONFIG_ZMK_KSCAN_GPIO_DRIVER debounce.c) +zephyr_library_sources_ifdef(CONFIG_ZMK_KSCAN_GPIO_DRIVER kscan_gpio.c) zephyr_library_sources_ifdef(CONFIG_ZMK_KSCAN_GPIO_MATRIX kscan_gpio_matrix.c) zephyr_library_sources_ifdef(CONFIG_ZMK_KSCAN_GPIO_DIRECT kscan_gpio_direct.c) zephyr_library_sources_ifdef(CONFIG_ZMK_KSCAN_GPIO_DEMUX kscan_gpio_demux.c) diff --git a/app/drivers/kscan/kscan_gpio.c b/app/drivers/kscan/kscan_gpio.c new file mode 100644 index 00000000..4963f678 --- /dev/null +++ b/app/drivers/kscan/kscan_gpio.c @@ -0,0 +1,33 @@ +/* + * Copyright (c) 2022 The ZMK Contributors + * + * SPDX-License-Identifier: MIT + */ + +#include "kscan_gpio.h" + +#include + +static int compare_ports(const void *a, const void *b) { + const struct kscan_gpio *gpio_a = a; + const struct kscan_gpio *gpio_b = b; + + return gpio_a->spec.port - gpio_b->spec.port; +} + +void kscan_gpio_list_sort_by_port(struct kscan_gpio_list *list) { + qsort(list->gpios, list->len, sizeof(list->gpios[0]), compare_ports); +} + +int kscan_gpio_pin_get(const struct kscan_gpio *gpio, struct kscan_gpio_port_state *state) { + if (gpio->spec.port != state->port) { + state->port = gpio->spec.port; + + const int err = gpio_port_get(state->port, &state->value); + if (err) { + return err; + } + } + + return (state->value & BIT(gpio->spec.pin)) != 0; +} diff --git a/app/drivers/kscan/kscan_gpio.h b/app/drivers/kscan/kscan_gpio.h new file mode 100644 index 00000000..39343136 --- /dev/null +++ b/app/drivers/kscan/kscan_gpio.h @@ -0,0 +1,61 @@ +/* + * Copyright (c) 2022 The ZMK Contributors + * + * SPDX-License-Identifier: MIT + */ + +#pragma once + +#include +#include +#include +#include +#include +#include + +struct kscan_gpio { + struct gpio_dt_spec spec; + /** The index of the GPIO in the devicetree *-gpios array. */ + size_t index; +}; + +/** GPIO_DT_SPEC_GET_BY_IDX(), but for a struct kscan_gpio. */ +#define KSCAN_GPIO_GET_BY_IDX(node_id, prop, idx) \ + ((struct kscan_gpio){.spec = GPIO_DT_SPEC_GET_BY_IDX(node_id, prop, idx), .index = idx}) + +struct kscan_gpio_list { + struct kscan_gpio *gpios; + size_t len; +}; + +/** Define a kscan_gpio_list from a compile-time GPIO array. */ +#define KSCAN_GPIO_LIST(gpio_array) \ + ((struct kscan_gpio_list){.gpios = gpio_array, .len = ARRAY_SIZE(gpio_array)}) + +struct kscan_gpio_port_state { + const struct device *port; + gpio_port_value_t value; +}; + +/** + * Sorts a GPIO list by port so it can be used with kscan_gpio_pin_get(). + */ +void kscan_gpio_list_sort_by_port(struct kscan_gpio_list *list); + +/** + * Get logical level of an input pin. + * + * This is equivalent to gpio_pin_get() except that, when iterating through the + * pins in a list which is sorted by kscan_gpio_list_sort_by_port(), it only + * performs one read per port instead of one read per pin. + * + * @param gpio The input pin to read. + * @param state An object to track state between reads. Must be zero-initialized before the first + * use. + * + * @retval 1 If pin logical value is 1 / active. + * @retval 0 If pin logical value is 0 / inactive. + * @retval -EIO I/O error when accessing an external GPIO chip. + * @retval -EWOULDBLOCK if operation would block. + */ +int kscan_gpio_pin_get(const struct kscan_gpio *gpio, struct kscan_gpio_port_state *state); diff --git a/app/drivers/kscan/kscan_gpio_direct.c b/app/drivers/kscan/kscan_gpio_direct.c index 586e0d95..d43d716b 100644 --- a/app/drivers/kscan/kscan_gpio_direct.c +++ b/app/drivers/kscan/kscan_gpio_direct.c @@ -5,6 +5,7 @@ */ #include "debounce.h" +#include "kscan_gpio.h" #include #include @@ -41,7 +42,7 @@ LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL); #define INST_INPUTS_LEN(n) DT_INST_PROP_LEN(n, input_gpios) #define KSCAN_DIRECT_INPUT_CFG_INIT(idx, inst_idx) \ - GPIO_DT_SPEC_GET_BY_IDX(DT_DRV_INST(inst_idx), input_gpios, idx) + KSCAN_GPIO_GET_BY_IDX(DT_DRV_INST(inst_idx), input_gpios, idx) struct kscan_direct_irq_callback { const struct device *dev; @@ -50,6 +51,7 @@ struct kscan_direct_irq_callback { struct kscan_direct_data { const struct device *dev; + struct kscan_gpio_list inputs; kscan_callback_t callback; struct k_work_delayable work; #if USE_INTERRUPTS @@ -62,17 +64,7 @@ struct kscan_direct_data { struct debounce_state *pin_state; }; -struct kscan_gpio_list { - const struct gpio_dt_spec *gpios; - size_t len; -}; - -/** Define a kscan_gpio_list from a compile-time GPIO array. */ -#define KSCAN_GPIO_LIST(gpio_array) \ - ((struct kscan_gpio_list){.gpios = gpio_array, .len = ARRAY_SIZE(gpio_array)}) - struct kscan_direct_config { - struct kscan_gpio_list inputs; struct debounce_config debounce_config; int32_t debounce_scan_period_ms; int32_t poll_period_ms; @@ -81,10 +73,10 @@ struct kscan_direct_config { #if USE_INTERRUPTS static int kscan_direct_interrupt_configure(const struct device *dev, const gpio_flags_t flags) { - const struct kscan_direct_config *config = dev->config; + const struct kscan_direct_data *data = dev->data; - for (int i = 0; i < config->inputs.len; i++) { - const struct gpio_dt_spec *gpio = &config->inputs.gpios[i]; + for (int i = 0; i < data->inputs.len; i++) { + const struct gpio_dt_spec *gpio = &data->inputs.gpios[i].spec; int err = gpio_pin_interrupt_configure_dt(gpio, flags); if (err) { @@ -134,16 +126,16 @@ static gpio_flags_t kscan_gpio_get_extra_flags(const struct gpio_dt_spec *gpio, static int kscan_inputs_set_flags(const struct kscan_gpio_list *inputs, const struct gpio_dt_spec *active_gpio) { - gpio_flags_t extra_flags; for (int i = 0; i < inputs->len; i++) { - extra_flags = GPIO_INPUT | kscan_gpio_get_extra_flags(&inputs->gpios[i], - &inputs->gpios[i] == active_gpio); + const bool active = &inputs->gpios[i].spec == active_gpio; + const gpio_flags_t extra_flags = + GPIO_INPUT | kscan_gpio_get_extra_flags(&inputs->gpios[i].spec, active); LOG_DBG("Extra flags equal to: %d", extra_flags); - int err = gpio_pin_configure_dt(&inputs->gpios[i], extra_flags); + int err = gpio_pin_configure_dt(&inputs->gpios[i].spec, extra_flags); if (err) { - LOG_ERR("Unable to configure flags on pin %d on %s", inputs->gpios[i].pin, - inputs->gpios[i].port->name); + LOG_ERR("Unable to configure flags on pin %d on %s", inputs->gpios[i].spec.pin, + inputs->gpios[i].spec.port->name); return err; } } @@ -179,28 +171,35 @@ static int kscan_direct_read(const struct device *dev) { const struct kscan_direct_config *config = dev->config; // Read the inputs. - for (int i = 0; i < config->inputs.len; i++) { - const struct gpio_dt_spec *gpio = &config->inputs.gpios[i]; + struct kscan_gpio_port_state state = {0}; - const bool active = gpio_pin_get_dt(gpio); + for (int i = 0; i < data->inputs.len; i++) { + const struct kscan_gpio *gpio = &data->inputs.gpios[i]; - debounce_update(&data->pin_state[i], active, config->debounce_scan_period_ms, + const int active = kscan_gpio_pin_get(gpio, &state); + if (active < 0) { + LOG_ERR("Failed to read port %s: %i", gpio->spec.port->name, active); + return active; + } + + debounce_update(&data->pin_state[gpio->index], active, config->debounce_scan_period_ms, &config->debounce_config); } // Process the new state. bool continue_scan = false; - for (int i = 0; i < config->inputs.len; i++) { - struct debounce_state *state = &data->pin_state[i]; + for (int i = 0; i < data->inputs.len; i++) { + const struct kscan_gpio *gpio = &data->inputs.gpios[i]; + struct debounce_state *state = &data->pin_state[gpio->index]; if (debounce_get_changed(state)) { const bool pressed = debounce_is_pressed(state); - LOG_DBG("Sending event at 0,%i state %s", i, pressed ? "on" : "off"); - data->callback(dev, 0, i, pressed); + LOG_DBG("Sending event at 0,%i state %s", gpio->index, pressed ? "on" : "off"); + data->callback(dev, 0, gpio->index, pressed); if (config->toggle_mode && pressed) { - kscan_inputs_set_flags(&config->inputs, &config->inputs.gpios[i]); + kscan_inputs_set_flags(&data->inputs, &gpio->spec); } } @@ -289,10 +288,11 @@ static int kscan_direct_init_input_inst(const struct device *dev, const struct g } static int kscan_direct_init_inputs(const struct device *dev) { + const struct kscan_direct_data *data = dev->data; const struct kscan_direct_config *config = dev->config; - for (int i = 0; i < config->inputs.len; i++) { - const struct gpio_dt_spec *gpio = &config->inputs.gpios[i]; + for (int i = 0; i < data->inputs.len; i++) { + const struct gpio_dt_spec *gpio = &data->inputs.gpios[i].spec; int err = kscan_direct_init_input_inst(dev, gpio, i, config->toggle_mode); if (err) { return err; @@ -307,6 +307,9 @@ static int kscan_direct_init(const struct device *dev) { data->dev = dev; + // Sort inputs by port so we can read each port just once per scan. + kscan_gpio_list_sort_by_port(&data->inputs); + kscan_direct_init_inputs(dev); k_work_init_delayable(&data->work, kscan_direct_work_handler); @@ -326,7 +329,7 @@ static const struct kscan_driver_api kscan_direct_api = { BUILD_ASSERT(INST_DEBOUNCE_RELEASE_MS(n) <= DEBOUNCE_COUNTER_MAX, \ "ZMK_KSCAN_DEBOUNCE_RELEASE_MS or debounce-release-ms is too large"); \ \ - static const struct gpio_dt_spec kscan_direct_inputs_##n[] = { \ + static struct kscan_gpio kscan_direct_inputs_##n[] = { \ LISTIFY(INST_INPUTS_LEN(n), KSCAN_DIRECT_INPUT_CFG_INIT, (, ), n)}; \ \ static struct debounce_state kscan_direct_state_##n[INST_INPUTS_LEN(n)]; \ @@ -335,10 +338,11 @@ static const struct kscan_driver_api kscan_direct_api = { (static struct kscan_direct_irq_callback kscan_direct_irqs_##n[INST_INPUTS_LEN(n)];)) \ \ static struct kscan_direct_data kscan_direct_data_##n = { \ - .pin_state = kscan_direct_state_##n, COND_INTERRUPTS((.irqs = kscan_direct_irqs_##n, ))}; \ + .inputs = KSCAN_GPIO_LIST(kscan_direct_inputs_##n), \ + .pin_state = kscan_direct_state_##n, \ + COND_INTERRUPTS((.irqs = kscan_direct_irqs_##n, ))}; \ \ static struct kscan_direct_config kscan_direct_config_##n = { \ - .inputs = KSCAN_GPIO_LIST(kscan_direct_inputs_##n), \ .debounce_config = \ { \ .debounce_press_ms = INST_DEBOUNCE_PRESS_MS(n), \ diff --git a/app/drivers/kscan/kscan_gpio_matrix.c b/app/drivers/kscan/kscan_gpio_matrix.c index d2121273..b8e72044 100644 --- a/app/drivers/kscan/kscan_gpio_matrix.c +++ b/app/drivers/kscan/kscan_gpio_matrix.c @@ -5,6 +5,7 @@ */ #include "debounce.h" +#include "kscan_gpio.h" #include #include @@ -50,9 +51,9 @@ LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL); COND_CODE_1(CONFIG_ZMK_KSCAN_MATRIX_POLLING, pollcode, intcode) #define KSCAN_GPIO_ROW_CFG_INIT(idx, inst_idx) \ - GPIO_DT_SPEC_GET_BY_IDX(DT_DRV_INST(inst_idx), row_gpios, idx) + KSCAN_GPIO_GET_BY_IDX(DT_DRV_INST(inst_idx), row_gpios, idx) #define KSCAN_GPIO_COL_CFG_INIT(idx, inst_idx) \ - GPIO_DT_SPEC_GET_BY_IDX(DT_DRV_INST(inst_idx), col_gpios, idx) + KSCAN_GPIO_GET_BY_IDX(DT_DRV_INST(inst_idx), col_gpios, idx) enum kscan_diode_direction { KSCAN_ROW2COL, @@ -66,6 +67,7 @@ struct kscan_matrix_irq_callback { struct kscan_matrix_data { const struct device *dev; + struct kscan_gpio_list inputs; kscan_callback_t callback; struct k_work_delayable work; #if USE_INTERRUPTS @@ -76,26 +78,16 @@ struct kscan_matrix_data { int64_t scan_time; /** * Current state of the matrix as a flattened 2D array of length - * (config->rows.len * config->cols.len) + * (config->rows * config->cols) */ struct debounce_state *matrix_state; }; -struct kscan_gpio_list { - const struct gpio_dt_spec *gpios; - size_t len; -}; - -/** Define a kscan_gpio_list from a compile-time GPIO array. */ -#define KSCAN_GPIO_LIST(gpio_array) \ - ((struct kscan_gpio_list){.gpios = gpio_array, .len = ARRAY_SIZE(gpio_array)}) - struct kscan_matrix_config { - struct kscan_gpio_list rows; - struct kscan_gpio_list cols; - struct kscan_gpio_list inputs; struct kscan_gpio_list outputs; struct debounce_config debounce_config; + size_t rows; + size_t cols; int32_t debounce_scan_period_ms; int32_t poll_period_ms; enum kscan_diode_direction diode_direction; @@ -105,10 +97,10 @@ struct kscan_matrix_config { * Get the index into a matrix state array from a row and column. */ static int state_index_rc(const struct kscan_matrix_config *config, const int row, const int col) { - __ASSERT(row < config->rows.len, "Invalid row %i", row); - __ASSERT(col < config->cols.len, "Invalid column %i", col); + __ASSERT(row < config->rows, "Invalid row %i", row); + __ASSERT(col < config->cols, "Invalid column %i", col); - return (col * config->rows.len) + row; + return (col * config->rows) + row; } /** @@ -125,7 +117,7 @@ static int kscan_matrix_set_all_outputs(const struct device *dev, const int valu const struct kscan_matrix_config *config = dev->config; for (int i = 0; i < config->outputs.len; i++) { - const struct gpio_dt_spec *gpio = &config->outputs.gpios[i]; + const struct gpio_dt_spec *gpio = &config->outputs.gpios[i].spec; int err = gpio_pin_set_dt(gpio, value); if (err) { @@ -139,10 +131,10 @@ static int kscan_matrix_set_all_outputs(const struct device *dev, const int valu #if USE_INTERRUPTS static int kscan_matrix_interrupt_configure(const struct device *dev, const gpio_flags_t flags) { - const struct kscan_matrix_config *config = dev->config; + const struct kscan_matrix_data *data = dev->data; - for (int i = 0; i < config->inputs.len; i++) { - const struct gpio_dt_spec *gpio = &config->inputs.gpios[i]; + for (int i = 0; i < data->inputs.len; i++) { + const struct gpio_dt_spec *gpio = &data->inputs.gpios[i].spec; int err = gpio_pin_interrupt_configure_dt(gpio, flags); if (err) { @@ -226,32 +218,37 @@ static int kscan_matrix_read(const struct device *dev) { const struct kscan_matrix_config *config = dev->config; // Scan the matrix. - for (int o = 0; o < config->outputs.len; o++) { - const struct gpio_dt_spec *out_gpio = &config->outputs.gpios[o]; + for (int i = 0; i < config->outputs.len; i++) { + const struct kscan_gpio *out_gpio = &config->outputs.gpios[i]; - int err = gpio_pin_set_dt(out_gpio, 1); + int err = gpio_pin_set_dt(&out_gpio->spec, 1); if (err) { - LOG_ERR("Failed to set output %i active: %i", o, err); + LOG_ERR("Failed to set output %i active: %i", out_gpio->index, err); return err; } #if CONFIG_ZMK_KSCAN_MATRIX_WAIT_BEFORE_INPUTS > 0 k_busy_wait(CONFIG_ZMK_KSCAN_MATRIX_WAIT_BEFORE_INPUTS); #endif + struct kscan_gpio_port_state state = {0}; - for (int i = 0; i < config->inputs.len; i++) { - const struct gpio_dt_spec *in_gpio = &config->inputs.gpios[i]; + for (int j = 0; j < data->inputs.len; j++) { + const struct kscan_gpio *in_gpio = &data->inputs.gpios[j]; - const int index = state_index_io(config, i, o); - const bool active = gpio_pin_get_dt(in_gpio); + const int index = state_index_io(config, in_gpio->index, out_gpio->index); + const int active = kscan_gpio_pin_get(in_gpio, &state); + if (active < 0) { + LOG_ERR("Failed to read port %s: %i", in_gpio->spec.port->name, active); + return active; + } debounce_update(&data->matrix_state[index], active, config->debounce_scan_period_ms, &config->debounce_config); } - err = gpio_pin_set_dt(out_gpio, 0); + err = gpio_pin_set_dt(&out_gpio->spec, 0); if (err) { - LOG_ERR("Failed to set output %i inactive: %i", o, err); + LOG_ERR("Failed to set output %i inactive: %i", out_gpio->index, err); return err; } @@ -263,8 +260,8 @@ static int kscan_matrix_read(const struct device *dev) { // Process the new state. bool continue_scan = false; - for (int r = 0; r < config->rows.len; r++) { - for (int c = 0; c < config->cols.len; c++) { + for (int r = 0; r < config->rows; r++) { + for (int c = 0; c < config->cols; c++) { const int index = state_index_rc(config, r, c); struct debounce_state *state = &data->matrix_state[index]; @@ -329,28 +326,28 @@ static int kscan_matrix_disable(const struct device *dev) { #endif } -static int kscan_matrix_init_input_inst(const struct device *dev, const struct gpio_dt_spec *gpio, - const int index) { - if (!device_is_ready(gpio->port)) { - LOG_ERR("GPIO is not ready: %s", gpio->port->name); +static int kscan_matrix_init_input_inst(const struct device *dev, const struct kscan_gpio *gpio) { + if (!device_is_ready(gpio->spec.port)) { + LOG_ERR("GPIO is not ready: %s", gpio->spec.port->name); return -ENODEV; } - int err = gpio_pin_configure_dt(gpio, GPIO_INPUT); + int err = gpio_pin_configure_dt(&gpio->spec, GPIO_INPUT); if (err) { - LOG_ERR("Unable to configure pin %u on %s for input", gpio->pin, gpio->port->name); + LOG_ERR("Unable to configure pin %u on %s for input", gpio->spec.pin, + gpio->spec.port->name); return err; } - LOG_DBG("Configured pin %u on %s for input", gpio->pin, gpio->port->name); + LOG_DBG("Configured pin %u on %s for input", gpio->spec.pin, gpio->spec.port->name); #if USE_INTERRUPTS struct kscan_matrix_data *data = dev->data; - struct kscan_matrix_irq_callback *irq = &data->irqs[index]; + struct kscan_matrix_irq_callback *irq = &data->irqs[gpio->index]; irq->dev = dev; - gpio_init_callback(&irq->callback, kscan_matrix_irq_callback_handler, BIT(gpio->pin)); - err = gpio_add_callback(gpio->port, &irq->callback); + gpio_init_callback(&irq->callback, kscan_matrix_irq_callback_handler, BIT(gpio->spec.pin)); + err = gpio_add_callback(gpio->spec.port, &irq->callback); if (err) { LOG_ERR("Error adding the callback to the input device: %i", err); return err; @@ -361,11 +358,11 @@ static int kscan_matrix_init_input_inst(const struct device *dev, const struct g } static int kscan_matrix_init_inputs(const struct device *dev) { - const struct kscan_matrix_config *config = dev->config; + const struct kscan_matrix_data *data = dev->data; - for (int i = 0; i < config->inputs.len; i++) { - const struct gpio_dt_spec *gpio = &config->inputs.gpios[i]; - int err = kscan_matrix_init_input_inst(dev, gpio, i); + for (int i = 0; i < data->inputs.len; i++) { + const struct kscan_gpio *gpio = &data->inputs.gpios[i]; + int err = kscan_matrix_init_input_inst(dev, gpio); if (err) { return err; } @@ -396,7 +393,7 @@ static int kscan_matrix_init_outputs(const struct device *dev) { const struct kscan_matrix_config *config = dev->config; for (int i = 0; i < config->outputs.len; i++) { - const struct gpio_dt_spec *gpio = &config->outputs.gpios[i]; + const struct gpio_dt_spec *gpio = &config->outputs.gpios[i].spec; int err = kscan_matrix_init_output_inst(dev, gpio); if (err) { return err; @@ -411,6 +408,9 @@ static int kscan_matrix_init(const struct device *dev) { data->dev = dev; + // Sort inputs by port so we can read each port just once per scan. + kscan_gpio_list_sort_by_port(&data->inputs); + kscan_matrix_init_inputs(dev); kscan_matrix_init_outputs(dev); kscan_matrix_set_all_outputs(dev, 0); @@ -432,10 +432,10 @@ static const struct kscan_driver_api kscan_matrix_api = { BUILD_ASSERT(INST_DEBOUNCE_RELEASE_MS(n) <= DEBOUNCE_COUNTER_MAX, \ "ZMK_KSCAN_DEBOUNCE_RELEASE_MS or debounce-release-ms is too large"); \ \ - static const struct gpio_dt_spec kscan_matrix_rows_##n[] = { \ + static struct kscan_gpio kscan_matrix_rows_##n[] = { \ LISTIFY(INST_ROWS_LEN(n), KSCAN_GPIO_ROW_CFG_INIT, (, ), n)}; \ \ - static const struct gpio_dt_spec kscan_matrix_cols_##n[] = { \ + static struct kscan_gpio kscan_matrix_cols_##n[] = { \ LISTIFY(INST_COLS_LEN(n), KSCAN_GPIO_COL_CFG_INIT, (, ), n)}; \ \ static struct debounce_state kscan_matrix_state_##n[INST_MATRIX_LEN(n)]; \ @@ -444,14 +444,14 @@ static const struct kscan_driver_api kscan_matrix_api = { (static struct kscan_matrix_irq_callback kscan_matrix_irqs_##n[INST_INPUTS_LEN(n)];)) \ \ static struct kscan_matrix_data kscan_matrix_data_##n = { \ + .inputs = \ + KSCAN_GPIO_LIST(COND_DIODE_DIR(n, (kscan_matrix_cols_##n), (kscan_matrix_rows_##n))), \ .matrix_state = kscan_matrix_state_##n, \ COND_INTERRUPTS((.irqs = kscan_matrix_irqs_##n, ))}; \ \ static struct kscan_matrix_config kscan_matrix_config_##n = { \ - .rows = KSCAN_GPIO_LIST(kscan_matrix_rows_##n), \ - .cols = KSCAN_GPIO_LIST(kscan_matrix_cols_##n), \ - .inputs = \ - KSCAN_GPIO_LIST(COND_DIODE_DIR(n, (kscan_matrix_cols_##n), (kscan_matrix_rows_##n))), \ + .rows = ARRAY_SIZE(kscan_matrix_rows_##n), \ + .cols = ARRAY_SIZE(kscan_matrix_cols_##n), \ .outputs = \ KSCAN_GPIO_LIST(COND_DIODE_DIR(n, (kscan_matrix_rows_##n), (kscan_matrix_cols_##n))), \ .debounce_config = \ diff --git a/app/include/zmk/ble.h b/app/include/zmk/ble.h index 4380a33a..1c84777d 100644 --- a/app/include/zmk/ble.h +++ b/app/include/zmk/ble.h @@ -15,7 +15,7 @@ #if ZMK_BLE_IS_CENTRAL #define ZMK_BLE_PROFILE_COUNT (CONFIG_BT_MAX_PAIRED - 1) -#define ZMK_BLE_SPLIT_PERIPHERAL_COUNT 1 +#define ZMK_SPLIT_BLE_PERIPHERAL_COUNT 1 #else #define ZMK_BLE_PROFILE_COUNT CONFIG_BT_MAX_PAIRED #endif diff --git a/app/src/ble.c b/app/src/ble.c index a7037d0c..c7bdc401 100644 --- a/app/src/ble.c +++ b/app/src/ble.c @@ -118,6 +118,7 @@ void set_profile_address(uint8_t index, const bt_addr_le_t *addr) { bool zmk_ble_active_profile_is_connected() { struct bt_conn *conn; + struct bt_conn_info info; bt_addr_le_t *addr = zmk_ble_active_profile_addr(); if (!bt_addr_le_cmp(addr, BT_ADDR_LE_ANY)) { return false; @@ -125,9 +126,11 @@ bool zmk_ble_active_profile_is_connected() { return false; } + bt_conn_get_info(conn, &info); + bt_conn_unref(conn); - return true; + return info.state == BT_CONN_STATE_CONNECTED; } #define CHECKED_ADV_STOP() \ diff --git a/app/src/display/Kconfig b/app/src/display/Kconfig index 32be2a27..63ba968b 100644 --- a/app/src/display/Kconfig +++ b/app/src/display/Kconfig @@ -6,9 +6,8 @@ menuconfig ZMK_DISPLAY default n select DISPLAY select LVGL - select LV_THEMES - select LV_THEME_MONO select LV_CONF_MINIMAL + imply LV_USE_THEME_MONO if ZMK_DISPLAY @@ -16,6 +15,13 @@ config ZMK_DISPLAY_BLANK_ON_IDLE bool "Blank display on idle" default y if SSD1306 +if LV_USE_THEME_MONO + +config ZMK_DISPLAY_INVERT + bool "Invert display colors" + +endif + choice LV_TXT_ENC default LV_TXT_ENC_UTF8 diff --git a/app/src/display/main.c b/app/src/display/main.c index e34f8a53..e15e2de0 100644 --- a/app/src/display/main.c +++ b/app/src/display/main.c @@ -91,7 +91,8 @@ int zmk_display_is_initialized() { return initialized; } static void initialize_theme() { #if IS_ENABLED(CONFIG_LV_USE_THEME_MONO) lv_disp_t *disp = lv_disp_get_default(); - lv_theme_t *theme = lv_theme_mono_init(disp, false, CONFIG_LV_FONT_DEFAULT); + lv_theme_t *theme = + lv_theme_mono_init(disp, IS_ENABLED(CONFIG_ZMK_DISPLAY_INVERT), CONFIG_LV_FONT_DEFAULT); theme->font_small = CONFIG_ZMK_LV_FONT_DEFAULT_SMALL; disp->theme = theme; diff --git a/app/src/keymap.c b/app/src/keymap.c index 909fd20d..da25fc09 100644 --- a/app/src/keymap.c +++ b/app/src/keymap.c @@ -218,7 +218,7 @@ int zmk_keymap_apply_position_state(uint8_t source, int layer, uint32_t position #endif case BEHAVIOR_LOCALITY_GLOBAL: #if ZMK_BLE_IS_CENTRAL - for (int i = 0; i < ZMK_BLE_SPLIT_PERIPHERAL_COUNT; i++) { + for (int i = 0; i < ZMK_SPLIT_BLE_PERIPHERAL_COUNT; i++) { zmk_split_bt_invoke_behavior(i, &binding, event, pressed); } #endif diff --git a/app/src/split/bluetooth/Kconfig b/app/src/split/bluetooth/Kconfig index 53119d82..005d75fd 100644 --- a/app/src/split/bluetooth/Kconfig +++ b/app/src/split/bluetooth/Kconfig @@ -21,11 +21,11 @@ config ZMK_SPLIT_BLE_CENTRAL_POSITION_QUEUE_SIZE int "Max number of key position state events to queue when received from peripherals" default 5 -config ZMK_BLE_SPLIT_CENTRAL_SPLIT_RUN_STACK_SIZE +config ZMK_SPLIT_BLE_CENTRAL_SPLIT_RUN_STACK_SIZE int "BLE split central write thread stack size" default 512 -config ZMK_BLE_SPLIT_CENTRAL_SPLIT_RUN_QUEUE_SIZE +config ZMK_SPLIT_BLE_CENTRAL_SPLIT_RUN_QUEUE_SIZE int "Max number of behavior run events to queue to send to the peripheral(s)" default 5 diff --git a/app/src/split/bluetooth/central.c b/app/src/split/bluetooth/central.c index ac073c59..a7c0d8a9 100644 --- a/app/src/split/bluetooth/central.c +++ b/app/src/split/bluetooth/central.c @@ -47,7 +47,7 @@ struct peripheral_slot { uint8_t changed_positions[POSITION_STATE_DATA_LEN]; }; -static struct peripheral_slot peripherals[ZMK_BLE_SPLIT_PERIPHERAL_COUNT]; +static struct peripheral_slot peripherals[ZMK_SPLIT_BLE_PERIPHERAL_COUNT]; static const struct bt_uuid_128 split_service_uuid = BT_UUID_INIT_128(ZMK_SPLIT_BT_SERVICE_UUID); @@ -65,7 +65,7 @@ void peripheral_event_work_callback(struct k_work *work) { K_WORK_DEFINE(peripheral_event_work, peripheral_event_work_callback); int peripheral_slot_index_for_conn(struct bt_conn *conn) { - for (int i = 0; i < ZMK_BLE_SPLIT_PERIPHERAL_COUNT; i++) { + for (int i = 0; i < ZMK_SPLIT_BLE_PERIPHERAL_COUNT; i++) { if (peripherals[i].conn == conn) { return i; } @@ -84,7 +84,7 @@ struct peripheral_slot *peripheral_slot_for_conn(struct bt_conn *conn) { } int release_peripheral_slot(int index) { - if (index < 0 || index >= ZMK_BLE_SPLIT_PERIPHERAL_COUNT) { + if (index < 0 || index >= ZMK_SPLIT_BLE_PERIPHERAL_COUNT) { return -EINVAL; } @@ -131,7 +131,7 @@ int release_peripheral_slot(int index) { } int reserve_peripheral_slot() { - for (int i = 0; i < ZMK_BLE_SPLIT_PERIPHERAL_COUNT; i++) { + for (int i = 0; i < ZMK_SPLIT_BLE_PERIPHERAL_COUNT; i++) { if (peripherals[i].state == PERIPHERAL_SLOT_STATE_OPEN) { // Be sure the slot is fully reinitialized. release_peripheral_slot(i); @@ -504,7 +504,7 @@ static struct bt_conn_cb conn_callbacks = { }; K_THREAD_STACK_DEFINE(split_central_split_run_q_stack, - CONFIG_ZMK_BLE_SPLIT_CENTRAL_SPLIT_RUN_STACK_SIZE); + CONFIG_ZMK_SPLIT_BLE_CENTRAL_SPLIT_RUN_STACK_SIZE); struct k_work_q split_central_split_run_q; @@ -515,7 +515,7 @@ struct zmk_split_run_behavior_payload_wrapper { K_MSGQ_DEFINE(zmk_split_central_split_run_msgq, sizeof(struct zmk_split_run_behavior_payload_wrapper), - CONFIG_ZMK_BLE_SPLIT_CENTRAL_SPLIT_RUN_QUEUE_SIZE, 4); + CONFIG_ZMK_SPLIT_BLE_CENTRAL_SPLIT_RUN_QUEUE_SIZE, 4); void split_central_split_run_callback(struct k_work *work) { struct zmk_split_run_behavior_payload_wrapper payload_wrapper; diff --git a/docs/docs/behaviors/hold-tap.md b/docs/docs/behaviors/hold-tap.md index f0096606..2a8489a1 100644 --- a/docs/docs/behaviors/hold-tap.md +++ b/docs/docs/behaviors/hold-tap.md @@ -133,6 +133,23 @@ See the following example, which uses a hold-tap behavior definition, configured By default, `hold-trigger-key-positions` are evaluated upon the first _key press_ after the hold-tap. For homerow mods, this is not always ideal, because it prevents combining multiple modifiers unless they are included in `hold-trigger-key-positions`. To overwrite this behavior, one can set `hold-trigger-on-release`. If set to true, the evaluation of `hold-trigger-key-positions` gets delayed until _key release_. This allows combining multiple modifiers when the next key is _held_, while still deciding the hold-tap in favor of a tap when the next key is _tapped_. +#### Using different behavior types with hold-taps + +You can create instances of hold-taps invoking most [behavior types](../features/keymaps.md#behaviors) for hold or tap actions, by referencing their node labels in the `bindings` value. +The two parameters that are passed to the hold-tap in your keymap will be forwarded to the referred behaviors, first one to the hold behavior and second one to the tap. + +If you use behaviors that accept no parameters such as [mod-morphs](mod-morph.md) or [macros](macros.md), you can pass a dummy parameter value such as `0` to the hold-tap when you use it in your keymap. +For instance, a hold-tap with node label `caps` and `bindings = <&kp>, <&caps_word>;` can be used in the keymap as below to send the caps lock keycode on hold and invoke the [caps word behavior](caps-word.md) on tap: + +``` +&caps CAPS 0 +``` + +:::info +You cannot use behaviors that expect more than one parameter such as [`&bt`](bluetooth.md) and [`&rgb_ug`](underglow.md) with hold-taps, due to the limitations of the [devicetree keymap format](../config/index.md#devicetree-files). +One workaround is to create a [macro](macros.md) that invokes those behaviors and use the macro as the hold or tap action. +::: + ### Example Use-Cases