diff options
230 files changed, 6645 insertions, 2901 deletions
diff --git a/.clang-format b/.clang-format new file mode 100644 index 0000000..3c29522 --- /dev/null +++ b/.clang-format @@ -0,0 +1,4 @@ +BasedOnStyle: LLVM +IndentWidth: 4 +ColumnLimit: 100 +SortIncludes: false diff --git a/.devcontainer/.bashrc b/.devcontainer/.bashrc new file mode 100644 index 0000000..9fdb8f6 --- /dev/null +++ b/.devcontainer/.bashrc @@ -0,0 +1,8 @@ +export LS_OPTIONS='-F --color=auto' +alias ls='ls $LS_OPTIONS' +if [ "${CODESPACES}" = "true" ]; then + export WORKSPACE_DIR="$HOME/workspace/zmk" +fi +if [ -f "$WORKSPACE_DIR/zephyr/zephyr-env.sh" ]; then + source "$WORKSPACE_DIR/zephyr/zephyr-env.sh" +fi diff --git a/.devcontainer/Dockerfile b/.devcontainer/Dockerfile new file mode 100644 index 0000000..184aae9 --- /dev/null +++ b/.devcontainer/Dockerfile @@ -0,0 +1,11 @@ +FROM zmkfirmware/zephyr-west-action-arm + +RUN apt-get -y update && \ + apt-get -y upgrade && \ + apt-get install --no-install-recommends -y \ + ssh \ + gpg && \ + rm -rf /var/lib/apt/lists/* + +COPY .bashrc tmp +RUN mv /tmp/.bashrc ~/.bashrc diff --git a/.devcontainer/devcontainer.json b/.devcontainer/devcontainer.json new file mode 100644 index 0000000..14feeff --- /dev/null +++ b/.devcontainer/devcontainer.json @@ -0,0 +1,11 @@ +{ + "name": "ZMK Development", + "dockerFile": "Dockerfile", + "extensions": ["ms-vscode.cpptools"], + "runArgs": ["--security-opt", "label=disable"], + "containerEnv": {"WORKSPACE_DIR": "${containerWorkspaceFolder}"}, + "settings": { + "terminal.integrated.shell.linux": "/bin/bash" + }, +} + diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000..3d05d86 --- /dev/null +++ b/.gitattributes @@ -0,0 +1,6 @@ +* text=auto + +# Always use Unix-style line endings for Bash scripts so they work in +# Docker on Windows. +.bashrc text eol=lf +*.sh text eol=lf diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index c8a711c..60c337d 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -16,7 +16,13 @@ jobs: - kyria_right - lily58_left - lily58_right + - iris_left + - iris_right - romac + - romac_plus + - settings_reset + - quefrency_left + - quefrency_right include: - board: proton_c shield: clueboard_california @@ -69,7 +75,7 @@ jobs: args: 'build "-s app -b ${{ matrix.board }} -- -DSHIELD=${{ matrix.shield }}"' - name: Archive Build uses: actions/upload-artifact@v2 - if: ${{ matrix.board == 'nice_nano' }} + if: ${{ matrix.board != 'proton_c' }} with: name: "${{ matrix.board }}-${{ matrix.shield }}-zmk-uf2" path: build/zephyr/zmk.uf2 diff --git a/.github/workflows/clang-format-lint.yml b/.github/workflows/clang-format-lint.yml new file mode 100644 index 0000000..70ae986 --- /dev/null +++ b/.github/workflows/clang-format-lint.yml @@ -0,0 +1,29 @@ +name: clang-format-lint + +on: + push: + paths: + - "app/boards/**/*.c" + - "app/include/**/*.h" + - "app/src/**" + - "app/drivers/**/*.c" + - "app/drivers/**/*.h" + pull_request: + paths: + - "app/boards/**/*.c" + - "app/include/**/*.h" + - "app/src/**" + - "app/drivers/**/*.c" + - "app/drivers/**/*.h" + +jobs: + build: + runs-on: ubuntu-latest + name: clang-format lint + + steps: + - uses: actions/checkout@v2 + - uses: DoozyX/clang-format-lint-action@v0.9 + with: + source: "./app" + extensions: "h,c" diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 3d2c76b..a6af058 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -1,4 +1,12 @@ -on: [push, pull_request] +on: + push: + paths: + - "app/tests/**" + - "app/src/**" + pull_request: + paths: + - "app/tests/**" + - "app/src/**" name: Test @@ -4,3 +4,4 @@ /tools /zephyr /build +*.DS_Store @@ -0,0 +1,11 @@ +# The ZMK Project consists of many contributors. This file includes individuals +# who have contributed significant changes to the project. To be added to here, +# please submit a PR to the project repo. +Peter Johanson (@petejohanson) +Innovaker (@innovaker) +Nick Winans (@Nicell) +Okke Formsma (@okke-formsma) +Cody McGinnis (@BrainWart) +Kurtis Lew (@kurtis-lew) +Richard Jones (@bmcgavin) +Kevin Chen (@chenkevinh)
\ No newline at end of file diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md new file mode 100644 index 0000000..3ea8c78 --- /dev/null +++ b/CONTRIBUTING.md @@ -0,0 +1,120 @@ +# Contributing To ZMK + +Thanks for taking an interest in contributing to ZMK! After reading through the documentation, if +you have any questions, please come join us on the +[ZMK Discord Server][discord-invite]. + +## Code of Conduct + +All community members are expected to abide by the [Code of Conduct][code-of-conduct]. +For any and all conduct inquiries or concerns, please contact conduct@zmkfirmware.dev. + +[code-of-conduct]: https://github.com/zmkfirmware/zmk/blob/main/CODE_OF_CONDUCT.md + +## How Can I Contribute + +There are many different ways that you can contribute to ZMK, several of which require no coding +abilities. These include: + +- Chat Support +- Issue Reporting/Commenting +- Testing +- Documentation +- Code Contributions + +## Chat Support + +Providing user support on the [ZMK Discord Server][discord-invite] is a great way to help the +project. In particular, answering questions in the [#help](https://discord.com/channels/719497620560543766/719909884769992755) channel is incredibly appreciated. + +## Issue Reporting/Commenting + +Often, you might encounter unexpected behavior when building, flashing, or running the ZMK +firmware. Submitting or commenting on issues on GitHub is a great way to contribute to the +ZMK project. + +### Before Submitting a Report + +- Review the [Frequently Asked Questions](https://zmkfirmware.dev/docs/faq). +- Check the [Troubleshooting Guide](https://zmkfirmware.dev/docs/troubleshooting) for answers. +- Search the [open issues](https://github.com/zmkfirmware/zmk/issues) for an existing report that + matches your problem. + +### Opening A Report + +To open a report: + +- Head to https://github.com/zmkfirmware/zmk/issues/new +- Provide an accurate summary of the issue in the title. +- Provide as much detail as you can about the issue including: + - What [board/shield](https://zmkfirmware.dev/docs/faq#what-is-a-board) you are using. + - A link to the user repository, if you used it to build your firmware. + - Exact steps to reproduce the problem. + - Any relevant screenshots or [logs](https://zmkfirmware.dev/docs/dev-guide-usb-logging) + +## Testing + +The `help wanted` label will be added to any [pull requests](https://github.com/zmkfirmware/zmk/pulls?q=is%3Aopen+is%3Apr+label%3A%22help+wanted%22) +or [issues](https://github.com/zmkfirmware/zmk/issues?q=is%3Aopen+is%3Aissue+label%3A%22help+wanted%22) +where user testing can assist the ZMK contributors to verify fixes, confirm +bugs, etc. + +When providing testing feedback, please provide: + +- Exact steps used to test +- Any hardware details relevant to testing +- Pass/fail summary for testing. +- Full details of any failures, including: + - Logs + - Screenshots + +## Documentation + +Quality documentation is a huge part of what makes a successful project. Contributions to add +documentation to areas not currently covered are greatly appreciated. + +### Contributing + +- The documentation site can be found in the main ZMK repo, in the + [docs/](https://github.com/zmkfirmware/zmk/tree/main/docs) subdirectory. +- The documentation is maintained using [Docusaurus V2](https://v2.docusaurus.io/docs/). +- To get started, from the `docs/` directory, run `npm ci` and then `npm start`. +- Enhancements should be submitted as pull requests to the `main` branch of ZMK. + +## Code Contributions + +### Development Setup + +To get your development environment setup going, start at the +[basic setup](https://zmkfirmware.dev/docs/dev-setup) docs, and make sure you can build and flash +your own locally built firmware. + +### Formatting + +ZMK uses `clang-format` to ensure consist formatting for our source code. Before submitting your +changes, make sure you've manually run `clang-format`, or have your IDE configured to auto-format +on save. + +### Commit Messages + +The ZMK project is working towards, but not yet enforcing, the use of +[conventional commits](https://www.conventionalcommits.org/en/v1.0.0/) for commit messages. + +Further documentation and details will be provided here soon. + +### Pull Requests + +When opening a pull request with your changes, please: + +- Submit the PR to the `main` branch of the + [`zmkfirmware/zmk`](https://github.com/zmkfirmware/zmk) repository. +- Use a descriptive title that summarizes the change. +- In the description, include: + - References to any open issues fixed by the PR. + - Feature added by the PR + - Bugs fixed by the PR. + - Testing you've performed locally. + - Requested testing by reviewers or testers. + - Screenshots or logs that support understanding the change. + +[discord-invite]: https://zmkfirmware.dev/community/discord/invite diff --git a/app/CMakeLists.txt b/app/CMakeLists.txt index 2e24fdc..39509ed 100644 --- a/app/CMakeLists.txt +++ b/app/CMakeLists.txt @@ -23,38 +23,45 @@ zephyr_linker_sources(RODATA include/linker/zmk-events.ld) # Add your source file to the "app" target. This must come after # find_package(Zephyr) which defines the target. target_include_directories(app PRIVATE include) +target_sources_ifdef(CONFIG_ZMK_SLEEP app PRIVATE src/power.c) target_sources(app PRIVATE src/kscan.c) target_sources(app PRIVATE src/matrix_transform.c) target_sources(app PRIVATE src/hid.c) target_sources(app PRIVATE src/sensors.c) target_sources_ifdef(CONFIG_ZMK_DISPLAY app PRIVATE src/display.c) target_sources(app PRIVATE src/event_manager.c) +target_sources_ifdef(CONFIG_ZMK_BLE app PRIVATE src/ble_unpair_combo.c) +target_sources_ifdef(CONFIG_ZMK_EXT_POWER app PRIVATE src/ext_power_generic.c) target_sources(app PRIVATE src/events/position_state_changed.c) target_sources(app PRIVATE src/events/keycode_state_changed.c) target_sources(app PRIVATE src/events/modifiers_state_changed.c) target_sources(app PRIVATE src/events/sensor_event.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_hold_tap.c) -target_sources(app PRIVATE src/behaviors/behavior_momentary_layer.c) -target_sources(app PRIVATE src/behaviors/behavior_toggle_layer.c) -target_sources(app PRIVATE src/behaviors/behavior_transparent.c) -target_sources(app PRIVATE src/behaviors/behavior_none.c) -target_sources(app PRIVATE src/behaviors/behavior_sensor_rotate_key_press.c) -target_sources(app PRIVATE src/keymap.c) +target_sources_ifdef(CONFIG_ZMK_BLE app PRIVATE src/events/ble_active_profile_changed.c) +if (NOT CONFIG_ZMK_SPLIT_BLE_ROLE_PERIPHERAL) + 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_hold_tap.c) + target_sources(app PRIVATE src/behaviors/behavior_momentary_layer.c) + target_sources(app PRIVATE src/behaviors/behavior_toggle_layer.c) + target_sources(app PRIVATE src/behaviors/behavior_transparent.c) + target_sources(app PRIVATE src/behaviors/behavior_none.c) + target_sources(app PRIVATE src/behaviors/behavior_sensor_rotate_key_press.c) + target_sources_ifdef(CONFIG_ZMK_EXT_POWER app PRIVATE src/behaviors/behavior_ext_power.c) + target_sources(app PRIVATE src/keymap.c) +endif() target_sources_ifdef(CONFIG_ZMK_RGB_UNDERGLOW app PRIVATE src/behaviors/behavior_rgb_underglow.c) +target_sources_ifdef(CONFIG_ZMK_BLE app PRIVATE src/behaviors/behavior_bt.c) target_sources_ifdef(CONFIG_ZMK_BLE app PRIVATE src/ble.c) -target_sources_ifdef(CONFIG_ZMK_BLE app PRIVATE src/ble_unpair_combo.c) target_sources_ifdef(CONFIG_ZMK_SPLIT_BLE_ROLE_PERIPHERAL app PRIVATE src/split_listener.c) target_sources_ifdef(CONFIG_ZMK_SPLIT_BLE_ROLE_PERIPHERAL app PRIVATE src/split/bluetooth/service.c) target_sources_ifdef(CONFIG_ZMK_SPLIT_BLE_ROLE_CENTRAL app PRIVATE src/split/bluetooth/central.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) -target_sources_ifdef(CONFIG_ZMK_USB app PRIVATE src/usb_hid.c) +target_sources_ifdef(CONFIG_USB app PRIVATE src/usb.c) target_sources_ifdef(CONFIG_ZMK_BLE app PRIVATE src/hog.c) target_sources_ifdef(CONFIG_ZMK_RGB_UNDERGLOW app PRIVATE src/rgb_underglow.c) target_sources(app PRIVATE src/endpoints.c) target_sources(app PRIVATE src/hid_listener.c) target_sources(app PRIVATE src/main.c) -zephyr_cc_option(-Wfatal-errors)
\ No newline at end of file +zephyr_cc_option(-Wfatal-errors) diff --git a/app/Kconfig b/app/Kconfig index 877fce4..fca4912 100644 --- a/app/Kconfig +++ b/app/Kconfig @@ -21,12 +21,16 @@ menuconfig ZMK_USB select USB_DEVICE_STACK select USB_DEVICE_HID -if ZMK_USB +if USB config ZMK_USB_INIT_PRIORITY int "Init Priority" default 50 +endif + +if ZMK_USB + config USB_NUMOF_EP_WRITE_RETRIES default 10 @@ -37,21 +41,26 @@ menuconfig ZMK_BLE select BT select BT_SMP select BT_SMP_SC_PAIR_ONLY + select BT_SMP_APP_PAIRING_ACCEPT select BT_PERIPHERAL select BT_GATT_DIS select BT_GATT_BAS + select BT_SETTINGS select SETTINGS - # select BT_SETTINGS if ZMK_BLE config ZMK_BLE_INIT_PRIORITY int "Init Priority" default 50 - + config SYSTEM_WORKQUEUE_STACK_SIZE default 2048 +config ZMK_BLE_CLEAR_BONDS_ON_START + bool "Configuration that clears all bond information from the keyboard on startup." + default n + # HID GATT notifications sent this way are *not* picked up by Linux, and possibly others. config BT_GATT_NOTIFY_MULTIPLE default n @@ -67,6 +76,29 @@ endif endmenu +menuconfig ZMK_SLEEP + bool "Enable deep sleep support" + imply USB + +if ZMK_SLEEP + +config SYS_POWER_DEEP_SLEEP_STATES + default y + +choice SYS_PM_POLICY + default SYS_PM_POLICY_APP +endchoice + +config ZMK_IDLE_SLEEP_TIMEOUT + int "Milliseconds to wait to sleep when going idle" + default 900000 + +endif + +config ZMK_EXT_POWER + bool "Enable support to control external power output" + default y + config ZMK_DISPLAY bool "ZMK display support" default n @@ -101,17 +133,6 @@ config ZMK_SPLIT_BLE_ROLE_CENTRAL select BT_CENTRAL select BT_GATT_CLIENT -if ZMK_SPLIT_BLE_ROLE_CENTRAL - -config BT_MAX_CONN - default 5 - -config BT_MAX_PAIRED - # Bump this everywhere once we support switching active connections! - default 2 - -endif - config ZMK_SPLIT_BLE_ROLE_PERIPHERAL bool "Peripheral" @@ -120,9 +141,11 @@ if ZMK_SPLIT_BLE_ROLE_PERIPHERAL config ZMK_USB default n +config BT_MAX_PAIRED + default 1 config BT_MAX_CONN - default 5 + default 1 config BT_GAP_AUTO_UPDATE_CONN_PARAMS default n @@ -135,8 +158,31 @@ endif endif -endmenu +if ZMK_BLE + +if ZMK_SPLIT_BLE && ZMK_SPLIT_BLE_ROLE_CENTRAL + +config BT_MAX_CONN + default 6 + +config BT_MAX_PAIRED + default 6 + +endif +if !ZMK_SPLIT_BLE + +config BT_MAX_CONN + default 5 + +config BT_MAX_PAIRED + default 5 + +endif + +endif + +endmenu config ZMK_KSCAN_MOCK_DRIVER bool "Enable mock kscan driver to simulate key presses" diff --git a/app/boards/arm/dz60rgb/CMakeLists.txt b/app/boards/arm/dz60rgb/CMakeLists.txt new file mode 100644 index 0000000..940af1f --- /dev/null +++ b/app/boards/arm/dz60rgb/CMakeLists.txt @@ -0,0 +1,7 @@ +# SPDX-License-Identifier: MIT + +if(CONFIG_PINMUX) +zephyr_library() +zephyr_library_sources(pinmux.c) +zephyr_library_include_directories(${ZEPHYR_BASE}/drivers) +endif() diff --git a/app/boards/arm/dz60rgb/Kconfig.board b/app/boards/arm/dz60rgb/Kconfig.board new file mode 100644 index 0000000..ec8dad6 --- /dev/null +++ b/app/boards/arm/dz60rgb/Kconfig.board @@ -0,0 +1,6 @@ +# Copyright (c) 2020 The ZMK Contributors +# SPDX-License-Identifier: MIT + +config BOARD_DZ60RGB_REV1 + bool "DZ60RGB Keyboard" + depends on SOC_STM32F303XC diff --git a/app/boards/arm/dz60rgb/Kconfig.defconfig b/app/boards/arm/dz60rgb/Kconfig.defconfig new file mode 100644 index 0000000..779d312 --- /dev/null +++ b/app/boards/arm/dz60rgb/Kconfig.defconfig @@ -0,0 +1,14 @@ +# DZ60RGB keyboard configuration + +# Copyright (c) 2020 The ZMK Contributors +# SPDX-License-Identifier: MIT + +if BOARD_DZ60RGB_REV1 + +config ZMK_KEYBOARD_NAME + default "DZ60RGB Rev 1" + +config ZMK_USB + default y + +endif # BOARD_DZ60RGB_REV1 diff --git a/app/boards/arm/dz60rgb/board.cmake b/app/boards/arm/dz60rgb/board.cmake new file mode 100644 index 0000000..10f6e29 --- /dev/null +++ b/app/boards/arm/dz60rgb/board.cmake @@ -0,0 +1,7 @@ +# SPDX-License-Identifier: MIT + +board_runner_args(dfu-util "--pid=0483:df11" "--alt=0" "--dfuse") +board_runner_args(jlink "--device=STM32F303CC" "--speed=4000") + +include(${ZEPHYR_BASE}/boards/common/openocd.board.cmake) +include(${ZEPHYR_BASE}/boards/common/jlink.board.cmake) diff --git a/app/boards/arm/dz60rgb/dz60rgb_rev1.dts b/app/boards/arm/dz60rgb/dz60rgb_rev1.dts new file mode 100644 index 0000000..85c4ca5 --- /dev/null +++ b/app/boards/arm/dz60rgb/dz60rgb_rev1.dts @@ -0,0 +1,88 @@ +/* + * Copyright (c) 2020 The ZMK Contributors + * + * SPDX-License-Identifier: MIT + */ + +/dts-v1/; +#include <st/f3/stm32f303Xc.dtsi> + +#include <dt-bindings/zmk/matrix-transform.h> + +/ { + model = "DZ60RGB, Rev 1"; + compatible = "dz60rgb,rev1", "st,stm32f303"; + + chosen { + zephyr,sram = &sram0; + zephyr,flash = &flash0; + zmk,kscan = &kscan0; + zmk,matrix_transform = &default_transform; + }; + + default_transform: keymap_transform_0 { + compatible = "zmk,matrix-transform"; + columns = <14>; + rows = <5>; + 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(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(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,13) +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(4,0) RC(4,1) RC(4,2) RC(4,5) RC(4,8) RC(4,9) RC(4,10) RC(4,11) RC(4,13) + >; + }; + + kscan0: kscan { + compatible = "zmk,kscan-gpio-matrix"; + label = "KSCAN"; + + diode-direction = "col2row"; + row-gpios + = <&gpioa 9 (GPIO_ACTIVE_HIGH | GPIO_PULL_DOWN)> + , <&gpiob 10 (GPIO_ACTIVE_HIGH | GPIO_PULL_DOWN)> + , <&gpiob 11 (GPIO_ACTIVE_HIGH | GPIO_PULL_DOWN)> + , <&gpiob 14 (GPIO_ACTIVE_HIGH | GPIO_PULL_DOWN)> + , <&gpiob 12 (GPIO_ACTIVE_HIGH | GPIO_PULL_DOWN)> + ; + col-gpios + = <&gpioa 6 GPIO_ACTIVE_HIGH> + , <&gpioa 7 GPIO_ACTIVE_HIGH> + , <&gpiob 0 GPIO_ACTIVE_HIGH> + , <&gpiob 13 GPIO_ACTIVE_HIGH> + , <&gpiob 15 GPIO_ACTIVE_HIGH> + , <&gpioa 8 GPIO_ACTIVE_HIGH> + , <&gpioa 15 GPIO_ACTIVE_HIGH> + , <&gpiob 3 GPIO_ACTIVE_HIGH> + , <&gpiob 4 GPIO_ACTIVE_HIGH> + , <&gpiob 5 GPIO_ACTIVE_HIGH> + , <&gpiob 8 GPIO_ACTIVE_HIGH> + , <&gpiob 9 GPIO_ACTIVE_HIGH> + , <&gpioc 13 GPIO_ACTIVE_HIGH> + , <&gpioc 14 GPIO_ACTIVE_HIGH> + ; + }; + +}; + +&usb { + status = "okay"; +}; + +&flash0 { + /* + * For more information, see: + * http://docs.zephyrproject.org/latest/guides/dts/index.html#flash-partitions + */ + partitions { + compatible = "fixed-partitions"; + #address-cells = <1>; + #size-cells = <1>; + + /* Set 6Kb of storage at the end of the 256Kb of flash */ + storage_partition: partition@3e800 { + label = "storage"; + reg = <0x0003e800 0x00001800>; + }; + }; +}; diff --git a/app/boards/arm/dz60rgb/dz60rgb_rev1.keymap b/app/boards/arm/dz60rgb/dz60rgb_rev1.keymap new file mode 100644 index 0000000..8de01c1 --- /dev/null +++ b/app/boards/arm/dz60rgb/dz60rgb_rev1.keymap @@ -0,0 +1,25 @@ +#include <behaviors.dtsi> +#include <dt-bindings/zmk/keys.h> + +/ { + keymap { + compatible = "zmk,keymap"; + + default_layer { +// ------------------------------------------------------------------------------------------ +// | ESC | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 0 | - | = | BKSP | +// | TAB | Q | W | E | R | T | Y | U | I | O | P | [ | ] | "|" | +// | CAPS | A | S | D | F | G | H | J | K | L | ; | ' | ENTER | +// | SHIFT | Z | X | C | V | B | N | M | , | . | SHIFT(/) | ^ | DEL | +// | CTL | WIN | ALT | SPACE | ALT | MO(1) | <- | v | -> | +// ------------------------------------------------------------------------------------------ + bindings = < + &kp ESC &kp NUM_1 &kp NUM_2 &kp NUM_3 &kp NUM_4 &kp NUM_5 &kp NUM_6 &kp NUM_7 &kp NUM_8 &kp NUM_9 &kp NUM_0 &kp MINUS &kp EQL &kp BKSP + &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 CLCK &kp A &kp S &kp D &kp F &kp G &kp H &kp J &kp K &kp L &kp SCLN &kp QUOT &kp RET + &kp LSFT &kp Z &kp X &kp C &kp V &kp B &kp N &kp M &kp CMMA &kp DOT &mt MOD_RSFT FSLH &kp UARW &kp DEL + &kp LCTL &kp LGUI &kp LALT &kp SPC &kp RALT &mo 1 &kp LARW &kp DARW &kp RARW + >; + }; + }; +};
\ No newline at end of file diff --git a/app/boards/arm/dz60rgb/dz60rgb_rev1.yaml b/app/boards/arm/dz60rgb/dz60rgb_rev1.yaml new file mode 100644 index 0000000..d283621 --- /dev/null +++ b/app/boards/arm/dz60rgb/dz60rgb_rev1.yaml @@ -0,0 +1,19 @@ +identifier: DZ60RGB_rev1 +name: DZ60RGBREV1 +type: keyboard +arch: arm +toolchain: + - zephyr + - gnuarmemb + - xtools +ram: 40 +supported: + - gpio + - i2c + - counter + - spi + - usb_device + - lsm303dlhc + - nvs + - can + - kscan diff --git a/app/boards/arm/dz60rgb/dz60rgb_rev1_defconfig b/app/boards/arm/dz60rgb/dz60rgb_rev1_defconfig new file mode 100644 index 0000000..33840f9 --- /dev/null +++ b/app/boards/arm/dz60rgb/dz60rgb_rev1_defconfig @@ -0,0 +1,27 @@ +# SPDX-License-Identifier: MIT + +CONFIG_SOC_SERIES_STM32F3X=y +CONFIG_SOC_STM32F303XC=y +# 72MHz system clock +CONFIG_SYS_CLOCK_HW_CYCLES_PER_SEC=72000000 + +# enable pinmux +CONFIG_PINMUX=y + +# enable GPIO +CONFIG_GPIO=y + +# clock configuration +CONFIG_CLOCK_CONTROL=y + +# Clock configuration for Cube Clock control driver +CONFIG_CLOCK_STM32_HSE_CLOCK=8000000 +CONFIG_CLOCK_STM32_SYSCLK_SRC_PLL=y +# use HSE as PLL input +CONFIG_CLOCK_STM32_PLL_SRC_HSE=y +# produce 72MHz clock at PLL output +CONFIG_CLOCK_STM32_PLL_PREDIV=1 +CONFIG_CLOCK_STM32_PLL_MULTIPLIER=9 +CONFIG_CLOCK_STM32_AHB_PRESCALER=1 +CONFIG_CLOCK_STM32_APB1_PRESCALER=2 +CONFIG_CLOCK_STM32_APB2_PRESCALER=1 diff --git a/app/boards/arm/dz60rgb/pinmux.c b/app/boards/arm/dz60rgb/pinmux.c new file mode 100644 index 0000000..f59a31b --- /dev/null +++ b/app/boards/arm/dz60rgb/pinmux.c @@ -0,0 +1,67 @@ +/* + * Copyright (c) 2017 I-SENSE group of ICCS + * + * SPDX-License-Identifier: MIT + */ + +#include <kernel.h> +#include <device.h> +#include <init.h> +#include <drivers/pinmux.h> +#include <sys/sys_io.h> + +#include <pinmux/stm32/pinmux_stm32.h> + +/* pin assignments for STM32F3DISCOVERY board */ +static const struct pin_config pinconf[] = { +#if DT_NODE_HAS_STATUS(DT_NODELABEL(usart1), okay) && CONFIG_SERIAL + {STM32_PIN_PC4, STM32F3_PINMUX_FUNC_PC4_USART1_TX}, + {STM32_PIN_PC5, STM32F3_PINMUX_FUNC_PC5_USART1_RX}, +#endif +#if DT_NODE_HAS_STATUS(DT_NODELABEL(usart2), okay) && CONFIG_SERIAL + {STM32_PIN_PA2, STM32F3_PINMUX_FUNC_PA2_USART2_TX}, + {STM32_PIN_PA3, STM32F3_PINMUX_FUNC_PA3_USART2_RX}, +#endif +#if DT_NODE_HAS_STATUS(DT_NODELABEL(i2c1), okay) && CONFIG_I2C + {STM32_PIN_PB6, STM32F3_PINMUX_FUNC_PB6_I2C1_SCL}, + {STM32_PIN_PB7, STM32F3_PINMUX_FUNC_PB7_I2C1_SDA}, +#endif +#if DT_NODE_HAS_STATUS(DT_NODELABEL(i2c2), okay) && CONFIG_I2C + {STM32_PIN_PA9, STM32F3_PINMUX_FUNC_PA9_I2C2_SCL}, + {STM32_PIN_PA10, STM32F3_PINMUX_FUNC_PA10_I2C2_SDA}, +#endif +#if DT_NODE_HAS_STATUS(DT_NODELABEL(spi1), okay) && CONFIG_SPI +#ifdef CONFIG_SPI_STM32_USE_HW_SS + {STM32_PIN_PA4, STM32F3_PINMUX_FUNC_PA4_SPI1_NSS}, +#endif /* CONFIG_SPI_STM32_USE_HW_SS */ + {STM32_PIN_PA5, STM32F3_PINMUX_FUNC_PA5_SPI1_SCK}, + {STM32_PIN_PA6, STM32F3_PINMUX_FUNC_PA6_SPI1_MISO}, + {STM32_PIN_PA7, STM32F3_PINMUX_FUNC_PA7_SPI1_MOSI}, +#endif +#if DT_NODE_HAS_STATUS(DT_NODELABEL(spi2), okay) && CONFIG_SPI +#ifdef CONFIG_SPI_STM32_USE_HW_SS + {STM32_PIN_PB12, STM32F3_PINMUX_FUNC_PB12_SPI2_NSS}, +#endif /* CONFIG_SPI_STM32_USE_HW_SS */ + {STM32_PIN_PB13, STM32F3_PINMUX_FUNC_PB13_SPI2_SCK}, + {STM32_PIN_PB14, STM32F3_PINMUX_FUNC_PB14_SPI2_MISO}, + {STM32_PIN_PB15, STM32F3_PINMUX_FUNC_PB15_SPI2_MOSI}, +#endif +#ifdef CONFIG_USB_DC_STM32 + {STM32_PIN_PA11, STM32F3_PINMUX_FUNC_PA11_USB_DM}, + {STM32_PIN_PA12, STM32F3_PINMUX_FUNC_PA12_USB_DP}, +#endif /* CONFIG_USB_DC_STM32 */ +#if DT_NODE_HAS_STATUS(DT_NODELABEL(can1), okay) && CONFIG_CAN + {STM32_PIN_PD0, STM32F3_PINMUX_FUNC_PD0_CAN1_RX}, + {STM32_PIN_PD1, STM32F3_PINMUX_FUNC_PD1_CAN1_TX}, +#endif +}; + +static int pinmux_stm32_init(struct device *port) { + ARG_UNUSED(port); + + stm32_setup_pins(pinconf, ARRAY_SIZE(pinconf)); + + return 0; +} + +SYS_INIT(pinmux_stm32_init, PRE_KERNEL_1, CONFIG_PINMUX_STM32_DEVICE_INITIALIZATION_PRIORITY);
\ No newline at end of file diff --git a/app/boards/arm/nice_nano/nice_nano.dts b/app/boards/arm/nice_nano/nice_nano.dts index 2e9556b..0538b1d 100644 --- a/app/boards/arm/nice_nano/nice_nano.dts +++ b/app/boards/arm/nice_nano/nice_nano.dts @@ -29,6 +29,15 @@ }; }; + ext-power { + compatible = "zmk,ext-power-generic"; + label = "EXT_POWER"; + control-gpios = <&gpio0 13 GPIO_ACTIVE_LOW>; + }; +}; + +&gpiote { + status = "okay"; }; &gpio0 { diff --git a/app/boards/arm/nrf52840_m2/CMakeLists.txt b/app/boards/arm/nrf52840_m2/CMakeLists.txt new file mode 100644 index 0000000..84b2ab9 --- /dev/null +++ b/app/boards/arm/nrf52840_m2/CMakeLists.txt @@ -0,0 +1,13 @@ +# +# Copyright (c) 2020 The ZMK Contributors +# SPDX-License-Identifier: MIT +# + +set_property(GLOBAL APPEND PROPERTY extra_post_build_commands + COMMAND ${PYTHON_EXECUTABLE} ${ZEPHYR_BASE}/../tools/uf2/utils/uf2conv.py + -c + -b 0x26000 + -f 0xADA52840 + -o ${PROJECT_BINARY_DIR}/${CONFIG_KERNEL_BIN_NAME}.uf2 + ${PROJECT_BINARY_DIR}/${CONFIG_KERNEL_BIN_NAME}.bin +) diff --git a/app/boards/arm/nrf52840_m2/Kconfig b/app/boards/arm/nrf52840_m2/Kconfig new file mode 100644 index 0000000..faff492 --- /dev/null +++ b/app/boards/arm/nrf52840_m2/Kconfig @@ -0,0 +1,10 @@ +# +# Copyright (c) 2020 The ZMK Contributors +# SPDX-License-Identifier: MIT +# + +config BOARD_ENABLE_DCDC + bool "Enable DCDC mode" + select SOC_DCDC_NRF52X + default y + depends on BOARD_NRF52840_M2 diff --git a/app/boards/arm/nrf52840_m2/Kconfig.board b/app/boards/arm/nrf52840_m2/Kconfig.board new file mode 100644 index 0000000..6ade68c --- /dev/null +++ b/app/boards/arm/nrf52840_m2/Kconfig.board @@ -0,0 +1,10 @@ +# Maker Diary nrf52840 M.2 board configuration +# +# Copyright (c) 2020 The ZMK Contributors +# SPDX-License-Identifier: MIT +# + +config BOARD_NRF52840_M2 + bool "nrf52480_m2" + depends on SOC_NRF52840_QIAA + diff --git a/app/boards/arm/nrf52840_m2/Kconfig.defconfig b/app/boards/arm/nrf52840_m2/Kconfig.defconfig new file mode 100644 index 0000000..98fcd08 --- /dev/null +++ b/app/boards/arm/nrf52840_m2/Kconfig.defconfig @@ -0,0 +1,30 @@ +# +# Copyright (c) 2020 The ZMK Contributors +# SPDX-License-Identifier: MIT +# + +if BOARD_NRF52840_M2 + +config BOARD + default "nrf52480_m2" + +if USB + +config USB_NRFX + default y + +config USB_DEVICE_STACK + default y + +endif # USB + +config BT_CTLR + default BT + +config ZMK_BLE + default y + +config ZMK_USB + default y + +endif # BOARD_NRF52840_M2 diff --git a/app/boards/arm/nrf52840_m2/board.cmake b/app/boards/arm/nrf52840_m2/board.cmake new file mode 100644 index 0000000..55b44e2 --- /dev/null +++ b/app/boards/arm/nrf52840_m2/board.cmake @@ -0,0 +1,9 @@ +# +# Copyright (c) 2020 The ZMK Contributors +# SPDX-License-Identifier: MIT +# + +board_runner_args(nrfjprog "--nrf-family=NRF52" "--softreset") + +include(${ZEPHYR_BASE}/boards/common/nrfjprog.board.cmake) +include(${ZEPHYR_BASE}/boards/common/blackmagicprobe.board.cmake) diff --git a/app/boards/arm/nrf52840_m2/nrf52840_m2.dts b/app/boards/arm/nrf52840_m2/nrf52840_m2.dts new file mode 100644 index 0000000..fb5b0ff --- /dev/null +++ b/app/boards/arm/nrf52840_m2/nrf52840_m2.dts @@ -0,0 +1,97 @@ +/* + * Copyright (c) 2020 The ZMK Contributors + * SPDX-License-Identifier: MIT + */ + +/dts-v1/; +#include <nordic/nrf52840_qiaa.dtsi> + +/ { + model = "Makerdiary nRF52840 M.2 module"; + compatible = "makerdiary,nrf52840_m2"; + + chosen { + zephyr,code-partition = &code_partition; + //zephyr,console = &uart0; + //zephyr,bt-mon-uart = &uart0; + //zephyr,bt-c2h-uart = &uart0; + zephyr,sram = &sram0; + zephyr,flash = &flash0; + }; + + leds { + compatible = "gpio-leds"; + red_led: led_0 { + gpios = <&gpio0 30 GPIO_ACTIVE_HIGH>; + label = "Red LED"; + }; + green_led: led_1 { + gpios = <&gpio0 29 GPIO_ACTIVE_HIGH>; + label = "Green LED"; + }; + blue_led: led_2 { + gpios = <&gpio0 31 GPIO_ACTIVE_HIGH>; + label = "Blue LED"; + }; + }; + +}; + +&gpio0 { + status = "okay"; +}; + +&gpio1 { + status = "okay"; +}; + +&uart0 { + compatible = "nordic,nrf-uart"; + status = "okay"; + current-speed = <115200>; + tx-pin = <16>; + rx-pin = <15>; + rts-pin = <14>; + cts-pin = <13>; +}; + +&usbd { + compatible = "nordic,nrf-usbd"; + status = "okay"; +}; + + +&flash0 { + /* + * For more information, see: + * http://docs.zephyrproject.org/latest/devices/dts/flash_partitions.html + */ + partitions { + compatible = "fixed-partitions"; + #address-cells = <1>; + #size-cells = <1>; + + boot_partition: partition@0 { + label = "adafruit_boot"; + reg = <0x000000000 0x0000C000>; + }; + code_partition: partition@26000 { + label = "code_partition"; + reg = <0x00026000 0x000d2000>; + }; + + /* + * The flash starting at 0x000f8000 and ending at + * 0x000fffff is reserved for use by the application. + */ + + /* + * Storage partition will be used by FCB/LittleFS/NVS + * if enabled. + */ + storage_partition: partition@f8000 { + label = "storage"; + reg = <0x000f8000 0x00008000>; + }; + }; +}; diff --git a/app/boards/arm/nrf52840_m2/nrf52840_m2.yaml b/app/boards/arm/nrf52840_m2/nrf52840_m2.yaml new file mode 100644 index 0000000..0a999bb --- /dev/null +++ b/app/boards/arm/nrf52840_m2/nrf52840_m2.yaml @@ -0,0 +1,15 @@ +identifier: nrf52840_m2 +name: Makerdiary nRF52840 M.2 module +type: mcu +arch: arm +toolchain: + - zephyr + - gnuarmemb + - xtools +supported: + - adc + - usb_device + - ble + - ieee802154 + - pwm + - watchdog diff --git a/app/boards/arm/nrf52840_m2/nrf52840_m2_defconfig b/app/boards/arm/nrf52840_m2/nrf52840_m2_defconfig new file mode 100644 index 0000000..e74438b --- /dev/null +++ b/app/boards/arm/nrf52840_m2/nrf52840_m2_defconfig @@ -0,0 +1,23 @@ +# +# Copyright (c) 2020 The ZMK Contributors +# SPDX-License-Identifier: MIT +# + +CONFIG_SOC_SERIES_NRF52X=y +CONFIG_SOC_NRF52840_QIAA=y +CONFIG_BOARD_NRF52840_M2=y + +# Enable MPU +CONFIG_ARM_MPU=y + +# enable GPIO +CONFIG_GPIO=y + +CONFIG_USE_DT_CODE_PARTITION=y + +CONFIG_MPU_ALLOW_FLASH_WRITE=y +CONFIG_NVS=y +CONFIG_SETTINGS_NVS=y +CONFIG_FLASH=y +CONFIG_FLASH_PAGE_LAYOUT=y +CONFIG_FLASH_MAP=y diff --git a/app/boards/arm/nrfmicro/Kconfig.board b/app/boards/arm/nrfmicro/Kconfig.board index 177373a..36b2d55 100644 --- a/app/boards/arm/nrfmicro/Kconfig.board +++ b/app/boards/arm/nrfmicro/Kconfig.board @@ -1,6 +1,6 @@ # nrfmicro board configuration -# Copyright (c) 2020 Okke Formsma, joric +# Copyright (c) 2020 The ZMK Contributors # SPDX-License-Identifier: MIT config BOARD_NRFMICRO_11 diff --git a/app/boards/arm/nrfmicro/Kconfig.defconfig b/app/boards/arm/nrfmicro/Kconfig.defconfig index 9cce5a9..7957b4a 100644 --- a/app/boards/arm/nrfmicro/Kconfig.defconfig +++ b/app/boards/arm/nrfmicro/Kconfig.defconfig @@ -1,6 +1,6 @@ # Electronut Labs Papyr board configuration -# Copyright (c) 2020 Okke Formsma, joric +# Copyright (c) 2020 The ZMK Contributors # SPDX-License-Identifier: MIT if BOARD_NRFMICRO_11 || BOARD_NRFMICRO_11_FLIPPED || BOARD_NRFMICRO_13 diff --git a/app/boards/arm/nrfmicro/arduino_pro_micro_pins.dtsi b/app/boards/arm/nrfmicro/arduino_pro_micro_pins.dtsi index 6cde2b4..558391d 100644 --- a/app/boards/arm/nrfmicro/arduino_pro_micro_pins.dtsi +++ b/app/boards/arm/nrfmicro/arduino_pro_micro_pins.dtsi @@ -1,5 +1,5 @@ /* - * Copyright (c) 2020 Okke Formsma, joric + * Copyright (c) 2020 The ZMK Contributors * * SPDX-License-Identifier: MIT */ diff --git a/app/boards/arm/nrfmicro/arduino_pro_micro_pins_flipped.dtsi b/app/boards/arm/nrfmicro/arduino_pro_micro_pins_flipped.dtsi index 9bad7f4..68ca266 100644 --- a/app/boards/arm/nrfmicro/arduino_pro_micro_pins_flipped.dtsi +++ b/app/boards/arm/nrfmicro/arduino_pro_micro_pins_flipped.dtsi @@ -1,5 +1,5 @@ /* - * Copyright (c) 2020 Okke Formsma, joric + * Copyright (c) 2020 The ZMK Contributors * * SPDX-License-Identifier: MIT */ diff --git a/app/boards/arm/nrfmicro/nrfmicro_11.dts b/app/boards/arm/nrfmicro/nrfmicro_11.dts index dc07ac8..87c650e 100644 --- a/app/boards/arm/nrfmicro/nrfmicro_11.dts +++ b/app/boards/arm/nrfmicro/nrfmicro_11.dts @@ -1,5 +1,5 @@ /* - * Copyright (c) 2020 Okke Formsma, joric + * Copyright (c) 2020 The ZMK Contributors * * SPDX-License-Identifier: MIT */ @@ -26,6 +26,11 @@ }; }; + ext-power { + compatible = "zmk,ext-power-generic"; + label = "EXT_POWER"; + control-gpios = <&gpio1 9 GPIO_ACTIVE_HIGH>; + }; }; &gpio0 { diff --git a/app/boards/arm/nrfmicro/nrfmicro_11_flipped.dts b/app/boards/arm/nrfmicro/nrfmicro_11_flipped.dts index 5bf493f..ea15b81 100644 --- a/app/boards/arm/nrfmicro/nrfmicro_11_flipped.dts +++ b/app/boards/arm/nrfmicro/nrfmicro_11_flipped.dts @@ -1,5 +1,5 @@ /* - * Copyright (c) 2020 Okke Formsma, joric + * Copyright (c) 2020 The ZMK Contributors * * SPDX-License-Identifier: MIT */ @@ -26,6 +26,11 @@ }; }; + ext-power { + compatible = "zmk,ext-power-generic"; + label = "EXT_POWER"; + control-gpios = <&gpio1 9 GPIO_ACTIVE_HIGH>; + }; }; &gpio0 { diff --git a/app/boards/arm/nrfmicro/nrfmicro_13.dts b/app/boards/arm/nrfmicro/nrfmicro_13.dts index dc07ac8..ef43946 100644 --- a/app/boards/arm/nrfmicro/nrfmicro_13.dts +++ b/app/boards/arm/nrfmicro/nrfmicro_13.dts @@ -1,5 +1,5 @@ /* - * Copyright (c) 2020 Okke Formsma, joric + * Copyright (c) 2020 The ZMK Contributors * * SPDX-License-Identifier: MIT */ @@ -26,6 +26,11 @@ }; }; + ext-power { + compatible = "zmk,ext-power-generic"; + label = "EXT_POWER"; + control-gpios = <&gpio1 9 GPIO_ACTIVE_LOW>; + }; }; &gpio0 { diff --git a/app/boards/arm/nrfmicro/pinmux.c b/app/boards/arm/nrfmicro/pinmux.c index 3792b59..30117d0 100644 --- a/app/boards/arm/nrfmicro/pinmux.c +++ b/app/boards/arm/nrfmicro/pinmux.c @@ -1,5 +1,5 @@ /* - * Copyright (c) 2020 Okke Formsma, joric + * Copyright (c) 2020 The ZMK Contributors * * SPDX-License-Identifier: MIT */ @@ -11,31 +11,19 @@ #include <sys/sys_io.h> #include <devicetree.h> -static int pinmux_nrfmicro_init(struct device *port) -{ - ARG_UNUSED(port); - - struct device *p1 = device_get_binding("GPIO_1"); +static int pinmux_nrfmicro_init(struct device *port) { + ARG_UNUSED(port); #if CONFIG_BOARD_NRFMICRO_13 - struct device *p0 = device_get_binding("GPIO_0"); - // enable EXT_VCC (use 0 for nRFMicro 1.3, use 1 for nRFMicro 1.1) - gpio_pin_configure(p1, 9, GPIO_OUTPUT); - gpio_pin_set(p1, 9, 0); - + struct device *p0 = device_get_binding("GPIO_0"); #if CONFIG_BOARD_NRFMICRO_CHARGER - gpio_pin_configure(p0, 5, GPIO_OUTPUT); - gpio_pin_set(p0, 5, 0); + gpio_pin_configure(p0, 5, GPIO_OUTPUT); + gpio_pin_set(p0, 5, 0); #else - gpio_pin_configure(p0, 5, GPIO_INPUT); + gpio_pin_configure(p0, 5, GPIO_INPUT); #endif - -#else - // enable EXT_VCC (use 0 for nRFMicro 1.3, use 1 for nRFMicro 1.1) - gpio_pin_configure(p1, 9, GPIO_OUTPUT); - gpio_pin_set(p1, 9, 1); #endif - return 0; + return 0; } SYS_INIT(pinmux_nrfmicro_init, APPLICATION, CONFIG_APPLICATION_INIT_PRIORITY); diff --git a/app/boards/arm/planck/Kconfig.board b/app/boards/arm/planck/Kconfig.board index 0952448..fe15e1a 100644 --- a/app/boards/arm/planck/Kconfig.board +++ b/app/boards/arm/planck/Kconfig.board @@ -1,6 +1,6 @@ # Planck V6 board configuration -# Copyright (c) 2020 Peter Johanson +# Copyright (c) 2020 The ZMK Contributors # SPDX-License-Identifier: MIT config BOARD_PLANCK_REV6 diff --git a/app/boards/arm/planck/Kconfig.defconfig b/app/boards/arm/planck/Kconfig.defconfig index 93118d4..913c1c1 100644 --- a/app/boards/arm/planck/Kconfig.defconfig +++ b/app/boards/arm/planck/Kconfig.defconfig @@ -1,6 +1,6 @@ # Planck keyboard configuration -# Copyright (c) 2020 Peter Johanson +# Copyright (c) 2020 The ZMK Contributors # SPDX-License-Identifier: MIT if BOARD_PLANCK_REV6 @@ -11,4 +11,7 @@ config ZMK_KEYBOARD_NAME config ZMK_USB default y +config ZMK_KSCAN_MATRIX_POLLING + default y + endif # BOARD_PLANCK_REV6 diff --git a/app/boards/arm/planck/board.cmake b/app/boards/arm/planck/board.cmake index 4843c41..772796d 100644 --- a/app/boards/arm/planck/board.cmake +++ b/app/boards/arm/planck/board.cmake @@ -1,6 +1,7 @@ # SPDX-License-Identifier: MIT +board_runner_args(dfu-util "--pid=0483:df11" "--alt=0" "--dfuse") board_runner_args(jlink "--device=STM32F303VC" "--speed=4000") -include(${ZEPHYR_BASE}/boards/common/openocd.board.cmake) +include(${ZEPHYR_BASE}/boards/common/dfu-util.board.cmake) include(${ZEPHYR_BASE}/boards/common/jlink.board.cmake) diff --git a/app/boards/arm/planck/pinmux.c b/app/boards/arm/planck/pinmux.c index 5df5c90..61cc16a 100644 --- a/app/boards/arm/planck/pinmux.c +++ b/app/boards/arm/planck/pinmux.c @@ -15,55 +15,53 @@ /* pin assignments for STM32F3DISCOVERY board */ static const struct pin_config pinconf[] = { #ifdef CONFIG_UART_1 - {STM32_PIN_PC4, STM32F3_PINMUX_FUNC_PC4_USART1_TX}, - {STM32_PIN_PC5, STM32F3_PINMUX_FUNC_PC5_USART1_RX}, + {STM32_PIN_PC4, STM32F3_PINMUX_FUNC_PC4_USART1_TX}, + {STM32_PIN_PC5, STM32F3_PINMUX_FUNC_PC5_USART1_RX}, #endif /* CONFIG_UART_1 */ #ifdef CONFIG_UART_2 - {STM32_PIN_PA2, STM32F3_PINMUX_FUNC_PA2_USART2_TX}, - {STM32_PIN_PA3, STM32F3_PINMUX_FUNC_PA3_USART2_RX}, + {STM32_PIN_PA2, STM32F3_PINMUX_FUNC_PA2_USART2_TX}, + {STM32_PIN_PA3, STM32F3_PINMUX_FUNC_PA3_USART2_RX}, #endif /* CONFIG_UART_2 */ #ifdef CONFIG_I2C_1 - {STM32_PIN_PB6, STM32F3_PINMUX_FUNC_PB6_I2C1_SCL}, - {STM32_PIN_PB7, STM32F3_PINMUX_FUNC_PB7_I2C1_SDA}, + {STM32_PIN_PB6, STM32F3_PINMUX_FUNC_PB6_I2C1_SCL}, + {STM32_PIN_PB7, STM32F3_PINMUX_FUNC_PB7_I2C1_SDA}, #endif /* CONFIG_I2C_1 */ #ifdef CONFIG_I2C_2 - {STM32_PIN_PA9, STM32F3_PINMUX_FUNC_PA9_I2C2_SCL}, - {STM32_PIN_PA10, STM32F3_PINMUX_FUNC_PA10_I2C2_SDA}, + {STM32_PIN_PA9, STM32F3_PINMUX_FUNC_PA9_I2C2_SCL}, + {STM32_PIN_PA10, STM32F3_PINMUX_FUNC_PA10_I2C2_SDA}, #endif /* CONFIG_I2C_2 */ #ifdef CONFIG_SPI_1 #ifdef CONFIG_SPI_STM32_USE_HW_SS - {STM32_PIN_PA4, STM32F3_PINMUX_FUNC_PA4_SPI1_NSS}, + {STM32_PIN_PA4, STM32F3_PINMUX_FUNC_PA4_SPI1_NSS}, #endif /* CONFIG_SPI_STM32_USE_HW_SS */ - {STM32_PIN_PA5, STM32F3_PINMUX_FUNC_PA5_SPI1_SCK}, - {STM32_PIN_PA6, STM32F3_PINMUX_FUNC_PA6_SPI1_MISO}, - {STM32_PIN_PA7, STM32F3_PINMUX_FUNC_PA7_SPI1_MOSI}, + {STM32_PIN_PA5, STM32F3_PINMUX_FUNC_PA5_SPI1_SCK}, + {STM32_PIN_PA6, STM32F3_PINMUX_FUNC_PA6_SPI1_MISO}, + {STM32_PIN_PA7, STM32F3_PINMUX_FUNC_PA7_SPI1_MOSI}, #endif /* CONFIG_SPI_1 */ #ifdef CONFIG_SPI_2 #ifdef CONFIG_SPI_STM32_USE_HW_SS - {STM32_PIN_PB12, STM32F3_PINMUX_FUNC_PB12_SPI2_NSS}, + {STM32_PIN_PB12, STM32F3_PINMUX_FUNC_PB12_SPI2_NSS}, #endif /* CONFIG_SPI_STM32_USE_HW_SS */ - {STM32_PIN_PB13, STM32F3_PINMUX_FUNC_PB13_SPI2_SCK}, - {STM32_PIN_PB14, STM32F3_PINMUX_FUNC_PB14_SPI2_MISO}, - {STM32_PIN_PB15, STM32F3_PINMUX_FUNC_PB15_SPI2_MOSI}, + {STM32_PIN_PB13, STM32F3_PINMUX_FUNC_PB13_SPI2_SCK}, + {STM32_PIN_PB14, STM32F3_PINMUX_FUNC_PB14_SPI2_MISO}, + {STM32_PIN_PB15, STM32F3_PINMUX_FUNC_PB15_SPI2_MOSI}, #endif /* CONFIG_SPI_2 */ #ifdef CONFIG_USB_DC_STM32 - {STM32_PIN_PA11, STM32F3_PINMUX_FUNC_PA11_USB_DM}, - {STM32_PIN_PA12, STM32F3_PINMUX_FUNC_PA12_USB_DP}, + {STM32_PIN_PA11, STM32F3_PINMUX_FUNC_PA11_USB_DM}, + {STM32_PIN_PA12, STM32F3_PINMUX_FUNC_PA12_USB_DP}, #endif /* CONFIG_USB_DC_STM32 */ #ifdef CONFIG_CAN_1 - {STM32_PIN_PD0, STM32F3_PINMUX_FUNC_PD0_CAN1_RX}, - {STM32_PIN_PD1, STM32F3_PINMUX_FUNC_PD1_CAN1_TX}, + {STM32_PIN_PD0, STM32F3_PINMUX_FUNC_PD0_CAN1_RX}, + {STM32_PIN_PD1, STM32F3_PINMUX_FUNC_PD1_CAN1_TX}, #endif /* CONFIG_CAN_1 */ }; -static int pinmux_stm32_init(struct device *port) -{ - ARG_UNUSED(port); +static int pinmux_stm32_init(struct device *port) { + ARG_UNUSED(port); - stm32_setup_pins(pinconf, ARRAY_SIZE(pinconf)); + stm32_setup_pins(pinconf, ARRAY_SIZE(pinconf)); - return 0; + return 0; } -SYS_INIT(pinmux_stm32_init, PRE_KERNEL_1, - CONFIG_PINMUX_STM32_DEVICE_INITIALIZATION_PRIORITY); +SYS_INIT(pinmux_stm32_init, PRE_KERNEL_1, CONFIG_PINMUX_STM32_DEVICE_INITIALIZATION_PRIORITY); diff --git a/app/boards/arm/planck/planck_rev6_defconfig b/app/boards/arm/planck/planck_rev6_defconfig index a440ff1..e34ce00 100644 --- a/app/boards/arm/planck/planck_rev6_defconfig +++ b/app/boards/arm/planck/planck_rev6_defconfig @@ -20,9 +20,6 @@ CONFIG_CLOCK_STM32_HSE_CLOCK=8000000 CONFIG_CLOCK_STM32_SYSCLK_SRC_PLL=y # use HSE as PLL input CONFIG_CLOCK_STM32_PLL_SRC_HSE=y -# however, the board does not have an external oscillator, so just use -# the 8MHz clock signal coming from integrated STLink -CONFIG_CLOCK_STM32_HSE_BYPASS=y # produce 72MHz clock at PLL output CONFIG_CLOCK_STM32_PLL_PREDIV=1 CONFIG_CLOCK_STM32_PLL_MULTIPLIER=9 diff --git a/app/boards/arm/proton_c/pinmux.c b/app/boards/arm/proton_c/pinmux.c index a6aaae0..9ffabd7 100644 --- a/app/boards/arm/proton_c/pinmux.c +++ b/app/boards/arm/proton_c/pinmux.c @@ -15,55 +15,53 @@ /* pin assignments for STM32F3DISCOVERY board */ static const struct pin_config pinconf[] = { #if DT_NODE_HAS_STATUS(DT_NODELABEL(usart1), okay) && CONFIG_SERIAL - {STM32_PIN_PC4, STM32F3_PINMUX_FUNC_PC4_USART1_TX}, - {STM32_PIN_PC5, STM32F3_PINMUX_FUNC_PC5_USART1_RX}, + {STM32_PIN_PC4, STM32F3_PINMUX_FUNC_PC4_USART1_TX}, + {STM32_PIN_PC5, STM32F3_PINMUX_FUNC_PC5_USART1_RX}, #endif #if DT_NODE_HAS_STATUS(DT_NODELABEL(usart2), okay) && CONFIG_SERIAL - {STM32_PIN_PA2, STM32F3_PINMUX_FUNC_PA2_USART2_TX}, - {STM32_PIN_PA3, STM32F3_PINMUX_FUNC_PA3_USART2_RX}, + {STM32_PIN_PA2, STM32F3_PINMUX_FUNC_PA2_USART2_TX}, + {STM32_PIN_PA3, STM32F3_PINMUX_FUNC_PA3_USART2_RX}, #endif #if DT_NODE_HAS_STATUS(DT_NODELABEL(i2c1), okay) && CONFIG_I2C - {STM32_PIN_PB6, STM32F3_PINMUX_FUNC_PB6_I2C1_SCL}, - {STM32_PIN_PB7, STM32F3_PINMUX_FUNC_PB7_I2C1_SDA}, + {STM32_PIN_PB6, STM32F3_PINMUX_FUNC_PB6_I2C1_SCL}, + {STM32_PIN_PB7, STM32F3_PINMUX_FUNC_PB7_I2C1_SDA}, #endif #if DT_NODE_HAS_STATUS(DT_NODELABEL(i2c2), okay) && CONFIG_I2C - {STM32_PIN_PA9, STM32F3_PINMUX_FUNC_PA9_I2C2_SCL}, - {STM32_PIN_PA10, STM32F3_PINMUX_FUNC_PA10_I2C2_SDA}, + {STM32_PIN_PA9, STM32F3_PINMUX_FUNC_PA9_I2C2_SCL}, + {STM32_PIN_PA10, STM32F3_PINMUX_FUNC_PA10_I2C2_SDA}, #endif #if DT_NODE_HAS_STATUS(DT_NODELABEL(spi1), okay) && CONFIG_SPI #ifdef CONFIG_SPI_STM32_USE_HW_SS - {STM32_PIN_PA4, STM32F3_PINMUX_FUNC_PA4_SPI1_NSS}, + {STM32_PIN_PA4, STM32F3_PINMUX_FUNC_PA4_SPI1_NSS}, #endif /* CONFIG_SPI_STM32_USE_HW_SS */ - {STM32_PIN_PA5, STM32F3_PINMUX_FUNC_PA5_SPI1_SCK}, - {STM32_PIN_PA6, STM32F3_PINMUX_FUNC_PA6_SPI1_MISO}, - {STM32_PIN_PA7, STM32F3_PINMUX_FUNC_PA7_SPI1_MOSI}, + {STM32_PIN_PA5, STM32F3_PINMUX_FUNC_PA5_SPI1_SCK}, + {STM32_PIN_PA6, STM32F3_PINMUX_FUNC_PA6_SPI1_MISO}, + {STM32_PIN_PA7, STM32F3_PINMUX_FUNC_PA7_SPI1_MOSI}, #endif #if DT_NODE_HAS_STATUS(DT_NODELABEL(spi2), okay) && CONFIG_SPI #ifdef CONFIG_SPI_STM32_USE_HW_SS - {STM32_PIN_PB12, STM32F3_PINMUX_FUNC_PB12_SPI2_NSS}, + {STM32_PIN_PB12, STM32F3_PINMUX_FUNC_PB12_SPI2_NSS}, #endif /* CONFIG_SPI_STM32_USE_HW_SS */ - {STM32_PIN_PB13, STM32F3_PINMUX_FUNC_PB13_SPI2_SCK}, - {STM32_PIN_PB14, STM32F3_PINMUX_FUNC_PB14_SPI2_MISO}, - {STM32_PIN_PB15, STM32F3_PINMUX_FUNC_PB15_SPI2_MOSI}, + {STM32_PIN_PB13, STM32F3_PINMUX_FUNC_PB13_SPI2_SCK}, + {STM32_PIN_PB14, STM32F3_PINMUX_FUNC_PB14_SPI2_MISO}, + {STM32_PIN_PB15, STM32F3_PINMUX_FUNC_PB15_SPI2_MOSI}, #endif #ifdef CONFIG_USB_DC_STM32 - {STM32_PIN_PA11, STM32F3_PINMUX_FUNC_PA11_USB_DM}, - {STM32_PIN_PA12, STM32F3_PINMUX_FUNC_PA12_USB_DP}, -#endif /* CONFIG_USB_DC_STM32 */ + {STM32_PIN_PA11, STM32F3_PINMUX_FUNC_PA11_USB_DM}, + {STM32_PIN_PA12, STM32F3_PINMUX_FUNC_PA12_USB_DP}, +#endif /* CONFIG_USB_DC_STM32 */ #if DT_NODE_HAS_STATUS(DT_NODELABEL(can1), okay) && CONFIG_CAN - {STM32_PIN_PD0, STM32F3_PINMUX_FUNC_PD0_CAN1_RX}, - {STM32_PIN_PD1, STM32F3_PINMUX_FUNC_PD1_CAN1_TX}, + {STM32_PIN_PD0, STM32F3_PINMUX_FUNC_PD0_CAN1_RX}, + {STM32_PIN_PD1, STM32F3_PINMUX_FUNC_PD1_CAN1_TX}, #endif }; -static int pinmux_stm32_init(struct device *port) -{ - ARG_UNUSED(port); +static int pinmux_stm32_init(struct device *port) { + ARG_UNUSED(port); - stm32_setup_pins(pinconf, ARRAY_SIZE(pinconf)); + stm32_setup_pins(pinconf, ARRAY_SIZE(pinconf)); - return 0; + return 0; } -SYS_INIT(pinmux_stm32_init, PRE_KERNEL_1, - CONFIG_PINMUX_STM32_DEVICE_INITIALIZATION_PRIORITY); +SYS_INIT(pinmux_stm32_init, PRE_KERNEL_1, CONFIG_PINMUX_STM32_DEVICE_INITIALIZATION_PRIORITY); diff --git a/app/boards/shields/clueboard_california/Kconfig.defconfig b/app/boards/shields/clueboard_california/Kconfig.defconfig index 2408f9f..e101ea7 100644 --- a/app/boards/shields/clueboard_california/Kconfig.defconfig +++ b/app/boards/shields/clueboard_california/Kconfig.defconfig @@ -8,7 +8,7 @@ config ZMK_KEYBOARD_NAME # across A & B controllers, and STM32F303CCT6 can't enable # interrutps for multiple controllers for the same "line" # for the external interrupts. -config ZMK_KSCAN_GPIO_POLLING +config ZMK_KSCAN_DIRECT_POLLING default y endif diff --git a/app/boards/shields/clueboard_california/clueboard_california.keymap b/app/boards/shields/clueboard_california/clueboard_california.keymap index 7a84b11..44a0b07 100644 --- a/app/boards/shields/clueboard_california/clueboard_california.keymap +++ b/app/boards/shields/clueboard_california/clueboard_california.keymap @@ -1,3 +1,9 @@ +/* + * Copyright (c) 2020 The ZMK Contributors + * + * SPDX-License-Identifier: MIT + */ + #include <behaviors.dtsi> #include <dt-bindings/zmk/keys.h> diff --git a/app/boards/shields/corne/corne.keymap b/app/boards/shields/corne/corne.keymap index c0bbb4a..64d4a00 100644 --- a/app/boards/shields/corne/corne.keymap +++ b/app/boards/shields/corne/corne.keymap @@ -1,5 +1,12 @@ +/* + * Copyright (c) 2020 The ZMK Contributors + * + * SPDX-License-Identifier: MIT + */ + #include <behaviors.dtsi> #include <dt-bindings/zmk/keys.h> +#include <dt-bindings/zmk/bt.h> / { keymap { @@ -21,14 +28,14 @@ lower_layer { // ----------------------------------------------------------------------------------------- // | ESC | 1 | 2 | 3 | 4 | 5 | | 6 | 7 | 8 | 9 | 0 | BKSP | -// | CTRL | | | | | | | LFT | DWN | UP | RGT | | | +// | BTCLR| BT1 | BT2 | BT3 | BT4 | BT5 | | LFT | DWN | UP | RGT | | | // | SHFT | | | | | | | | | | | | | // | GUI | | SPC | | ENT | | ALT | bindings = < - &kp ESC &kp NUM_1 &kp NUM_2 &kp NUM_3 &kp NUM_4 &kp NUM_5 &kp NUM_6 &kp NUM_7 &kp NUM_8 &kp NUM_9 &kp NUM_0 &kp BKSP - &kp LCTL &trans &trans &trans &trans &trans &kp LARW &kp DARW &kp UARW &kp RARW &trans &trans - &kp LSFT &trans &trans &trans &trans &trans &trans &trans &trans &trans &trans &trans - &kp LGUI &trans &kp SPC &kp RET &trans &kp RALT + &kp ESC &kp NUM_1 &kp NUM_2 &kp NUM_3 &kp NUM_4 &kp NUM_5 &kp NUM_6 &kp NUM_7 &kp NUM_8 &kp NUM_9 &kp NUM_0 &kp BKSP + &bt BT_CLR &bt BT_SEL 0 &bt BT_SEL 1 &bt BT_SEL 2 &bt BT_SEL 3 &bt BT_SEL 4 &kp LARW &kp DARW &kp UARW &kp RARW &trans &trans + &kp LSFT &trans &trans &trans &trans &trans &trans &trans &trans &trans &trans &trans + &kp LGUI &trans &kp SPC &kp RET &trans &kp RALT >; }; diff --git a/app/boards/shields/iris/iris.keymap b/app/boards/shields/iris/iris.keymap index 46b0817..8b06b1b 100644 --- a/app/boards/shields/iris/iris.keymap +++ b/app/boards/shields/iris/iris.keymap @@ -1,8 +1,12 @@ -# Copyright (c) 2020 Pete Johanson, Kurtis Lew -# SPDX-License-Identifier: MIT +/* + * Copyright (c) 2020 The ZMK Contributors + * + * SPDX-License-Identifier: MIT + */ #include <behaviors.dtsi> #include <dt-bindings/zmk/keys.h> +#include <dt-bindings/zmk/bt.h> / { keymap { @@ -26,17 +30,17 @@ lower_layer { // ------------------------------------------------------------------------------------------------------------ -// | | | | | | | | | | | | | | +// | BTCLR | BT1 | BT2 | BT3 | BT4 | BT5 | | | | | | | | // | F1 | F2 | F3 | F4 | F5 | F6 | | F7 | F8 | F9 | F10 | F11 | F12 | // | ` | ! | @ | # | $ | % | | ^ | & | * | ( | ) | ~ | // | | | | | | | | | | | _ | + | { | } | "|" | // | | | | | | | | bindings = < -&trans &trans &trans &trans &trans &trans &trans &trans &trans &trans &trans &trans -&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 GRAV &kp BANG &kp ATSN &kp HASH &kp CURU &kp PRCT &kp CRRT &kp AMPS &kp KMLT &kp LPRN &kp RPRN &kp TILD -&trans &trans &trans &trans &trans &trans &trans &trans &trans &kp MINUS &kp KPLS &kp LCUR &kp RCUR &kp PIPE - &trans &trans &trans &trans &trans &trans +&bt BT_CLR &bt BT_SEL 0 &bt BT_SEL 1 &bt BT_SEL 2 &bt BT_SEL 3 &bt BT_SEL 4 &trans &trans &trans &trans &trans &trans +&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 GRAV &kp BANG &kp ATSN &kp HASH &kp CURU &kp PRCT &kp CRRT &kp AMPS &kp KMLT &kp LPRN &kp RPRN &kp TILD +&trans &trans &trans &trans &trans &trans &trans &trans &trans &kp MINUS &kp KPLS &kp LCUR &kp RCUR &kp PIPE + &trans &trans &trans &trans &trans &trans >; }; diff --git a/app/boards/shields/iris/iris_right.overlay b/app/boards/shields/iris/iris_right.overlay index 3f3dcb8..2d5dce8 100644 --- a/app/boards/shields/iris/iris_right.overlay +++ b/app/boards/shields/iris/iris_right.overlay @@ -12,12 +12,12 @@ &kscan0 { col-gpios - = <&pro_micro_a 1 GPIO_ACTIVE_HIGH> - , <&pro_micro_a 0 GPIO_ACTIVE_HIGH> - , <&pro_micro_d 15 GPIO_ACTIVE_HIGH> - , <&pro_micro_d 14 GPIO_ACTIVE_HIGH> + = <&pro_micro_d 10 GPIO_ACTIVE_HIGH> , <&pro_micro_d 16 GPIO_ACTIVE_HIGH> - , <&pro_micro_d 10 GPIO_ACTIVE_HIGH> + , <&pro_micro_d 14 GPIO_ACTIVE_HIGH> + , <&pro_micro_d 15 GPIO_ACTIVE_HIGH> + , <&pro_micro_a 0 GPIO_ACTIVE_HIGH> + , <&pro_micro_a 1 GPIO_ACTIVE_HIGH> ; }; diff --git a/app/boards/shields/kyria/kyria.keymap b/app/boards/shields/kyria/kyria.keymap index f689ef6..ac0d13f 100644 --- a/app/boards/shields/kyria/kyria.keymap +++ b/app/boards/shields/kyria/kyria.keymap @@ -1,3 +1,9 @@ +/* + * Copyright (c) 2020 The ZMK Contributors + * + * SPDX-License-Identifier: MIT + */ + #include <behaviors.dtsi> #include <dt-bindings/zmk/keys.h> diff --git a/app/boards/shields/lily58/lily58.keymap b/app/boards/shields/lily58/lily58.keymap index afd2694..d44b3fe 100644 --- a/app/boards/shields/lily58/lily58.keymap +++ b/app/boards/shields/lily58/lily58.keymap @@ -1,5 +1,13 @@ +/* + * Copyright (c) 2020 The ZMK Contributors + * + * SPDX-License-Identifier: MIT + */ + #include <behaviors.dtsi> #include <dt-bindings/zmk/keys.h> +#include <dt-bindings/zmk/bt.h> +#include <dt-bindings/zmk/ext_power.h> / { keymap { @@ -25,17 +33,17 @@ lower_layer { // ------------------------------------------------------------------------------------------------------------ -// | | | | | | | | | | | | | | +// | BTCLR | BT1 | BT2 | BT3 | BT4 | BT5 | | | | | | | | // | F1 | F2 | F3 | F4 | F5 | F6 | | F7 | F8 | F9 | F10 | F11 | F12 | // | ` | ! | @ | # | $ | % | | ^ | & | * | ( | ) | ~ | // | | | | | | | | | | | _ | + | { | } | "|" | // | | | | | | | | | | bindings = < -&trans &trans &trans &trans &trans &trans &trans &trans &trans &trans &trans &trans -&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 GRAV &kp BANG &kp ATSN &kp HASH &kp CURU &kp PRCT &kp CRRT &kp AMPS &kp KMLT &kp LPRN &kp RPRN &kp TILD -&trans &trans &trans &trans &trans &trans &trans &trans &trans &kp MINUS &kp KPLS &kp LCUR &kp RCUR &kp PIPE - &trans &trans &trans &trans &trans &trans &trans &trans +&bt BT_CLR &bt BT_SEL 0 &bt BT_SEL 1 &bt BT_SEL 2 &bt BT_SEL 3 &bt BT_SEL 4 &trans &trans &trans &trans &trans &trans +&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 GRAV &kp BANG &kp ATSN &kp HASH &kp CURU &kp PRCT &kp CRRT &kp AMPS &kp KMLT &kp LPRN &kp RPRN &kp TILD +&trans &ext_power EP_ON &ext_power EP_OFF &ext_power EP_TOG &trans &trans &trans &trans &trans &kp MINUS &kp KPLS &kp LCUR &kp RCUR &kp PIPE + &trans &trans &trans &trans &trans &trans &trans &trans >; sensor-bindings = <&inc_dec_cp M_VOLU M_VOLD>; diff --git a/app/boards/shields/m60/Kconfig.defconfig b/app/boards/shields/m60/Kconfig.defconfig new file mode 100644 index 0000000..e31d5a5 --- /dev/null +++ b/app/boards/shields/m60/Kconfig.defconfig @@ -0,0 +1,11 @@ +# +# Copyright (c) 2020 The ZMK Contributors +# SPDX-License-Identifier: MIT +# + +if SHIELD_M60 + +config ZMK_KEYBOARD_NAME + default "m60" + +endif diff --git a/app/boards/shields/m60/Kconfig.shield b/app/boards/shields/m60/Kconfig.shield new file mode 100644 index 0000000..47a28e2 --- /dev/null +++ b/app/boards/shields/m60/Kconfig.shield @@ -0,0 +1,7 @@ +# +# Copyright (c) 2020 The ZMK Contributors +# SPDX-License-Identifier: MIT +# + +config SHIELD_M60 + def_bool $(shields_list_contains,m60) diff --git a/app/tests/momentary-layer/early-key-release/pending b/app/boards/shields/m60/m60.conf index e69de29..e69de29 100644 --- a/app/tests/momentary-layer/early-key-release/pending +++ b/app/boards/shields/m60/m60.conf diff --git a/app/boards/shields/m60/m60.keymap b/app/boards/shields/m60/m60.keymap new file mode 100644 index 0000000..cbe356c --- /dev/null +++ b/app/boards/shields/m60/m60.keymap @@ -0,0 +1,42 @@ +/* + * Copyright (c) 2020 The ZMK Contributors + * + * SPDX-License-Identifier: MIT + */ + +#include <behaviors.dtsi> +#include <dt-bindings/zmk/keys.h> +#include <dt-bindings/zmk/bt.h> + +/ { + keymap0: keymap { + compatible = "zmk,keymap"; + + default_layer { +// ------------------------------------------------------------------------------------------ +// | ESC | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 0 | - | = | BKSP | +// | TAB | Q | W | E | R | T | Y | U | I | O | P | [ | ] | \ | +// | CAPS | A | S | D | F | G | H | J | K | L | ; | ' | ENTER | +// | SHIFT | Z | X | C | V | B | N | M | , | . | / | SHIFT | +// | CTL | WIN | ALT | SPACE | ALT | MO(1) | WIN | CTRL | +// ------------------------------------------------------------------------------------------ + bindings = < + &kp ESC &kp NUM_1 &kp NUM_2 &kp NUM_3 &kp NUM_4 &kp NUM_5 &kp NUM_6 &kp NUM_7 &kp NUM_8 &kp NUM_9 &kp NUM_0 &kp MINUS &kp EQL &kp BKSP + &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 CLCK &kp A &kp S &kp D &kp F &kp G &kp H &kp J &kp K &kp L &kp SCLN &kp QUOT &kp RET + &kp LSFT &kp Z &kp X &kp C &kp V &kp B &kp N &kp M &kp CMMA &kp DOT &kp FSLH &kp RSFT + &kp LCTL &kp LGUI &kp LALT &kp SPC &kp RALT &mo 1 &kp RGUI &kp RCTL + >; + }; + + fn_layer { + bindings = < +&kp GRAV &kp F1 &kp F2 &kp F3 &kp F4 &kp F5 &kp F6 &kp F7 &kp F8 &kp F9 &kp F10 &kp F11 &kp F12 &bootloader +&trans &bt BT_CLR &none &none &none &none &none &none &none &none &none &none &none &reset +&trans &bt BT_SEL 0 &bt BT_SEL 1 &bt BT_SEL 2 &bt BT_SEL 3 &bt BT_SEL 4 &kp LARW &kp DARW &kp UARW &kp RARW &none &none &trans +&trans &none &none &none &none &none &none &none &none &none &none &trans +&trans &trans &trans &trans &trans &trans &trans &trans + >; + }; + }; +}; diff --git a/app/boards/shields/m60/m60.overlay b/app/boards/shields/m60/m60.overlay new file mode 100644 index 0000000..babae2d --- /dev/null +++ b/app/boards/shields/m60/m60.overlay @@ -0,0 +1,66 @@ +/* + * Copyright (c) 2020 The ZMK Contributors + * + * SPDX-License-Identifier: MIT + */ + +#include <dt-bindings/zmk/matrix-transform.h> + +/ { + chosen { + zmk,kscan = &kscan0; + zmk,matrix_transform = &default_transform; + }; + + kscan0: kscan { + compatible = "zmk,kscan-gpio-matrix"; + label = "KSCAN"; + + diode-direction = "col2row"; + row-gpios + = <&gpio0 5 (GPIO_ACTIVE_HIGH | GPIO_PULL_DOWN)> + , <&gpio0 6 (GPIO_ACTIVE_HIGH | GPIO_PULL_DOWN)> + , <&gpio0 7 (GPIO_ACTIVE_HIGH | GPIO_PULL_DOWN)> + , <&gpio0 8 (GPIO_ACTIVE_HIGH | GPIO_PULL_DOWN)> + , <&gpio1 9 (GPIO_ACTIVE_HIGH | GPIO_PULL_DOWN)> + , <&gpio1 8 (GPIO_ACTIVE_HIGH | GPIO_PULL_DOWN)> + , <&gpio0 12 (GPIO_ACTIVE_HIGH | GPIO_PULL_DOWN)> + , <&gpio0 11 (GPIO_ACTIVE_HIGH | GPIO_PULL_DOWN)> + ; + col-gpios + = <&gpio0 19 GPIO_ACTIVE_HIGH> + , <&gpio0 20 GPIO_ACTIVE_HIGH> + , <&gpio0 21 GPIO_ACTIVE_HIGH> + , <&gpio0 22 GPIO_ACTIVE_HIGH> + , <&gpio0 23 GPIO_ACTIVE_HIGH> + , <&gpio0 24 GPIO_ACTIVE_HIGH> + , <&gpio0 25 GPIO_ACTIVE_HIGH> + , <&gpio0 26 GPIO_ACTIVE_HIGH> + ; + }; + + default_transform: keymap_transform_0 { + compatible = "zmk,matrix-transform"; + columns = <8>; + rows = <8>; +// | MX1 | MX2 | MX3 | MX4 | MX5 | MX6 | MX7 | MX8 | MX9 | MX10 | MX11 | MX12 | MX13 | MX14 | +// | MX15 | MX16 | MX17 | MX18 | MX19 | MX20 | MX21 | MX22 | MX23 | MX24 | MX25 | MX26 | MX27 | MX28 | +// | MX29 | MX30 | MX31 | MX32 | MX33 | MX34 | MX35 | MX36 | MX37 | MX38 | MX39 | MX40 | MX41 | +// | MX42 | MX43 | MX44 | MX45 | MX46 | MX47 | MX48 | MX49 | MX50 | MX51 | MX52 | MX53 | +// | MX54 | MX55 | MX56 | MX57 | MX58 | MX59 | MX60 | MX61 | + 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(1,0) RC(1,1) RC(1,2) RC(1,3) RC(1,4) RC(1,5) +RC(3,3) RC(3,2) RC(3,1) RC(3,0) RC(2,7) RC(2,6) RC(2,5) RC(2,4) RC(2,3) RC(2,2) RC(2,1) RC(2,0) RC(1,7) RC(1,6) +RC(3,4) RC(3,5) RC(3,6) RC(3,7) RC(4,0) RC(4,1) RC(4,2) RC(4,3) RC(4,4) RC(4,5) RC(4,6) RC(4,7) RC(5,0) +RC(6,4) RC(6,3) RC(6,2) RC(6,1) RC(6,0) RC(5,7) RC(5,6) RC(5,5) RC(5,4) RC(5,3) RC(5,2) RC(5,1) +RC(6,5) RC(6,6) RC(6,7) RC(7,0) RC(7,1) RC(7,2) RC(7,3) RC(7,4) + >; + }; + + bt_unpair_combo: bt_unpair_combo { + compatible = "zmk,bt-unpair-combo"; + key-positions = <0 53>; + }; + +}; + diff --git a/app/boards/shields/m60/readme.md b/app/boards/shields/m60/readme.md new file mode 100644 index 0000000..e801c78 --- /dev/null +++ b/app/boards/shields/m60/readme.md @@ -0,0 +1,14 @@ +# [Makerdiary M60](https://wiki.makerdiary.com/m60) + +A 60% ANSI keyboard designed and manufactured by Makerdiary. +http://makerdiary.com + +## Features + +- Per key RGB LED. +- Uses makerdiary M.2 nRF52840 module +- Matrix wiring + +## Hardware Notes + +https://wiki.makerdiary.com/m60/developer_guide/hardware/ diff --git a/app/boards/shields/microdox/Kconfig.defconfig b/app/boards/shields/microdox/Kconfig.defconfig new file mode 100644 index 0000000..4840ece --- /dev/null +++ b/app/boards/shields/microdox/Kconfig.defconfig @@ -0,0 +1,60 @@ +# Copyright (c) 2020 The ZMK Contributors +# SPDX-License-Identifier: MIT + +if SHIELD_MICRODOX_LEFT + +config ZMK_KEYBOARD_NAME + default "Microdox Left" + +endif + + +if SHIELD_MICRODOX_RIGHT + +config ZMK_KEYBOARD_NAME + default "Microdox Right" + +endif + +if SHIELD_MICRODOX_LEFT || SHIELD_MICRODOX_RIGHT + +config ZMK_SPLIT + default y + +if ZMK_DISPLAY + +config I2C + default y + +config SSD1306 + default y + +config SSD1306_REVERSE_MODE + default y + +endif # ZMK_DISPLAY + +if LVGL + +config LVGL_HOR_RES + default 128 + +config LVGL_VER_RES + default 32 + +config LVGL_VDB_SIZE + default 64 + +config LVGL_DPI + default 148 + +config LVGL_BITS_PER_PIXEL + default 1 + +choice LVGL_COLOR_DEPTH + default LVGL_COLOR_DEPTH_1 +endchoice + +endif # LVGL + +endif diff --git a/app/boards/shields/microdox/Kconfig.shield b/app/boards/shields/microdox/Kconfig.shield new file mode 100644 index 0000000..ac79eab --- /dev/null +++ b/app/boards/shields/microdox/Kconfig.shield @@ -0,0 +1,8 @@ +# Copyright (c) 2020 The ZMK Contributors +# SPDX-License-Identifier: MIT + +config SHIELD_MICRODOX_LEFT + def_bool $(shields_list_contains,microdox_left) + +config SHIELD_MICRODOX_RIGHT + def_bool $(shields_list_contains,microdox_right) diff --git a/app/boards/shields/microdox/boards/nice_nano.overlay b/app/boards/shields/microdox/boards/nice_nano.overlay new file mode 100644 index 0000000..58cd861 --- /dev/null +++ b/app/boards/shields/microdox/boards/nice_nano.overlay @@ -0,0 +1,34 @@ +/* + * Copyright (c) 2020 The ZMK Contributors + * + * SPDX-License-Identifier: MIT + */ +&spi1 { + compatible = "nordic,nrf-spi"; + /* Cannot be used together with i2c0. */ + status = "okay"; + mosi-pin = <6>; + // Unused pins, needed for SPI definition, but not used by the ws2812 driver itself. + sck-pin = <5>; + miso-pin = <7>; + + led_strip: ws2812@0 { + compatible = "worldsemi,ws2812-spi"; + label = "SK6812mini"; + + /* SPI */ + reg = <0>; /* ignored, but necessary for SPI bindings */ + spi-max-frequency = <4000000>; + + /* WS2812 */ + chain-length = <6>; /* There are per-key RGB, but the first 6 are underglow */ + spi-one-frame = <0x70>; + spi-zero-frame = <0x40>; + }; +}; + +/ { + chosen { + zmk,underglow = &led_strip; + }; +}; diff --git a/app/boards/shields/microdox/microdox.conf b/app/boards/shields/microdox/microdox.conf new file mode 100644 index 0000000..b79385b --- /dev/null +++ b/app/boards/shields/microdox/microdox.conf @@ -0,0 +1,6 @@ +# Uncomment the following lines to enable the Corne RGB Underglow +# ZMK_RGB_UNDERGLOW=y +# CONFIG_WS2812_STRIP=y + +# Uncomment the following line to enable the Corne OLED Display +# CONFIG_ZMK_DISPLAY=y diff --git a/app/boards/shields/microdox/microdox.dtsi b/app/boards/shields/microdox/microdox.dtsi new file mode 100644 index 0000000..55c67dd --- /dev/null +++ b/app/boards/shields/microdox/microdox.dtsi @@ -0,0 +1,66 @@ +/* + * Copyright (c) 2020 The ZMK Contributors + * + * SPDX-License-Identifier: MIT + */ + +#include <dt-bindings/zmk/matrix-transform.h> + +/ { + chosen { + zmk,kscan = &kscan0; + zmk,matrix_transform = &default_transform; + }; + + default_transform: keymap_transform_0 { + compatible = "zmk,matrix-transform"; + columns = <10>; + rows = <4>; +// | SW1 | SW2 | SW3 | SW4 | SW5 | | SW5 | SW4 | SW3 | SW2 | SW1 | +// | SW6 | SW7 | SW8 | SW9 | SW10 | | SW10 | SW9 | SW8 | SW7 | SW6 | +// | SW11 | SW12 | SW13 | SW14 | SW15 | | SW15 | SW14 | SW13 | SW12 | SW11 | +// | SW16 | SW17 | SW18 | | SW18 | SW17 | SW16 | + 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(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(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(3,2) RC(3,3) RC(3,4) RC(3,5) RC(3,6) RC(3,7) + >; + }; + + kscan0: kscan { + compatible = "zmk,kscan-gpio-matrix"; + label = "KSCAN"; + + diode-direction = "col2row"; + row-gpios + = <&pro_micro_d 16 (GPIO_ACTIVE_HIGH | GPIO_PULL_DOWN)> + , <&pro_micro_d 10 (GPIO_ACTIVE_HIGH | GPIO_PULL_DOWN)> + , <&pro_micro_d 8 (GPIO_ACTIVE_HIGH | GPIO_PULL_DOWN)> + , <&pro_micro_d 9 (GPIO_ACTIVE_HIGH | GPIO_PULL_DOWN)> + ; + + }; + + // TODO: per-key RGB node(s)? +}; + +&pro_micro_i2c { + status = "okay"; + + oled: ssd1306@3c { + compatible = "solomon,ssd1306fb"; + reg = <0x3c>; + label = "DISPLAY"; + width = <128>; + height = <32>; + segment-offset = <0>; + page-offset = <0>; + display-offset = <0>; + multiplex-ratio = <31>; + segment-remap; + com-invdir; + com-sequential; + prechargep = <0x22>; + }; +}; diff --git a/app/boards/shields/microdox/microdox.keymap b/app/boards/shields/microdox/microdox.keymap new file mode 100644 index 0000000..5747771 --- /dev/null +++ b/app/boards/shields/microdox/microdox.keymap @@ -0,0 +1,73 @@ +/* + * Copyright (c) 2020 The ZMK Contributors + * + * SPDX-License-Identifier: MIT + */ + +#include <behaviors.dtsi> +#include <dt-bindings/zmk/keys.h> +#include <dt-bindings/zmk/bt.h> + +/ { + keymap { + compatible = "zmk,keymap"; + + default_layer { +// ----------------------------------------------------------------------------------------- +// | Q | W | E | R | T | | Y | U | I | O | P | +// | A | S | D | F | G | | H | J | K | L | ; | +// | Z | X | C | V | B | | N | M | , | . | / | +// | GUI | NAV | SHFT | | SPC | SYM | ALT | + bindings = < + &kp Q &kp W &kp E &kp R &kp T &kp Y &kp U &kp I &kp O &kp P + &kp A &kp S &kp D &kp F &kp G &kp H &kp J &kp K &kp L &kp SCLN + &kp Z &kp X &kp C &kp V &kp B &kp N &kp M &kp CMMA &kp DOT &kp FSLH + &kp LGUI &mo 1 &kp LSFT &kp SPC &mo 2 &kp RALT + >; + }; + nav_layer { +// ----------------------------------------------------------------------------------------- +// |BTCLR| | ESC | ~ | | | TAB | HOME | UP | END | DEL | +// | BT1 | GUI | ALT | CTRL | NUM | | / | LEFT | DOWN | RGT | BKSP | +// | BT2 | | | | | | \ | ENT | | | | +// | | | | | | | | + bindings = < + &bt BT_CLR &trans &kp ESC &kp TILD &trans &kp TAB &kp HOME &kp UARW &kp END &kp DEL + &bt BT_SEL 0 &kp GUI &kp RALT &kp LCTL &mo 3 &kp FSLH &kp LARW &kp DARW &kp RARW &kp BKSP + &bt BT_SEL 1 &trans &trans &trans &trans &kp BSLH &kp RET &trans &trans &trans + &trans &trans &trans &trans &trans &trans + >; + }; + + sym_layer { +// ----------------------------------------------------------------------------------------- +// | ! | @ | # | $ | % | | ^ | & | * | ( | ) | +// | | | | | | | - | = | { | } | "|" | +// | | | | | | | _ | + | [ | ] | \ | +// | GUI | | SPC | | ENT | | ALT | + bindings = < + &kp BANG &kp ATSN &kp HASH &kp CURU &kp PRCT &kp CRRT &kp AMPS &kp KMLT &kp LPRN &kp RPRN + &trans &trans &trans &trans &trans &kp MINUS &kp EQL &kp LBKT &kp RBKT &kp PIPE + &trans &trans &trans &trans &trans &trans &trans &trans &trans &kp BSLH + &kp LGUI &trans &kp SPC &kp RET &trans &kp RALT + >; + }; + +// This layer is unreachable until "tri layer state" is sorted out. +// Leaving it here for completeness. + num_layer { +// ----------------------------------------------------------------------------------------- +// | | | | | | | A | 7 | 8 | 9 | D | +// | | | | | | | B | 4 | 5 | 6 | E | +// | | | | | | | C | 1 | 2 | 3 | F | +// | | | | | 0 | . | | + bindings = < + &trans &trans &trans &trans &trans &kp A &kp NUM_7 &kp NUM_8 &kp NUM_9 &kp D + &trans &trans &trans &trans &trans &kp B &kp NUM_4 &kp NUM_5 &kp NUM_6 &kp E + &trans &trans &trans &trans &trans &kp C &kp NUM_1 &kp NUM_2 &kp NUM_3 &kp F + &trans &trans &trans &kp NUM_0 &kp DOT &trans + >; + }; + }; +}; + diff --git a/app/boards/shields/microdox/microdox_left.conf b/app/boards/shields/microdox/microdox_left.conf new file mode 100644 index 0000000..1e028a7 --- /dev/null +++ b/app/boards/shields/microdox/microdox_left.conf @@ -0,0 +1,2 @@ +CONFIG_ZMK_SPLIT=y +CONFIG_ZMK_SPLIT_BLE_ROLE_CENTRAL=y diff --git a/app/boards/shields/microdox/microdox_left.overlay b/app/boards/shields/microdox/microdox_left.overlay new file mode 100644 index 0000000..4d0378e --- /dev/null +++ b/app/boards/shields/microdox/microdox_left.overlay @@ -0,0 +1,17 @@ +/* + * Copyright (c) 2020 The ZMK Contributors + * + * SPDX-License-Identifier: MIT + */ + +#include "microdox.dtsi" + +&kscan0 { + col-gpios + = <&pro_micro_a 3 GPIO_ACTIVE_HIGH> + , <&pro_micro_a 2 GPIO_ACTIVE_HIGH> + , <&pro_micro_a 1 GPIO_ACTIVE_HIGH> + , <&pro_micro_a 0 GPIO_ACTIVE_HIGH> + , <&pro_micro_d 15 GPIO_ACTIVE_HIGH> + ; +}; diff --git a/app/boards/shields/microdox/microdox_right.conf b/app/boards/shields/microdox/microdox_right.conf new file mode 100644 index 0000000..990cf7c --- /dev/null +++ b/app/boards/shields/microdox/microdox_right.conf @@ -0,0 +1,2 @@ +CONFIG_ZMK_SPLIT=y +CONFIG_ZMK_SPLIT_BLE_ROLE_PERIPHERAL=y diff --git a/app/boards/shields/microdox/microdox_right.overlay b/app/boards/shields/microdox/microdox_right.overlay new file mode 100644 index 0000000..c5622b2 --- /dev/null +++ b/app/boards/shields/microdox/microdox_right.overlay @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2020 The ZMK Contributors + * + * SPDX-License-Identifier: MIT + */ + +#include "microdox.dtsi" + +&default_transform { + col-offset = <5>; +}; + +&kscan0 { + col-gpios + = <&pro_micro_d 15 GPIO_ACTIVE_HIGH> + , <&pro_micro_a 0 GPIO_ACTIVE_HIGH> + , <&pro_micro_a 1 GPIO_ACTIVE_HIGH> + , <&pro_micro_a 2 GPIO_ACTIVE_HIGH> + , <&pro_micro_a 3 GPIO_ACTIVE_HIGH> + ; +}; diff --git a/app/boards/shields/qaz/Kconfig.defconfig b/app/boards/shields/qaz/Kconfig.defconfig index f82f458..c84180b 100644 --- a/app/boards/shields/qaz/Kconfig.defconfig +++ b/app/boards/shields/qaz/Kconfig.defconfig @@ -1,5 +1,5 @@ - # Copyright (c) 2020 TJ Campie - # SPDX-License-Identifier: MIT +# Copyright (c) 2020 The ZMK Contributors +# SPDX-License-Identifier: MIT if SHIELD_QAZ diff --git a/app/boards/shields/qaz/Kconfig.shield b/app/boards/shields/qaz/Kconfig.shield index 9da6077..7cc8f1e 100644 --- a/app/boards/shields/qaz/Kconfig.shield +++ b/app/boards/shields/qaz/Kconfig.shield @@ -1,5 +1,5 @@ -# Copyright (c) 2020 TJ Campie +# Copyright (c) 2020 The ZMK Contributors # SPDX-License-Identifier: MIT - - config SHIELD_MY_BOARD - def_bool $(shields_list_contains,qaz) + +config SHIELD_QAZ + def_bool $(shields_list_contains,qaz) diff --git a/app/tests/toggle-layer/early-key-release/pending b/app/boards/shields/qaz/qaz.conf index e69de29..e69de29 100644 --- a/app/tests/toggle-layer/early-key-release/pending +++ b/app/boards/shields/qaz/qaz.conf diff --git a/app/boards/shields/qaz/qaz.keymap b/app/boards/shields/qaz/qaz.keymap index ffa2f6c..3622972 100644 --- a/app/boards/shields/qaz/qaz.keymap +++ b/app/boards/shields/qaz/qaz.keymap @@ -1,16 +1,30 @@ /* - * Copyright (c) 2020 TJ Campie - * + * Copyright (c) 2020 The ZMK Contributors + * * SPDX-License-Identifier: MIT */ #include <behaviors.dtsi> #include <dt-bindings/zmk/keys.h> +#include <dt-bindings/zmk/bt.h> #define DEFAULT 0 #define NUM_SYM 1 #define NAV 2 - + +/ { + behaviors { + hm: homerow_mods { + compatible = "zmk,behavior-hold-tap"; + label = "homerow mods"; + #binding-cells = <2>; + tapping_term_ms = <225>; + flavor = "tap-preferred"; + bindings = <&kp>, <&kp>; + }; + }; +}; + / { keymap { compatible = "zmk,keymap"; @@ -18,9 +32,9 @@ default_layer { bindings = < &kp Q &kp W &kp E &kp R &kp T &kp Y &kp U &kp I &kp O &kp P - &kp A &kp S &kp D &kp F &kp G &kp H &kp J &kp K &kp L &kp RET + &hm LGUI A &hm LALT S &hm LCTL D &hm LSFT F &kp G &kp H &hm RSFT J &hm RCTL K &hm RALT L &hm RGUI RET &kp Z &kp X &kp C &kp V &kp B &kp N &kp M &kp CMMA &kp DOT - &kp LSFT &kp LGUI &kp LALT &mo NAV &kp SPC &mo NUM_SYM &kp QUOT &kp FSLH + &kp LSFT &kp LGUI &kp LALT < NAV RET < NUM_SYM SPC &kp COLN &kp QUOT &kp FSLH >; }; num_sym { @@ -34,10 +48,10 @@ nav { bindings = < - &none &none &none &none &none &none &none &kp UARW &none &kp BKSP + &bt BT_CLR &bt BT_NXT &bt BT_PRV &none &none &none &none &kp UARW &none &kp BKSP &trans &trans &trans &trans &none &none &kp LARW &kp DARW &kp RARW &none &none &none &none &none &none &none &none &none &none - &none &none &none &none &none &trans &trans &kp RET &trans &kp FSLH + &none &none &none &trans &trans &kp RET &trans &kp FSLH >; }; }; diff --git a/app/boards/shields/qaz/qaz.overlay b/app/boards/shields/qaz/qaz.overlay index 472005b..4e73370 100644 --- a/app/boards/shields/qaz/qaz.overlay +++ b/app/boards/shields/qaz/qaz.overlay @@ -1,6 +1,6 @@ /* - * Copyright (c) 2020 TJ Campie - * + * Copyright (c) 2020 The ZMK Contributors + * * SPDX-License-Identifier: MIT */ diff --git a/app/boards/shields/quefrency/Kconfig.defconfig b/app/boards/shields/quefrency/Kconfig.defconfig new file mode 100644 index 0000000..2b00cb6 --- /dev/null +++ b/app/boards/shields/quefrency/Kconfig.defconfig @@ -0,0 +1,17 @@ +#Copyright (c) 2020 The ZMK Contributors +#SPDX-License-Identifier: MIT + + +if SHIELD_QUEFRENCY_LEFT + +config ZMK_KEYBOARD_NAME + default "Quefrency Left" + +endif + +if SHIELD_QUEFRENCY_RIGHT + +config ZMK_KEYBOARD_NAME + default "Quefrency Right" + +endif diff --git a/app/boards/shields/quefrency/Kconfig.shield b/app/boards/shields/quefrency/Kconfig.shield new file mode 100644 index 0000000..d205e58 --- /dev/null +++ b/app/boards/shields/quefrency/Kconfig.shield @@ -0,0 +1,8 @@ +# Copyright (c) 2020 The ZMK Contributors +# SPDX-License-Identifier: MIT + +config SHIELD_QUEFRENCY_LEFT + def_bool $(shields_list_contains,quefrency_left) + +config SHIELD_QUEFRENCY_RIGHT + def_bool $(shields_list_contains,quefrency_right) diff --git a/app/boards/shields/quefrency/quefrency.conf b/app/boards/shields/quefrency/quefrency.conf new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/app/boards/shields/quefrency/quefrency.conf diff --git a/app/boards/shields/quefrency/quefrency.dtsi b/app/boards/shields/quefrency/quefrency.dtsi new file mode 100644 index 0000000..5f1e908 --- /dev/null +++ b/app/boards/shields/quefrency/quefrency.dtsi @@ -0,0 +1,32 @@ +/* + * Copyright (c) 2020 The ZMK Contributors + * + * SPDX-License-Identifier: MIT + */ + +#include <dt-bindings/zmk/matrix-transform.h> + +/ { + chosen { + zmk,kscan = &kscan0; + + zmk,matrix_transform = &default_transform; + }; + + /* + * This transform correspondsto the 60% left without macro keypad and 65% right, even this + * combination of PCBs can have keys in different locations based on configuration. + */ + default_transform: keymap_transform_0 { + compatible = "zmk,matrix-transform"; + columns = <15>; + rows = <6>; + 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,14) RC(5,13) +RC(1,0) RC(1,1) RC(1,2) RC(1,3) RC(1,4) RC(1,5) /**/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(5,14) +RC(2,0) RC(2,1) RC(2,2) RC(2,3) RC(2,4) RC(2,5) /**/ RC(2,7) RC(2,8) RC(2,9) RC(2,10) RC(2,11) RC(2,12) RC(2,14) RC(2,13) +RC(3,0) 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,12) RC(3,13) RC(3,14) RC(3,11) +RC(4,0) RC(4,1) RC(4,2) RC(4,4) RC(4,6) /**/ RC(4,7) RC(4,10) RC(4,11) RC(4,12) RC(4,13) RC(4,14) RC(4,9) + >; + }; +}; diff --git a/app/boards/shields/quefrency/quefrency.keymap b/app/boards/shields/quefrency/quefrency.keymap new file mode 100644 index 0000000..21c5ed4 --- /dev/null +++ b/app/boards/shields/quefrency/quefrency.keymap @@ -0,0 +1,51 @@ +/* + * Copyright (c) 2020 The ZMK Contributors + * + * SPDX-License-Identifier: MIT + */ + +#include <behaviors.dtsi> +#include <dt-bindings/zmk/bt.h> +#include <dt-bindings/zmk/keys.h> + +/ { + keymap { + compatible = "zmk,keymap"; + +// ---------------------------------------------- ----------------------------------------------------- +// | ESC | 1 | 2 | 3 | 4 | 5 | 6 | | 7 | 8 | 9 | 0 | - | = | BKSPC | ` | +// | TAB | Q | W | E | R | T | | Y | U | I | O | P | [ | ] | \ | DEL | +// | LCTRL | A | S | D | F | G | | H | J | K | L | ; | ' | ENTER | PGUP | +// | SHIFT | Z | X | C | V | B | | N | M | , | . | / | RSHFT | UP | PGDN | +// | LCTRL | LGUI | LALT | SPACE | FN | | SPACE | RALT | FN | RCTRL | LFT | DWN | RGHT | +// ------------------------------------------- ------------------------------------------------------ + + default_layer { + bindings = < + &kp ESC &kp NUM_1 &kp NUM_2 &kp NUM_3 &kp NUM_4 &kp NUM_5 &kp NUM_6 /**/ &kp NUM_7 &kp NUM_8 &kp NUM_9 &kp NUM_0 &kp MINUS &kp EQL &kp BKSP &kp GRAV + &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 HOME + &kp LCTL &kp A &kp S &kp D &kp F &kp G /**/ &kp H &kp J &kp K &kp L &kp SCLN &kp QUOT &kp RET &kp PGUP + &kp LSFT &kp Z &kp X &kp C &kp V &kp B /**/ &kp N &kp M &kp CMMA &kp DOT &kp FSLH &kp RSFT &kp UARW &kp PGDN + &kp LCTL &kp LGUI &kp LALT &kp SPC &mo 1 /**/ &kp SPC &kp RALT &mo 1 &kp RCTL &kp LARW &kp DARW &kp RARW + >; + }; + +// ---------------------------------------------- ----------------------------------------------------- +// |BT_CLR| F1 | F2 | F3 | F4 | F5 | F6 | | F7 | F8 | F9 | F10 | F11 | F12 | |BT_CLR| +// | | BT-0 | BT-1| BT-2 | | | | | | | | | | | | | +// | | | | | | | | | | | | | | | | +// | | | | | | | | | | | | | | | | +// | | | | | | | | | | | | | | +// ------------------------------------------- ------------------------------------------------------ + + fn_layer { + bindings = < + &bt BT_CLR &kp F1 &kp F2 &kp F3 &kp F4 &kp F5 &kp F6 /**/ &kp F7 &kp F8 &kp F9 &kp F10 &kp F11 &kp F12 &trans &bt BT_CLR + &trans &bt BT_SEL 0 &bt BT_SEL 1 &bt BT_SEL 2 &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 &trans &trans &trans /**/ &trans &trans &trans &trans &trans &trans &trans &trans + &trans &trans &trans &trans &trans /**/ &trans &trans &trans &trans &trans &trans &trans + >; + }; + }; +}; diff --git a/app/boards/shields/quefrency/quefrency_left.conf b/app/boards/shields/quefrency/quefrency_left.conf new file mode 100644 index 0000000..1e028a7 --- /dev/null +++ b/app/boards/shields/quefrency/quefrency_left.conf @@ -0,0 +1,2 @@ +CONFIG_ZMK_SPLIT=y +CONFIG_ZMK_SPLIT_BLE_ROLE_CENTRAL=y diff --git a/app/boards/shields/quefrency/quefrency_left.overlay b/app/boards/shields/quefrency/quefrency_left.overlay new file mode 100644 index 0000000..a385cc5 --- /dev/null +++ b/app/boards/shields/quefrency/quefrency_left.overlay @@ -0,0 +1,37 @@ +/* + * Copyright (c) 2020 The ZMK Contributors + * + * SPDX-License-Identifier: MIT + */ + +#include "quefrency.dtsi" + +/ { + /* This kscan is for the 60% left half without macro keys the + * macro pad layout may require different column and row pins + */ + kscan0: kscan { + compatible = "zmk,kscan-gpio-matrix"; + label = "KSCAN"; + diode-direction = "col2row"; + + + col-gpios + = <&pro_micro_a 2 GPIO_ACTIVE_HIGH> + , <&pro_micro_a 1 GPIO_ACTIVE_HIGH> + , <&pro_micro_a 0 GPIO_ACTIVE_HIGH> + , <&pro_micro_d 15 GPIO_ACTIVE_HIGH> + , <&pro_micro_d 14 GPIO_ACTIVE_HIGH> + , <&pro_micro_d 16 GPIO_ACTIVE_HIGH> + , <&pro_micro_d 10 GPIO_ACTIVE_HIGH> + ; + + row-gpios + = <&pro_micro_a 3 (GPIO_ACTIVE_HIGH | GPIO_PULL_DOWN)> + , <&pro_micro_a 6 (GPIO_ACTIVE_HIGH | GPIO_PULL_DOWN)> + , <&pro_micro_a 7 (GPIO_ACTIVE_HIGH | GPIO_PULL_DOWN)> + , <&pro_micro_d 7 (GPIO_ACTIVE_HIGH | GPIO_PULL_DOWN)> + , <&pro_micro_a 8 (GPIO_ACTIVE_HIGH | GPIO_PULL_DOWN)> + ; + }; +}; diff --git a/app/boards/shields/quefrency/quefrency_right.conf b/app/boards/shields/quefrency/quefrency_right.conf new file mode 100644 index 0000000..990cf7c --- /dev/null +++ b/app/boards/shields/quefrency/quefrency_right.conf @@ -0,0 +1,2 @@ +CONFIG_ZMK_SPLIT=y +CONFIG_ZMK_SPLIT_BLE_ROLE_PERIPHERAL=y diff --git a/app/boards/shields/quefrency/quefrency_right.overlay b/app/boards/shields/quefrency/quefrency_right.overlay new file mode 100644 index 0000000..53e0f77 --- /dev/null +++ b/app/boards/shields/quefrency/quefrency_right.overlay @@ -0,0 +1,45 @@ +/* + * Copyright (c) 2020 The ZMK Contributors + * + * SPDX-License-Identifier: MIT + */ + +#include "quefrency.dtsi" + +&default_transform { + col-offset = <7>; +}; + +/ { + + /* This kscan is for the 65% right half the 60% right half + * may require different column and row pins + */ + kscan0: kscan { + compatible = "zmk,kscan-gpio-matrix"; + label = "KSCAN"; + diode-direction = "col2row"; + + + + col-gpios + = <&pro_micro_a 2 GPIO_ACTIVE_HIGH> + , <&pro_micro_a 1 GPIO_ACTIVE_HIGH> + , <&pro_micro_a 0 GPIO_ACTIVE_HIGH> + , <&pro_micro_d 15 GPIO_ACTIVE_HIGH> + , <&pro_micro_d 14 GPIO_ACTIVE_HIGH> + , <&pro_micro_d 16 GPIO_ACTIVE_HIGH> + , <&pro_micro_d 10 GPIO_ACTIVE_HIGH> + , <&pro_micro_d 5 GPIO_ACTIVE_HIGH> + ; + + row-gpios + = <&pro_micro_a 3 (GPIO_ACTIVE_HIGH | GPIO_PULL_DOWN)> + , <&pro_micro_a 6 (GPIO_ACTIVE_HIGH | GPIO_PULL_DOWN)> + , <&pro_micro_a 7 (GPIO_ACTIVE_HIGH | GPIO_PULL_DOWN)> + , <&pro_micro_d 7 (GPIO_ACTIVE_HIGH | GPIO_PULL_DOWN)> + , <&pro_micro_a 8 (GPIO_ACTIVE_HIGH | GPIO_PULL_DOWN)> + , <&pro_micro_a 9 (GPIO_ACTIVE_HIGH | GPIO_PULL_DOWN)> + ; + }; +}; diff --git a/app/boards/shields/romac/romac.keymap b/app/boards/shields/romac/romac.keymap index f94a829..31e1ce8 100644 --- a/app/boards/shields/romac/romac.keymap +++ b/app/boards/shields/romac/romac.keymap @@ -1,11 +1,12 @@ /* - * Copyright (c) 2020 Pete Johanson, Richard Jones + * Copyright (c) 2020 The ZMK Contributors * * SPDX-License-Identifier: MIT */ #include <behaviors.dtsi> #include <dt-bindings/zmk/keys.h> +#include <dt-bindings/zmk/bt.h> / { keymap { @@ -28,16 +29,16 @@ nav_layer { // ----------------------- -// | _ | HOME | PGUP | -// | _ | END | PGDN | -// | _ | _ | _ | -// | _ | _ | RET | +// | BTNXT | HOME | PGUP | +// | BTPRV | END | PGDN | +// | BTCLR | _ | _ | +// | _ | _ | RET | // ----------------------- bindings = < - &trans &kp HOME &kp PGUP - &trans &kp END &kp PGDN - &trans &trans &trans - &trans &trans &kp RET + &bt BT_NXT &kp HOME &kp PGUP + &bt BT_PRV &kp END &kp PGDN + &bt BT_CLR &trans &trans + &trans &trans &kp RET >; }; }; diff --git a/app/boards/shields/romac_plus/Kconfig.defconfig b/app/boards/shields/romac_plus/Kconfig.defconfig new file mode 100644 index 0000000..c4efdb9 --- /dev/null +++ b/app/boards/shields/romac_plus/Kconfig.defconfig @@ -0,0 +1,9 @@ +# Copyright (c) 2020 The ZMK Contributors +# SPDX-License-Identifier: MIT + +if SHIELD_ROMAC_PLUS + +config ZMK_KEYBOARD_NAME + default "RoMac+ v4" + +endif
\ No newline at end of file diff --git a/app/boards/shields/romac_plus/Kconfig.shield b/app/boards/shields/romac_plus/Kconfig.shield new file mode 100644 index 0000000..a7c7c61 --- /dev/null +++ b/app/boards/shields/romac_plus/Kconfig.shield @@ -0,0 +1,5 @@ +# Copyright (c) 2020 The ZMK Contributors +# SPDX-License-Identifier: MIT + +config SHIELD_ROMAC_PLUS + def_bool $(shields_list_contains,romac_plus) diff --git a/app/boards/shields/romac_plus/boards/nice_nano.overlay b/app/boards/shields/romac_plus/boards/nice_nano.overlay new file mode 100644 index 0000000..a8dafa2 --- /dev/null +++ b/app/boards/shields/romac_plus/boards/nice_nano.overlay @@ -0,0 +1,28 @@ +&spi1 {
+ compatible = "nordic,nrf-spi";
+ status = "okay";
+ mosi-pin = <6>;
+ // Unused pins, needed for SPI definition, but not used by the ws2812 driver itself.
+ sck-pin = <5>;
+ miso-pin = <7>;
+
+ led_strip: ws2812@0 {
+ compatible = "worldsemi,ws2812-spi";
+ label = "WS2812";
+
+ /* SPI */
+ reg = <0>; /* ignored, but necessary for SPI bindings */
+ spi-max-frequency = <4000000>;
+
+ /* WS2812 */
+ chain-length = <10>; /* arbitrary; change at will */
+ spi-one-frame = <0x70>;
+ spi-zero-frame = <0x40>;
+ };
+};
+
+/ {
+ chosen {
+ zmk,underglow = &led_strip;
+ };
+};
\ No newline at end of file diff --git a/app/boards/shields/romac_plus/romac_plus.conf b/app/boards/shields/romac_plus/romac_plus.conf new file mode 100644 index 0000000..d784dc4 --- /dev/null +++ b/app/boards/shields/romac_plus/romac_plus.conf @@ -0,0 +1,6 @@ +# Copyright (c) 2020 The ZMK Contributors
+# SPDX-License-Identifier: MIT
+
+# Uncomment to enable encoder
+#CONFIG_EC11=y
+#CONFIG_EC11_TRIGGER_GLOBAL_THREAD=y
\ No newline at end of file diff --git a/app/boards/shields/romac_plus/romac_plus.dtsi b/app/boards/shields/romac_plus/romac_plus.dtsi new file mode 100644 index 0000000..9b148ca --- /dev/null +++ b/app/boards/shields/romac_plus/romac_plus.dtsi @@ -0,0 +1,56 @@ +/*
+ * Copyright (c) 2020 The ZMK Contributors
+ *
+ * SPDX-License-Identifier: MIT
+ */
+
+#include <dt-bindings/zmk/matrix-transform.h>
+
+/ {
+ chosen {
+ zmk,kscan = &kscan0;
+ zmk,matrix_transform = &default_transform;
+ };
+
+ default_transform: keymap_transform_0 {
+ compatible = "zmk,matrix-transform";
+ columns = <3>;
+ rows = <4>;
+
+ map = <
+RC(0,0) RC(0,1) RC(0,2)
+RC(1,0) RC(1,1) RC(1,2)
+RC(2,0) RC(2,1) RC(2,2)
+RC(3,0) RC(3,1) RC(3,2)
+ >;
+ };
+
+ kscan0: kscan {
+ compatible = "zmk,kscan-gpio-matrix";
+ label = "KSCAN";
+
+ diode-direction = "col2row";
+ row-gpios
+ = <&pro_micro_d 5 (GPIO_ACTIVE_HIGH | GPIO_PULL_DOWN)>
+ , <&pro_micro_d 4 (GPIO_ACTIVE_HIGH | GPIO_PULL_DOWN)>
+ , <&pro_micro_d 0 (GPIO_ACTIVE_HIGH | GPIO_PULL_DOWN)>
+ , <&pro_micro_d 1 (GPIO_ACTIVE_HIGH | GPIO_PULL_DOWN)>
+ ;
+ };
+
+ left_encoder: encoder_left {
+ compatible = "alps,ec11";
+ label = "LEFT_ENCODER";
+ a-gpios = <&pro_micro_d 16 (GPIO_ACTIVE_HIGH | GPIO_PULL_UP)>;
+ b-gpios = <&pro_micro_d 14 (GPIO_ACTIVE_HIGH | GPIO_PULL_UP)>;
+ resolution = <4>;
+ status = "disabled";
+ };
+
+ sensors {
+ compatible = "zmk,keymap-sensors";
+ sensors = <&left_encoder>;
+ };
+
+ // TODO: per-key RGB node(s)?
+};
\ No newline at end of file diff --git a/app/boards/shields/romac_plus/romac_plus.keymap b/app/boards/shields/romac_plus/romac_plus.keymap new file mode 100644 index 0000000..d8d1f78 --- /dev/null +++ b/app/boards/shields/romac_plus/romac_plus.keymap @@ -0,0 +1,49 @@ +/* + * Copyright (c) 2020 The ZMK Contributors + * + * SPDX-License-Identifier: MIT + */ + +#include <behaviors.dtsi> +#include <dt-bindings/zmk/bt.h> +#include <dt-bindings/zmk/keys.h> + +/ { + keymap { + compatible = "zmk,keymap"; + + default_layer { +// -------------------------- +// | 7 | 8 | 9 | +// | 4 | 5 | 6 | +// | 1 | 2 | 3 | +// | M_PLAY | 0 | MO(1) | +// -------------------------- + bindings = < + &kp NUM_7 &kp NUM_8 &kp NUM_9 + &kp NUM_4 &kp NUM_5 &kp NUM_6 + &kp NUM_1 &kp NUM_2 &kp NUM_3 + &cp M_PLAY &kp NUM_0 &mo 1 + >; + + sensor-bindings = <&inc_dec_cp M_NEXT M_PREV>; + }; + + nav_layer { +// -------------------------- +// | BT_CLR | HOME | PGUP | +// | _ | END | PGDN | +// | _ | _ | _ | +// | _ | _ | _ | +// -------------------------- + bindings = < + &bt BT_CLR &kp HOME &kp PGUP + &trans &kp END &kp PGDN + &trans &trans &trans + &trans &trans &trans + >; + + sensor-bindings = <&inc_dec_kp A B>; + }; + }; +};
\ No newline at end of file diff --git a/app/boards/shields/romac_plus/romac_plus.overlay b/app/boards/shields/romac_plus/romac_plus.overlay new file mode 100644 index 0000000..8643034 --- /dev/null +++ b/app/boards/shields/romac_plus/romac_plus.overlay @@ -0,0 +1,31 @@ +/* + * Copyright (c) 2020 The ZMK Contributors + * + * SPDX-License-Identifier: MIT + */ + +#include "romac_plus.dtsi" + +/ { + chosen { + zmk,kscan = &kscan0; + }; + + kscan0: kscan { + compatible = "zmk,kscan-gpio-matrix"; + label = "KSCAN"; + + diode-direction = "col2row"; + + col-gpios + = <&pro_micro_a 1 GPIO_ACTIVE_HIGH> + , <&pro_micro_a 2 GPIO_ACTIVE_HIGH> + , <&pro_micro_a 3 GPIO_ACTIVE_HIGH> + ; + }; + +}; + +&left_encoder { + status = "okay"; +};
\ No newline at end of file diff --git a/app/boards/shields/settings_reset/Kconfig.defconfig b/app/boards/shields/settings_reset/Kconfig.defconfig new file mode 100644 index 0000000..6d050cb --- /dev/null +++ b/app/boards/shields/settings_reset/Kconfig.defconfig @@ -0,0 +1,10 @@ +# Copyright (c) 2020 The ZMK Contributors +# SPDX-License-Identifier: MIT + +if SHIELD_SETTINGS_RESET + +config ZMK_KEYBOARD_NAME + default "SETTINGS RESET" + +endif + diff --git a/app/boards/shields/settings_reset/Kconfig.shield b/app/boards/shields/settings_reset/Kconfig.shield new file mode 100644 index 0000000..b5ce97f --- /dev/null +++ b/app/boards/shields/settings_reset/Kconfig.shield @@ -0,0 +1,5 @@ +# Copyright (c) 2020 The ZMK Contributors +# SPDX-License-Identifier: MIT + +config SHIELD_SETTINGS_RESET + def_bool $(shields_list_contains,settings_reset) diff --git a/app/boards/shields/settings_reset/settings_reset.conf b/app/boards/shields/settings_reset/settings_reset.conf new file mode 100644 index 0000000..8052a6c --- /dev/null +++ b/app/boards/shields/settings_reset/settings_reset.conf @@ -0,0 +1 @@ +CONFIG_ZMK_BLE_CLEAR_BONDS_ON_START=y diff --git a/app/boards/shields/settings_reset/settings_reset.keymap b/app/boards/shields/settings_reset/settings_reset.keymap new file mode 100644 index 0000000..0523644 --- /dev/null +++ b/app/boards/shields/settings_reset/settings_reset.keymap @@ -0,0 +1,22 @@ +/* + * Copyright (c) 2020 The ZMK Contributors + * + * SPDX-License-Identifier: MIT + */ + +#include <behaviors.dtsi> +#include <dt-bindings/zmk/keys.h> + +/ { + keymap { + compatible = "zmk,keymap"; + + default_layer { + bindings = < + &reset + >; + }; + }; +}; + + diff --git a/app/boards/shields/settings_reset/settings_reset.overlay b/app/boards/shields/settings_reset/settings_reset.overlay new file mode 100644 index 0000000..a2b5799 --- /dev/null +++ b/app/boards/shields/settings_reset/settings_reset.overlay @@ -0,0 +1,24 @@ +/* + * Copyright (c) 2020 The ZMK Contributors + * + * SPDX-License-Identifier: MIT + */ + +#include <dt-bindings/zmk/matrix-transform.h> + +/ { + chosen { + zmk,kscan = &kscan0; + }; + + kscan0: kscan { + compatible = "zmk,kscan-gpio-direct"; + label = "KSCAN"; + + input-gpios + = <&pro_micro_d 4 (GPIO_ACTIVE_LOW | GPIO_PULL_UP)> + ; + }; + +}; + diff --git a/app/boards/shields/sofle/sofle.dtsi b/app/boards/shields/sofle/sofle.dtsi index 653a772..bff42f6 100644 --- a/app/boards/shields/sofle/sofle.dtsi +++ b/app/boards/shields/sofle/sofle.dtsi @@ -50,6 +50,7 @@ RC(3,0) RC(3,1) RC(3,2) RC(3,3) RC(3,4) RC(3,5) RC(4,5) RC(4,6) RC(3,6) RC(3,7) a-gpios = <&pro_micro_a 3 (GPIO_ACTIVE_HIGH | GPIO_PULL_UP)>; b-gpios = <&pro_micro_a 2 (GPIO_ACTIVE_HIGH | GPIO_PULL_UP)>; resolution = <4>; + status = "disabled"; }; right_encoder: encoder_right { @@ -58,6 +59,7 @@ RC(3,0) RC(3,1) RC(3,2) RC(3,3) RC(3,4) RC(3,5) RC(4,5) RC(4,6) RC(3,6) RC(3,7) a-gpios = <&pro_micro_a 2 (GPIO_ACTIVE_HIGH | GPIO_PULL_UP)>; b-gpios = <&pro_micro_a 3 (GPIO_ACTIVE_HIGH | GPIO_PULL_UP)>; resolution = <4>; + status = "disabled"; }; sensors { diff --git a/app/boards/shields/sofle/sofle.keymap b/app/boards/shields/sofle/sofle.keymap index aadffa1..e2ebc1a 100644 --- a/app/boards/shields/sofle/sofle.keymap +++ b/app/boards/shields/sofle/sofle.keymap @@ -1,5 +1,12 @@ +/* + * Copyright (c) 2020 The ZMK Contributors + * + * SPDX-License-Identifier: MIT + */ + #include <behaviors.dtsi> #include <dt-bindings/zmk/keys.h> +#include <dt-bindings/zmk/bt.h> / { keymap { @@ -44,17 +51,17 @@ raise_layer { // ------------------------------------------------------------------------------------------------------------ -// | | | | | | | | | | | | | | +// |BTCLR| BT1 | BT2 | BT3 | BT4 | BT5 | | | | | | | | // | | INS | PSCR | GUI | | | | PGUP | | ^ | | | | // | | ALT | CTRL | SHIFT | | CAPS | | PGDN | <- | v | -> | DEL | BKSPC | // | | UNDO | CUT | COPY | PASTE | | | | | | | | | | | // | | | | | | | | | | | | bindings = < -&trans &trans &trans &trans &trans &trans &trans &trans &trans &trans &trans &trans -&trans &kp INS &kp PRSC &kp GUI &trans &trans &kp PGUP &trans &kp UARW &trans &kp NUM_0 &trans -&trans &kp LALT &kp LCTL &kp LSFT &trans &kp CLCK &kp PGDN &kp LARW &kp DARW &kp RARW &kp DEL &kp BKSP -&trans &kp UNDO &kp CUT &kp COPY &kp PSTE &trans &trans &trans &trans &trans &trans &trans &trans &trans - &trans &trans &trans &trans &trans &trans &trans &trans &trans &trans +&bt BT_CLR &bt BT_SEL 0 &bt BT_SEL 1 &bt BT_SEL 2 &bt BT_SEL 3 &bt BT_SEL 4 &trans &trans &trans &trans &trans &trans +&trans &kp INS &kp PRSC &kp GUI &trans &trans &kp PGUP &trans &kp UARW &trans &kp NUM_0 &trans +&trans &kp LALT &kp LCTL &kp LSFT &trans &kp CLCK &kp PGDN &kp LARW &kp DARW &kp RARW &kp DEL &kp BKSP +&trans &kp UNDO &kp CUT &kp COPY &kp PSTE &trans &trans &trans &trans &trans &trans &trans &trans &trans + &trans &trans &trans &trans &trans &trans &trans &trans &trans &trans >; sensor-bindings = <&inc_dec_cp M_VOLU M_VOLD &inc_dec_kp PGUP PGDN>; diff --git a/app/boards/shields/splitreus62/splitreus62.keymap b/app/boards/shields/splitreus62/splitreus62.keymap index 920e61a..07dd2ad 100644 --- a/app/boards/shields/splitreus62/splitreus62.keymap +++ b/app/boards/shields/splitreus62/splitreus62.keymap @@ -1,9 +1,10 @@ /* - * Copyright (c) 2020 Derek Schmell + * Copyright (c) 2020 The ZMK Contributors * * SPDX-License-Identifier: MIT */ + #include <behaviors.dtsi> #include <dt-bindings/zmk/keys.h> diff --git a/app/boards/shields/tg4x/Kconfig.defconfig b/app/boards/shields/tg4x/Kconfig.defconfig new file mode 100644 index 0000000..ca9fa2c --- /dev/null +++ b/app/boards/shields/tg4x/Kconfig.defconfig @@ -0,0 +1,9 @@ +# Copyright (c) 2020 The ZMK Contributors +# SPDX-License-Identifier: MIT + +if SHIELD_TG4X + +config ZMK_KEYBOARD_NAME + default "TG4X" + +endif
\ No newline at end of file diff --git a/app/boards/shields/tg4x/Kconfig.shield b/app/boards/shields/tg4x/Kconfig.shield new file mode 100644 index 0000000..7e98b71 --- /dev/null +++ b/app/boards/shields/tg4x/Kconfig.shield @@ -0,0 +1,5 @@ +# Copyright (c) 2020 The ZMK Contributors +# SPDX-License-Identifier: MIT + +config SHIELD_TG4X + def_bool $(shields_list_contains,tg4x) diff --git a/app/boards/shields/tg4x/tg4x.keymap b/app/boards/shields/tg4x/tg4x.keymap new file mode 100644 index 0000000..bee5c87 --- /dev/null +++ b/app/boards/shields/tg4x/tg4x.keymap @@ -0,0 +1,58 @@ +/* + * Copyright (c) 2020 The ZMK Contributors + * + * SPDX-License-Identifier: MIT + */ + +#include <behaviors.dtsi> +#include <dt-bindings/zmk/keys.h> +#include <dt-bindings/zmk/bt.h> + +#define DEFAULT 0 +#define LOWER 1 +#define RAISE 2 + +/ { + behaviors { + hm: homerow_mods { + compatible = "zmk,behavior-hold-tap"; + label = "homerow mods"; + #binding-cells = <2>; + tapping_term_ms = <225>; + flavor = "tap-preferred"; + bindings = <&kp>, <&kp>; + }; + }; +}; + +/ { + keymap { + compatible = "zmk,keymap"; + + default_layer { + bindings = < + &kp ESC &kp Q &kp W &kp E &kp R &kp T &kp Y &kp U &kp I &kp O &kp P &kp BKSP + &kp TAB &hm LGUI A &hm LALT S &hm LCTL D &hm LSFT F &kp G &kp H &hm RSFT J &hm RCTL K &hm RALT L &hm RGUI SCLN &kp RET + &kp LSFT &kp Z &kp X &kp C &kp V &kp B &kp N &kp M &kp CMMA &kp DOT &kp FSLH &kp QUOT + &kp LCTL &kp LALT &kp LGUI < 1 BKSP < 2 SPC &kp LARW &kp DARW &kp UARW &kp RARW + >; + }; + lower { + bindings = < + &kp GRAV &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 PRSC + &kp DEL &trans &kp VOLU &trans &trans &trans &trans &kp LARW &kp DARW &kp UARW &kp RARW &trans + &trans &trans &kp VOLD &trans &trans &trans &trans &trans &trans &bt BT_PRV &bt BT_NXT &bt BT_CLR + &bootloader &reset &trans &trans &trans &trans &bt BT_SEL 0 &bt BT_SEL 1 &bt BT_SEL 2 &bt BT_SEL 3 + >; + }; + + raise { + bindings = < + &kp GRAV &kp NUM_1 &kp NUM_2 &kp NUM_3 &kp NUM_4 &kp NUM_5 &kp NUM_6 &kp NUM_7 &kp NUM_8 &kp NUM_9 &kp NUM_0 &kp PRSC + &kp DEL &kp F1 &kp F2 &kp F3 &kp F4 &kp F5 &kp F6 &kp MINUS &kp EQL &kp LBKT &kp RBKT &kp BSLH + &trans &kp F7 &kp F8 &kp F9 &kp F10 &kp F11 &kp F12 &kp TILD &kp HOME &kp PGUP &kp PGDN &kp END + &trans &trans &trans &trans &trans &trans &kp M_NEXT &kp M_VOLD &kp M_VOLU &kp M_PLAY + >; + }; + }; +};
\ No newline at end of file diff --git a/app/boards/shields/tg4x/tg4x.overlay b/app/boards/shields/tg4x/tg4x.overlay new file mode 100644 index 0000000..10ce524 --- /dev/null +++ b/app/boards/shields/tg4x/tg4x.overlay @@ -0,0 +1,56 @@ +/* + * Copyright (c) 2020 The ZMK Contrbutors + * + * SPDX-License-Identifier: MIT + */ + +#include <dt-bindings/zmk/matrix-transform.h> + +/ { + chosen { + zmk,kscan = &kscan0; + zmk,matrix_transform = &default_transform; + }; + + default_transform: keymap_transform_0 { + compatible = "zmk,matrix-transform"; + columns = <7>; + rows = <8>; + map = < + RC(0,0) RC(0,1) RC(0,2) RC(0,3) RC(0,4) RC(0,5) RC(0,6) RC(4,0) RC(4,1) RC(4,2) RC(4,3) RC(4,5) + RC(1,0) RC(1,1) RC(1,2) RC(1,3) RC(1,4) RC(1,5) RC(1,6) RC(5,0) RC(5,1) RC(5,2) RC(5,3) RC(5,4) + RC(2,0) RC(2,1) RC(2,2) RC(2,3) RC(2,4) RC(2,5) RC(2,6) RC(6,0) RC(6,1) RC(6,2) RC(6,3) RC(6,4) + RC(3,0) RC(3,1) RC(3,2) RC(3,4) RC(3,5) RC(7,1) RC(7,2) RC(7,3) RC(7,4) + >; + }; + + kscan0: kscan { + compatible = "zmk,kscan-gpio-matrix"; + label = "KSCAN"; + diode-direction = "col2row"; + + col-gpios + = <&pro_micro_d 1 GPIO_ACTIVE_HIGH> + , <&pro_micro_d 14 GPIO_ACTIVE_HIGH> + , <&pro_micro_d 15 GPIO_ACTIVE_HIGH> + , <&pro_micro_a 0 GPIO_ACTIVE_HIGH> + , <&pro_micro_a 1 GPIO_ACTIVE_HIGH> + , <&pro_micro_a 2 GPIO_ACTIVE_HIGH> + , <&pro_micro_a 3 GPIO_ACTIVE_HIGH> + ; + + row-gpios + = <&pro_micro_a 9 (GPIO_ACTIVE_HIGH | GPIO_PULL_DOWN)> + , <&pro_micro_a 8 (GPIO_ACTIVE_HIGH | GPIO_PULL_DOWN)> + , <&pro_micro_d 7 (GPIO_ACTIVE_HIGH | GPIO_PULL_DOWN)> + , <&pro_micro_a 7 (GPIO_ACTIVE_HIGH | GPIO_PULL_DOWN)> + , <&pro_micro_d 5 (GPIO_ACTIVE_HIGH | GPIO_PULL_DOWN)> + , <&pro_micro_a 6 (GPIO_ACTIVE_HIGH | GPIO_PULL_DOWN)> + , <&pro_micro_d 3 (GPIO_ACTIVE_HIGH | GPIO_PULL_DOWN)> + , <&pro_micro_d 2 (GPIO_ACTIVE_HIGH | GPIO_PULL_DOWN)> + ; + + }; + +}; + diff --git a/app/drivers/zephyr/Kconfig b/app/drivers/zephyr/Kconfig index 0237846..0534cab 100644 --- a/app/drivers/zephyr/Kconfig +++ b/app/drivers/zephyr/Kconfig @@ -5,10 +5,14 @@ config ZMK_KSCAN_GPIO_DRIVER if ZMK_KSCAN_GPIO_DRIVER -config ZMK_KSCAN_GPIO_POLLING - bool "Poll for key event triggers instead of using interrupts" +config ZMK_KSCAN_MATRIX_POLLING + bool "Poll for key event triggers instead of using interrupts on matrix boards." default n +config ZMK_KSCAN_DIRECT_POLLING + bool "Poll for key event triggers instead of using interrupts on direct wired boards." + default n + endif config ZMK_KSCAN_INIT_PRIORITY diff --git a/app/drivers/zephyr/ec11.c b/app/drivers/zephyr/ec11.c index 1bc5d6a..00d0090 100644 --- a/app/drivers/zephyr/ec11.c +++ b/app/drivers/zephyr/ec11.c @@ -1,5 +1,5 @@ /* - * Copyright (c) 2020 Peter Johanson + * Copyright (c) 2020 The ZMK Contributors * * SPDX-License-Identifier: MIT */ @@ -18,136 +18,131 @@ LOG_MODULE_REGISTER(EC11, CONFIG_SENSOR_LOG_LEVEL); -static int ec11_get_ab_state(struct device *dev) -{ - struct ec11_data *drv_data = dev->driver_data; - const struct ec11_config *drv_cfg = dev->config_info; +static int ec11_get_ab_state(struct device *dev) { + struct ec11_data *drv_data = dev->driver_data; + const struct ec11_config *drv_cfg = dev->config_info; - return (gpio_pin_get(drv_data->a, drv_cfg->a_pin) << 1) | gpio_pin_get(drv_data->b, drv_cfg->b_pin); + return (gpio_pin_get(drv_data->a, drv_cfg->a_pin) << 1) | + gpio_pin_get(drv_data->b, drv_cfg->b_pin); } -static int ec11_sample_fetch(struct device *dev, enum sensor_channel chan) -{ - struct ec11_data *drv_data = dev->driver_data; - const struct ec11_config *drv_cfg = dev->config_info; - u8_t val; - s8_t delta; - - __ASSERT_NO_MSG(chan == SENSOR_CHAN_ALL || chan == SENSOR_CHAN_ROTATION); - - val = ec11_get_ab_state(dev); - - LOG_DBG("prev: %d, new: %d", drv_data->ab_state, val); - - switch(val | (drv_data->ab_state << 2)) { - case 0b0010: case 0b0100: case 0b1101: case 0b1011: - delta = -1; - break; - case 0b0001: case 0b0111: case 0b1110: case 0b1000: - delta = 1; - break; - default: - delta = 0; - break; - } - - LOG_DBG("Delta: %d", delta); - - drv_data->pulses += delta; - drv_data->ab_state = val; - - drv_data->ticks = drv_data->pulses / drv_cfg->resolution; - drv_data->delta = delta; - drv_data->pulses %= drv_cfg->resolution; - - return 0; +static int ec11_sample_fetch(struct device *dev, enum sensor_channel chan) { + struct ec11_data *drv_data = dev->driver_data; + const struct ec11_config *drv_cfg = dev->config_info; + u8_t val; + s8_t delta; + + __ASSERT_NO_MSG(chan == SENSOR_CHAN_ALL || chan == SENSOR_CHAN_ROTATION); + + val = ec11_get_ab_state(dev); + + LOG_DBG("prev: %d, new: %d", drv_data->ab_state, val); + + switch (val | (drv_data->ab_state << 2)) { + case 0b0010: + case 0b0100: + case 0b1101: + case 0b1011: + delta = -1; + break; + case 0b0001: + case 0b0111: + case 0b1110: + case 0b1000: + delta = 1; + break; + default: + delta = 0; + break; + } + + LOG_DBG("Delta: %d", delta); + + drv_data->pulses += delta; + drv_data->ab_state = val; + + drv_data->ticks = drv_data->pulses / drv_cfg->resolution; + drv_data->delta = delta; + drv_data->pulses %= drv_cfg->resolution; + + return 0; } -static int ec11_channel_get(struct device *dev, - enum sensor_channel chan, - struct sensor_value *val) -{ - struct ec11_data *drv_data = dev->driver_data; - - if (chan != SENSOR_CHAN_ROTATION) { - return -ENOTSUP; - } - - val->val1 = drv_data->ticks; - val->val2 = drv_data->delta; - - return 0; +static int ec11_channel_get(struct device *dev, enum sensor_channel chan, + struct sensor_value *val) { + struct ec11_data *drv_data = dev->driver_data; + + if (chan != SENSOR_CHAN_ROTATION) { + return -ENOTSUP; + } + + val->val1 = drv_data->ticks; + val->val2 = drv_data->delta; + + return 0; } static const struct sensor_driver_api ec11_driver_api = { #ifdef CONFIG_EC11_TRIGGER - .trigger_set = ec11_trigger_set, + .trigger_set = ec11_trigger_set, #endif - .sample_fetch = ec11_sample_fetch, - .channel_get = ec11_channel_get, + .sample_fetch = ec11_sample_fetch, + .channel_get = ec11_channel_get, }; -int ec11_init(struct device *dev) -{ - struct ec11_data *drv_data = dev->driver_data; - const struct ec11_config *drv_cfg = dev->config_info; - - LOG_DBG("A: %s %d B: %s %d resolution %d", drv_cfg->a_label, drv_cfg->a_pin, drv_cfg->b_label, drv_cfg->b_pin, drv_cfg->resolution); - - drv_data->a = device_get_binding(drv_cfg->a_label); - if (drv_data->a == NULL) { - LOG_ERR("Failed to get pointer to A GPIO device"); - return -EINVAL; - } +int ec11_init(struct device *dev) { + struct ec11_data *drv_data = dev->driver_data; + const struct ec11_config *drv_cfg = dev->config_info; - drv_data->b = device_get_binding(drv_cfg->b_label); - if (drv_data->b == NULL) { - LOG_ERR("Failed to get pointer to B GPIO device"); - return -EINVAL; - } + LOG_DBG("A: %s %d B: %s %d resolution %d", drv_cfg->a_label, drv_cfg->a_pin, drv_cfg->b_label, + drv_cfg->b_pin, drv_cfg->resolution); - if (gpio_pin_configure(drv_data->a, drv_cfg->a_pin, - drv_cfg->a_flags - | GPIO_INPUT)) { - LOG_DBG("Failed to configure A pin"); - return -EIO; - } + drv_data->a = device_get_binding(drv_cfg->a_label); + if (drv_data->a == NULL) { + LOG_ERR("Failed to get pointer to A GPIO device"); + return -EINVAL; + } - if (gpio_pin_configure(drv_data->b, drv_cfg->b_pin, - drv_cfg->b_flags - | GPIO_INPUT)) { - LOG_DBG("Failed to configure B pin"); - return -EIO; - } + drv_data->b = device_get_binding(drv_cfg->b_label); + if (drv_data->b == NULL) { + LOG_ERR("Failed to get pointer to B GPIO device"); + return -EINVAL; + } + if (gpio_pin_configure(drv_data->a, drv_cfg->a_pin, drv_cfg->a_flags | GPIO_INPUT)) { + LOG_DBG("Failed to configure A pin"); + return -EIO; + } + if (gpio_pin_configure(drv_data->b, drv_cfg->b_pin, drv_cfg->b_flags | GPIO_INPUT)) { + LOG_DBG("Failed to configure B pin"); + return -EIO; + } #ifdef CONFIG_EC11_TRIGGER - if (ec11_init_interrupt(dev) < 0) { - LOG_DBG("Failed to initialize interrupt!"); - return -EIO; - } + if (ec11_init_interrupt(dev) < 0) { + LOG_DBG("Failed to initialize interrupt!"); + return -EIO; + } #endif - drv_data->ab_state = ec11_get_ab_state(dev); + drv_data->ab_state = ec11_get_ab_state(dev); - return 0; + return 0; } -#define EC11_INST(n) \ - struct ec11_data ec11_data_##n; \ - const struct ec11_config ec11_cfg_##n = { \ - .a_label = DT_INST_GPIO_LABEL(n, a_gpios), \ - .a_pin = DT_INST_GPIO_PIN(n, a_gpios), \ - .a_flags = DT_INST_GPIO_FLAGS(n, a_gpios), \ - .b_label = DT_INST_GPIO_LABEL(n, b_gpios), \ - .b_pin = DT_INST_GPIO_PIN(n, b_gpios), \ - .b_flags = DT_INST_GPIO_FLAGS(n, b_gpios), \ - COND_CODE_0(DT_INST_NODE_HAS_PROP(n, resolution), (1), (DT_INST_PROP(n, resolution))), \ - }; \ - DEVICE_AND_API_INIT(ec11, DT_INST_LABEL(n), ec11_init, \ - &ec11_data_##n, \ - &ec11_cfg_##n, POST_KERNEL, CONFIG_SENSOR_INIT_PRIORITY, \ - &ec11_driver_api); +#define EC11_INST(n) \ + struct ec11_data ec11_data_##n; \ + const struct ec11_config ec11_cfg_##n = { \ + .a_label = DT_INST_GPIO_LABEL(n, a_gpios), \ + .a_pin = DT_INST_GPIO_PIN(n, a_gpios), \ + .a_flags = DT_INST_GPIO_FLAGS(n, a_gpios), \ + .b_label = DT_INST_GPIO_LABEL(n, b_gpios), \ + .b_pin = DT_INST_GPIO_PIN(n, b_gpios), \ + .b_flags = DT_INST_GPIO_FLAGS(n, b_gpios), \ + COND_CODE_0(DT_INST_NODE_HAS_PROP(n, resolution), (1), (DT_INST_PROP(n, resolution))), \ + }; \ + DEVICE_AND_API_INIT(ec11_##n, DT_INST_LABEL(n), ec11_init, &ec11_data_##n, &ec11_cfg_##n, \ + POST_KERNEL, CONFIG_SENSOR_INIT_PRIORITY, &ec11_driver_api); DT_INST_FOREACH_STATUS_OKAY(EC11_INST)
\ No newline at end of file diff --git a/app/drivers/zephyr/ec11.h b/app/drivers/zephyr/ec11.h index 823d92d..e62e733 100644 --- a/app/drivers/zephyr/ec11.h +++ b/app/drivers/zephyr/ec11.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2020 Peter Johanson + * Copyright (c) 2020 The ZMK Contributors * * SPDX-License-Identifier: MIT */ @@ -11,39 +11,39 @@ #include <sys/util.h> struct ec11_config { - const char *a_label; - const u8_t a_pin; - const u8_t a_flags; + const char *a_label; + const u8_t a_pin; + const u8_t a_flags; - const char *b_label; - const u8_t b_pin; - const u8_t b_flags; + const char *b_label; + const u8_t b_pin; + const u8_t b_flags; - const u8_t resolution; + const u8_t resolution; }; struct ec11_data { - struct device *a; - struct device *b; - u8_t ab_state; - s8_t pulses; - s8_t ticks; - s8_t delta; + struct device *a; + struct device *b; + u8_t ab_state; + s8_t pulses; + s8_t ticks; + s8_t delta; #ifdef CONFIG_EC11_TRIGGER - struct gpio_callback a_gpio_cb; - struct gpio_callback b_gpio_cb; - struct device *dev; + struct gpio_callback a_gpio_cb; + struct gpio_callback b_gpio_cb; + struct device *dev; - sensor_trigger_handler_t handler; - const struct sensor_trigger *trigger; + sensor_trigger_handler_t handler; + const struct sensor_trigger *trigger; #if defined(CONFIG_EC11_TRIGGER_OWN_THREAD) - K_THREAD_STACK_MEMBER(thread_stack, CONFIG_EC11_THREAD_STACK_SIZE); - struct k_sem gpio_sem; - struct k_thread thread; + K_THREAD_STACK_MEMBER(thread_stack, CONFIG_EC11_THREAD_STACK_SIZE); + struct k_sem gpio_sem; + struct k_thread thread; #elif defined(CONFIG_EC11_TRIGGER_GLOBAL_THREAD) - struct k_work work; + struct k_work work; #endif #endif /* CONFIG_EC11_TRIGGER */ @@ -51,9 +51,8 @@ struct ec11_data { #ifdef CONFIG_EC11_TRIGGER -int ec11_trigger_set(struct device *dev, - const struct sensor_trigger *trig, - sensor_trigger_handler_t handler); +int ec11_trigger_set(struct device *dev, const struct sensor_trigger *trig, + sensor_trigger_handler_t handler); int ec11_init_interrupt(struct device *dev); #endif diff --git a/app/drivers/zephyr/ec11_trigger.c b/app/drivers/zephyr/ec11_trigger.c index 709d1fb..248ac32 100644 --- a/app/drivers/zephyr/ec11_trigger.c +++ b/app/drivers/zephyr/ec11_trigger.c @@ -1,5 +1,5 @@ /* - * Copyright (c) 2016 Intel Corporation + * Copyright (c) 2020 The ZMK Contributors * * SPDX-License-Identifier: MIT */ @@ -19,158 +19,130 @@ extern struct ec11_data ec11_driver; #include <logging/log.h> LOG_MODULE_DECLARE(EC11, CONFIG_SENSOR_LOG_LEVEL); -static inline void setup_int(struct device *dev, - bool enable) -{ - struct ec11_data *data = dev->driver_data; - const struct ec11_config *cfg = dev->config_info; - - LOG_DBG("enabled %s", (enable ? "true" : "false")); - - if (gpio_pin_interrupt_configure(data->a, - cfg->a_pin, - enable - ? GPIO_INT_EDGE_BOTH - : GPIO_INT_DISABLE)) { - LOG_WRN("Unable to set A pin GPIO interrupt"); - } - - if (gpio_pin_interrupt_configure(data->b, - cfg->b_pin, - enable - ? GPIO_INT_EDGE_BOTH - : GPIO_INT_DISABLE)) { - LOG_WRN("Unable to set A pin GPIO interrupt"); - } +static inline void setup_int(struct device *dev, bool enable) { + struct ec11_data *data = dev->driver_data; + const struct ec11_config *cfg = dev->config_info; + + LOG_DBG("enabled %s", (enable ? "true" : "false")); + + if (gpio_pin_interrupt_configure(data->a, cfg->a_pin, + enable ? GPIO_INT_EDGE_BOTH : GPIO_INT_DISABLE)) { + LOG_WRN("Unable to set A pin GPIO interrupt"); + } + + if (gpio_pin_interrupt_configure(data->b, cfg->b_pin, + enable ? GPIO_INT_EDGE_BOTH : GPIO_INT_DISABLE)) { + LOG_WRN("Unable to set A pin GPIO interrupt"); + } } -static void ec11_a_gpio_callback(struct device *dev, - struct gpio_callback *cb, u32_t pins) -{ - struct ec11_data *drv_data = - CONTAINER_OF(cb, struct ec11_data, a_gpio_cb); +static void ec11_a_gpio_callback(struct device *dev, struct gpio_callback *cb, u32_t pins) { + struct ec11_data *drv_data = CONTAINER_OF(cb, struct ec11_data, a_gpio_cb); - LOG_DBG(""); + LOG_DBG(""); - setup_int(drv_data->dev, false); + setup_int(drv_data->dev, false); #if defined(CONFIG_EC11_TRIGGER_OWN_THREAD) - k_sem_give(&drv_data->gpio_sem); + k_sem_give(&drv_data->gpio_sem); #elif defined(CONFIG_EC11_TRIGGER_GLOBAL_THREAD) - k_work_submit(&drv_data->work); + k_work_submit(&drv_data->work); #endif } -static void ec11_b_gpio_callback(struct device *dev, - struct gpio_callback *cb, u32_t pins) -{ - struct ec11_data *drv_data = - CONTAINER_OF(cb, struct ec11_data, b_gpio_cb); +static void ec11_b_gpio_callback(struct device *dev, struct gpio_callback *cb, u32_t pins) { + struct ec11_data *drv_data = CONTAINER_OF(cb, struct ec11_data, b_gpio_cb); - LOG_DBG(""); + LOG_DBG(""); - setup_int(drv_data->dev, false); + setup_int(drv_data->dev, false); #if defined(CONFIG_EC11_TRIGGER_OWN_THREAD) - k_sem_give(&drv_data->gpio_sem); + k_sem_give(&drv_data->gpio_sem); #elif defined(CONFIG_EC11_TRIGGER_GLOBAL_THREAD) - k_work_submit(&drv_data->work); + k_work_submit(&drv_data->work); #endif } -static void ec11_thread_cb(void *arg) -{ - struct device *dev = arg; - struct ec11_data *drv_data = dev->driver_data; +static void ec11_thread_cb(void *arg) { + struct device *dev = arg; + struct ec11_data *drv_data = dev->driver_data; - drv_data->handler(dev, drv_data->trigger); + drv_data->handler(dev, drv_data->trigger); - setup_int(dev, true); + setup_int(dev, true); } #ifdef CONFIG_EC11_TRIGGER_OWN_THREAD -static void ec11_thread(int dev_ptr, int unused) -{ - struct device *dev = INT_TO_POINTER(dev_ptr); - struct ec11_data *drv_data = dev->driver_data; +static void ec11_thread(int dev_ptr, int unused) { + struct device *dev = INT_TO_POINTER(dev_ptr); + struct ec11_data *drv_data = dev->driver_data; - ARG_UNUSED(unused); + ARG_UNUSED(unused); - while (1) { - k_sem_take(&drv_data->gpio_sem, K_FOREVER); - ec11_thread_cb(dev); - } + while (1) { + k_sem_take(&drv_data->gpio_sem, K_FOREVER); + ec11_thread_cb(dev); + } } #endif #ifdef CONFIG_EC11_TRIGGER_GLOBAL_THREAD -static void ec11_work_cb(struct k_work *work) -{ - struct ec11_data *drv_data = - CONTAINER_OF(work, struct ec11_data, work); +static void ec11_work_cb(struct k_work *work) { + struct ec11_data *drv_data = CONTAINER_OF(work, struct ec11_data, work); - LOG_DBG(""); + LOG_DBG(""); - ec11_thread_cb(drv_data->dev); + ec11_thread_cb(drv_data->dev); } #endif -int ec11_trigger_set(struct device *dev, - const struct sensor_trigger *trig, - sensor_trigger_handler_t handler) -{ - struct ec11_data *drv_data = dev->driver_data; +int ec11_trigger_set(struct device *dev, const struct sensor_trigger *trig, + sensor_trigger_handler_t handler) { + struct ec11_data *drv_data = dev->driver_data; - setup_int(dev, false); + setup_int(dev, false); - k_msleep(5); + k_msleep(5); - drv_data->trigger = trig; - drv_data->handler = handler; + drv_data->trigger = trig; + drv_data->handler = handler; - setup_int(dev, true); + setup_int(dev, true); - return 0; + return 0; } -int ec11_init_interrupt(struct device *dev) -{ - struct ec11_data *drv_data = dev->driver_data; - const struct ec11_config *drv_cfg = dev->config_info; - - drv_data->dev = dev; - /* setup gpio interrupt */ +int ec11_init_interrupt(struct device *dev) { + struct ec11_data *drv_data = dev->driver_data; + const struct ec11_config *drv_cfg = dev->config_info; + drv_data->dev = dev; + /* setup gpio interrupt */ - gpio_init_callback(&drv_data->a_gpio_cb, - ec11_a_gpio_callback, - BIT(drv_cfg->a_pin)); + gpio_init_callback(&drv_data->a_gpio_cb, ec11_a_gpio_callback, BIT(drv_cfg->a_pin)); - if (gpio_add_callback(drv_data->a, &drv_data->a_gpio_cb) < 0) { - LOG_DBG("Failed to set A callback!"); - return -EIO; - } + if (gpio_add_callback(drv_data->a, &drv_data->a_gpio_cb) < 0) { + LOG_DBG("Failed to set A callback!"); + return -EIO; + } - gpio_init_callback(&drv_data->b_gpio_cb, - ec11_b_gpio_callback, - BIT(drv_cfg->b_pin)); + gpio_init_callback(&drv_data->b_gpio_cb, ec11_b_gpio_callback, BIT(drv_cfg->b_pin)); - if (gpio_add_callback(drv_data->b, &drv_data->b_gpio_cb) < 0) { - LOG_DBG("Failed to set B callback!"); - return -EIO; - } + if (gpio_add_callback(drv_data->b, &drv_data->b_gpio_cb) < 0) { + LOG_DBG("Failed to set B callback!"); + return -EIO; + } #if defined(CONFIG_EC11_TRIGGER_OWN_THREAD) - k_sem_init(&drv_data->gpio_sem, 0, UINT_MAX); + k_sem_init(&drv_data->gpio_sem, 0, UINT_MAX); - k_thread_create(&drv_data->thread, drv_data->thread_stack, - CONFIG_EC11_THREAD_STACK_SIZE, - (k_thread_entry_t)ec11_thread, dev, - 0, NULL, K_PRIO_COOP(CONFIG_EC11_THREAD_PRIORITY), - 0, K_NO_WAIT); + k_thread_create(&drv_data->thread, drv_data->thread_stack, CONFIG_EC11_THREAD_STACK_SIZE, + (k_thread_entry_t)ec11_thread, dev, 0, NULL, + K_PRIO_COOP(CONFIG_EC11_THREAD_PRIORITY), 0, K_NO_WAIT); #elif defined(CONFIG_EC11_TRIGGER_GLOBAL_THREAD) - k_work_init(&drv_data->work, ec11_work_cb); + k_work_init(&drv_data->work, ec11_work_cb); #endif - return 0; + return 0; } diff --git a/app/drivers/zephyr/kscan_gpio_direct.c b/app/drivers/zephyr/kscan_gpio_direct.c index a3aa8c4..4818c99 100644 --- a/app/drivers/zephyr/kscan_gpio_direct.c +++ b/app/drivers/zephyr/kscan_gpio_direct.c @@ -1,5 +1,5 @@ /* - * Copyright (c) 2020 Peter Johanson <peter@peterjohanson.com> + * Copyright (c) 2020 The ZMK Contributors * * SPDX-License-Identifier: MIT */ @@ -15,249 +15,216 @@ LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL); #if DT_HAS_COMPAT_STATUS_OKAY(DT_DRV_COMPAT) -struct kscan_gpio_item_config -{ - char *label; - gpio_pin_t pin; - gpio_flags_t flags; +struct kscan_gpio_item_config { + char *label; + gpio_pin_t pin; + gpio_flags_t flags; }; union work_reference { - struct k_delayed_work delayed; - struct k_work direct; + struct k_delayed_work delayed; + struct k_work direct; }; -struct kscan_gpio_config -{ - u8_t num_of_inputs; - u8_t debounce_period; - struct kscan_gpio_item_config inputs[]; +struct kscan_gpio_config { + u8_t num_of_inputs; + u8_t debounce_period; + struct kscan_gpio_item_config inputs[]; }; -struct kscan_gpio_data -{ -#if defined(CONFIG_ZMK_KSCAN_GPIO_POLLING) - struct k_timer poll_timer; -#endif /* defined(CONFIG_ZMK_KSCAN_GPIO_POLLING) */ - kscan_callback_t callback; - union work_reference work; - struct device *dev; - u32_t pin_state; - struct device *inputs[]; +struct kscan_gpio_data { +#if defined(CONFIG_ZMK_KSCAN_DIRECT_POLLING) + struct k_timer poll_timer; +#endif /* defined(CONFIG_ZMK_KSCAN_DIRECT_POLLING) */ + kscan_callback_t callback; + union work_reference work; + struct device *dev; + u32_t pin_state; + struct device *inputs[]; }; -static struct device **kscan_gpio_input_devices(struct device *dev) -{ - struct kscan_gpio_data *data = dev->driver_data; - return data->inputs; +static struct device **kscan_gpio_input_devices(struct device *dev) { + struct kscan_gpio_data *data = dev->driver_data; + return data->inputs; } -static const struct kscan_gpio_item_config *kscan_gpio_input_configs(struct device *dev) -{ - const struct kscan_gpio_config *cfg = dev->config_info; - return cfg->inputs; +static const struct kscan_gpio_item_config *kscan_gpio_input_configs(struct device *dev) { + const struct kscan_gpio_config *cfg = dev->config_info; + return cfg->inputs; } -#if !defined(CONFIG_ZMK_KSCAN_GPIO_POLLING) +#if !defined(CONFIG_ZMK_KSCAN_DIRECT_POLLING) -struct kscan_gpio_irq_callback -{ - union work_reference *work; - u8_t debounce_period; - struct gpio_callback callback; +struct kscan_gpio_irq_callback { + union work_reference *work; + u8_t debounce_period; + struct gpio_callback callback; }; -static int kscan_gpio_config_interrupts(struct device *dev, gpio_flags_t flags) -{ - const struct kscan_gpio_config *cfg = dev->config_info; - struct device **devices = kscan_gpio_input_devices(dev); - const struct kscan_gpio_item_config *configs = kscan_gpio_input_configs(dev); - - for (int i = 0; i < cfg->num_of_inputs; i++) - { - struct device *dev = devices[i]; - const struct kscan_gpio_item_config *cfg = &configs[i]; - - int err = gpio_pin_interrupt_configure(dev, cfg->pin, flags); - - if (err) - { - LOG_ERR("Unable to enable matrix GPIO interrupt"); - return err; - } - } - - return 0; +static int kscan_gpio_config_interrupts(struct device *dev, gpio_flags_t flags) { + const struct kscan_gpio_config *cfg = dev->config_info; + struct device **devices = kscan_gpio_input_devices(dev); + const struct kscan_gpio_item_config *configs = kscan_gpio_input_configs(dev); + + for (int i = 0; i < cfg->num_of_inputs; i++) { + struct device *dev = devices[i]; + const struct kscan_gpio_item_config *cfg = &configs[i]; + + int err = gpio_pin_interrupt_configure(dev, cfg->pin, flags); + + if (err) { + LOG_ERR("Unable to enable matrix GPIO interrupt"); + return err; + } + } + + return 0; } -static int kscan_gpio_direct_enable(struct device *dev) -{ - return kscan_gpio_config_interrupts(dev, - GPIO_INT_DEBOUNCE | GPIO_INT_EDGE_BOTH); +static int kscan_gpio_direct_enable(struct device *dev) { + return kscan_gpio_config_interrupts(dev, GPIO_INT_DEBOUNCE | GPIO_INT_EDGE_BOTH); } -static int kscan_gpio_direct_disable(struct device *dev) -{ - return kscan_gpio_config_interrupts(dev, - GPIO_INT_DISABLE); +static int kscan_gpio_direct_disable(struct device *dev) { + return kscan_gpio_config_interrupts(dev, GPIO_INT_DISABLE); } -static void kscan_gpio_irq_callback_handler(struct device *dev, - struct gpio_callback *cb, gpio_port_pins_t pin) -{ - struct kscan_gpio_irq_callback *data = - CONTAINER_OF(cb, struct kscan_gpio_irq_callback, callback); - - if (data->debounce_period > 0) { - k_delayed_work_cancel(&data->work->delayed); - k_delayed_work_submit(&data->work->delayed, K_MSEC(data->debounce_period)); - } else { - k_work_submit(&data->work->direct); - } +static void kscan_gpio_irq_callback_handler(struct device *dev, struct gpio_callback *cb, + gpio_port_pins_t pin) { + struct kscan_gpio_irq_callback *data = + CONTAINER_OF(cb, struct kscan_gpio_irq_callback, callback); + + if (data->debounce_period > 0) { + k_delayed_work_cancel(&data->work->delayed); + k_delayed_work_submit(&data->work->delayed, K_MSEC(data->debounce_period)); + } else { + k_work_submit(&data->work->direct); + } } -#else /* !defined(CONFIG_ZMK_KSCAN_GPIO_POLLING) */ +#else /* !defined(CONFIG_ZMK_KSCAN_DIRECT_POLLING) */ -static void kscan_gpio_timer_handler(struct k_timer *timer) -{ - struct kscan_gpio_data *data = - CONTAINER_OF(timer, struct kscan_gpio_data, poll_timer); +static void kscan_gpio_timer_handler(struct k_timer *timer) { + struct kscan_gpio_data *data = CONTAINER_OF(timer, struct kscan_gpio_data, poll_timer); - k_work_submit(&data->work.direct); + k_work_submit(&data->work.direct); } -static int kscan_gpio_direct_enable(struct device *dev) -{ - struct kscan_gpio_data *data = dev->driver_data; - k_timer_start(&data->poll_timer, K_MSEC(10), K_MSEC(10)); - return 0; +static int kscan_gpio_direct_enable(struct device *dev) { + struct kscan_gpio_data *data = dev->driver_data; + k_timer_start(&data->poll_timer, K_MSEC(10), K_MSEC(10)); + return 0; } -static int kscan_gpio_direct_disable(struct device *dev) -{ - struct kscan_gpio_data *data = dev->driver_data; - k_timer_stop(&data->poll_timer); - return 0; +static int kscan_gpio_direct_disable(struct device *dev) { + struct kscan_gpio_data *data = dev->driver_data; + k_timer_stop(&data->poll_timer); + return 0; } -#endif /* defined(CONFIG_ZMK_KSCAN_GPIO_POLLING) */ - -static int kscan_gpio_direct_configure(struct device *dev, kscan_callback_t callback) -{ - struct kscan_gpio_data *data = dev->driver_data; - if (!callback) - { - return -EINVAL; - } - data->callback = callback; - return 0; +#endif /* defined(CONFIG_ZMK_KSCAN_DIRECT_POLLING) */ + +static int kscan_gpio_direct_configure(struct device *dev, kscan_callback_t callback) { + struct kscan_gpio_data *data = dev->driver_data; + if (!callback) { + return -EINVAL; + } + data->callback = callback; + return 0; } -static int kscan_gpio_read(struct device *dev) -{ - struct kscan_gpio_data *data = dev->driver_data; - const struct kscan_gpio_config *cfg = dev->config_info; - u32_t read_state = data->pin_state; - for (int i = 0; i < cfg->num_of_inputs; i++) - { - struct device *in_dev = kscan_gpio_input_devices(dev)[i]; - const struct kscan_gpio_item_config *in_cfg = &kscan_gpio_input_configs(dev)[i]; - WRITE_BIT(read_state, i, gpio_pin_get(in_dev, in_cfg->pin) > 0); - } - for (int i = 0; i < cfg->num_of_inputs; i++) - { - bool prev_pressed = BIT(i) & data->pin_state; - bool pressed = BIT(i) & read_state; - if (pressed != prev_pressed) - { - LOG_DBG("Sending event at %d,%d state %s", - 0, i, (pressed ? "on" : "off")); - WRITE_BIT(data->pin_state, i, pressed); - data->callback(dev, 0, i, pressed); - } - } - return 0; +static int kscan_gpio_read(struct device *dev) { + struct kscan_gpio_data *data = dev->driver_data; + const struct kscan_gpio_config *cfg = dev->config_info; + u32_t read_state = data->pin_state; + for (int i = 0; i < cfg->num_of_inputs; i++) { + struct device *in_dev = kscan_gpio_input_devices(dev)[i]; + const struct kscan_gpio_item_config *in_cfg = &kscan_gpio_input_configs(dev)[i]; + WRITE_BIT(read_state, i, gpio_pin_get(in_dev, in_cfg->pin) > 0); + } + for (int i = 0; i < cfg->num_of_inputs; i++) { + bool prev_pressed = BIT(i) & data->pin_state; + bool pressed = BIT(i) & read_state; + if (pressed != prev_pressed) { + LOG_DBG("Sending event at %d,%d state %s", 0, i, (pressed ? "on" : "off")); + WRITE_BIT(data->pin_state, i, pressed); + data->callback(dev, 0, i, pressed); + } + } + return 0; } -static void kscan_gpio_work_handler(struct k_work *work) -{ - struct kscan_gpio_data *data = - CONTAINER_OF(work, struct kscan_gpio_data, work); - kscan_gpio_read(data->dev); +static void kscan_gpio_work_handler(struct k_work *work) { + struct kscan_gpio_data *data = CONTAINER_OF(work, struct kscan_gpio_data, work); + kscan_gpio_read(data->dev); } static const struct kscan_driver_api gpio_driver_api = { - .config = kscan_gpio_direct_configure, - .enable_callback = kscan_gpio_direct_enable, - .disable_callback = kscan_gpio_direct_disable, + .config = kscan_gpio_direct_configure, + .enable_callback = kscan_gpio_direct_enable, + .disable_callback = kscan_gpio_direct_disable, }; -#define KSCAN_DIRECT_INPUT_ITEM(i,n) \ - { \ - .label = DT_INST_GPIO_LABEL_BY_IDX(n, input_gpios, i), \ - .pin = DT_INST_GPIO_PIN_BY_IDX(n, input_gpios, i), \ - .flags = DT_INST_GPIO_FLAGS_BY_IDX(n, input_gpios, i), \ - }, +#define KSCAN_DIRECT_INPUT_ITEM(i, n) \ + { \ + .label = DT_INST_GPIO_LABEL_BY_IDX(n, input_gpios, i), \ + .pin = DT_INST_GPIO_PIN_BY_IDX(n, input_gpios, i), \ + .flags = DT_INST_GPIO_FLAGS_BY_IDX(n, input_gpios, i), \ + }, #define INST_INPUT_LEN(n) DT_INST_PROP_LEN(n, input_gpios) -#define GPIO_INST_INIT(n) \ - COND_CODE_0(CONFIG_ZMK_KSCAN_GPIO_POLLING, (static struct kscan_gpio_irq_callback \ - irq_callbacks_##n[INST_INPUT_LEN(n)];), ()) \ - static struct kscan_gpio_data kscan_gpio_data_##n = { \ - .inputs = { [INST_INPUT_LEN(n)-1] = NULL } \ - }; \ - static int kscan_gpio_init_##n(struct device *dev) \ - { \ - struct kscan_gpio_data *data = dev->driver_data; \ - const struct kscan_gpio_config *cfg = dev->config_info; \ - int err; \ - struct device **input_devices = kscan_gpio_input_devices(dev); \ - for (int i = 0; i < cfg->num_of_inputs; i++) \ - { \ - const struct kscan_gpio_item_config *in_cfg = &kscan_gpio_input_configs(dev)[i]; \ - input_devices[i] = device_get_binding(in_cfg->label); \ - if (!input_devices[i]) \ - { \ - LOG_ERR("Unable to find input GPIO device"); \ - return -EINVAL; \ - } \ - err = gpio_pin_configure(input_devices[i], in_cfg->pin, GPIO_INPUT | in_cfg->flags); \ - if (err) \ - { \ - LOG_ERR("Unable to configure pin %d on %s for input", in_cfg->pin, in_cfg->label); \ - return err; \ - } \ - COND_CODE_0(CONFIG_ZMK_KSCAN_GPIO_POLLING, \ - ( \ - irq_callbacks_##n[i].work = &data->work; \ - irq_callbacks_##n[i].debounce_period = cfg->debounce_period; \ - gpio_init_callback(&irq_callbacks_##n[i].callback, kscan_gpio_irq_callback_handler, BIT(in_cfg->pin)); \ - err = gpio_add_callback(input_devices[i], &irq_callbacks_##n[i].callback); \ - if (err) \ - { \ - LOG_ERR("Error adding the callback to the column device"); \ - return err; \ - } \ - ), ()) \ - } \ - data->dev = dev; \ - COND_CODE_1(CONFIG_ZMK_KSCAN_GPIO_POLLING, (k_timer_init(&data->poll_timer, kscan_gpio_timer_handler, NULL);), ( )) \ - if (cfg->debounce_period > 0) { \ - k_delayed_work_init(&data->work.delayed, kscan_gpio_work_handler); \ - } else { \ - k_work_init(&data->work.direct, kscan_gpio_work_handler); \ - } \ - return 0; \ - } \ - static const struct kscan_gpio_config kscan_gpio_config_##n = { \ - .inputs = { UTIL_LISTIFY(INST_INPUT_LEN(n), KSCAN_DIRECT_INPUT_ITEM, n) }, \ - .num_of_inputs = INST_INPUT_LEN(n), \ - .debounce_period = DT_INST_PROP(n, debounce_period) \ - }; \ - DEVICE_AND_API_INIT(kscan_gpio_##n, DT_INST_LABEL(n), kscan_gpio_init_##n, \ - &kscan_gpio_data_##n, &kscan_gpio_config_##n, \ - POST_KERNEL, CONFIG_ZMK_KSCAN_INIT_PRIORITY, \ - &gpio_driver_api); +#define GPIO_INST_INIT(n) \ + COND_CODE_0(IS_ENABLED(CONFIG_ZMK_KSCAN_DIRECT_POLLING), \ + (static struct kscan_gpio_irq_callback irq_callbacks_##n[INST_INPUT_LEN(n)];), ()) \ + static struct kscan_gpio_data kscan_gpio_data_##n = { \ + .inputs = {[INST_INPUT_LEN(n) - 1] = NULL}}; \ + static int kscan_gpio_init_##n(struct device *dev) { \ + struct kscan_gpio_data *data = dev->driver_data; \ + const struct kscan_gpio_config *cfg = dev->config_info; \ + int err; \ + struct device **input_devices = kscan_gpio_input_devices(dev); \ + for (int i = 0; i < cfg->num_of_inputs; i++) { \ + const struct kscan_gpio_item_config *in_cfg = &kscan_gpio_input_configs(dev)[i]; \ + input_devices[i] = device_get_binding(in_cfg->label); \ + if (!input_devices[i]) { \ + LOG_ERR("Unable to find input GPIO device"); \ + return -EINVAL; \ + } \ + err = gpio_pin_configure(input_devices[i], in_cfg->pin, GPIO_INPUT | in_cfg->flags); \ + if (err) { \ + LOG_ERR("Unable to configure pin %d on %s for input", in_cfg->pin, in_cfg->label); \ + return err; \ + } \ + COND_CODE_0( \ + IS_ENABLED(CONFIG_ZMK_KSCAN_DIRECT_POLLING), \ + (irq_callbacks_##n[i].work = &data->work; \ + irq_callbacks_##n[i].debounce_period = cfg->debounce_period; \ + gpio_init_callback(&irq_callbacks_##n[i].callback, \ + kscan_gpio_irq_callback_handler, BIT(in_cfg->pin)); \ + err = gpio_add_callback(input_devices[i], &irq_callbacks_##n[i].callback); \ + if (err) { \ + LOG_ERR("Error adding the callback to the column device"); \ + return err; \ + }), \ + ()) \ + } \ + data->dev = dev; \ + COND_CODE_1(IS_ENABLED(CONFIG_ZMK_KSCAN_DIRECT_POLLING), \ + (k_timer_init(&data->poll_timer, kscan_gpio_timer_handler, NULL);), ()) \ + if (cfg->debounce_period > 0) { \ + k_delayed_work_init(&data->work.delayed, kscan_gpio_work_handler); \ + } else { \ + k_work_init(&data->work.direct, kscan_gpio_work_handler); \ + } \ + return 0; \ + } \ + static const struct kscan_gpio_config kscan_gpio_config_##n = { \ + .inputs = {UTIL_LISTIFY(INST_INPUT_LEN(n), KSCAN_DIRECT_INPUT_ITEM, n)}, \ + .num_of_inputs = INST_INPUT_LEN(n), \ + .debounce_period = DT_INST_PROP(n, debounce_period)}; \ + DEVICE_AND_API_INIT(kscan_gpio_##n, DT_INST_LABEL(n), kscan_gpio_init_##n, \ + &kscan_gpio_data_##n, &kscan_gpio_config_##n, POST_KERNEL, \ + CONFIG_ZMK_KSCAN_INIT_PRIORITY, &gpio_driver_api); DT_INST_FOREACH_STATUS_OKAY(GPIO_INST_INIT) -#endif /* DT_HAS_COMPAT_STATUS_OKAY(DT_DRV_COMPAT) */
\ No newline at end of file +#endif /* DT_HAS_COMPAT_STATUS_OKAY(DT_DRV_COMPAT) */ diff --git a/app/drivers/zephyr/kscan_gpio_matrix.c b/app/drivers/zephyr/kscan_gpio_matrix.c index b19c492..ec4fb39 100644 --- a/app/drivers/zephyr/kscan_gpio_matrix.c +++ b/app/drivers/zephyr/kscan_gpio_matrix.c @@ -1,5 +1,5 @@ /* - * Copyright (c) 2020 Peter Johanson <peter@peterjohanson.com> + * Copyright (c) 2020 The ZMK Contributors * * SPDX-License-Identifier: MIT */ @@ -15,266 +15,279 @@ LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL); #if DT_HAS_COMPAT_STATUS_OKAY(DT_DRV_COMPAT) -struct kscan_gpio_item_config -{ - char *label; - gpio_pin_t pin; - gpio_flags_t flags; +struct kscan_gpio_item_config { + char *label; + gpio_pin_t pin; + gpio_flags_t flags; }; - -#define _KSCAN_GPIO_ITEM_CFG_INIT(n, prop, idx) \ - { \ - .label = DT_INST_GPIO_LABEL_BY_IDX(n, prop, idx), \ - .pin = DT_INST_GPIO_PIN_BY_IDX(n, prop, idx), \ - .flags = DT_INST_GPIO_FLAGS_BY_IDX(n, prop, idx), \ - }, +#define _KSCAN_GPIO_ITEM_CFG_INIT(n, prop, idx) \ + { \ + .label = DT_INST_GPIO_LABEL_BY_IDX(n, prop, idx), \ + .pin = DT_INST_GPIO_PIN_BY_IDX(n, prop, idx), \ + .flags = DT_INST_GPIO_FLAGS_BY_IDX(n, prop, idx), \ + }, #define _KSCAN_GPIO_ROW_CFG_INIT(idx, n) _KSCAN_GPIO_ITEM_CFG_INIT(n, row_gpios, idx) #define _KSCAN_GPIO_COL_CFG_INIT(idx, n) _KSCAN_GPIO_ITEM_CFG_INIT(n, col_gpios, idx) - + +#if !defined(CONFIG_ZMK_KSCAN_MATRIX_POLLING) static int kscan_gpio_config_interrupts(struct device **devices, - const struct kscan_gpio_item_config *configs, - size_t len, gpio_flags_t flags) -{ - for (int i = 0; i < len; i++) - { - struct device *dev = devices[i]; - const struct kscan_gpio_item_config *cfg = &configs[i]; + const struct kscan_gpio_item_config *configs, size_t len, + gpio_flags_t flags) { + for (int i = 0; i < len; i++) { + struct device *dev = devices[i]; + const struct kscan_gpio_item_config *cfg = &configs[i]; - int err = gpio_pin_interrupt_configure(dev, cfg->pin, flags); + int err = gpio_pin_interrupt_configure(dev, cfg->pin, flags); - if (err) - { - LOG_ERR("Unable to enable matrix GPIO interrupt"); - return err; - } - } + if (err) { + LOG_ERR("Unable to enable matrix GPIO interrupt"); + return err; + } + } - return 0; + return 0; } +#endif + #define INST_MATRIX_ROWS(n) DT_INST_PROP_LEN(n, row_gpios) #define INST_MATRIX_COLS(n) DT_INST_PROP_LEN(n, col_gpios) -#define INST_OUTPUT_LEN(n) COND_CODE_0(DT_ENUM_IDX(DT_DRV_INST(n), diode_direction), (INST_MATRIX_ROWS(n)), (INST_MATRIX_COLS(n))) -#define INST_INPUT_LEN(n) COND_CODE_0(DT_ENUM_IDX(DT_DRV_INST(n), diode_direction), (INST_MATRIX_COLS(n)), (INST_MATRIX_ROWS(n))) +#define INST_OUTPUT_LEN(n) \ + COND_CODE_0(DT_ENUM_IDX(DT_DRV_INST(n), diode_direction), (INST_MATRIX_ROWS(n)), \ + (INST_MATRIX_COLS(n))) +#define INST_INPUT_LEN(n) \ + COND_CODE_0(DT_ENUM_IDX(DT_DRV_INST(n), diode_direction), (INST_MATRIX_COLS(n)), \ + (INST_MATRIX_ROWS(n))) -#define GPIO_INST_INIT(n) \ - struct kscan_gpio_irq_callback_##n \ - { \ - struct COND_CODE_0(DT_INST_PROP(n, debounce_period), (k_work), (k_delayed_work)) * work; \ - struct gpio_callback callback; \ - }; \ - static struct kscan_gpio_irq_callback_##n \ - irq_callbacks_##n[INST_INPUT_LEN(n)]; \ - struct kscan_gpio_config_##n \ - { \ - struct kscan_gpio_item_config rows[INST_MATRIX_ROWS(n)]; \ - struct kscan_gpio_item_config cols[INST_MATRIX_COLS(n)]; \ - }; \ - struct kscan_gpio_data_##n \ - { \ - kscan_callback_t callback; \ - struct COND_CODE_0(DT_INST_PROP(n, debounce_period), (k_work), (k_delayed_work)) work; \ - bool matrix_state[INST_MATRIX_ROWS(n)][INST_MATRIX_COLS(n)]; \ - struct device *rows[INST_MATRIX_ROWS(n)]; \ - struct device *cols[INST_MATRIX_COLS(n)]; \ - struct device *dev; \ - }; \ - static struct device **kscan_gpio_input_devices_##n(struct device *dev) \ - { \ - struct kscan_gpio_data_##n *data = dev->driver_data; \ - return (COND_CODE_0(DT_ENUM_IDX(DT_DRV_INST(n), diode_direction), (data->cols), (data->rows))); \ - } \ - static const struct kscan_gpio_item_config *kscan_gpio_input_configs_##n(struct device *dev) \ - { \ - const struct kscan_gpio_config_##n *cfg = dev->config_info; \ - return ((COND_CODE_0(DT_ENUM_IDX(DT_DRV_INST(n), diode_direction), (cfg->cols), (cfg->rows)))); \ - } \ - static struct device **kscan_gpio_output_devices_##n(struct device *dev) \ - { \ - struct kscan_gpio_data_##n *data = dev->driver_data; \ - return (COND_CODE_0(DT_ENUM_IDX(DT_DRV_INST(n), diode_direction), (data->rows), (data->cols))); \ - } \ - static const struct kscan_gpio_item_config *kscan_gpio_output_configs_##n(struct device *dev) \ - { \ - const struct kscan_gpio_config_##n *cfg = dev->config_info; \ - return (COND_CODE_0(DT_ENUM_IDX(DT_DRV_INST(n), diode_direction), (cfg->rows), (cfg->cols))); \ - } \ - static int kscan_gpio_enable_interrupts_##n(struct device *dev) \ - { \ - return kscan_gpio_config_interrupts(kscan_gpio_input_devices_##n(dev), kscan_gpio_input_configs_##n(dev), INST_INPUT_LEN(n), \ - GPIO_INT_DEBOUNCE | GPIO_INT_EDGE_BOTH); \ - } \ - static int kscan_gpio_disable_interrupts_##n(struct device *dev) \ - { \ - return kscan_gpio_config_interrupts(kscan_gpio_input_devices_##n(dev), kscan_gpio_input_configs_##n(dev), INST_INPUT_LEN(n), \ - GPIO_INT_DISABLE); \ - } \ - static void kscan_gpio_set_output_state_##n(struct device *dev, int value) \ - { \ - for (int i = 0; i < INST_OUTPUT_LEN(n); i++) \ - { \ - struct device *in_dev = kscan_gpio_output_devices_##n(dev)[i]; \ - const struct kscan_gpio_item_config *cfg = &kscan_gpio_output_configs_##n(dev)[i]; \ - gpio_pin_set(in_dev, cfg->pin, value); \ - } \ - } \ - static void kscan_gpio_set_matrix_state_##n(bool state[INST_MATRIX_ROWS(n)][INST_MATRIX_COLS(n)], u32_t input_index, u32_t output_index, bool value) \ - { \ - state[COND_CODE_0(DT_ENUM_IDX(DT_DRV_INST(n), diode_direction), (output_index), (input_index))][COND_CODE_0(DT_ENUM_IDX(DT_DRV_INST(n), diode_direction), (input_index), (output_index))] = value; \ - } \ - static int kscan_gpio_read_##n(struct device *dev) \ - { \ - bool submit_follow_up_read = false; \ - struct kscan_gpio_data_##n *data = dev->driver_data; \ - static bool read_state[INST_MATRIX_ROWS(n)][INST_MATRIX_COLS(n)]; \ - /* Disable our interrupts temporarily while we scan, to avoid */ \ - /* re-entry while we iterate columns and set them active one by one */ \ - /* to get pressed state for each matrix cell. */ \ - kscan_gpio_disable_interrupts_##n(dev); \ - kscan_gpio_set_output_state_##n(dev, 0); \ - for (int o = 0; o < INST_OUTPUT_LEN(n); o++) \ - { \ - struct device *out_dev = kscan_gpio_output_devices_##n(dev)[o]; \ - const struct kscan_gpio_item_config *out_cfg = &kscan_gpio_output_configs_##n(dev)[o]; \ - gpio_pin_set(out_dev, out_cfg->pin, 1); \ - for (int i = 0; i < INST_INPUT_LEN(n); i++) \ - { \ - struct device *in_dev = kscan_gpio_input_devices_##n(dev)[i]; \ - const struct kscan_gpio_item_config *in_cfg = &kscan_gpio_input_configs_##n(dev)[i]; \ - kscan_gpio_set_matrix_state_##n(read_state, i, o, gpio_pin_get(in_dev, in_cfg->pin) > 0); \ - } \ - gpio_pin_set(out_dev, out_cfg->pin, 0); \ - } \ - /* Set all our outputs as active again, then re-enable interrupts, */ \ - /* so we can trigger interrupts again for future press/release */ \ - kscan_gpio_set_output_state_##n(dev, 1); \ - kscan_gpio_enable_interrupts_##n(dev); \ - for (int r = 0; r < INST_MATRIX_ROWS(n); r++) \ - { \ - for (int c = 0; c < INST_MATRIX_COLS(n); c++) \ - { \ - bool pressed = read_state[r][c]; \ - /* Follow up reads needed because further interrupts won't fire on already tripped input GPIO pins */ \ - submit_follow_up_read = (submit_follow_up_read || pressed); \ - if (pressed != data->matrix_state[r][c]) \ - { \ - LOG_DBG("Sending event at %d,%d state %s", \ - r, c, (pressed ? "on" : "off")); \ - data->matrix_state[r][c] = pressed; \ - data->callback(dev, r, c, pressed); \ - } \ - } \ - } \ - if (submit_follow_up_read) { \ - COND_CODE_0(DT_INST_PROP(n, debounce_period), \ - ({ k_work_submit(&data->work); }), \ - ({ \ - k_delayed_work_cancel(&data->work); \ - k_delayed_work_submit(&data->work, K_MSEC(5)); })) \ - } \ - return 0; \ - } \ - static void kscan_gpio_work_handler_##n(struct k_work *work) \ - { \ - struct kscan_gpio_data_##n *data = \ - CONTAINER_OF(work, struct kscan_gpio_data_##n, work); \ - kscan_gpio_read_##n(data->dev); \ - } \ - static void kscan_gpio_irq_callback_handler_##n(struct device *dev, \ - struct gpio_callback *cb, gpio_port_pins_t pin) \ - { \ - struct kscan_gpio_irq_callback_##n *data = \ - CONTAINER_OF(cb, struct kscan_gpio_irq_callback_##n, callback); \ - COND_CODE_0(DT_INST_PROP(n, debounce_period), \ - ({ k_work_submit(data->work); }), \ - ({ \ - k_delayed_work_cancel(data->work); \ - k_delayed_work_submit(data->work, K_MSEC(DT_INST_PROP(n, debounce_period))); })) \ - } \ - static struct kscan_gpio_data_##n kscan_gpio_data_##n = { \ - .rows = { [INST_MATRIX_ROWS(n)-1] = NULL}, \ - .cols = { [INST_MATRIX_COLS(n)-1] = NULL }\ - }; \ - static int kscan_gpio_configure_##n(struct device *dev, kscan_callback_t callback) \ - { \ - struct kscan_gpio_data_##n *data = dev->driver_data; \ - if (!callback) \ - { \ - return -EINVAL; \ - } \ - data->callback = callback; \ - return 0; \ - }; \ - static int kscan_gpio_enable_##n(struct device *dev) \ - { \ - int err = kscan_gpio_enable_interrupts_##n(dev); \ - if (err) { return err; } \ - return kscan_gpio_read_##n(dev); \ - }; \ - static int kscan_gpio_init_##n(struct device *dev) \ - { \ - struct kscan_gpio_data_##n *data = dev->driver_data; \ - int err; \ - struct device **input_devices = kscan_gpio_input_devices_##n(dev); \ - for (int i = 0; i < INST_INPUT_LEN(n); i++) \ - { \ - const struct kscan_gpio_item_config *in_cfg = &kscan_gpio_input_configs_##n(dev)[i]; \ - input_devices[i] = device_get_binding(in_cfg->label); \ - if (!input_devices[i]) \ - { \ - LOG_ERR("Unable to find input GPIO device"); \ - return -EINVAL; \ - } \ - err = gpio_pin_configure(input_devices[i], in_cfg->pin, GPIO_INPUT | in_cfg->flags); \ - if (err) \ - { \ - LOG_ERR("Unable to configure pin %d on %s for input", in_cfg->pin, in_cfg->label); \ - return err; \ - } \ - irq_callbacks_##n[i].work = &data->work; \ - gpio_init_callback(&irq_callbacks_##n[i].callback, kscan_gpio_irq_callback_handler_##n, BIT(in_cfg->pin)); \ - err = gpio_add_callback(input_devices[i], &irq_callbacks_##n[i].callback); \ - if (err) \ - { \ - LOG_ERR("Error adding the callback to the column device"); \ - return err; \ - } \ - } \ - struct device **output_devices = kscan_gpio_output_devices_##n(dev); \ - for (int o = 0; o < INST_OUTPUT_LEN(n); o++) \ - { \ - const struct kscan_gpio_item_config *out_cfg = &kscan_gpio_output_configs_##n(dev)[o]; \ - output_devices[o] = device_get_binding(out_cfg->label); \ - if (!output_devices[o]) \ - { \ - LOG_ERR("Unable to find output GPIO device"); \ - return -EINVAL; \ - } \ - err = gpio_pin_configure(output_devices[o], out_cfg->pin, GPIO_OUTPUT_ACTIVE | out_cfg->flags); \ - if (err) \ - { \ - LOG_ERR("Unable to configure pin %d on %s for output", out_cfg->pin, out_cfg->label); \ - return err; \ - } \ - } \ - data->dev = dev; \ - (COND_CODE_0(DT_INST_PROP(n, debounce_period), (k_work_init), (k_delayed_work_init)))(&data->work, kscan_gpio_work_handler_##n); \ - return 0; \ - } \ - static const struct kscan_driver_api gpio_driver_api_##n = { \ - .config = kscan_gpio_configure_##n, \ - .enable_callback = kscan_gpio_enable_##n, \ - .disable_callback = kscan_gpio_disable_interrupts_##n, \ - }; \ - static const struct kscan_gpio_config_##n kscan_gpio_config_##n = { \ - .rows = { UTIL_LISTIFY(INST_MATRIX_ROWS(n), _KSCAN_GPIO_ROW_CFG_INIT, n) }, \ - .cols = { UTIL_LISTIFY(INST_MATRIX_COLS(n), _KSCAN_GPIO_COL_CFG_INIT, n) }, \ - }; \ - DEVICE_AND_API_INIT(kscan_gpio_##n, DT_INST_LABEL(n), kscan_gpio_init_##n, \ - &kscan_gpio_data_##n, &kscan_gpio_config_##n, \ - APPLICATION, CONFIG_APPLICATION_INIT_PRIORITY, \ - &gpio_driver_api_##n); +#define GPIO_INST_INIT(n) \ + struct kscan_gpio_irq_callback_##n { \ + struct COND_CODE_0(DT_INST_PROP(n, debounce_period), (k_work), (k_delayed_work)) * work; \ + struct gpio_callback callback; \ + struct device *dev; \ + }; \ + static struct kscan_gpio_irq_callback_##n irq_callbacks_##n[INST_INPUT_LEN(n)]; \ + struct kscan_gpio_config_##n { \ + struct kscan_gpio_item_config rows[INST_MATRIX_ROWS(n)]; \ + struct kscan_gpio_item_config cols[INST_MATRIX_COLS(n)]; \ + }; \ + struct kscan_gpio_data_##n { \ + kscan_callback_t callback; \ + COND_CODE_1(CONFIG_ZMK_KSCAN_MATRIX_POLLING, (struct k_timer poll_timer;), ()) \ + struct COND_CODE_0(DT_INST_PROP(n, debounce_period), (k_work), (k_delayed_work)) work; \ + bool matrix_state[INST_MATRIX_ROWS(n)][INST_MATRIX_COLS(n)]; \ + struct device *rows[INST_MATRIX_ROWS(n)]; \ + struct device *cols[INST_MATRIX_COLS(n)]; \ + struct device *dev; \ + }; \ + static struct device **kscan_gpio_input_devices_##n(struct device *dev) { \ + struct kscan_gpio_data_##n *data = dev->driver_data; \ + return (COND_CODE_0(DT_ENUM_IDX(DT_DRV_INST(n), diode_direction), (data->cols), \ + (data->rows))); \ + } \ + static const struct kscan_gpio_item_config *kscan_gpio_input_configs_##n(struct device *dev) { \ + const struct kscan_gpio_config_##n *cfg = dev->config_info; \ + return (( \ + COND_CODE_0(DT_ENUM_IDX(DT_DRV_INST(n), diode_direction), (cfg->cols), (cfg->rows)))); \ + } \ + static struct device **kscan_gpio_output_devices_##n(struct device *dev) { \ + struct kscan_gpio_data_##n *data = dev->driver_data; \ + return (COND_CODE_0(DT_ENUM_IDX(DT_DRV_INST(n), diode_direction), (data->rows), \ + (data->cols))); \ + } \ + static const struct kscan_gpio_item_config *kscan_gpio_output_configs_##n( \ + struct device *dev) { \ + const struct kscan_gpio_config_##n *cfg = dev->config_info; \ + return ( \ + COND_CODE_0(DT_ENUM_IDX(DT_DRV_INST(n), diode_direction), (cfg->rows), (cfg->cols))); \ + } \ + COND_CODE_1(CONFIG_ZMK_KSCAN_MATRIX_POLLING, (), \ + ( \ + static int kscan_gpio_enable_interrupts_##n(struct device *dev) { \ + return kscan_gpio_config_interrupts( \ + kscan_gpio_input_devices_##n(dev), kscan_gpio_input_configs_##n(dev), \ + INST_INPUT_LEN(n), GPIO_INT_LEVEL_ACTIVE); \ + } static int kscan_gpio_disable_interrupts_##n(struct device *dev) { \ + return kscan_gpio_config_interrupts(kscan_gpio_input_devices_##n(dev), \ + kscan_gpio_input_configs_##n(dev), \ + INST_INPUT_LEN(n), GPIO_INT_DISABLE); \ + })) \ + static void kscan_gpio_set_output_state_##n(struct device *dev, int value) { \ + int err; \ + for (int i = 0; i < INST_OUTPUT_LEN(n); i++) { \ + struct device *in_dev = kscan_gpio_output_devices_##n(dev)[i]; \ + const struct kscan_gpio_item_config *cfg = &kscan_gpio_output_configs_##n(dev)[i]; \ + if ((err = gpio_pin_set(in_dev, cfg->pin, value))) { \ + LOG_DBG("FAILED TO SET OUTPUT %d to %d", cfg->pin, err); \ + } \ + } \ + } \ + static void kscan_gpio_set_matrix_state_##n( \ + bool state[INST_MATRIX_ROWS(n)][INST_MATRIX_COLS(n)], u32_t input_index, \ + u32_t output_index, bool value) { \ + state[COND_CODE_0(DT_ENUM_IDX(DT_DRV_INST(n), diode_direction), (output_index), \ + (input_index))] \ + [COND_CODE_0(DT_ENUM_IDX(DT_DRV_INST(n), diode_direction), (input_index), \ + (output_index))] = value; \ + } \ + static int kscan_gpio_read_##n(struct device *dev) { \ + bool submit_follow_up_read = false; \ + struct kscan_gpio_data_##n *data = dev->driver_data; \ + static bool read_state[INST_MATRIX_ROWS(n)][INST_MATRIX_COLS(n)]; \ + /* Disable our interrupts temporarily while we scan, to avoid */ \ + /* re-entry while we iterate columns and set them active one by one */ \ + /* to get pressed state for each matrix cell. */ \ + kscan_gpio_set_output_state_##n(dev, 0); \ + for (int o = 0; o < INST_OUTPUT_LEN(n); o++) { \ + struct device *out_dev = kscan_gpio_output_devices_##n(dev)[o]; \ + const struct kscan_gpio_item_config *out_cfg = &kscan_gpio_output_configs_##n(dev)[o]; \ + gpio_pin_set(out_dev, out_cfg->pin, 1); \ + for (int i = 0; i < INST_INPUT_LEN(n); i++) { \ + struct device *in_dev = kscan_gpio_input_devices_##n(dev)[i]; \ + const struct kscan_gpio_item_config *in_cfg = \ + &kscan_gpio_input_configs_##n(dev)[i]; \ + kscan_gpio_set_matrix_state_##n(read_state, i, o, \ + gpio_pin_get(in_dev, in_cfg->pin) > 0); \ + } \ + gpio_pin_set(out_dev, out_cfg->pin, 0); \ + } \ + /* Set all our outputs as active again. */ \ + kscan_gpio_set_output_state_##n(dev, 1); \ + for (int r = 0; r < INST_MATRIX_ROWS(n); r++) { \ + for (int c = 0; c < INST_MATRIX_COLS(n); c++) { \ + bool pressed = read_state[r][c]; \ + /* Follow up reads needed because further interrupts won't fire on already tripped \ + * input GPIO pins */ \ + submit_follow_up_read = (submit_follow_up_read || pressed); \ + if (pressed != data->matrix_state[r][c]) { \ + LOG_DBG("Sending event at %d,%d state %s", r, c, (pressed ? "on" : "off")); \ + data->matrix_state[r][c] = pressed; \ + data->callback(dev, r, c, pressed); \ + } \ + } \ + } \ + if (submit_follow_up_read) { \ + COND_CODE_0(DT_INST_PROP(n, debounce_period), ({ k_work_submit(&data->work); }), ({ \ + k_delayed_work_cancel(&data->work); \ + k_delayed_work_submit(&data->work, K_MSEC(5)); \ + })) \ + } else { \ + COND_CODE_1(CONFIG_ZMK_KSCAN_MATRIX_POLLING, (), \ + (kscan_gpio_enable_interrupts_##n(dev);)) \ + } \ + return 0; \ + } \ + static void kscan_gpio_work_handler_##n(struct k_work *work) { \ + struct kscan_gpio_data_##n *data = CONTAINER_OF(work, struct kscan_gpio_data_##n, work); \ + kscan_gpio_read_##n(data->dev); \ + } \ + static void kscan_gpio_irq_callback_handler_##n(struct device *dev, struct gpio_callback *cb, \ + gpio_port_pins_t pin) { \ + struct kscan_gpio_irq_callback_##n *data = \ + CONTAINER_OF(cb, struct kscan_gpio_irq_callback_##n, callback); \ + COND_CODE_1(CONFIG_ZMK_KSCAN_MATRIX_POLLING, (), \ + (kscan_gpio_disable_interrupts_##n(data->dev);)) \ + COND_CODE_0(DT_INST_PROP(n, debounce_period), ({ k_work_submit(data->work); }), ({ \ + k_delayed_work_cancel(data->work); \ + k_delayed_work_submit(data->work, \ + K_MSEC(DT_INST_PROP(n, debounce_period))); \ + })) \ + } \ + \ + static struct kscan_gpio_data_##n kscan_gpio_data_##n = { \ + .rows = {[INST_MATRIX_ROWS(n) - 1] = NULL}, .cols = {[INST_MATRIX_COLS(n) - 1] = NULL}}; \ + static int kscan_gpio_configure_##n(struct device *dev, kscan_callback_t callback) { \ + struct kscan_gpio_data_##n *data = dev->driver_data; \ + if (!callback) { \ + return -EINVAL; \ + } \ + data->callback = callback; \ + LOG_DBG("Configured GPIO %d", n); \ + return 0; \ + }; \ + static int kscan_gpio_enable_##n(struct device *dev) { \ + COND_CODE_1(CONFIG_ZMK_KSCAN_MATRIX_POLLING, \ + (struct kscan_gpio_data_##n *data = dev->driver_data; \ + k_timer_start(&data->poll_timer, K_MSEC(10), K_MSEC(10)); return 0;), \ + (int err = kscan_gpio_enable_interrupts_##n(dev); \ + if (err) { return err; } return kscan_gpio_read_##n(dev);)) \ + }; \ + static int kscan_gpio_disable_##n(struct device *dev) { \ + COND_CODE_1(CONFIG_ZMK_KSCAN_MATRIX_POLLING, \ + (struct kscan_gpio_data_##n *data = dev->driver_data; \ + k_timer_stop(&data->poll_timer); return 0;), \ + (return kscan_gpio_disable_interrupts_##n(dev);)) \ + }; \ + COND_CODE_1(CONFIG_ZMK_KSCAN_MATRIX_POLLING, \ + (static void kscan_gpio_timer_handler(struct k_timer *timer) { \ + struct kscan_gpio_data_##n *data = \ + CONTAINER_OF(timer, struct kscan_gpio_data_##n, poll_timer); \ + k_work_submit(&data->work.work); \ + }), \ + ()) \ + static int kscan_gpio_init_##n(struct device *dev) { \ + struct kscan_gpio_data_##n *data = dev->driver_data; \ + int err; \ + struct device **input_devices = kscan_gpio_input_devices_##n(dev); \ + for (int i = 0; i < INST_INPUT_LEN(n); i++) { \ + const struct kscan_gpio_item_config *in_cfg = &kscan_gpio_input_configs_##n(dev)[i]; \ + input_devices[i] = device_get_binding(in_cfg->label); \ + if (!input_devices[i]) { \ + LOG_ERR("Unable to find input GPIO device"); \ + return -EINVAL; \ + } \ + err = gpio_pin_configure(input_devices[i], in_cfg->pin, GPIO_INPUT | in_cfg->flags); \ + if (err) { \ + LOG_ERR("Unable to configure pin %d on %s for input", in_cfg->pin, in_cfg->label); \ + return err; \ + } else { \ + LOG_DBG("Configured pin %d on %s for input", in_cfg->pin, in_cfg->label); \ + } \ + irq_callbacks_##n[i].work = &data->work; \ + irq_callbacks_##n[i].dev = dev; \ + gpio_init_callback(&irq_callbacks_##n[i].callback, \ + kscan_gpio_irq_callback_handler_##n, BIT(in_cfg->pin)); \ + err = gpio_add_callback(input_devices[i], &irq_callbacks_##n[i].callback); \ + if (err) { \ + LOG_ERR("Error adding the callback to the column device"); \ + return err; \ + } \ + } \ + struct device **output_devices = kscan_gpio_output_devices_##n(dev); \ + for (int o = 0; o < INST_OUTPUT_LEN(n); o++) { \ + const struct kscan_gpio_item_config *out_cfg = &kscan_gpio_output_configs_##n(dev)[o]; \ + output_devices[o] = device_get_binding(out_cfg->label); \ + if (!output_devices[o]) { \ + LOG_ERR("Unable to find output GPIO device"); \ + return -EINVAL; \ + } \ + err = gpio_pin_configure(output_devices[o], out_cfg->pin, \ + GPIO_OUTPUT_ACTIVE | out_cfg->flags); \ + if (err) { \ + LOG_ERR("Unable to configure pin %d on %s for output", out_cfg->pin, \ + out_cfg->label); \ + return err; \ + } \ + } \ + data->dev = dev; \ + COND_CODE_1(CONFIG_ZMK_KSCAN_MATRIX_POLLING, \ + (k_timer_init(&data->poll_timer, kscan_gpio_timer_handler, NULL);), ()) \ + (COND_CODE_0(DT_INST_PROP(n, debounce_period), (k_work_init), (k_delayed_work_init)))( \ + &data->work, kscan_gpio_work_handler_##n); \ + return 0; \ + } \ + static const struct kscan_driver_api gpio_driver_api_##n = { \ + .config = kscan_gpio_configure_##n, \ + .enable_callback = kscan_gpio_enable_##n, \ + .disable_callback = kscan_gpio_disable_##n, \ + }; \ + static const struct kscan_gpio_config_##n kscan_gpio_config_##n = { \ + .rows = {UTIL_LISTIFY(INST_MATRIX_ROWS(n), _KSCAN_GPIO_ROW_CFG_INIT, n)}, \ + .cols = {UTIL_LISTIFY(INST_MATRIX_COLS(n), _KSCAN_GPIO_COL_CFG_INIT, n)}, \ + }; \ + DEVICE_AND_API_INIT(kscan_gpio_##n, DT_INST_LABEL(n), kscan_gpio_init_##n, \ + &kscan_gpio_data_##n, &kscan_gpio_config_##n, APPLICATION, \ + CONFIG_APPLICATION_INIT_PRIORITY, &gpio_driver_api_##n); DT_INST_FOREACH_STATUS_OKAY(GPIO_INST_INIT) diff --git a/app/dts/behaviors.dtsi b/app/dts/behaviors.dtsi index ab70bcc..36c918c 100644 --- a/app/dts/behaviors.dtsi +++ b/app/dts/behaviors.dtsi @@ -7,4 +7,6 @@ #include <behaviors/toggle_layer.dtsi> #include <behaviors/reset.dtsi> #include <behaviors/sensor_rotate_key_press.dtsi> -#include <behaviors/rgb_underglow.dtsi>
\ No newline at end of file +#include <behaviors/rgb_underglow.dtsi> +#include <behaviors/bluetooth.dtsi> +#include <behaviors/ext_power.dtsi> diff --git a/app/dts/behaviors/bluetooth.dtsi b/app/dts/behaviors/bluetooth.dtsi new file mode 100644 index 0000000..ea09f4a --- /dev/null +++ b/app/dts/behaviors/bluetooth.dtsi @@ -0,0 +1,9 @@ +/ { + behaviors { + bt: behavior_bluetooth { + compatible = "zmk,behavior-bluetooth"; + label = "BLUETOOTH"; + #binding-cells = <2>; + }; + }; +}; diff --git a/app/dts/behaviors/ext_power.dtsi b/app/dts/behaviors/ext_power.dtsi new file mode 100644 index 0000000..92f0035 --- /dev/null +++ b/app/dts/behaviors/ext_power.dtsi @@ -0,0 +1,9 @@ +/ { + behaviors { + ext_power: behavior_ext_power { + compatible = "zmk,behavior-ext-power"; + label = "EXT_POWER_BEHAVIOR"; + #binding-cells = <1>; + }; + }; +}; diff --git a/app/dts/behaviors/toggle_layer.dtsi b/app/dts/behaviors/toggle_layer.dtsi index 86a050d..4045396 100644 --- a/app/dts/behaviors/toggle_layer.dtsi +++ b/app/dts/behaviors/toggle_layer.dtsi @@ -1,10 +1,10 @@ /* - * Copyright (c) 2020 Cody McGinnis <brainwart@gmail.com> + * Copyright (c) 2020 The ZMK Contributors * * SPDX-License-Identifier: MIT */ - - / { + +/ { behaviors { tog: behavior_toggle_layer { compatible = "zmk,behavior-toggle-layer"; diff --git a/app/dts/bindings/behaviors/zmk,behavior-bluetooth.yaml b/app/dts/bindings/behaviors/zmk,behavior-bluetooth.yaml new file mode 100644 index 0000000..127ebe0 --- /dev/null +++ b/app/dts/bindings/behaviors/zmk,behavior-bluetooth.yaml @@ -0,0 +1,8 @@ +# Copyright (c) 2020, Peter Johanson +# SPDX-License-Identifier: MIT + +description: Bluetooth Behavior + +compatible: "zmk,behavior-bluetooth" + +include: two_param.yaml diff --git a/app/dts/bindings/behaviors/zmk,behavior-ext-power.yaml b/app/dts/bindings/behaviors/zmk,behavior-ext-power.yaml new file mode 100644 index 0000000..d86c6f9 --- /dev/null +++ b/app/dts/bindings/behaviors/zmk,behavior-ext-power.yaml @@ -0,0 +1,10 @@ +# +# Copyright (c) 2020, The ZMK Contributors +# SPDX-License-Identifier: MIT +# + +description: External power control Behavior + +compatible: "zmk,behavior-ext-power" + +include: one_param.yaml diff --git a/app/dts/bindings/behaviors/zmk,behavior-hold-tap.yaml b/app/dts/bindings/behaviors/zmk,behavior-hold-tap.yaml index a20578f..5f74e9a 100644 --- a/app/dts/bindings/behaviors/zmk,behavior-hold-tap.yaml +++ b/app/dts/bindings/behaviors/zmk,behavior-hold-tap.yaml @@ -1,4 +1,4 @@ -# Copyright (c) 2020, Cody McGinnis; Okke Formsma +# Copyright (c) 2020 The ZMK Contributors # SPDX-License-Identifier: MIT description: Hold or Tap behavior @@ -12,12 +12,12 @@ properties: type: phandles required: true tapping_term_ms: - type: int + type: int flavor: type: string required: false default: "hold-preferred" enum: - - "hold-preferred" - - "balanced" - - "tap-preferred"
\ No newline at end of file + - "hold-preferred" + - "balanced" + - "tap-preferred" diff --git a/app/dts/bindings/behaviors/zmk,behavior-rgb-underglow.yaml b/app/dts/bindings/behaviors/zmk,behavior-rgb-underglow.yaml index 6b6d5b0..2cb74b9 100644 --- a/app/dts/bindings/behaviors/zmk,behavior-rgb-underglow.yaml +++ b/app/dts/bindings/behaviors/zmk,behavior-rgb-underglow.yaml @@ -1,8 +1,8 @@ -# Copyright (c) 2020, Nick Winans +# Copyright (c) 2020 The ZMK Contributors # SPDX-License-Identifier: MIT description: RGB Underglow Action compatible: "zmk,behavior-rgb-underglow" -include: one_param.yaml
\ No newline at end of file +include: one_param.yaml diff --git a/app/dts/bindings/behaviors/zmk,behavior-sensor-rotate-key-press.yaml b/app/dts/bindings/behaviors/zmk,behavior-sensor-rotate-key-press.yaml index bbf3537..6b33910 100644 --- a/app/dts/bindings/behaviors/zmk,behavior-sensor-rotate-key-press.yaml +++ b/app/dts/bindings/behaviors/zmk,behavior-sensor-rotate-key-press.yaml @@ -1,4 +1,4 @@ -# Copyright (c) 2020, Pete Johanson +# Copyright (c) 2020, The ZMK Contributors # SPDX-License-Identifier: MIT description: Sensor rotate key press/release behavior diff --git a/app/dts/bindings/behaviors/zmk,behavior-toggle-layer.yaml b/app/dts/bindings/behaviors/zmk,behavior-toggle-layer.yaml index 065949f..0a9723f 100644 --- a/app/dts/bindings/behaviors/zmk,behavior-toggle-layer.yaml +++ b/app/dts/bindings/behaviors/zmk,behavior-toggle-layer.yaml @@ -1,4 +1,4 @@ -# Copyright (c) 2020, Cody McGinnis <brainwart@gmail.com> +# Copyright (c) 2020 The ZMK Contributors # SPDX-License-Identifier: MIT description: Toggle Layer diff --git a/app/dts/bindings/zmk,ext-power-generic.yaml b/app/dts/bindings/zmk,ext-power-generic.yaml new file mode 100644 index 0000000..5a38a09 --- /dev/null +++ b/app/dts/bindings/zmk,ext-power-generic.yaml @@ -0,0 +1,20 @@ +# +# Copyright (c) 2020, The ZMK Contributors +# SPDX-License-Identifier: MIT +# + +description: | + Generic driver for controlling the external power output + by toggling the control-gpio pin status + (Only in supported hardware) + +compatible: "zmk,ext-power-generic" + +properties: + control-gpios: + type: phandle-array + required: true + label: + type: string + required: true + diff --git a/app/dts/bindings/zmk,keymap-sensors.yaml b/app/dts/bindings/zmk,keymap-sensors.yaml index c56361d..86ae5c2 100644 --- a/app/dts/bindings/zmk,keymap-sensors.yaml +++ b/app/dts/bindings/zmk,keymap-sensors.yaml @@ -1,3 +1,8 @@ +# +# Copyright (c) 2020, The ZMK Contributors +# SPDX-License-Identifier: MIT +# + description: | Allows defining the collection of sensors bound in the keymap layers diff --git a/app/include/drivers/behavior.h b/app/include/drivers/behavior.h index c24d11b..cf259b1 100644 --- a/app/include/drivers/behavior.h +++ b/app/include/drivers/behavior.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2020 Peter Johanson + * Copyright (c) 2020 The ZMK Contributors * * SPDX-License-Identifier: MIT */ @@ -10,6 +10,7 @@ #include <stddef.h> #include <device.h> #include <zmk/keys.h> +#include <zmk/behavior.h> /** * @cond INTERNAL_HIDDEN @@ -19,19 +20,20 @@ * (Internal use only.) */ -typedef int (*behavior_keymap_binding_callback_t)(struct device *dev, u32_t position, u32_t param1, u32_t param2); -typedef int (*behavior_sensor_keymap_binding_callback_t)(struct device *dev, struct device *sensor, u32_t param1, u32_t param2); +typedef int (*behavior_keymap_binding_callback_t)(struct zmk_behavior_binding *binding, + struct zmk_behavior_binding_event event); +typedef int (*behavior_sensor_keymap_binding_callback_t)(struct zmk_behavior_binding *binding, + struct device *sensor); __subsystem struct behavior_driver_api { - behavior_keymap_binding_callback_t binding_pressed; - behavior_keymap_binding_callback_t binding_released; - behavior_sensor_keymap_binding_callback_t sensor_binding_triggered; + behavior_keymap_binding_callback_t binding_pressed; + behavior_keymap_binding_callback_t binding_released; + behavior_sensor_keymap_binding_callback_t sensor_binding_triggered; }; /** * @endcond */ - /** * @brief Handle the keymap binding being pressed * @param dev Pointer to the device structure for the driver instance. @@ -41,18 +43,19 @@ __subsystem struct behavior_driver_api { * @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); +__syscall int behavior_keymap_binding_pressed(struct zmk_behavior_binding *binding, + struct zmk_behavior_binding_event event); -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; +static inline int z_impl_behavior_keymap_binding_pressed(struct zmk_behavior_binding *binding, + struct zmk_behavior_binding_event event) { + struct device *dev = device_get_binding(binding->behavior_dev); + const struct behavior_driver_api *api = (const struct behavior_driver_api *)dev->driver_api; - if (api->binding_pressed == NULL) { - return -ENOTSUP; - } + if (api->binding_pressed == NULL) { + return -ENOTSUP; + } - return api->binding_pressed(dev, position, param1, param2); + return api->binding_pressed(binding, event); } /** @@ -63,18 +66,19 @@ static inline int z_impl_behavior_keymap_binding_pressed(struct device *dev, u32 * @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); +__syscall int behavior_keymap_binding_released(struct zmk_behavior_binding *binding, + struct zmk_behavior_binding_event event); -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; +static inline int z_impl_behavior_keymap_binding_released(struct zmk_behavior_binding *binding, + struct zmk_behavior_binding_event event) { + struct device *dev = device_get_binding(binding->behavior_dev); + const struct behavior_driver_api *api = (const struct behavior_driver_api *)dev->driver_api; - if (api->binding_released == NULL) { - return -ENOTSUP; - } + if (api->binding_released == NULL) { + return -ENOTSUP; + } - return api->binding_released(dev, position, param1, param2); + return api->binding_released(binding, event); } /** @@ -87,21 +91,22 @@ static inline int z_impl_behavior_keymap_binding_released(struct device *dev, u3 * @retval 0 If successful. * @retval Negative errno code if failure. */ -__syscall int behavior_sensor_keymap_binding_triggered(struct device *dev, struct device *sensor, u32_t param1, u32_t param2); +__syscall int behavior_sensor_keymap_binding_triggered(struct zmk_behavior_binding *binding, + struct device *sensor); -static inline int z_impl_behavior_sensor_keymap_binding_triggered(struct device *dev, struct device *sensor, u32_t param1, u32_t param2) -{ - const struct behavior_driver_api *api = - (const struct behavior_driver_api *)dev->driver_api; +static inline int +z_impl_behavior_sensor_keymap_binding_triggered(struct zmk_behavior_binding *binding, + struct device *sensor) { + struct device *dev = device_get_binding(binding->behavior_dev); + const struct behavior_driver_api *api = (const struct behavior_driver_api *)dev->driver_api; - if (api->sensor_binding_triggered == NULL) { - return -ENOTSUP; - } + if (api->sensor_binding_triggered == NULL) { + return -ENOTSUP; + } - return api->sensor_binding_triggered(dev, sensor, param1, param2); + return api->sensor_binding_triggered(binding, sensor); } - /** * @} */ diff --git a/app/include/drivers/ext_power.h b/app/include/drivers/ext_power.h new file mode 100644 index 0000000..6c1923e --- /dev/null +++ b/app/include/drivers/ext_power.h @@ -0,0 +1,104 @@ +/* + * Copyright (c) 2020 The ZMK Contributors + * + * SPDX-License-Identifier: MIT + */ + +#pragma once + +#include <zephyr/types.h> +#include <stddef.h> +#include <device.h> + +#ifdef __cplusplus +extern "C" { +#endif + +/** + * @cond INTERNAL_HIDDEN + * + * Behavior driver API definition and system call entry points. + * + * (Internal use only.) + */ + +typedef int (*ext_power_enable_t)(struct device *dev); +typedef int (*ext_power_disable_t)(struct device *dev); +typedef int (*ext_power_get_t)(struct device *dev); + +__subsystem struct ext_power_api { + ext_power_enable_t enable; + ext_power_disable_t disable; + ext_power_get_t get; +}; +/** + * @endcond + */ + +/** + * @brief Enable the external power output + * @param dev Pointer to the device structure for the driver instance. + * + * @retval 0 If successful. + * @retval Negative errno code if failure. + */ +__syscall int ext_power_enable(struct device *dev); + +static inline int z_impl_ext_power_enable(struct device *dev) { + const struct ext_power_api *api = (const struct ext_power_api *)dev->driver_api; + + if (api->enable == NULL) { + return -ENOTSUP; + } + + return api->enable(dev); +} + +/** + * @brief Disable the external power output + * @param dev Pointer to the device structure for the driver instance. + * + * @retval 0 If successful. + * @retval Negative errno code if failure. + */ +__syscall int ext_power_disable(struct device *dev); + +static inline int z_impl_ext_power_disable(struct device *dev) { + const struct ext_power_api *api = (const struct ext_power_api *)dev->driver_api; + + if (api->disable == NULL) { + return -ENOTSUP; + } + + return api->disable(dev); +} + +/** + * @brief Get the current status of the external power output + * @param dev Pointer to the device structure for the driver instance. + * + * @retval 0 If ext power is disabled. + * @retval 1 if ext power is enabled. + * @retval Negative errno code if failure. + */ +__syscall int ext_power_get(struct device *dev); + +static inline int z_impl_ext_power_get(struct device *dev) { + const struct ext_power_api *api = (const struct ext_power_api *)dev->driver_api; + + if (api->get == NULL) { + return -ENOTSUP; + } + + return api->get(dev); +} + +#ifdef __cplusplus +} +#endif + +/** + * @} + */ + +#include <syscalls/ext_power.h> diff --git a/app/include/dt-bindings/zmk/bt.h b/app/include/dt-bindings/zmk/bt.h new file mode 100644 index 0000000..8ca1060 --- /dev/null +++ b/app/include/dt-bindings/zmk/bt.h @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2020 The ZMK Contributors + * + * SPDX-License-Identifier: MIT + */ + +#define BT_CLR_CMD 0 +#define BT_NXT_CMD 1 +#define BT_PRV_CMD 2 +#define BT_SEL_CMD 3 +// #define BT_FULL_RESET_CMD 4 + +/* +Note: Some future commands will include additional parameters, so we +defines these aliases up front. +*/ + +#define BT_CLR BT_CLR_CMD 0 +#define BT_NXT BT_NXT_CMD 0 +#define BT_PRV BT_PRV_CMD 0 +#define BT_SEL BT_SEL_CMD diff --git a/app/include/dt-bindings/zmk/ext_power.h b/app/include/dt-bindings/zmk/ext_power.h new file mode 100644 index 0000000..2a3e846 --- /dev/null +++ b/app/include/dt-bindings/zmk/ext_power.h @@ -0,0 +1,13 @@ +/* + * Copyright (c) 2020 The ZMK Contributors + * + * SPDX-License-Identifier: MIT + */ + +#define EXT_POWER_OFF_CMD 0 +#define EXT_POWER_ON_CMD 1 +#define EXT_POWER_TOGGLE_CMD 2 + +#define EP_ON EXT_POWER_ON_CMD +#define EP_OFF EXT_POWER_OFF_CMD +#define EP_TOG EXT_POWER_TOGGLE_CMD diff --git a/app/include/dt-bindings/zmk/keys.h b/app/include/dt-bindings/zmk/keys.h index cd9261d..a3b2229 100644 --- a/app/include/dt-bindings/zmk/keys.h +++ b/app/include/dt-bindings/zmk/keys.h @@ -1,3 +1,8 @@ +/* + * Copyright (c) 2020 The ZMK Contributors + * + * SPDX-License-Identifier: MIT + */ #pragma once @@ -55,7 +60,7 @@ #define QUOT 0x34 #define GRAV 0x35 #define CMMA 0x36 -#define DOT 0x37 +#define DOT 0x37 #define FSLH 0x38 #define CLCK 0x39 #define F1 0x3A @@ -77,8 +82,8 @@ #define INS 0x49 #define HOME 0x4A #define PGUP 0x4B -#define DEL 0x4C -#define END 0x4D +#define DEL 0x4C +#define END 0x4D #define PGDN 0x4E #define RARW 0x4F #define LARW 0x50 diff --git a/app/include/dt-bindings/zmk/kscan-mock.h b/app/include/dt-bindings/zmk/kscan-mock.h index d481899..eff218b 100644 --- a/app/include/dt-bindings/zmk/kscan-mock.h +++ b/app/include/dt-bindings/zmk/kscan-mock.h @@ -1,3 +1,9 @@ +/* + * Copyright (c) 2020 The ZMK Contributors + * + * SPDX-License-Identifier: MIT + */ + #pragma once #define ZMK_MOCK_IS_PRESS(v) ((v & (0x01 << 31)) != 0) diff --git a/app/include/dt-bindings/zmk/matrix-transform.h b/app/include/dt-bindings/zmk/matrix-transform.h index 4fd3e6c..2989cb6 100644 --- a/app/include/dt-bindings/zmk/matrix-transform.h +++ b/app/include/dt-bindings/zmk/matrix-transform.h @@ -1,3 +1,8 @@ +/* + * Copyright (c) 2020 The ZMK Contributors + * + * SPDX-License-Identifier: MIT + */ #define KT_ROW(item) (item >> 8) #define KT_COL(item) (item & 0xFF) diff --git a/app/include/dt-bindings/zmk/reset.h b/app/include/dt-bindings/zmk/reset.h index b513649..2b3d876 100644 --- a/app/include/dt-bindings/zmk/reset.h +++ b/app/include/dt-bindings/zmk/reset.h @@ -1,12 +1,13 @@ /* - * Copyright (c) 2020 Peter Johanson <peter@peterjohanson.com> + * Copyright (c) 2020 The ZMK Contributors * * SPDX-License-Identifier: MIT */ -#define RST_WARM 0x00 -#define RST_COLD 0x01 +#define RST_WARM 0x00 +#define RST_COLD 0x01 -// AdaFruit nrf52 Bootloader Specific. See https://github.com/adafruit/Adafruit_nRF52_Bootloader/blob/d6b28e66053eea467166f44875e3c7ec741cb471/src/main.c#L107 +// AdaFruit nrf52 Bootloader Specific. See +// https://github.com/adafruit/Adafruit_nRF52_Bootloader/blob/d6b28e66053eea467166f44875e3c7ec741cb471/src/main.c#L107 -#define RST_UF2 0x57
\ No newline at end of file +#define RST_UF2 0x57
\ No newline at end of file diff --git a/app/include/dt-bindings/zmk/rgb.h b/app/include/dt-bindings/zmk/rgb.h index c2efda8..eb72180 100644 --- a/app/include/dt-bindings/zmk/rgb.h +++ b/app/include/dt-bindings/zmk/rgb.h @@ -1,3 +1,8 @@ +/* + * Copyright (c) 2020 The ZMK Contributors + * + * SPDX-License-Identifier: MIT + */ #define RGB_TOG 0 #define RGB_HUI 1 diff --git a/app/include/linker/zmk-events.ld b/app/include/linker/zmk-events.ld index 44063be..78d00bb 100644 --- a/app/include/linker/zmk-events.ld +++ b/app/include/linker/zmk-events.ld @@ -1,5 +1,5 @@ /* - * Copyright (c) 2020 Peter Johanson <peter@peterjohanson.com> + * Copyright (c) 2020 The ZMK Contributors * * SPDX-License-Identifier: MIT */ diff --git a/app/include/zmk/behavior.h b/app/include/zmk/behavior.h index 63edcc9..428ae24 100644 --- a/app/include/zmk/behavior.h +++ b/app/include/zmk/behavior.h @@ -1,3 +1,8 @@ +/* + * Copyright (c) 2020 The ZMK Contributors + * + * SPDX-License-Identifier: MIT + */ #pragma once @@ -5,4 +10,10 @@ struct zmk_behavior_binding { char *behavior_dev; u32_t param1; u32_t param2; +}; + +struct zmk_behavior_binding_event { + int layer; + u32_t position; + s64_t timestamp; };
\ No newline at end of file diff --git a/app/include/zmk/ble.h b/app/include/zmk/ble.h index e760ed7..1cf71a7 100644 --- a/app/include/zmk/ble.h +++ b/app/include/zmk/ble.h @@ -1,7 +1,25 @@ +/* + * Copyright (c) 2020 The ZMK Contributors + * + * SPDX-License-Identifier: MIT + */ #pragma once #include <zmk/keys.h> +#include <zmk/ble/profile.h> + +int zmk_ble_clear_bonds(); +int zmk_ble_prof_next(); +int zmk_ble_prof_prev(); +int zmk_ble_prof_select(u8_t index); + +bt_addr_le_t *zmk_ble_active_profile_addr(); +char *zmk_ble_active_profile_name(); int zmk_ble_unpair_all(); bool zmk_ble_handle_key_user(struct zmk_key_event *key_event); + +#if IS_ENABLED(CONFIG_ZMK_SPLIT_BLE_ROLE_CENTRAL) +void zmk_ble_set_peripheral_addr(bt_addr_le_t *addr); +#endif /* IS_ENABLED(CONFIG_ZMK_SPLIT_BLE_ROLE_CENTRAL) */
\ No newline at end of file diff --git a/app/include/zmk/ble/profile.h b/app/include/zmk/ble/profile.h new file mode 100644 index 0000000..1df2743 --- /dev/null +++ b/app/include/zmk/ble/profile.h @@ -0,0 +1,16 @@ +/* + * Copyright (c) 2020 The ZMK Contributors + * + * SPDX-License-Identifier: MIT + */ + +#pragma once + +#include <bluetooth/addr.h> + +#define ZMK_BLE_PROFILE_NAME_MAX 15 + +struct zmk_ble_profile { + char name[ZMK_BLE_PROFILE_NAME_MAX]; + bt_addr_le_t peer; +}; diff --git a/app/include/zmk/display.h b/app/include/zmk/display.h index 93ac3ff..def4392 100644 --- a/app/include/zmk/display.h +++ b/app/include/zmk/display.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2020 Peter Johanson <peter@peterjohanson.com> + * Copyright (c) 2020 The ZMK Contributors * * SPDX-License-Identifier: MIT */ diff --git a/app/include/zmk/endpoints.h b/app/include/zmk/endpoints.h index 4c0f442..aad6265 100644 --- a/app/include/zmk/endpoints.h +++ b/app/include/zmk/endpoints.h @@ -1,3 +1,9 @@ +/* + * Copyright (c) 2020 The ZMK Contributors + * + * SPDX-License-Identifier: MIT + */ + #pragma once #include <zmk/keys.h> diff --git a/app/include/zmk/event-manager.h b/app/include/zmk/event-manager.h index d9a56a4..4b8f72c 100644 --- a/app/include/zmk/event-manager.h +++ b/app/include/zmk/event-manager.h @@ -1,22 +1,21 @@ /* - * Copyright (c) 2020 Peter Johanson <peter@peterjohanson.com> + * Copyright (c) 2020 The ZMK Contributors * * SPDX-License-Identifier: MIT */ - + #pragma once #include <stddef.h> #include <kernel.h> #include <zephyr/types.h> -struct zmk_event_type -{ +struct zmk_event_type { const char *name; }; struct zmk_event_header { - const struct zmk_event_type* event; + const struct zmk_event_type *event; u8_t last_listener_index; }; @@ -24,8 +23,7 @@ struct zmk_event_header { #define ZMK_EV_EVENT_CAPTURED 2 typedef int (*zmk_listener_callback_t)(const struct zmk_event_header *eh); -struct zmk_listener -{ +struct zmk_listener { zmk_listener_callback_t callback; }; @@ -34,55 +32,50 @@ struct zmk_event_subscription { const struct zmk_listener *listener; }; -#define ZMK_EVENT_DECLARE(event_type) \ - struct event_type* new_##event_type(); \ - bool is_##event_type(const struct zmk_event_header *eh); \ - struct event_type* cast_##event_type(const struct zmk_event_header *eh); \ +#define ZMK_EVENT_DECLARE(event_type) \ + struct event_type *new_##event_type(); \ + bool is_##event_type(const struct zmk_event_header *eh); \ + struct event_type *cast_##event_type(const struct zmk_event_header *eh); \ extern const struct zmk_event_type zmk_event_##event_type; -#define ZMK_EVENT_IMPL(event_type) \ - const struct zmk_event_type zmk_event_##event_type = { \ - .name = STRINGIFY(event_type) \ - }; \ - const struct zmk_event_type* zmk_event_ref_##event_type __used __attribute__((__section__(".event_type"))) = &zmk_event_##event_type; \ - struct event_type* new_##event_type() { \ - struct event_type* ev = (struct event_type *) k_malloc(sizeof(struct event_type)); \ - ev->header.event = &zmk_event_##event_type; \ - return ev; \ - }; \ - bool is_##event_type(const struct zmk_event_header *eh) { \ - return eh->event == &zmk_event_##event_type; \ - }; \ - struct event_type* cast_##event_type(const struct zmk_event_header *eh) {\ - return (struct event_type*)eh; \ +#define ZMK_EVENT_IMPL(event_type) \ + const struct zmk_event_type zmk_event_##event_type = {.name = STRINGIFY(event_type)}; \ + const struct zmk_event_type *zmk_event_ref_##event_type __used \ + __attribute__((__section__(".event_type"))) = &zmk_event_##event_type; \ + struct event_type *new_##event_type() { \ + struct event_type *ev = (struct event_type *)k_malloc(sizeof(struct event_type)); \ + ev->header.event = &zmk_event_##event_type; \ + return ev; \ + }; \ + bool is_##event_type(const struct zmk_event_header *eh) { \ + return eh->event == &zmk_event_##event_type; \ + }; \ + struct event_type *cast_##event_type(const struct zmk_event_header *eh) { \ + return (struct event_type *)eh; \ }; - -#define ZMK_LISTENER(mod, cb) \ - const struct zmk_listener zmk_listener_##mod = { \ - .callback = cb \ - }; +#define ZMK_LISTENER(mod, cb) const struct zmk_listener zmk_listener_##mod = {.callback = cb}; -#define ZMK_SUBSCRIPTION(mod, ev_type) \ - const Z_DECL_ALIGN(struct zmk_event_subscription) _CONCAT(_CONCAT(zmk_event_sub_,mod),ev_type) __used __attribute__((__section__(".event_subscription"))) = { \ - .event_type = &zmk_event_##ev_type, \ - .listener = &zmk_listener_##mod, \ +#define ZMK_SUBSCRIPTION(mod, ev_type) \ + const Z_DECL_ALIGN(struct zmk_event_subscription) \ + _CONCAT(_CONCAT(zmk_event_sub_, mod), ev_type) __used \ + __attribute__((__section__(".event_subscription"))) = { \ + .event_type = &zmk_event_##ev_type, \ + .listener = &zmk_listener_##mod, \ }; -#define ZMK_EVENT_RAISE(ev) \ - zmk_event_manager_raise((struct zmk_event_header *)ev); +#define ZMK_EVENT_RAISE(ev) zmk_event_manager_raise((struct zmk_event_header *)ev); -#define ZMK_EVENT_RAISE_AFTER(ev, mod) \ +#define ZMK_EVENT_RAISE_AFTER(ev, mod) \ zmk_event_manager_raise_after((struct zmk_event_header *)ev, &zmk_listener_##mod); - -#define ZMK_EVENT_RAISE_AT(ev, mod) \ +#define ZMK_EVENT_RAISE_AT(ev, mod) \ zmk_event_manager_raise_at((struct zmk_event_header *)ev, &zmk_listener_##mod); -#define ZMK_EVENT_RELEASE(ev) \ - zmk_event_manager_release((struct zmk_event_header *)ev); +#define ZMK_EVENT_RELEASE(ev) zmk_event_manager_release((struct zmk_event_header *)ev); int zmk_event_manager_raise(struct zmk_event_header *event); -int zmk_event_manager_raise_after(struct zmk_event_header *event, const struct zmk_listener *listener); +int zmk_event_manager_raise_after(struct zmk_event_header *event, + const struct zmk_listener *listener); int zmk_event_manager_raise_at(struct zmk_event_header *event, const struct zmk_listener *listener); int zmk_event_manager_release(struct zmk_event_header *event); diff --git a/app/include/zmk/events/ble-active-profile-changed.h b/app/include/zmk/events/ble-active-profile-changed.h new file mode 100644 index 0000000..1e3a198 --- /dev/null +++ b/app/include/zmk/events/ble-active-profile-changed.h @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2020 The ZMK Contributors + * + * SPDX-License-Identifier: MIT + */ + +#pragma once + +#include <zephyr.h> +#include <zmk/event-manager.h> +#include <device.h> + +#include <zmk/ble/profile.h> + +struct ble_active_profile_changed { + struct zmk_event_header header; + u8_t index; + struct zmk_ble_profile *profile; +}; + +ZMK_EVENT_DECLARE(ble_active_profile_changed); diff --git a/app/include/zmk/events/keycode-state-changed.h b/app/include/zmk/events/keycode-state-changed.h index e12813c..1e2c24e 100644 --- a/app/include/zmk/events/keycode-state-changed.h +++ b/app/include/zmk/events/keycode-state-changed.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2020 Peter Johanson <peter@peterjohanson.com> + * Copyright (c) 2020 The ZMK Contributors * * SPDX-License-Identifier: MIT */ @@ -18,12 +18,11 @@ struct keycode_state_changed { ZMK_EVENT_DECLARE(keycode_state_changed); -inline struct keycode_state_changed* create_keycode_state_changed(u8_t usage_page, u32_t keycode, bool state) -{ - struct keycode_state_changed* ev = new_keycode_state_changed(); +inline struct keycode_state_changed *create_keycode_state_changed(u8_t usage_page, u32_t keycode, + bool state) { + struct keycode_state_changed *ev = new_keycode_state_changed(); ev->usage_page = usage_page; ev->keycode = keycode; ev->state = state; - return ev; }
\ No newline at end of file diff --git a/app/include/zmk/events/modifiers-state-changed.h b/app/include/zmk/events/modifiers-state-changed.h index a17a093..d2e02f8 100644 --- a/app/include/zmk/events/modifiers-state-changed.h +++ b/app/include/zmk/events/modifiers-state-changed.h @@ -1,9 +1,9 @@ /* - * Copyright (c) 2020 Peter Johanson <peter@peterjohanson.com> + * Copyright (c) 2020 The ZMK Contributors * * SPDX-License-Identifier: MIT */ - + #pragma once #include <zephyr.h> @@ -18,9 +18,9 @@ struct modifiers_state_changed { ZMK_EVENT_DECLARE(modifiers_state_changed); -inline struct modifiers_state_changed* create_modifiers_state_changed(zmk_mod_flags modifiers, bool state) -{ - struct modifiers_state_changed* ev = new_modifiers_state_changed(); +inline struct modifiers_state_changed *create_modifiers_state_changed(zmk_mod_flags modifiers, + bool state) { + struct modifiers_state_changed *ev = new_modifiers_state_changed(); ev->modifiers = modifiers; ev->state = state; diff --git a/app/include/zmk/events/position-state-changed.h b/app/include/zmk/events/position-state-changed.h index c91688e..e4cbbbe 100644 --- a/app/include/zmk/events/position-state-changed.h +++ b/app/include/zmk/events/position-state-changed.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2020 Peter Johanson <peter@peterjohanson.com> + * Copyright (c) 2020 The ZMK Contributors * * SPDX-License-Identifier: MIT */ @@ -13,6 +13,7 @@ struct position_state_changed { struct zmk_event_header header; u32_t position; bool state; + s64_t timestamp; }; ZMK_EVENT_DECLARE(position_state_changed);
\ No newline at end of file diff --git a/app/include/zmk/events/sensor-event.h b/app/include/zmk/events/sensor-event.h index f881ac5..a9302a5 100644 --- a/app/include/zmk/events/sensor-event.h +++ b/app/include/zmk/events/sensor-event.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2020 Peter Johanson <peter@peterjohanson.com> + * Copyright (c) 2020 The ZMK Contributors * * SPDX-License-Identifier: MIT */ diff --git a/app/include/zmk/handlers.h b/app/include/zmk/handlers.h index a10851f..7ce1d27 100644 --- a/app/include/zmk/handlers.h +++ b/app/include/zmk/handlers.h @@ -1,3 +1,9 @@ +/* + * Copyright (c) 2020 The ZMK Contributors + * + * SPDX-License-Identifier: MIT + */ + #pragma once #include <dt-bindings/zmk/keys.h> diff --git a/app/include/zmk/hid.h b/app/include/zmk/hid.h index 138606a..744de98 100644 --- a/app/include/zmk/hid.h +++ b/app/include/zmk/hid.h @@ -1,3 +1,9 @@ +/* + * Copyright (c) 2020 The ZMK Contributors + * + * SPDX-License-Identifier: MIT + */ + #pragma once #include <usb/usb_device.h> @@ -134,25 +140,21 @@ static const u8_t zmk_hid_report_desc[] = { // u8_t keys[6]; // } __packed; -struct zmk_hid_keypad_report_body -{ +struct zmk_hid_keypad_report_body { zmk_mod_flags modifiers; u8_t keys[13]; } __packed; -struct zmk_hid_keypad_report -{ +struct zmk_hid_keypad_report { u8_t report_id; struct zmk_hid_keypad_report_body body; } __packed; -struct zmk_hid_consumer_report_body -{ +struct zmk_hid_consumer_report_body { u8_t keys[6]; } __packed; -struct zmk_hid_consumer_report -{ +struct zmk_hid_consumer_report { u8_t report_id; struct zmk_hid_consumer_report_body body; } __packed; diff --git a/app/include/zmk/hog.h b/app/include/zmk/hog.h index e2f976a..a7dda51 100644 --- a/app/include/zmk/hog.h +++ b/app/include/zmk/hog.h @@ -1,3 +1,8 @@ +/* + * Copyright (c) 2020 The ZMK Contributors + * + * SPDX-License-Identifier: MIT + */ #pragma once diff --git a/app/include/zmk/keymap.h b/app/include/zmk/keymap.h index c1f6393..b8f4969 100644 --- a/app/include/zmk/keymap.h +++ b/app/include/zmk/keymap.h @@ -1,3 +1,9 @@ +/* + * Copyright (c) 2020 The ZMK Contributors + * + * SPDX-License-Identifier: MIT + */ + #pragma once bool zmk_keymap_layer_active(u8_t layer); @@ -5,4 +11,4 @@ int zmk_keymap_layer_activate(u8_t layer); int zmk_keymap_layer_deactivate(u8_t layer); int zmk_keymap_layer_toggle(u8_t layer); -int zmk_keymap_position_state_changed(u32_t position, bool pressed); +int zmk_keymap_position_state_changed(u32_t position, bool pressed, s64_t timestamp); diff --git a/app/include/zmk/keys.h b/app/include/zmk/keys.h index 6966bba..af474b1 100644 --- a/app/include/zmk/keys.h +++ b/app/include/zmk/keys.h @@ -1,3 +1,9 @@ +/* + * Copyright (c) 2020 The ZMK Contributors + * + * SPDX-License-Identifier: MIT + */ + #pragma once #include <zephyr.h> @@ -8,8 +14,7 @@ typedef u8_t zmk_action; typedef u8_t zmk_mod; typedef u8_t zmk_mod_flags; -struct zmk_key_event -{ +struct zmk_key_event { u32_t column; u32_t row; zmk_key key; diff --git a/app/include/zmk/kscan.h b/app/include/zmk/kscan.h index ebffccd..3352600 100644 --- a/app/include/zmk/kscan.h +++ b/app/include/zmk/kscan.h @@ -1,3 +1,9 @@ +/* + * Copyright (c) 2020 The ZMK Contributors + * + * SPDX-License-Identifier: MIT + */ + #pragma once int zmk_kscan_init(char *name); diff --git a/app/include/zmk/matrix.h b/app/include/zmk/matrix.h index 3088263..b3e2323 100644 --- a/app/include/zmk/matrix.h +++ b/app/include/zmk/matrix.h @@ -1,3 +1,9 @@ +/* + * Copyright (c) 2020 The ZMK Contributors + * + * SPDX-License-Identifier: MIT + */ + #pragma once #include <devicetree.h> @@ -9,20 +15,20 @@ #define ZMK_KEYMAP_TRANSFORM_NODE DT_CHOSEN(zmk_matrix_transform) #define ZMK_KEYMAP_LEN DT_PROP_LEN(ZMK_KEYMAP_TRANSFORM_NODE, map) -#define ZMK_MATRIX_ROWS DT_PROP(ZMK_KEYMAP_TRANSFORM_NODE,rows) -#define ZMK_MATRIX_COLS DT_PROP(ZMK_KEYMAP_TRANSFORM_NODE,columns) +#define ZMK_MATRIX_ROWS DT_PROP(ZMK_KEYMAP_TRANSFORM_NODE, rows) +#define ZMK_MATRIX_COLS DT_PROP(ZMK_KEYMAP_TRANSFORM_NODE, columns) #else /* DT_HAS_CHOSEN(zmk_matrix_transform) */ -#if DT_NODE_HAS_PROP(ZMK_MATRIX_NODE_ID,row_gpios) -#define ZMK_MATRIX_ROWS DT_PROP_LEN(ZMK_MATRIX_NODE_ID,row_gpios) -#define ZMK_MATRIX_COLS DT_PROP_LEN(ZMK_MATRIX_NODE_ID,col_gpios) -#elif DT_NODE_HAS_PROP(ZMK_MATRIX_NODE_ID,input_gpios) +#if DT_NODE_HAS_PROP(ZMK_MATRIX_NODE_ID, row_gpios) +#define ZMK_MATRIX_ROWS DT_PROP_LEN(ZMK_MATRIX_NODE_ID, row_gpios) +#define ZMK_MATRIX_COLS DT_PROP_LEN(ZMK_MATRIX_NODE_ID, col_gpios) +#elif DT_NODE_HAS_PROP(ZMK_MATRIX_NODE_ID, input_gpios) #define ZMK_MATRIX_ROWS 1 -#define ZMK_MATRIX_COLS DT_PROP_LEN(ZMK_MATRIX_NODE_ID,input_gpios) +#define ZMK_MATRIX_COLS DT_PROP_LEN(ZMK_MATRIX_NODE_ID, input_gpios) #else -#define ZMK_MATRIX_ROWS DT_PROP(ZMK_MATRIX_NODE_ID,rows) -#define ZMK_MATRIX_COLS DT_PROP(ZMK_MATRIX_NODE_ID,columns) +#define ZMK_MATRIX_ROWS DT_PROP(ZMK_MATRIX_NODE_ID, rows) +#define ZMK_MATRIX_COLS DT_PROP(ZMK_MATRIX_NODE_ID, columns) #endif #define ZMK_KEYMAP_LEN (ZMK_MATRIX_COLS * ZMK_MATRIX_ROWS) diff --git a/app/include/zmk/matrix_transform.h b/app/include/zmk/matrix_transform.h index e9e969a..29c2afc 100644 --- a/app/include/zmk/matrix_transform.h +++ b/app/include/zmk/matrix_transform.h @@ -1,3 +1,9 @@ +/* + * Copyright (c) 2020 The ZMK Contributors + * + * SPDX-License-Identifier: MIT + */ + #pragma once u32_t zmk_matrix_transform_row_column_to_position(u32_t row, u32_t column);
\ No newline at end of file diff --git a/app/include/zmk/rgb_underglow.h b/app/include/zmk/rgb_underglow.h index 69e9a9b..94cc32c 100644 --- a/app/include/zmk/rgb_underglow.h +++ b/app/include/zmk/rgb_underglow.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2020 Nick Winans <nick@winans.codes> + * Copyright (c) 2020 The ZMK Contributors * * SPDX-License-Identifier: MIT */ diff --git a/app/include/zmk/sensors.h b/app/include/zmk/sensors.h index c60cd4b..63dcade 100644 --- a/app/include/zmk/sensors.h +++ b/app/include/zmk/sensors.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2020 Peter Johanson <peter@peterjohanson.com> + * Copyright (c) 2020 The ZMK Contributors * * SPDX-License-Identifier: MIT */ diff --git a/app/include/zmk/split/bluetooth/service.h b/app/include/zmk/split/bluetooth/service.h index 954e0cd..c2be512 100644 --- a/app/include/zmk/split/bluetooth/service.h +++ b/app/include/zmk/split/bluetooth/service.h @@ -1,3 +1,9 @@ +/* + * Copyright (c) 2020 The ZMK Contributors + * + * SPDX-License-Identifier: MIT + */ + #pragma once int zmk_split_bt_position_pressed(u8_t position); diff --git a/app/include/zmk/split/bluetooth/uuid.h b/app/include/zmk/split/bluetooth/uuid.h index 59f2f71..a31884d 100644 --- a/app/include/zmk/split/bluetooth/uuid.h +++ b/app/include/zmk/split/bluetooth/uuid.h @@ -1,12 +1,17 @@ +/* + * Copyright (c) 2020 The ZMK Contributors + * + * SPDX-License-Identifier: MIT + */ + #pragma once #include <bluetooth/uuid.h> #ifndef BT_UUID_NUM_OF_DIGITALS -#define BT_UUID_NUM_OF_DIGITALS BT_UUID_DECLARE_16(0x2909) +#define BT_UUID_NUM_OF_DIGITALS BT_UUID_DECLARE_16(0x2909) #endif -#define ZMK_BT_SPLIT_UUID(num) BT_UUID_128_ENCODE(num, 0x0096, 0x7107, 0xc967, 0xc5cfb1c2482a) +#define ZMK_BT_SPLIT_UUID(num) BT_UUID_128_ENCODE(num, 0x0096, 0x7107, 0xc967, 0xc5cfb1c2482a) #define ZMK_SPLIT_BT_SERVICE_UUID ZMK_BT_SPLIT_UUID(0x00000000) #define ZMK_SPLIT_BT_CHAR_POSITION_STATE_UUID ZMK_BT_SPLIT_UUID(0x00000001) - diff --git a/app/include/zmk/usb.h b/app/include/zmk/usb.h new file mode 100644 index 0000000..452fd54 --- /dev/null +++ b/app/include/zmk/usb.h @@ -0,0 +1,19 @@ +/* + * Copyright (c) 2020 The ZMK Contributors + * + * SPDX-License-Identifier: MIT + */ + +#pragma once + +#include <usb/usb_device.h> +#include <usb/class/usb_hid.h> + +#include <zmk/keys.h> +#include <zmk/hid.h> + +enum usb_dc_status_code zmk_usb_get_status(); + +#ifdef CONFIG_ZMK_USB +int zmk_usb_hid_send_report(u8_t *report, size_t len); +#endif /* CONFIG_ZMK_USB */
\ No newline at end of file diff --git a/app/include/zmk/usb_hid.h b/app/include/zmk/usb_hid.h deleted file mode 100644 index 5d7c744..0000000 --- a/app/include/zmk/usb_hid.h +++ /dev/null @@ -1,14 +0,0 @@ -#ifndef ZMK_USB_HID -#define ZMK_USB_HID - -#include <usb/usb_device.h> -#include <usb/class/usb_hid.h> - -#include <zmk/keys.h> -#include <zmk/hid.h> - -int zmk_usb_hid_init(); - -int zmk_usb_hid_send_report(u8_t *report, size_t len); - -#endif diff --git a/app/run-test.sh b/app/run-test.sh index be335c6..dd8c8ee 100755 --- a/app/run-test.sh +++ b/app/run-test.sh @@ -1,6 +1,6 @@ #!/bin/sh # -# Copyright (c) 2020 Peter Johanson; Cody McGinnis +# Copyright (c) 2020 The ZMK Contributors # # SPDX-License-Identifier: MIT # @@ -22,6 +22,7 @@ echo "Running $testcase:" west build -d build/$testcase -b native_posix -- -DZMK_CONFIG=$testcase > /dev/null 2>&1 if [ $? -gt 0 ]; then echo "FAIL: $testcase did not build" >> ./build/tests/pass-fail.log + exit 1 else ./build/$testcase/zephyr/zmk.exe | sed -e "s/.*> //" | tee build/$testcase/keycode_events_full.log | sed -n -f $testcase/events.patterns > build/$testcase/keycode_events.log diff -au $testcase/keycode_events.snapshot build/$testcase/keycode_events.log diff --git a/app/src/behaviors/behavior_bt.c b/app/src/behaviors/behavior_bt.c new file mode 100644 index 0000000..066c437 --- /dev/null +++ b/app/src/behaviors/behavior_bt.c @@ -0,0 +1,51 @@ +/* + * Copyright (c) 2020 The ZMK Contributors + * + * SPDX-License-Identifier: MIT + */ + +#define DT_DRV_COMPAT zmk_behavior_bluetooth + +#include <device.h> +#include <drivers/behavior.h> +#include <dt-bindings/zmk/bt.h> +#include <bluetooth/conn.h> +#include <logging/log.h> +#include <zmk/behavior.h> + +LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL); + +#include <zmk/ble.h> + +static int on_keymap_binding_pressed(struct zmk_behavior_binding *binding, + struct zmk_behavior_binding_event event) { + switch (binding->param1) { + case BT_CLR_CMD: + return zmk_ble_clear_bonds(); + case BT_NXT_CMD: + return zmk_ble_prof_next(); + case BT_PRV_CMD: + return zmk_ble_prof_prev(); + case BT_SEL_CMD: + return zmk_ble_prof_select(binding->param2); + default: + LOG_ERR("Unknown BT command: %d", binding->param1); + } + + return -ENOTSUP; +} + +static int behavior_bt_init(struct device *dev) { return 0; }; + +static int on_keymap_binding_released(struct zmk_behavior_binding *binding, + struct zmk_behavior_binding_event event) { + return 0; +} + +static const struct behavior_driver_api behavior_bt_driver_api = { + .binding_pressed = on_keymap_binding_pressed, + .binding_released = on_keymap_binding_released, +}; + +DEVICE_AND_API_INIT(behavior_bt, DT_INST_LABEL(0), behavior_bt_init, NULL, NULL, APPLICATION, + CONFIG_KERNEL_INIT_PRIORITY_DEFAULT, &behavior_bt_driver_api); diff --git a/app/src/behaviors/behavior_ext_power.c b/app/src/behaviors/behavior_ext_power.c new file mode 100644 index 0000000..825f983 --- /dev/null +++ b/app/src/behaviors/behavior_ext_power.c @@ -0,0 +1,57 @@ +/* + * Copyright (c) 2020 The ZMK Contributors + * + * SPDX-License-Identifier: MIT + */ + +#define DT_DRV_COMPAT zmk_behavior_ext_power + +#include <device.h> +#include <devicetree.h> +#include <drivers/behavior.h> +#include <drivers/ext_power.h> + +#include <dt-bindings/zmk/ext_power.h> + +#include <logging/log.h> +LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL); + +static int on_keymap_binding_pressed(struct zmk_behavior_binding *binding, + struct zmk_behavior_binding_event event) { + struct device *ext_power = device_get_binding("EXT_POWER"); + if (ext_power == NULL) { + LOG_ERR("Unable to retrieve ext_power device: %d", binding->param1); + return -EIO; + } + + switch (binding->param1) { + case EXT_POWER_OFF_CMD: + return ext_power_disable(ext_power); + case EXT_POWER_ON_CMD: + return ext_power_enable(ext_power); + case EXT_POWER_TOGGLE_CMD: + if (ext_power_get(ext_power) > 0) + return ext_power_disable(ext_power); + else + return ext_power_enable(ext_power); + default: + LOG_ERR("Unknown ext_power command: %d", binding->param1); + } + + return -ENOTSUP; +} + +static int on_keymap_binding_released(struct zmk_behavior_binding *binding, + struct zmk_behavior_binding_event event) { + return 0; +} + +static int behavior_ext_power_init(struct device *dev) { return 0; }; + +static const struct behavior_driver_api behavior_ext_power_driver_api = { + .binding_pressed = on_keymap_binding_pressed, + .binding_released = on_keymap_binding_released, +}; + +DEVICE_AND_API_INIT(behavior_ext_power, DT_INST_LABEL(0), behavior_ext_power_init, NULL, NULL, + APPLICATION, CONFIG_APPLICATION_INIT_PRIORITY, &behavior_ext_power_driver_api); diff --git a/app/src/behaviors/behavior_hold_tap.c b/app/src/behaviors/behavior_hold_tap.c index 2c6d996..8b3620e 100644 --- a/app/src/behaviors/behavior_hold_tap.c +++ b/app/src/behaviors/behavior_hold_tap.c @@ -1,5 +1,5 @@ /* - * Copyright (c) 2020 Cody McGinnis, Okke Formsma + * Copyright (c) 2020 The ZMK Contributors * * SPDX-License-Identifier: MIT */ @@ -10,7 +10,6 @@ #include <drivers/behavior.h> #include <logging/log.h> #include <zmk/behavior.h> - #include <zmk/matrix.h> #include <zmk/endpoints.h> #include <zmk/event-manager.h> @@ -18,6 +17,7 @@ #include <zmk/events/keycode-state-changed.h> #include <zmk/events/modifiers-state-changed.h> #include <zmk/hid.h> +#include <zmk/behavior.h> LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL); @@ -29,36 +29,35 @@ LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL); // increase if you have keyboard with more keys. #define ZMK_BHV_HOLD_TAP_POSITION_NOT_USED 9999 - enum flavor { - ZMK_BHV_HOLD_TAP_FLAVOR_HOLD_PREFERRED = 0, - ZMK_BHV_HOLD_TAP_FLAVOR_BALANCED = 1, - ZMK_BHV_HOLD_TAP_FLAVOR_TAP_PREFERRED = 2, + ZMK_BHV_HOLD_TAP_FLAVOR_HOLD_PREFERRED = 0, + ZMK_BHV_HOLD_TAP_FLAVOR_BALANCED = 1, + ZMK_BHV_HOLD_TAP_FLAVOR_TAP_PREFERRED = 2, }; struct behavior_hold_tap_behaviors { - struct zmk_behavior_binding tap; - struct zmk_behavior_binding hold; + struct zmk_behavior_binding tap; + struct zmk_behavior_binding hold; }; -typedef k_timeout_t (*timer_func)(); - struct behavior_hold_tap_config { - timer_func tapping_term_ms; - struct behavior_hold_tap_behaviors *behaviors; - enum flavor flavor; + int tapping_term_ms; + struct behavior_hold_tap_behaviors *behaviors; + enum flavor flavor; }; // this data is specific for each hold-tap struct active_hold_tap { - s32_t position; - u32_t param_hold; - u32_t param_tap; - bool is_decided; - bool is_hold; - const struct behavior_hold_tap_config *config; - struct k_delayed_work work; - bool work_is_cancelled; + s32_t position; + // todo: move these params into the config->behaviors->tap and + u32_t param_hold; + u32_t param_tap; + s64_t timestamp; + bool is_decided; + bool is_hold; + const struct behavior_hold_tap_config *config; + struct k_delayed_work work; + bool work_is_cancelled; }; // The undecided hold tap is the hold tap that needs to be decided before @@ -71,371 +70,396 @@ struct active_hold_tap active_hold_taps[ZMK_BHV_HOLD_TAP_MAX_HELD] = {}; // We capture most position_state_changed events and some modifiers_state_changed events. const struct zmk_event_header *captured_events[ZMK_BHV_HOLD_TAP_MAX_CAPTURED_EVENTS] = {}; -static int capture_event(const struct zmk_event_header *event) -{ - for (int i = 0; i < ZMK_BHV_HOLD_TAP_MAX_CAPTURED_EVENTS; i++) { - if (captured_events[i] == NULL) { - captured_events[i] = event; - return 0; - } - } - return -ENOMEM; +static int capture_event(const struct zmk_event_header *event) { + for (int i = 0; i < ZMK_BHV_HOLD_TAP_MAX_CAPTURED_EVENTS; i++) { + if (captured_events[i] == NULL) { + captured_events[i] = event; + return 0; + } + } + return -ENOMEM; } -static struct position_state_changed *find_captured_keydown_event(u32_t position) -{ - struct position_state_changed *last_match = NULL; - for (int i = 0; i < ZMK_BHV_HOLD_TAP_MAX_CAPTURED_EVENTS; i++) { - const struct zmk_event_header *eh = captured_events[i]; - if (eh == NULL) { - return last_match; - } - if (!is_position_state_changed(eh)) { - continue; - } - struct position_state_changed *position_event = cast_position_state_changed(eh); - if (position_event->position == position && position_event->state) { - last_match = position_event; - } - } - return last_match; +static struct position_state_changed *find_captured_keydown_event(u32_t position) { + struct position_state_changed *last_match = NULL; + for (int i = 0; i < ZMK_BHV_HOLD_TAP_MAX_CAPTURED_EVENTS; i++) { + const struct zmk_event_header *eh = captured_events[i]; + if (eh == NULL) { + return last_match; + } + if (!is_position_state_changed(eh)) { + continue; + } + struct position_state_changed *position_event = cast_position_state_changed(eh); + if (position_event->position == position && position_event->state) { + last_match = position_event; + } + } + return last_match; } const struct zmk_listener zmk_listener_behavior_hold_tap; -static void release_captured_events() -{ - if (undecided_hold_tap != NULL) { - return; - } - - // We use a trick to prevent copying the captured_events array. - // - // Events for different mod-tap instances are separated by a NULL pointer. - // - // The first event popped will never be catched by the next active hold-tap - // because to start capturing a mod-tap-key-down event must first completely - // go through the events queue. - // - // Example of this release process; - // [mt2_down, k1_down, k1_up, mt2_up, null, ...] - // ^ - // mt2_down position event isn't captured because no hold-tap is active. - // mt2_down behavior event is handled, now we have an undecided hold-tap - // [null, k1_down, k1_up, mt2_up, null, ...] - // ^ - // k1_down is captured by the mt2 mod-tap - // !note that searches for find_captured_keydown_event by the mt2 behavior will stop at the first null encountered - // [mt1_down, null, k1_up, mt2_up, null, ...] - // ^ - // k1_up event is captured by the new hold-tap: - // [k1_down, k1_up, null, mt2_up, null, ...] - // ^ - // mt2_up event is not captured but causes release of mt2 behavior - // [k1_down, k1_up, null, null, null, ...] - // now mt2 will start releasing it's own captured positions. - for (int i = 0; i < ZMK_BHV_HOLD_TAP_MAX_CAPTURED_EVENTS; i++) { - const struct zmk_event_header *captured_event = captured_events[i]; - if (captured_event == NULL) { - return; - } - captured_events[i] = NULL; - if (undecided_hold_tap != NULL) { - k_msleep(10); - } - if (is_position_state_changed(captured_event)) { - struct position_state_changed *position_event = cast_position_state_changed(captured_event); - LOG_DBG("Releasing key position event for position %d %s", position_event->position, (position_event->state ? "pressed" : "released")); - } else { - struct keycode_state_changed *modifier_event = cast_keycode_state_changed(captured_event); - LOG_DBG("Releasing mods changed event 0x%02X %s", modifier_event->keycode, (modifier_event->state ? "pressed" : "released")); - } - ZMK_EVENT_RAISE_AT(captured_event, behavior_hold_tap); - } +static void release_captured_events() { + if (undecided_hold_tap != NULL) { + return; + } + + // We use a trick to prevent copying the captured_events array. + // + // Events for different mod-tap instances are separated by a NULL pointer. + // + // The first event popped will never be catched by the next active hold-tap + // because to start capturing a mod-tap-key-down event must first completely + // go through the events queue. + // + // Example of this release process; + // [mt2_down, k1_down, k1_up, mt2_up, null, ...] + // ^ + // mt2_down position event isn't captured because no hold-tap is active. + // mt2_down behavior event is handled, now we have an undecided hold-tap + // [null, k1_down, k1_up, mt2_up, null, ...] + // ^ + // k1_down is captured by the mt2 mod-tap + // !note that searches for find_captured_keydown_event by the mt2 behavior will stop at the + // first null encountered [mt1_down, null, k1_up, mt2_up, null, ...] + // ^ + // k1_up event is captured by the new hold-tap: + // [k1_down, k1_up, null, mt2_up, null, ...] + // ^ + // mt2_up event is not captured but causes release of mt2 behavior + // [k1_down, k1_up, null, null, null, ...] + // now mt2 will start releasing it's own captured positions. + for (int i = 0; i < ZMK_BHV_HOLD_TAP_MAX_CAPTURED_EVENTS; i++) { + const struct zmk_event_header *captured_event = captured_events[i]; + if (captured_event == NULL) { + return; + } + captured_events[i] = NULL; + if (undecided_hold_tap != NULL) { + k_msleep(10); + } + if (is_position_state_changed(captured_event)) { + struct position_state_changed *position_event = + cast_position_state_changed(captured_event); + LOG_DBG("Releasing key position event for position %d %s", position_event->position, + (position_event->state ? "pressed" : "released")); + } else { + struct keycode_state_changed *modifier_event = + cast_keycode_state_changed(captured_event); + LOG_DBG("Releasing mods changed event 0x%02X %s", modifier_event->keycode, + (modifier_event->state ? "pressed" : "released")); + } + ZMK_EVENT_RAISE_AT(captured_event, behavior_hold_tap); + } } -static struct active_hold_tap *find_hold_tap(u32_t position) -{ - for (int i = 0; i < ZMK_BHV_HOLD_TAP_MAX_HELD; i++) { - if (active_hold_taps[i].position == position) { - return &active_hold_taps[i]; - } - } - return NULL; +static struct active_hold_tap *find_hold_tap(u32_t position) { + for (int i = 0; i < ZMK_BHV_HOLD_TAP_MAX_HELD; i++) { + if (active_hold_taps[i].position == position) { + return &active_hold_taps[i]; + } + } + return NULL; } -static struct active_hold_tap *store_hold_tap(u32_t position, u32_t param_hold, u32_t param_tap, const struct behavior_hold_tap_config *config) -{ - for (int i = 0; i < ZMK_BHV_HOLD_TAP_MAX_HELD; i++) { - if (active_hold_taps[i].position != ZMK_BHV_HOLD_TAP_POSITION_NOT_USED) { - continue; - } - active_hold_taps[i].position = position; - active_hold_taps[i].is_decided = false; - active_hold_taps[i].is_hold = false; - active_hold_taps[i].config = config; - active_hold_taps[i].param_hold = param_hold; - active_hold_taps[i].param_tap = param_tap; - return &active_hold_taps[i]; - } - return NULL; +static struct active_hold_tap *store_hold_tap(u32_t position, u32_t param_hold, u32_t param_tap, + s64_t timestamp, + const struct behavior_hold_tap_config *config) { + for (int i = 0; i < ZMK_BHV_HOLD_TAP_MAX_HELD; i++) { + if (active_hold_taps[i].position != ZMK_BHV_HOLD_TAP_POSITION_NOT_USED) { + continue; + } + active_hold_taps[i].position = position; + active_hold_taps[i].is_decided = false; + active_hold_taps[i].is_hold = false; + active_hold_taps[i].config = config; + active_hold_taps[i].param_hold = param_hold; + active_hold_taps[i].param_tap = param_tap; + active_hold_taps[i].timestamp = timestamp; + return &active_hold_taps[i]; + } + return NULL; } -static void clear_hold_tap(struct active_hold_tap *hold_tap) -{ - hold_tap->position = ZMK_BHV_HOLD_TAP_POSITION_NOT_USED; - hold_tap->is_decided = false; - hold_tap->is_hold = false; - hold_tap->work_is_cancelled = false; +static void clear_hold_tap(struct active_hold_tap *hold_tap) { + hold_tap->position = ZMK_BHV_HOLD_TAP_POSITION_NOT_USED; + hold_tap->is_decided = false; + hold_tap->is_hold = false; + hold_tap->work_is_cancelled = false; } enum decision_moment { - HT_KEY_UP = 0, - HT_OTHER_KEY_DOWN = 1, - HT_OTHER_KEY_UP = 2, - HT_TIMER_EVENT = 3, + HT_KEY_UP = 0, + HT_OTHER_KEY_DOWN = 1, + HT_OTHER_KEY_UP = 2, + HT_TIMER_EVENT = 3, }; -static void decide_balanced(struct active_hold_tap *hold_tap, enum decision_moment event) -{ - switch (event) { - case HT_KEY_UP: - hold_tap->is_hold = 0; - hold_tap->is_decided = true; - break; - case HT_OTHER_KEY_UP: - case HT_TIMER_EVENT: - hold_tap->is_hold = 1; - hold_tap->is_decided = true; - break; - default: return; - } +static void decide_balanced(struct active_hold_tap *hold_tap, enum decision_moment event) { + switch (event) { + case HT_KEY_UP: + hold_tap->is_hold = 0; + hold_tap->is_decided = true; + break; + case HT_OTHER_KEY_UP: + case HT_TIMER_EVENT: + hold_tap->is_hold = 1; + hold_tap->is_decided = true; + break; + default: + return; + } } -static void decide_tap_preferred(struct active_hold_tap *hold_tap, enum decision_moment event) -{ - switch (event) { - case HT_KEY_UP: - hold_tap->is_hold = 0; - hold_tap->is_decided = true; - break; - case HT_TIMER_EVENT: - hold_tap->is_hold = 1; - hold_tap->is_decided = true; - break; - default: return; - } +static void decide_tap_preferred(struct active_hold_tap *hold_tap, enum decision_moment event) { + switch (event) { + case HT_KEY_UP: + hold_tap->is_hold = 0; + hold_tap->is_decided = true; + break; + case HT_TIMER_EVENT: + hold_tap->is_hold = 1; + hold_tap->is_decided = true; + break; + default: + return; + } } -static void decide_hold_preferred(struct active_hold_tap *hold_tap, enum decision_moment event) -{ - switch (event) { - case HT_KEY_UP: - hold_tap->is_hold = 0; - hold_tap->is_decided = true; - break; - case HT_OTHER_KEY_DOWN: - case HT_TIMER_EVENT: - hold_tap->is_hold = 1; - hold_tap->is_decided = true; - break; - default: return; - } +static void decide_hold_preferred(struct active_hold_tap *hold_tap, enum decision_moment event) { + switch (event) { + case HT_KEY_UP: + hold_tap->is_hold = 0; + hold_tap->is_decided = true; + break; + case HT_OTHER_KEY_DOWN: + case HT_TIMER_EVENT: + hold_tap->is_hold = 1; + hold_tap->is_decided = true; + break; + default: + return; + } } -static inline char* flavor_str(enum flavor flavor) { - switch(flavor) { - case ZMK_BHV_HOLD_TAP_FLAVOR_HOLD_PREFERRED: - return "hold-preferred"; - case ZMK_BHV_HOLD_TAP_FLAVOR_BALANCED: - return "balanced"; - case ZMK_BHV_HOLD_TAP_FLAVOR_TAP_PREFERRED: - return "tap-preferred"; - } - return "UNKNOWN FLAVOR"; +static inline char *flavor_str(enum flavor flavor) { + switch (flavor) { + case ZMK_BHV_HOLD_TAP_FLAVOR_HOLD_PREFERRED: + return "hold-preferred"; + case ZMK_BHV_HOLD_TAP_FLAVOR_BALANCED: + return "balanced"; + case ZMK_BHV_HOLD_TAP_FLAVOR_TAP_PREFERRED: + return "tap-preferred"; + } + return "UNKNOWN FLAVOR"; } -static void decide_hold_tap(struct active_hold_tap *hold_tap, enum decision_moment event) -{ - if (hold_tap->is_decided) { - return; - } - - if (hold_tap != undecided_hold_tap) { - LOG_DBG("ERROR found undecided tap hold that is not the active tap hold"); - return; - } - - switch(hold_tap->config->flavor) { - case ZMK_BHV_HOLD_TAP_FLAVOR_HOLD_PREFERRED: - decide_hold_preferred(hold_tap, event); - case ZMK_BHV_HOLD_TAP_FLAVOR_BALANCED: - decide_balanced(hold_tap, event); - case ZMK_BHV_HOLD_TAP_FLAVOR_TAP_PREFERRED: - decide_tap_preferred(hold_tap, event); - } - - if (!hold_tap->is_decided) { - return; - } - - LOG_DBG("%d decided %s (%s event %d)", - hold_tap->position, - hold_tap->is_hold ? "hold" : "tap", - flavor_str(hold_tap->config->flavor), - event); - undecided_hold_tap = NULL; - - struct zmk_behavior_binding *behavior; - if (hold_tap->is_hold) { - behavior = &hold_tap->config->behaviors->hold; - struct device *behavior_device = device_get_binding(behavior->behavior_dev); - behavior_keymap_binding_pressed(behavior_device, hold_tap->position, hold_tap->param_hold, 0); - } else { - behavior = &hold_tap->config->behaviors->tap; - struct device *behavior_device = device_get_binding(behavior->behavior_dev); - behavior_keymap_binding_pressed(behavior_device, hold_tap->position, hold_tap->param_tap, 0); - } - release_captured_events(); +static void decide_hold_tap(struct active_hold_tap *hold_tap, enum decision_moment event_type) { + if (hold_tap->is_decided) { + return; + } + + if (hold_tap != undecided_hold_tap) { + LOG_DBG("ERROR found undecided tap hold that is not the active tap hold"); + return; + } + + switch (hold_tap->config->flavor) { + case ZMK_BHV_HOLD_TAP_FLAVOR_HOLD_PREFERRED: + decide_hold_preferred(hold_tap, event_type); + case ZMK_BHV_HOLD_TAP_FLAVOR_BALANCED: + decide_balanced(hold_tap, event_type); + case ZMK_BHV_HOLD_TAP_FLAVOR_TAP_PREFERRED: + decide_tap_preferred(hold_tap, event_type); + } + + if (!hold_tap->is_decided) { + return; + } + + LOG_DBG("%d decided %s (%s event %d)", hold_tap->position, hold_tap->is_hold ? "hold" : "tap", + flavor_str(hold_tap->config->flavor), event_type); + undecided_hold_tap = NULL; + + struct zmk_behavior_binding_event event = { + .position = hold_tap->position, + .timestamp = hold_tap->timestamp, + }; + + struct zmk_behavior_binding binding; + if (hold_tap->is_hold) { + binding.behavior_dev = hold_tap->config->behaviors->hold.behavior_dev; + binding.param1 = hold_tap->param_hold; + binding.param2 = 0; + } else { + binding.behavior_dev = hold_tap->config->behaviors->tap.behavior_dev; + binding.param1 = hold_tap->param_tap; + binding.param2 = 0; + } + behavior_keymap_binding_pressed(&binding, event); + release_captured_events(); } -static int on_hold_tap_binding_pressed(struct device *dev, u32_t position, u32_t param_hold, u32_t param_tap) -{ - const struct behavior_hold_tap_config *cfg = dev->config_info; - - if (undecided_hold_tap != NULL) { - LOG_DBG("ERROR another hold-tap behavior is undecided."); - // if this happens, make sure the behavior events occur AFTER other position events. - return 0; - } - - struct active_hold_tap *hold_tap = store_hold_tap(position, param_hold, param_tap, cfg); - if (hold_tap == NULL) { - LOG_ERR("unable to store hold-tap info, did you press more than %d hold-taps?", ZMK_BHV_HOLD_TAP_MAX_HELD); - return 0; - } - - LOG_DBG("%d new undecided hold_tap", position); - undecided_hold_tap = hold_tap; - k_delayed_work_submit(&hold_tap->work, cfg->tapping_term_ms()); - - // todo: once we get timing info for keypresses, start the timer relative to the original keypress - // don't forget to simulate a timer-event before the event after that time was handled. - - return 0; +static int on_hold_tap_binding_pressed(struct zmk_behavior_binding *binding, + struct zmk_behavior_binding_event event) { + struct device *dev = device_get_binding(binding->behavior_dev); + const struct behavior_hold_tap_config *cfg = dev->config_info; + + if (undecided_hold_tap != NULL) { + LOG_DBG("ERROR another hold-tap behavior is undecided."); + // if this happens, make sure the behavior events occur AFTER other position events. + return 0; + } + + struct active_hold_tap *hold_tap = + store_hold_tap(event.position, binding->param1, binding->param2, event.timestamp, cfg); + if (hold_tap == NULL) { + LOG_ERR("unable to store hold-tap info, did you press more than %d hold-taps?", + ZMK_BHV_HOLD_TAP_MAX_HELD); + return 0; + } + + LOG_DBG("%d new undecided hold_tap", event.position); + undecided_hold_tap = hold_tap; + + // if this behavior was queued we have to adjust the timer to only + // wait for the remaining time. + s32_t tapping_term_ms_left = (hold_tap->timestamp + cfg->tapping_term_ms) - k_uptime_get(); + if (tapping_term_ms_left > 0) { + k_delayed_work_submit(&hold_tap->work, K_MSEC(tapping_term_ms_left)); + } + + return 0; } -static int on_hold_tap_binding_released(struct device *dev, u32_t position, u32_t _, u32_t __) -{ - struct active_hold_tap *hold_tap = find_hold_tap(position); - - if (hold_tap == NULL) { - LOG_ERR("ACTIVE_HOLD_TAP_CLEANED_UP_TOO_EARLY"); - return 0; - } - - int work_cancel_result = k_delayed_work_cancel(&hold_tap->work); - decide_hold_tap(hold_tap, HT_KEY_UP); - - struct zmk_behavior_binding *behavior; - if (hold_tap->is_hold) { - behavior = &hold_tap->config->behaviors->hold; - struct device *behavior_device = device_get_binding(behavior->behavior_dev); - behavior_keymap_binding_released(behavior_device, hold_tap->position, hold_tap->param_hold, 0); - } else { - behavior = &hold_tap->config->behaviors->tap; - struct device *behavior_device = device_get_binding(behavior->behavior_dev); - behavior_keymap_binding_released(behavior_device, hold_tap->position, hold_tap->param_tap, 0); - } - - - if (work_cancel_result == -EINPROGRESS) { - // let the timer handler clean up - // if we'd clear now, the timer may call back for an uninitialized active_hold_tap. - LOG_DBG("%d hold-tap timer work in event queue", position); - hold_tap->work_is_cancelled = true; - } else { - LOG_DBG("%d cleaning up hold-tap", position); - clear_hold_tap(hold_tap); - } - - return 0; +static int on_hold_tap_binding_released(struct zmk_behavior_binding *binding, + struct zmk_behavior_binding_event event) { + struct active_hold_tap *hold_tap = find_hold_tap(event.position); + if (hold_tap == NULL) { + LOG_ERR("ACTIVE_HOLD_TAP_CLEANED_UP_TOO_EARLY"); + return 0; + } + + // If these events were queued, the timer event may be queued too late or not at all. + // We insert a timer event before the TH_KEY_UP event to verify. + int work_cancel_result = k_delayed_work_cancel(&hold_tap->work); + if (event.timestamp > (hold_tap->timestamp + hold_tap->config->tapping_term_ms)) { + decide_hold_tap(hold_tap, HT_TIMER_EVENT); + } + + decide_hold_tap(hold_tap, HT_KEY_UP); + + // todo: set up the binding and data items inside of the active_hold_tap struct + struct zmk_behavior_binding_event sub_behavior_data = { + .position = hold_tap->position, + .timestamp = hold_tap->timestamp, + }; + + struct zmk_behavior_binding sub_behavior_binding; + if (hold_tap->is_hold) { + sub_behavior_binding.behavior_dev = hold_tap->config->behaviors->hold.behavior_dev; + sub_behavior_binding.param1 = hold_tap->param_hold; + sub_behavior_binding.param2 = 0; + } else { + sub_behavior_binding.behavior_dev = hold_tap->config->behaviors->tap.behavior_dev; + sub_behavior_binding.param1 = hold_tap->param_tap; + sub_behavior_binding.param2 = 0; + } + behavior_keymap_binding_released(&sub_behavior_binding, sub_behavior_data); + + if (work_cancel_result == -EINPROGRESS) { + // let the timer handler clean up + // if we'd clear now, the timer may call back for an uninitialized active_hold_tap. + LOG_DBG("%d hold-tap timer work in event queue", event.position); + hold_tap->work_is_cancelled = true; + } else { + LOG_DBG("%d cleaning up hold-tap", event.position); + clear_hold_tap(hold_tap); + } + + return 0; } static const struct behavior_driver_api behavior_hold_tap_driver_api = { - .binding_pressed = on_hold_tap_binding_pressed, - .binding_released = on_hold_tap_binding_released, + .binding_pressed = on_hold_tap_binding_pressed, + .binding_released = on_hold_tap_binding_released, }; - -static int position_state_changed_listener(const struct zmk_event_header *eh) -{ - struct position_state_changed *ev = cast_position_state_changed(eh); - - if (undecided_hold_tap == NULL) { - LOG_DBG("%d bubble (no undecided hold_tap active)", ev->position); - return 0; - } - - if (undecided_hold_tap->position == ev->position) { - if (ev->state) { // keydown - LOG_ERR("hold-tap listener should be called before before most other listeners!"); - return 0; - } else { // keyup - LOG_DBG("%d bubble undecided hold-tap keyrelease event", undecided_hold_tap->position); - return 0; - } - } - - if (!ev->state && find_captured_keydown_event(ev->position) == NULL) { - // no keydown event has been captured, let it bubble. - // we'll catch modifiers later in modifier_state_changed_listener - LOG_DBG("%d bubbling %d %s event", undecided_hold_tap->position, ev->position, ev->state ? "down" : "up"); - return 0; - } - - LOG_DBG("%d capturing %d %s event", undecided_hold_tap->position, ev->position, ev->state ? "down" : "up"); - capture_event(eh); - decide_hold_tap(undecided_hold_tap, ev->state ? HT_OTHER_KEY_DOWN : HT_OTHER_KEY_UP); - return ZMK_EV_EVENT_CAPTURED; +static int position_state_changed_listener(const struct zmk_event_header *eh) { + struct position_state_changed *ev = cast_position_state_changed(eh); + + if (undecided_hold_tap == NULL) { + LOG_DBG("%d bubble (no undecided hold_tap active)", ev->position); + return 0; + } + + if (undecided_hold_tap->position == ev->position) { + if (ev->state) { // keydown + LOG_ERR("hold-tap listener should be called before before most other listeners!"); + return 0; + } else { // keyup + LOG_DBG("%d bubble undecided hold-tap keyrelease event", undecided_hold_tap->position); + return 0; + } + } + + // If these events were queued, the timer event may be queued too late or not at all. + // We make a timer decision before the other key events are handled if the timer would + // have run out. + if (ev->timestamp > + (undecided_hold_tap->timestamp + undecided_hold_tap->config->tapping_term_ms)) { + decide_hold_tap(undecided_hold_tap, HT_TIMER_EVENT); + } + + if (!ev->state && find_captured_keydown_event(ev->position) == NULL) { + // no keydown event has been captured, let it bubble. + // we'll catch modifiers later in modifier_state_changed_listener + LOG_DBG("%d bubbling %d %s event", undecided_hold_tap->position, ev->position, + ev->state ? "down" : "up"); + return 0; + } + + LOG_DBG("%d capturing %d %s event", undecided_hold_tap->position, ev->position, + ev->state ? "down" : "up"); + capture_event(eh); + decide_hold_tap(undecided_hold_tap, ev->state ? HT_OTHER_KEY_DOWN : HT_OTHER_KEY_UP); + return ZMK_EV_EVENT_CAPTURED; } -static bool is_mod(struct keycode_state_changed *ev) -{ - return ev->usage_page == USAGE_KEYPAD && ev->keycode >= LCTL && ev->keycode <= RGUI; +static bool is_mod(struct keycode_state_changed *ev) { + return ev->usage_page == USAGE_KEYPAD && ev->keycode >= LCTL && ev->keycode <= RGUI; } -static int keycode_state_changed_listener(const struct zmk_event_header *eh) -{ - // we want to catch layer-up events too... how? - struct keycode_state_changed *ev = cast_keycode_state_changed(eh); - - if (undecided_hold_tap == NULL) { - // LOG_DBG("0x%02X bubble (no undecided hold_tap active)", ev->keycode); - return 0; - } - - if (!is_mod(ev)) { - // LOG_DBG("0x%02X bubble (not a mod)", ev->keycode); - return 0; - } - - // only key-up events will bubble through position_state_changed_listener - // if a undecided_hold_tap is active. - LOG_DBG("%d capturing 0x%02X %s event", undecided_hold_tap->position, ev->keycode, ev->state ? "down" : "up"); - capture_event(eh); - return ZMK_EV_EVENT_CAPTURED; +static int keycode_state_changed_listener(const struct zmk_event_header *eh) { + // we want to catch layer-up events too... how? + struct keycode_state_changed *ev = cast_keycode_state_changed(eh); + + if (undecided_hold_tap == NULL) { + // LOG_DBG("0x%02X bubble (no undecided hold_tap active)", ev->keycode); + return 0; + } + + if (!is_mod(ev)) { + // LOG_DBG("0x%02X bubble (not a mod)", ev->keycode); + return 0; + } + + // only key-up events will bubble through position_state_changed_listener + // if a undecided_hold_tap is active. + LOG_DBG("%d capturing 0x%02X %s event", undecided_hold_tap->position, ev->keycode, + ev->state ? "down" : "up"); + capture_event(eh); + return ZMK_EV_EVENT_CAPTURED; } - -int behavior_hold_tap_listener(const struct zmk_event_header *eh) -{ - if (is_position_state_changed(eh)) { - return position_state_changed_listener(eh); - } else if (is_keycode_state_changed(eh)) { - return keycode_state_changed_listener(eh); - } - return 0; +int behavior_hold_tap_listener(const struct zmk_event_header *eh) { + if (is_position_state_changed(eh)) { + return position_state_changed_listener(eh); + } else if (is_keycode_state_changed(eh)) { + return keycode_state_changed_listener(eh); + } + return 0; } ZMK_LISTENER(behavior_hold_tap, behavior_hold_tap_listener); @@ -443,60 +467,54 @@ ZMK_SUBSCRIPTION(behavior_hold_tap, position_state_changed); // this should be modifiers_state_changed, but unfrotunately that's not implemented yet. ZMK_SUBSCRIPTION(behavior_hold_tap, keycode_state_changed); -void behavior_hold_tap_timer_work_handler(struct k_work *item) -{ - struct active_hold_tap *hold_tap = CONTAINER_OF(item, struct active_hold_tap, work); +void behavior_hold_tap_timer_work_handler(struct k_work *item) { + struct active_hold_tap *hold_tap = CONTAINER_OF(item, struct active_hold_tap, work); - if (hold_tap->work_is_cancelled) { - clear_hold_tap(hold_tap); - } else { - decide_hold_tap(hold_tap, HT_TIMER_EVENT); - } + if (hold_tap->work_is_cancelled) { + clear_hold_tap(hold_tap); + } else { + decide_hold_tap(hold_tap, HT_TIMER_EVENT); + } } -static int behavior_hold_tap_init(struct device *dev) -{ - static bool init_first_run = true; - - if (init_first_run) { - for (int i = 0; i < ZMK_BHV_HOLD_TAP_MAX_HELD; i++) { - k_delayed_work_init(&active_hold_taps[i].work, behavior_hold_tap_timer_work_handler); - active_hold_taps[i].position = ZMK_BHV_HOLD_TAP_POSITION_NOT_USED; - } - } - init_first_run = false; - return 0; +static int behavior_hold_tap_init(struct device *dev) { + static bool init_first_run = true; + + if (init_first_run) { + for (int i = 0; i < ZMK_BHV_HOLD_TAP_MAX_HELD; i++) { + k_delayed_work_init(&active_hold_taps[i].work, behavior_hold_tap_timer_work_handler); + active_hold_taps[i].position = ZMK_BHV_HOLD_TAP_POSITION_NOT_USED; + } + } + init_first_run = false; + return 0; } struct behavior_hold_tap_data {}; static struct behavior_hold_tap_data behavior_hold_tap_data; -#define _TRANSFORM_ENTRY(idx, node) \ - { \ - .behavior_dev = DT_LABEL(DT_INST_PHANDLE_BY_IDX(node, bindings, idx)), \ - .param1 = COND_CODE_0(DT_INST_PHA_HAS_CELL_AT_IDX(node, bindings, idx, param1), (0), (DT_INST_PHA_BY_IDX(node, bindings, idx, param1))), \ - .param2 = COND_CODE_0(DT_INST_PHA_HAS_CELL_AT_IDX(node, bindings, idx, param2), (0), (DT_INST_PHA_BY_IDX(node, bindings, idx, param2))), \ - }, - -#define KP_INST(n) \ - static k_timeout_t behavior_hold_tap_config_##n##_gettime() { return K_MSEC(DT_INST_PROP(n, tapping_term_ms)); } \ - static struct behavior_hold_tap_behaviors behavior_hold_tap_behaviors_##n = { \ - .hold = _TRANSFORM_ENTRY(0, n) \ - .tap = _TRANSFORM_ENTRY(1, n) \ - }; \ - static struct behavior_hold_tap_config behavior_hold_tap_config_##n = { \ - .behaviors = &behavior_hold_tap_behaviors_##n, \ - .tapping_term_ms = &behavior_hold_tap_config_##n##_gettime, \ - .flavor = DT_ENUM_IDX(DT_DRV_INST(n), flavor), \ - }; \ - DEVICE_AND_API_INIT( \ - behavior_hold_tap_##n, DT_INST_LABEL(n), behavior_hold_tap_init, \ - &behavior_hold_tap_data, \ - &behavior_hold_tap_config_##n, \ - APPLICATION, CONFIG_KERNEL_INIT_PRIORITY_DEFAULT, \ - &behavior_hold_tap_driver_api); +/* todo: get rid of unused param1 and param2. */ +#define _TRANSFORM_ENTRY(idx, node) \ + { \ + .behavior_dev = DT_LABEL(DT_INST_PHANDLE_BY_IDX(node, bindings, idx)), \ + .param1 = COND_CODE_0(DT_INST_PHA_HAS_CELL_AT_IDX(node, bindings, idx, param1), (0), \ + (DT_INST_PHA_BY_IDX(node, bindings, idx, param1))), \ + .param2 = COND_CODE_0(DT_INST_PHA_HAS_CELL_AT_IDX(node, bindings, idx, param2), (0), \ + (DT_INST_PHA_BY_IDX(node, bindings, idx, param2))), \ + }, + +#define KP_INST(n) \ + static struct behavior_hold_tap_behaviors behavior_hold_tap_behaviors_##n = { \ + .hold = _TRANSFORM_ENTRY(0, n).tap = _TRANSFORM_ENTRY(1, n)}; \ + static struct behavior_hold_tap_config behavior_hold_tap_config_##n = { \ + .behaviors = &behavior_hold_tap_behaviors_##n, \ + .tapping_term_ms = DT_INST_PROP(n, tapping_term_ms), \ + .flavor = DT_ENUM_IDX(DT_DRV_INST(n), flavor), \ + }; \ + DEVICE_AND_API_INIT(behavior_hold_tap_##n, DT_INST_LABEL(n), behavior_hold_tap_init, \ + &behavior_hold_tap_data, &behavior_hold_tap_config_##n, APPLICATION, \ + CONFIG_KERNEL_INIT_PRIORITY_DEFAULT, &behavior_hold_tap_driver_api); DT_INST_FOREACH_STATUS_OKAY(KP_INST) - #endif
\ No newline at end of file diff --git a/app/src/behaviors/behavior_key_press.c b/app/src/behaviors/behavior_key_press.c index 7404c79..923b098 100644 --- a/app/src/behaviors/behavior_key_press.c +++ b/app/src/behaviors/behavior_key_press.c @@ -1,5 +1,5 @@ /* - * Copyright (c) 2020 Peter Johanson <peter@peterjohanson.com> + * Copyright (c) 2020 The ZMK Contributors * * SPDX-License-Identifier: MIT */ @@ -12,49 +12,46 @@ #include <zmk/event-manager.h> #include <zmk/events/keycode-state-changed.h> +#include <zmk/behavior.h> LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL); struct behavior_key_press_config { - u8_t usage_page; + u8_t usage_page; }; -struct behavior_key_press_data { }; +struct behavior_key_press_data {}; -static int behavior_key_press_init(struct device *dev) -{ - return 0; -}; +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); +static int on_keymap_binding_pressed(struct zmk_behavior_binding *binding, + struct zmk_behavior_binding_event event) { + struct device *dev = device_get_binding(binding->behavior_dev); + const struct behavior_key_press_config *cfg = dev->config_info; + LOG_DBG("position %d usage_page 0x%02X keycode 0x%02X", event.position, cfg->usage_page, + binding->param1); - return ZMK_EVENT_RAISE(create_keycode_state_changed(cfg->usage_page, keycode, true)); + return ZMK_EVENT_RAISE(create_keycode_state_changed(cfg->usage_page, binding->param1, true)); } -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); +static int on_keymap_binding_released(struct zmk_behavior_binding *binding, + struct zmk_behavior_binding_event event) { + struct device *dev = device_get_binding(binding->behavior_dev); + const struct behavior_key_press_config *cfg = dev->config_info; + LOG_DBG("position %d usage_page 0x%02X keycode 0x%02X", event.position, cfg->usage_page, + binding->param1); - return ZMK_EVENT_RAISE(create_keycode_state_changed(cfg->usage_page, keycode, false)); + return ZMK_EVENT_RAISE(create_keycode_state_changed(cfg->usage_page, binding->param1, false)); } static const struct behavior_driver_api behavior_key_press_driver_api = { - .binding_pressed = on_keymap_binding_pressed, - .binding_released = on_keymap_binding_released -}; + .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); -#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)
\ No newline at end of file +DT_INST_FOREACH_STATUS_OKAY(KP_INST) diff --git a/app/src/behaviors/behavior_momentary_layer.c b/app/src/behaviors/behavior_momentary_layer.c index 8a8584a..b1fb14b 100644 --- a/app/src/behaviors/behavior_momentary_layer.c +++ b/app/src/behaviors/behavior_momentary_layer.c @@ -1,5 +1,5 @@ /* - * Copyright (c) 2020 Peter Johanson <peter@peterjohanson.com> + * Copyright (c) 2020 The ZMK Contributors * * SPDX-License-Identifier: MIT */ @@ -11,44 +11,34 @@ #include <logging/log.h> #include <zmk/keymap.h> +#include <zmk/behavior.h> LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL); -struct behavior_mo_config { }; -struct behavior_mo_data { }; +struct behavior_mo_config {}; +struct behavior_mo_data {}; -static int behavior_mo_init(struct device *dev) -{ - return 0; -}; +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_pressed(struct zmk_behavior_binding *binding, + struct zmk_behavior_binding_event event) { + LOG_DBG("position %d layer %d", event.position, binding->param1); + return zmk_keymap_layer_activate(binding->param1); } -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 int mo_keymap_binding_released(struct zmk_behavior_binding *binding, + struct zmk_behavior_binding_event event) { + LOG_DBG("position %d layer %d", event.position, binding->param1); + return zmk_keymap_layer_deactivate(binding->param1); } static const struct behavior_driver_api behavior_mo_driver_api = { - .binding_pressed = mo_keymap_binding_pressed, - .binding_released = mo_keymap_binding_released -}; - + .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, +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); diff --git a/app/src/behaviors/behavior_none.c b/app/src/behaviors/behavior_none.c index e822d5e..96ea9d5 100644 --- a/app/src/behaviors/behavior_none.c +++ b/app/src/behaviors/behavior_none.c @@ -1,5 +1,5 @@ /* - * Copyright (c) 2020 Peter Johanson <peter@peterjohanson.com> + * Copyright (c) 2020 The ZMK Contributors * * SPDX-License-Identifier: MIT */ @@ -11,38 +11,34 @@ #include <drivers/behavior.h> #include <logging/log.h> +#include <zmk/behavior.h> + LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL); -struct behavior_none_config { }; -struct behavior_none_data { }; +struct behavior_none_config {}; +struct behavior_none_data {}; -static int behavior_none_init(struct device *dev) -{ - return 0; -}; +static int behavior_none_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 0; +static int on_keymap_binding_pressed(struct zmk_behavior_binding *binding, + struct zmk_behavior_binding_event event) { + return 0; } -static int on_keymap_binding_released(struct device *dev, u32_t position, u32_t _param1, u32_t _param2) -{ - return 0; +static int on_keymap_binding_released(struct zmk_behavior_binding *binding, + struct zmk_behavior_binding_event event) { + return 0; } static const struct behavior_driver_api behavior_none_driver_api = { - .binding_pressed = on_keymap_binding_pressed, - .binding_released = on_keymap_binding_released, + .binding_pressed = on_keymap_binding_pressed, + .binding_released = on_keymap_binding_released, }; - static const struct behavior_none_config behavior_none_config = {}; static struct behavior_none_data behavior_none_data; -DEVICE_AND_API_INIT(behavior_none, DT_INST_LABEL(0), behavior_none_init, - &behavior_none_data, - &behavior_none_config, - APPLICATION, CONFIG_KERNEL_INIT_PRIORITY_DEFAULT, +DEVICE_AND_API_INIT(behavior_none, DT_INST_LABEL(0), behavior_none_init, &behavior_none_data, + &behavior_none_config, APPLICATION, CONFIG_KERNEL_INIT_PRIORITY_DEFAULT, &behavior_none_driver_api);
\ No newline at end of file diff --git a/app/src/behaviors/behavior_reset.c b/app/src/behaviors/behavior_reset.c index 30a96ea..d1233a5 100644 --- a/app/src/behaviors/behavior_reset.c +++ b/app/src/behaviors/behavior_reset.c @@ -1,5 +1,5 @@ /* - * Copyright (c) 2020 Peter Johanson <peter@peterjohanson.com> + * Copyright (c) 2020 The ZMK Contributors * * SPDX-License-Identifier: MIT */ @@ -11,40 +11,37 @@ #include <drivers/behavior.h> #include <logging/log.h> +#include <zmk/behavior.h> + LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL); struct behavior_reset_config { - int type; + int type; }; -static int behavior_reset_init(struct device *dev) -{ - return 0; -}; +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) -{ - const struct behavior_reset_config *cfg = dev->config_info; +static int on_keymap_binding_pressed(struct zmk_behavior_binding *binding, + struct zmk_behavior_binding_event event) { + struct device *dev = device_get_binding(binding->behavior_dev); + const struct behavior_reset_config *cfg = dev->config_info; - // TODO: Correct magic code for going into DFU? - // See https://github.com/adafruit/Adafruit_nRF52_Bootloader/blob/d6b28e66053eea467166f44875e3c7ec741cb471/src/main.c#L107 - sys_reboot(cfg->type); - return 0; + // TODO: Correct magic code for going into DFU? + // See + // https://github.com/adafruit/Adafruit_nRF52_Bootloader/blob/d6b28e66053eea467166f44875e3c7ec741cb471/src/main.c#L107 + sys_reboot(cfg->type); + return 0; } static const struct behavior_driver_api behavior_reset_driver_api = { - .binding_pressed = on_keymap_binding_pressed, + .binding_pressed = on_keymap_binding_pressed, }; +#define RST_INST(n) \ + static const struct behavior_reset_config behavior_reset_config_##n = { \ + .type = DT_INST_PROP(n, type)}; \ + DEVICE_AND_API_INIT(behavior_reset_##n, DT_INST_LABEL(n), behavior_reset_init, NULL, \ + &behavior_reset_config_##n, APPLICATION, \ + CONFIG_KERNEL_INIT_PRIORITY_DEFAULT, &behavior_reset_driver_api); -#define RST_INST(n) \ - static const struct behavior_reset_config behavior_reset_config_##n = { \ - .type = DT_INST_PROP(n, type) \ - }; \ - DEVICE_AND_API_INIT(behavior_reset_##n, DT_INST_LABEL(n), behavior_reset_init, \ - NULL, \ - &behavior_reset_config_##n, \ - APPLICATION, CONFIG_KERNEL_INIT_PRIORITY_DEFAULT, \ - &behavior_reset_driver_api); - DT_INST_FOREACH_STATUS_OKAY(RST_INST)
\ No newline at end of file diff --git a/app/src/behaviors/behavior_rgb_underglow.c b/app/src/behaviors/behavior_rgb_underglow.c index 7a48e07..2ee6716 100644 --- a/app/src/behaviors/behavior_rgb_underglow.c +++ b/app/src/behaviors/behavior_rgb_underglow.c @@ -1,5 +1,5 @@ /* - * Copyright (c) 2020 Nick Winans <nick@winans.codes> + * Copyright (c) 2020 The ZMK Contributors * * SPDX-License-Identifier: MIT */ @@ -12,51 +12,46 @@ #include <dt-bindings/zmk/rgb.h> #include <zmk/rgb_underglow.h> +#include <zmk/keymap.h> LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL); -static int behavior_rgb_underglow_init(struct device *dev) -{ - return 0; -} +static int behavior_rgb_underglow_init(struct device *dev) { return 0; } -static int on_keymap_binding_pressed(struct device *dev, u32_t position, u32_t action, u32_t _) -{ - switch (action) - { +static int on_keymap_binding_pressed(struct zmk_behavior_binding *binding, + struct zmk_behavior_binding_event event) { + switch (binding->param1) { case RGB_TOG: - return zmk_rgb_underglow_toggle(); + return zmk_rgb_underglow_toggle(); case RGB_HUI: - return zmk_rgb_underglow_change_hue(1); + return zmk_rgb_underglow_change_hue(1); case RGB_HUD: - return zmk_rgb_underglow_change_hue(-1); + return zmk_rgb_underglow_change_hue(-1); case RGB_SAI: - return zmk_rgb_underglow_change_sat(1); + return zmk_rgb_underglow_change_sat(1); case RGB_SAD: - return zmk_rgb_underglow_change_sat(-1); + return zmk_rgb_underglow_change_sat(-1); case RGB_BRI: - return zmk_rgb_underglow_change_brt(1); + return zmk_rgb_underglow_change_brt(1); case RGB_BRD: - return zmk_rgb_underglow_change_brt(-1); + return zmk_rgb_underglow_change_brt(-1); case RGB_SPI: - return zmk_rgb_underglow_change_spd(1); + return zmk_rgb_underglow_change_spd(1); case RGB_SPD: - return zmk_rgb_underglow_change_spd(-1); + return zmk_rgb_underglow_change_spd(-1); case RGB_EFF: - return zmk_rgb_underglow_cycle_effect(1); + return zmk_rgb_underglow_cycle_effect(1); case RGB_EFR: - return zmk_rgb_underglow_cycle_effect(-1); - } + return zmk_rgb_underglow_cycle_effect(-1); + } - return -ENOTSUP; + return -ENOTSUP; } static const struct behavior_driver_api behavior_rgb_underglow_driver_api = { - .binding_pressed = on_keymap_binding_pressed, + .binding_pressed = on_keymap_binding_pressed, }; -DEVICE_AND_API_INIT(behavior_rgb_underglow, DT_INST_LABEL(0), behavior_rgb_underglow_init, - NULL, - NULL, - APPLICATION, CONFIG_KERNEL_INIT_PRIORITY_DEFAULT, +DEVICE_AND_API_INIT(behavior_rgb_underglow, DT_INST_LABEL(0), behavior_rgb_underglow_init, NULL, + NULL, APPLICATION, CONFIG_KERNEL_INIT_PRIORITY_DEFAULT, &behavior_rgb_underglow_driver_api);
\ No newline at end of file diff --git a/app/src/behaviors/behavior_sensor_rotate_key_press.c b/app/src/behaviors/behavior_sensor_rotate_key_press.c index fb0fb85..71c4376 100644 --- a/app/src/behaviors/behavior_sensor_rotate_key_press.c +++ b/app/src/behaviors/behavior_sensor_rotate_key_press.c @@ -1,5 +1,5 @@ /* - * Copyright (c) 2020 Peter Johanson <peter@peterjohanson.com> + * Copyright (c) 2020 The ZMK Contributors * * SPDX-License-Identifier: MIT */ @@ -17,75 +17,70 @@ LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL); struct behavior_sensor_rotate_key_press_config { - u8_t usage_page; -}; -struct behavior_sensor_rotate_key_press_data { }; - -static int behavior_sensor_rotate_key_press_init(struct device *dev) -{ - return 0; + u8_t usage_page; }; +struct behavior_sensor_rotate_key_press_data {}; +static int behavior_sensor_rotate_key_press_init(struct device *dev) { return 0; }; -static int on_sensor_binding_triggered(struct device *dev, struct device *sensor, u32_t increment_keycode, u32_t decrement_keycode) -{ - const struct behavior_sensor_rotate_key_press_config *cfg = dev->config_info; - struct sensor_value value; - int err; - u32_t keycode; - struct keycode_state_changed *ev; - LOG_DBG("usage_page 0x%02X inc keycode 0x%02X dec keycode 0x%02X", cfg->usage_page, increment_keycode, decrement_keycode); +static int on_sensor_binding_triggered(struct zmk_behavior_binding *binding, + struct device *sensor) { + struct device *dev = device_get_binding(binding->behavior_dev); + const struct behavior_sensor_rotate_key_press_config *cfg = dev->config_info; + struct sensor_value value; + int err; + u32_t keycode; + struct keycode_state_changed *ev; + LOG_DBG("usage_page 0x%02X inc keycode 0x%02X dec keycode 0x%02X", cfg->usage_page, + binding->param1, binding->param2); - err = sensor_channel_get(sensor, SENSOR_CHAN_ROTATION, &value); + err = sensor_channel_get(sensor, SENSOR_CHAN_ROTATION, &value); - if (err) { - LOG_WRN("Failed to ge sensor rotation value: %d", err); - return err; - } + if (err) { + LOG_WRN("Failed to ge sensor rotation value: %d", err); + return err; + } - switch (value.val1) { + switch (value.val1) { case 1: - keycode = increment_keycode; - break; + keycode = binding->param1; + break; case -1: - keycode = decrement_keycode; - break; + keycode = binding->param2; + break; default: - return -ENOTSUP; - } - - LOG_DBG("SEND %d", keycode); + return -ENOTSUP; + } + LOG_DBG("SEND %d", keycode); - ev = new_keycode_state_changed(); - ev->usage_page = cfg->usage_page; - ev->keycode = keycode; - ev->state = true; - ZMK_EVENT_RAISE(ev); + ev = new_keycode_state_changed(); + ev->usage_page = cfg->usage_page; + ev->keycode = keycode; + ev->state = true; + ZMK_EVENT_RAISE(ev); - // TODO: Better way to do this? - k_msleep(5); + // TODO: Better way to do this? + k_msleep(5); - ev = new_keycode_state_changed(); - ev->usage_page = cfg->usage_page; - ev->keycode = keycode; - ev->state = false; - return ZMK_EVENT_RAISE(ev); + ev = new_keycode_state_changed(); + ev->usage_page = cfg->usage_page; + ev->keycode = keycode; + ev->state = false; + return ZMK_EVENT_RAISE(ev); } static const struct behavior_driver_api behavior_sensor_rotate_key_press_driver_api = { - .sensor_binding_triggered = on_sensor_binding_triggered -}; - -#define KP_INST(n) \ - static const struct behavior_sensor_rotate_key_press_config behavior_sensor_rotate_key_press_config_##n = { \ - .usage_page = DT_INST_PROP(n, usage_page) \ - }; \ - static struct behavior_sensor_rotate_key_press_data behavior_sensor_rotate_key_press_data_##n; \ - DEVICE_AND_API_INIT(behavior_sensor_rotate_key_press_##n, DT_INST_LABEL(n), behavior_sensor_rotate_key_press_init, \ - &behavior_sensor_rotate_key_press_data_##n, \ - &behavior_sensor_rotate_key_press_config_##n, \ - APPLICATION, CONFIG_KERNEL_INIT_PRIORITY_DEFAULT, \ - &behavior_sensor_rotate_key_press_driver_api); + .sensor_binding_triggered = on_sensor_binding_triggered}; + +#define KP_INST(n) \ + static const struct behavior_sensor_rotate_key_press_config \ + behavior_sensor_rotate_key_press_config_##n = {.usage_page = DT_INST_PROP(n, usage_page)}; \ + static struct behavior_sensor_rotate_key_press_data behavior_sensor_rotate_key_press_data_##n; \ + DEVICE_AND_API_INIT( \ + behavior_sensor_rotate_key_press_##n, DT_INST_LABEL(n), \ + behavior_sensor_rotate_key_press_init, &behavior_sensor_rotate_key_press_data_##n, \ + &behavior_sensor_rotate_key_press_config_##n, APPLICATION, \ + CONFIG_KERNEL_INIT_PRIORITY_DEFAULT, &behavior_sensor_rotate_key_press_driver_api); DT_INST_FOREACH_STATUS_OKAY(KP_INST)
\ No newline at end of file diff --git a/app/src/behaviors/behavior_toggle_layer.c b/app/src/behaviors/behavior_toggle_layer.c index 02a76d2..b3c6961 100644 --- a/app/src/behaviors/behavior_toggle_layer.c +++ b/app/src/behaviors/behavior_toggle_layer.c @@ -1,5 +1,5 @@ /* - * Copyright (c) 2020 Cody McGinnis <brainwart@gmail.com> + * Copyright (c) 2020 The ZMK Contributors * * SPDX-License-Identifier: MIT */ @@ -11,33 +11,25 @@ #include <logging/log.h> #include <zmk/keymap.h> +#include <zmk/behavior.h> LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL); -struct behavior_tog_config -{ -}; -struct behavior_tog_data -{ -}; +struct behavior_tog_config {}; +struct behavior_tog_data {}; -static int behavior_tog_init(struct device *dev) -{ - return 0; -}; +static int behavior_tog_init(struct device *dev) { return 0; }; -static int tog_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_toggle(layer); +static int tog_keymap_binding_pressed(struct zmk_behavior_binding *binding, + struct zmk_behavior_binding_event event) { + LOG_DBG("position %d layer %d", event.position, binding->param1); + return zmk_keymap_layer_toggle(binding->param1); } -static int tog_keymap_binding_released(struct device *dev, u32_t position, u32_t layer, u32_t _) -{ - LOG_DBG("position %d layer %d", position, layer); - - return 0; +static int tog_keymap_binding_released(struct zmk_behavior_binding *binding, + struct zmk_behavior_binding_event event) { + LOG_DBG("position %d layer %d", event.position, binding->param1); + return 0; } static const struct behavior_driver_api behavior_tog_driver_api = { @@ -49,8 +41,6 @@ static const struct behavior_tog_config behavior_tog_config = {}; static struct behavior_tog_data behavior_tog_data; -DEVICE_AND_API_INIT(behavior_tog, DT_INST_LABEL(0), behavior_tog_init, - &behavior_tog_data, - &behavior_tog_config, - APPLICATION, CONFIG_KERNEL_INIT_PRIORITY_DEFAULT, +DEVICE_AND_API_INIT(behavior_tog, DT_INST_LABEL(0), behavior_tog_init, &behavior_tog_data, + &behavior_tog_config, APPLICATION, CONFIG_KERNEL_INIT_PRIORITY_DEFAULT, &behavior_tog_driver_api); diff --git a/app/src/behaviors/behavior_transparent.c b/app/src/behaviors/behavior_transparent.c index 5109264..cede369 100644 --- a/app/src/behaviors/behavior_transparent.c +++ b/app/src/behaviors/behavior_transparent.c @@ -1,5 +1,5 @@ /* - * Copyright (c) 2020 Peter Johanson <peter@peterjohanson.com> + * Copyright (c) 2020 The ZMK Contributors * * SPDX-License-Identifier: MIT */ @@ -11,38 +11,34 @@ #include <drivers/behavior.h> #include <logging/log.h> +#include <zmk/behavior.h> + LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL); -struct behavior_transparent_config { }; -struct behavior_transparent_data { }; +struct behavior_transparent_config {}; +struct behavior_transparent_data {}; -static int behavior_transparent_init(struct device *dev) -{ - return 0; -}; +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_pressed(struct zmk_behavior_binding *binding, + struct zmk_behavior_binding_event event) { + return 1; } -static int on_keymap_binding_released(struct device *dev, u32_t position, u32_t _param1, u32_t _param2) -{ - return 1; +static int on_keymap_binding_released(struct zmk_behavior_binding *binding, + struct zmk_behavior_binding_event event) { + return 1; } static const struct behavior_driver_api behavior_transparent_driver_api = { - .binding_pressed = on_keymap_binding_pressed, - .binding_released = on_keymap_binding_released, + .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);
\ No newline at end of file + &behavior_transparent_data, &behavior_transparent_config, APPLICATION, + CONFIG_KERNEL_INIT_PRIORITY_DEFAULT, &behavior_transparent_driver_api);
\ No newline at end of file diff --git a/app/src/ble.c b/app/src/ble.c index 0e96d16..49e2b3b 100644 --- a/app/src/ble.c +++ b/app/src/ble.c @@ -1,5 +1,5 @@ /* - * Copyright (c) 2020 Peter Johanson + * Copyright (c) 2020 The ZMK Contributors * * SPDX-License-Identifier: MIT */ @@ -8,6 +8,8 @@ #include <init.h> #include <math.h> +#include <stdlib.h> +#include <stdio.h> #include <settings/settings.h> #include <bluetooth/bluetooth.h> @@ -15,37 +17,224 @@ #include <bluetooth/hci.h> #include <bluetooth/uuid.h> #include <bluetooth/gatt.h> +#include <bluetooth/hci_err.h> +#if IS_ENABLED(CONFIG_SETTINGS) + +#include <settings/settings.h> + +#endif #include <logging/log.h> LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL); +#include <zmk/ble.h> #include <zmk/keys.h> #include <zmk/split/bluetooth/uuid.h> +#include <zmk/event-manager.h> +#include <zmk/events/ble-active-profile-changed.h> static struct bt_conn *auth_passkey_entry_conn; static u8_t passkey_entries[6] = {0, 0, 0, 0, 0, 0}; static u8_t passkey_digit = 0; -#if IS_ENABLED(CONFIG_ZMK_SPLIT_BLE_ROLE_PERIPHERAL) -#define ZMK_ADV_PARAMS BT_LE_ADV_PARAM(BT_LE_ADV_OPT_CONNECTABLE | \ - BT_LE_ADV_OPT_USE_NAME | \ - BT_LE_ADV_OPT_ONE_TIME, \ - BT_GAP_ADV_FAST_INT_MIN_2, \ - BT_GAP_ADV_FAST_INT_MAX_2, NULL) +#if IS_ENABLED(CONFIG_ZMK_SPLIT_BLE_ROLE_CENTRAL) +#define PROFILE_COUNT (CONFIG_BT_MAX_PAIRED - 1) #else -#define ZMK_ADV_PARAMS BT_LE_ADV_CONN_NAME +#define PROFILE_COUNT CONFIG_BT_MAX_PAIRED #endif -static void connected(struct bt_conn *conn, u8_t err) -{ - char addr[BT_ADDR_LE_STR_LEN]; +static struct zmk_ble_profile profiles[PROFILE_COUNT]; +static u8_t active_profile; + +static const struct bt_data zmk_ble_ad[] = { + BT_DATA_BYTES(BT_DATA_FLAGS, (BT_LE_AD_GENERAL | BT_LE_AD_NO_BREDR)), + BT_DATA_BYTES(BT_DATA_UUID16_SOME, +#if !IS_ENABLED(CONFIG_ZMK_SPLIT_BLE_ROLE_PERIPHERAL) + 0x12, 0x18, /* HID Service */ +#endif + 0x0f, 0x18 /* Battery Service */ + ), +#if IS_ENABLED(CONFIG_ZMK_SPLIT_BLE_ROLE_PERIPHERAL) + BT_DATA_BYTES(BT_DATA_UUID128_ALL, ZMK_SPLIT_BT_SERVICE_UUID) +#endif +}; + +#if IS_ENABLED(CONFIG_ZMK_SPLIT_BLE_ROLE_CENTRAL) + +static bt_addr_le_t peripheral_addr; + +#endif /* IS_ENABLED(CONFIG_ZMK_SPLIT_BLE_ROLE_CENTRAL) */ + +static void raise_profile_changed_event() { + struct ble_active_profile_changed *ev = new_ble_active_profile_changed(); + ev->index = active_profile; + ev->profile = &profiles[active_profile]; + + ZMK_EVENT_RAISE(ev); +} + +static bool active_profile_is_open() { + return !bt_addr_le_cmp(&profiles[active_profile].peer, BT_ADDR_LE_ANY); +} + +void set_profile_address(u8_t index, const bt_addr_le_t *addr) { + char setting_name[15]; + char addr_str[BT_ADDR_LE_STR_LEN]; + + bt_addr_le_to_str(addr, addr_str, sizeof(addr_str)); + + memcpy(&profiles[index].peer, addr, sizeof(bt_addr_le_t)); + sprintf(setting_name, "ble/profiles/%d", index); + LOG_DBG("Setting profile addr for %s to %s", log_strdup(setting_name), log_strdup(addr_str)); + settings_save_one(setting_name, &profiles[index], sizeof(struct zmk_ble_profile)); + raise_profile_changed_event(); +} + +int zmk_ble_adv_pause() { + int err = bt_le_adv_stop(); + if (err) { + LOG_ERR("Failed to stop advertising (err %d)", err); + return err; + } + + return 0; +}; + +int zmk_ble_adv_resume() { + LOG_DBG("active_profile %d, directed? %s", active_profile, + active_profile_is_open() ? "no" : "yes"); + + int err = bt_le_adv_start(BT_LE_ADV_CONN_NAME, zmk_ble_ad, ARRAY_SIZE(zmk_ble_ad), NULL, 0); + if (err) { + LOG_ERR("Advertising failed to start (err %d)", err); + return err; + } + + return 0; +}; + +int zmk_ble_clear_bonds() { + LOG_DBG(""); + + if (bt_addr_le_cmp(&profiles[active_profile].peer, BT_ADDR_LE_ANY)) { + LOG_DBG("Unpairing!"); + bt_unpair(BT_ID_DEFAULT, &profiles[active_profile].peer); + set_profile_address(active_profile, BT_ADDR_LE_ANY); + } + + return 0; +}; + +int zmk_ble_prof_select(u8_t index) { + LOG_DBG("profile %d", index); + if (active_profile == index) { + return 0; + } + + active_profile = index; + return settings_save_one("ble/active_profile", &active_profile, sizeof(active_profile)); + + raise_profile_changed_event(); +}; + +int zmk_ble_prof_next() { + LOG_DBG(""); + return zmk_ble_prof_select((active_profile + 1) % PROFILE_COUNT); +}; + +int zmk_ble_prof_prev() { + LOG_DBG(""); + return zmk_ble_prof_select((active_profile + PROFILE_COUNT - 1) % PROFILE_COUNT); +}; + +bt_addr_le_t *zmk_ble_active_profile_addr() { return &profiles[active_profile].peer; } + +char *zmk_ble_active_profile_name() { return profiles[active_profile].name; } + +#if IS_ENABLED(CONFIG_ZMK_SPLIT_BLE_ROLE_CENTRAL) + +void zmk_ble_set_peripheral_addr(bt_addr_le_t *addr) { + memcpy(&peripheral_addr, addr, sizeof(bt_addr_le_t)); + settings_save_one("ble/peripheral_address", addr, sizeof(bt_addr_le_t)); +} + +#endif /* IS_ENABLED(CONFIG_ZMK_SPLIT_BLE_ROLE_CENTRAL) */ + +#if IS_ENABLED(CONFIG_SETTINGS) + +static int ble_profiles_handle_set(const char *name, size_t len, settings_read_cb read_cb, + void *cb_arg) { + const char *next; + + LOG_DBG("Setting BLE value %s", log_strdup(name)); + + if (settings_name_steq(name, "profiles", &next) && next) { + char *endptr; + u8_t idx = strtoul(next, &endptr, 10); + if (*endptr != '\0') { + LOG_WRN("Invalid profile index: %s", log_strdup(next)); + return -EINVAL; + } + + if (len != sizeof(struct zmk_ble_profile)) { + LOG_ERR("Invalid profile size (got %d expected %d)", len, + sizeof(struct zmk_ble_profile)); + return -EINVAL; + } + if (idx >= PROFILE_COUNT) { + LOG_WRN("Profile address for index %d is larger than max of %d", idx, PROFILE_COUNT); + return -EINVAL; + } + + int err = read_cb(cb_arg, &profiles[idx], sizeof(struct zmk_ble_profile)); + if (err <= 0) { + LOG_ERR("Failed to handle profile address from settings (err %d)", err); + return err; + } + + char addr_str[BT_ADDR_LE_STR_LEN]; + bt_addr_le_to_str(&profiles[idx].peer, addr_str, sizeof(addr_str)); + + LOG_DBG("Loaded %s address for profile %d", log_strdup(addr_str), idx); + } else if (settings_name_steq(name, "active_profile", &next) && !next) { + if (len != sizeof(active_profile)) { + return -EINVAL; + } + + int err = read_cb(cb_arg, &active_profile, sizeof(active_profile)); + if (err <= 0) { + LOG_ERR("Failed to handle active profile from settings (err %d)", err); + return err; + } + } +#if IS_ENABLED(CONFIG_ZMK_SPLIT_BLE_ROLE_CENTRAL) + else if (settings_name_steq(name, "peripheral_address", &next) && !next) { + if (len != sizeof(bt_addr_le_t)) { + return -EINVAL; + } + + int err = read_cb(cb_arg, &peripheral_addr, sizeof(bt_addr_le_t)); + if (err <= 0) { + LOG_ERR("Failed to handle peripheral address from settings (err %d)", err); + return err; + } + } +#endif + + return 0; +}; + +struct settings_handler profiles_handler = {.name = "ble", .h_set = ble_profiles_handle_set}; +#endif /* IS_ENABLED(CONFIG_SETTINGS) */ + +static void connected(struct bt_conn *conn, u8_t err) { + char addr[BT_ADDR_LE_STR_LEN]; bt_addr_le_to_str(bt_conn_get_dst(conn), addr, sizeof(addr)); - if (err) - { + if (err) { LOG_WRN("Failed to connect to %s (%u)", log_strdup(addr), err); return; } @@ -58,36 +247,37 @@ static void connected(struct bt_conn *conn, u8_t err) bt_conn_le_phy_update(conn, BT_CONN_LE_PHY_PARAM_2M); #endif - if (bt_conn_set_security(conn, BT_SECURITY_L2)) - { + if (bt_conn_set_security(conn, BT_SECURITY_L2)) { LOG_ERR("Failed to set security"); } } -static void disconnected(struct bt_conn *conn, u8_t reason) -{ +static void disconnected(struct bt_conn *conn, u8_t reason) { char addr[BT_ADDR_LE_STR_LEN]; bt_addr_le_to_str(bt_conn_get_dst(conn), addr, sizeof(addr)); LOG_DBG("Disconnected from %s (reason 0x%02x)", log_strdup(addr), reason); + +#if IS_ENABLED(CONFIG_ZMK_SPLIT_BLE_ROLE_CENTRAL) + // if (bt_addr_le_cmp(&peripheral_addr, BT_ADDR_LE_ANY) && bt_addr_le_cmp(&peripheral_addr, + // bt_conn_get_dst(conn))) { + // zmk_ble_adv_resume(); + // } +#else + // zmk_ble_adv_resume(); +#endif } -static void security_changed(struct bt_conn *conn, bt_security_t level, - enum bt_security_err err) -{ +static void security_changed(struct bt_conn *conn, bt_security_t level, enum bt_security_err err) { char addr[BT_ADDR_LE_STR_LEN]; bt_addr_le_to_str(bt_conn_get_dst(conn), addr, sizeof(addr)); - if (!err) - { + if (!err) { LOG_DBG("Security changed: %s level %u", log_strdup(addr), level); - } - else - { - LOG_ERR("Security failed: %s level %u err %d", log_strdup(addr), level, - err); + } else { + LOG_ERR("Security failed: %s level %u err %d", log_strdup(addr), level, err); } } @@ -97,8 +287,7 @@ static struct bt_conn_cb conn_callbacks = { .security_changed = security_changed, }; -static void auth_passkey_display(struct bt_conn *conn, unsigned int passkey) -{ +static void auth_passkey_display(struct bt_conn *conn, unsigned int passkey) { char addr[BT_ADDR_LE_STR_LEN]; bt_addr_le_to_str(bt_conn_get_dst(conn), addr, sizeof(addr)); @@ -108,8 +297,7 @@ static void auth_passkey_display(struct bt_conn *conn, unsigned int passkey) #ifdef CONFIG_ZMK_BLE_PASSKEY_ENTRY -static void auth_passkey_entry(struct bt_conn *conn) -{ +static void auth_passkey_entry(struct bt_conn *conn) { char addr[BT_ADDR_LE_STR_LEN]; bt_addr_le_to_str(bt_conn_get_dst(conn), addr, sizeof(addr)); @@ -120,14 +308,12 @@ static void auth_passkey_entry(struct bt_conn *conn) #endif -static void auth_cancel(struct bt_conn *conn) -{ +static void auth_cancel(struct bt_conn *conn) { char addr[BT_ADDR_LE_STR_LEN]; bt_addr_le_to_str(bt_conn_get_dst(conn), addr, sizeof(addr)); - if (auth_passkey_entry_conn) - { + if (auth_passkey_entry_conn) { bt_conn_unref(auth_passkey_entry_conn); auth_passkey_entry_conn = NULL; } @@ -137,7 +323,51 @@ static void auth_cancel(struct bt_conn *conn) LOG_DBG("Pairing cancelled: %s", log_strdup(addr)); } +#if !IS_ENABLED(CONFIG_ZMK_SPLIT_BLE_ROLE_PERIPHERAL) +static enum bt_security_err auth_pairing_accept(struct bt_conn *conn, + const struct bt_conn_pairing_feat *const feat) { + struct bt_conn_info info; + bt_conn_get_info(conn, &info); + + LOG_DBG("role %d, open? %s", info.role, active_profile_is_open() ? "yes" : "no"); + if (info.role == BT_CONN_ROLE_SLAVE && !active_profile_is_open()) { + LOG_WRN("Rejecting pairing request to taken profile %d", active_profile); + return BT_SECURITY_ERR_PAIR_NOT_ALLOWED; + } + + return BT_SECURITY_ERR_SUCCESS; +}; +#endif /* !IS_ENABLED(CONFIG_ZMK_SPLIT_BLE_ROLE_PERIPHERAL) */ + +static void auth_pairing_complete(struct bt_conn *conn, bool bonded) { + struct bt_conn_info info; + char addr[BT_ADDR_LE_STR_LEN]; + const bt_addr_le_t *dst = bt_conn_get_dst(conn); + + bt_addr_le_to_str(dst, addr, sizeof(addr)); + bt_conn_get_info(conn, &info); + + if (info.role != BT_CONN_ROLE_SLAVE) { + LOG_DBG("SKIPPING FOR ROLE %d", info.role); + return; + } + +#if !IS_ENABLED(CONFIG_ZMK_SPLIT_BLE_ROLE_PERIPHERAL) + if (!active_profile_is_open()) { + LOG_ERR("Pairing completed but current profile is not open: %s", log_strdup(addr)); + bt_unpair(BT_ID_DEFAULT, dst); + return; + } +#endif /* !IS_ENABLED(CONFIG_ZMK_SPLIT_BLE_ROLE_PERIPHERAL) */ + + set_profile_address(active_profile, dst); +}; + static struct bt_conn_auth_cb zmk_ble_auth_cb_display = { +#if !IS_ENABLED(CONFIG_ZMK_SPLIT_BLE_ROLE_PERIPHERAL) + .pairing_accept = auth_pairing_accept, +#endif /* !IS_ENABLED(CONFIG_ZMK_SPLIT_BLE_ROLE_PERIPHERAL) */ + .pairing_complete = auth_pairing_complete, // .passkey_display = auth_passkey_display, #ifdef CONFIG_ZMK_BLE_PASSKEY_ENTRY @@ -146,51 +376,54 @@ static struct bt_conn_auth_cb zmk_ble_auth_cb_display = { .cancel = auth_cancel, }; -static const struct bt_data zmk_ble_ad[] = { - BT_DATA_BYTES(BT_DATA_FLAGS, (BT_LE_AD_GENERAL | BT_LE_AD_NO_BREDR)), - BT_DATA_BYTES(BT_DATA_UUID16_SOME, -#if !IS_ENABLED(CONFIG_ZMK_SPLIT_BLE_ROLE_PERIPHERAL) - 0x12, 0x18, /* HID Service */ -#endif - 0x0f, 0x18 /* Battery Service */ - ), -#if IS_ENABLED(CONFIG_ZMK_SPLIT_BLE_ROLE_PERIPHERAL) - BT_DATA_BYTES(BT_DATA_UUID128_ALL, - ZMK_SPLIT_BT_SERVICE_UUID) -#endif -}; - -static void zmk_ble_ready(int err) -{ +static void zmk_ble_ready(int err) { LOG_DBG("ready? %d", err); - if (err) - { + if (err) { LOG_ERR("Bluetooth init failed (err %d)", err); return; } - err = bt_le_adv_start(ZMK_ADV_PARAMS, zmk_ble_ad, ARRAY_SIZE(zmk_ble_ad), NULL, 0); - if (err) - { - LOG_ERR("Advertising failed to start (err %d)", err); - return; - } + zmk_ble_adv_resume(); } -static int zmk_ble_init(struct device *_arg) -{ +static int zmk_ble_init(struct device *_arg) { int err = bt_enable(NULL); - if (err) - { + if (err) { LOG_ERR("BLUETOOTH FAILED (%d)", err); return err; } - if (IS_ENABLED(CONFIG_BT_SETTINGS)) - { - settings_load(); +#if IS_ENABLED(CONFIG_SETTINGS) + settings_subsys_init(); + + err = settings_register(&profiles_handler); + if (err) { + LOG_ERR("Failed to setup the profile settings handler (err %d)", err); + return err; + } + + settings_load(); + +#endif + +#if IS_ENABLED(CONFIG_ZMK_BLE_CLEAR_BONDS_ON_START) + LOG_WRN("Clearing all existing BLE bond information from the keyboard"); + + for (int i = 0; i < 10; i++) { + bt_unpair(i, NULL); + } + + for (int i = 0; i < PROFILE_COUNT; i++) { + char setting_name[15]; + sprintf(setting_name, "ble/profiles/%d", i); + + err = settings_delete(setting_name); + if (err) { + LOG_ERR("Failed to delete setting: %d", err); + } } +#endif bt_conn_cb_register(&conn_callbacks); bt_conn_auth_cb_register(&zmk_ble_auth_cb_display); @@ -200,23 +433,28 @@ static int zmk_ble_init(struct device *_arg) return 0; } -int zmk_ble_unpair_all() -{ - LOG_DBG(""); - return bt_unpair(BT_ID_DEFAULT, NULL); +int zmk_ble_unpair_all() { + int resp = 0; + for (int i = BT_ID_DEFAULT; i < CONFIG_BT_ID_MAX; i++) { + + int err = bt_unpair(BT_ID_DEFAULT, NULL); + if (err) { + resp = err; + LOG_ERR("Failed to unpair devices (err %d)", err); + } + } + + return resp; }; -bool zmk_ble_handle_key_user(struct zmk_key_event *key_event) -{ +bool zmk_ble_handle_key_user(struct zmk_key_event *key_event) { zmk_key key = key_event->key; - if (!auth_passkey_entry_conn) - { + if (!auth_passkey_entry_conn) { return true; } - if (key < NUM_1 || key > NUM_0) - { + if (key < NUM_1 || key > NUM_0) { return true; } @@ -224,11 +462,9 @@ bool zmk_ble_handle_key_user(struct zmk_key_event *key_event) passkey_entries[passkey_digit++] = val; - if (passkey_digit == 6) - { + if (passkey_digit == 6) { u32_t passkey = 0; - for (int i = 5; i >= 0; i--) - { + for (int i = 5; i >= 0; i--) { passkey = (passkey * 10) + val; } bt_conn_auth_passkey_entry(auth_passkey_entry_conn, passkey); @@ -239,6 +475,4 @@ bool zmk_ble_handle_key_user(struct zmk_key_event *key_event) return false; } -SYS_INIT(zmk_ble_init, - APPLICATION, - CONFIG_ZMK_BLE_INIT_PRIORITY); +SYS_INIT(zmk_ble_init, APPLICATION, CONFIG_ZMK_BLE_INIT_PRIORITY); diff --git a/app/src/ble_unpair_combo.c b/app/src/ble_unpair_combo.c index 82fa834..b6e542d 100644 --- a/app/src/ble_unpair_combo.c +++ b/app/src/ble_unpair_combo.c @@ -1,5 +1,5 @@ /* - * Copyright (c) 2020 Peter Johanson + * Copyright (c) 2020 The ZMK Contributors * * SPDX-License-Identifier: MIT */ @@ -18,14 +18,12 @@ LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL); #include <zmk/event-manager.h> #include <zmk/events/position-state-changed.h> - static u8_t combo_state; const u32_t key_positions[] = DT_INST_PROP(0, key_positions); #define KP_LEN DT_INST_PROP_LEN(0, key_positions) -int index_for_key_position(u32_t kp) -{ +int index_for_key_position(u32_t kp) { for (int i = 0; i < KP_LEN; i++) { if (key_positions[i] == kp) { return i; @@ -35,8 +33,7 @@ int index_for_key_position(u32_t kp) return -1; } -int unpair_combo_listener(const struct zmk_event_header *eh) -{ +int unpair_combo_listener(const struct zmk_event_header *eh) { if (is_position_state_changed(eh)) { const struct position_state_changed *psc = cast_position_state_changed(eh); @@ -51,8 +48,7 @@ int unpair_combo_listener(const struct zmk_event_header *eh) return 0; }; -void unpair_combo_work_handler(struct k_work *work) -{ +void unpair_combo_work_handler(struct k_work *work) { for (int i = 0; i < KP_LEN; i++) { if (!(combo_state & BIT(i))) { LOG_DBG("Key position %d not held, skipping unpair combo", key_positions[i]); @@ -65,8 +61,7 @@ void unpair_combo_work_handler(struct k_work *work) struct k_delayed_work unpair_combo_work; -int zmk_ble_unpair_combo_init(struct device *_unused) -{ +int zmk_ble_unpair_combo_init(struct device *_unused) { k_delayed_work_init(&unpair_combo_work, unpair_combo_work_handler); k_delayed_work_submit(&unpair_combo_work, K_SECONDS(2)); @@ -76,8 +71,6 @@ int zmk_ble_unpair_combo_init(struct device *_unused) ZMK_LISTENER(zmk_ble_unpair_combo, unpair_combo_listener); ZMK_SUBSCRIPTION(zmk_ble_unpair_combo, position_state_changed); -SYS_INIT(zmk_ble_unpair_combo_init, - APPLICATION, - CONFIG_APPLICATION_INIT_PRIORITY); +SYS_INIT(zmk_ble_unpair_combo_init, APPLICATION, CONFIG_APPLICATION_INIT_PRIORITY); #endif /* DT_HAS_COMPAT_STATUS_OKAY(DT_DRV_COMPAT) */ diff --git a/app/src/display.c b/app/src/display.c index 9021914..ecd1908 100644 --- a/app/src/display.c +++ b/app/src/display.c @@ -1,5 +1,5 @@ /* - * Copyright (c) 2020 Peter Johanson + * Copyright (c) 2020 The ZMK Contributors * * SPDX-License-Identifier: MIT */ @@ -20,8 +20,7 @@ static struct device *display; static lv_obj_t *screen; -int zmk_display_init() -{ +int zmk_display_init() { lv_obj_t *hello_world_label; lv_obj_t *count_label; @@ -48,8 +47,7 @@ int zmk_display_init() return 0; } -void zmk_display_task_handler() -{ +void zmk_display_task_handler() { lv_tick_inc(10); lv_task_handler(); k_sleep(K_MSEC(10)); diff --git a/app/src/endpoints.c b/app/src/endpoints.c index c9ba716..79d294e 100644 --- a/app/src/endpoints.c +++ b/app/src/endpoints.c @@ -1,33 +1,35 @@ +/* + * Copyright (c) 2020 The ZMK Contributors + * + * SPDX-License-Identifier: MIT + */ #include <zmk/endpoints.h> #include <zmk/hid.h> -#include <zmk/usb_hid.h> +#include <zmk/usb.h> #include <zmk/hog.h> #include <logging/log.h> LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL); -int zmk_endpoints_send_report(u8_t usage_page) -{ +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; LOG_DBG("usage page 0x%02X", usage_page); - switch (usage_page) - { + switch (usage_page) { 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) - { + if (zmk_usb_hid_send_report((u8_t *)keypad_report, sizeof(struct zmk_hid_keypad_report)) != + 0) { LOG_DBG("USB Send Failed"); } #endif /* CONFIG_ZMK_USB */ #ifdef CONFIG_ZMK_BLE err = zmk_hog_send_keypad_report(&keypad_report->body); - if (err) - { + if (err) { LOG_ERR("FAILED TO SEND OVER HOG: %d", err); } #endif /* CONFIG_ZMK_BLE */ @@ -36,16 +38,15 @@ int zmk_endpoints_send_report(u8_t usage_page) 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) - { + if (zmk_usb_hid_send_report((u8_t *)consumer_report, + sizeof(struct zmk_hid_consumer_report)) != 0) { LOG_DBG("USB Send Failed"); } #endif /* CONFIG_ZMK_USB */ #ifdef CONFIG_ZMK_BLE err = zmk_hog_send_consumer_report(&consumer_report->body); - if (err) - { + if (err) { LOG_ERR("FAILED TO SEND OVER HOG: %d", err); } #endif /* CONFIG_ZMK_BLE */ @@ -58,4 +59,3 @@ int zmk_endpoints_send_report(u8_t usage_page) return 0; } - diff --git a/app/src/event_manager.c b/app/src/event_manager.c index 47ad6b7..226f3ce 100644 --- a/app/src/event_manager.c +++ b/app/src/event_manager.c @@ -1,9 +1,9 @@ /* - * Copyright (c) 2020 Peter Johanson <peter@peterjohanson.com> + * Copyright (c) 2020 The ZMK Contributors * * SPDX-License-Identifier: MIT */ - + #include <zephyr.h> #include <logging/log.h> @@ -11,15 +11,13 @@ LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL); #include <zmk/event-manager.h> -extern struct zmk_event_type* __event_type_start[]; -extern struct zmk_event_type* __event_type_end[]; +extern struct zmk_event_type *__event_type_start[]; +extern struct zmk_event_type *__event_type_end[]; extern struct zmk_event_subscription __event_subscriptions_start[]; extern struct zmk_event_subscription __event_subscriptions_end[]; - -int zmk_event_manager_handle_from(struct zmk_event_header *event, u8_t start_index) -{ +int zmk_event_manager_handle_from(struct zmk_event_header *event, u8_t start_index) { int ret = 0; u8_t len = __event_subscriptions_end - __event_subscriptions_start; for (int i = start_index; i < len; i++) { @@ -31,15 +29,15 @@ int zmk_event_manager_handle_from(struct zmk_event_header *event, u8_t start_ind goto release; } else if (ret > 0) { switch (ret) { - case ZMK_EV_EVENT_HANDLED: - LOG_DBG("Listener handled the event"); - ret = 0; - goto release; - case ZMK_EV_EVENT_CAPTURED: - LOG_DBG("Listener captured the event"); - event->last_listener_index = i; - // Listeners are expected to free events they capture - return 0; + case ZMK_EV_EVENT_HANDLED: + LOG_DBG("Listener handled the event"); + ret = 0; + goto release; + case ZMK_EV_EVENT_CAPTURED: + LOG_DBG("Listener captured the event"); + event->last_listener_index = i; + // Listeners are expected to free events they capture + return 0; } } } @@ -50,19 +48,18 @@ release: return ret; } -int zmk_event_manager_raise(struct zmk_event_header *event) -{ +int zmk_event_manager_raise(struct zmk_event_header *event) { return zmk_event_manager_handle_from(event, 0); } -int zmk_event_manager_raise_after(struct zmk_event_header *event, const struct zmk_listener *listener) -{ +int zmk_event_manager_raise_after(struct zmk_event_header *event, + const struct zmk_listener *listener) { u8_t len = __event_subscriptions_end - __event_subscriptions_start; for (int i = 0; i < len; i++) { struct zmk_event_subscription *ev_sub = __event_subscriptions_start + i; if (ev_sub->event_type == event->event && ev_sub->listener == listener) { - return zmk_event_manager_handle_from(event, i+1); + return zmk_event_manager_handle_from(event, i + 1); } } @@ -71,8 +68,8 @@ int zmk_event_manager_raise_after(struct zmk_event_header *event, const struct z return -EINVAL; } -int zmk_event_manager_raise_at(struct zmk_event_header *event, const struct zmk_listener *listener) -{ +int zmk_event_manager_raise_at(struct zmk_event_header *event, + const struct zmk_listener *listener) { u8_t len = __event_subscriptions_end - __event_subscriptions_start; for (int i = 0; i < len; i++) { struct zmk_event_subscription *ev_sub = __event_subscriptions_start + i; @@ -87,7 +84,6 @@ int zmk_event_manager_raise_at(struct zmk_event_header *event, const struct zmk_ return -EINVAL; } -int zmk_event_manager_release(struct zmk_event_header *event) -{ +int zmk_event_manager_release(struct zmk_event_header *event) { return zmk_event_manager_handle_from(event, event->last_listener_index + 1); } diff --git a/app/src/events/ble_active_profile_changed.c b/app/src/events/ble_active_profile_changed.c new file mode 100644 index 0000000..06988e2 --- /dev/null +++ b/app/src/events/ble_active_profile_changed.c @@ -0,0 +1,10 @@ +/* + * Copyright (c) 2020 The ZMK Contributors + * + * SPDX-License-Identifier: MIT + */ + +#include <kernel.h> +#include <zmk/events/ble-active-profile-changed.h> + +ZMK_EVENT_IMPL(ble_active_profile_changed);
\ No newline at end of file diff --git a/app/src/events/keycode_state_changed.c b/app/src/events/keycode_state_changed.c index 73508e1..9a1984a 100644 --- a/app/src/events/keycode_state_changed.c +++ b/app/src/events/keycode_state_changed.c @@ -1,5 +1,5 @@ /* - * Copyright (c) 2020 Peter Johanson <peter@peterjohanson.com> + * Copyright (c) 2020 The ZMK Contributors * * SPDX-License-Identifier: MIT */ diff --git a/app/src/events/modifiers_state_changed.c b/app/src/events/modifiers_state_changed.c index 71ec015..8becd02 100644 --- a/app/src/events/modifiers_state_changed.c +++ b/app/src/events/modifiers_state_changed.c @@ -1,5 +1,5 @@ /* - * Copyright (c) 2020 Peter Johanson <peter@peterjohanson.com> + * Copyright (c) 2020 The ZMK Contributors * * SPDX-License-Identifier: MIT */ diff --git a/app/src/events/position_state_changed.c b/app/src/events/position_state_changed.c index 69cba03..f8f1a26 100644 --- a/app/src/events/position_state_changed.c +++ b/app/src/events/position_state_changed.c @@ -1,5 +1,5 @@ /* - * Copyright (c) 2020 Peter Johanson <peter@peterjohanson.com> + * Copyright (c) 2020 The ZMK Contributors * * SPDX-License-Identifier: MIT */ diff --git a/app/src/events/sensor_event.c b/app/src/events/sensor_event.c index 1b0302d..c6b80cd 100644 --- a/app/src/events/sensor_event.c +++ b/app/src/events/sensor_event.c @@ -1,5 +1,5 @@ /* - * Copyright (c) 2020 Peter Johanson <peter@peterjohanson.com> + * Copyright (c) 2020 The ZMK Contributors * * SPDX-License-Identifier: MIT */ diff --git a/app/src/ext_power_generic.c b/app/src/ext_power_generic.c new file mode 100644 index 0000000..4817030 --- /dev/null +++ b/app/src/ext_power_generic.c @@ -0,0 +1,91 @@ +/* + * Copyright (c) 2020 The ZMK Contributors + * + * SPDX-License-Identifier: MIT + */ + +#define DT_DRV_COMPAT zmk_ext_power_generic + +#include <device.h> +#include <init.h> +#include <drivers/gpio.h> +#include <drivers/ext_power.h> + +#if DT_HAS_COMPAT_STATUS_OKAY(DT_DRV_COMPAT) + +#include <logging/log.h> +LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL); + +struct ext_power_generic_config { + const char *label; + const u8_t pin; + const u8_t flags; +}; + +struct ext_power_generic_data { + struct device *gpio; + bool status; +}; + +static int ext_power_generic_enable(struct device *dev) { + struct ext_power_generic_data *data = dev->driver_data; + const struct ext_power_generic_config *config = dev->config_info; + + if (gpio_pin_set(data->gpio, config->pin, 1)) { + LOG_WRN("Failed to set ext-power control pin"); + return -EIO; + } + data->status = true; + return 0; +} + +static int ext_power_generic_disable(struct device *dev) { + struct ext_power_generic_data *data = dev->driver_data; + const struct ext_power_generic_config *config = dev->config_info; + + if (gpio_pin_set(data->gpio, config->pin, 0)) { + LOG_WRN("Failed to clear ext-power control pin"); + return -EIO; + } + data->status = false; + return 0; +} + +static int ext_power_generic_get(struct device *dev) { + struct ext_power_generic_data *data = dev->driver_data; + return data->status; +} + +static int ext_power_generic_init(struct device *dev) { + struct ext_power_generic_data *data = dev->driver_data; + const struct ext_power_generic_config *config = dev->config_info; + + data->gpio = device_get_binding(config->label); + if (data->gpio == NULL) { + LOG_ERR("Failed to get ext-power control device"); + return -EINVAL; + } + + if (gpio_pin_configure(data->gpio, config->pin, config->flags | GPIO_OUTPUT)) { + LOG_ERR("Failed to configure ext-power control pin"); + return -EIO; + } + + return 0; +} + +static const struct ext_power_generic_config config = { + .label = DT_INST_GPIO_LABEL(0, control_gpios), + .pin = DT_INST_GPIO_PIN(0, control_gpios), + .flags = DT_INST_GPIO_FLAGS(0, control_gpios)}; + +static struct ext_power_generic_data data = {.status = false}; + +static const struct ext_power_api api = {.enable = ext_power_generic_enable, + .disable = ext_power_generic_disable, + .get = ext_power_generic_get}; + +DEVICE_AND_API_INIT(ext_power_generic, DT_INST_LABEL(0), ext_power_generic_init, &data, &config, + APPLICATION, CONFIG_KERNEL_INIT_PRIORITY_DEFAULT, &api); + +#endif /* DT_HAS_COMPAT_STATUS_OKAY(DT_DRV_COMPAT) */ diff --git a/app/src/hid.c b/app/src/hid.c index ca04056..f80906c 100644 --- a/app/src/hid.c +++ b/app/src/hid.c @@ -1,44 +1,36 @@ +/* + * Copyright (c) 2020 The ZMK Contributors + * + * SPDX-License-Identifier: MIT + */ + #include <logging/log.h> LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL); #include <zmk/hid.h> static struct zmk_hid_keypad_report kp_report = { - .report_id = 1, - .body = { - .modifiers = 0, - .keys = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}}}; - -static struct zmk_hid_consumer_report consumer_report = { - .report_id = 2, - .body = { - .keys = {0,0,0,0,0,0}}}; - -#define _TOGGLE_MOD(mod, state) \ - if (modifier > MOD_RGUI) \ - { \ - return -EINVAL; \ - } \ - WRITE_BIT(kp_report.body.modifiers, mod, state); \ + .report_id = 1, .body = {.modifiers = 0, .keys = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}}}; + +static struct zmk_hid_consumer_report consumer_report = {.report_id = 2, + .body = {.keys = {0, 0, 0, 0, 0, 0}}}; + +#define _TOGGLE_MOD(mod, state) \ + if (modifier > MOD_RGUI) { \ + return -EINVAL; \ + } \ + WRITE_BIT(kp_report.body.modifiers, mod, state); \ return 0; -int zmk_hid_register_mod(zmk_mod modifier) -{ - _TOGGLE_MOD(modifier, true); -} -int zmk_hid_unregister_mod(zmk_mod modifier) -{ - _TOGGLE_MOD(modifier, false); -} +int zmk_hid_register_mod(zmk_mod modifier) { _TOGGLE_MOD(modifier, true); } +int zmk_hid_unregister_mod(zmk_mod modifier) { _TOGGLE_MOD(modifier, false); } -int zmk_hid_register_mods(zmk_mod_flags modifiers) -{ +int zmk_hid_register_mods(zmk_mod_flags modifiers) { kp_report.body.modifiers |= modifiers; return 0; } -int zmk_hid_unregister_mods(zmk_mod_flags modifiers) -{ +int zmk_hid_unregister_mods(zmk_mod_flags modifiers) { kp_report.body.modifiers &= ~modifiers; return 0; } @@ -61,28 +53,21 @@ 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(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; \ +#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; \ } - -int zmk_hid_keypad_press(zmk_key code) -{ - if (code >= LCTL && code <= RGUI) - { +int zmk_hid_keypad_press(zmk_key code) { + if (code >= LCTL && code <= RGUI) { return zmk_hid_register_mod(code - LCTL); } - - if (code > ZMK_HID_MAX_KEYCODE) - { + if (code > ZMK_HID_MAX_KEYCODE) { return -EINVAL; } @@ -93,15 +78,12 @@ int zmk_hid_keypad_press(zmk_key code) return 0; }; -int zmk_hid_keypad_release(zmk_key code) -{ - if (code >= LCTL && code <= RGUI) - { +int zmk_hid_keypad_release(zmk_key code) { + if (code >= LCTL && code <= RGUI) { return zmk_hid_unregister_mod(code - LCTL); } - if (code > ZMK_HID_MAX_KEYCODE) - { + if (code > ZMK_HID_MAX_KEYCODE) { return -EINVAL; } @@ -112,25 +94,20 @@ int zmk_hid_keypad_release(zmk_key code) return 0; }; -int zmk_hid_consumer_press(zmk_key code) -{ +int zmk_hid_consumer_press(zmk_key code) { TOGGLE_CONSUMER(0U, code); return 0; }; - -int zmk_hid_consumer_release(zmk_key code) -{ +int zmk_hid_consumer_release(zmk_key code) { TOGGLE_CONSUMER(code, 0U); return 0; }; -struct zmk_hid_keypad_report *zmk_hid_get_keypad_report() -{ +struct zmk_hid_keypad_report *zmk_hid_get_keypad_report() { return &kp_report; } -struct zmk_hid_consumer_report *zmk_hid_get_consumer_report() -{ +struct zmk_hid_consumer_report *zmk_hid_get_consumer_report() { return &consumer_report; } diff --git a/app/src/hid_listener.c b/app/src/hid_listener.c index 69aa23e..4467e6d 100644 --- a/app/src/hid_listener.c +++ b/app/src/hid_listener.c @@ -1,5 +1,5 @@ /* - * Copyright (c) 2020 Peter Johanson <peter@peterjohanson.com> + * Copyright (c) 2020 The ZMK Contributors * * SPDX-License-Identifier: MIT */ @@ -15,90 +15,84 @@ LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL); #include <zmk/hid.h> #include <zmk/endpoints.h> +static int hid_listener_keycode_pressed(u8_t usage_page, u32_t keycode) { + int err; + LOG_DBG("usage_page 0x%02X keycode 0x%02X", usage_page, keycode); -static int hid_listener_keycode_pressed(u8_t usage_page, u32_t keycode) -{ - int err; - LOG_DBG("usage_page 0x%02X keycode 0x%02X", usage_page, keycode); - - switch (usage_page) { - case USAGE_KEYPAD: - err = zmk_hid_keypad_press(keycode); - if (err) { - LOG_ERR("Unable to press keycode"); - return err; + 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; } - 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); + return zmk_endpoints_send_report(usage_page); } -static int hid_listener_keycode_released(u8_t usage_page, u32_t keycode) -{ - int err; - LOG_DBG("usage_page 0x%02X keycode 0x%02X", usage_page, keycode); - - switch (usage_page) { - case USAGE_KEYPAD: - err = zmk_hid_keypad_release(keycode); - if (err) { - LOG_ERR("Unable to release keycode"); - return err; - } - break; - case USAGE_CONSUMER: - err = zmk_hid_consumer_release(keycode); - if (err) { - LOG_ERR("Unable to release keycode"); - return err; +static int hid_listener_keycode_released(u8_t usage_page, u32_t keycode) { + int err; + LOG_DBG("usage_page 0x%02X keycode 0x%02X", usage_page, keycode); + + switch (usage_page) { + case USAGE_KEYPAD: + err = zmk_hid_keypad_release(keycode); + if (err) { + LOG_ERR("Unable to release keycode"); + return err; + } + break; + case USAGE_CONSUMER: + err = zmk_hid_consumer_release(keycode); + if (err) { + LOG_ERR("Unable to release keycode"); + return err; + } + break; } - break; - } - return zmk_endpoints_send_report(usage_page); + return zmk_endpoints_send_report(usage_page); } -static int hid_listener_modifiers_pressed(zmk_mod_flags modifiers) -{ - LOG_DBG("modifiers %d", modifiers); - - zmk_hid_register_mods(modifiers); - return zmk_endpoints_send_report(USAGE_KEYPAD); +static int hid_listener_modifiers_pressed(zmk_mod_flags modifiers) { + LOG_DBG("modifiers %d", modifiers); + + zmk_hid_register_mods(modifiers); + return zmk_endpoints_send_report(USAGE_KEYPAD); } -static int hid_listener_modifiers_released(zmk_mod_flags modifiers) -{ - LOG_DBG("modifiers %d", modifiers); - - zmk_hid_unregister_mods(modifiers); - return zmk_endpoints_send_report(USAGE_KEYPAD); +static int hid_listener_modifiers_released(zmk_mod_flags modifiers) { + LOG_DBG("modifiers %d", modifiers); + + zmk_hid_unregister_mods(modifiers); + return zmk_endpoints_send_report(USAGE_KEYPAD); } -int hid_listener(const struct zmk_event_header *eh) -{ - if (is_keycode_state_changed(eh)) { - const struct keycode_state_changed *ev = cast_keycode_state_changed(eh); - if (ev->state) { - hid_listener_keycode_pressed(ev->usage_page, ev->keycode); - } else { - hid_listener_keycode_released(ev->usage_page, ev->keycode); - } - } else if (is_modifiers_state_changed(eh)) { - const struct modifiers_state_changed *ev = cast_modifiers_state_changed(eh); - if (ev->state) { - hid_listener_modifiers_pressed(ev->modifiers); - } else { - hid_listener_modifiers_released(ev->modifiers); +int hid_listener(const struct zmk_event_header *eh) { + if (is_keycode_state_changed(eh)) { + const struct keycode_state_changed *ev = cast_keycode_state_changed(eh); + if (ev->state) { + hid_listener_keycode_pressed(ev->usage_page, ev->keycode); + } else { + hid_listener_keycode_released(ev->usage_page, ev->keycode); + } + } else if (is_modifiers_state_changed(eh)) { + const struct modifiers_state_changed *ev = cast_modifiers_state_changed(eh); + if (ev->state) { + hid_listener_modifiers_pressed(ev->modifiers); + } else { + hid_listener_modifiers_released(ev->modifiers); + } } - } - return 0; + return 0; } ZMK_LISTENER(hid_listener, hid_listener); diff --git a/app/src/hog.c b/app/src/hog.c index 589c28f..bcd652d 100644 --- a/app/src/hog.c +++ b/app/src/hog.c @@ -1,5 +1,15 @@ +/* + * Copyright (c) 2020 The ZMK Contributors + * + * SPDX-License-Identifier: MIT + */ + #include <settings/settings.h> +#include <logging/log.h> + +LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL); + #include <bluetooth/bluetooth.h> #include <bluetooth/gatt.h> @@ -7,21 +17,18 @@ #include <zmk/hog.h> #include <zmk/hid.h> -enum -{ +enum { HIDS_REMOTE_WAKE = BIT(0), HIDS_NORMALLY_CONNECTABLE = BIT(1), }; -struct hids_info -{ +struct hids_info { u16_t version; /* version number of base USB HID Specification */ u8_t code; /* country HID Device hardware is localized for. */ u8_t flags; } __packed; -struct hids_report -{ +struct hids_report { u8_t id; /* report id */ u8_t type; /* report type */ } __packed; @@ -32,8 +39,7 @@ static struct hids_info info = { .flags = HIDS_NORMALLY_CONNECTABLE & HIDS_REMOTE_WAKE, }; -enum -{ +enum { HIDS_INPUT = 0x01, HIDS_OUTPUT = 0x02, HIDS_FEATURE = 0x03, @@ -53,31 +59,37 @@ static bool host_requests_notification = false; static u8_t ctrl_point; // static u8_t proto_mode; -static ssize_t read_hids_info(struct bt_conn *conn, const struct bt_gatt_attr *attr, void *buf, u16_t len, u16_t offset) -{ - return bt_gatt_attr_read(conn, attr, buf, len, offset, attr->user_data, sizeof(struct hids_info)); +static ssize_t read_hids_info(struct bt_conn *conn, const struct bt_gatt_attr *attr, void *buf, + u16_t len, u16_t offset) { + return bt_gatt_attr_read(conn, attr, buf, len, offset, attr->user_data, + sizeof(struct hids_info)); } -static ssize_t read_hids_report_ref(struct bt_conn *conn, const struct bt_gatt_attr *attr, void *buf, u16_t len, u16_t offset) -{ - return bt_gatt_attr_read(conn, attr, buf, len, offset, attr->user_data, sizeof(struct hids_report)); +static ssize_t read_hids_report_ref(struct bt_conn *conn, const struct bt_gatt_attr *attr, + void *buf, u16_t len, u16_t offset) { + return bt_gatt_attr_read(conn, attr, buf, len, offset, attr->user_data, + sizeof(struct hids_report)); } -static ssize_t read_hids_report_map(struct bt_conn *conn, const struct bt_gatt_attr *attr, void *buf, u16_t len, u16_t offset) -{ - return bt_gatt_attr_read(conn, attr, buf, len, offset, zmk_hid_report_desc, sizeof(zmk_hid_report_desc)); +static ssize_t read_hids_report_map(struct bt_conn *conn, const struct bt_gatt_attr *attr, + void *buf, u16_t len, u16_t offset) { + return bt_gatt_attr_read(conn, attr, buf, len, offset, zmk_hid_report_desc, + sizeof(zmk_hid_report_desc)); } -static ssize_t read_hids_input_report(struct bt_conn *conn, const struct bt_gatt_attr *attr, void *buf, u16_t len, u16_t offset) -{ +static ssize_t read_hids_input_report(struct bt_conn *conn, const struct bt_gatt_attr *attr, + void *buf, u16_t len, u16_t offset) { struct zmk_hid_keypad_report_body *report_body = &zmk_hid_get_keypad_report()->body; - return bt_gatt_attr_read(conn, attr, buf, len, offset, report_body, sizeof(struct zmk_hid_keypad_report_body)); + return bt_gatt_attr_read(conn, attr, buf, len, offset, report_body, + sizeof(struct zmk_hid_keypad_report_body)); } -static ssize_t read_hids_consumer_input_report(struct bt_conn *conn, const struct bt_gatt_attr *attr, void *buf, u16_t len, u16_t offset) -{ +static ssize_t read_hids_consumer_input_report(struct bt_conn *conn, + const struct bt_gatt_attr *attr, void *buf, + u16_t len, u16_t offset) { struct zmk_hid_consumer_report_body *report_body = &zmk_hid_get_consumer_report()->body; - return bt_gatt_attr_read(conn, attr, buf, len, offset, report_body, sizeof(struct zmk_hid_consumer_report_body)); + return bt_gatt_attr_read(conn, attr, buf, len, offset, report_body, + sizeof(struct zmk_hid_consumer_report_body)); } // static ssize_t write_proto_mode(struct bt_conn *conn, @@ -89,20 +101,15 @@ static ssize_t read_hids_consumer_input_report(struct bt_conn *conn, const struc // return 0; // } -static void input_ccc_changed(const struct bt_gatt_attr *attr, u16_t value) -{ +static void input_ccc_changed(const struct bt_gatt_attr *attr, u16_t value) { host_requests_notification = (value == BT_GATT_CCC_NOTIFY) ? 1 : 0; } -static ssize_t write_ctrl_point(struct bt_conn *conn, - const struct bt_gatt_attr *attr, - const void *buf, u16_t len, u16_t offset, - u8_t flags) -{ +static ssize_t write_ctrl_point(struct bt_conn *conn, const struct bt_gatt_attr *attr, + const void *buf, u16_t len, u16_t offset, u8_t flags) { u8_t *value = attr->user_data; - if (offset + len > sizeof(ctrl_point)) - { + if (offset + len > sizeof(ctrl_point)) { return BT_GATT_ERR(BT_ATT_ERR_INVALID_OFFSET); } @@ -112,42 +119,65 @@ static ssize_t write_ctrl_point(struct bt_conn *conn, } /* HID Service Declaration */ -BT_GATT_SERVICE_DEFINE(hog_svc, - BT_GATT_PRIMARY_SERVICE(BT_UUID_HIDS), - // BT_GATT_CHARACTERISTIC(BT_UUID_HIDS_PROTOCOL_MODE, BT_GATT_CHRC_WRITE_WITHOUT_RESP, - // BT_GATT_PERM_WRITE, NULL, write_proto_mode, &proto_mode), - BT_GATT_CHARACTERISTIC(BT_UUID_HIDS_INFO, BT_GATT_CHRC_READ, - BT_GATT_PERM_READ, read_hids_info, NULL, &info), - BT_GATT_CHARACTERISTIC(BT_UUID_HIDS_REPORT_MAP, BT_GATT_CHRC_READ, - BT_GATT_PERM_READ, read_hids_report_map, NULL, NULL), - - BT_GATT_CHARACTERISTIC(BT_UUID_HIDS_REPORT, - BT_GATT_CHRC_READ | BT_GATT_CHRC_NOTIFY, - BT_GATT_PERM_READ_ENCRYPT, - read_hids_input_report, NULL, NULL), - BT_GATT_CCC(input_ccc_changed, - BT_GATT_PERM_READ_ENCRYPT | BT_GATT_PERM_WRITE_ENCRYPT), - BT_GATT_DESCRIPTOR(BT_UUID_HIDS_REPORT_REF, BT_GATT_PERM_READ, - read_hids_report_ref, NULL, &input), - BT_GATT_CHARACTERISTIC(BT_UUID_HIDS_REPORT, - BT_GATT_CHRC_READ | BT_GATT_CHRC_NOTIFY, - BT_GATT_PERM_READ_ENCRYPT, - read_hids_consumer_input_report, NULL, NULL), - BT_GATT_CCC(input_ccc_changed, - BT_GATT_PERM_READ_ENCRYPT | BT_GATT_PERM_WRITE_ENCRYPT), - BT_GATT_DESCRIPTOR(BT_UUID_HIDS_REPORT_REF, BT_GATT_PERM_READ, - read_hids_report_ref, NULL, &consumer_input), - BT_GATT_CHARACTERISTIC(BT_UUID_HIDS_CTRL_POINT, - BT_GATT_CHRC_WRITE_WITHOUT_RESP, - BT_GATT_PERM_WRITE, - NULL, write_ctrl_point, &ctrl_point)); - -int zmk_hog_send_keypad_report(struct zmk_hid_keypad_report_body *report) -{ - return bt_gatt_notify(NULL, &hog_svc.attrs[5], report, sizeof(struct zmk_hid_keypad_report_body)); +BT_GATT_SERVICE_DEFINE( + hog_svc, BT_GATT_PRIMARY_SERVICE(BT_UUID_HIDS), + // BT_GATT_CHARACTERISTIC(BT_UUID_HIDS_PROTOCOL_MODE, BT_GATT_CHRC_WRITE_WITHOUT_RESP, + // BT_GATT_PERM_WRITE, NULL, write_proto_mode, &proto_mode), + BT_GATT_CHARACTERISTIC(BT_UUID_HIDS_INFO, BT_GATT_CHRC_READ, BT_GATT_PERM_READ, read_hids_info, + NULL, &info), + BT_GATT_CHARACTERISTIC(BT_UUID_HIDS_REPORT_MAP, BT_GATT_CHRC_READ, BT_GATT_PERM_READ, + read_hids_report_map, NULL, NULL), + + BT_GATT_CHARACTERISTIC(BT_UUID_HIDS_REPORT, BT_GATT_CHRC_READ | BT_GATT_CHRC_NOTIFY, + BT_GATT_PERM_READ_ENCRYPT, read_hids_input_report, NULL, NULL), + BT_GATT_CCC(input_ccc_changed, BT_GATT_PERM_READ_ENCRYPT | BT_GATT_PERM_WRITE_ENCRYPT), + BT_GATT_DESCRIPTOR(BT_UUID_HIDS_REPORT_REF, BT_GATT_PERM_READ, read_hids_report_ref, NULL, + &input), + BT_GATT_CHARACTERISTIC(BT_UUID_HIDS_REPORT, BT_GATT_CHRC_READ | BT_GATT_CHRC_NOTIFY, + BT_GATT_PERM_READ_ENCRYPT, read_hids_consumer_input_report, NULL, NULL), + BT_GATT_CCC(input_ccc_changed, BT_GATT_PERM_READ_ENCRYPT | BT_GATT_PERM_WRITE_ENCRYPT), + BT_GATT_DESCRIPTOR(BT_UUID_HIDS_REPORT_REF, BT_GATT_PERM_READ, read_hids_report_ref, NULL, + &consumer_input), + BT_GATT_CHARACTERISTIC(BT_UUID_HIDS_CTRL_POINT, BT_GATT_CHRC_WRITE_WITHOUT_RESP, + BT_GATT_PERM_WRITE, NULL, write_ctrl_point, &ctrl_point)); + +struct bt_conn *destination_connection() { + struct bt_conn *conn; + bt_addr_le_t *addr = zmk_ble_active_profile_addr(); + LOG_DBG("Address pointer %p", addr); + if (!bt_addr_le_cmp(addr, BT_ADDR_LE_ANY)) { + LOG_WRN("Not sending, no active address for current profile"); + return NULL; + } else if ((conn = bt_conn_lookup_addr_le(BT_ID_DEFAULT, addr)) == NULL) { + LOG_WRN("Not sending, not connected to active profile"); + return NULL; + } + + return conn; +} + +int zmk_hog_send_keypad_report(struct zmk_hid_keypad_report_body *report) { + struct bt_conn *conn = destination_connection(); + if (conn == NULL) { + return -ENOTCONN; + } + + LOG_DBG("Sending to NULL? %s", conn == NULL ? "yes" : "no"); + + int err = + bt_gatt_notify(conn, &hog_svc.attrs[5], report, sizeof(struct zmk_hid_keypad_report_body)); + bt_conn_unref(conn); + return err; }; -int zmk_hog_send_consumer_report(struct zmk_hid_consumer_report_body *report) -{ - return bt_gatt_notify(NULL, &hog_svc.attrs[10], report, sizeof(struct zmk_hid_consumer_report_body)); +int zmk_hog_send_consumer_report(struct zmk_hid_consumer_report_body *report) { + struct bt_conn *conn = destination_connection(); + if (conn == NULL) { + return -ENOTCONN; + } + + int err = bt_gatt_notify(conn, &hog_svc.attrs[10], report, + sizeof(struct zmk_hid_consumer_report_body)); + bt_conn_unref(conn); + return err; }; diff --git a/app/src/keymap.c b/app/src/keymap.c index 57cdad6..74fe60d 100644 --- a/app/src/keymap.c +++ b/app/src/keymap.c @@ -1,3 +1,8 @@ +/* + * Copyright (c) 2020 The ZMK Contributors + * + * SPDX-License-Identifier: MIT + */ #include <sys/util.h> #include <logging/log.h> @@ -19,33 +24,38 @@ static u8_t zmk_keymap_layer_default = 0; #define DT_DRV_COMPAT zmk_keymap -#define LAYER_CHILD_LEN(node) 1+ +#define LAYER_CHILD_LEN(node) 1 + #define ZMK_KEYMAP_NODE DT_DRV_INST(0) -#define ZMK_KEYMAP_LAYERS_LEN (DT_INST_FOREACH_CHILD(0, LAYER_CHILD_LEN) 0) +#define ZMK_KEYMAP_LAYERS_LEN (DT_INST_FOREACH_CHILD(0, LAYER_CHILD_LEN) 0) #define LAYER_NODE(l) DT_PHANDLE_BY_IDX(ZMK_KEYMAP_NODE, layers, l) -#define _TRANSFORM_ENTRY(idx, layer) \ - { .behavior_dev = DT_LABEL(DT_PHANDLE_BY_IDX(layer, bindings, idx)), \ - .param1 = COND_CODE_0(DT_PHA_HAS_CELL_AT_IDX(layer, bindings, idx, param1), (0), (DT_PHA_BY_IDX(layer, bindings, idx, param1))), \ - .param2 = COND_CODE_0(DT_PHA_HAS_CELL_AT_IDX(layer, bindings, idx, param2), (0), (DT_PHA_BY_IDX(layer, bindings, idx, param2))), \ - }, - -#define TRANSFORMED_LAYER(node) \ - { UTIL_LISTIFY(DT_PROP_LEN(node, bindings), _TRANSFORM_ENTRY, node) }, +#define _TRANSFORM_ENTRY(idx, layer) \ + { \ + .behavior_dev = DT_LABEL(DT_PHANDLE_BY_IDX(layer, bindings, idx)), \ + .param1 = COND_CODE_0(DT_PHA_HAS_CELL_AT_IDX(layer, bindings, idx, param1), (0), \ + (DT_PHA_BY_IDX(layer, bindings, idx, param1))), \ + .param2 = COND_CODE_0(DT_PHA_HAS_CELL_AT_IDX(layer, bindings, idx, param2), (0), \ + (DT_PHA_BY_IDX(layer, bindings, idx, param2))), \ + }, +#define TRANSFORMED_LAYER(node) {UTIL_LISTIFY(DT_PROP_LEN(node, bindings), _TRANSFORM_ENTRY, node)}, #if ZMK_KEYMAP_HAS_SENSORS -#define _TRANSFORM_SENSOR_ENTRY(idx, layer) \ - { .behavior_dev = DT_LABEL(DT_PHANDLE_BY_IDX(layer, sensor_bindings, idx)), \ - .param1 = COND_CODE_0(DT_PHA_HAS_CELL_AT_IDX(layer, sensor_bindings, idx, param1), (0), (DT_PHA_BY_IDX(layer, sensor_bindings, idx, param1))), \ - .param2 = COND_CODE_0(DT_PHA_HAS_CELL_AT_IDX(layer, sensor_bindings, idx, param2), (0), (DT_PHA_BY_IDX(layer, sensor_bindings, idx, param2))), \ - }, - -#define SENSOR_LAYER(node) \ - COND_CODE_1(DT_NODE_HAS_PROP(node, sensor_bindings), \ - ({ UTIL_LISTIFY(DT_PROP_LEN(node, sensor_bindings), _TRANSFORM_SENSOR_ENTRY, node) }), \ - ({})), +#define _TRANSFORM_SENSOR_ENTRY(idx, layer) \ + { \ + .behavior_dev = DT_LABEL(DT_PHANDLE_BY_IDX(layer, sensor_bindings, idx)), \ + .param1 = COND_CODE_0(DT_PHA_HAS_CELL_AT_IDX(layer, sensor_bindings, idx, param1), (0), \ + (DT_PHA_BY_IDX(layer, sensor_bindings, idx, param1))), \ + .param2 = COND_CODE_0(DT_PHA_HAS_CELL_AT_IDX(layer, sensor_bindings, idx, param2), (0), \ + (DT_PHA_BY_IDX(layer, sensor_bindings, idx, param2))), \ + }, + +#define SENSOR_LAYER(node) \ + COND_CODE_1( \ + DT_NODE_HAS_PROP(node, sensor_bindings), \ + ({UTIL_LISTIFY(DT_PROP_LEN(node, sensor_bindings), _TRANSFORM_SENSOR_ENTRY, node)}), \ + ({})), #endif /* ZMK_KEYMAP_HAS_SENSORS */ @@ -57,155 +67,144 @@ static u8_t zmk_keymap_layer_default = 0; static u32_t zmk_keymap_active_behavior_layer[ZMK_KEYMAP_LEN]; static struct zmk_behavior_binding zmk_keymap[ZMK_KEYMAP_LAYERS_LEN][ZMK_KEYMAP_LEN] = { - DT_INST_FOREACH_CHILD(0, TRANSFORMED_LAYER) -}; + DT_INST_FOREACH_CHILD(0, TRANSFORMED_LAYER)}; #if ZMK_KEYMAP_HAS_SENSORS -static struct zmk_behavior_binding zmk_sensor_keymap[ZMK_KEYMAP_LAYERS_LEN][ZMK_KEYMAP_SENSORS_LEN] = { - DT_INST_FOREACH_CHILD(0, SENSOR_LAYER) -}; +static struct zmk_behavior_binding zmk_sensor_keymap[ZMK_KEYMAP_LAYERS_LEN] + [ZMK_KEYMAP_SENSORS_LEN] = { + DT_INST_FOREACH_CHILD(0, SENSOR_LAYER)}; #endif /* ZMK_KEYMAP_HAS_SENSORS */ -#define SET_LAYER_STATE(layer, state) \ - if (layer >= 32) \ - { \ - return -EINVAL; \ - } \ - WRITE_BIT(zmk_keymap_layer_state, layer, state); \ - return 0; - -bool zmk_keymap_layer_active(u8_t layer) -{ - return (zmk_keymap_layer_state & (BIT(layer))) == (BIT(layer)); -}; +#define SET_LAYER_STATE(layer, state) \ + if (layer >= 32) { \ + return -EINVAL; \ + } \ + WRITE_BIT(zmk_keymap_layer_state, layer, state); \ + return 0; -int zmk_keymap_layer_activate(u8_t layer) -{ - SET_LAYER_STATE(layer, true); +bool zmk_keymap_layer_active(u8_t layer) { + return (zmk_keymap_layer_state & (BIT(layer))) == (BIT(layer)); }; -int zmk_keymap_layer_deactivate(u8_t layer) -{ - SET_LAYER_STATE(layer, false); -}; +int zmk_keymap_layer_activate(u8_t layer) { SET_LAYER_STATE(layer, true); }; + +int zmk_keymap_layer_deactivate(u8_t layer) { SET_LAYER_STATE(layer, false); }; -int zmk_keymap_layer_toggle(u8_t layer) -{ - if (zmk_keymap_layer_active(layer)) - { - return zmk_keymap_layer_deactivate(layer); - } +int zmk_keymap_layer_toggle(u8_t layer) { + if (zmk_keymap_layer_active(layer)) { + return zmk_keymap_layer_deactivate(layer); + } - return zmk_keymap_layer_activate(layer); + return zmk_keymap_layer_activate(layer); }; -bool is_active_layer(u8_t layer, u32_t layer_state) -{ - return (layer_state & BIT(layer)) == BIT(layer) - || layer == zmk_keymap_layer_default; +bool is_active_layer(u8_t layer, u32_t layer_state) { + return (layer_state & BIT(layer)) == BIT(layer) || layer == zmk_keymap_layer_default; } -int zmk_keymap_apply_position_state(int layer, u32_t position, bool pressed) -{ - struct zmk_behavior_binding *binding = &zmk_keymap[layer][position]; - struct device *behavior; - - LOG_DBG("layer: %d position: %d, binding name: %s", layer, position, log_strdup(binding->behavior_dev)); - - behavior = device_get_binding(binding->behavior_dev); - - if (!behavior) { - LOG_DBG("No behavior assigned to %d on layer %d", position, layer); - return 1; - } - - if (pressed) { - return behavior_keymap_binding_pressed(behavior, position, binding->param1, binding->param2); - } else { - return behavior_keymap_binding_released(behavior, position, binding->param1, binding->param2); - } +int zmk_keymap_apply_position_state(int layer, u32_t position, bool pressed, s64_t timestamp) { + struct zmk_behavior_binding *binding = &zmk_keymap[layer][position]; + struct device *behavior; + struct zmk_behavior_binding_event event = { + .layer = layer, + .position = position, + .timestamp = timestamp, + }; + + LOG_DBG("layer: %d position: %d, binding name: %s", layer, position, + log_strdup(binding->behavior_dev)); + + behavior = device_get_binding(binding->behavior_dev); + + if (!behavior) { + LOG_DBG("No behavior assigned to %d on layer %d", position, layer); + return 1; + } + + if (pressed) { + return behavior_keymap_binding_pressed(binding, event); + } else { + return behavior_keymap_binding_released(binding, event); + } } - -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--) - { - u32_t layer_state = pressed ? zmk_keymap_layer_state : zmk_keymap_active_behavior_layer[position]; - if (is_active_layer(layer, layer_state)) - { - int ret = zmk_keymap_apply_position_state(layer, position, pressed); - - zmk_keymap_active_behavior_layer[position] = zmk_keymap_layer_state; - - 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 -ENOTSUP; + +int zmk_keymap_position_state_changed(u32_t position, bool pressed, s64_t timestamp) { + for (int layer = ZMK_KEYMAP_LAYERS_LEN - 1; layer >= zmk_keymap_layer_default; layer--) { + u32_t layer_state = + pressed ? zmk_keymap_layer_state : zmk_keymap_active_behavior_layer[position]; + if (is_active_layer(layer, layer_state)) { + int ret = zmk_keymap_apply_position_state(layer, position, pressed, timestamp); + + zmk_keymap_active_behavior_layer[position] = zmk_keymap_layer_state; + + 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 -ENOTSUP; } #if ZMK_KEYMAP_HAS_SENSORS -int zmk_keymap_sensor_triggered(u8_t sensor_number, struct device *sensor) -{ - 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) && zmk_sensor_keymap[layer] != NULL) - { - struct zmk_behavior_binding *binding = &zmk_sensor_keymap[layer][sensor_number]; - struct device *behavior; - int ret; - - LOG_DBG("layer: %d sensor_number: %d, binding name: %s", layer, sensor_number, log_strdup(binding->behavior_dev)); - - behavior = device_get_binding(binding->behavior_dev); - - if (!behavior) { - LOG_DBG("No behavior assigned to %d on layer %d", sensor_number, layer); - continue; - } - - ret = behavior_sensor_keymap_binding_triggered(behavior, sensor, binding->param1, binding->param2); - - 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 -ENOTSUP; +int zmk_keymap_sensor_triggered(u8_t sensor_number, struct device *sensor) { + 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) && + zmk_sensor_keymap[layer] != NULL) { + struct zmk_behavior_binding *binding = &zmk_sensor_keymap[layer][sensor_number]; + struct device *behavior; + int ret; + + LOG_DBG("layer: %d sensor_number: %d, binding name: %s", layer, sensor_number, + log_strdup(binding->behavior_dev)); + + behavior = device_get_binding(binding->behavior_dev); + + if (!behavior) { + LOG_DBG("No behavior assigned to %d on layer %d", sensor_number, layer); + continue; + } + + ret = behavior_sensor_keymap_binding_triggered(binding, sensor); + + 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 -ENOTSUP; } #endif /* ZMK_KEYMAP_HAS_SENSORS */ -int keymap_listener(const struct zmk_event_header *eh) -{ - if (is_position_state_changed(eh)) { - const struct position_state_changed *ev = cast_position_state_changed(eh); - return zmk_keymap_position_state_changed(ev->position, ev->state); +int keymap_listener(const struct zmk_event_header *eh) { + if (is_position_state_changed(eh)) { + const struct position_state_changed *ev = cast_position_state_changed(eh); + return zmk_keymap_position_state_changed(ev->position, ev->state, ev->timestamp); #if ZMK_KEYMAP_HAS_SENSORS - } else if (is_sensor_event(eh)) { - const struct sensor_event *ev = cast_sensor_event(eh); - return zmk_keymap_sensor_triggered(ev->sensor_number, ev->sensor); + } else if (is_sensor_event(eh)) { + const struct sensor_event *ev = cast_sensor_event(eh); + return zmk_keymap_sensor_triggered(ev->sensor_number, ev->sensor); #endif /* ZMK_KEYMAP_HAS_SENSORS */ - } + } - return -ENOTSUP; + return -ENOTSUP; } ZMK_LISTENER(keymap, keymap_listener); @@ -214,4 +213,3 @@ ZMK_SUBSCRIPTION(keymap, position_state_changed); #if ZMK_KEYMAP_HAS_SENSORS ZMK_SUBSCRIPTION(keymap, sensor_event); #endif /* ZMK_KEYMAP_HAS_SENSORS */ - diff --git a/app/src/kscan.c b/app/src/kscan.c index 63713f1..8575e70 100644 --- a/app/src/kscan.c +++ b/app/src/kscan.c @@ -1,5 +1,5 @@ /* - * Copyright (c) 2020 Peter Johanson + * Copyright (c) 2020 The ZMK Contributors * * SPDX-License-Identifier: MIT */ @@ -18,61 +18,56 @@ LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL); #define ZMK_KSCAN_EVENT_STATE_PRESSED 0 #define ZMK_KSCAN_EVENT_STATE_RELEASED 1 -struct zmk_kscan_event -{ - u32_t row; - u32_t column; - u32_t state; +struct zmk_kscan_event { + u32_t row; + u32_t column; + u32_t state; }; -struct zmk_kscan_msg_processor -{ - struct k_work work; +struct zmk_kscan_msg_processor { + struct k_work work; } msg_processor; K_MSGQ_DEFINE(zmk_kscan_msgq, sizeof(struct zmk_kscan_event), CONFIG_ZMK_KSCAN_EVENT_QUEUE_SIZE, 4); -static void zmk_kscan_callback(struct device *dev, u32_t row, u32_t column, bool pressed) -{ - struct zmk_kscan_event ev = { - .row = row, - .column = column, - .state = (pressed ? ZMK_KSCAN_EVENT_STATE_PRESSED : ZMK_KSCAN_EVENT_STATE_RELEASED)}; +static void zmk_kscan_callback(struct device *dev, u32_t row, u32_t column, bool pressed) { + struct zmk_kscan_event ev = { + .row = row, + .column = column, + .state = (pressed ? ZMK_KSCAN_EVENT_STATE_PRESSED : ZMK_KSCAN_EVENT_STATE_RELEASED)}; - k_msgq_put(&zmk_kscan_msgq, &ev, K_NO_WAIT); - k_work_submit(&msg_processor.work); + k_msgq_put(&zmk_kscan_msgq, &ev, K_NO_WAIT); + k_work_submit(&msg_processor.work); } -void zmk_kscan_process_msgq(struct k_work *item) -{ - struct zmk_kscan_event ev; +void zmk_kscan_process_msgq(struct k_work *item) { + struct zmk_kscan_event ev; - while (k_msgq_get(&zmk_kscan_msgq, &ev, K_NO_WAIT) == 0) - { - bool pressed = (ev.state == ZMK_KSCAN_EVENT_STATE_PRESSED); - u32_t position = zmk_matrix_transform_row_column_to_position(ev.row, ev.column); - struct position_state_changed *pos_ev; - LOG_DBG("Row: %d, col: %d, position: %d, pressed: %s\n", ev.row, ev.column, position, (pressed ? "true" : "false")); - pos_ev = new_position_state_changed(); - pos_ev->state = pressed; - pos_ev->position = position; - ZMK_EVENT_RAISE(pos_ev); - } + while (k_msgq_get(&zmk_kscan_msgq, &ev, K_NO_WAIT) == 0) { + bool pressed = (ev.state == ZMK_KSCAN_EVENT_STATE_PRESSED); + u32_t position = zmk_matrix_transform_row_column_to_position(ev.row, ev.column); + struct position_state_changed *pos_ev; + LOG_DBG("Row: %d, col: %d, position: %d, pressed: %s\n", ev.row, ev.column, position, + (pressed ? "true" : "false")); + pos_ev = new_position_state_changed(); + pos_ev->state = pressed; + pos_ev->position = position; + pos_ev->timestamp = k_uptime_get(); + ZMK_EVENT_RAISE(pos_ev); + } } -int zmk_kscan_init(char *name) -{ - struct device *dev = device_get_binding(name); - if (dev == NULL) - { - LOG_ERR("Failed to get the KSCAN device"); - return -EINVAL; - } +int zmk_kscan_init(char *name) { + struct device *dev = device_get_binding(name); + if (dev == NULL) { + LOG_ERR("Failed to get the KSCAN device"); + return -EINVAL; + } - k_work_init(&msg_processor.work, zmk_kscan_process_msgq); + k_work_init(&msg_processor.work, zmk_kscan_process_msgq); - kscan_config(dev, zmk_kscan_callback); - kscan_enable_callback(dev); + kscan_config(dev, zmk_kscan_callback); + kscan_enable_callback(dev); - return 0; + return 0; } diff --git a/app/src/kscan_composite.c b/app/src/kscan_composite.c index d46484b..f8e8d60 100644 --- a/app/src/kscan_composite.c +++ b/app/src/kscan_composite.c @@ -1,5 +1,5 @@ /* - * Copyright (c) 2020 Peter Johanson <peter@peterjohanson.com> + * Copyright (c) 2020 The ZMK Contributors * * SPDX-License-Identifier: MIT */ @@ -15,37 +15,30 @@ LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL); #define MATRIX_ROWS DT_PROP(MATRIX_NODE_ID, rows) #define MATRIX_COLS DT_PROP(MATRIX_NODE_ID, columns) -struct kscan_composite_child_config -{ +struct kscan_composite_child_config { char *label; u8_t row_offset; u8_t column_offset; }; -#define CHILD_CONFIG(inst) \ - { \ - .label = DT_LABEL(DT_PHANDLE(inst, kscan)), \ - .row_offset = DT_PROP(inst, row_offset), \ - .column_offset = DT_PROP(inst, column_offset)}, +#define CHILD_CONFIG(inst) \ + {.label = DT_LABEL(DT_PHANDLE(inst, kscan)), \ + .row_offset = DT_PROP(inst, row_offset), \ + .column_offset = DT_PROP(inst, column_offset)}, const struct kscan_composite_child_config kscan_composite_children[] = { DT_FOREACH_CHILD(MATRIX_NODE_ID, CHILD_CONFIG)}; -struct kscan_composite_config -{ -}; +struct kscan_composite_config {}; -struct kscan_composite_data -{ +struct kscan_composite_data { kscan_callback_t callback; struct device *dev; }; -static int kscan_composite_enable_callback(struct device *dev) -{ - for (int i = 0; i < sizeof(kscan_composite_children) / sizeof(kscan_composite_children[0]); i++) - { +static int kscan_composite_enable_callback(struct device *dev) { + for (int i = 0; i < ARRAY_SIZE(kscan_composite_children); i++) { const struct kscan_composite_child_config *cfg = &kscan_composite_children[i]; kscan_enable_callback(device_get_binding(cfg->label)); @@ -53,10 +46,8 @@ static int kscan_composite_enable_callback(struct device *dev) return 0; } -static int kscan_composite_disable_callback(struct device *dev) -{ - for (int i = 0; i < sizeof(kscan_composite_children) / sizeof(kscan_composite_children[0]); i++) - { +static int kscan_composite_disable_callback(struct device *dev) { + for (int i = 0; i < ARRAY_SIZE(kscan_composite_children); i++) { const struct kscan_composite_child_config *cfg = &kscan_composite_children[i]; kscan_disable_callback(device_get_binding(cfg->label)); @@ -64,18 +55,16 @@ static int kscan_composite_disable_callback(struct device *dev) return 0; } -static void kscan_composite_child_callback(struct device *child_dev, u32_t row, u32_t column, bool pressed) -{ +static void kscan_composite_child_callback(struct device *child_dev, u32_t row, u32_t column, + bool pressed) { // TODO: Ideally we can get this passed into our callback! struct device *dev = device_get_binding(DT_INST_LABEL(0)); struct kscan_composite_data *data = dev->driver_data; - for (int i = 0; i < sizeof(kscan_composite_children) / sizeof(kscan_composite_children[0]); i++) - { + for (int i = 0; i < ARRAY_SIZE(kscan_composite_children); i++) { const struct kscan_composite_child_config *cfg = &kscan_composite_children[i]; - if (device_get_binding(cfg->label) != child_dev) - { + if (device_get_binding(cfg->label) != child_dev) { continue; } @@ -83,17 +72,14 @@ static void kscan_composite_child_callback(struct device *child_dev, u32_t row, } } -static int kscan_composite_configure(struct device *dev, kscan_callback_t callback) -{ +static int kscan_composite_configure(struct device *dev, kscan_callback_t callback) { struct kscan_composite_data *data = dev->driver_data; - if (!callback) - { + if (!callback) { return -EINVAL; } - for (int i = 0; i < sizeof(kscan_composite_children) / sizeof(kscan_composite_children[0]); i++) - { + for (int i = 0; i < ARRAY_SIZE(kscan_composite_children); i++) { const struct kscan_composite_child_config *cfg = &kscan_composite_children[i]; kscan_config(device_get_binding(cfg->label), &kscan_composite_child_callback); @@ -104,8 +90,7 @@ static int kscan_composite_configure(struct device *dev, kscan_callback_t callba return 0; } -static int kscan_composite_init(struct device *dev) -{ +static int kscan_composite_init(struct device *dev) { struct kscan_composite_data *data = dev->driver_data; data->dev = dev; @@ -123,8 +108,6 @@ static const struct kscan_composite_config kscan_composite_config = {}; static struct kscan_composite_data kscan_composite_data; -DEVICE_AND_API_INIT(kscan_composite, DT_INST_LABEL(0), kscan_composite_init, - &kscan_composite_data, - &kscan_composite_config, - APPLICATION, CONFIG_KERNEL_INIT_PRIORITY_DEFAULT, +DEVICE_AND_API_INIT(kscan_composite, DT_INST_LABEL(0), kscan_composite_init, &kscan_composite_data, + &kscan_composite_config, APPLICATION, CONFIG_KERNEL_INIT_PRIORITY_DEFAULT, &mock_driver_api); diff --git a/app/src/kscan_mock.c b/app/src/kscan_mock.c index d626c71..e9c15a0 100644 --- a/app/src/kscan_mock.c +++ b/app/src/kscan_mock.c @@ -1,5 +1,5 @@ /* - * Copyright (c) 2020 Peter Johanson <peter@peterjohanson.com> + * Copyright (c) 2020 The ZMK Contributors * * SPDX-License-Identifier: MIT */ @@ -15,8 +15,7 @@ LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL); #include <dt-bindings/zmk/kscan-mock.h> -struct kscan_mock_data -{ +struct kscan_mock_data { kscan_callback_t callback; u8_t event_index; @@ -24,20 +23,17 @@ struct kscan_mock_data struct device *dev; }; -static int kscan_mock_disable_callback(struct device *dev) -{ +static int kscan_mock_disable_callback(struct device *dev) { struct kscan_mock_data *data = dev->driver_data; k_delayed_work_cancel(&data->work); return 0; } -static int kscan_mock_configure(struct device *dev, kscan_callback_t callback) -{ +static int kscan_mock_configure(struct device *dev, kscan_callback_t callback) { struct kscan_mock_data *data = dev->driver_data; - if (!callback) - { + if (!callback) { return -EINVAL; } @@ -47,64 +43,53 @@ static int kscan_mock_configure(struct device *dev, kscan_callback_t callback) return 0; } -#define MOCK_INST_INIT(n) \ - struct kscan_mock_config_##n \ - { \ - u32_t events[DT_INST_PROP_LEN(n, events)]; \ - bool exit_after; \ - }; \ - static void kscan_mock_schedule_next_event_##n(struct device *dev) \ - { \ - struct kscan_mock_data *data = dev->driver_data; \ - const struct kscan_mock_config_##n *cfg = dev->config_info; \ - if (data->event_index < DT_INST_PROP_LEN(n, events)) \ - { \ - u32_t ev = cfg->events[data->event_index]; \ - LOG_DBG("delaying next keypress: %d", ZMK_MOCK_MSEC(ev)); \ - k_delayed_work_submit(&data->work, K_MSEC(ZMK_MOCK_MSEC(ev))); \ - } else if (cfg->exit_after) { \ - LOG_DBG("Exiting"); \ - exit(0); \ - } \ - } \ - static void kscan_mock_work_handler_##n(struct k_work *work) \ - { \ - struct kscan_mock_data *data = \ - CONTAINER_OF(work, struct kscan_mock_data, work); \ - const struct kscan_mock_config_##n *cfg = data->dev->config_info; \ - u32_t ev = cfg->events[data->event_index]; \ - LOG_DBG("ev %u row %d column %d state %d\n", ev, \ - ZMK_MOCK_ROW(ev), ZMK_MOCK_COL(ev), ZMK_MOCK_IS_PRESS(ev)); \ - data->callback(data->dev, \ - ZMK_MOCK_ROW(ev), ZMK_MOCK_COL(ev), ZMK_MOCK_IS_PRESS(ev)); \ - kscan_mock_schedule_next_event_##n(data->dev); \ - data->event_index++; \ - } \ - static int kscan_mock_init_##n(struct device *dev) \ - { \ - struct kscan_mock_data *data = dev->driver_data; \ - data->dev = dev; \ - k_delayed_work_init(&data->work, kscan_mock_work_handler_##n); \ - return 0; \ - } \ - static int kscan_mock_enable_callback_##n(struct device *dev) \ - { \ - kscan_mock_schedule_next_event_##n(dev); \ - return 0; \ - } \ - static const struct kscan_driver_api mock_driver_api_##n = { \ - .config = kscan_mock_configure, \ - .enable_callback = kscan_mock_enable_callback_##n, \ - .disable_callback = kscan_mock_disable_callback, \ - }; \ - static struct kscan_mock_data kscan_mock_data_##n; \ - static const struct kscan_mock_config_##n kscan_mock_config_##n = { \ - .events = DT_INST_PROP(n, events), \ - .exit_after = DT_INST_PROP(n, exit_after) }; \ - DEVICE_AND_API_INIT(kscan_mock_##n, DT_INST_LABEL(n), kscan_mock_init_##n, \ - &kscan_mock_data_##n, \ - &kscan_mock_config_##n, \ - APPLICATION, CONFIG_KERNEL_INIT_PRIORITY_DEFAULT, \ - &mock_driver_api_##n); +#define MOCK_INST_INIT(n) \ + struct kscan_mock_config_##n { \ + u32_t events[DT_INST_PROP_LEN(n, events)]; \ + bool exit_after; \ + }; \ + static void kscan_mock_schedule_next_event_##n(struct device *dev) { \ + struct kscan_mock_data *data = dev->driver_data; \ + const struct kscan_mock_config_##n *cfg = dev->config_info; \ + if (data->event_index < DT_INST_PROP_LEN(n, events)) { \ + u32_t ev = cfg->events[data->event_index]; \ + LOG_DBG("delaying next keypress: %d", ZMK_MOCK_MSEC(ev)); \ + k_delayed_work_submit(&data->work, K_MSEC(ZMK_MOCK_MSEC(ev))); \ + } else if (cfg->exit_after) { \ + LOG_DBG("Exiting"); \ + exit(0); \ + } \ + } \ + static void kscan_mock_work_handler_##n(struct k_work *work) { \ + struct kscan_mock_data *data = CONTAINER_OF(work, struct kscan_mock_data, work); \ + const struct kscan_mock_config_##n *cfg = data->dev->config_info; \ + u32_t ev = cfg->events[data->event_index]; \ + LOG_DBG("ev %u row %d column %d state %d\n", ev, ZMK_MOCK_ROW(ev), ZMK_MOCK_COL(ev), \ + ZMK_MOCK_IS_PRESS(ev)); \ + data->callback(data->dev, ZMK_MOCK_ROW(ev), ZMK_MOCK_COL(ev), ZMK_MOCK_IS_PRESS(ev)); \ + kscan_mock_schedule_next_event_##n(data->dev); \ + data->event_index++; \ + } \ + static int kscan_mock_init_##n(struct device *dev) { \ + struct kscan_mock_data *data = dev->driver_data; \ + data->dev = dev; \ + k_delayed_work_init(&data->work, kscan_mock_work_handler_##n); \ + return 0; \ + } \ + static int kscan_mock_enable_callback_##n(struct device *dev) { \ + kscan_mock_schedule_next_event_##n(dev); \ + return 0; \ + } \ + static const struct kscan_driver_api mock_driver_api_##n = { \ + .config = kscan_mock_configure, \ + .enable_callback = kscan_mock_enable_callback_##n, \ + .disable_callback = kscan_mock_disable_callback, \ + }; \ + static struct kscan_mock_data kscan_mock_data_##n; \ + static const struct kscan_mock_config_##n kscan_mock_config_##n = { \ + .events = DT_INST_PROP(n, events), .exit_after = DT_INST_PROP(n, exit_after)}; \ + DEVICE_AND_API_INIT(kscan_mock_##n, DT_INST_LABEL(n), kscan_mock_init_##n, \ + &kscan_mock_data_##n, &kscan_mock_config_##n, APPLICATION, \ + CONFIG_KERNEL_INIT_PRIORITY_DEFAULT, &mock_driver_api_##n); DT_INST_FOREACH_STATUS_OKAY(MOCK_INST_INIT) diff --git a/app/src/main.c b/app/src/main.c index 5a678ee..0551356 100644 --- a/app/src/main.c +++ b/app/src/main.c @@ -1,5 +1,5 @@ /* - * Copyright (c) 2020 Peter Johanson + * Copyright (c) 2020 The ZMK Contributors * * SPDX-License-Identifier: MIT */ @@ -15,23 +15,30 @@ LOG_MODULE_REGISTER(zmk, CONFIG_ZMK_LOG_LEVEL); #include <zmk/matrix.h> #include <zmk/kscan.h> #include <zmk/display.h> +#include <drivers/ext_power.h> #define ZMK_KSCAN_DEV DT_LABEL(ZMK_MATRIX_NODE_ID) -void main(void) -{ - LOG_INF("Welcome to ZMK!\n"); +void main(void) { + struct device *ext_power; + LOG_INF("Welcome to ZMK!\n"); - if (zmk_kscan_init(ZMK_KSCAN_DEV) != 0) - { - return; - } + if (zmk_kscan_init(ZMK_KSCAN_DEV) != 0) { + return; + } + + // Enable the external VCC output + ext_power = device_get_binding("EXT_POWER"); + if (ext_power != NULL) { + const struct ext_power_api *ext_power_api = ext_power->driver_api; + ext_power_api->enable(ext_power); + } #ifdef CONFIG_ZMK_DISPLAY - zmk_display_init(); + zmk_display_init(); - while (1) { - zmk_display_task_handler(); - } + while (1) { + zmk_display_task_handler(); + } #endif /* CONFIG_ZMK_DISPLAY */ } diff --git a/app/src/matrix_transform.c b/app/src/matrix_transform.c index 7ecf1ae..4e68a56 100644 --- a/app/src/matrix_transform.c +++ b/app/src/matrix_transform.c @@ -1,3 +1,8 @@ +/* + * Copyright (c) 2020 The ZMK Contributors + * + * SPDX-License-Identifier: MIT + */ #include <zephyr.h> #include <zmk/matrix_transform.h> @@ -6,16 +11,15 @@ #ifdef ZMK_KEYMAP_TRANSFORM_NODE -#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, +#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) }; +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 zmk_matrix_transform_row_column_to_position(u32_t row, u32_t column) { u32_t matrix_index; #if DT_NODE_HAS_PROP(ZMK_KEYMAP_TRANSFORM_NODE, col_offset) diff --git a/app/src/power.c b/app/src/power.c new file mode 100644 index 0000000..73b3f12 --- /dev/null +++ b/app/src/power.c @@ -0,0 +1,69 @@ +/* + * Copyright (c) 2020 The ZMK Contributors + * + * SPDX-License-Identifier: MIT + */ + +#include <zephyr.h> +#include <kernel.h> +#include <power/power.h> + +#include <logging/log.h> + +LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL); + +#include <zmk/usb.h> +#include <zmk/event-manager.h> +#include <zmk/events/position-state-changed.h> +#include <zmk/events/sensor-event.h> + +static u32_t power_last_uptime; + +#define MAX_IDLE_MS CONFIG_ZMK_IDLE_SLEEP_TIMEOUT + +bool is_usb_power_present() { +#ifdef CONFIG_USB + enum usb_dc_status_code usb_status = zmk_usb_get_status(); + switch (usb_status) { + case USB_DC_DISCONNECTED: + case USB_DC_UNKNOWN: + return false; + default: + return true; + } +#else + return false; +#endif /* CONFIG_USB */ +} + +enum power_states sys_pm_policy_next_state(s32_t ticks) { +#ifdef CONFIG_SYS_POWER_DEEP_SLEEP_STATES +#ifdef CONFIG_HAS_SYS_POWER_STATE_DEEP_SLEEP_1 + s32_t current = k_uptime_get(); + if (power_last_uptime > 0 && !is_usb_power_present() && + current - power_last_uptime > MAX_IDLE_MS) { + return SYS_POWER_STATE_DEEP_SLEEP_1; + } +#endif /* CONFIG_HAS_SYS_POWER_STATE_DEEP_SLEEP_1 */ +#endif /* CONFIG_SYS_POWER_DEEP_SLEEP_STATES */ + + return SYS_POWER_STATE_ACTIVE; +} + +int power_event_listener(const struct zmk_event_header *eh) { + power_last_uptime = k_uptime_get(); + + return 0; +} + +int power_init() { + power_last_uptime = k_uptime_get(); + + return 0; +} + +ZMK_LISTENER(power, power_event_listener); +ZMK_SUBSCRIPTION(power, position_state_changed); +ZMK_SUBSCRIPTION(power, sensor_event); + +SYS_INIT(power_init, APPLICATION, CONFIG_APPLICATION_INIT_PRIORITY);
\ No newline at end of file diff --git a/app/src/rgb_underglow.c b/app/src/rgb_underglow.c index 95adcec..13912e3 100644 --- a/app/src/rgb_underglow.c +++ b/app/src/rgb_underglow.c @@ -1,5 +1,5 @@ /* - * Copyright (c) 2020 Nick Winans <nick@winans.codes> + * Copyright (c) 2020 The ZMK Contributors * * SPDX-License-Identifier: MIT */ @@ -18,8 +18,8 @@ LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL); -#define STRIP_LABEL DT_LABEL(DT_CHOSEN(zmk_underglow)) -#define STRIP_NUM_PIXELS DT_PROP(DT_CHOSEN(zmk_underglow), chain_length) +#define STRIP_LABEL DT_LABEL(DT_CHOSEN(zmk_underglow)) +#define STRIP_NUM_PIXELS DT_PROP(DT_CHOSEN(zmk_underglow), chain_length) enum rgb_underglow_effect { UNDERGLOW_EFFECT_SOLID, @@ -30,19 +30,19 @@ enum rgb_underglow_effect { }; struct led_hsb { - u16_t h; - u8_t s; - u8_t b; + u16_t h; + u8_t s; + u8_t b; }; struct rgb_underglow_state { u16_t hue; - u8_t saturation; - u8_t brightness; - u8_t animation_speed; - u8_t current_effect; + u8_t saturation; + u8_t brightness; + u8_t animation_speed; + u8_t current_effect; u16_t animation_step; - bool on; + bool on; }; struct rgb_underglow_state state; @@ -51,8 +51,7 @@ struct device *led_strip; struct led_rgb pixels[STRIP_NUM_PIXELS]; -static struct led_rgb hsb_to_rgb(struct led_hsb hsb) -{ +static struct led_rgb hsb_to_rgb(struct led_hsb hsb) { double r, g, b; u8_t i = hsb.h / 60; @@ -63,64 +62,81 @@ static struct led_rgb hsb_to_rgb(struct led_hsb hsb) double q = v * (1 - f * s); double t = v * (1 - (1 - f) * s); - switch (i % 6) - { - case 0: r = v; g = t; b = p; break; - case 1: r = q; g = v; b = p; break; - case 2: r = p; g = v; b = t; break; - case 3: r = p; g = q; b = v; break; - case 4: r = t; g = p; b = v; break; - case 5: r = v; g = p; b = q; break; + switch (i % 6) { + case 0: + r = v; + g = t; + b = p; + break; + case 1: + r = q; + g = v; + b = p; + break; + case 2: + r = p; + g = v; + b = t; + break; + case 3: + r = p; + g = q; + b = v; + break; + case 4: + r = t; + g = p; + b = v; + break; + case 5: + r = v; + g = p; + b = q; + break; } - struct led_rgb rgb = { r: r*255, g: g*255, b: b*255 }; + struct led_rgb rgb = {r : r * 255, g : g * 255, b : b * 255}; return rgb; } -static void zmk_rgb_underglow_effect_solid() -{ - for (int i=0; i<STRIP_NUM_PIXELS; i++) - { +static void zmk_rgb_underglow_effect_solid() { + for (int i = 0; i < STRIP_NUM_PIXELS; i++) { int hue = state.hue; int sat = state.saturation; int brt = state.brightness; - struct led_hsb hsb = { hue, sat, brt }; + struct led_hsb hsb = {hue, sat, brt}; pixels[i] = hsb_to_rgb(hsb); } } -static void zmk_rgb_underglow_effect_breathe() -{ - for (int i=0; i<STRIP_NUM_PIXELS; i++) - { +static void zmk_rgb_underglow_effect_breathe() { + for (int i = 0; i < STRIP_NUM_PIXELS; i++) { int hue = state.hue; int sat = state.saturation; int brt = abs(state.animation_step - 1200) / 12; - struct led_hsb hsb = { hue, sat, brt }; + struct led_hsb hsb = {hue, sat, brt}; pixels[i] = hsb_to_rgb(hsb); } state.animation_step += state.animation_speed * 10; - + if (state.animation_step > 2400) { state.animation_step = 0; } } -static void zmk_rgb_underglow_effect_spectrum() -{ - for (int i=0; i<STRIP_NUM_PIXELS; i++) - { +static void zmk_rgb_underglow_effect_spectrum() { + for (int i = 0; i < STRIP_NUM_PIXELS; i++) { int hue = state.animation_step; int sat = state.saturation; int brt = state.brightness; - struct led_hsb hsb = { hue, sat, brt }; + struct led_hsb hsb = {hue, sat, brt}; pixels[i] = hsb_to_rgb(hsb); } @@ -129,15 +145,13 @@ static void zmk_rgb_underglow_effect_spectrum() state.animation_step = state.animation_step % 360; } -static void zmk_rgb_underglow_effect_swirl() -{ - for (int i=0; i<STRIP_NUM_PIXELS; i++) - { +static void zmk_rgb_underglow_effect_swirl() { + for (int i = 0; i < STRIP_NUM_PIXELS; i++) { int hue = (360 / STRIP_NUM_PIXELS * i + state.animation_step) % 360; int sat = state.saturation; int brt = state.brightness; - struct led_hsb hsb = { hue, sat, brt }; + struct led_hsb hsb = {hue, sat, brt}; pixels[i] = hsb_to_rgb(hsb); } @@ -146,22 +160,20 @@ static void zmk_rgb_underglow_effect_swirl() state.animation_step = state.animation_step % 360; } -static void zmk_rgb_underglow_tick(struct k_work *work) -{ - switch (state.current_effect) - { - case UNDERGLOW_EFFECT_SOLID: - zmk_rgb_underglow_effect_solid(); - break; - case UNDERGLOW_EFFECT_BREATHE: - zmk_rgb_underglow_effect_breathe(); - break; - case UNDERGLOW_EFFECT_SPECTRUM: - zmk_rgb_underglow_effect_spectrum(); - break; - case UNDERGLOW_EFFECT_SWIRL: - zmk_rgb_underglow_effect_swirl(); - break; +static void zmk_rgb_underglow_tick(struct k_work *work) { + switch (state.current_effect) { + case UNDERGLOW_EFFECT_SOLID: + zmk_rgb_underglow_effect_solid(); + break; + case UNDERGLOW_EFFECT_BREATHE: + zmk_rgb_underglow_effect_breathe(); + break; + case UNDERGLOW_EFFECT_SPECTRUM: + zmk_rgb_underglow_effect_spectrum(); + break; + case UNDERGLOW_EFFECT_SWIRL: + zmk_rgb_underglow_effect_swirl(); + break; } led_strip_update_rgb(led_strip, pixels, STRIP_NUM_PIXELS); @@ -169,31 +181,29 @@ static void zmk_rgb_underglow_tick(struct k_work *work) K_WORK_DEFINE(underglow_work, zmk_rgb_underglow_tick); -static void zmk_rgb_underglow_tick_handler(struct k_timer *timer) -{ +static void zmk_rgb_underglow_tick_handler(struct k_timer *timer) { k_work_submit(&underglow_work); } K_TIMER_DEFINE(underglow_tick, zmk_rgb_underglow_tick_handler, NULL); -static int zmk_rgb_underglow_init(struct device *_arg) -{ - led_strip = device_get_binding(STRIP_LABEL); - if (led_strip) { - LOG_INF("Found LED strip device %s", STRIP_LABEL); - } else { - LOG_ERR("LED strip device %s not found", STRIP_LABEL); - return -EINVAL; - } +static int zmk_rgb_underglow_init(struct device *_arg) { + led_strip = device_get_binding(STRIP_LABEL); + if (led_strip) { + LOG_INF("Found LED strip device %s", STRIP_LABEL); + } else { + LOG_ERR("LED strip device %s not found", STRIP_LABEL); + return -EINVAL; + } state = (struct rgb_underglow_state){ - hue: 0, - saturation: 100, - brightness: 100, - animation_speed: 3, - current_effect: 0, - animation_step: 0, - on: true + hue : 0, + saturation : 100, + brightness : 100, + animation_speed : 3, + current_effect : 0, + animation_step : 0, + on : true }; k_timer_start(&underglow_tick, K_NO_WAIT, K_MSEC(50)); @@ -201,9 +211,9 @@ static int zmk_rgb_underglow_init(struct device *_arg) return 0; } -int zmk_rgb_underglow_cycle_effect(int direction) -{ - if (!led_strip) return -ENODEV; +int zmk_rgb_underglow_cycle_effect(int direction) { + if (!led_strip) + return -ENODEV; if (state.current_effect == 0 && direction < 0) { state.current_effect = UNDERGLOW_EFFECT_NUMBER - 1; @@ -215,15 +225,15 @@ int zmk_rgb_underglow_cycle_effect(int direction) if (state.current_effect >= UNDERGLOW_EFFECT_NUMBER) { state.current_effect = 0; } - + state.animation_step = 0; return 0; } -int zmk_rgb_underglow_toggle() -{ - if (!led_strip) return -ENODEV; +int zmk_rgb_underglow_toggle() { + if (!led_strip) + return -ENODEV; state.on = !state.on; @@ -232,9 +242,8 @@ int zmk_rgb_underglow_toggle() k_timer_start(&underglow_tick, K_NO_WAIT, K_MSEC(50)); } else { - for (int i=0; i<STRIP_NUM_PIXELS; i++) - { - pixels[i] = (struct led_rgb){ r: 0, g: 0, b: 0}; + for (int i = 0; i < STRIP_NUM_PIXELS; i++) { + pixels[i] = (struct led_rgb){r : 0, g : 0, b : 0}; } led_strip_update_rgb(led_strip, pixels, STRIP_NUM_PIXELS); @@ -245,15 +254,15 @@ int zmk_rgb_underglow_toggle() return 0; } -int zmk_rgb_underglow_change_hue(int direction) -{ - if (!led_strip) return -ENODEV; +int zmk_rgb_underglow_change_hue(int direction) { + if (!led_strip) + return -ENODEV; if (state.hue == 0 && direction < 0) { state.hue = 350; return 0; } - + state.hue += direction * CONFIG_ZMK_RGB_UNDERGLOW_HUE_STEP; if (state.hue > 350) { @@ -263,9 +272,9 @@ int zmk_rgb_underglow_change_hue(int direction) return 0; } -int zmk_rgb_underglow_change_sat(int direction) -{ - if (!led_strip) return -ENODEV; +int zmk_rgb_underglow_change_sat(int direction) { + if (!led_strip) + return -ENODEV; if (state.saturation == 0 && direction < 0) { return 0; @@ -280,9 +289,9 @@ int zmk_rgb_underglow_change_sat(int direction) return 0; } -int zmk_rgb_underglow_change_brt(int direction) -{ - if (!led_strip) return -ENODEV; +int zmk_rgb_underglow_change_brt(int direction) { + if (!led_strip) + return -ENODEV; if (state.brightness == 0 && direction < 0) { return 0; @@ -297,9 +306,9 @@ int zmk_rgb_underglow_change_brt(int direction) return 0; } -int zmk_rgb_underglow_change_spd(int direction) -{ - if (!led_strip) return -ENODEV; +int zmk_rgb_underglow_change_spd(int direction) { + if (!led_strip) + return -ENODEV; if (state.animation_speed == 1 && direction < 0) { return 0; @@ -314,6 +323,4 @@ int zmk_rgb_underglow_change_spd(int direction) return 0; } -SYS_INIT(zmk_rgb_underglow_init, - APPLICATION, - CONFIG_APPLICATION_INIT_PRIORITY); +SYS_INIT(zmk_rgb_underglow_init, APPLICATION, CONFIG_APPLICATION_INIT_PRIORITY); diff --git a/app/src/sensors.c b/app/src/sensors.c index 501ce15..15f374b 100644 --- a/app/src/sensors.c +++ b/app/src/sensors.c @@ -1,5 +1,5 @@ /* - * Copyright (c) 2020 Peter Johanson + * Copyright (c) 2020 The ZMK Contributors * * SPDX-License-Identifier: MIT */ @@ -24,19 +24,19 @@ struct sensors_data_item { struct sensor_trigger trigger; }; -#define _SENSOR_ITEM(node) {.dev = NULL, .trigger = { .type = SENSOR_TRIG_DELTA, .chan = SENSOR_CHAN_ROTATION } }, -#define SENSOR_ITEM(idx, _) COND_CODE_1(DT_NODE_HAS_STATUS(ZMK_KEYMAP_SENSORS_BY_IDX(idx),okay), (_SENSOR_ITEM(ZMK_KEYMAP_SENSORS_BY_IDX(idx))),()) +#define _SENSOR_ITEM(node) \ + {.dev = NULL, .trigger = {.type = SENSOR_TRIG_DELTA, .chan = SENSOR_CHAN_ROTATION}}, +#define SENSOR_ITEM(idx, _) \ + COND_CODE_1(DT_NODE_HAS_STATUS(ZMK_KEYMAP_SENSORS_BY_IDX(idx), okay), \ + (_SENSOR_ITEM(ZMK_KEYMAP_SENSORS_BY_IDX(idx))), ()) -static struct sensors_data_item sensors[] = { - UTIL_LISTIFY(ZMK_KEYMAP_SENSORS_LEN, SENSOR_ITEM, 0) -}; +static struct sensors_data_item sensors[] = {UTIL_LISTIFY(ZMK_KEYMAP_SENSORS_LEN, SENSOR_ITEM, 0)}; -static void zmk_sensors_trigger_handler(struct device *dev, struct sensor_trigger *trigger) -{ +static void zmk_sensors_trigger_handler(struct device *dev, struct sensor_trigger *trigger) { int err; - struct sensors_data_item * item = CONTAINER_OF(trigger, struct sensors_data_item, trigger); + struct sensors_data_item *item = CONTAINER_OF(trigger, struct sensors_data_item, trigger); struct sensor_event *event; - + LOG_DBG("sensor %d", item->sensor_number); err = sensor_sample_fetch(dev); @@ -52,8 +52,7 @@ static void zmk_sensors_trigger_handler(struct device *dev, struct sensor_trigge ZMK_EVENT_RAISE(event); } -static void zmk_sensors_init_item(const char *node, u8_t i, u8_t abs_i) -{ +static void zmk_sensors_init_item(const char *node, u8_t i, u8_t abs_i) { LOG_DBG("Init %s at index %d with sensor_number %d", node, i, abs_i); sensors[i].dev = device_get_binding(node); @@ -68,10 +67,11 @@ static void zmk_sensors_init_item(const char *node, u8_t i, u8_t abs_i) } #define _SENSOR_INIT(node) zmk_sensors_init_item(DT_LABEL(node), local_index++, absolute_index++); -#define SENSOR_INIT(idx, _i) COND_CODE_1(DT_NODE_HAS_STATUS(ZMK_KEYMAP_SENSORS_BY_IDX(idx),okay), (_SENSOR_INIT(ZMK_KEYMAP_SENSORS_BY_IDX(idx))),(absolute_index++;)) +#define SENSOR_INIT(idx, _i) \ + COND_CODE_1(DT_NODE_HAS_STATUS(ZMK_KEYMAP_SENSORS_BY_IDX(idx), okay), \ + (_SENSOR_INIT(ZMK_KEYMAP_SENSORS_BY_IDX(idx))), (absolute_index++;)) -static int zmk_sensors_init(struct device *_arg) -{ +static int zmk_sensors_init(struct device *_arg) { int local_index = 0; int absolute_index = 0; @@ -79,8 +79,6 @@ static int zmk_sensors_init(struct device *_arg) return 0; } -SYS_INIT(zmk_sensors_init, - APPLICATION, - CONFIG_APPLICATION_INIT_PRIORITY); +SYS_INIT(zmk_sensors_init, APPLICATION, CONFIG_APPLICATION_INIT_PRIORITY); #endif /* ZMK_KEYMAP_HAS_SENSORS */
\ No newline at end of file diff --git a/app/src/split/bluetooth/central.c b/app/src/split/bluetooth/central.c index 237096f..ed52ba0 100644 --- a/app/src/split/bluetooth/central.c +++ b/app/src/split/bluetooth/central.c @@ -1,5 +1,5 @@ /* - * Copyright (c) 2020 Peter Johanson + * Copyright (c) 2020 The ZMK Contributors * * SPDX-License-Identifier: MIT */ @@ -10,12 +10,14 @@ #include <bluetooth/conn.h> #include <bluetooth/uuid.h> #include <bluetooth/gatt.h> +#include <bluetooth/hci.h> #include <sys/byteorder.h> #include <logging/log.h> LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL); +#include <zmk/ble.h> #include <zmk/split/bluetooth/uuid.h> #include <zmk/event-manager.h> #include <zmk/events/position-state-changed.h> @@ -31,283 +33,291 @@ static struct bt_uuid_128 uuid = BT_UUID_INIT_128(ZMK_SPLIT_BT_SERVICE_UUID); static struct bt_gatt_discover_params discover_params; static struct bt_gatt_subscribe_params subscribe_params; -static u8_t split_central_notify_func(struct bt_conn *conn, - struct bt_gatt_subscribe_params *params, - const void *data, u16_t length) -{ - static u8_t position_state[POSITION_STATE_DATA_LEN]; +static u8_t split_central_notify_func(struct bt_conn *conn, struct bt_gatt_subscribe_params *params, + const void *data, u16_t length) { + static u8_t position_state[POSITION_STATE_DATA_LEN]; + + u8_t changed_positions[POSITION_STATE_DATA_LEN]; + + if (!data) { + LOG_DBG("[UNSUBSCRIBED]"); + params->value_handle = 0U; + return BT_GATT_ITER_STOP; + } + + LOG_DBG("[NOTIFICATION] data %p length %u", data, length); + + for (int i = 0; i < POSITION_STATE_DATA_LEN; i++) { + changed_positions[i] = ((u8_t *)data)[i] ^ position_state[i]; + position_state[i] = ((u8_t *)data)[i]; + } + + for (int i = 0; i < POSITION_STATE_DATA_LEN; i++) { + for (int j = 0; j < 8; j++) { + if (changed_positions[i] & BIT(j)) { + u32_t position = (i * 8) + j; + bool pressed = position_state[i] & BIT(j); + struct position_state_changed *pos_ev = new_position_state_changed(); + pos_ev->position = position; + pos_ev->state = pressed; + pos_ev->timestamp = k_uptime_get(); + + LOG_DBG("Trigger key position state change for %d", position); + ZMK_EVENT_RAISE(pos_ev); + } + } + } - u8_t changed_positions[POSITION_STATE_DATA_LEN]; + return BT_GATT_ITER_CONTINUE; +} - if (!data) { - LOG_DBG("[UNSUBSCRIBED]"); - params->value_handle = 0U; - return BT_GATT_ITER_STOP; - } +static int split_central_subscribe(struct bt_conn *conn) { + int err = bt_gatt_subscribe(conn, &subscribe_params); + switch (err) { + case -EALREADY: + LOG_DBG("[ALREADY SUBSCRIBED]"); + break; + // break; + // bt_gatt_unsubscribe(conn, &subscribe_params); + // return split_central_subscribe(conn); + case 0: + LOG_DBG("[SUBSCRIBED]"); + break; + default: + LOG_ERR("Subscribe failed (err %d)", err); + break; + } - LOG_DBG("[NOTIFICATION] data %p length %u", data, length); + return 0; +} - for (int i = 0; i < POSITION_STATE_DATA_LEN; i++) { - changed_positions[i] = ((u8_t *)data)[i] ^ position_state[i]; - position_state[i] = ((u8_t *)data)[i]; - } +static u8_t split_central_discovery_func(struct bt_conn *conn, const struct bt_gatt_attr *attr, + struct bt_gatt_discover_params *params) { + int err; + + if (!attr) { + LOG_DBG("Discover complete"); + (void)memset(params, 0, sizeof(*params)); + return BT_GATT_ITER_STOP; + } + + LOG_DBG("[ATTRIBUTE] handle %u", attr->handle); + + if (!bt_uuid_cmp(discover_params.uuid, BT_UUID_DECLARE_128(ZMK_SPLIT_BT_SERVICE_UUID))) { + memcpy(&uuid, BT_UUID_DECLARE_128(ZMK_SPLIT_BT_CHAR_POSITION_STATE_UUID), sizeof(uuid)); + discover_params.uuid = &uuid.uuid; + discover_params.start_handle = attr->handle + 1; + discover_params.type = BT_GATT_DISCOVER_CHARACTERISTIC; + + err = bt_gatt_discover(conn, &discover_params); + if (err) { + LOG_ERR("Discover failed (err %d)", err); + } + } else if (!bt_uuid_cmp(discover_params.uuid, + BT_UUID_DECLARE_128(ZMK_SPLIT_BT_CHAR_POSITION_STATE_UUID))) { + memcpy(&uuid, BT_UUID_GATT_CCC, sizeof(uuid)); + discover_params.uuid = &uuid.uuid; + discover_params.start_handle = attr->handle + 2; + discover_params.type = BT_GATT_DISCOVER_DESCRIPTOR; + subscribe_params.value_handle = bt_gatt_attr_value_handle(attr); + + err = bt_gatt_discover(conn, &discover_params); + if (err) { + LOG_ERR("Discover failed (err %d)", err); + } + } else { + subscribe_params.notify = split_central_notify_func; + subscribe_params.value = BT_GATT_CCC_NOTIFY; + subscribe_params.ccc_handle = attr->handle; + + split_central_subscribe(conn); + + return BT_GATT_ITER_STOP; + } + + return BT_GATT_ITER_STOP; +} - for (int i = 0; i < POSITION_STATE_DATA_LEN; i++) { - for (int j = 0; j < 8; j++) { - if (changed_positions[i] & BIT(j)) { - u32_t position = (i * 8) + j; - bool pressed = position_state[i] & BIT(j); - struct position_state_changed *pos_ev = new_position_state_changed(); - pos_ev->position = position; - pos_ev->state = pressed; +static void split_central_process_connection(struct bt_conn *conn) { + int err; - LOG_DBG("Trigger key position state change for %d", position); - ZMK_EVENT_RAISE(pos_ev); - } - } - } + LOG_DBG("Current security for connection: %d", bt_conn_get_security(conn)); + err = bt_conn_set_security(conn, BT_SECURITY_L2); + if (err) { + LOG_ERR("Failed to set security (reason %d)", err); + return; + } - return BT_GATT_ITER_CONTINUE; -} + if (conn == default_conn && !subscribe_params.value) { + discover_params.uuid = &uuid.uuid; + discover_params.func = split_central_discovery_func; + discover_params.start_handle = 0x0001; + discover_params.end_handle = 0xffff; + discover_params.type = BT_GATT_DISCOVER_PRIMARY; -static u8_t split_central_discovery_func(struct bt_conn *conn, - const struct bt_gatt_attr *attr, - struct bt_gatt_discover_params *params) -{ - int err; - - if (!attr) { - LOG_DBG("Discover complete"); - (void)memset(params, 0, sizeof(*params)); - return BT_GATT_ITER_STOP; - } - - LOG_DBG("[ATTRIBUTE] handle %u", attr->handle); - - if (!bt_uuid_cmp(discover_params.uuid, BT_UUID_DECLARE_128(ZMK_SPLIT_BT_SERVICE_UUID))) { - memcpy(&uuid, BT_UUID_DECLARE_128(ZMK_SPLIT_BT_CHAR_POSITION_STATE_UUID), sizeof(uuid)); - discover_params.uuid = &uuid.uuid; - discover_params.start_handle = attr->handle + 1; - discover_params.type = BT_GATT_DISCOVER_CHARACTERISTIC; - - err = bt_gatt_discover(conn, &discover_params); - if (err) { - LOG_ERR("Discover failed (err %d)", err); - } - } else if (!bt_uuid_cmp(discover_params.uuid, - BT_UUID_DECLARE_128(ZMK_SPLIT_BT_CHAR_POSITION_STATE_UUID))) { - memcpy(&uuid, BT_UUID_GATT_CCC, sizeof(uuid)); - discover_params.uuid = &uuid.uuid; - discover_params.start_handle = attr->handle + 2; - discover_params.type = BT_GATT_DISCOVER_DESCRIPTOR; - subscribe_params.value_handle = bt_gatt_attr_value_handle(attr); - - err = bt_gatt_discover(conn, &discover_params); - if (err) { - LOG_ERR("Discover failed (err %d)", err); - } - } else { - subscribe_params.notify = split_central_notify_func; - subscribe_params.value = BT_GATT_CCC_NOTIFY; - subscribe_params.ccc_handle = attr->handle; - - err = bt_gatt_subscribe(conn, &subscribe_params); - if (err && err != -EALREADY) { - LOG_ERR("Subscribe failed (err %d)", err); - } else { - LOG_DBG("[SUBSCRIBED]"); - } - - return BT_GATT_ITER_STOP; - } - - return BT_GATT_ITER_STOP; -} + err = bt_gatt_discover(default_conn, &discover_params); + if (err) { + LOG_ERR("Discover failed(err %d)", err); + return; + } + } -static void split_central_process_connection(struct bt_conn *conn) { - int err; - - LOG_DBG("Current security for connection: %d", bt_conn_get_security(conn)); - - err = bt_conn_set_security(conn, BT_SECURITY_L2); - if (err) { - LOG_ERR("Failed to set security (reason %d)", err); - return; - } - - if (conn == default_conn) { - discover_params.uuid = &uuid.uuid; - discover_params.func = split_central_discovery_func; - discover_params.start_handle = 0x0001; - discover_params.end_handle = 0xffff; - discover_params.type = BT_GATT_DISCOVER_PRIMARY; - - err = bt_gatt_discover(default_conn, &discover_params); - if (err) { - LOG_ERR("Discover failed(err %d)", err); - return; - } - } - - struct bt_conn_info info; - - bt_conn_get_info(conn, &info); - - LOG_DBG("New connection params: Interval: %d, Latency: %d, PHY: %d", info.le.interval, info.le.latency, info.le.phy->rx_phy); + struct bt_conn_info info; + + bt_conn_get_info(conn, &info); + + LOG_DBG("New connection params: Interval: %d, Latency: %d, PHY: %d", info.le.interval, + info.le.latency, info.le.phy->rx_phy); } -static bool split_central_eir_found(struct bt_data *data, void *user_data) -{ - bt_addr_le_t *addr = user_data; - int i; +static bool split_central_eir_found(struct bt_data *data, void *user_data) { + bt_addr_le_t *addr = user_data; + int i; - LOG_DBG("[AD]: %u data_len %u", data->type, data->data_len); + LOG_DBG("[AD]: %u data_len %u", data->type, data->data_len); - switch (data->type) { - case BT_DATA_UUID128_SOME: - case BT_DATA_UUID128_ALL: - if (data->data_len % 16 != 0U) { - LOG_ERR("AD malformed"); - return true; - } + switch (data->type) { + case BT_DATA_UUID128_SOME: + case BT_DATA_UUID128_ALL: + if (data->data_len % 16 != 0U) { + LOG_ERR("AD malformed"); + return true; + } - for (i = 0; i < data->data_len; i += 16) { - struct bt_le_conn_param *param; - struct bt_uuid uuid; - int err; + for (i = 0; i < data->data_len; i += 16) { + struct bt_le_conn_param *param; + struct bt_uuid uuid; + int err; if (!bt_uuid_create(&uuid, &data->data[i], 16)) { LOG_ERR("Unable to load UUID"); continue; } - if (!bt_uuid_cmp(&uuid, BT_UUID_DECLARE_128(ZMK_SPLIT_BT_SERVICE_UUID))) { - char uuid_str[BT_UUID_STR_LEN]; - char service_uuid_str[BT_UUID_STR_LEN]; - - bt_uuid_to_str(&uuid, uuid_str, sizeof(uuid_str)); - bt_uuid_to_str(BT_UUID_DECLARE_128(ZMK_SPLIT_BT_SERVICE_UUID), service_uuid_str, sizeof(service_uuid_str)); - LOG_DBG("UUID %s does not match split UUID: %s", log_strdup(uuid_str), log_strdup(service_uuid_str)); - continue; - } - - LOG_DBG("Found the split service"); - - err = bt_le_scan_stop(); - if (err) { - LOG_ERR("Stop LE scan failed (err %d)", err); - continue; - } - - default_conn = bt_conn_lookup_addr_le(BT_ID_DEFAULT, addr); - if (default_conn) { - LOG_DBG("Found existing connection"); - split_central_process_connection(default_conn); - } else { - param = BT_LE_CONN_PARAM(0x0006, 0x0006, 30, 400); - err = bt_conn_le_create(addr, BT_CONN_LE_CREATE_CONN, - param, &default_conn); - if (err) { - LOG_ERR("Create conn failed (err %d)", err); - start_scan(); - } - - err = bt_conn_le_phy_update(default_conn, BT_CONN_LE_PHY_PARAM_2M); - if (err) { - LOG_ERR("Update phy conn failed (err %d)", err); - start_scan(); - } - } - - return false; - } - } - - return true; + if (!bt_uuid_cmp(&uuid, BT_UUID_DECLARE_128(ZMK_SPLIT_BT_SERVICE_UUID))) { + char uuid_str[BT_UUID_STR_LEN]; + char service_uuid_str[BT_UUID_STR_LEN]; + + bt_uuid_to_str(&uuid, uuid_str, sizeof(uuid_str)); + bt_uuid_to_str(BT_UUID_DECLARE_128(ZMK_SPLIT_BT_SERVICE_UUID), service_uuid_str, + sizeof(service_uuid_str)); + LOG_DBG("UUID %s does not match split UUID: %s", log_strdup(uuid_str), + log_strdup(service_uuid_str)); + continue; + } + + LOG_DBG("Found the split service"); + + zmk_ble_set_peripheral_addr(addr); + + err = bt_le_scan_stop(); + if (err) { + LOG_ERR("Stop LE scan failed (err %d)", err); + continue; + } + + default_conn = bt_conn_lookup_addr_le(BT_ID_DEFAULT, addr); + if (default_conn) { + LOG_DBG("Found existing connection"); + split_central_process_connection(default_conn); + } else { + param = BT_LE_CONN_PARAM(0x0006, 0x0006, 30, 400); + + err = bt_conn_le_create(addr, BT_CONN_LE_CREATE_CONN, param, &default_conn); + if (err) { + LOG_ERR("Create conn failed (err %d) (create conn? 0x%04x)", err, + BT_HCI_OP_LE_CREATE_CONN); + start_scan(); + } + + err = bt_conn_le_phy_update(default_conn, BT_CONN_LE_PHY_PARAM_2M); + if (err) { + LOG_ERR("Update phy conn failed (err %d)", err); + start_scan(); + } + } + + return false; + } + } + + return true; } static void split_central_device_found(const bt_addr_le_t *addr, s8_t rssi, u8_t type, - struct net_buf_simple *ad) -{ - char dev[BT_ADDR_LE_STR_LEN]; - - bt_addr_le_to_str(addr, dev, sizeof(dev)); - LOG_DBG("[DEVICE]: %s, AD evt type %u, AD data len %u, RSSI %i", - log_strdup(dev), type, ad->len, rssi); - - /* We're only interested in connectable events */ - if (type == BT_GAP_ADV_TYPE_ADV_IND || - type == BT_GAP_ADV_TYPE_ADV_DIRECT_IND) { - bt_data_parse(ad, split_central_eir_found, (void *)addr); - } + struct net_buf_simple *ad) { + char dev[BT_ADDR_LE_STR_LEN]; + + bt_addr_le_to_str(addr, dev, sizeof(dev)); + LOG_DBG("[DEVICE]: %s, AD evt type %u, AD data len %u, RSSI %i", log_strdup(dev), type, ad->len, + rssi); + + /* We're only interested in connectable events */ + if (type == BT_GAP_ADV_TYPE_ADV_IND || type == BT_GAP_ADV_TYPE_ADV_DIRECT_IND) { + bt_data_parse(ad, split_central_eir_found, (void *)addr); + } } -static int start_scan(void) -{ - int err; +static int start_scan(void) { + int err; - err = bt_le_scan_start(BT_LE_SCAN_PASSIVE, split_central_device_found); - if (err) { - LOG_ERR("Scanning failed to start (err %d)", err); - return err; - } + err = bt_le_scan_start(BT_LE_SCAN_PASSIVE, split_central_device_found); + if (err) { + LOG_ERR("Scanning failed to start (err %d)", err); + return err; + } - LOG_DBG("Scanning successfully started"); + LOG_DBG("Scanning successfully started"); return 0; } -static void split_central_connected(struct bt_conn *conn, u8_t conn_err) -{ - char addr[BT_ADDR_LE_STR_LEN]; +static void split_central_connected(struct bt_conn *conn, u8_t conn_err) { + char addr[BT_ADDR_LE_STR_LEN]; - bt_addr_le_to_str(bt_conn_get_dst(conn), addr, sizeof(addr)); + bt_addr_le_to_str(bt_conn_get_dst(conn), addr, sizeof(addr)); - if (conn_err) { - LOG_ERR("Failed to connect to %s (%u)", addr, conn_err); + if (conn_err) { + LOG_ERR("Failed to connect to %s (%u)", log_strdup(addr), conn_err); - bt_conn_unref(default_conn); - default_conn = NULL; + bt_conn_unref(default_conn); + default_conn = NULL; - start_scan(); - return; - } + start_scan(); + return; + } - LOG_DBG("Connected: %s", log_strdup(addr)); + LOG_DBG("Connected: %s", log_strdup(addr)); - split_central_process_connection(conn); + split_central_process_connection(conn); } -static void split_central_disconnected(struct bt_conn *conn, u8_t reason) -{ - char addr[BT_ADDR_LE_STR_LEN]; +static void split_central_disconnected(struct bt_conn *conn, u8_t reason) { + char addr[BT_ADDR_LE_STR_LEN]; - bt_addr_le_to_str(bt_conn_get_dst(conn), addr, sizeof(addr)); + bt_addr_le_to_str(bt_conn_get_dst(conn), addr, sizeof(addr)); - LOG_DBG("Disconnected: %s (reason %d)", log_strdup(addr), reason); + LOG_DBG("Disconnected: %s (reason %d)", log_strdup(addr), reason); - if (default_conn != conn) { - return; - } + if (default_conn != conn) { + return; + } - bt_conn_unref(default_conn); - default_conn = NULL; + bt_conn_unref(default_conn); + default_conn = NULL; - start_scan(); + start_scan(); } static struct bt_conn_cb conn_callbacks = { - .connected = split_central_connected, - .disconnected = split_central_disconnected, + .connected = split_central_connected, + .disconnected = split_central_disconnected, }; -int zmk_split_bt_central_init(struct device *_arg) -{ - bt_conn_cb_register(&conn_callbacks); +int zmk_split_bt_central_init(struct device *_arg) { + bt_conn_cb_register(&conn_callbacks); - return start_scan(); + return start_scan(); } -SYS_INIT(zmk_split_bt_central_init, - APPLICATION, - CONFIG_ZMK_BLE_INIT_PRIORITY);
\ No newline at end of file +SYS_INIT(zmk_split_bt_central_init, APPLICATION, CONFIG_ZMK_BLE_INIT_PRIORITY);
\ No newline at end of file diff --git a/app/src/split/bluetooth/service.c b/app/src/split/bluetooth/service.c index e1d232a..86af685 100644 --- a/app/src/split/bluetooth/service.c +++ b/app/src/split/bluetooth/service.c @@ -1,6 +1,16 @@ +/* + * Copyright (c) 2020 The ZMK Contributors + * + * SPDX-License-Identifier: MIT + */ #include <zephyr/types.h> #include <sys/util.h> + +#include <logging/log.h> + +LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL); + #include <bluetooth/gatt.h> #include <bluetooth/uuid.h> @@ -11,40 +21,36 @@ static u8_t num_of_positions = ZMK_KEYMAP_LEN; static u8_t position_state[16]; -static ssize_t split_svc_pos_state(struct bt_conn *conn, const struct bt_gatt_attr *attrs, void *buf, u16_t len, u16_t offset) -{ - return bt_gatt_attr_read(conn, attrs, buf, len, offset, &position_state, sizeof(position_state)); +static ssize_t split_svc_pos_state(struct bt_conn *conn, const struct bt_gatt_attr *attrs, + void *buf, u16_t len, u16_t offset) { + return bt_gatt_attr_read(conn, attrs, buf, len, offset, &position_state, + sizeof(position_state)); } -static ssize_t split_svc_num_of_positions(struct bt_conn *conn, const struct bt_gatt_attr *attrs, void *buf, u16_t len, u16_t offset) -{ +static ssize_t split_svc_num_of_positions(struct bt_conn *conn, const struct bt_gatt_attr *attrs, + void *buf, u16_t len, u16_t offset) { return bt_gatt_attr_read(conn, attrs, buf, len, offset, attrs->user_data, sizeof(u8_t)); } -static void split_svc_pos_state_ccc(const struct bt_gatt_attr *attr, u16_t value) -{ +static void split_svc_pos_state_ccc(const struct bt_gatt_attr *attr, u16_t value) { + LOG_DBG("value %d", value); } +BT_GATT_SERVICE_DEFINE( + split_svc, BT_GATT_PRIMARY_SERVICE(BT_UUID_DECLARE_128(ZMK_SPLIT_BT_SERVICE_UUID)), + BT_GATT_CHARACTERISTIC(BT_UUID_DECLARE_128(ZMK_SPLIT_BT_CHAR_POSITION_STATE_UUID), + BT_GATT_CHRC_READ | BT_GATT_CHRC_NOTIFY, BT_GATT_PERM_READ_ENCRYPT, + split_svc_pos_state, NULL, &position_state), + BT_GATT_CCC(split_svc_pos_state_ccc, BT_GATT_PERM_READ_ENCRYPT | BT_GATT_PERM_WRITE_ENCRYPT), + BT_GATT_DESCRIPTOR(BT_UUID_NUM_OF_DIGITALS, BT_GATT_PERM_READ, split_svc_num_of_positions, NULL, + &num_of_positions), ); -BT_GATT_SERVICE_DEFINE(split_svc, - BT_GATT_PRIMARY_SERVICE(BT_UUID_DECLARE_128(ZMK_SPLIT_BT_SERVICE_UUID)), - BT_GATT_CHARACTERISTIC(BT_UUID_DECLARE_128(ZMK_SPLIT_BT_CHAR_POSITION_STATE_UUID), BT_GATT_CHRC_READ | BT_GATT_CHRC_NOTIFY, - BT_GATT_PERM_READ_ENCRYPT, - split_svc_pos_state, NULL, &position_state), - BT_GATT_CCC(split_svc_pos_state_ccc, - BT_GATT_PERM_READ_ENCRYPT | BT_GATT_PERM_WRITE_ENCRYPT), - BT_GATT_DESCRIPTOR(BT_UUID_NUM_OF_DIGITALS, BT_GATT_PERM_READ, - split_svc_num_of_positions, NULL, &num_of_positions), -); - -int zmk_split_bt_position_pressed(u8_t position) -{ +int zmk_split_bt_position_pressed(u8_t position) { WRITE_BIT(position_state[position / 8], position % 8, true); return bt_gatt_notify(NULL, &split_svc.attrs[1], &position_state, sizeof(position_state)); } -int zmk_split_bt_position_released(u8_t position) -{ +int zmk_split_bt_position_released(u8_t position) { WRITE_BIT(position_state[position / 8], position % 8, false); return bt_gatt_notify(NULL, &split_svc.attrs[1], &position_state, sizeof(position_state)); }
\ No newline at end of file diff --git a/app/src/split_listener.c b/app/src/split_listener.c index 65f835a..b2adfb2 100644 --- a/app/src/split_listener.c +++ b/app/src/split_listener.c @@ -1,5 +1,5 @@ /* - * Copyright (c) 2020 Peter Johanson <peter@peterjohanson.com> + * Copyright (c) 2020 The ZMK Contributors * * SPDX-License-Identifier: MIT */ @@ -19,17 +19,17 @@ LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL); #include <zmk/hid.h> #include <zmk/endpoints.h> -int split_listener(const struct zmk_event_header *eh) -{ - if (is_position_state_changed(eh)) { - const struct position_state_changed *ev = cast_position_state_changed(eh); - if (ev->state) { - zmk_split_bt_position_pressed(ev->position); - } else { - zmk_split_bt_position_released(ev->position); +int split_listener(const struct zmk_event_header *eh) { + LOG_DBG(""); + if (is_position_state_changed(eh)) { + const struct position_state_changed *ev = cast_position_state_changed(eh); + if (ev->state) { + return zmk_split_bt_position_pressed(ev->position); + } else { + return zmk_split_bt_position_released(ev->position); + } } - } - return 0; + return 0; } ZMK_LISTENER(split_listener, split_listener); diff --git a/app/src/usb.c b/app/src/usb.c new file mode 100644 index 0000000..434b3d4 --- /dev/null +++ b/app/src/usb.c @@ -0,0 +1,86 @@ +/* + * Copyright (c) 2020 The ZMK Contributors + * + * SPDX-License-Identifier: MIT + */ + +#include <device.h> +#include <init.h> + +#include <usb/usb_device.h> +#include <usb/class/usb_hid.h> +#include <dt-bindings/zmk/keys.h> + +#include <zmk/hid.h> +#include <zmk/keymap.h> + +LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL); + +static enum usb_dc_status_code usb_status = USB_DC_UNKNOWN; + +#ifdef CONFIG_ZMK_USB + +static struct device *hid_dev; + +static K_SEM_DEFINE(hid_sem, 1, 1); + +static void in_ready_cb(void) { k_sem_give(&hid_sem); } + +static const struct hid_ops ops = { + .int_in_ready = in_ready_cb, +}; + +int zmk_usb_hid_send_report(const u8_t *report, size_t len) { + switch (usb_status) { + case USB_DC_SUSPEND: + return usb_wakeup_request(); + case USB_DC_ERROR: + case USB_DC_RESET: + case USB_DC_DISCONNECTED: + case USB_DC_UNKNOWN: + return -ENODEV; + default: + k_sem_take(&hid_sem, K_MSEC(30)); + int err = hid_int_ep_write(hid_dev, report, len, NULL); + + if (err) { + k_sem_give(&hid_sem); + } + + return err; + } +} + +#endif /* CONFIG_ZMK_USB */ + +enum usb_dc_status_code zmk_usb_get_status() { return usb_status; } + +void usb_status_cb(enum usb_dc_status_code status, const u8_t *params) { usb_status = status; }; + +static int zmk_usb_init(struct device *_arg) { + int usb_enable_ret; + +#ifdef CONFIG_ZMK_USB + hid_dev = device_get_binding("HID_0"); + if (hid_dev == NULL) { + LOG_ERR("Unable to locate HID device"); + return -EINVAL; + } + + usb_hid_register_device(hid_dev, zmk_hid_report_desc, sizeof(zmk_hid_report_desc), &ops); + + usb_hid_init(hid_dev); + +#endif /* CONFIG_ZMK_USB */ + + usb_enable_ret = usb_enable(usb_status_cb); + + if (usb_enable_ret != 0) { + LOG_ERR("Unable to enable USB"); + return -EINVAL; + } + + return 0; +} + +SYS_INIT(zmk_usb_init, APPLICATION, CONFIG_ZMK_USB_INIT_PRIORITY); diff --git a/app/src/usb_hid.c b/app/src/usb_hid.c deleted file mode 100644 index 784fc25..0000000 --- a/app/src/usb_hid.c +++ /dev/null @@ -1,87 +0,0 @@ - -#include <device.h> -#include <init.h> - -#include <usb/usb_device.h> -#include <usb/class/usb_hid.h> -#include <dt-bindings/zmk/keys.h> - -#include <zmk/hid.h> -#include <zmk/keymap.h> - -LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL); - -static enum usb_dc_status_code usb_status = USB_DC_UNKNOWN; - -static struct device *hid_dev; - -static K_SEM_DEFINE(hid_sem, 1, 1); - -static void in_ready_cb(void) -{ - k_sem_give(&hid_sem); -} - -static const struct hid_ops ops = -{ - .int_in_ready = in_ready_cb, -}; - -int zmk_usb_hid_send_report(const u8_t *report, size_t len) -{ - switch(usb_status) { - case USB_DC_SUSPEND: - return usb_wakeup_request(); - case USB_DC_ERROR: - case USB_DC_RESET: - case USB_DC_DISCONNECTED: - case USB_DC_UNKNOWN: - return -ENODEV; - default: - k_sem_take(&hid_sem, K_MSEC(30)); - int err = hid_int_ep_write(hid_dev, report, len, NULL); - - if (err) { - k_sem_give(&hid_sem); - } - - return err; - } -} - -void usb_hid_status_cb(enum usb_dc_status_code status, const u8_t *params) -{ - usb_status = status; -}; - -static int zmk_usb_hid_init(struct device *_arg) -{ - int usb_enable_ret; - - hid_dev = device_get_binding("HID_0"); - if (hid_dev == NULL) - { - LOG_ERR("Unable to locate HID device"); - return -EINVAL; - } - - usb_hid_register_device(hid_dev, - zmk_hid_report_desc, sizeof(zmk_hid_report_desc), - &ops); - - usb_hid_init(hid_dev); - - usb_enable_ret = usb_enable(usb_hid_status_cb); - - if (usb_enable_ret != 0) - { - LOG_ERR("Unable to enable USB"); - return -EINVAL; - } - - return 0; -} - -SYS_INIT(zmk_usb_hid_init, - APPLICATION, - CONFIG_ZMK_USB_INIT_PRIORITY); diff --git a/app/tests/hold-tap/balanced/many-nested/events.patterns b/app/tests/hold-tap/balanced/many-nested/events.patterns new file mode 100644 index 0000000..fdf2b15 --- /dev/null +++ b/app/tests/hold-tap/balanced/many-nested/events.patterns @@ -0,0 +1,4 @@ +s/.*hid_listener_keycode/kp/p +s/.*mo_keymap_binding/mo/p +s/.*on_hold_tap_binding/ht_binding/p +s/.*decide_hold_tap/ht_decide/p
\ No newline at end of file diff --git a/app/tests/hold-tap/balanced/many-nested/keycode_events.snapshot b/app/tests/hold-tap/balanced/many-nested/keycode_events.snapshot new file mode 100644 index 0000000..806896f --- /dev/null +++ b/app/tests/hold-tap/balanced/many-nested/keycode_events.snapshot @@ -0,0 +1,20 @@ +ht_binding_pressed: 0 new undecided hold_tap +ht_decide: 0 decided hold (balanced event 3) +kp_pressed: usage_page 0x07 keycode 0xe1 +ht_binding_pressed: 1 new undecided hold_tap +ht_decide: 1 decided hold (balanced event 3) +kp_pressed: usage_page 0x07 keycode 0xe0 +ht_binding_pressed: 2 new undecided hold_tap +ht_binding_released: 0 cleaning up hold-tap +ht_decide: 2 decided hold (balanced event 3) +kp_pressed: usage_page 0x07 keycode 0xe3 +ht_binding_pressed: 3 new undecided hold_tap +ht_binding_released: 1 cleaning up hold-tap +ht_decide: 3 decided hold (balanced event 3) +kp_pressed: usage_page 0x07 keycode 0xe2 +kp_released: usage_page 0x07 keycode 0xe1 +kp_released: usage_page 0x07 keycode 0xe0 +kp_released: usage_page 0x07 keycode 0xe3 +ht_binding_released: 2 cleaning up hold-tap +kp_released: usage_page 0x07 keycode 0xe2 +ht_binding_released: 3 cleaning up hold-tap diff --git a/app/tests/hold-tap/balanced/many-nested/native_posix.keymap b/app/tests/hold-tap/balanced/many-nested/native_posix.keymap new file mode 100644 index 0000000..3cb04c3 --- /dev/null +++ b/app/tests/hold-tap/balanced/many-nested/native_posix.keymap @@ -0,0 +1,41 @@ +#include <dt-bindings/zmk/keys.h> +#include <behaviors.dtsi> +#include <dt-bindings/zmk/kscan-mock.h> + +/ { + behaviors { + ht_bal: behavior_hold_tap_balanced { + compatible = "zmk,behavior-hold-tap"; + label = "HOLD_TAP_BALANCED"; + #binding-cells = <2>; + flavor = "balanced"; + tapping_term_ms = <300>; + bindings = <&kp>, <&kp>; + }; + }; + + keymap { + compatible = "zmk,keymap"; + label ="Default keymap"; + + default_layer { + bindings = < + &ht_bal LSFT F &ht_bal LCTL J + &ht_bal LGUI H &ht_bal LALT L + >; + }; + }; +}; + +&kscan { + events = < + ZMK_MOCK_PRESS(0,0,100) + ZMK_MOCK_PRESS(0,1,100) + ZMK_MOCK_PRESS(1,0,100) + ZMK_MOCK_PRESS(1,1,100) + ZMK_MOCK_RELEASE(0,0,100) + ZMK_MOCK_RELEASE(0,1,100) + ZMK_MOCK_RELEASE(1,0,100) + ZMK_MOCK_RELEASE(1,1,100) + >; +}; diff --git a/docs/blog/2020-09-21-zmk-sotf-2.md b/docs/blog/2020-09-21-zmk-sotf-2.md new file mode 100644 index 0000000..82278ad --- /dev/null +++ b/docs/blog/2020-09-21-zmk-sotf-2.md @@ -0,0 +1,106 @@ +--- +title: ZMK State Of The Firmware \#2 +author: Pete Johanson +author_title: Project Creator +author_url: https://gitlab.com/petejohanson +author_image_url: https://www.gravatar.com/avatar/2001ceff7e9dc753cf96fcb2e6f41110 +tags: [SOTF, keyboards, firmware, oss, ble] +--- + +Welcome to the second ZMK "State Of The Firmware" (SOTF)! + +This update will cover all the major activity since [SOTF #1](/blog/2020/08/12/zmk-sotf-1), preparations for the upcoming +Hacktoberfest activity, and a current open call for community feedback on a ZMK mascot. + +## Recent Activity + +So much going on in ZMK! + +- Added a new generic [Hold Tap behavior](https://zmkfirmware.dev/docs/behavior/hold-tap) + in [#146](https://github.com/zmkfirmware/zmk/pull/146) which now powers mod-tap, layer-tap, etc. - [okke-formsma] +- [BLE profile/connection management](https://zmkfirmware.dev/docs/behavior/bluetooth) + in [#133](https://github.com/zmkfirmware/zmk/pull/133) - [petejohanson] +- Integration tests were added to automate testing of behaviors in [#131](https://github.com/zmkfirmware/zmk/pull/131) by [BrainWart] & [petejohanson] +- [Toggle layer behavior](https://zmkfirmware.dev/docs/behavior/layers#toggle-layer), e.g. `&tog LOWER`, in + [#98](https://github.com/zmkfirmware/zmk/pull/98) - [BrainWart] +- Key fix for dropped press/release over HID [#93](https://github.com/zmkfirmware/zmk/pull/93)/[#96](https://github.com/zmkfirmware/zmk/pull/96) - [careyk007](https://github.com/careyk007) & [petejohanson] +- Code formatting standardized using `clang-format` in [#183](https://github.com/zmkfirmware/zmk/pull/183) - [petejohanson] +- [Bootloader reset behavior](https://zmkfirmware.dev/docs/behavior/reset#bootloader-reset), e.g. `&bootloader`, in [#116](https://github.com/zmkfirmware/zmk/pull/116) - [petejohanson] +- Various bug fixes and documentation + +## New Shields + +- QAZ in [#130](https://github.com/zmkfirmware/zmk/pull/130) - [tominabox1](https://github.com/tominabox1) +- Iris in [#151](https://github.com/zmkfirmware/zmk/pull/151) - [kurtis-lew](https://github.com/kurtis-lew) +- RoMac 2.1 in [#122](https://github.com/zmkfirmware/zmk/pull/122) - [bmcgavin](https://github.com/bmcgavin) +- Sofle in [#118](https://github.com/zmkfirmware/zmk/pull/118) - [CrossR](https://github.com/CrossR) +- splitreus62 in [#92](https://github.com/zmkfirmware/zmk/pull/92) - [Na-Cly](https://github.com/Na-Cly) + +## New Boards + +- DZ60RGB rev1 in [#166](https://github.com/zmkfirmware/zmk/pull/166) - [Nicell] +- nrfMicro in [#101](https://github.com/zmkfirmware/zmk/pull/101) - [okke-formsma] +- BlueMicro840 [#91](https://github.com/zmkfirmware/zmk/pull/91) - [Na-Cly](https://github.com/Na-Cly) + +## Hacktoberfest Preparation + +[Hacktoberfest](https://hacktoberfest.digitalocean.com/) is a yearly celebration of open source, +which encourages participation in OSS, especially from new contributors. + +The ZMK contributors have been busy preparing for folks to join in on the fun by contributing to +ZMK! + +- There is now a basic [Contributing Guide](https://github.com/zmkfirmware/zmk/blob/main/CONTRIBUTING.md) to help newcomers get oriented, and get up to speed. +- The [`Hacktoberfest`](https://github.com/zmkfirmware/zmk/issues?q=is%3Aissue+is%3Aopen+label%3AHacktoberfest) + issue label will help participants discover good issues to work on. + (The existing [`good first issue`](https://github.com/zmkfirmware/zmk/issues?q=is%3Aissue+is%3Aopen+label%3A%22good+first+issue%22) label also helps with this) + +We're looking forward to the launch of Hacktoberfest! + +## Mascot Selection Feedback + +The ZMK project would like to settle on a mascot! We're soliciting community feedback as part of +the process before a final mascot is selected. + +The current mascots up for consideration are: + +- Griffin +- Peregrine Falcon +- Zapata Wren +- Zorro (south american fox) + +If you're interested in helping with the decision, head over to [Issue #195](https://github.com/zmkfirmware/zmk/issues/195) and add a reaction! + +## Coming Soon! + +There still lots of activity in ZMK, and plenty of exciting upcoming changes. + +- Improved modifier infrastructure, including "shifted keycodes" - [okke-formsma] +- Battery percentage reporting over BLE - [Nicell] +- Complete defines for HID keycodes/usage IDs - [innovaker](https://github.com/innovaker) +- Additional core BLE connection/bond management work - [petejohanson] +- Improved power management - [petejohanson], [Nicell] +- One shot mod/layer behaviors - [okke-formsma] + +## Statistics + +Some statistics of interest for ZMK: + +- GitHub + - 115 Closed PRs + - 64 Stars + - 48 Forks +- Discord Chat + - 186 total registered +- Website (last 30 days) + - 7.4K page views + - 474 new users + +## Thanks! + +Thanks again to the numerous contributors and users who have made working on ZMK such a pleasure! + +[okke-formsma]: https://github.com/okke-formsma +[nicell]: https://github.com/Nicell +[petejohanson]: https://github.com/petejohanson +[brainwart]: https://github.com/BrainWart diff --git a/docs/docs/assets/dev-setup/vscode_devcontainer.png b/docs/docs/assets/dev-setup/vscode_devcontainer.png Binary files differnew file mode 100644 index 0000000..e5c22b0 --- /dev/null +++ b/docs/docs/assets/dev-setup/vscode_devcontainer.png diff --git a/docs/docs/assets/pro-micro/pro-micro-pins-labelled.jpg b/docs/docs/assets/pro-micro/pro-micro-pins-labelled.jpg Binary files differnew file mode 100644 index 0000000..f72d407 --- /dev/null +++ b/docs/docs/assets/pro-micro/pro-micro-pins-labelled.jpg diff --git a/docs/docs/assets/troubleshooting/filetransfer/linux.png b/docs/docs/assets/troubleshooting/filetransfer/linux.png Binary files differnew file mode 100644 index 0000000..c192dfa --- /dev/null +++ b/docs/docs/assets/troubleshooting/filetransfer/linux.png diff --git a/docs/docs/assets/troubleshooting/filetransfer/mac.png b/docs/docs/assets/troubleshooting/filetransfer/mac.png Binary files differnew file mode 100644 index 0000000..f28ca8d --- /dev/null +++ b/docs/docs/assets/troubleshooting/filetransfer/mac.png diff --git a/docs/docs/assets/troubleshooting/filetransfer/windows.png b/docs/docs/assets/troubleshooting/filetransfer/windows.png Binary files differnew file mode 100644 index 0000000..91cb8a6 --- /dev/null +++ b/docs/docs/assets/troubleshooting/filetransfer/windows.png diff --git a/docs/docs/assets/troubleshooting/keymaps/errorscreen.png b/docs/docs/assets/troubleshooting/keymaps/errorscreen.png Binary files differnew file mode 100644 index 0000000..73b5b58 --- /dev/null +++ b/docs/docs/assets/troubleshooting/keymaps/errorscreen.png diff --git a/docs/docs/assets/troubleshooting/keymaps/healthyEDIT.png b/docs/docs/assets/troubleshooting/keymaps/healthyEDIT.png Binary files differnew file mode 100644 index 0000000..50d2c73 --- /dev/null +++ b/docs/docs/assets/troubleshooting/keymaps/healthyEDIT.png diff --git a/docs/docs/assets/troubleshooting/keymaps/unhealthyEDIT.png b/docs/docs/assets/troubleshooting/keymaps/unhealthyEDIT.png Binary files differnew file mode 100644 index 0000000..3fd8dc7 --- /dev/null +++ b/docs/docs/assets/troubleshooting/keymaps/unhealthyEDIT.png diff --git a/docs/docs/behavior/bluetooth.md b/docs/docs/behavior/bluetooth.md new file mode 100644 index 0000000..be0fb23 --- /dev/null +++ b/docs/docs/behavior/bluetooth.md @@ -0,0 +1,76 @@ +--- +title: Bluetooth Behavior +sidebar_label: Bluetooth +--- + +## Summary + +The bluetooth behavior allows management of various settings and states related to the bluetooth connection(s) +between the keyboard and the host. By default, ZMK supports five "profiles" for selecting which bonded host +computer/laptop/keyboard should receive the keyboard input; many of the commands here operation on those profiles. + +## Bluetooth Command Defines + +Bluetooth command defines are provided through the [`dt-bindings/zmk/bt.h`](https://github.com/zmkfirmware/zmk/blob/main/app/include/dt-bindings/zmk/bt.h) header, +which is added at the top of the keymap file: + +``` +#include <dt-bindings/zmk/bt.h> +``` + +This will allow you to reference the actions defined in this header such as `BT_CLR_CMD`. + +Here is a table describing the command for each define: + +| Define | Action | +| ------------ | ---------------------------------------------------------------------------------------------- | +| `BT_CLR_CMD` | Clear bond information between the keyboard and host for the selected profile. | +| `BT_NXT_CMD` | Switch to the next profile, cycling through to the first one when the end is reached. | +| `BT_PRV_CMD` | Switch to the previous profile, cycling through to the last one when the beginning is reached. | +| `BT_SEL_CMD` | Select the 0-indexed profile by number. | + +Because at least one bluetooth commands takes an additional parameter, it is recommended to use +the following aliases in your keymap to avoid having to specify an ignored second parameter: + +| Define | Action | +| -------- | -------------------------------------------------------------------------------- | +| `BT_CLR` | Alias for `BT_CLR_CMD 0` to clear the current profile's bond to the current host | +| `BT_NXT` | Alias for `BT_NXT_CMD 0` to select the next profile | +| `BT_PRV` | Alias for `BT_PRV_CMD 0` to select the previous profile | +| `BT_SEL` | Alias for `BT_SEL_CMD` to select the given profile, e.g. `&bt BT_SEL 1` | + +## Bluetooth Behavior + +The bluetooth behavior completes an bluetooth action given on press. + +### Behavior Binding + +- Reference: `&bt` +- Parameter #1: The bluetooth command define, e.g. `BT_CLR_CMD` +- Parameter #2: (Reserved for future bluetooth command types) + +### Examples + +1. Behavior binding to clear the paired host for the selected profile: + + ``` + &bt BT_CLR + ``` + +1. Behavior binding to select the next profile: + + ``` + &bt BT_NXT + ``` + +1. Behavior binding to select the previous profile: + + ``` + &bt BT_PRV + ``` + +1. Behavior binding to select the 2nd profile (passed parameters are [zero based](https://en.wikipedia.org/wiki/Zero-based_numbering)): + + ``` + &bt BT_SEL 1 + ``` diff --git a/docs/docs/behavior/hold-tap.md b/docs/docs/behavior/hold-tap.md index fa68538..9f8f5fa 100644 --- a/docs/docs/behavior/hold-tap.md +++ b/docs/docs/behavior/hold-tap.md @@ -22,7 +22,11 @@ We call this the 'hold-preferred' flavor of hold-taps. While this flavor may wor  -### Configuration +### Basic usage +For basic usage, please see [mod-tap](./mod-tap.md) and [layer-tap](./layers.md) pages. + + +### Advanced Configuration A code example which configures a mod-tap setting that works with homerow mods: ``` @@ -58,5 +62,5 @@ If this config does not work for you, try the flavor "tap-preferred" and a short If you want to use a tap-hold with a keycode from a different code page, you have to define another behavior with another "bindings" parameter.For example, if you want to use SHIFT and volume up, define the bindings like `bindings = <&kp>, <&cp>;`. Only single-argument behaviors are supported at the moment. -#### Note -Astute readers may notice similarities between the possible behaviors in ZMK and other firmware, such as QMK. The hold-preferred flavor works similar to the `HOLD_ON_OTHER_KEY_PRESS` setting. The 'balanced' flavor is similar to the `PERMISSIVE_HOLD` setting, and the `tap-preferred` flavor is similar to `IGNORE_MOD_TAP_INTERRUPT`.
\ No newline at end of file +#### Comparison to QMK +The hold-preferred flavor works similar to the `HOLD_ON_OTHER_KEY_PRESS` setting in QMK. The 'balanced' flavor is similar to the `PERMISSIVE_HOLD` setting, and the `tap-preferred` flavor is similar to `IGNORE_MOD_TAP_INTERRUPT`.
\ No newline at end of file diff --git a/docs/docs/behavior/key-press.md b/docs/docs/behavior/key-press.md index f58225d..d427f9d 100644 --- a/docs/docs/behavior/key-press.md +++ b/docs/docs/behavior/key-press.md @@ -27,6 +27,11 @@ There is an [open issue](https://github.com/zmkfirmware/zmk/issues/21) to provid complete set of defines for the full keypad and consumer usage pages in the future for ZMK. ::: +### Improperly defined keymap - `dtlib.DTError: <board>.dts.pre.tmp:<line number>` + +When compiling firmware from a keymap, it may be common to encounter an error in the form of a`dtlib.DTError: <board>.dts.pre.tmp:<line number>`. +For instructions to resolve such an error, click [here](../troubleshooting###Improperly-defined-keymap) + ## Keypad Key Press The "keypad key press" behavior sends standard keypad keycodes on press/release. @@ -59,4 +64,4 @@ Example: ``` &cp M_PREV -``` +```
\ No newline at end of file diff --git a/docs/docs/behavior/layers.md b/docs/docs/behavior/layers.md index da7f07f..c769388 100644 --- a/docs/docs/behavior/layers.md +++ b/docs/docs/behavior/layers.md @@ -26,7 +26,7 @@ This allows you to use those defines, e.g. `LOWER` later in your keymap. ## Momentary Layer -The "momentary layer" behavior allows you to enable a layer while a certain key is pressed. Immediately upon +The "momentary layer" behavior enables a layer while a certain key is pressed. Immediately upon activation of the key, the layer is enabled, and immediately open release of the key, the layer is disabled again. @@ -41,9 +41,25 @@ Example: &mo LOWER ``` +## Layer-tap + +The "layer-tap" behavior enables a layer when a key is held, and output another key when the key is only tapped for a short time. For more information on the inner workings of layer-tap, see [hold-tap](./hold-tap.md). + +### Behavior Binding +- Reference: `<` +- Parameter: The layer number to enable when held, e.g. `1` +- Parameter: The keycode to send when tapped, e.g. `A` + +Example: + +``` +< LOWER SPC +``` + + ## Toggle Layer -The "toggle layer" behavior allows you to enable a layer until the layer is manually disabled. +The "toggle layer" behavior enables a layer until the layer is manually disabled. ### Behavior Binding diff --git a/docs/docs/behavior/mod-tap.md b/docs/docs/behavior/mod-tap.md index dcac492..068928a 100644 --- a/docs/docs/behavior/mod-tap.md +++ b/docs/docs/behavior/mod-tap.md @@ -31,8 +31,8 @@ You can configure a different tapping term in your keymap: ``` &mt { - tapping_term_ms: <400>; -} + tapping_term_ms = <400>; +}; / { keymap { diff --git a/docs/docs/behavior/power.md b/docs/docs/behavior/power.md new file mode 100644 index 0000000..6b8237b --- /dev/null +++ b/docs/docs/behavior/power.md @@ -0,0 +1,64 @@ +--- +title: Power Management Behaviors +sidebar_label: Power Management +--- + +## Summary + +These page contains some of the power management behaviors currently supported by ZMK. + +## External Power Control + +The External power control behavior allows enabling or disabling the VCC power output +to save power. Some of the LEDs will consume power even in OFF state. To preserve +battery life in this scenario, some controller boards have support to disable the +external power completely. + +The following boards currently support this feature: +- nRFMicro +- nice!nano + +## External Power Control Command Defines + +External power control command defines are provided through the [`dt-bindings/zmk/ext_power.h`](https://github.com/zmkfirmware/zmk/blob/main/app/include/dt-bindings/zmk/ext_power.h) header, +which is added at the top of the keymap file: + +``` +#include <dt-bindings/zmk/ext_power.h> +``` + +This will allow you to reference the actions defined in this header such as `EXT_POWER_OFF_CMD`. + +Here is a table describing the command for each define: + +| Define | Action | Alias | +| ------------ | -------------------------------------- | -------- | +| `EXT_POWER_OFF_CMD` | Disable the external power. | `EP_OFF` | +| `EXT_POWER_ON_CMD` | Enable the external power. | `EP_ON` | +| `EXT_POWER_TOGGLE_CMD` | Toggle the external power. | `EP_TOG` | + +### Behavior Binding + +- Reference: `&ext_power` +- Parameter#1: Command, e.g `EP_ON` + +### Example: + +1. Behavior binding to enable the external power + + ``` + &ext_power EP_ON + ``` + +1. Behavior binding to disable the external power + + ``` + &ext_power EP_OFF + ``` + +1. Behavior binding to toggle the external power + + ``` + &ext_power EP_TOG + ``` + diff --git a/docs/docs/bond-reset.md b/docs/docs/bond-reset.md index 1ba79ee..504fd72 100644 --- a/docs/docs/bond-reset.md +++ b/docs/docs/bond-reset.md @@ -1,7 +1,7 @@ --- id: bond-reset -title: Reset BLE Connections -sidebar_label: BLE Reset +title: Reset BLE Connections (DEPRECATED) +sidebar_label: BLE Reset (DEPRECATED) --- Known as a 'bond reset', each keyboard has a special key combination independent of the user defined key map which will diff --git a/docs/docs/customization.md b/docs/docs/customization.md index 0bb1794..261c40d 100644 --- a/docs/docs/customization.md +++ b/docs/docs/customization.md @@ -1,10 +1,21 @@ --- id: customization -title: Customizing ZMK +title: Customizing ZMK/`zmk-config` folders sidebar_label: Customizing ZMK --- After verifying you can successfully flash the default firmware, you will probably want to begin customizing your keymap and other keyboard options. +[In the initial setup tutorial](user-setup), you created a Github repository called `zmk-config`. This repository is a discrete filesystem which works +with the main `zmk` firmware repository to build your desired firmware. The main advantage of a discrete configuration folder is ensuring that the +working components of ZMK are kept separate from your personal keyboard settings, reducing the amount of file manipulation in the configuration process. +This makes flashing ZMK to your keyboard much easier, especially because you don't need to keep an up-to-date copy of zmk on your computer at all times. + +On default `zmk-config` folder should contain two files: +* `<shield>.conf` +* `<shield>`.keymap + +However, your config folder can also be modified to include a `boards/` directory for keymaps and configurations for multiple boards/shields +outside of the default keyboard setting definitions. ## Configuration Changes @@ -26,8 +37,23 @@ GitHub Actions job to build your firmware which you can download once it complet If you need to, a review of [Learn The Basics Of Git In Under 10 Minutes](https://www.freecodecamp.org/news/learn-the-basics-of-git-in-under-10-minutes-da548267cc91/) will help you get these steps right. ::: +## Building from a local `zmk` fork using `zmk-config` + +[As outlined here](dev-build-flash), firmware comes in the form of .uf2 files, which can be built locally using the command `west build`. Normally, +`west build` will default to using the in-tree .keymap and .conf files found in your local copy of the `zmk` repository. However, you can append the command, `-DZMK_CONFIG="C:/the/absolute/path/config"` to `west build` in order to use the contents of your `zmk-config` folder instead of the +default keyboard settings. + **Notice that this path should point to the folder labelled `config` within your `zmk-config` folder.** + + +For instance, building kyria firmware from a user `myUser`'s `zmk-config` folder on Windows 10 may look something like this: + +``` +west build -b nice_nano -- -DSHIELD=kyria_left -DZMK_CONFIG="C:/Users/myUser/Documents/Github/zmk-config/config" +``` + ## Flashing Your Changes For normal keyboards, follow the same flashing instructions as before to flash your updated firmware. For split keyboards, only the central (left) side will need to be reflashed if you are just updating your keymap. +More troubleshooting information for split keyboards can be found [here](troubleshooting#split-keyboard-halves-unable-to-pair). diff --git a/docs/docs/dev-boards-shields-keymaps.md b/docs/docs/dev-boards-shields-keymaps.md index cfe5252..9ed5a32 100644 --- a/docs/docs/dev-boards-shields-keymaps.md +++ b/docs/docs/dev-boards-shields-keymaps.md @@ -35,6 +35,8 @@ in the `app/boards/${arch}/${board_name}` directory, e.g. `app/boards/arm/planck ## Pro Micro Compatible Keyboard + + For keyboards that require a (usually Pro Micro compatible) add-on board to operate, the ZMK integration pieces are places in the _shield_ definition for that keyboard, allowing users to swap in different Pro Micro compatible boards (e.g. Proton-C, or nice!nano) and build a firmware the matches their actual diff --git a/docs/docs/dev-build.md b/docs/docs/dev-build.md index 816468e..83ed8cb 100644 --- a/docs/docs/dev-build.md +++ b/docs/docs/dev-build.md @@ -84,6 +84,19 @@ west build -d build/right -b nice_nano -- -DSHIELD=kyria_right ``` This produces `left` and `right` subfolders under the `build` directory and two separate .uf2 files. For future work on a specific half, use the `-d` parameter again to ensure you are building into the correct location. +### Building from `zmk-config` Folder + +Instead of building .uf2 files using the default keymap and config files, you can build directly from your [`zmk-config` folder](user-setup#github-repo) by adding +`-DZMK_CONFIG="C:/the/absolute/path/config"` to your `west build` command. **Notice that this path should point to the folder labelled `config` within your `zmk-config` folder.** + + +For instance, building kyria firmware from a user `myUser`'s `zmk-config` folder on Windows 10 may look something like this: + +``` +west build -b nice_nano -- -DSHIELD=kyria_left -DZMK_CONFIG="C:/Users/myUser/Documents/Github/zmk-config/config" +``` + + ## Flashing Once built, the previously supplied parameters will be remembered so you can run the following to flash your diff --git a/docs/docs/dev-clean-room.md b/docs/docs/dev-clean-room.md index c11171c..2f301ec 100644 --- a/docs/docs/dev-clean-room.md +++ b/docs/docs/dev-clean-room.md @@ -6,7 +6,7 @@ sidebar_label: Clean Room :::warning -Anyone wanting to contribute code to ZMK _must_ read this, and adhere to the steps outlines in order to not violate any licenses/copyright of other projects +Anyone wanting to contribute code to ZMK _MUST_ read this, and adhere to the steps outlines in order to not violate any licenses/copyright of other projects ::: @@ -22,8 +22,8 @@ or duplicating any of the GPL code found in those other projects, even though th Contributors to ZMK must adhere to the following standard. -- Implementations of features for ZMK _MUST_ not reuse any existing code from any projects not licensed with the MIT license. -- Contributors _MUST_ not study or refer to any GPL licensed source code while working on ZMK. +- Implementations of features for ZMK _MUST NOT_ reuse any existing code from any projects not licensed with the MIT license. +- Contributors _MUST NOT_ study or refer to any GPL licensed source code while working on ZMK. - Contributors _MAY_ read the documentation from other GPL licensed projects, to gain a broad understanding of the behavior of certain features in order to implement equivalent features for ZMK. - Contributors _MAY_ refer to the [QMK Configurator](https://config.qmk.fm/) to inspect existing layouts/keymaps for keyboards, and re-implement them for ZMK. diff --git a/docs/docs/dev-guide-new-shield.md b/docs/docs/dev-guide-new-shield.md index 8556623..9f7b23c 100644 --- a/docs/docs/dev-guide-new-shield.md +++ b/docs/docs/dev-guide-new-shield.md @@ -3,6 +3,9 @@ id: dev-guide-new-shield title: New Keyboard Shield --- +import Tabs from '@theme/Tabs'; +import TabItem from '@theme/TabItem'; + ## Overview This guide will walk through the steps necessary to add ZMK support for a keyboard the uses a (Pro Micro compatible) addon MCU board to provide the microprocessor. @@ -13,9 +16,15 @@ The high level steps are: - Add the shield overlay file to define the [KSCAN driver]() for detecting key press/release. - (Optional) Add the matrix transform for mapping KSCAN row/column values to sane key positions. This is needed for non-rectangular keyboards, or where the underlying row/column pin arrangement does not map one to one with logical locations on the keyboard. - Add a default keymap, which users can override in their own configs as needed. +- Add support for features such as encoders, OLED displays, or RGB underglow. +- Update build.yml It may be helpful to review the upstream [shields documentation](https://docs.zephyrproject.org/2.3.0/guides/porting/shields.html#shields) to get a proper understanding of the underlying system before continuing. +:::note +ZMK support for split keyboards requires a few more files than single boards to ensure proper connectivity between the central and peripheral units. Check the following guides thoroughly to ensure that all the files are in place. +::: + ## New Shield Directory Shields for Zephyr applications go into the `boards/shields/` directory; since ZMK's Zephyr application lives in the `app/` subdirectory of the repository, that means the new shield directory should be: @@ -40,6 +49,16 @@ config SHIELD_MY_BOARD This will make sure the new configuration `SHIELD_MY_BOARD` is set to true whenever `my_board` is added as a shield in your build. + +**For split boards**, you will need to add configurations for the left and right sides. +``` +config SHIELD_MY_BOARD_LEFT + def_bool $(shields_list_contains,my_board_left) + +config SHIELD_MY_BOARD_RIGHT + def_bool $(shields_list_contains,my_board_right) +``` + ### Kconfig.defconfig The `Kconfig.defconfig` file is where overrides for various configuration settings @@ -58,7 +77,40 @@ config ZMK_KEYBOARD_NAME endif ``` -## Shield Overlay + +Similarly to defining the halves of a split board in `Kconfig.shield` it is important to set the `ZMK_KEYBOARD_NAME` for each half of a split keyboard. + +``` +if SHIELD_MY_BOARD_LEFT + +config ZMK_KEYBOARD_NAME + default "My Awesome Keyboard Left" + +endif + +if SHIELD_MY_BOARD_RIGHT + +config ZMK_KEYBOARD_NAME + default "My Awesome Keyboard Right" + +endif +``` + +## Shield Overlays + + + + +ZMK uses the green color coded pin names to generate devicetree node references. For example, to refer to the node `D0` in the devicetree files, use `&pro_micro_d 0` or to refer to `A1`, use `&pro_micro_a 1`. + +<Tabs +defaultValue="unibody" +values={[ +{label: 'Unibody Shields', value: 'unibody'}, +{label: 'Split Shields', value: 'split'}, +]}> + +<TabItem value="unibody"> The `<shield_name>.overlay` is the devicetree description of the keyboard shield that is merged with the primary board devicetree description before the build. For ZMK, this file at a minimum should include the [chosen]() node named `zmk,kscan` that references a KSCAN driver instance. For a simple 3x3 macropad matrix, this might look something like: @@ -89,6 +141,146 @@ this might look something like: }; ``` +</TabItem> + +<TabItem value="split"> + +### .dtsi files and Shield Overlays (Split Shields) + +Unlike unibody keyboards, split keyboards have a core .dtsi file with shield overlays for each half of the keyboard. +It is preferred to define only the `col-gpios` or `row-gpios` in the common shield .dtsi, depending on the `diode-direction` value. +For `col2row` directed boards like the iris, the shared .dtsi file may look like this: + +``` +#include <dt-bindings/zmk/matrix-transform.h> + +/ { + chosen { + zmk,kscan = &kscan0; + zmk,matrix_transform = &default_transform; + }; + + default_transform: keymap_transform_0 { + compatible = "zmk,matrix-transform"; + columns = <16>; + rows = <4>; +// | SW6 | SW5 | SW4 | SW3 | SW2 | SW1 | | SW1 | SW2 | SW3 | SW4 | SW5 | SW6 | +// | SW12 | SW11 | SW10 | SW9 | SW8 | SW7 | | SW7 | SW8 | SW9 | SW10 | SW11 | SW12 | +// | SW18 | SW17 | SW16 | SW15 | SW14 | SW13 | | SW13 | SW14 | SW15 | SW16 | SW17 | SW18 | +// | SW24 | SW23 | SW22 | SW21 | SW20 | SW19 | SW25 | | SW25 | SW19 | SW20 | SW21 | SW22 | SW23 | SW24 | +// | SW29 | SW28 | SW27 | SW26 | | SW26 | SW27 | SW28 | SW29 | + 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(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(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(3,0) RC(3,1) RC(3,2) RC(3,3) RC(3,4) RC(3,5) RC(4,2) RC(4,9) RC(3,6) RC(3,7) RC(3,8) RC(3,9) RC(3,10) RC(3,11) + RC(4,3) RC(4,4) RC(4,5) RC(4,6) RC(4,7) RC(4,8) + >; + }; + + kscan0: kscan { + compatible = "zmk,kscan-gpio-matrix"; + label = "KSCAN"; + + diode-direction = "col2row"; + row-gpios + = <&pro_micro_d 6 (GPIO_ACTIVE_HIGH | GPIO_PULL_DOWN)> // Row A from the schematic file + , <&pro_micro_d 7 (GPIO_ACTIVE_HIGH | GPIO_PULL_DOWN)> // Row B from the schematic file + , <&pro_micro_d 8 (GPIO_ACTIVE_HIGH | GPIO_PULL_DOWN)> // Row C from the schematic file + , <&pro_micro_d 0 (GPIO_ACTIVE_HIGH | GPIO_PULL_DOWN)> // Row D from the schematic file + , <&pro_micro_d 4 (GPIO_ACTIVE_HIGH | GPIO_PULL_DOWN)> // Row E from the schematic file + ; + + }; +``` + +:::note +Notice that in addition to the common `row-gpios` that are declared in the kscan, the [matrix transform](#optional-matrix-transform) is defined in the .dtsi. +::: + +The missing `col-gpios` would be defined in your `<boardname>_left.overlay` and `<boardname>_right.overlay` files. +Keep in mind that the mirrored position of the GPIOs means that the `col-gpios` will appear reversed when the .overlay files are compared to one another. +Furthermore, the column offset for the [matrix transform](#optional-matrix-transform) should be added to the right half of the keyboard's overlay +because the keyboard's switch matrix is read from left to right, top to bottom. +This is exemplified with the iris .overlay files. + +``` +// iris_left.overlay + +#include "iris.dtsi" // Notice that the main dtsi files are included in the overlay. + +&kscan0 { + col-gpios + = <&pro_micro_a 1 GPIO_ACTIVE_HIGH> // col1 in the schematic + , <&pro_micro_a 0 GPIO_ACTIVE_HIGH> // col2 in the schematic + , <&pro_micro_d 15 GPIO_ACTIVE_HIGH> // col3 in the schematic + , <&pro_micro_d 14 GPIO_ACTIVE_HIGH> // col4 in the schematic + , <&pro_micro_d 16 GPIO_ACTIVE_HIGH> // col5 in the schematic + , <&pro_micro_d 10 GPIO_ACTIVE_HIGH> // col6 in the schematic + ; +}; +``` + +``` +// iris_right.overlay + +#include "iris.dtsi" + +&default_transform { // The matrix transform for this board is 6 columns over because the left half is 6 columns wide according to the matrix. + col-offset = <6>; +}; + +&kscan0 { + col-gpios + = <&pro_micro_d 10 GPIO_ACTIVE_HIGH> // col6 in the schematic + , <&pro_micro_d 16 GPIO_ACTIVE_HIGH> // col5 in the schematic + , <&pro_micro_d 14 GPIO_ACTIVE_HIGH> // col4 in the schematic + , <&pro_micro_d 15 GPIO_ACTIVE_HIGH> // col3 in the schematic + , <&pro_micro_a 0 GPIO_ACTIVE_HIGH> // col2 in the schematic + , <&pro_micro_a 1 GPIO_ACTIVE_HIGH> // col1 in the schematic + ; +}; + +``` + +### .conf files (Split Shields) + +While unibody boards only have one .conf file that applies configuration characteristics to the entire keyboard, +split keyboards are unique in that they contain multiple .conf files with different scopes. +For example, a split board called `my_awesome_split_board` would have the following files: + +* `my_awesome_split_board.conf` - Configuration elements affect both halves +* `my_awesome_split_board_left.conf` - Configuration elements only affect left half +* `my_awesome_split_board_right.conf` - Configuration elements only affect right half + +For proper communication between keyboard halves and that between the central half and the computer, +the **the central and peripheral halves of the keyboard must be defined**. This can be seen below. + +``` +// Central Half (Usually the left side: my_awesome_split_board_left.conf) + +CONFIG_ZMK_SPLIT=y +CONFIG_ZMK_SPLIT_BLE_ROLE_CENTRAL=y +``` + +``` +// Peripheral Half (Usually the right side: my_awesome_split_board_right.conf) + +CONFIG_ZMK_SPLIT=y +CONFIG_ZMK_SPLIT_BLE_ROLE_Peripheral=y +``` + +Using the .conf file that affects both halves of a split board would be for adding features like deep-sleep or rotary encoders. + +``` +// my_awesome_split_board.conf + +CONFIG_ZMK_SLEEP=y +``` + +</TabItem> +</Tabs> + ## (Optional) Matrix Transform Internally ZMK translates all row/column events into "key position" events to maintain a consistent model that works no matter what any possible GPIO matrix may look like for a certain keyboard. This is particularly helpful when: @@ -195,6 +387,91 @@ Further documentation on behaviors and bindings is forthcoming, but a summary of - `trans` is the "transparent" behavior, useful to be place in higher layers above `mo` bindings to be sure the key release is handled by the lower layer. No binding arguments are required. - `mt` is the "mod-tap" behavior, and takes two binding arguments, the modifier to use if held, and the keycode to send if tapped. +## Adding Features + +### Encoders + +EC11 encoder support can be added to your board or shield by adding the appropriate lines to your board/shield's configuration (.conf), device tree (.dtsi), overlay (.overlay), and keymap (.keymap) files. + +<Tabs +defaultValue="conf" +values={[ +{label: '.conf', value: 'conf'}, +{label: '.dtsi', value: 'dtsi'}, +{label: '.overlay', value: 'overlay'}, +{label: '.keymap', value: 'keymap'}, +]}> +<TabItem value="conf"> + +In your configuration file you will need to add the following lines so that the encoders can be enabled/disabled: + +``` +# Uncomment to enable encoder +# CONFIG_EC11=y +# CONFIG_EC11_TRIGGER_GLOBAL_THREAD=y +``` + +These should be commented by default for encoders that are optional/can be swapped with switches, but can be uncommented if encoders are part of the default design. + +:::note +If building locally for split boards, you may need to add these lines to the specific half's configuration file as well as the combined configuration file. +::: + +</TabItem> +<TabItem value = "dtsi"> +In your device tree file you will need to add the following lines to define the encoder sensor: + + +``` +left_encoder: encoder_left { + compatible = "alps,ec11"; + label = "LEFT_ENCODER"; + a-gpios = <PIN_A (GPIO_ACTIVE_HIGH | GPIO_PULL_UP)>; + b-gpios = <PIN_B (GPIO_ACTIVE_HIGH | GPIO_PULL_UP)>; + resolution = <4>; + }; +``` +Here you will have to replace PIN_A and PIN_B with the appropriate pins that your PCB utilizes for the encoder(s). For keyboards that use the Pro Micro or any of the Pro Micro replacements, Sparkfun's [Pro Micro Hookup Guide](https://learn.sparkfun.com/tutorials/pro-micro--fio-v3-hookup-guide/hardware-overview-pro-micro) has a pinout diagram that can be useful to determine the right pins. Reference either the blue numbers labeled "Arduino" (digital pins) or the green numbers labeled "Analog" (analog pins). For pins that are labeled as both digital and analog, refer to your specific board's .dtsi file to determine how you should refer to that pin. + +Add additional encoders as necessary by duplicating the above lines, replacing `left` with whatever you would like to call your encoder, and updating the pins. Note that support for peripheral (right) side sensors over BLE is still in progress. + +Once you have defined the encoder sensors, you will have to add them to the list of sensors: + +``` +sensors { + compatible = "zmk,keymap-sensors"; + sensors = <&left_encoder &right_encoder>; + }; +``` + +In this example, a left_encoder and right_encoder are both added. Additional encoders can be added with spaces separating each, and the order they are added here determines the order in which you define their behavior in your keymap. + +</TabItem> +<TabItem value = "overlay"> +Add the following lines to your overlay file(s) to enable the encoder: + +``` +&left_encoder { + status = "okay"; +}; +``` + +:::note +For split keyboards, make sure to add left hand encoders to the left .overlay file and right hand encoders to the right .overlay file. +::: + +</TabItem> +<TabItem value = "keymap"> +Add the following line to your keymap file to add default encoder behavior bindings: + +``` +sensor-bindings = <&inc_dec_cp M_VOLU M_VOLD>; +``` +Add additional bindings as necessary to match the default number of encoders on your board. See the [Encoders](/docs/feature/encoders) and [Keymap](/docs/feature/keymaps) feature documentation for more details. + +</TabItem> +</Tabs> + ## Testing Once you've fully created the new keyboard shield definition, @@ -209,3 +486,41 @@ and then flash with: ``` west flash ``` + +:::note +Further testing your keyboard shield without altering the root keymap file can be done with the use of `-DZMK_CONFIG` in your `west build` command, +shown [here](dev-build-flash#building-from-zmk-config-folder) +::: + +## Updating `build.yml` + +Before publishing your shield to the public via a PR, navigate to `build.yml` found in `.github/workflows` and add your shield to the appropriate list. An example edit to `build.yml` is shown below. + +``` +jobs: + build: + runs-on: ubuntu-latest + name: Build Test + strategy: + matrix: + board: [proton_c, nice_nano, bluemicro840_v1, nrfmicro_13] + shield: + - corne_left + - corne_right + - kyria_left + - kyria_right + - lily58_left + - lily58_right + - iris_left + - iris_right + - romac + - <MY_BOARD> + - <MY_SPLIT_BOARD_left> + - <MY_SPLIT_BOARD_right> + include: + - board: proton_c + shield: clueboard_california +``` +:::note +Notice that both the left and right halves of a split board need to be added to the list of shields for proper error checking. +:::note diff --git a/docs/docs/dev-setup.md b/docs/docs/dev-setup.md index 1d7d703..521d5d0 100644 --- a/docs/docs/dev-setup.md +++ b/docs/docs/dev-setup.md @@ -12,16 +12,17 @@ groupId="operating-systems" defaultValue="debian" values={[ {label: 'Debian/Ubuntu', value: 'debian'}, -{label: 'Raspberry OS', value: 'raspberryos'}, -{label: 'Fedora', value: 'fedora'}, {label: 'Windows', value: 'win'}, {label: 'macOS', value: 'mac'}, +{label: 'Raspberry OS', value: 'raspberryos'}, +{label: 'Fedora', value: 'fedora'}, +{label: 'VS Code & Docker', value: 'docker'}, ] }>{props.children}</Tabs>); ## Prerequisites -A unix-like environment with the following base packages installed: +ZMK requires the following base packages to first be installed: - Git - Python 3 @@ -34,6 +35,7 @@ A unix-like environment with the following base packages installed: <OsTabs> <TabItem value="debian"> + On Debian and Ubuntu, we'll use `apt` to install our base dependencies: First, if you haven't updated recently, or if this is a new install, @@ -74,7 +76,8 @@ or download and install CMake version 3.13.1 or newer manually. ::: </TabItem> <TabItem value="raspberryos"> -On Raspberry OS, we'll use apt to install our base dependencies: + +On Raspberry OS, we'll use `apt` to install our base dependencies: First, if you haven't updated recently, or if this is a new install, you should update to get the latest package information: @@ -165,6 +168,10 @@ Chocolatey is recommended and used for the following instructions. You can manua choco install ninja gperf python git ``` +It is recommended to install `dfu-util` to avoid any later confusion while flashing devices. You can do this by running this command with chocolatey: +``` shell +choco install dfu-util +``` </TabItem> <TabItem value="mac"> @@ -173,10 +180,24 @@ Chocolatey is recommended and used for the following instructions. You can manua Homebrew is required to install the system dependencies. If you haven't done so, visit [Homebrew](https://brew.sh/) for instructions. Once installed, use it to install the base dependencies: ``` -brew install cmake ninja python3 ccache dtc git wget +brew install cmake ninja python3 ccache dtc git wget dfu-util ``` </TabItem> +<TabItem value="docker"> + +This setup leverages the same [image which is used by the GitHub action](https://github.com/zmkfirmware/zephyr-west-action) for local development. Beyond the benefits of [dev/prod parity](https://12factor.net/dev-prod-parity), this approach is also the easiest to set up. No toolchain or dependencies are necessary when using Docker; the container image you'll be using already has the toolchain installed and set up to use. + + +1. Install [Docker Desktop](https://www.docker.com/products/docker-desktop) for your operating system. +2. Install [VS Code](https://code.visualstudio.com/) +3. Install the [Remote - Containers extension](https://marketplace.visualstudio.com/items?itemName=ms-vscode-remote.remote-containers) + +:::info +The docker container includes `west` and the compilation toolchain. If you're using docker and VS Code, you can skip right to [Source Code](#source-code). +::: + +</TabItem> </OsTabs> ## Setup @@ -185,14 +206,40 @@ brew install cmake ninja python3 ccache dtc git wget `west` is the [Zephyr™ meta-tool](https://docs.zephyrproject.org/2.3.0/guides/west/index.html) used to configure and build Zephyr™ applications. -West can be installed by using the `pip` python package manager. +West can be installed by using the `pip` python package manager. The [Zephyr™ instructions](https://docs.zephyrproject.org/latest/guides/west/install.html#installing-west) are summarized here: + +<Tabs +defaultValue="linux" +values={[ +{label: 'Linux', value: 'linux'}, +{label: 'Windows', value: 'win'}, +]}> +<TabItem value = 'linux'> ```sh pip3 install --user -U west ``` -:::danger pip user packages -If you haven't done so yet, you may need to add the Python Pip user package directory to your `PATH` otherwise your computer will not be able to find the `west` command. +</TabItem> +<TabItem value = 'win'> + +In `cmd.exe` as **Administrator**: + +```sh +pip3 install -U west +``` + +:::note +**For Windows, do not use the `--user` argument** that Linux uses otherwise `west` will be installed in a different location and the below instructions for adding Python `pip` will no longer apply. +::: + +Once `west` is installed, close Command Prompt and open a new session as a **user** for the remainder of the instructions. + +</TabItem> +</Tabs> + +:::danger `pip` user packages +If you haven't done so yet, you may need to add the Python `pip` package directory to your `PATH` otherwise your computer will not be able to find the `west` command. ::: <Tabs @@ -213,7 +260,8 @@ source ~/.bashrc <TabItem value = 'win'> 1. See the [Environment Variables](#environment-variables) section on how to get to the Environment Variables page. -3. Click "Edit..." and then "New" to add the directory where your west.exe is located. By default this should be something like `C:\Python38\Scripts`. +2. Under "System variables" select the "Path" variable. Click "Edit..." and then "New" to add the directory where your `west.exe` is located. By default this should be `C:\Python##\Scripts` where ## is your Python version number. +3. Close Command Prompt and open a new session for the changes to take effect, or run `refreshenv`. </TabItem> </Tabs> @@ -279,7 +327,7 @@ The installation will prompt with several questions about installation location, #### GNU ARM Embedded -Since the Zephyr™ SDK is not available for Windows, we recommending following the steps to install the [GNU ARM Embedded](https://docs.zephyrproject.org/2.3.0/getting_started/toolchain_3rd_party_x_compilers.html#gnu-arm-embedded). +Since the Zephyr™ SDK is not available for Windows, we recommending following the [Zephyr documentation](https://docs.zephyrproject.org/2.3.0/getting_started/toolchain_3rd_party_x_compilers.html#gnu-arm-embedded) to install a GNU ARM Embedded build. Note the warnings regarding installing the toolchain into a path with spaces, and make sure to follow the steps to add the environment variables which are also summarized with screenshots in the [Environment Variables](#environment-variables) section below. </TabItem> <TabItem value="mac"> @@ -308,7 +356,7 @@ The transient instructions must be run to build firmware using the current shell ### Source Code -Next, you'll need to clone the ZMK source repository if you haven't already: +Next, you'll need to clone the ZMK source repository if you haven't already. Navigate to the folder you would like to place your `zmk` directory in and run the following command: ``` git clone https://github.com/zmkfirmware/zmk.git @@ -320,12 +368,63 @@ Since ZMK is built as a Zephyr™ application, the next step is to use `west` to initialize and update your workspace. The ZMK Zephyr™ application is in the `app/` source directory: + #### Step into the repository +<OsTabs> +<TabItem value="debian"> + +```sh +cd zmk +``` + +</TabItem> +<TabItem value="raspberryos"> + +```sh +cd zmk +``` + +</TabItem> +<TabItem value="fedora"> + +```sh +cd zmk +``` + +</TabItem> +<TabItem value="mac"> + ```sh cd zmk ``` +</TabItem> +<TabItem value="win"> + +```sh +cd zmk +``` + +</TabItem> + +<TabItem value="docker"> + +Open the `zmk` checkout folder in VS Code. The repository includes a configuration for containerized development, so an alert will pop up: + + + +Click `Reopen in Container` in order to reopen the VS Code with the running container. + +The first time you do this on your machine, it will pull the docker image down from the registry and build the container. Subsequent launches are much faster! + +:::caution +All subsequent steps must be performed from the VS Code terminal _inside_ the container. +::: + +</TabItem> +</OsTabs> + #### Initialize West ```sh @@ -344,6 +443,17 @@ section again for links to how to do this west update ``` +:::tip +This step pulls down quite a bit of tooling. Go grab a cup of coffee, it can take 10-15 minutes even on a good internet connection! +::: + +:::info +If you're using Docker, you're done with setup! You must restart the container at this point. The easiest way to do so is to close the VS Code window, verify that the container has stopped in Docker Dashboard, and reopen the container with VS Code. + +Once your container is restarted, proceed to [Building and Flashing](./dev-build.md). +::: + + #### Export Zephyr™ Core ```sh @@ -358,12 +468,17 @@ pip3 install --user -r zephyr/scripts/requirements-base.txt ### Environment Variables -#### For GNU ARM Embedded on Windows +<Tabs +defaultValue="win" +values={[ +{label: 'Windows', value: 'win'}, +{label: 'Other OS', value: 'other'}, +]}> +<TabItem value = 'win'> -On Windows, you will have to set two environment variables for ZMK to build properly: `ZEPHYR_TOOLCHAIN_VARIANT` and `GNUARMEMB_TOOLCHAIN_PATH`. +#### For GNU ARM Embedded on Windows -<details> -<summary> Steps to Update Environment Variables </summary> +On Windows, only two environment variables need to be set for ZMK to build properly: `ZEPHYR_TOOLCHAIN_VARIANT` and `GNUARMEMB_TOOLCHAIN_PATH`. 1. Open Start Menu and type 'env' to find the 'Edit the system environment variables' option. Open it. @@ -381,11 +496,15 @@ On Windows, you will have to set two environment variables for ZMK to build prop  -5. Create another variable with variable name 'GNUARMEMB_TOOLCHAIN_PATH' and value set to wherever you installed your toolchain. Click OK to save. +5. Create another variable with variable name 'GNUARMEMB_TOOLCHAIN_PATH' and value set to wherever you installed your toolchain. **Make sure this path does not contain any spaces.** If it does, rename the folder and update here. Click OK to save.  -</details> +6. Close Command Prompt and reopen, or run `refreshenv` to apply the changes. + +</TabItem> + +<TabItem value = 'other'> #### For Zephyr @@ -396,48 +515,10 @@ We suggest two main [options](https://docs.zephyrproject.org/2.3.0/guides/env_va To load the Zephyr environment properly for just one transient shell, run the following from your ZMK checkout directory: -<OsTabs> -<TabItem value="debian"> - -``` -source zephyr/zephyr-env.sh -``` - -</TabItem> - -<TabItem value="raspberryos"> - -``` -source zephyr/zephyr-env.sh -``` - -</TabItem> - -<TabItem value="fedora"> - -``` -source zephyr/zephyr-env.sh -``` - -</TabItem> - -<TabItem value="mac"> - ``` source zephyr/zephyr-env.sh ``` -</TabItem> - -<TabItem value="win"> - -``` -source zephyr/zephyr-env.cmd -``` - -</TabItem> -</OsTabs> - ##### All Shells To load the environment variables for your shell every time, @@ -469,3 +550,7 @@ cat ~/.zephyrrc >> ~/.zshrc </TabItem> </Tabs> + +</TabItem> + +</Tabs> diff --git a/docs/docs/faq.md b/docs/docs/faq.md index 2ab158d..d8bbd71 100644 --- a/docs/docs/faq.md +++ b/docs/docs/faq.md @@ -76,7 +76,7 @@ Please note, many keyboards only have a single PCB which includes the “brains ### What bootloader does ZMK use? -ZMK isn’t designed for any particular bootloader, and supports flashing different boards with different flash utilities (e.g. OpenOCD, nrfjprog, etc.). So if you have any difficulties, please let us know on [Discord](https://discord.gg/VJnx9kr)! +ZMK isn’t designed for any particular bootloader, and supports flashing different boards with different flash utilities (e.g. OpenOCD, nrfjprog, etc.). So if you have any difficulties, please let us know on [Discord](https://zmkfirmware.dev/community/discord/invite)! ### Can I contribute? @@ -84,11 +84,11 @@ Of course! Please use the developer [documentation](/docs) to get started! ### I have an idea! What should I do? -Please join us on [Discord](https://discord.gg/VJnx9kr) and discuss it with us! +Please join us on [Discord](https://zmkfirmware.dev/community/discord/invite) and discuss it with us! ### I want to add a new keyboard! What should I do? -The exact process for the management of all the possible hardware is still being finalized, but any developer looking to contribute new keyboard definitions should chat with us on [Discord](https://discord.gg/VJnx9kr) to get started. +The exact process for the management of all the possible hardware is still being finalized, but any developer looking to contribute new keyboard definitions should chat with us on [Discord](https://zmkfirmware.dev/community/discord/invite) to get started. ### Does ZMK have a Code of Conduct? diff --git a/docs/docs/feature/encoders.md b/docs/docs/feature/encoders.md index 16537ad..9caccc2 100644 --- a/docs/docs/feature/encoders.md +++ b/docs/docs/feature/encoders.md @@ -3,4 +3,42 @@ title: Encoders sidebar_label: Encoders --- -TODO: Documentation on encoders. +Existing support for encoders in ZMK is focused around the five pin EC11 rotary encoder with push button design used in the majority of current keyboard and macropad designs. + +## Enabling EC11 Encoders + +To enable encoders for boards that have existing encoder support, uncomment the `EC11_CONFIG=y` and `CONFIG_EC11_TRIGGER_GLOBAL_THREAD=y` lines in your board's .conf file in your `zmk-config/config` folder. Save and push your changes, then download and flash the new firmware. + +## Customizing EC11 Encoder Behavior + +Encoder behavior in ZMK is configured in two different locations as the push button and rotation behaviors are handled in two separate ways. + +### Push Button + +Keyboards and macropads with encoder support will typically take the two EC11 pins responsible for the push button and include them as part of the matrix for the keys. To configure what is sent by the push button, find the encoder's position in the keyboard matrix and assign it a behavior the same as you would any other key. + +### Rotation + +Rotation is handled separately as a type of sensor. The behavior for this is set in `sensor-bindings`, which is defined in each keymap layer in the following format: + +``` +sensor-bindings = <BINDING CW_KEY CCW_KEY>; +``` + +- `BINDING` is one of two rotation bindings that are currently defined, `&inc_dec_cp` for consumer key presses or `&inc_dec_kp` for normal key presses (see [Key Press](/docs/behavior/key-press) for the difference between the two). +- `CW_KEY` is the keycode activated by a clockwise turn. +- `CCW_KEY` is the keycode activated by a counter-clockwise turn. + +Additional encoders can be configured by adding more `BINDING CW_KEY CCW_KEY` sets immediately after the first. + +As an example, a complete `sensor-bindings` for a Kyria with two encoders could look like: + +``` +sensor-bindings = <&inc_dec_cp M_VOLU M_VOLD &inc_dec_kp PGUP PGDN>; +``` + +Here, the left encoder is configured to control volume up and down while the right encoder sends either Page Up or Page Down. + +## Adding Encoder Support + +See the [New Keyboard Shield](/docs/dev-guide-new-shield#encoders) documentation for how to add or modify additional encoders to your shield. diff --git a/docs/docs/feature/keymaps.md b/docs/docs/feature/keymaps.md index d991925..56fc2cc 100644 --- a/docs/docs/feature/keymaps.md +++ b/docs/docs/feature/keymaps.md @@ -55,7 +55,7 @@ in the stack _also_ get the event. ## Behavior Bindings Binding a behavior at a certain key position may include up to two extra parameters that are used to -alter the behavior when that specific key position is activated/deactived. For example, when binding +alter the behavior when that specific key position is activated/deactivated. For example, when binding the "key press" (`kp`) behavior at a certain key position, you must specific _which_ keycode should be used for that key position. diff --git a/docs/docs/hardware.md b/docs/docs/hardware.md index 299d1f5..fe229df 100644 --- a/docs/docs/hardware.md +++ b/docs/docs/hardware.md @@ -16,7 +16,7 @@ That being said, there are currently only a few specific [boards](/docs/faq#what ## Boards -- [nice!nano](https://docs.nicekeyboards.com/#/nice!nano/) (`nice_nano`) +- [nice!nano](https://nicekeyboards.com/products/nice-nano-v1-0) (`nice_nano`) - [nrfMicro](https://github.com/joric/nrfmicro) (`nrfmicro_13`, `nrfmicro_11`, `nrfmicro_11_flipped`) - [BlueMicro840](https://store.jpconstantineau.com/#/group/bluemicro) (`bluemicro840_v1`) - [QMK Proton-C](https://qmk.fm/proton-c/) (`proton_c`) @@ -26,6 +26,11 @@ That being said, there are currently only a few specific [boards](/docs/faq#what - [Kyria](https://splitkb.com/products/kyria-pcb-kit) (`kyria_left` and `kyria_right`) - [Corne](https://github.com/foostan/crkbd) (`corne_left` and `corne_right`) - [Lily58](https://github.com/kata0510/Lily58) (`lily58_left` and `lily58_right`) +- [Sofle](https://github.com/josefadamcik/SofleKeyboard) (`sofle_left` and `sofle_right`) +- [Splitreus62](https://github.com/Na-Cly/splitreus62) (`splitreus62_left` and `splitreus62_right`) +- [RoMac+ v4](https://www.littlekeyboards.com/products/romac) (`romac_plus`) +- [RoMac v2](https://mechboards.co.uk/shop/kits/romac-macro-pad/) (`romac`) +- [QAZ](https://www.cbkbd.com/product/qaz-keyboard-kit) (`qaz`) ## Other Hardware diff --git a/docs/docs/intro.md b/docs/docs/intro.md index 29045f7..60fe0d2 100644 --- a/docs/docs/intro.md +++ b/docs/docs/intro.md @@ -5,34 +5,41 @@ sidebar_label: Introduction --- ZMK Firmware is an open source (MIT) keyboard -firmware built on the [Zephyr™ Project](https://zephyrproject.com/) Real Time Operating System (RTOS). - -The goal is to provider a powerful, featureful keyboard firmware that is free -of licensing issues that prevent upstream BLE support as a first-class -feature. +firmware built on the [Zephyr™ Project](https://zephyrproject.org/) Real Time Operating System (RTOS). ZMK's goal is to provide a modern, wireless, and powerful firmware free of licensing issues. ## Features -At this point, ZMK is _missing_ more features than it has. Currently, the mostly working bits -include: - -- HID Over GATT (HOG) - This is the official term for BLE HID devices -- Keymaps and layers with basic keycodes -- Some initial work on one "behavior", Mod-Tap -- Basic HID over USB -- Basic consumer (media) keycodes. -- Basic OLED display logic -- Basic Split support -- Encoders - -## Missing Features - -- One Shot -- Layer Tap -- Complete split support -- Battery reporting -- Low power mode -- Shell over BLE +ZMK is currently missing some features found in other popular firmware. This table compares the features supported by ZMK, BlueMicro and QMK: + + +| **Feature** | ZMK | BlueMicro | QMK | +|--------------------------------------------------------------------------------------------------------|:-----------:|:------------:|:-----------:| +| Low Latency BLE Support | ✅ | ✅ | | +| Multi-Device BLE Support | ✅ | | | +| USB Connectivity | ✅ | | ✅ | +| User Configuration Repositories | ✅ | | | +| Split Keyboard Support | ✅ | ✅ | ✅ | +| [Keymaps and Layers](behavior/layers) | ✅ | ✅ | ✅ | +| [Hold-Tap](behavior/hold-tap) (which includes [Mod-Tap](behavior/mod-tap) and [Layer-Tap](behavior/layers/#layer-tap)) | ✅ | ✅ | ✅ | +| [Basic Keycodes](behavior/key-press) | ✅ | ✅ | ✅ | +| [Basic consumer (Media) Keycodes](behavior/key-press#consumer-key-press) | ✅ | ✅ | ✅ | +| [Encoders](feature/encoders)[^1] | ✅ | | ✅ | +| [OLED Display Support](feature/displays)[^2] | 🚧 | 🚧 | ✅ | +| [RGB Underglow](feature/underglow) | ✅ | ✅ | ✅ | +| One Shot Keys | 🚧 | ✅ | ✅ | +| Combo Keys | 🚧 | | ✅ | +| Macros | 🚧 | ✅ | ✅ | +| Mouse Keys | | ✅ | ✅ | +| Low Active Power Usage | ✅ | | | +| [Low Power Sleep States](https://github.com/zmkfirmware/zmk/pull/211) | 🚧 | ✅ | | +| [Low Power Mode (VCC Shutoff)](https://github.com/zmkfirmware/zmk/pull/242) | 🚧 | | | +| [Battery Reporting](https://github.com/zmkfirmware/zmk/issues/47) | 🚧 | ✅ | | +| Shell over BLE | | | | +| Realtime Keymap Updating | 💡 | | ✅ | +| AVR/8 Bit | | | ✅ | +| [Wide Range of ARM Chips Supported](https://docs.zephyrproject.org/latest/boards/index.html) | ✅ | | | +[^2]: Encoders are not currently supported on peripheral side splits. +[^1]: OLEDs are currently proof of concept in ZMK. ## Code Of Conduct diff --git a/docs/docs/troubleshooting.md b/docs/docs/troubleshooting.md new file mode 100644 index 0000000..36682e7 --- /dev/null +++ b/docs/docs/troubleshooting.md @@ -0,0 +1,82 @@ +--- +id: troubleshooting +title: Troubleshooting +sidebar_title: Troubleshooting +--- +### Summary + +The following page provides suggestions for common errors that may occur during firmware compilation. If the information provided is insufficient to resolve the issue, feel free to seek out help from the [ZMK Discord](https://zmkfirmware.dev/community/discord/invite). + +### File Transfer Error + +Variations of the warnings shown below occur when flashing the `<firmware>.uf2` onto the microcontroller. This is because the microcontroller resets itself before the OS receives confirmation that the file transfer is complete. Errors like this are normal and can generally be ignored. Verification of a functional board can be done by attempting to pair your newly flashed keyboard to your computer via Bluetooth or plugging in a USB cable if `ZMK_USB` is enabled in your Kconfig.defconfig. + +|  | +| :-------------------------------------------------------------------------------: | +| An example of the file transfer error on Windows 10 | + +|  | +| :-------------------------------------------------------------------------------: | +| An example of the file transfer error on Linux | + +|  | +| :-------------------------------------------------------------------------------: | +| An example of the file transfer error on MacOS | + + +### CMake Error + +``` +CMake Warning at C:/zmk/zephyr/subsys/usb/CMakeLists.txt:28 (message): + CONFIG_USB_DEVICE_VID has default value 0x2FE3. + + This value is only for testing and MUST be configured for USB products. + + +CMake Warning at C:/zmk/zephyr/subsys/usb/CMakeLists.txt:34 (message): + CONFIG_USB_DEVICE_PID has default value 0x100. + + This value is only for testing and MUST be configured for USB products. +``` + +CMake Warnings shown above during `west build` are normal occurrences. They should not negatively affect the firmware's ability to function as normal. + +On the other hand, an error along the lines of `CMake Error at (zmk directory)/zephyr/cmake/generic_toolchain.cmake:64 (include): include could not find load file:` during firmware compilation indicates that the Zephyr Environment Variables are not properly defined. +For more information, click [here](../docs/dev-setup#environment-variables). + + +### dtlib.DTError + +An error along the lines of `dtlib.DTError: <board>.dts.pre.tmp:<line number>` during firmware compilation indicates an issue within the `<shield>.keymap` file. +This can be verified by checking the file in question, found in `mkdir/app/build`. + +|  | +| :-------------------------------------------------------------------------------: | +| An example of the dtlib.DTError when compiling an iris with the nice!nano while the keymap is not properly defined | + +After opening the `<board>.dts.pre.tmp:<line number>` and scrolling down to the referenced line, one can locate errors within their shield's keymap by checking if the referenced keycodes were properly converted into the correct [USB HID Usage ID](https://www.usb.org/document-library/hid-usage-tables-12). + +|  | +| :-------------------------------------------------------------------------------: | +| An incorrectly defined keymap unable to compile. As shown in red, `&kp SPAC` is not a valid reference to the [USB HID Usage ID](https://www.usb.org/document-library/hid-usage-tables-12) used for "Keyboard Spacebar" | + +|  | +| :-------------------------------------------------------------------------------: | +| A properly defined keymap with successful compilation. As shown in red, the corrected keycode (`&kp SPC`) references the proper Usage ID defined in the [USB HID Usage Tables](https://www.usb.org/document-library/hid-usage-tables-12)| + +### Split Keyboard Halves Unable to Pair + +Previously, pairing split keyboard halves involved a **BLE Reset** via a combination of held keys that removed all bluetooth profile information from the keyboard. +Since then, a much simpler procedure of performing a bluetooth reset for split keyboards has come about, without the need for any file modification: + +**New Procedure:** + +1. Log into Github and download the "settings clear" UF2 image from the [latest build in Github Actions](https://github.com/zmkfirmware/zmk/actions?query=workflow%3ABuild+branch%3Amain) +1. Put each half of the split keyboard into bootloader mode +1. Flash one of the halves of the split with the "settings clear" UF2 image from step 1. Immediately after flashing "settings clear" to the chosen half, immediately put it into bootloader mode +to avoid accidental bonding between the halves. +1. Repeat step 3 with the other half of the split keyboard +1. Flash the actual image for each half of the split keyboard (e.g `my_board_left.uf2` to the left half, `my_board_right.uf2` to the right half) + +After completing these steps, pair the halves of the split keyboard together by resetting them at the same time. Most commonly, this is done by grounding the reset pins +for each of your keyboard's microcontrollers or pressing the reset buttons at the same time.
\ No newline at end of file diff --git a/docs/docs/user-setup.md b/docs/docs/user-setup.md index 2aade82..2785096 100644 --- a/docs/docs/user-setup.md +++ b/docs/docs/user-setup.md @@ -63,6 +63,7 @@ defaultValue="curl" values={[ {label: 'Using curl', value: 'curl'}, {label: 'Using wget', value: 'wget'}, +{label: 'Using PowerShell', value: 'PowerShell'}, ]}> <TabItem value="curl"> @@ -78,6 +79,12 @@ bash -c "$(wget https://zmkfirmware.dev/setup.sh -O -)" ``` </TabItem> +<TabItem value="PowerShell"> + +``` +iex ((New-Object System.Net.WebClient).DownloadString('https://zmkfirmware.dev/setup.ps1'))" +``` +</TabItem> </Tabs> ### MCU Board Selection diff --git a/docs/sidebars.js b/docs/sidebars.js index 53c5cf3..12b4a6e 100644 --- a/docs/sidebars.js +++ b/docs/sidebars.js @@ -1,6 +1,13 @@ module.exports = { someSidebar: { - "Getting Started": ["intro", "hardware", "faq", "user-setup","customization", "bond-reset"], + "Getting Started": [ + "intro", + "hardware", + "faq", + "user-setup", + "customization", + "troubleshooting" + ], Features: [ "feature/keymaps", "feature/displays", @@ -11,9 +18,12 @@ module.exports = { "behavior/key-press", "behavior/layers", "behavior/misc", + "behavior/hold-tap", "behavior/mod-tap", "behavior/reset", + "behavior/bluetooth", "behavior/lighting", + "behavior/power", ], Development: [ "dev-clean-room", @@ -23,6 +33,8 @@ module.exports = { "dev-posix-board", "dev-tests", ], - "Dev Guides": ["dev-guide-new-shield", "dev-guide-usb-logging"], + "Dev Guides": [ + "dev-guide-new-shield", + "dev-guide-usb-logging"], }, }; diff --git a/docs/static/setup.ps1 b/docs/static/setup.ps1 new file mode 100644 index 0000000..5ebd376 --- /dev/null +++ b/docs/static/setup.ps1 @@ -0,0 +1,206 @@ +# Copyright (c) 2020 The ZMK Contributors
+#
+# SPDX-License-Identifier: MIT
+
+$ErrorActionPreference = "Stop"
+
+function Get-Choice-From-Options {
+ param(
+ [String[]] $Options,
+ [String] $Prompt
+ )
+
+ while ($true) {
+ for ($i = 0; $i -lt $Options.length; $i++) {
+ Write-Host "$($i + 1)) $($Options[$i])"
+ }
+
+ Write-Host "$($Options.length + 1)) Quit"
+ $selection = Read-Host $Prompt
+
+ if ($selection -eq $Options.length + 1) {
+ Write-Host "Goodbye!"
+ exit 1
+ }
+ elseif ($selection -le $Options.length) {
+ $choice = $($selection - 1)
+ break
+ }
+ else {
+ Write-Host "Invalid Option. Try another one."
+ }
+ }
+
+ return $choice
+}
+
+function Test-Git-Config {
+ param(
+ [String] $Option,
+ [String] $ErrMsg
+ )
+
+ git config $Option | Out-Null
+
+ if ($lastExitCode -ne 0) {
+ Write-Host $ErrMsg
+ exit 1
+ }
+}
+
+try {
+ git | Out-Null
+}
+catch [System.Management.Automation.CommandNotFoundException] {
+ Write-Host "Git is not installed, and is required for this script!"
+ exit 1
+}
+
+Test-Git-Config -Option "user.name" -ErrMsg "Git username not set!`nRun: git config --global user.name 'My Name'"
+Test-Git-Config -Option "user.email" -ErrMsg "Git email not set!`nRun: git config --global user.email 'example@myemail.com'"
+
+$permission = (Get-Acl $pwd).Access |
+?{$_.IdentityReference -match $env:UserName `
+ -and $_.FileSystemRights -match "FullControl" `
+ -or $_.FileSystemRights -match "Write" } |
+
+ Select IdentityReference,FileSystemRights
+
+If (-Not $permission){
+ Write-Host "Sorry, you do not have write permissions in this directory."
+ Write-Host "Please try running this script again from a directory that you do have write permissions for."
+ exit 1
+}
+
+$repo_path = "https://github.com/zmkfirmware/zmk-config-split-template.git"
+
+$title = "ZMK Config Setup:"
+$prompt = "Pick an MCU board"
+$options = "nice!nano", "QMK Proton-C", "BlueMicro840 (v1)", "makerdiary nRF52840 M.2"
+$boards = "nice_nano", "proton_c", "bluemicro840_v1", "nrf52840_m2"
+
+Write-Host "$title"
+Write-Host ""
+Write-Host "MCU Board Selection:"
+
+$choice = Get-Choice-From-Options -Options $options -Prompt $prompt
+$board = $($boards[$choice])
+
+Write-Host ""
+Write-Host "Keyboard Shield Selection:"
+$prompt = "Pick a keyboard"
+
+# TODO: Add support for "Other" and linking to docs on adding custom shields in user config repos.
+$options = "Kyria", "Lily58", "Corne", "Splitreus62", "Sofle", "Iris", "RoMac", "RoMac+", "makerdiary M60", "Microdox", "TG4X", "QAZ"
+$names = "kyria", "lily58", "corne", "splitreus62", "sofle", "iris", "romac", "romac_plus", "m60", "microdox", "tg4x", "qaz"
+$splits = "y", "y", "y", "y", "y", "y", "n", "n", "n", "y", "n", "n"
+
+$choice = Get-Choice-From-Options -Options $options -Prompt $prompt
+$shield_title = $($options[$choice])
+$shield = $($names[$choice])
+$split = $($splits[$choice])
+
+if ($split -eq "n") {
+ $repo_path = "https://github.com/zmkfirmware/zmk-config-template.git"
+}
+
+$copy_keymap = Read-Host "Copy in the stock keymap for customisation? [Yn]"
+
+if ($copy_keymap -eq "" -or $copy_keymap -eq "Y" -or $copy_keymap -eq "y") {
+ $copy_keymap = "yes"
+}
+
+$github_user = Read-Host "GitHub Username (leave empty to skip GitHub repo creation)"
+
+if ($github_user -ne "") {
+ $repo_name = Read-Host "GitHub Repo Name [zmk-config]"
+
+ if ($repo_name -eq "") {
+ $repo_name = "zmk-config"
+ }
+
+ $github_repo = Read-Host "GitHub Repo [https://github.com/$github_user/$repo_name.git]"
+
+ if ($github_repo -eq "") {
+ $github_repo = "https://github.com/$github_user/$repo_name.git"
+ }
+}
+else {
+ $repo_name = "zmk-config"
+ $github_repo = ""
+}
+
+Write-Host ""
+Write-Host "Preparing a user config for:"
+Write-Host "* MCU Board: ${board}"
+Write-Host "* Shield: ${shield}"
+
+if ($copy_keymap -eq "yes") {
+ Write-Host "* Copy Keymap?: Yes"
+}
+else {
+ Write-Host "* Copy Keymap?: No"
+}
+
+if ($github_repo -ne "") {
+ Write-Host "* GitHub Repo to Push (please create this in GH first!): $github_repo"
+}
+
+Write-Host ""
+$do_it = Read-Host "Continue? [Yn]"
+
+if ($do_it -ne "" -and $do_it -ne "Y" -and $do_it -ne "y") {
+ Write-Host "Aborting..."
+ exit 1
+}
+
+git clone --single-branch "$repo_path" "$repo_name"
+Set-Location "$repo_name"
+
+Push-Location config
+
+Invoke-RestMethod -Uri "https://raw.githubusercontent.com/zmkfirmware/zmk/main/app/boards/shields/${shield}/${shield}.conf" -OutFile "${shield}.conf"
+
+if ($copy_keymap -eq "yes") {
+ Invoke-RestMethod -Uri "https://raw.githubusercontent.com/zmkfirmware/zmk/main/app/boards/shields/${shield}/${shield}.keymap" -OutFile "${shield}.keymap"
+}
+
+Pop-Location
+
+$build_file = (Get-Content .github/workflows/build.yml).replace("BOARD_NAME", $board)
+$build_file = $build_file.replace("SHIELD_NAME", $shield)
+$build_file = $build_file.replace("KEYBOARD_TITLE", $shield_title)
+
+if ($board -eq "proton_c") {
+ $build_file = $build_file.replace("uf2", "hex")
+}
+
+Set-Content -Path .github/workflows/build.yml -Value $build_file
+
+Remove-Item -Recurse -Force .git
+git init .
+git add .
+git commit -m "Initial User Config."
+
+if ($github_repo -ne "") {
+ git remote add origin "$github_repo"
+
+ git push --set-upstream origin $(git symbolic-ref --short HEAD)
+
+ # If push failed, assume that the origin was incorrect and give instructions on fixing.
+ if ($lastExitCode -ne 0) {
+ Write-Host "Remote repository $github_repo not found..."
+ Write-Host "Check GitHub URL, and try adding again."
+ Write-Host "Run the following: "
+ Write-Host " git remote rm origin"
+ Write-Host " git remote add origin FIXED_URL"
+ Write-Host " git push --set-upstream origin $(git symbolic-ref --short HEAD)"
+ Write-Host "Once pushed, your firmware should be availalbe from GitHub Actions at: $actions"
+ exit 1
+ }
+
+ if ($github_repo -imatch "https") {
+ $actions = "$($github_repo.substring(0, $github_repo.length - 4))/actions"
+ Write-Host "Your firmware should be availalbe from GitHub Actions shortly: $actions"
+ }
+}
diff --git a/docs/static/setup.sh b/docs/static/setup.sh index 70defdf..e45a7ed 100644 --- a/docs/static/setup.sh +++ b/docs/static/setup.sh @@ -1,49 +1,69 @@ -#!/bin/sh +#!/bin/bash + +# Copyright (c) 2020 The ZMK Contributors +# +# SPDX-License-Identifier: MIT set -e -repo_path="https://github.com/zmkfirmware/zmk-config-split-template.git" -title="ZMK Config Setup:" +check_exists() { + command_to_run=$1 + error_message=$2 + + if ! eval "$command_to_run" &> /dev/null; then + printf "%s\n" "$error_message" + exit 1 + fi +} +check_exists "command -v git" "git is not installed, and is required for this script!" +check_exists "command -v curl" "curl is not installed, and is required for this script!" -# TODO: Check for git being installed -# TODO: Check for curl being installed -# TODO: Check for user.name and user.email git configs being set +check_exists "git config user.name" "Git username not set!\nRun: git config --global user.name 'My Name'" +check_exists "git config user.email" "Git email not set!\nRun: git config --global user.email 'example@myemail.com'" + +# Check to see if the user has write permissions in this directory to prevent a cryptic error later on +if [ ! -w `pwd` ]; then + echo 'Sorry, you do not have write permissions in this directory.'; + echo 'Please try running this script again from a directory that you do have write permissions for.'; + exit 1 +fi + +repo_path="https://github.com/zmkfirmware/zmk-config-split-template.git" +title="ZMK Config Setup:" prompt="Pick an MCU board:" -options=("nice!nano" "QMK Proton-C" "BlueMicro840 (v1)") +options=("nice!nano" "QMK Proton-C" "BlueMicro840 (v1)" "makerdiary nRF52840 M.2") echo "$title" echo "" echo "MCU Board Selection:" PS3="$prompt " -select opt in "${options[@]}" "Quit"; do +select opt in "${options[@]}" "Quit"; do case "$REPLY" in 1 ) board="nice_nano"; break;; 2 ) board="proton_c"; break;; 3 ) board="bluemicro840_v1"; break;; + 3 ) board="nrf52840_m2"; break;; - $(( ${#options[@]}+1 )) ) echo "Goodbye!"; exit;; - *) echo "Invalid option. Try another one.";continue;; + $(( ${#options[@]}+1 )) ) echo "Goodbye!"; exit 1;; + *) echo "Invalid option. Try another one."; continue;; esac done -#read -p "Is this board a complete keyboard [yN]: " complete -#echo "$complete" - echo "" echo "Keyboard Shield Selection:" prompt="Pick an keyboard:" -options=("Kyria" "Lily58" "Corne" "Splitreus62" "Sofle" "Iris") +options=("Kyria" "Lily58" "Corne" "Splitreus62" "Sofle" "Iris" "RoMac" "RoMac+" "makerdiary M60" "Microdox" "TG4X" "QAZ") PS3="$prompt " # TODO: Add support for "Other" and linking to docs on adding custom shields in user config repos. -# select opt in "${options[@]}" "Other" "Quit"; do -select opt in "${options[@]}" "Quit"; do +# select opt in "${options[@]}" "Other" "Quit"; do +select opt in "${options[@]}" "Quit"; do case "$REPLY" in @@ -53,50 +73,62 @@ select opt in "${options[@]}" "Quit"; do 4 ) shield_title="Splitreus62" shield="splitreus62"; split="y"; break;; 5 ) shield_title="Sofle" shield="sofle"; split="y"; break;; 6 ) shield_title="Iris" shield="iris"; split="y"; break;; + 7 ) shield_title="RoMac" shield="romac"; split="n"; break;; + 8 ) shield_title="RoMac+" shield="romac_plus"; split="n"; break;; + 9 ) shield_title="M60" shield="m60"; split="n"; break;; + 10 ) shield_title="Microdox" shield="microdox"; split="y"; break;; + 11 ) shield_title="TG4X" shield="tg4x"; split="n"; break;; + 12 ) shield_title="QAZ" shield="qaz"; split="n"; break;; # Add link to docs on adding your own custom shield in your ZMK config! - # $(( ${#options[@]}+1 )) ) echo "Other!"; break;; - $(( ${#options[@]}+1 )) ) echo "Goodbye!"; exit;; + # $(( ${#options[@]}+1 )) ) echo "Other!"; break;; + $(( ${#options[@]}+1 )) ) echo "Goodbye!"; exit 1;; *) echo "Invalid option. Try another one.";continue;; esac done -read -e -p "Copy in the stock keymap for customization? [Yn]: " copy_keymap +if [ "$split" == "n" ]; then + repo_path="https://github.com/zmkfirmware/zmk-config-template.git" +fi + +read -r -e -p "Copy in the stock keymap for customization? [Yn]: " copy_keymap if [ -z "$copy_keymap" ] || [ "$copy_keymap" == "Y" ] || [ "$copy_keymap" == "y" ]; then copy_keymap="yes"; fi -read -e -p "GitHub Username (leave empty to skip GitHub repo creation): " github_user +read -r -e -p "GitHub Username (leave empty to skip GitHub repo creation): " github_user if [ -n "$github_user" ]; then - read -p "GitHub Repo Name [zmk-config]: " repo_name - if [ -z "$repo_name" ]; then repo_name="zmk-config"; fi + read -r -p "GitHub Repo Name [zmk-config]: " repo_name + if [ -z "$repo_name" ]; then repo_name="zmk-config"; fi - read -p "GitHub Repo [https://github.com/${github_user}/${repo_name}.git]: " github_repo + read -r -p "GitHub Repo [https://github.com/${github_user}/${repo_name}.git]: " github_repo - if [ -z "$github_repo" ]; then github_repo="https://github.com/${github_user}/${repo_name}.git"; fi + if [ -z "$github_repo" ]; then github_repo="https://github.com/${github_user}/${repo_name}.git"; fi else - repo_name="zmk-config" + repo_name="zmk-config" fi echo "" echo "Preparing a user config for:" echo "* MCU Board: ${board}" echo "* Shield: ${shield}" + if [ "$copy_keymap" == "yes" ]; then echo "* Copy Keymap?: ✓" else echo "* Copy Keymap?: ❌" fi + if [ -n "$github_repo" ]; then - echo "* GitHub Repo To Push (please create this in GH first!): ${github_repo}" + echo "* GitHub Repo To Push (please create this in GH first!): ${github_repo}" fi echo "" -read -p "Continue? [Yn]: " do_it +read -r -p "Continue? [Yn]: " do_it if [ -n "$do_it" ] && [ "$do_it" != "y" ] && [ "$do_it" != "Y" ]; then - echo "Aborting..." - exit + echo "Aborting..." + exit 1 fi git clone --single-branch $repo_path ${repo_name} @@ -113,10 +145,10 @@ fi popd sed -i'.orig' \ - -e "s/BOARD_NAME/$board/" \ - -e "s/SHIELD_NAME/$shield/" \ - -e "s/KEYBOARD_TITLE/$shield_title/" \ - .github/workflows/build.yml + -e "s/BOARD_NAME/$board/" \ + -e "s/SHIELD_NAME/$shield/" \ + -e "s/KEYBOARD_TITLE/$shield_title/" \ + .github/workflows/build.yml if [ "$board" == "proton_c" ]; then # Proton-C board still fa @@ -131,11 +163,24 @@ git add . git commit -m "Initial User Config." if [ -n "$github_repo" ]; then - git remote add origin "$github_repo" - git push --set-upstream origin $(git symbolic-ref --short HEAD) + git remote add origin "$github_repo" + git push --set-upstream origin "$(git symbolic-ref --short HEAD)" + push_return_code=$? + + # If push failed, assume that the origin was incorrect and give instructions on fixing. + if [ ${push_return_code} -ne 0 ]; then + echo "Remote repository $github_repo not found..." + echo "Check GitHub URL, and try adding again." + echo "Run the following: " + echo " git remote rm origin" + echo " git remote add origin FIXED_URL" + echo " git push --set-upstream origin $(git symbolic-ref --short HEAD)" + echo "Once pushed, your firmware should be availalbe from GitHub Actions at: ${github_repo%.git}/actions" + exit 1 + fi # TODO: Support determing the actions URL when non-https:// repo URL is used. if [ "${github_repo}" != "${github_repo#https://}" ]; then - echo "Your firmware should be available from the GitHub Actions shortly: ${github_url%.git}/actions" + echo "Your firmware should be available from GitHub Actions shortly: ${github_repo%.git}/actions" fi fi |