From 98524a95671fe9d819a65d1515f9bc37895efc3d Mon Sep 17 00:00:00 2001 From: kadoyau <11990327+kadoyau@users.noreply.github.com> Date: Sun, 11 Jun 2023 04:32:50 +0900 Subject: [PATCH 01/17] fix(docs): Fix INT6 keycode description --- docs/src/data/hid.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/src/data/hid.js b/docs/src/data/hid.js index 00d48f3f..45767172 100644 --- a/docs/src/data/hid.js +++ b/docs/src/data/hid.js @@ -3477,7 +3477,7 @@ export default [ }, { names: ["INTERNATIONAL_6", "INT6", "INT_KPJPCOMMA"], - description: ", [カソマ] (International 6)", + description: ", [カンマ] (International 6)", context: "Keyboard", clarify: false, usages: [ From 9d39a87f67668b4c083e09b04adf006d6fd1385d Mon Sep 17 00:00:00 2001 From: pixls Date: Sat, 10 Jun 2023 20:49:30 -0400 Subject: [PATCH 02/17] fix(docs): Change user-setup.md order to agree with order in setup script Fixes #1281 Co-authored-by: Cem Aksoylar --- docs/docs/user-setup.md | 47 +++++++++++++++++++++++------------------ 1 file changed, 26 insertions(+), 21 deletions(-) diff --git a/docs/docs/user-setup.md b/docs/docs/user-setup.md index a2e491b8..8faa72df 100644 --- a/docs/docs/user-setup.md +++ b/docs/docs/user-setup.md @@ -88,36 +88,41 @@ powershell -Command "iex ((New-Object System.Net.WebClient).DownloadString('http +### Keyboard Selection + +When prompted, enter the number for the corresponding keyboard you would like to target: + +``` +Keyboard Selection: + 1) 2% Milk 19) Ferris 0.2 37) Nibble + 2) A. Dux 20) Fourier Rev. 1 38) nice!60 + 3) BAT43 21) Helix 39) Osprette + 4) BDN9 Rev2 22) Hummingbird 40) Pancake + 5) BFO-9000 23) Iris 41) Planck Rev6 + 6) Boardsource 3x4 Macropad 24) etc... +Pick an keyboard: +``` + +:::note For a keyboard not in the included list: +If you are building firmware for a new keyboard that is not included in the built-in +list of keyboards, you can choose any keyboard from the list that is similar to yours (e.g. in terms of unibody/split and [onboard controller](hardware.mdx#onboard)/[composite](hardware.mdx#composite)) to generate the repository, +and edit / add necessary files. You can follow the [new shield guide](development/new-shield.md) if you are adding support for a composite keyboard. +::: + ### MCU Board Selection +If the keyboard selected uses an onboard controller you will skip this step. When prompted, enter the number for the corresponding MCU board you would like to target: ``` MCU Board Selection: -1) nice!nano -2) QMK Proton-C -3) Quit +1) BlueMicro840 v1 5) nRF52840 M.2 Module 9) QMK Proton-C +2) Mikoto 5.20 6) nRFMicro 1.1 (flipped) 10) Seeeduino XIAO +3) nice!nano v1 7) nRFMicro 1.1/1.2 11) Seeeduino XIAO BLE +4) nice!nano v2 8) nRFMicro 1.3/1.4 12) Quit Pick an MCU board: ``` -### Keyboard Shield Selection - -:::note -If you are building firmware for a new keyboard shield that is not included in the built-in -list of shields, you can choose any shield from the list that is similar to yours to generate the repository, -and edit / add necessary files according to the [guide for adding new keyboard shield](development/new-shield.md). -::: - -When prompted, enter the number for the corresponding keyboard shield you would like to target: - -``` -Keyboard Shield Selection: -1) Kyria -2) Lily58 -3) Quit -Pick an keyboard: -``` - ### Keymap Customization At the next prompt, you have an opportunity to decide if you want the stock keymap file copied in From 0be0d0763081de0c163dd26c29752d1085381dbb Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Sun, 11 Jun 2023 04:35:54 +0000 Subject: [PATCH 03/17] chore(deps): bump minimatch and serve-handler in /docs Bumps [minimatch](https://github.com/isaacs/minimatch) and [serve-handler](https://github.com/zeit/serve-handler). These dependencies needed to be updated together. Updates `minimatch` from 3.0.4 to 3.1.2 - [Release notes](https://github.com/isaacs/minimatch/releases) - [Commits](https://github.com/isaacs/minimatch/compare/v3.0.4...v3.1.2) Updates `serve-handler` from 6.1.3 to 6.1.5 - [Release notes](https://github.com/zeit/serve-handler/releases) - [Commits](https://github.com/zeit/serve-handler/compare/6.1.3...6.1.5) --- updated-dependencies: - dependency-name: minimatch dependency-type: indirect - dependency-name: serve-handler dependency-type: indirect ... Signed-off-by: dependabot[bot] --- docs/package-lock.json | 35 ++++++++--------------------------- 1 file changed, 8 insertions(+), 27 deletions(-) diff --git a/docs/package-lock.json b/docs/package-lock.json index 3183a51c..f0cd1fc8 100644 --- a/docs/package-lock.json +++ b/docs/package-lock.json @@ -13612,31 +13612,20 @@ } }, "node_modules/serve-handler": { - "version": "6.1.3", - "resolved": "https://registry.npmjs.org/serve-handler/-/serve-handler-6.1.3.tgz", - "integrity": "sha512-FosMqFBNrLyeiIDvP1zgO6YoTzFYHxLDEIavhlmQ+knB2Z7l1t+kGLHkZIDN7UVWqQAmKI3D20A6F6jo3nDd4w==", + "version": "6.1.5", + "resolved": "https://registry.npmjs.org/serve-handler/-/serve-handler-6.1.5.tgz", + "integrity": "sha512-ijPFle6Hwe8zfmBxJdE+5fta53fdIY0lHISJvuikXB3VYFafRjMRpOffSPvCYsbKyBA7pvy9oYr/BT1O3EArlg==", "dependencies": { "bytes": "3.0.0", "content-disposition": "0.5.2", "fast-url-parser": "1.1.3", "mime-types": "2.1.18", - "minimatch": "3.0.4", + "minimatch": "3.1.2", "path-is-inside": "1.0.2", "path-to-regexp": "2.2.1", "range-parser": "1.2.0" } }, - "node_modules/serve-handler/node_modules/minimatch": { - "version": "3.0.4", - "resolved": "https://registry.npmjs.org/minimatch/-/minimatch-3.0.4.tgz", - "integrity": "sha512-yJHVQEhyqPLUTgt9B83PXu6W3rx4MvvHvSUvToogpwoGDOUQ+yDrR0HRot+yOCdCO7u4hX3pWft6kWBBcqh0UA==", - "dependencies": { - "brace-expansion": "^1.1.7" - }, - "engines": { - "node": "*" - } - }, "node_modules/serve-handler/node_modules/path-to-regexp": { "version": "2.2.1", "resolved": "https://registry.npmjs.org/path-to-regexp/-/path-to-regexp-2.2.1.tgz", @@ -25765,28 +25754,20 @@ } }, "serve-handler": { - "version": "6.1.3", - "resolved": "https://registry.npmjs.org/serve-handler/-/serve-handler-6.1.3.tgz", - "integrity": "sha512-FosMqFBNrLyeiIDvP1zgO6YoTzFYHxLDEIavhlmQ+knB2Z7l1t+kGLHkZIDN7UVWqQAmKI3D20A6F6jo3nDd4w==", + "version": "6.1.5", + "resolved": "https://registry.npmjs.org/serve-handler/-/serve-handler-6.1.5.tgz", + "integrity": "sha512-ijPFle6Hwe8zfmBxJdE+5fta53fdIY0lHISJvuikXB3VYFafRjMRpOffSPvCYsbKyBA7pvy9oYr/BT1O3EArlg==", "requires": { "bytes": "3.0.0", "content-disposition": "0.5.2", "fast-url-parser": "1.1.3", "mime-types": "2.1.18", - "minimatch": "3.0.4", + "minimatch": "3.1.2", "path-is-inside": "1.0.2", "path-to-regexp": "2.2.1", "range-parser": "1.2.0" }, "dependencies": { - "minimatch": { - "version": "3.0.4", - "resolved": "https://registry.npmjs.org/minimatch/-/minimatch-3.0.4.tgz", - "integrity": "sha512-yJHVQEhyqPLUTgt9B83PXu6W3rx4MvvHvSUvToogpwoGDOUQ+yDrR0HRot+yOCdCO7u4hX3pWft6kWBBcqh0UA==", - "requires": { - "brace-expansion": "^1.1.7" - } - }, "path-to-regexp": { "version": "2.2.1", "resolved": "https://registry.npmjs.org/path-to-regexp/-/path-to-regexp-2.2.1.tgz", From 9d714c0b69fee2098a010d29e534051aeca26386 Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Sun, 11 Jun 2023 06:47:34 +0000 Subject: [PATCH 04/17] chore(deps-dev): bump webpack from 5.80.0 to 5.86.0 in /docs Bumps [webpack](https://github.com/webpack/webpack) from 5.80.0 to 5.86.0. - [Release notes](https://github.com/webpack/webpack/releases) - [Commits](https://github.com/webpack/webpack/compare/v5.80.0...v5.86.0) --- updated-dependencies: - dependency-name: webpack dependency-type: direct:development update-type: version-update:semver-minor ... Signed-off-by: dependabot[bot] --- docs/package-lock.json | 46 +++++++++++++++++++++--------------------- docs/package.json | 2 +- 2 files changed, 24 insertions(+), 24 deletions(-) diff --git a/docs/package-lock.json b/docs/package-lock.json index f0cd1fc8..5badb07e 100644 --- a/docs/package-lock.json +++ b/docs/package-lock.json @@ -42,7 +42,7 @@ "prettier": "^2.8.7", "string-replace-loader": "^3.1.0", "typescript": "^5.0.4", - "webpack": "^5.80.0" + "webpack": "^5.86.0" } }, "node_modules/@algolia/autocomplete-core": { @@ -3950,9 +3950,9 @@ } }, "node_modules/acorn-import-assertions": { - "version": "1.8.0", - "resolved": "https://registry.npmjs.org/acorn-import-assertions/-/acorn-import-assertions-1.8.0.tgz", - "integrity": "sha512-m7VZ3jwz4eK6A4Vtt8Ew1/mNbP24u0FhdyfA7fSvnJR6LMdfOYnmuIrrJAgrYfYJ10F/otaHTtrtrtmHdMNzEw==", + "version": "1.9.0", + "resolved": "https://registry.npmjs.org/acorn-import-assertions/-/acorn-import-assertions-1.9.0.tgz", + "integrity": "sha512-cmMwop9x+8KFhxvKrKfPYmN6/pKTYYHBqLa0DfvVZcKMJWNyWLnaqND7dx/qn66R7ewM1UX5XMaDVP5wlVTaVA==", "peerDependencies": { "acorn": "^8" } @@ -6069,9 +6069,9 @@ } }, "node_modules/enhanced-resolve": { - "version": "5.13.0", - "resolved": "https://registry.npmjs.org/enhanced-resolve/-/enhanced-resolve-5.13.0.tgz", - "integrity": "sha512-eyV8f0y1+bzyfh8xAwW/WTSZpLbjhqc4ne9eGSH4Zo2ejdyiNG9pU6mf9DG8a7+Auk6MFTlNOT4Y2y/9k8GKVg==", + "version": "5.14.1", + "resolved": "https://registry.npmjs.org/enhanced-resolve/-/enhanced-resolve-5.14.1.tgz", + "integrity": "sha512-Vklwq2vDKtl0y/vtwjSesgJ5MYS7Etuk5txS8VdKL4AOS1aUlD96zqIfsOSLQsdv3xgMRbtkWM8eG9XDfKUPow==", "dependencies": { "graceful-fs": "^4.2.4", "tapable": "^2.2.0" @@ -15390,9 +15390,9 @@ "integrity": "sha512-2JAn3z8AR6rjK8Sm8orRC0h/bcl/DqL7tRPdGZ4I1CjdF+EaMLmYxBHyXuKL849eucPFhvBoxMsflfOb8kxaeQ==" }, "node_modules/webpack": { - "version": "5.80.0", - "resolved": "https://registry.npmjs.org/webpack/-/webpack-5.80.0.tgz", - "integrity": "sha512-OIMiq37XK1rWO8mH9ssfFKZsXg4n6klTEDL7S8/HqbAOBBaiy8ABvXvz0dDCXeEF9gqwxSvVk611zFPjS8hJxA==", + "version": "5.86.0", + "resolved": "https://registry.npmjs.org/webpack/-/webpack-5.86.0.tgz", + "integrity": "sha512-3BOvworZ8SO/D4GVP+GoRC3fVeg5MO4vzmq8TJJEkdmopxyazGDxN8ClqN12uzrZW9Tv8EED8v5VSb6Sqyi0pg==", "dependencies": { "@types/eslint-scope": "^3.7.3", "@types/estree": "^1.0.0", @@ -15400,10 +15400,10 @@ "@webassemblyjs/wasm-edit": "^1.11.5", "@webassemblyjs/wasm-parser": "^1.11.5", "acorn": "^8.7.1", - "acorn-import-assertions": "^1.7.6", + "acorn-import-assertions": "^1.9.0", "browserslist": "^4.14.5", "chrome-trace-event": "^1.0.2", - "enhanced-resolve": "^5.13.0", + "enhanced-resolve": "^5.14.1", "es-module-lexer": "^1.2.1", "eslint-scope": "5.1.1", "events": "^3.2.0", @@ -18899,9 +18899,9 @@ "integrity": "sha512-7zFpHzhnqYKrkYdUjF1HI1bzd0VygEGX8lFk4k5zVMqHEoES+P+7TKI+EvLO9WVMJ8eekdO0aDEK044xTXwPPA==" }, "acorn-import-assertions": { - "version": "1.8.0", - "resolved": "https://registry.npmjs.org/acorn-import-assertions/-/acorn-import-assertions-1.8.0.tgz", - "integrity": "sha512-m7VZ3jwz4eK6A4Vtt8Ew1/mNbP24u0FhdyfA7fSvnJR6LMdfOYnmuIrrJAgrYfYJ10F/otaHTtrtrtmHdMNzEw==", + "version": "1.9.0", + "resolved": "https://registry.npmjs.org/acorn-import-assertions/-/acorn-import-assertions-1.9.0.tgz", + "integrity": "sha512-cmMwop9x+8KFhxvKrKfPYmN6/pKTYYHBqLa0DfvVZcKMJWNyWLnaqND7dx/qn66R7ewM1UX5XMaDVP5wlVTaVA==", "requires": {} }, "acorn-jsx": { @@ -20426,9 +20426,9 @@ } }, "enhanced-resolve": { - "version": "5.13.0", - "resolved": "https://registry.npmjs.org/enhanced-resolve/-/enhanced-resolve-5.13.0.tgz", - "integrity": "sha512-eyV8f0y1+bzyfh8xAwW/WTSZpLbjhqc4ne9eGSH4Zo2ejdyiNG9pU6mf9DG8a7+Auk6MFTlNOT4Y2y/9k8GKVg==", + "version": "5.14.1", + "resolved": "https://registry.npmjs.org/enhanced-resolve/-/enhanced-resolve-5.14.1.tgz", + "integrity": "sha512-Vklwq2vDKtl0y/vtwjSesgJ5MYS7Etuk5txS8VdKL4AOS1aUlD96zqIfsOSLQsdv3xgMRbtkWM8eG9XDfKUPow==", "requires": { "graceful-fs": "^4.2.4", "tapable": "^2.2.0" @@ -27031,9 +27031,9 @@ "integrity": "sha512-2JAn3z8AR6rjK8Sm8orRC0h/bcl/DqL7tRPdGZ4I1CjdF+EaMLmYxBHyXuKL849eucPFhvBoxMsflfOb8kxaeQ==" }, "webpack": { - "version": "5.80.0", - "resolved": "https://registry.npmjs.org/webpack/-/webpack-5.80.0.tgz", - "integrity": "sha512-OIMiq37XK1rWO8mH9ssfFKZsXg4n6klTEDL7S8/HqbAOBBaiy8ABvXvz0dDCXeEF9gqwxSvVk611zFPjS8hJxA==", + "version": "5.86.0", + "resolved": "https://registry.npmjs.org/webpack/-/webpack-5.86.0.tgz", + "integrity": "sha512-3BOvworZ8SO/D4GVP+GoRC3fVeg5MO4vzmq8TJJEkdmopxyazGDxN8ClqN12uzrZW9Tv8EED8v5VSb6Sqyi0pg==", "requires": { "@types/eslint-scope": "^3.7.3", "@types/estree": "^1.0.0", @@ -27041,10 +27041,10 @@ "@webassemblyjs/wasm-edit": "^1.11.5", "@webassemblyjs/wasm-parser": "^1.11.5", "acorn": "^8.7.1", - "acorn-import-assertions": "^1.7.6", + "acorn-import-assertions": "^1.9.0", "browserslist": "^4.14.5", "chrome-trace-event": "^1.0.2", - "enhanced-resolve": "^5.13.0", + "enhanced-resolve": "^5.14.1", "es-module-lexer": "^1.2.1", "eslint-scope": "5.1.1", "events": "^3.2.0", diff --git a/docs/package.json b/docs/package.json index 15d5aa38..d82e54ec 100644 --- a/docs/package.json +++ b/docs/package.json @@ -61,6 +61,6 @@ "prettier": "^2.8.7", "string-replace-loader": "^3.1.0", "typescript": "^5.0.4", - "webpack": "^5.80.0" + "webpack": "^5.86.0" } } From 9ff1eaeb5a5f892d17ca87c38697466fd48fc301 Mon Sep 17 00:00:00 2001 From: Joel Spadin Date: Sun, 14 May 2023 11:44:49 -0500 Subject: [PATCH 05/17] fix: Enable BT_TINYCRYPT_ECC when using HCI BT_TINYCRYPT_ECC is required for BT_SMP_SC_PAIR_ONLY to work on setups that use BT HCI like the nRF5340. --- app/Kconfig | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/app/Kconfig b/app/Kconfig index 1537bd61..18ee473d 100644 --- a/app/Kconfig +++ b/app/Kconfig @@ -120,6 +120,10 @@ menuconfig ZMK_BLE if ZMK_BLE +# BT_TINYCRYPT_ECC is required for BT_SMP_SC_PAIR_ONLY when using HCI +config BT_TINYCRYPT_ECC + default y if BT_HCI && !BT_CTLR + choice BT_LL_SW_LLCP_IMPL default BT_LL_SW_LLCP_LEGACY From dcf5e75fa674184d9615793882db1c3f4c4194b9 Mon Sep 17 00:00:00 2001 From: Joel Spadin Date: Sun, 14 May 2023 13:08:33 -0500 Subject: [PATCH 06/17] fix(boards): Bump nRF5340 DK I2C buffer size Increased the I2C buffer size again, since it needs to be at least 641 to support 128x64 displays. --- app/boards/nrf5340dk_nrf5340_cpuapp.overlay | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/boards/nrf5340dk_nrf5340_cpuapp.overlay b/app/boards/nrf5340dk_nrf5340_cpuapp.overlay index 66d2332f..9427d9ca 100644 --- a/app/boards/nrf5340dk_nrf5340_cpuapp.overlay +++ b/app/boards/nrf5340dk_nrf5340_cpuapp.overlay @@ -6,5 +6,5 @@ &arduino_i2c { // Default buffer size is too small for use with displays. - zephyr,concat-buf-size = <512>; + zephyr,concat-buf-size = <1024>; }; From 2244bd3d81931753dfd170e804a90a487a205aa1 Mon Sep 17 00:00:00 2001 From: Peter Johanson Date: Wed, 1 Sep 2021 03:49:18 +0000 Subject: [PATCH 07/17] refactor(sensors): Sensor event channel data, resolution tweaks. * Refactor sensor events to include channel data, necessary for prop split encoders, and avoiding duplicate calls, to fetch channel data twice, etc. * More consistent behavior driver API. * Allow setting triggers per resolution at the behavior level optionally. --- app/Kconfig | 20 +++ app/dts/bindings/zmk,keymap-sensors.yaml | 12 +- app/include/drivers/behavior.h | 26 ++-- app/include/zmk/events/sensor_event.h | 17 ++- app/include/zmk/sensors.h | 14 ++ app/include/zmk/virtual_key_position.h | 5 + app/src/behaviors/behavior_sensor_rotate.c | 8 +- .../behaviors/behavior_sensor_rotate_common.c | 59 +++++--- .../behaviors/behavior_sensor_rotate_common.h | 16 ++- .../behaviors/behavior_sensor_rotate_var.c | 6 +- app/src/keymap.c | 25 ++-- app/src/sensors.c | 133 +++++++++++++----- 12 files changed, 258 insertions(+), 83 deletions(-) diff --git a/app/Kconfig b/app/Kconfig index 18ee473d..1766bf07 100644 --- a/app/Kconfig +++ b/app/Kconfig @@ -194,6 +194,18 @@ rsource "src/split/Kconfig" #Basic Keyboard Setup endmenu +menu "Encoders" + +config ZMK_ENCODERS_DEFAULT_TRIGGERS_PER_ROTATION + int "Default behavior triggers per rotation" + help + Unless overridden for a specific behavior in the keymap/devicetree, this value + determines how many times to trigger the bound behavior per full rotation. + For tactile encoders with detents, this usually should match the number of + detents per rotation of the encoder. + default 30 + +endmenu menu "Display/LED Options" rsource "src/display/Kconfig" @@ -523,6 +535,14 @@ config ZMK_WPM config SENSOR default y +if ZMK_KEYMAP_SENSORS + +config ZMK_KEYMAP_SENSORS_DEFAULT_TRIGGERS_PER_ROTATION + int "Default triggers per rotation" + default 20 + +endif # ZMK_KEYMAP_SENSORS + choice CBPRINTF_IMPLEMENTATION default CBPRINTF_NANO diff --git a/app/dts/bindings/zmk,keymap-sensors.yaml b/app/dts/bindings/zmk,keymap-sensors.yaml index a879684f..5282f25b 100644 --- a/app/dts/bindings/zmk,keymap-sensors.yaml +++ b/app/dts/bindings/zmk,keymap-sensors.yaml @@ -9,4 +9,14 @@ compatible: "zmk,keymap-sensors" properties: sensors: type: phandles - required: true + required: false + triggers-per-rotation: + type: int + required: false + +child-binding: + description: Per-sensor configuration settings + properties: + triggers-per-rotation: + type: int + required: false diff --git a/app/include/drivers/behavior.h b/app/include/drivers/behavior.h index 380fc76f..0aa5d85e 100644 --- a/app/include/drivers/behavior.h +++ b/app/include/drivers/behavior.h @@ -12,6 +12,7 @@ #include #include #include +#include #include /** @@ -24,9 +25,10 @@ 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, - const struct device *sensor, - struct zmk_behavior_binding_event event); +typedef int (*behavior_sensor_keymap_binding_callback_t)( + struct zmk_behavior_binding *binding, struct zmk_behavior_binding_event event, + const struct zmk_sensor_config *sensor_config, size_t channel_data_size, + const struct zmk_sensor_channel_data channel_data[channel_data_size]); enum behavior_locality { BEHAVIOR_LOCALITY_CENTRAL, @@ -158,14 +160,15 @@ static inline int z_impl_behavior_keymap_binding_released(struct zmk_behavior_bi * @retval 0 If successful. * @retval Negative errno code if failure. */ -__syscall int behavior_sensor_keymap_binding_triggered(struct zmk_behavior_binding *binding, - const struct device *sensor, - struct zmk_behavior_binding_event event); +__syscall int behavior_sensor_keymap_binding_triggered( + struct zmk_behavior_binding *binding, struct zmk_behavior_binding_event event, + const struct zmk_sensor_config *sensor_config, size_t channel_data_size, + const struct zmk_sensor_channel_data *channel_data); -static inline int -z_impl_behavior_sensor_keymap_binding_triggered(struct zmk_behavior_binding *binding, - const struct device *sensor, - struct zmk_behavior_binding_event event) { +static inline int z_impl_behavior_sensor_keymap_binding_triggered( + struct zmk_behavior_binding *binding, struct zmk_behavior_binding_event event, + const struct zmk_sensor_config *sensor_config, size_t channel_data_size, + const struct zmk_sensor_channel_data *channel_data) { const struct device *dev = device_get_binding(binding->behavior_dev); if (dev == NULL) { @@ -178,7 +181,8 @@ z_impl_behavior_sensor_keymap_binding_triggered(struct zmk_behavior_binding *bin return -ENOTSUP; } - return api->sensor_binding_triggered(binding, sensor, event); + return api->sensor_binding_triggered(binding, event, sensor_config, channel_data_size, + channel_data); } /** diff --git a/app/include/zmk/events/sensor_event.h b/app/include/zmk/events/sensor_event.h index 9398bcbb..5a5aa3ac 100644 --- a/app/include/zmk/events/sensor_event.h +++ b/app/include/zmk/events/sensor_event.h @@ -6,12 +6,21 @@ #pragma once -#include + +#include #include -#include +#include +#include + +// TODO: Move to Kconfig when we need more than one channel +#define ZMK_SENSOR_EVENT_MAX_CHANNELS 1 + struct zmk_sensor_event { - uint8_t sensor_number; - const struct device *sensor; + uint8_t sensor_position; + + size_t channel_data_size; + struct zmk_sensor_channel_data channel_data[ZMK_SENSOR_EVENT_MAX_CHANNELS]; + int64_t timestamp; }; diff --git a/app/include/zmk/sensors.h b/app/include/zmk/sensors.h index 9e54695f..41061127 100644 --- a/app/include/zmk/sensors.h +++ b/app/include/zmk/sensors.h @@ -6,6 +6,9 @@ #pragma once +#include + +#define _SENSOR_CHILD_LEN(node) 1 + #define ZMK_KEYMAP_SENSORS_NODE DT_INST(0, zmk_keymap_sensors) #define ZMK_KEYMAP_HAS_SENSORS DT_NODE_HAS_STATUS(ZMK_KEYMAP_SENSORS_NODE, okay) #define ZMK_KEYMAP_SENSORS_BY_IDX(idx) DT_PHANDLE_BY_IDX(ZMK_KEYMAP_SENSORS_NODE, sensors, idx) @@ -15,3 +18,14 @@ #else #define ZMK_KEYMAP_SENSORS_LEN 0 #endif + +const struct zmk_sensor_config *zmk_sensors_get_config_at_position(uint8_t sensor_position); + +struct zmk_sensor_config { + uint16_t triggers_per_rotation; +}; + +struct zmk_sensor_channel_data { + enum sensor_channel channel; + struct sensor_value value; +}; diff --git a/app/include/zmk/virtual_key_position.h b/app/include/zmk/virtual_key_position.h index 48deee5c..b8f20683 100644 --- a/app/include/zmk/virtual_key_position.h +++ b/app/include/zmk/virtual_key_position.h @@ -14,6 +14,11 @@ */ #define ZMK_VIRTUAL_KEY_POSITION_SENSOR(index) (ZMK_KEYMAP_LEN + (index)) +/** + * Gets the sensor number from the virtual key position. + */ +#define ZMK_SENSOR_POSITION_FROM_VIRTUAL_KEY_POSITION(vkp) ((vkp)-ZMK_KEYMAP_LEN) + /** * Gets the virtual key position to use for the combo with the given index. */ diff --git a/app/src/behaviors/behavior_sensor_rotate.c b/app/src/behaviors/behavior_sensor_rotate.c index e12278bb..86846d5b 100644 --- a/app/src/behaviors/behavior_sensor_rotate.c +++ b/app/src/behaviors/behavior_sensor_rotate.c @@ -33,8 +33,10 @@ static int behavior_sensor_rotate_init(const struct device *dev) { return 0; }; .tap_ms = DT_INST_PROP_OR(n, tap_ms, 5), \ .override_params = false, \ }; \ - DEVICE_DT_INST_DEFINE( \ - n, behavior_sensor_rotate_init, NULL, NULL, &behavior_sensor_rotate_config_##n, \ - APPLICATION, CONFIG_KERNEL_INIT_PRIORITY_DEFAULT, &behavior_sensor_rotate_driver_api); + static struct behavior_sensor_rotate_data behavior_sensor_rotate_data_##n = {}; \ + DEVICE_DT_INST_DEFINE(n, behavior_sensor_rotate_init, NULL, &behavior_sensor_rotate_data_##n, \ + &behavior_sensor_rotate_config_##n, APPLICATION, \ + CONFIG_KERNEL_INIT_PRIORITY_DEFAULT, \ + &behavior_sensor_rotate_driver_api); DT_INST_FOREACH_STATUS_OKAY(SENSOR_ROTATE_INST) diff --git a/app/src/behaviors/behavior_sensor_rotate_common.c b/app/src/behaviors/behavior_sensor_rotate_common.c index bd31170e..99e4e019 100644 --- a/app/src/behaviors/behavior_sensor_rotate_common.c +++ b/app/src/behaviors/behavior_sensor_rotate_common.c @@ -5,48 +5,75 @@ #include #include +#include #include "behavior_sensor_rotate_common.h" LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL); int zmk_behavior_sensor_rotate_common_trigger(struct zmk_behavior_binding *binding, - const struct device *sensor, - struct zmk_behavior_binding_event event) { + struct zmk_behavior_binding_event event, + const struct zmk_sensor_config *sensor_config, + size_t channel_data_size, + const struct zmk_sensor_channel_data *channel_data) { const struct device *dev = device_get_binding(binding->behavior_dev); const struct behavior_sensor_rotate_config *cfg = dev->config; + struct behavior_sensor_rotate_data *data = dev->data; - struct sensor_value value; + const struct sensor_value value = channel_data[0].value; + int triggers; + int sensor_position = ZMK_SENSOR_POSITION_FROM_VIRTUAL_KEY_POSITION(event.position); - const int err = sensor_channel_get(sensor, SENSOR_CHAN_ROTATION, &value); + // Some funky special casing for "old encoder behavior" where ticks where reported in val2 only, + // instead of rotational degrees in val1. + // REMOVE ME: Remove after a grace period of old ec11 sensor behavior + if (value.val1 == 0) { + triggers = value.val2; + } else { + struct sensor_value remainder = data->remainder[sensor_position]; - if (err < 0) { - LOG_WRN("Failed to get sensor rotation value: %d", err); - return err; + remainder.val1 += value.val1; + remainder.val2 += value.val2; + + if (remainder.val2 >= 1000000 || remainder.val2 <= 1000000) { + remainder.val1 += remainder.val2 / 1000000; + remainder.val2 %= 1000000; + } + + int trigger_degrees = 360 / sensor_config->triggers_per_rotation; + triggers = remainder.val1 / trigger_degrees; + remainder.val1 %= trigger_degrees; + + data->remainder[sensor_position] = remainder; } + LOG_DBG( + "val1: %d, val2: %d, remainder: %d/%d triggers: %d inc keycode 0x%02X dec keycode 0x%02X", + value.val1, value.val2, data->remainder[sensor_position].val1, + data->remainder[sensor_position].val2, triggers, binding->param1, binding->param2); + struct zmk_behavior_binding triggered_binding; - switch (value.val1) { - case 1: + if (triggers > 0) { triggered_binding = cfg->cw_binding; if (cfg->override_params) { triggered_binding.param1 = binding->param1; } - break; - case -1: + } else if (triggers < 0) { + triggers = -triggers; triggered_binding = cfg->ccw_binding; if (cfg->override_params) { triggered_binding.param1 = binding->param2; } - break; - default: - return -ENOTSUP; + } else { + return 0; } LOG_DBG("Sensor binding: %s", binding->behavior_dev); - zmk_behavior_queue_add(event.position, triggered_binding, true, cfg->tap_ms); - zmk_behavior_queue_add(event.position, triggered_binding, false, 0); + for (int i = 0; i < triggers; i++) { + zmk_behavior_queue_add(event.position, triggered_binding, true, cfg->tap_ms); + zmk_behavior_queue_add(event.position, triggered_binding, false, 0); + } return ZMK_BEHAVIOR_OPAQUE; } diff --git a/app/src/behaviors/behavior_sensor_rotate_common.h b/app/src/behaviors/behavior_sensor_rotate_common.h index 2d58218d..eab443a3 100644 --- a/app/src/behaviors/behavior_sensor_rotate_common.h +++ b/app/src/behaviors/behavior_sensor_rotate_common.h @@ -1,5 +1,11 @@ +/* + * Copyright (c) 2023 The ZMK Contributors + * + * SPDX-License-Identifier: MIT + */ #include +#include struct behavior_sensor_rotate_config { struct zmk_behavior_binding cw_binding; @@ -8,6 +14,12 @@ struct behavior_sensor_rotate_config { bool override_params; }; +struct behavior_sensor_rotate_data { + struct sensor_value remainder[ZMK_KEYMAP_SENSORS_LEN]; +}; + int zmk_behavior_sensor_rotate_common_trigger(struct zmk_behavior_binding *binding, - const struct device *sensor, - struct zmk_behavior_binding_event event); \ No newline at end of file + struct zmk_behavior_binding_event event, + const struct zmk_sensor_config *sensor_config, + size_t channel_data_size, + const struct zmk_sensor_channel_data *channel_data); \ No newline at end of file diff --git a/app/src/behaviors/behavior_sensor_rotate_var.c b/app/src/behaviors/behavior_sensor_rotate_var.c index a82267a5..95bb9961 100644 --- a/app/src/behaviors/behavior_sensor_rotate_var.c +++ b/app/src/behaviors/behavior_sensor_rotate_var.c @@ -24,8 +24,10 @@ static int behavior_sensor_rotate_var_init(const struct device *dev) { return 0; .tap_ms = DT_INST_PROP(n, tap_ms), \ .override_params = true, \ }; \ + static struct behavior_sensor_rotate_data behavior_sensor_rotate_var_data_##n = {}; \ DEVICE_DT_INST_DEFINE( \ - n, behavior_sensor_rotate_var_init, NULL, NULL, &behavior_sensor_rotate_var_config_##n, \ - APPLICATION, CONFIG_KERNEL_INIT_PRIORITY_DEFAULT, &behavior_sensor_rotate_var_driver_api); + n, behavior_sensor_rotate_var_init, NULL, &behavior_sensor_rotate_var_data_##n, \ + &behavior_sensor_rotate_var_config_##n, APPLICATION, CONFIG_KERNEL_INIT_PRIORITY_DEFAULT, \ + &behavior_sensor_rotate_var_driver_api); DT_INST_FOREACH_STATUS_OKAY(SENSOR_ROTATE_VAR_INST) diff --git a/app/src/keymap.c b/app/src/keymap.c index da25fc09..16543d37 100644 --- a/app/src/keymap.c +++ b/app/src/keymap.c @@ -252,27 +252,34 @@ int zmk_keymap_position_state_changed(uint8_t source, uint32_t position, bool pr } #if ZMK_KEYMAP_HAS_SENSORS -int zmk_keymap_sensor_triggered(uint8_t sensor_number, const struct device *sensor, - int64_t timestamp) { +int zmk_keymap_sensor_triggered( + uint8_t sensor_position, size_t channel_data_size, + const struct zmk_sensor_channel_data channel_data[channel_data_size], int64_t timestamp) { for (int layer = ZMK_KEYMAP_LAYERS_LEN - 1; layer >= _zmk_keymap_layer_default; layer--) { if (zmk_keymap_layer_active(layer)) { - struct zmk_behavior_binding *binding = &zmk_sensor_keymap[layer][sensor_number]; + struct zmk_behavior_binding *binding = &zmk_sensor_keymap[layer][sensor_position]; const struct device *behavior; int ret; - LOG_DBG("layer: %d sensor_number: %d, binding name: %s", layer, sensor_number, + LOG_DBG("layer: %d sensor_position: %d, binding name: %s", layer, sensor_position, 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); + LOG_DBG("No behavior assigned to %d on layer %d", sensor_position, layer); continue; } struct zmk_behavior_binding_event event = { - .position = ZMK_VIRTUAL_KEY_POSITION_SENSOR(sensor_number), .timestamp = timestamp}; - ret = behavior_sensor_keymap_binding_triggered(binding, sensor, event); + .layer = layer, + .position = ZMK_VIRTUAL_KEY_POSITION_SENSOR(sensor_position), + .timestamp = timestamp, + }; + + ret = behavior_sensor_keymap_binding_triggered( + binding, event, zmk_sensors_get_config_at_position(sensor_position), + channel_data_size, channel_data); if (ret > 0) { LOG_DBG("behavior processing to continue to next layer"); @@ -301,8 +308,8 @@ int keymap_listener(const zmk_event_t *eh) { #if ZMK_KEYMAP_HAS_SENSORS const struct zmk_sensor_event *sensor_ev; if ((sensor_ev = as_zmk_sensor_event(eh)) != NULL) { - return zmk_keymap_sensor_triggered(sensor_ev->sensor_number, sensor_ev->sensor, - sensor_ev->timestamp); + return zmk_keymap_sensor_triggered(sensor_ev->sensor_position, sensor_ev->channel_data_size, + sensor_ev->channel_data, sensor_ev->timestamp); } #endif /* ZMK_KEYMAP_HAS_SENSORS */ diff --git a/app/src/sensors.c b/app/src/sensors.c index 1b92147f..5f41c4f2 100644 --- a/app/src/sensors.c +++ b/app/src/sensors.c @@ -18,65 +18,128 @@ LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL); #if ZMK_KEYMAP_HAS_SENSORS -struct sensors_data_item { - uint8_t sensor_number; +struct sensors_item_cfg { + uint8_t sensor_position; + const struct zmk_sensor_config *config; const struct device *dev; struct sensor_trigger trigger; }; -#define _SENSOR_ITEM(node) \ +#define _SENSOR_ITEM(idx, node) \ { \ - .dev = NULL, .trigger = {.type = SENSOR_TRIG_DELTA, .chan = SENSOR_CHAN_ROTATION } \ + .dev = DEVICE_DT_GET_OR_NULL(node), \ + .trigger = {.type = SENSOR_TRIG_DATA_READY, .chan = SENSOR_CHAN_ROTATION}, \ + .config = &configs[idx] \ + } +#define SENSOR_ITEM(idx, _i) _SENSOR_ITEM(idx, ZMK_KEYMAP_SENSORS_BY_IDX(idx)) + +#define PLUS_ONE(n) +1 +#define ZMK_KEYMAP_SENSORS_CHILD_COUNT (0 DT_FOREACH_CHILD(ZMK_KEYMAP_SENSORS_NODE, PLUS_ONE)) +#define SENSOR_CHILD_ITEM(node) \ + { \ + .triggers_per_rotation = \ + DT_PROP_OR(node, triggers_per_rotation, \ + DT_PROP_OR(ZMK_KEYMAP_SENSORS_NODE, triggers_per_rotation, \ + CONFIG_ZMK_KEYMAP_SENSORS_DEFAULT_TRIGGERS_PER_ROTATION)) \ + } +#define SENSOR_CHILD_DEFAULTS(idx, arg) \ + { .triggers_per_rotation = DT_PROP_OR(ZMK_KEYMAP_SENSORS_NODE, triggers_per_rotation, 20) } + +static struct zmk_sensor_config configs[] = { +#if ZMK_KEYMAP_SENSORS_CHILD_COUNT > 0 + DT_FOREACH_CHILD_SEP(ZMK_KEYMAP_SENSORS_NODE, SENSOR_CHILD_ITEM, (, )) +#else + LISTIFY(ZMK_KEYMAP_SENSORS_LEN, SENSOR_CHILD_DEFAULTS, (, ), 0) +#endif +}; + +static struct sensors_item_cfg sensors[] = {LISTIFY(ZMK_KEYMAP_SENSORS_LEN, SENSOR_ITEM, (, ), 0)}; + +static ATOMIC_DEFINE(pending_sensors, ZMK_KEYMAP_SENSORS_LEN); + +const struct zmk_sensor_config *zmk_sensors_get_config_at_position(uint8_t sensor_position) { + if (sensor_position > ARRAY_SIZE(configs)) { + return NULL; } -#define SENSOR_ITEM(idx, _node) \ - COND_CODE_1(DT_NODE_HAS_STATUS(ZMK_KEYMAP_SENSORS_BY_IDX(idx), okay), \ - (_SENSOR_ITEM(ZMK_KEYMAP_SENSORS_BY_IDX(idx))), ({})) + return &configs[sensor_position]; +} -static struct sensors_data_item sensors[] = {LISTIFY(ZMK_KEYMAP_SENSORS_LEN, SENSOR_ITEM, (, ), 0)}; - -static void zmk_sensors_trigger_handler(const struct device *dev, - const struct sensor_trigger *trigger) { +static void trigger_sensor_data_for_position(uint32_t sensor_position) { int err; - const struct sensors_data_item *item = CONTAINER_OF(trigger, struct sensors_data_item, trigger); + const struct sensors_item_cfg *item = &sensors[sensor_position]; - LOG_DBG("sensor %d", item->sensor_number); - - err = sensor_sample_fetch(dev); + err = sensor_sample_fetch(item->dev); if (err) { LOG_WRN("Failed to fetch sample from device %d", err); return; } - ZMK_EVENT_RAISE(new_zmk_sensor_event((struct zmk_sensor_event){ - .sensor_number = item->sensor_number, .sensor = dev, .timestamp = k_uptime_get()})); -} + struct sensor_value value; + err = sensor_channel_get(item->dev, item->trigger.chan, &value); -static void zmk_sensors_init_item(const char *node, uint8_t i, uint8_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); - sensors[i].sensor_number = abs_i; - - if (!sensors[i].dev) { - LOG_WRN("Failed to find device for %s", node); + if (err) { + LOG_WRN("Failed to get channel data from device %d", err); return; } - sensor_trigger_set(sensors[i].dev, &sensors[i].trigger, zmk_sensors_trigger_handler); + ZMK_EVENT_RAISE(new_zmk_sensor_event( + (struct zmk_sensor_event){.sensor_position = item->sensor_position, + .channel_data = {(struct zmk_sensor_channel_data){ + .value = value, .channel = item->trigger.chan}}, + .timestamp = k_uptime_get()})); } -#define _SENSOR_INIT(node) \ - zmk_sensors_init_item(DT_PROP(node, label), 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++;)) +static void run_sensors_data_trigger(struct k_work *work) { + for (int i = 0; i < ARRAY_SIZE(sensors); i++) { + if (atomic_test_and_clear_bit(pending_sensors, i)) { + trigger_sensor_data_for_position(i); + } + } +} + +K_WORK_DEFINE(sensor_data_work, run_sensors_data_trigger); + +static void zmk_sensors_trigger_handler(const struct device *dev, + const struct sensor_trigger *trigger) { + const struct sensors_item_cfg *test_item = + CONTAINER_OF(trigger, struct sensors_item_cfg, trigger); + int sensor_index = test_item - sensors; + + if (sensor_index < 0 || sensor_index >= ARRAY_SIZE(sensors)) { + LOG_ERR("Invalid sensor item triggered our callback"); + return; + } + + if (k_is_in_isr()) { + atomic_set_bit(pending_sensors, sensor_index); + k_work_submit(&sensor_data_work); + } else { + trigger_sensor_data_for_position(sensor_index); + } +} + +static void zmk_sensors_init_item(uint8_t i) { + LOG_DBG("Init sensor at index %d", i); + + sensors[i].sensor_position = i; + + if (!sensors[i].dev) { + LOG_DBG("No local device for %d", i); + return; + } + + int err = sensor_trigger_set(sensors[i].dev, &sensors[i].trigger, zmk_sensors_trigger_handler); + if (err) { + LOG_WRN("Failed to set sensor trigger (%d)", err); + } +} + +#define SENSOR_INIT(idx, _t) zmk_sensors_init_item(idx); static int zmk_sensors_init(const struct device *_arg) { - int local_index = 0; - int absolute_index = 0; - LISTIFY(ZMK_KEYMAP_SENSORS_LEN, SENSOR_INIT, (), 0) + return 0; } From 295ed83409ab0bede761eb2e51f4e4670e4dbf08 Mon Sep 17 00:00:00 2001 From: Peter Johanson Date: Thu, 2 Dec 2021 15:07:29 +0000 Subject: [PATCH 08/17] refactor(sensors): ec11 rotation sensor value in degrees. * Add new `steps` property to the `aips,ec11` binding, to make the driver properly report degrees in the rotation delta channel. * Handle old sensor values in sensor rotate behavior. --- app/drivers/sensor/ec11/Kconfig | 2 ++ app/drivers/sensor/ec11/ec11.c | 33 +++++++++++++++---- app/drivers/sensor/ec11/ec11.h | 1 + .../zephyr/dts/bindings/sensor/alps,ec11.yaml | 5 +++ app/include/zmk/events/sensor_event.h | 4 +-- app/include/zmk/sensors.h | 2 +- 6 files changed, 38 insertions(+), 9 deletions(-) diff --git a/app/drivers/sensor/ec11/Kconfig b/app/drivers/sensor/ec11/Kconfig index e86d092a..5da32728 100644 --- a/app/drivers/sensor/ec11/Kconfig +++ b/app/drivers/sensor/ec11/Kconfig @@ -3,6 +3,8 @@ menuconfig EC11 bool "EC11 Incremental Encoder Sensor" + default y + depends on DT_HAS_ALPS_EC11_ENABLED depends on GPIO help Enable driver for EC11 incremental encoder sensors. diff --git a/app/drivers/sensor/ec11/ec11.c b/app/drivers/sensor/ec11/ec11.c index 7091f73e..ee8b41e7 100644 --- a/app/drivers/sensor/ec11/ec11.c +++ b/app/drivers/sensor/ec11/ec11.c @@ -16,6 +16,8 @@ #include "ec11.h" +#define FULL_ROTATION 360 + LOG_MODULE_REGISTER(EC11, CONFIG_SENSOR_LOG_LEVEL); static int ec11_get_ab_state(const struct device *dev) { @@ -59,9 +61,14 @@ static int ec11_sample_fetch(const struct device *dev, enum sensor_channel chan) 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; + // TODO: Temporary code for backwards compatibility to support + // the sensor channel rotation reporting *ticks* instead of delta of degrees. + // REMOVE ME + if (drv_cfg->steps == 0) { + drv_data->ticks = drv_data->pulses / drv_cfg->resolution; + drv_data->delta = delta; + drv_data->pulses %= drv_cfg->resolution; + } return 0; } @@ -69,13 +76,26 @@ static int ec11_sample_fetch(const struct device *dev, enum sensor_channel chan) static int ec11_channel_get(const struct device *dev, enum sensor_channel chan, struct sensor_value *val) { struct ec11_data *drv_data = dev->data; + const struct ec11_config *drv_cfg = dev->config; + int32_t pulses = drv_data->pulses; if (chan != SENSOR_CHAN_ROTATION) { return -ENOTSUP; } - val->val1 = drv_data->ticks; - val->val2 = drv_data->delta; + drv_data->pulses = 0; + + if (drv_cfg->steps > 0) { + val->val1 = (pulses * FULL_ROTATION) / drv_cfg->steps; + val->val2 = (pulses * FULL_ROTATION) % drv_cfg->steps; + if (val->val2 != 0) { + val->val2 *= 1000000; + val->val2 /= drv_cfg->steps; + } + } else { + val->val1 = drv_data->ticks; + val->val2 = drv_data->delta; + } return 0; } @@ -132,7 +152,8 @@ int ec11_init(const struct device *dev) { const struct ec11_config ec11_cfg_##n = { \ .a = GPIO_DT_SPEC_INST_GET(n, a_gpios), \ .b = GPIO_DT_SPEC_INST_GET(n, b_gpios), \ - COND_CODE_0(DT_INST_NODE_HAS_PROP(n, resolution), (1), (DT_INST_PROP(n, resolution))), \ + .resolution = DT_INST_PROP_OR(n, resolution, 1), \ + .steps = DT_INST_PROP_OR(n, steps, 0), \ }; \ DEVICE_DT_INST_DEFINE(n, ec11_init, NULL, &ec11_data_##n, &ec11_cfg_##n, POST_KERNEL, \ CONFIG_SENSOR_INIT_PRIORITY, &ec11_driver_api); diff --git a/app/drivers/sensor/ec11/ec11.h b/app/drivers/sensor/ec11/ec11.h index 82c21572..4e2e5d26 100644 --- a/app/drivers/sensor/ec11/ec11.h +++ b/app/drivers/sensor/ec11/ec11.h @@ -14,6 +14,7 @@ struct ec11_config { const struct gpio_dt_spec a; const struct gpio_dt_spec b; + const uint16_t steps; const uint8_t resolution; }; diff --git a/app/drivers/zephyr/dts/bindings/sensor/alps,ec11.yaml b/app/drivers/zephyr/dts/bindings/sensor/alps,ec11.yaml index 5cbe77a2..3672ea30 100644 --- a/app/drivers/zephyr/dts/bindings/sensor/alps,ec11.yaml +++ b/app/drivers/zephyr/dts/bindings/sensor/alps,ec11.yaml @@ -18,4 +18,9 @@ properties: resolution: type: int description: Number of pulses per tick + deprecated: true + required: false + steps: + type: int + description: Number of pulses in one full rotation required: false diff --git a/app/include/zmk/events/sensor_event.h b/app/include/zmk/events/sensor_event.h index 5a5aa3ac..c5157447 100644 --- a/app/include/zmk/events/sensor_event.h +++ b/app/include/zmk/events/sensor_event.h @@ -6,11 +6,11 @@ #pragma once - +#include #include + #include #include -#include // TODO: Move to Kconfig when we need more than one channel #define ZMK_SENSOR_EVENT_MAX_CHANNELS 1 diff --git a/app/include/zmk/sensors.h b/app/include/zmk/sensors.h index 41061127..06fbc63e 100644 --- a/app/include/zmk/sensors.h +++ b/app/include/zmk/sensors.h @@ -6,7 +6,7 @@ #pragma once -#include +#include #define _SENSOR_CHILD_LEN(node) 1 + #define ZMK_KEYMAP_SENSORS_NODE DT_INST(0, zmk_keymap_sensors) From f0f7e2081b5a4aa5bc35d7d8855b0e73e962319b Mon Sep 17 00:00:00 2001 From: Peter Johanson Date: Thu, 26 May 2022 16:53:41 +0000 Subject: [PATCH 09/17] refactor(config): Select SENSOR as needed. * Don't force on SENSOR Kconfig setting unless needed based on the detected DT. --- app/Kconfig | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/app/Kconfig b/app/Kconfig index 1766bf07..5b313eff 100644 --- a/app/Kconfig +++ b/app/Kconfig @@ -532,8 +532,11 @@ config ZMK_WPM bool "Calculate WPM" default n -config SENSOR +config ZMK_KEYMAP_SENSORS + bool "Enable Keymap Sensors support" default y + depends on DT_HAS_ZMK_KEYMAP_SENSORS_ENABLED + select SENSOR if ZMK_KEYMAP_SENSORS From 621d946d2926f28aa866947f110182726fa9ca10 Mon Sep 17 00:00:00 2001 From: Peter Johanson Date: Sun, 21 May 2023 21:27:41 -0700 Subject: [PATCH 10/17] refactor(bluetooth): Bump HoG stack size. * Bump the default stack size for the HoG processing thread to avoid issues w/ some pathways. --- app/Kconfig | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/Kconfig b/app/Kconfig index 5b313eff..a54ad389 100644 --- a/app/Kconfig +++ b/app/Kconfig @@ -135,7 +135,7 @@ config SYSTEM_WORKQUEUE_STACK_SIZE config ZMK_BLE_THREAD_STACK_SIZE int "BLE notify thread stack size" - default 512 + default 768 config ZMK_BLE_THREAD_PRIORITY int "BLE notify thread priority" From f8aaaff556e1c43c6646ea1c1ee7faa2907cdf51 Mon Sep 17 00:00:00 2001 From: Peter Johanson Date: Sun, 16 Apr 2023 08:18:57 +0000 Subject: [PATCH 11/17] refactor(shields): Updated ZMK Uno encoder config. * Move to new steps/triggers-per-rotation config. * Leverage QDEC Nordic driver when used on Nordic DK. --- .../boards/nrf52840dk_nrf52840.overlay | 24 +++++++++ app/boards/shields/zmk_uno/zmk_uno.overlay | 49 ++++++++++--------- 2 files changed, 50 insertions(+), 23 deletions(-) create mode 100644 app/boards/shields/zmk_uno/boards/nrf52840dk_nrf52840.overlay diff --git a/app/boards/shields/zmk_uno/boards/nrf52840dk_nrf52840.overlay b/app/boards/shields/zmk_uno/boards/nrf52840dk_nrf52840.overlay new file mode 100644 index 00000000..5ac7af7c --- /dev/null +++ b/app/boards/shields/zmk_uno/boards/nrf52840dk_nrf52840.overlay @@ -0,0 +1,24 @@ + +/ { + // First, delete the existing basic GPIO based instance. + /delete-node/ encoder; +}; + +&pinctrl { + qdec_default: qdec_default { + group1 { + psels = , + ; + bias-pull-up; + }; + }; +}; + +// Set up the QDEC hardware based driver and give it the same label as the deleted node. +encoder: &qdec0 { + status = "okay"; + led-pre = <0>; + steps = <80>; + pinctrl-0 = <&qdec_default>; + pinctrl-names = "default"; +}; diff --git a/app/boards/shields/zmk_uno/zmk_uno.overlay b/app/boards/shields/zmk_uno/zmk_uno.overlay index 04332911..78f3b4a7 100644 --- a/app/boards/shields/zmk_uno/zmk_uno.overlay +++ b/app/boards/shields/zmk_uno/zmk_uno.overlay @@ -40,10 +40,10 @@ // Commented out until we add more powerful power domain support // external_power { - // compatible = "zmk,ext-power-generic"; - // label = "EXT_POWER"; - // init-delay-ms = <200>; - // control-gpios = <&arduino_header 1 GPIO_ACTIVE_LOW>; + // compatible = "zmk,ext-power-generic"; + // label = "EXT_POWER"; + // init-delay-ms = <200>; + // control-gpios = <&arduino_header 1 GPIO_ACTIVE_LOW>; // }; rgb_power { @@ -128,14 +128,14 @@ diode-direction = "col2row"; col-gpios - = <&arduino_header 10 GPIO_ACTIVE_HIGH> - , <&arduino_header 9 GPIO_ACTIVE_HIGH> - ; + = <&arduino_header 10 GPIO_ACTIVE_HIGH> + , <&arduino_header 9 GPIO_ACTIVE_HIGH> + ; row-gpios - = <&arduino_header 13 (GPIO_ACTIVE_HIGH | GPIO_PULL_DOWN)> - , <&arduino_header 11 (GPIO_ACTIVE_HIGH | GPIO_PULL_DOWN)> - ; + = <&arduino_header 13 (GPIO_ACTIVE_HIGH | GPIO_PULL_DOWN)> + , <&arduino_header 11 (GPIO_ACTIVE_HIGH | GPIO_PULL_DOWN)> + ; }; @@ -144,11 +144,11 @@ status = "disabled"; input-gpios - = <&arduino_header 10 (GPIO_ACTIVE_LOW | GPIO_PULL_UP)> - , <&arduino_header 9 (GPIO_ACTIVE_LOW | GPIO_PULL_UP)> - , <&arduino_header 13 (GPIO_ACTIVE_LOW | GPIO_PULL_UP)> - , <&arduino_header 11 (GPIO_ACTIVE_LOW | GPIO_PULL_UP)> - ; + = <&arduino_header 10 (GPIO_ACTIVE_LOW | GPIO_PULL_UP)> + , <&arduino_header 9 (GPIO_ACTIVE_LOW | GPIO_PULL_UP)> + , <&arduino_header 13 (GPIO_ACTIVE_LOW | GPIO_PULL_UP)> + , <&arduino_header 11 (GPIO_ACTIVE_LOW | GPIO_PULL_UP)> + ; }; @@ -157,23 +157,26 @@ toggle-mode; input-gpios - = <&arduino_header 4 (GPIO_ACTIVE_LOW | GPIO_PULL_UP)> - , <&arduino_header 3 (GPIO_ACTIVE_LOW | GPIO_PULL_UP)> - , <&arduino_header 2 (GPIO_ACTIVE_LOW | GPIO_PULL_UP)> - ; + = <&arduino_header 4 (GPIO_ACTIVE_LOW | GPIO_PULL_UP)> + , <&arduino_header 3 (GPIO_ACTIVE_LOW | GPIO_PULL_UP)> + , <&arduino_header 2 (GPIO_ACTIVE_LOW | GPIO_PULL_UP)> + ; }; encoder: encoder { label = "ENCODER"; - resolution = <4>; + steps = <80>; compatible = "alps,ec11"; - a-gpios = <&arduino_header 14 GPIO_PULL_UP>; - b-gpios = <&arduino_header 15 GPIO_PULL_UP>; + a-gpios = <&arduino_header 15 GPIO_PULL_UP>; + b-gpios = <&arduino_header 14 GPIO_PULL_UP>; }; - sensors { compatible = "zmk,keymap-sensors"; sensors = <&encoder>; + triggers-per-rotation = <20>; + left { + triggers-per-rotation = <20>; + }; }; }; From d781ec795b9d9e053ce71c30450ba2b979eab43b Mon Sep 17 00:00:00 2001 From: Peter Johanson Date: Tue, 18 Apr 2023 07:14:18 +0000 Subject: [PATCH 12/17] refactor(bluetooth): Add battery reporting config. * Add dedicated battery reporting Kconfig that is `imply`d by enabling ZMK_BLE. --- app/CMakeLists.txt | 4 ++-- app/Kconfig | 8 +++++++- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/app/CMakeLists.txt b/app/CMakeLists.txt index a647e883..ea3b8f73 100644 --- a/app/CMakeLists.txt +++ b/app/CMakeLists.txt @@ -77,8 +77,8 @@ endif() target_sources_ifdef(CONFIG_ZMK_RGB_UNDERGLOW app PRIVATE src/behaviors/behavior_rgb_underglow.c) target_sources_ifdef(CONFIG_ZMK_BACKLIGHT app PRIVATE src/behaviors/behavior_backlight.c) -target_sources_ifdef(CONFIG_ZMK_BLE app PRIVATE src/events/battery_state_changed.c) -target_sources_ifdef(CONFIG_ZMK_BLE app PRIVATE src/battery.c) +target_sources_ifdef(CONFIG_ZMK_BATTERY_REPORTING app PRIVATE src/events/battery_state_changed.c) +target_sources_ifdef(CONFIG_ZMK_BATTERY_REPORTING app PRIVATE src/battery.c) target_sources_ifdef(CONFIG_ZMK_SPLIT app PRIVATE src/events/split_peripheral_status_changed.c) add_subdirectory(src/split) diff --git a/app/Kconfig b/app/Kconfig index a54ad389..9c0606b0 100644 --- a/app/Kconfig +++ b/app/Kconfig @@ -114,9 +114,9 @@ menuconfig ZMK_BLE select BT_SMP_APP_PAIRING_ACCEPT select BT_PERIPHERAL select BT_DIS - select BT_BAS select BT_SETTINGS select SETTINGS + imply ZMK_BATTERY_REPORTING if ZMK_BLE @@ -322,6 +322,12 @@ endmenu menu "Power Management" +config ZMK_BATTERY_REPORTING + bool "Battery level detection/reporting" + default n + select SENSOR + select BT_BAS if ZMK_BLE + config ZMK_IDLE_TIMEOUT int "Milliseconds of inactivity before entering idle state (OLED shutoff, etc)" default 30000 From 8b29f6d34556d98df60f529e84ee66e49e6bf0c0 Mon Sep 17 00:00:00 2001 From: Peter Johanson Date: Thu, 4 May 2023 00:04:20 +0000 Subject: [PATCH 13/17] refactor(sensors): Split data handling from triggers. * All sensor behaviors should see sensor data, then selectively only have some trigger their behaviors. --- app/include/drivers/behavior.h | 59 ++++++++++++-- app/src/behaviors/behavior_sensor_rotate.c | 3 +- .../behaviors/behavior_sensor_rotate_common.c | 31 +++++-- .../behaviors/behavior_sensor_rotate_common.h | 13 ++- .../behaviors/behavior_sensor_rotate_var.c | 3 +- app/src/keymap.c | 81 +++++++++++-------- 6 files changed, 135 insertions(+), 55 deletions(-) diff --git a/app/include/drivers/behavior.h b/app/include/drivers/behavior.h index 0aa5d85e..d7e57d02 100644 --- a/app/include/drivers/behavior.h +++ b/app/include/drivers/behavior.h @@ -23,9 +23,17 @@ * (Internal use only.) */ +enum behavior_sensor_binding_process_mode { + BEHAVIOR_SENSOR_BINDING_PROCESS_MODE_TRIGGER, + BEHAVIOR_SENSOR_BINDING_PROCESS_MODE_DISCARD, +}; + 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)( +typedef int (*behavior_sensor_keymap_binding_process_callback_t)( + struct zmk_behavior_binding *binding, struct zmk_behavior_binding_event event, + enum behavior_sensor_binding_process_mode mode); +typedef int (*behavior_sensor_keymap_binding_data_callback_t)( struct zmk_behavior_binding *binding, struct zmk_behavior_binding_event event, const struct zmk_sensor_config *sensor_config, size_t channel_data_size, const struct zmk_sensor_channel_data channel_data[channel_data_size]); @@ -41,7 +49,8 @@ __subsystem struct behavior_driver_api { behavior_keymap_binding_callback_t binding_convert_central_state_dependent_params; behavior_keymap_binding_callback_t binding_pressed; behavior_keymap_binding_callback_t binding_released; - behavior_sensor_keymap_binding_callback_t sensor_binding_triggered; + behavior_sensor_keymap_binding_data_callback_t sensor_binding_data; + behavior_sensor_keymap_binding_process_callback_t sensor_binding_process; }; /** * @endcond @@ -151,7 +160,7 @@ static inline int z_impl_behavior_keymap_binding_released(struct zmk_behavior_bi } /** - * @brief Handle the a sensor keymap binding being triggered + * @brief Handle the a sensor keymap binding processing any incoming data from the sensor * @param binding Sensor keymap binding which was triggered. * @param sensor Pointer to the sensor device structure for the sensor driver instance. * @param virtual_key_position ZMK_KEYMAP_LEN + sensor number @@ -160,12 +169,12 @@ static inline int z_impl_behavior_keymap_binding_released(struct zmk_behavior_bi * @retval 0 If successful. * @retval Negative errno code if failure. */ -__syscall int behavior_sensor_keymap_binding_triggered( +__syscall int behavior_sensor_keymap_binding_data( struct zmk_behavior_binding *binding, struct zmk_behavior_binding_event event, const struct zmk_sensor_config *sensor_config, size_t channel_data_size, const struct zmk_sensor_channel_data *channel_data); -static inline int z_impl_behavior_sensor_keymap_binding_triggered( +static inline int z_impl_behavior_sensor_keymap_binding_data( struct zmk_behavior_binding *binding, struct zmk_behavior_binding_event event, const struct zmk_sensor_config *sensor_config, size_t channel_data_size, const struct zmk_sensor_channel_data *channel_data) { @@ -177,12 +186,46 @@ static inline int z_impl_behavior_sensor_keymap_binding_triggered( const struct behavior_driver_api *api = (const struct behavior_driver_api *)dev->api; - if (api->sensor_binding_triggered == NULL) { + if (api->sensor_binding_data == NULL) { return -ENOTSUP; } - return api->sensor_binding_triggered(binding, event, sensor_config, channel_data_size, - channel_data); + return api->sensor_binding_data(binding, event, sensor_config, channel_data_size, channel_data); +} + +/** + * @brief Handle the keymap sensor binding being triggered after updating any local data + * @param dev Pointer to the device structure for the driver instance. + * @param param1 User parameter specified at time of behavior binding. + * @param param2 User parameter specified at time of behavior binding. + * + * @retval 0 If successful. + * @retval Negative errno code if failure. + */ +// clang-format off +__syscall int behavior_sensor_keymap_binding_process( + struct zmk_behavior_binding *binding, + struct zmk_behavior_binding_event event, + enum behavior_sensor_binding_process_mode mode); +// clang-format on + +static inline int +z_impl_behavior_sensor_keymap_binding_process(struct zmk_behavior_binding *binding, + struct zmk_behavior_binding_event event, + enum behavior_sensor_binding_process_mode mode) { + const struct device *dev = device_get_binding(binding->behavior_dev); + + if (dev == NULL) { + return -EINVAL; + } + + const struct behavior_driver_api *api = (const struct behavior_driver_api *)dev->api; + + if (api->sensor_binding_process == NULL) { + return -ENOTSUP; + } + + return api->sensor_binding_process(binding, event, mode); } /** diff --git a/app/src/behaviors/behavior_sensor_rotate.c b/app/src/behaviors/behavior_sensor_rotate.c index 86846d5b..3b5bef62 100644 --- a/app/src/behaviors/behavior_sensor_rotate.c +++ b/app/src/behaviors/behavior_sensor_rotate.c @@ -13,7 +13,8 @@ #include "behavior_sensor_rotate_common.h" static const struct behavior_driver_api behavior_sensor_rotate_driver_api = { - .sensor_binding_triggered = zmk_behavior_sensor_rotate_common_trigger}; + .sensor_binding_data = zmk_behavior_sensor_rotate_common_data, + .sensor_binding_process = zmk_behavior_sensor_rotate_common_process}; static int behavior_sensor_rotate_init(const struct device *dev) { return 0; }; diff --git a/app/src/behaviors/behavior_sensor_rotate_common.c b/app/src/behaviors/behavior_sensor_rotate_common.c index 99e4e019..0a2b619d 100644 --- a/app/src/behaviors/behavior_sensor_rotate_common.c +++ b/app/src/behaviors/behavior_sensor_rotate_common.c @@ -11,13 +11,12 @@ LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL); -int zmk_behavior_sensor_rotate_common_trigger(struct zmk_behavior_binding *binding, - struct zmk_behavior_binding_event event, - const struct zmk_sensor_config *sensor_config, - size_t channel_data_size, - const struct zmk_sensor_channel_data *channel_data) { +int zmk_behavior_sensor_rotate_common_data(struct zmk_behavior_binding *binding, + struct zmk_behavior_binding_event event, + const struct zmk_sensor_config *sensor_config, + size_t channel_data_size, + const struct zmk_sensor_channel_data *channel_data) { const struct device *dev = device_get_binding(binding->behavior_dev); - const struct behavior_sensor_rotate_config *cfg = dev->config; struct behavior_sensor_rotate_data *data = dev->data; const struct sensor_value value = channel_data[0].value; @@ -52,6 +51,26 @@ int zmk_behavior_sensor_rotate_common_trigger(struct zmk_behavior_binding *bindi value.val1, value.val2, data->remainder[sensor_position].val1, data->remainder[sensor_position].val2, triggers, binding->param1, binding->param2); + data->triggers[sensor_position] = triggers; + return 0; +} + +int zmk_behavior_sensor_rotate_common_process(struct zmk_behavior_binding *binding, + struct zmk_behavior_binding_event event, + enum behavior_sensor_binding_process_mode mode) { + const struct device *dev = device_get_binding(binding->behavior_dev); + const struct behavior_sensor_rotate_config *cfg = dev->config; + struct behavior_sensor_rotate_data *data = dev->data; + + const int sensor_position = ZMK_SENSOR_POSITION_FROM_VIRTUAL_KEY_POSITION(event.position); + + if (mode != BEHAVIOR_SENSOR_BINDING_PROCESS_MODE_TRIGGER) { + data->triggers[sensor_position] = 0; + return 0; + } + + int triggers = data->triggers[sensor_position]; + struct zmk_behavior_binding triggered_binding; if (triggers > 0) { triggered_binding = cfg->cw_binding; diff --git a/app/src/behaviors/behavior_sensor_rotate_common.h b/app/src/behaviors/behavior_sensor_rotate_common.h index eab443a3..d9d4d855 100644 --- a/app/src/behaviors/behavior_sensor_rotate_common.h +++ b/app/src/behaviors/behavior_sensor_rotate_common.h @@ -4,6 +4,7 @@ * SPDX-License-Identifier: MIT */ +#include #include #include @@ -16,10 +17,14 @@ struct behavior_sensor_rotate_config { struct behavior_sensor_rotate_data { struct sensor_value remainder[ZMK_KEYMAP_SENSORS_LEN]; + int triggers[ZMK_KEYMAP_SENSORS_LEN]; }; -int zmk_behavior_sensor_rotate_common_trigger(struct zmk_behavior_binding *binding, +int zmk_behavior_sensor_rotate_common_data(struct zmk_behavior_binding *binding, + struct zmk_behavior_binding_event event, + const struct zmk_sensor_config *sensor_config, + size_t channel_data_size, + const struct zmk_sensor_channel_data *channel_data); +int zmk_behavior_sensor_rotate_common_process(struct zmk_behavior_binding *binding, struct zmk_behavior_binding_event event, - const struct zmk_sensor_config *sensor_config, - size_t channel_data_size, - const struct zmk_sensor_channel_data *channel_data); \ No newline at end of file + enum behavior_sensor_binding_process_mode mode); \ No newline at end of file diff --git a/app/src/behaviors/behavior_sensor_rotate_var.c b/app/src/behaviors/behavior_sensor_rotate_var.c index 95bb9961..3c2373b0 100644 --- a/app/src/behaviors/behavior_sensor_rotate_var.c +++ b/app/src/behaviors/behavior_sensor_rotate_var.c @@ -13,7 +13,8 @@ #include "behavior_sensor_rotate_common.h" static const struct behavior_driver_api behavior_sensor_rotate_var_driver_api = { - .sensor_binding_triggered = zmk_behavior_sensor_rotate_common_trigger}; + .sensor_binding_data = zmk_behavior_sensor_rotate_common_data, + .sensor_binding_process = zmk_behavior_sensor_rotate_common_process}; static int behavior_sensor_rotate_var_init(const struct device *dev) { return 0; }; diff --git a/app/src/keymap.c b/app/src/keymap.c index 16543d37..c8121176 100644 --- a/app/src/keymap.c +++ b/app/src/keymap.c @@ -252,48 +252,59 @@ int zmk_keymap_position_state_changed(uint8_t source, uint32_t position, bool pr } #if ZMK_KEYMAP_HAS_SENSORS -int zmk_keymap_sensor_triggered( - uint8_t sensor_position, size_t channel_data_size, - const struct zmk_sensor_channel_data channel_data[channel_data_size], int64_t timestamp) { - for (int layer = ZMK_KEYMAP_LAYERS_LEN - 1; layer >= _zmk_keymap_layer_default; layer--) { - if (zmk_keymap_layer_active(layer)) { - struct zmk_behavior_binding *binding = &zmk_sensor_keymap[layer][sensor_position]; - const struct device *behavior; - int ret; +int zmk_keymap_sensor_event(uint8_t sensor_position, size_t channel_data_size, + const struct zmk_sensor_channel_data channel_data[channel_data_size], + int64_t timestamp) { + bool opaque_response = false; - LOG_DBG("layer: %d sensor_position: %d, binding name: %s", layer, sensor_position, - binding->behavior_dev); + for (int layer = ZMK_KEYMAP_LAYERS_LEN - 1; layer >= 0; layer--) { + struct zmk_behavior_binding *binding = &zmk_sensor_keymap[layer][sensor_position]; + const struct device *behavior; + int ret; - behavior = device_get_binding(binding->behavior_dev); + LOG_DBG("layer: %d sensor_position: %d, binding name: %s", layer, sensor_position, + binding->behavior_dev); - if (!behavior) { - LOG_DBG("No behavior assigned to %d on layer %d", sensor_position, layer); - continue; - } + behavior = device_get_binding(binding->behavior_dev); - struct zmk_behavior_binding_event event = { - .layer = layer, - .position = ZMK_VIRTUAL_KEY_POSITION_SENSOR(sensor_position), - .timestamp = timestamp, - }; + if (!behavior) { + LOG_DBG("No behavior assigned to %d on layer %d", sensor_position, layer); + continue; + } - ret = behavior_sensor_keymap_binding_triggered( - binding, event, zmk_sensors_get_config_at_position(sensor_position), - channel_data_size, channel_data); + struct zmk_behavior_binding_event event = { + .layer = layer, + .position = ZMK_VIRTUAL_KEY_POSITION_SENSOR(sensor_position), + .timestamp = timestamp, + }; - 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; - } + ret = behavior_sensor_keymap_binding_data( + binding, event, zmk_sensors_get_config_at_position(sensor_position), channel_data_size, + channel_data); + + if (ret > 0) { + LOG_DBG("behavior processing to continue to next layer"); + continue; + } + + enum behavior_sensor_binding_process_mode mode = + (!opaque_response && layer >= _zmk_keymap_layer_default && + zmk_keymap_layer_active(layer)) + ? BEHAVIOR_SENSOR_BINDING_PROCESS_MODE_TRIGGER + : BEHAVIOR_SENSOR_BINDING_PROCESS_MODE_DISCARD; + + ret = behavior_sensor_keymap_binding_process(binding, event, mode); + + if (ret == ZMK_BEHAVIOR_OPAQUE) { + LOG_DBG("sensor event processing complete, behavior response was opaque"); + opaque_response = true; + } else if (ret < 0) { + LOG_DBG("Behavior returned error: %d", ret); + return ret; } } - return -ENOTSUP; + return 0; } #endif /* ZMK_KEYMAP_HAS_SENSORS */ @@ -308,8 +319,8 @@ int keymap_listener(const zmk_event_t *eh) { #if ZMK_KEYMAP_HAS_SENSORS const struct zmk_sensor_event *sensor_ev; if ((sensor_ev = as_zmk_sensor_event(eh)) != NULL) { - return zmk_keymap_sensor_triggered(sensor_ev->sensor_position, sensor_ev->channel_data_size, - sensor_ev->channel_data, sensor_ev->timestamp); + return zmk_keymap_sensor_event(sensor_ev->sensor_position, sensor_ev->channel_data_size, + sensor_ev->channel_data, sensor_ev->timestamp); } #endif /* ZMK_KEYMAP_HAS_SENSORS */ From 3a91b325138dfba8ede743aec28e2cdbc85d17d2 Mon Sep 17 00:00:00 2001 From: Peter Johanson Date: Tue, 16 May 2023 22:27:01 -0700 Subject: [PATCH 14/17] refactor(sensors): Use "sensor index" consistently --- app/include/drivers/behavior.h | 8 ++--- app/include/zmk/events/sensor_event.h | 4 +-- app/include/zmk/sensors.h | 3 +- .../behaviors/behavior_sensor_rotate_common.c | 18 +++++----- app/src/keymap.c | 33 +++++++++---------- app/src/sensors.c | 16 ++++----- 6 files changed, 40 insertions(+), 42 deletions(-) diff --git a/app/include/drivers/behavior.h b/app/include/drivers/behavior.h index d7e57d02..bf2843bb 100644 --- a/app/include/drivers/behavior.h +++ b/app/include/drivers/behavior.h @@ -33,7 +33,7 @@ typedef int (*behavior_keymap_binding_callback_t)(struct zmk_behavior_binding *b typedef int (*behavior_sensor_keymap_binding_process_callback_t)( struct zmk_behavior_binding *binding, struct zmk_behavior_binding_event event, enum behavior_sensor_binding_process_mode mode); -typedef int (*behavior_sensor_keymap_binding_data_callback_t)( +typedef int (*behavior_sensor_keymap_binding_accept_data_callback_t)( struct zmk_behavior_binding *binding, struct zmk_behavior_binding_event event, const struct zmk_sensor_config *sensor_config, size_t channel_data_size, const struct zmk_sensor_channel_data channel_data[channel_data_size]); @@ -49,7 +49,7 @@ __subsystem struct behavior_driver_api { behavior_keymap_binding_callback_t binding_convert_central_state_dependent_params; behavior_keymap_binding_callback_t binding_pressed; behavior_keymap_binding_callback_t binding_released; - behavior_sensor_keymap_binding_data_callback_t sensor_binding_data; + behavior_sensor_keymap_binding_accept_data_callback_t sensor_binding_data; behavior_sensor_keymap_binding_process_callback_t sensor_binding_process; }; /** @@ -169,12 +169,12 @@ static inline int z_impl_behavior_keymap_binding_released(struct zmk_behavior_bi * @retval 0 If successful. * @retval Negative errno code if failure. */ -__syscall int behavior_sensor_keymap_binding_data( +__syscall int behavior_sensor_keymap_binding_accept_data( struct zmk_behavior_binding *binding, struct zmk_behavior_binding_event event, const struct zmk_sensor_config *sensor_config, size_t channel_data_size, const struct zmk_sensor_channel_data *channel_data); -static inline int z_impl_behavior_sensor_keymap_binding_data( +static inline int z_impl_behavior_sensor_keymap_binding_accept_data( struct zmk_behavior_binding *binding, struct zmk_behavior_binding_event event, const struct zmk_sensor_config *sensor_config, size_t channel_data_size, const struct zmk_sensor_channel_data *channel_data) { diff --git a/app/include/zmk/events/sensor_event.h b/app/include/zmk/events/sensor_event.h index c5157447..f6d23ac7 100644 --- a/app/include/zmk/events/sensor_event.h +++ b/app/include/zmk/events/sensor_event.h @@ -16,12 +16,12 @@ #define ZMK_SENSOR_EVENT_MAX_CHANNELS 1 struct zmk_sensor_event { - uint8_t sensor_position; - size_t channel_data_size; struct zmk_sensor_channel_data channel_data[ZMK_SENSOR_EVENT_MAX_CHANNELS]; int64_t timestamp; + + uint8_t sensor_index; }; ZMK_EVENT_DECLARE(zmk_sensor_event); \ No newline at end of file diff --git a/app/include/zmk/sensors.h b/app/include/zmk/sensors.h index 06fbc63e..8ac1c283 100644 --- a/app/include/zmk/sensors.h +++ b/app/include/zmk/sensors.h @@ -8,7 +8,6 @@ #include -#define _SENSOR_CHILD_LEN(node) 1 + #define ZMK_KEYMAP_SENSORS_NODE DT_INST(0, zmk_keymap_sensors) #define ZMK_KEYMAP_HAS_SENSORS DT_NODE_HAS_STATUS(ZMK_KEYMAP_SENSORS_NODE, okay) #define ZMK_KEYMAP_SENSORS_BY_IDX(idx) DT_PHANDLE_BY_IDX(ZMK_KEYMAP_SENSORS_NODE, sensors, idx) @@ -19,7 +18,7 @@ #define ZMK_KEYMAP_SENSORS_LEN 0 #endif -const struct zmk_sensor_config *zmk_sensors_get_config_at_position(uint8_t sensor_position); +const struct zmk_sensor_config *zmk_sensors_get_config_at_index(uint8_t sensor_index); struct zmk_sensor_config { uint16_t triggers_per_rotation; diff --git a/app/src/behaviors/behavior_sensor_rotate_common.c b/app/src/behaviors/behavior_sensor_rotate_common.c index 0a2b619d..4ccfea0e 100644 --- a/app/src/behaviors/behavior_sensor_rotate_common.c +++ b/app/src/behaviors/behavior_sensor_rotate_common.c @@ -21,7 +21,7 @@ int zmk_behavior_sensor_rotate_common_data(struct zmk_behavior_binding *binding, const struct sensor_value value = channel_data[0].value; int triggers; - int sensor_position = ZMK_SENSOR_POSITION_FROM_VIRTUAL_KEY_POSITION(event.position); + int sensor_index = ZMK_SENSOR_POSITION_FROM_VIRTUAL_KEY_POSITION(event.position); // Some funky special casing for "old encoder behavior" where ticks where reported in val2 only, // instead of rotational degrees in val1. @@ -29,7 +29,7 @@ int zmk_behavior_sensor_rotate_common_data(struct zmk_behavior_binding *binding, if (value.val1 == 0) { triggers = value.val2; } else { - struct sensor_value remainder = data->remainder[sensor_position]; + struct sensor_value remainder = data->remainder[sensor_index]; remainder.val1 += value.val1; remainder.val2 += value.val2; @@ -43,15 +43,15 @@ int zmk_behavior_sensor_rotate_common_data(struct zmk_behavior_binding *binding, triggers = remainder.val1 / trigger_degrees; remainder.val1 %= trigger_degrees; - data->remainder[sensor_position] = remainder; + data->remainder[sensor_index] = remainder; } LOG_DBG( "val1: %d, val2: %d, remainder: %d/%d triggers: %d inc keycode 0x%02X dec keycode 0x%02X", - value.val1, value.val2, data->remainder[sensor_position].val1, - data->remainder[sensor_position].val2, triggers, binding->param1, binding->param2); + value.val1, value.val2, data->remainder[sensor_index].val1, + data->remainder[sensor_index].val2, triggers, binding->param1, binding->param2); - data->triggers[sensor_position] = triggers; + data->triggers[sensor_index] = triggers; return 0; } @@ -62,14 +62,14 @@ int zmk_behavior_sensor_rotate_common_process(struct zmk_behavior_binding *bindi const struct behavior_sensor_rotate_config *cfg = dev->config; struct behavior_sensor_rotate_data *data = dev->data; - const int sensor_position = ZMK_SENSOR_POSITION_FROM_VIRTUAL_KEY_POSITION(event.position); + const int sensor_index = ZMK_SENSOR_POSITION_FROM_VIRTUAL_KEY_POSITION(event.position); if (mode != BEHAVIOR_SENSOR_BINDING_PROCESS_MODE_TRIGGER) { - data->triggers[sensor_position] = 0; + data->triggers[sensor_index] = 0; return 0; } - int triggers = data->triggers[sensor_position]; + int triggers = data->triggers[sensor_index]; struct zmk_behavior_binding triggered_binding; if (triggers > 0) { diff --git a/app/src/keymap.c b/app/src/keymap.c index c8121176..020faf3f 100644 --- a/app/src/keymap.c +++ b/app/src/keymap.c @@ -252,38 +252,37 @@ int zmk_keymap_position_state_changed(uint8_t source, uint32_t position, bool pr } #if ZMK_KEYMAP_HAS_SENSORS -int zmk_keymap_sensor_event(uint8_t sensor_position, size_t channel_data_size, - const struct zmk_sensor_channel_data channel_data[channel_data_size], - int64_t timestamp) { +int zmk_keymap_sensor_event(uint8_t sensor_index, + const struct zmk_sensor_channel_data *channel_data, + size_t channel_data_size, int64_t timestamp) { bool opaque_response = false; for (int layer = ZMK_KEYMAP_LAYERS_LEN - 1; layer >= 0; layer--) { - struct zmk_behavior_binding *binding = &zmk_sensor_keymap[layer][sensor_position]; - const struct device *behavior; - int ret; + struct zmk_behavior_binding *binding = &zmk_sensor_keymap[layer][sensor_index]; - LOG_DBG("layer: %d sensor_position: %d, binding name: %s", layer, sensor_position, + LOG_DBG("layer: %d sensor_index: %d, binding name: %s", layer, sensor_index, binding->behavior_dev); - behavior = device_get_binding(binding->behavior_dev); - + const struct device *behavior = device_get_binding(binding->behavior_dev); if (!behavior) { - LOG_DBG("No behavior assigned to %d on layer %d", sensor_position, layer); + LOG_DBG("No behavior assigned to %d on layer %d", sensor_index, layer); continue; } struct zmk_behavior_binding_event event = { .layer = layer, - .position = ZMK_VIRTUAL_KEY_POSITION_SENSOR(sensor_position), + .position = ZMK_VIRTUAL_KEY_POSITION_SENSOR(sensor_index), .timestamp = timestamp, }; - ret = behavior_sensor_keymap_binding_data( - binding, event, zmk_sensors_get_config_at_position(sensor_position), channel_data_size, + int ret = behavior_sensor_keymap_binding_accept_data( + binding, event, zmk_sensors_get_config_at_index(sensor_index), channel_data_size, channel_data); - if (ret > 0) { - LOG_DBG("behavior processing to continue to next layer"); + if (ret < 0) { + LOG_WRN("behavior data accept for behavior %s returned an error (%d). Processing to " + "continue to next layer", + binding->behavior_dev, ret); continue; } @@ -319,8 +318,8 @@ int keymap_listener(const zmk_event_t *eh) { #if ZMK_KEYMAP_HAS_SENSORS const struct zmk_sensor_event *sensor_ev; if ((sensor_ev = as_zmk_sensor_event(eh)) != NULL) { - return zmk_keymap_sensor_event(sensor_ev->sensor_position, sensor_ev->channel_data_size, - sensor_ev->channel_data, sensor_ev->timestamp); + return zmk_keymap_sensor_event(sensor_ev->sensor_index, sensor_ev->channel_data, + sensor_ev->channel_data_size, sensor_ev->timestamp); } #endif /* ZMK_KEYMAP_HAS_SENSORS */ diff --git a/app/src/sensors.c b/app/src/sensors.c index 5f41c4f2..3a34ca23 100644 --- a/app/src/sensors.c +++ b/app/src/sensors.c @@ -19,7 +19,7 @@ LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL); #if ZMK_KEYMAP_HAS_SENSORS struct sensors_item_cfg { - uint8_t sensor_position; + uint8_t sensor_index; const struct zmk_sensor_config *config; const struct device *dev; struct sensor_trigger trigger; @@ -57,17 +57,17 @@ static struct sensors_item_cfg sensors[] = {LISTIFY(ZMK_KEYMAP_SENSORS_LEN, SENS static ATOMIC_DEFINE(pending_sensors, ZMK_KEYMAP_SENSORS_LEN); -const struct zmk_sensor_config *zmk_sensors_get_config_at_position(uint8_t sensor_position) { - if (sensor_position > ARRAY_SIZE(configs)) { +const struct zmk_sensor_config *zmk_sensors_get_config_at_index(uint8_t sensor_index) { + if (sensor_index > ARRAY_SIZE(configs)) { return NULL; } - return &configs[sensor_position]; + return &configs[sensor_index]; } -static void trigger_sensor_data_for_position(uint32_t sensor_position) { +static void trigger_sensor_data_for_position(uint32_t sensor_index) { int err; - const struct sensors_item_cfg *item = &sensors[sensor_position]; + const struct sensors_item_cfg *item = &sensors[sensor_index]; err = sensor_sample_fetch(item->dev); if (err) { @@ -84,7 +84,7 @@ static void trigger_sensor_data_for_position(uint32_t sensor_position) { } ZMK_EVENT_RAISE(new_zmk_sensor_event( - (struct zmk_sensor_event){.sensor_position = item->sensor_position, + (struct zmk_sensor_event){.sensor_index = item->sensor_index, .channel_data = {(struct zmk_sensor_channel_data){ .value = value, .channel = item->trigger.chan}}, .timestamp = k_uptime_get()})); @@ -122,7 +122,7 @@ static void zmk_sensors_trigger_handler(const struct device *dev, static void zmk_sensors_init_item(uint8_t i) { LOG_DBG("Init sensor at index %d", i); - sensors[i].sensor_position = i; + sensors[i].sensor_index = i; if (!sensors[i].dev) { LOG_DBG("No local device for %d", i); From 753802cd79ce12d2e5d86c72ced3a4cc76fe4564 Mon Sep 17 00:00:00 2001 From: Peter Johanson Date: Sun, 18 Jun 2023 05:59:31 +0000 Subject: [PATCH 15/17] fix(sensors): Clean ups based on code review. --- app/Kconfig | 17 +++++------------ app/include/drivers/behavior.h | 7 ++++--- app/src/behaviors/behavior_sensor_rotate.c | 2 +- .../behaviors/behavior_sensor_rotate_common.c | 9 ++++----- .../behaviors/behavior_sensor_rotate_common.h | 9 ++++----- app/src/behaviors/behavior_sensor_rotate_var.c | 2 +- app/src/sensors.c | 9 +++++++-- 7 files changed, 26 insertions(+), 29 deletions(-) diff --git a/app/Kconfig b/app/Kconfig index 9c0606b0..92641c14 100644 --- a/app/Kconfig +++ b/app/Kconfig @@ -194,18 +194,6 @@ rsource "src/split/Kconfig" #Basic Keyboard Setup endmenu -menu "Encoders" - -config ZMK_ENCODERS_DEFAULT_TRIGGERS_PER_ROTATION - int "Default behavior triggers per rotation" - help - Unless overridden for a specific behavior in the keymap/devicetree, this value - determines how many times to trigger the bound behavior per full rotation. - For tactile encoders with detents, this usually should match the number of - detents per rotation of the encoder. - default 30 - -endmenu menu "Display/LED Options" rsource "src/display/Kconfig" @@ -548,6 +536,11 @@ if ZMK_KEYMAP_SENSORS config ZMK_KEYMAP_SENSORS_DEFAULT_TRIGGERS_PER_ROTATION int "Default triggers per rotation" + help + Unless overridden for a sensor in the board/shield/devicetree, this value + determines how many times to trigger the bound behavior per full rotation. + For tactile encoders with detents, this usually should match the number of + detents per rotation of the encoder. default 20 endif # ZMK_KEYMAP_SENSORS diff --git a/app/include/drivers/behavior.h b/app/include/drivers/behavior.h index bf2843bb..066cc723 100644 --- a/app/include/drivers/behavior.h +++ b/app/include/drivers/behavior.h @@ -49,7 +49,7 @@ __subsystem struct behavior_driver_api { behavior_keymap_binding_callback_t binding_convert_central_state_dependent_params; behavior_keymap_binding_callback_t binding_pressed; behavior_keymap_binding_callback_t binding_released; - behavior_sensor_keymap_binding_accept_data_callback_t sensor_binding_data; + behavior_sensor_keymap_binding_accept_data_callback_t sensor_binding_accept_data; behavior_sensor_keymap_binding_process_callback_t sensor_binding_process; }; /** @@ -186,11 +186,12 @@ static inline int z_impl_behavior_sensor_keymap_binding_accept_data( const struct behavior_driver_api *api = (const struct behavior_driver_api *)dev->api; - if (api->sensor_binding_data == NULL) { + if (api->sensor_binding_accept_data == NULL) { return -ENOTSUP; } - return api->sensor_binding_data(binding, event, sensor_config, channel_data_size, channel_data); + return api->sensor_binding_accept_data(binding, event, sensor_config, channel_data_size, + channel_data); } /** diff --git a/app/src/behaviors/behavior_sensor_rotate.c b/app/src/behaviors/behavior_sensor_rotate.c index 3b5bef62..822bc206 100644 --- a/app/src/behaviors/behavior_sensor_rotate.c +++ b/app/src/behaviors/behavior_sensor_rotate.c @@ -13,7 +13,7 @@ #include "behavior_sensor_rotate_common.h" static const struct behavior_driver_api behavior_sensor_rotate_driver_api = { - .sensor_binding_data = zmk_behavior_sensor_rotate_common_data, + .sensor_binding_accept_data = zmk_behavior_sensor_rotate_common_accept_data, .sensor_binding_process = zmk_behavior_sensor_rotate_common_process}; static int behavior_sensor_rotate_init(const struct device *dev) { return 0; }; diff --git a/app/src/behaviors/behavior_sensor_rotate_common.c b/app/src/behaviors/behavior_sensor_rotate_common.c index 4ccfea0e..eea7bf48 100644 --- a/app/src/behaviors/behavior_sensor_rotate_common.c +++ b/app/src/behaviors/behavior_sensor_rotate_common.c @@ -11,11 +11,10 @@ LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL); -int zmk_behavior_sensor_rotate_common_data(struct zmk_behavior_binding *binding, - struct zmk_behavior_binding_event event, - const struct zmk_sensor_config *sensor_config, - size_t channel_data_size, - const struct zmk_sensor_channel_data *channel_data) { +int zmk_behavior_sensor_rotate_common_accept_data( + struct zmk_behavior_binding *binding, struct zmk_behavior_binding_event event, + const struct zmk_sensor_config *sensor_config, size_t channel_data_size, + const struct zmk_sensor_channel_data *channel_data) { const struct device *dev = device_get_binding(binding->behavior_dev); struct behavior_sensor_rotate_data *data = dev->data; diff --git a/app/src/behaviors/behavior_sensor_rotate_common.h b/app/src/behaviors/behavior_sensor_rotate_common.h index d9d4d855..d354b679 100644 --- a/app/src/behaviors/behavior_sensor_rotate_common.h +++ b/app/src/behaviors/behavior_sensor_rotate_common.h @@ -20,11 +20,10 @@ struct behavior_sensor_rotate_data { int triggers[ZMK_KEYMAP_SENSORS_LEN]; }; -int zmk_behavior_sensor_rotate_common_data(struct zmk_behavior_binding *binding, - struct zmk_behavior_binding_event event, - const struct zmk_sensor_config *sensor_config, - size_t channel_data_size, - const struct zmk_sensor_channel_data *channel_data); +int zmk_behavior_sensor_rotate_common_accept_data( + struct zmk_behavior_binding *binding, struct zmk_behavior_binding_event event, + const struct zmk_sensor_config *sensor_config, size_t channel_data_size, + const struct zmk_sensor_channel_data *channel_data); int zmk_behavior_sensor_rotate_common_process(struct zmk_behavior_binding *binding, struct zmk_behavior_binding_event event, enum behavior_sensor_binding_process_mode mode); \ No newline at end of file diff --git a/app/src/behaviors/behavior_sensor_rotate_var.c b/app/src/behaviors/behavior_sensor_rotate_var.c index 3c2373b0..e6d20cab 100644 --- a/app/src/behaviors/behavior_sensor_rotate_var.c +++ b/app/src/behaviors/behavior_sensor_rotate_var.c @@ -13,7 +13,7 @@ #include "behavior_sensor_rotate_common.h" static const struct behavior_driver_api behavior_sensor_rotate_var_driver_api = { - .sensor_binding_data = zmk_behavior_sensor_rotate_common_data, + .sensor_binding_accept_data = zmk_behavior_sensor_rotate_common_accept_data, .sensor_binding_process = zmk_behavior_sensor_rotate_common_process}; static int behavior_sensor_rotate_var_init(const struct device *dev) { return 0; }; diff --git a/app/src/sensors.c b/app/src/sensors.c index 3a34ca23..e339afe0 100644 --- a/app/src/sensors.c +++ b/app/src/sensors.c @@ -19,10 +19,10 @@ LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL); #if ZMK_KEYMAP_HAS_SENSORS struct sensors_item_cfg { - uint8_t sensor_index; const struct zmk_sensor_config *config; const struct device *dev; struct sensor_trigger trigger; + uint8_t sensor_index; }; #define _SENSOR_ITEM(idx, node) \ @@ -43,7 +43,11 @@ struct sensors_item_cfg { CONFIG_ZMK_KEYMAP_SENSORS_DEFAULT_TRIGGERS_PER_ROTATION)) \ } #define SENSOR_CHILD_DEFAULTS(idx, arg) \ - { .triggers_per_rotation = DT_PROP_OR(ZMK_KEYMAP_SENSORS_NODE, triggers_per_rotation, 20) } + { \ + .triggers_per_rotation = \ + DT_PROP_OR(ZMK_KEYMAP_SENSORS_NODE, triggers_per_rotation, \ + CONFIG_ZMK_KEYMAP_SENSORS_DEFAULT_TRIGGERS_PER_ROTATION) \ + } static struct zmk_sensor_config configs[] = { #if ZMK_KEYMAP_SENSORS_CHILD_COUNT > 0 @@ -85,6 +89,7 @@ static void trigger_sensor_data_for_position(uint32_t sensor_index) { ZMK_EVENT_RAISE(new_zmk_sensor_event( (struct zmk_sensor_event){.sensor_index = item->sensor_index, + .channel_data_size = 1, .channel_data = {(struct zmk_sensor_channel_data){ .value = value, .channel = item->trigger.chan}}, .timestamp = k_uptime_get()})); From 5763558a02154ab599775c770fcf954931816e52 Mon Sep 17 00:00:00 2001 From: Peter Johanson Date: Tue, 30 May 2023 02:38:28 +0000 Subject: [PATCH 16/17] feat(blog): Add post about sensor refactor. * Document changed configuration, support for other Zephyr drivers, next steps. Co-authored-by: Cem Aksoylar --- docs/blog/2023-06-18-encoder-refactors.md | 119 ++++++++++++++++++++++ 1 file changed, 119 insertions(+) create mode 100644 docs/blog/2023-06-18-encoder-refactors.md diff --git a/docs/blog/2023-06-18-encoder-refactors.md b/docs/blog/2023-06-18-encoder-refactors.md new file mode 100644 index 00000000..26f9cee8 --- /dev/null +++ b/docs/blog/2023-06-18-encoder-refactors.md @@ -0,0 +1,119 @@ +--- +title: "Major Encoder Refactor" +author: Pete Johanson +author_title: Project Creator +author_url: https://gitlab.com/petejohanson +author_image_url: https://www.gravatar.com/avatar/2001ceff7e9dc753cf96fcb2e6f41110 +tags: [firmware, zephyr, sensors, encoders] +--- + +Today, we merged a significant change to the low level sensor code that is used to support encoders. In particular, +this paves the way for completing the work on supporting split peripheral sensors/encoders, and other future sensors +like pointing devices. + +As part of the work, backwards compatibility for existing shields has been retained, but only for a grace period to allow out-of-tree shields to move to the new approach for encoders. + +Special thanks to [joelspadin] for the _thorough_ code review and testing throughout the development of the refactor. + +## Summary of Changes + +The following items have been merged: + +1. Split configuration of hardware details, and behavior configuration to allow more flexible functionality of sensors/encoders, in particular linear encoders that lack detents/"clicks" as they rotate. +2. Support for upstream Zephyr sensor drivers, including the NRFX QDEC driver that can be used on nRF52 based keyboards. +3. Sensor data handling changes that pave the way for split sensor handling easily. + +## Configuration Changes + +The major changes to configuration in the devicetree files relates to how the number of steps/triggers for a given encoder are set. In particular, the number of pulses/steps for a given encoder is configured first, allowing ZMK to determine the exact angular degrees of change that is represented by a single pulse on the data lines to that encoder. + +Once that angular degrees mapping is completed, now independently there is a configuration setting to control how many triggers of the behavior in the keymap should occur for each full rotation of the sensor. Another way to think of this is "how many degrees of rotation results in a triggering of the sensor behavior in your keymap layer". + +Splitting these two parts of the encoder configuration allows greater flexibility, and fine grained control of encoder behavior for linear encoders that don't have fixed detents. + +### Old Configuration + +Previously, an encoder configuration looked like: + +``` + left_encoder: encoder_left { + compatible = "alps,ec11"; + label = "LEFT_ENCODER"; + a-gpios = <&pro_micro 21 (GPIO_ACTIVE_HIGH | GPIO_PULL_UP)>; + b-gpios = <&pro_micro 20 (GPIO_ACTIVE_HIGH | GPIO_PULL_UP)>; + resolution = <4>; + }; +``` + +Here, the `resolution` property was used to indicate how many encoder pulses should trigger the sensor behavior one time. Next, the encoder is selected in the sensors node: + +``` + sensors { + compatible = "zmk,keymap-sensors"; + sensors = <&left_encoder &right_encoder>; + }; +``` + +That was the entirety of the configuration for encoders. + +### New Configuration + +``` + left_encoder: encoder_left { + compatible = "alps,ec11"; + label = "LEFT_ENCODER"; + a-gpios = <&pro_micro 21 (GPIO_ACTIVE_HIGH | GPIO_PULL_UP)>; + b-gpios = <&pro_micro 20 (GPIO_ACTIVE_HIGH | GPIO_PULL_UP)>; + steps = <80>; + }; +``` + +Here, the `steps` property is now used to indicate how many encoder pulses there are in a single complete rotation of the encoder. Next, the encoder is selected in the sensors node as before, but an additional configuration is used to indicate how many times the encoder should trigger the behavior in your keymap per rotation: + +``` + sensors { + compatible = "zmk,keymap-sensors"; + sensors = <&left_encoder &right_encoder>; + triggers-per-rotation = <20>; + }; +``` + +For tactile encoders that have detents, the `triggers-per-rotation` would match the number of detents on the encoder. For linear encoders, the value can be chosen to suit your needs. + +## Zephyr Sensor Drivers + +The configuration changes bring ZMK's code in line with how upstream Zephyr sensor drivers handle rotations. This has the added advantage of allowing us to leverage other sensor drivers. On Nordic MCUs, like nRF52840, the NRFX QDEC driver can be used, for example: + +``` +&pinctrl { + qdec_default: qdec_default { + group1 { + psels = , + ; + bias-pull-up; + }; + }; +}; + +// Set up the QDEC hardware based driver and give it the same label as the deleted node. +encoder: &qdec0 { + status = "okay"; + led-pre = <0>; + steps = <80>; + pinctrl-0 = <&qdec_default>; + pinctrl-names = "default"; +}; +``` + +The NRFX QDEC driver has the advantage of supporting optical encoders as well, and although it polls, it does so in hardware without waking the MCU core; initial basic power profiling is promising. + +## Split Sensor/Encoder Support + +In addition to the refactors for splitting the configuration, the changes merged included refactors designed to simplify and move forward with the long outstanding feature of supporting encoders on the peripheral side of split keyboards. That work is planned as a follow up. + +## Deprecation + +The old configuration will be supported for a period of one month, and then removed, giving users a grace period to complete the migration to the new separated configuration. + +[petejohanson]: https://github.com/petejohanson +[joelspadin]: https://github.com/joelspadin From e686fce4d917b708da7f2baba4115043d3f0deab Mon Sep 17 00:00:00 2001 From: Xudong Zheng <7pkvm5aw@slicealias.com> Date: Fri, 19 May 2023 18:41:08 -0400 Subject: [PATCH 17/17] refactor(split): allow central to define connection parameters Fixes #1614 --- app/src/split/bluetooth/Kconfig | 17 +++++++++++++++-- app/src/split/bluetooth/central.c | 4 +++- 2 files changed, 18 insertions(+), 3 deletions(-) diff --git a/app/src/split/bluetooth/Kconfig b/app/src/split/bluetooth/Kconfig index e2c8d5c3..0610b667 100644 --- a/app/src/split/bluetooth/Kconfig +++ b/app/src/split/bluetooth/Kconfig @@ -33,6 +33,18 @@ config ZMK_SPLIT_BLE_CENTRAL_SPLIT_RUN_QUEUE_SIZE int "Max number of behavior run events to queue to send to the peripheral(s)" default 5 +config ZMK_SPLIT_BLE_PREF_INT + int "Connection interval to use for split central/peripheral connection" + default 6 + +config ZMK_SPLIT_BLE_PREF_LATENCY + int "Latency to use for split central/peripheral connection" + default 30 + +config ZMK_SPLIT_BLE_PREF_TIMEOUT + int "Supervision timeout to use for split central/peripheral connection" + default 400 + endif # ZMK_SPLIT_ROLE_CENTRAL if !ZMK_SPLIT_ROLE_CENTRAL @@ -58,8 +70,9 @@ config BT_MAX_PAIRED config BT_MAX_CONN default 1 -config BT_PERIPHERAL_PREF_MAX_INT - default 6 +# Allow central to specify connection parameters. +config BT_GAP_AUTO_UPDATE_CONN_PARAMS + default n #!ZMK_SPLIT_ROLE_CENTRAL endif diff --git a/app/src/split/bluetooth/central.c b/app/src/split/bluetooth/central.c index 53e61be6..147760ff 100644 --- a/app/src/split/bluetooth/central.c +++ b/app/src/split/bluetooth/central.c @@ -383,7 +383,9 @@ static bool split_central_eir_found(const bt_addr_le_t *addr) { struct peripheral_slot *slot = &peripherals[slot_idx]; LOG_DBG("Initiating new connnection"); - struct bt_le_conn_param *param = BT_LE_CONN_PARAM(0x0006, 0x0006, 30, 400); + struct bt_le_conn_param *param = + BT_LE_CONN_PARAM(CONFIG_ZMK_SPLIT_BLE_PREF_INT, CONFIG_ZMK_SPLIT_BLE_PREF_INT, + CONFIG_ZMK_SPLIT_BLE_PREF_LATENCY, CONFIG_ZMK_SPLIT_BLE_PREF_TIMEOUT); err = bt_conn_le_create(addr, BT_CONN_LE_CREATE_CONN, param, &slot->conn); if (err < 0) { LOG_ERR("Create conn failed (err %d) (create conn? 0x%04x)", err, BT_HCI_OP_LE_CREATE_CONN);