From 2c0fe3934d461fe8b565f90f3c5d092ba231caf1 Mon Sep 17 00:00:00 2001 From: Alessandro Bortolin Date: Mon, 20 Dec 2021 13:58:31 +0100 Subject: [PATCH] feat(backlight): add tests --- app/Kconfig | 2 - app/src/backlight.c | 2 + app/tests/backlight/basic/events.patterns | 1 + .../backlight/basic/keycode_events.snapshot | 9 +++++ app/tests/backlight/basic/native_posix.conf | 14 +++++++ app/tests/backlight/basic/native_posix.keymap | 30 ++++++++++++++ app/tests/backlight/behavior_keymap.dtsi | 34 ++++++++++++++++ .../backlight/config-brt/events.patterns | 1 + .../config-brt/keycode_events.snapshot | 3 ++ .../backlight/config-brt/native_posix.conf | 15 +++++++ .../backlight/config-brt/native_posix.keymap | 12 ++++++ app/tests/backlight/config-on/events.patterns | 1 + .../config-on/keycode_events.snapshot | 3 ++ .../backlight/config-on/native_posix.conf | 15 +++++++ .../backlight/config-on/native_posix.keymap | 12 ++++++ .../backlight/config-step/events.patterns | 1 + .../config-step/keycode_events.snapshot | 11 ++++++ .../backlight/config-step/native_posix.conf | 16 ++++++++ .../backlight/config-step/native_posix.keymap | 36 +++++++++++++++++ .../backlight/low-brightness/events.patterns | 1 + .../low-brightness/keycode_events.snapshot | 12 ++++++ .../low-brightness/native_posix.conf | 14 +++++++ .../low-brightness/native_posix.keymap | 39 +++++++++++++++++++ docs/docs/features/backlight.md | 4 +- 24 files changed, 285 insertions(+), 3 deletions(-) create mode 100644 app/tests/backlight/basic/events.patterns create mode 100644 app/tests/backlight/basic/keycode_events.snapshot create mode 100644 app/tests/backlight/basic/native_posix.conf create mode 100644 app/tests/backlight/basic/native_posix.keymap create mode 100644 app/tests/backlight/behavior_keymap.dtsi create mode 100644 app/tests/backlight/config-brt/events.patterns create mode 100644 app/tests/backlight/config-brt/keycode_events.snapshot create mode 100644 app/tests/backlight/config-brt/native_posix.conf create mode 100644 app/tests/backlight/config-brt/native_posix.keymap create mode 100644 app/tests/backlight/config-on/events.patterns create mode 100644 app/tests/backlight/config-on/keycode_events.snapshot create mode 100644 app/tests/backlight/config-on/native_posix.conf create mode 100644 app/tests/backlight/config-on/native_posix.keymap create mode 100644 app/tests/backlight/config-step/events.patterns create mode 100644 app/tests/backlight/config-step/keycode_events.snapshot create mode 100644 app/tests/backlight/config-step/native_posix.conf create mode 100644 app/tests/backlight/config-step/native_posix.keymap create mode 100644 app/tests/backlight/low-brightness/events.patterns create mode 100644 app/tests/backlight/low-brightness/keycode_events.snapshot create mode 100644 app/tests/backlight/low-brightness/native_posix.conf create mode 100644 app/tests/backlight/low-brightness/native_posix.keymap diff --git a/app/Kconfig b/app/Kconfig index 02fef66a..1c9f929d 100644 --- a/app/Kconfig +++ b/app/Kconfig @@ -330,9 +330,7 @@ endif menuconfig ZMK_BACKLIGHT bool "LED backlight" - select PWM select LED - select ZMK_LED_PWM if ZMK_BACKLIGHT diff --git a/app/src/backlight.c b/app/src/backlight.c index 6ef7f8f0..46e3c9e3 100644 --- a/app/src/backlight.c +++ b/app/src/backlight.c @@ -44,6 +44,8 @@ static struct backlight_state state = {.brightness = CONFIG_ZMK_BACKLIGHT_BRT_ST static int zmk_backlight_update() { uint8_t brt = zmk_backlight_get_brt(); + LOG_DBG("Update backlight brightness: %d%%", brt); + for (int i = 0; i < BACKLIGHT_NUM_LEDS; i++) { int rc = led_set_brightness(backlight_dev, i, brt); if (rc != 0) { diff --git a/app/tests/backlight/basic/events.patterns b/app/tests/backlight/basic/events.patterns new file mode 100644 index 00000000..bb11bc15 --- /dev/null +++ b/app/tests/backlight/basic/events.patterns @@ -0,0 +1 @@ +s/.*zmk_backlight_update: //p diff --git a/app/tests/backlight/basic/keycode_events.snapshot b/app/tests/backlight/basic/keycode_events.snapshot new file mode 100644 index 00000000..4aa184cd --- /dev/null +++ b/app/tests/backlight/basic/keycode_events.snapshot @@ -0,0 +1,9 @@ +Update backlight brightness: 40% +Update backlight brightness: 60% +Update backlight brightness: 80% +Update backlight brightness: 60% +Update backlight brightness: 40% +Update backlight brightness: 0% +Update backlight brightness: 0% +Update backlight brightness: 40% +Update backlight brightness: 40% diff --git a/app/tests/backlight/basic/native_posix.conf b/app/tests/backlight/basic/native_posix.conf new file mode 100644 index 00000000..565121d3 --- /dev/null +++ b/app/tests/backlight/basic/native_posix.conf @@ -0,0 +1,14 @@ +CONFIG_KSCAN=n +CONFIG_ZMK_KSCAN_MOCK_DRIVER=y +CONFIG_ZMK_KSCAN_GPIO_DRIVER=n +CONFIG_GPIO=y +CONFIG_GPIO_EMUL=y +CONFIG_ZMK_BLE=n +CONFIG_LOG=y +CONFIG_LOG_BACKEND_SHOW_COLOR=n +CONFIG_ZMK_LOG_LEVEL_DBG=y +CONFIG_DEBUG=y +CONFIG_SYS_CLOCK_TICKS_PER_SEC=1000 + +CONFIG_LED_GPIO=y +CONFIG_ZMK_BACKLIGHT=y diff --git a/app/tests/backlight/basic/native_posix.keymap b/app/tests/backlight/basic/native_posix.keymap new file mode 100644 index 00000000..185dbf7c --- /dev/null +++ b/app/tests/backlight/basic/native_posix.keymap @@ -0,0 +1,30 @@ +#include "../behavior_keymap.dtsi" + +&kscan { + events = < + /* BL_INC */ + ZMK_MOCK_PRESS(0,0,10) + ZMK_MOCK_RELEASE(0,0,10) + /* BL_INC */ + ZMK_MOCK_PRESS(0,0,10) + ZMK_MOCK_RELEASE(0,0,10) + /* BL_DEC */ + ZMK_MOCK_PRESS(0,1,10) + ZMK_MOCK_RELEASE(0,1,10) + /* BL_DEC */ + ZMK_MOCK_PRESS(0,1,10) + ZMK_MOCK_RELEASE(0,1,10) + /* BL_OFF */ + ZMK_MOCK_PRESS(1,1,10) + ZMK_MOCK_RELEASE(1,1,10) + /* BL_OFF */ + ZMK_MOCK_PRESS(1,1,10) + ZMK_MOCK_RELEASE(1,1,10) + /* BL_ON */ + ZMK_MOCK_PRESS(1,0,10) + ZMK_MOCK_RELEASE(1,0,10) + /* BL_ON */ + ZMK_MOCK_PRESS(1,0,10) + ZMK_MOCK_RELEASE(1,0,10) + >; +}; diff --git a/app/tests/backlight/behavior_keymap.dtsi b/app/tests/backlight/behavior_keymap.dtsi new file mode 100644 index 00000000..26869272 --- /dev/null +++ b/app/tests/backlight/behavior_keymap.dtsi @@ -0,0 +1,34 @@ +#include +#include +#include +#include + +/ { + chosen { + zmk,backlight = &backlight; + }; + + backlight: leds { + compatible = "gpio-leds"; + led_0 { + gpios = <&gpio0 0 GPIO_ACTIVE_HIGH>; + label = "Backlight LED 0"; + }; + led_1 { + gpios = <&gpio0 1 GPIO_ACTIVE_HIGH>; + label = "Backlight LED 1"; + }; + }; + + keymap { + compatible = "zmk,keymap"; + label ="Default keymap"; + + default_layer { + bindings = < + &bl BL_INC &bl BL_DEC + &bl BL_ON &bl BL_OFF + >; + }; + }; +}; diff --git a/app/tests/backlight/config-brt/events.patterns b/app/tests/backlight/config-brt/events.patterns new file mode 100644 index 00000000..bb11bc15 --- /dev/null +++ b/app/tests/backlight/config-brt/events.patterns @@ -0,0 +1 @@ +s/.*zmk_backlight_update: //p diff --git a/app/tests/backlight/config-brt/keycode_events.snapshot b/app/tests/backlight/config-brt/keycode_events.snapshot new file mode 100644 index 00000000..3297a7cd --- /dev/null +++ b/app/tests/backlight/config-brt/keycode_events.snapshot @@ -0,0 +1,3 @@ +Update backlight brightness: 60% +Update backlight brightness: 80% +Update backlight brightness: 60% diff --git a/app/tests/backlight/config-brt/native_posix.conf b/app/tests/backlight/config-brt/native_posix.conf new file mode 100644 index 00000000..0d0758c0 --- /dev/null +++ b/app/tests/backlight/config-brt/native_posix.conf @@ -0,0 +1,15 @@ +CONFIG_KSCAN=n +CONFIG_ZMK_KSCAN_MOCK_DRIVER=y +CONFIG_ZMK_KSCAN_GPIO_DRIVER=n +CONFIG_GPIO=y +CONFIG_GPIO_EMUL=y +CONFIG_ZMK_BLE=n +CONFIG_LOG=y +CONFIG_LOG_BACKEND_SHOW_COLOR=n +CONFIG_ZMK_LOG_LEVEL_DBG=y +CONFIG_DEBUG=y +CONFIG_SYS_CLOCK_TICKS_PER_SEC=1000 + +CONFIG_LED_GPIO=y +CONFIG_ZMK_BACKLIGHT=y +CONFIG_ZMK_BACKLIGHT_BRT_START=60 diff --git a/app/tests/backlight/config-brt/native_posix.keymap b/app/tests/backlight/config-brt/native_posix.keymap new file mode 100644 index 00000000..6617c9f6 --- /dev/null +++ b/app/tests/backlight/config-brt/native_posix.keymap @@ -0,0 +1,12 @@ +#include "../behavior_keymap.dtsi" + +&kscan { + events = < + /* BL_INC */ + ZMK_MOCK_PRESS(0,0,10) + ZMK_MOCK_RELEASE(0,0,10) + /* BL_DEC */ + ZMK_MOCK_PRESS(0,1,10) + ZMK_MOCK_RELEASE(0,1,10) + >; +}; diff --git a/app/tests/backlight/config-on/events.patterns b/app/tests/backlight/config-on/events.patterns new file mode 100644 index 00000000..bb11bc15 --- /dev/null +++ b/app/tests/backlight/config-on/events.patterns @@ -0,0 +1 @@ +s/.*zmk_backlight_update: //p diff --git a/app/tests/backlight/config-on/keycode_events.snapshot b/app/tests/backlight/config-on/keycode_events.snapshot new file mode 100644 index 00000000..8797af58 --- /dev/null +++ b/app/tests/backlight/config-on/keycode_events.snapshot @@ -0,0 +1,3 @@ +Update backlight brightness: 0% +Update backlight brightness: 40% +Update backlight brightness: 0% diff --git a/app/tests/backlight/config-on/native_posix.conf b/app/tests/backlight/config-on/native_posix.conf new file mode 100644 index 00000000..241c66a7 --- /dev/null +++ b/app/tests/backlight/config-on/native_posix.conf @@ -0,0 +1,15 @@ +CONFIG_KSCAN=n +CONFIG_ZMK_KSCAN_MOCK_DRIVER=y +CONFIG_ZMK_KSCAN_GPIO_DRIVER=n +CONFIG_GPIO=y +CONFIG_GPIO_EMUL=y +CONFIG_ZMK_BLE=n +CONFIG_LOG=y +CONFIG_LOG_BACKEND_SHOW_COLOR=n +CONFIG_ZMK_LOG_LEVEL_DBG=y +CONFIG_DEBUG=y +CONFIG_SYS_CLOCK_TICKS_PER_SEC=1000 + +CONFIG_LED_GPIO=y +CONFIG_ZMK_BACKLIGHT=y +CONFIG_ZMK_BACKLIGHT_ON_START=n diff --git a/app/tests/backlight/config-on/native_posix.keymap b/app/tests/backlight/config-on/native_posix.keymap new file mode 100644 index 00000000..a95ccd93 --- /dev/null +++ b/app/tests/backlight/config-on/native_posix.keymap @@ -0,0 +1,12 @@ +#include "../behavior_keymap.dtsi" + +&kscan { + events = < + /* BL_ON */ + ZMK_MOCK_PRESS(1,0,10) + ZMK_MOCK_RELEASE(1,0,10) + /* BL_OFF */ + ZMK_MOCK_PRESS(1,1,10) + ZMK_MOCK_RELEASE(1,1,10) + >; +}; diff --git a/app/tests/backlight/config-step/events.patterns b/app/tests/backlight/config-step/events.patterns new file mode 100644 index 00000000..bb11bc15 --- /dev/null +++ b/app/tests/backlight/config-step/events.patterns @@ -0,0 +1 @@ +s/.*zmk_backlight_update: //p diff --git a/app/tests/backlight/config-step/keycode_events.snapshot b/app/tests/backlight/config-step/keycode_events.snapshot new file mode 100644 index 00000000..4532fed4 --- /dev/null +++ b/app/tests/backlight/config-step/keycode_events.snapshot @@ -0,0 +1,11 @@ +Update backlight brightness: 60% +Update backlight brightness: 90% +Update backlight brightness: 100% +Update backlight brightness: 100% +Update backlight brightness: 70% +Update backlight brightness: 40% +Update backlight brightness: 10% +Update backlight brightness: 0% +Update backlight brightness: 0% +Update backlight brightness: 30% +Update backlight brightness: 60% diff --git a/app/tests/backlight/config-step/native_posix.conf b/app/tests/backlight/config-step/native_posix.conf new file mode 100644 index 00000000..4df7a861 --- /dev/null +++ b/app/tests/backlight/config-step/native_posix.conf @@ -0,0 +1,16 @@ +CONFIG_KSCAN=n +CONFIG_ZMK_KSCAN_MOCK_DRIVER=y +CONFIG_ZMK_KSCAN_GPIO_DRIVER=n +CONFIG_GPIO=y +CONFIG_GPIO_EMUL=y +CONFIG_ZMK_BLE=n +CONFIG_LOG=y +CONFIG_LOG_BACKEND_SHOW_COLOR=n +CONFIG_ZMK_LOG_LEVEL_DBG=y +CONFIG_DEBUG=y +CONFIG_SYS_CLOCK_TICKS_PER_SEC=1000 + +CONFIG_LED_GPIO=y +CONFIG_ZMK_BACKLIGHT=y +CONFIG_ZMK_BACKLIGHT_BRT_START=60 +CONFIG_ZMK_BACKLIGHT_BRT_STEP=30 diff --git a/app/tests/backlight/config-step/native_posix.keymap b/app/tests/backlight/config-step/native_posix.keymap new file mode 100644 index 00000000..96fbe96a --- /dev/null +++ b/app/tests/backlight/config-step/native_posix.keymap @@ -0,0 +1,36 @@ +#include "../behavior_keymap.dtsi" + +&kscan { + events = < + /* BL_INC */ + ZMK_MOCK_PRESS(0,0,10) + ZMK_MOCK_RELEASE(0,0,10) + /* BL_INC */ + ZMK_MOCK_PRESS(0,0,10) + ZMK_MOCK_RELEASE(0,0,10) + /* BL_INC */ + ZMK_MOCK_PRESS(0,0,10) + ZMK_MOCK_RELEASE(0,0,10) + /* BL_DEC */ + ZMK_MOCK_PRESS(0,1,10) + ZMK_MOCK_RELEASE(0,1,10) + /* BL_DEC */ + ZMK_MOCK_PRESS(0,1,10) + ZMK_MOCK_RELEASE(0,1,10) + /* BL_DEC */ + ZMK_MOCK_PRESS(0,1,10) + ZMK_MOCK_RELEASE(0,1,10) + /* BL_DEC */ + ZMK_MOCK_PRESS(0,1,10) + ZMK_MOCK_RELEASE(0,1,10) + /* BL_DEC */ + ZMK_MOCK_PRESS(0,1,10) + ZMK_MOCK_RELEASE(0,1,10) + /* BL_INC */ + ZMK_MOCK_PRESS(0,0,10) + ZMK_MOCK_RELEASE(0,0,10) + /* BL_INC */ + ZMK_MOCK_PRESS(0,0,10) + ZMK_MOCK_RELEASE(0,0,10) + >; +}; diff --git a/app/tests/backlight/low-brightness/events.patterns b/app/tests/backlight/low-brightness/events.patterns new file mode 100644 index 00000000..bb11bc15 --- /dev/null +++ b/app/tests/backlight/low-brightness/events.patterns @@ -0,0 +1 @@ +s/.*zmk_backlight_update: //p diff --git a/app/tests/backlight/low-brightness/keycode_events.snapshot b/app/tests/backlight/low-brightness/keycode_events.snapshot new file mode 100644 index 00000000..9fee3247 --- /dev/null +++ b/app/tests/backlight/low-brightness/keycode_events.snapshot @@ -0,0 +1,12 @@ +Update backlight brightness: 40% +Update backlight brightness: 20% +Update backlight brightness: 0% +Update backlight brightness: 20% +Update backlight brightness: 0% +Update backlight brightness: 40% +Update backlight brightness: 60% +Update backlight brightness: 0% +Update backlight brightness: 40% +Update backlight brightness: 20% +Update backlight brightness: 0% +Update backlight brightness: 20% diff --git a/app/tests/backlight/low-brightness/native_posix.conf b/app/tests/backlight/low-brightness/native_posix.conf new file mode 100644 index 00000000..565121d3 --- /dev/null +++ b/app/tests/backlight/low-brightness/native_posix.conf @@ -0,0 +1,14 @@ +CONFIG_KSCAN=n +CONFIG_ZMK_KSCAN_MOCK_DRIVER=y +CONFIG_ZMK_KSCAN_GPIO_DRIVER=n +CONFIG_GPIO=y +CONFIG_GPIO_EMUL=y +CONFIG_ZMK_BLE=n +CONFIG_LOG=y +CONFIG_LOG_BACKEND_SHOW_COLOR=n +CONFIG_ZMK_LOG_LEVEL_DBG=y +CONFIG_DEBUG=y +CONFIG_SYS_CLOCK_TICKS_PER_SEC=1000 + +CONFIG_LED_GPIO=y +CONFIG_ZMK_BACKLIGHT=y diff --git a/app/tests/backlight/low-brightness/native_posix.keymap b/app/tests/backlight/low-brightness/native_posix.keymap new file mode 100644 index 00000000..3b01f700 --- /dev/null +++ b/app/tests/backlight/low-brightness/native_posix.keymap @@ -0,0 +1,39 @@ +#include "../behavior_keymap.dtsi" + +&kscan { + events = < + /* BL_DEC */ + ZMK_MOCK_PRESS(0,1,10) + ZMK_MOCK_RELEASE(0,1,10) + /* BL_DEC */ + ZMK_MOCK_PRESS(0,1,10) + ZMK_MOCK_RELEASE(0,1,10) + /* BL_ON */ + ZMK_MOCK_PRESS(1,0,10) + ZMK_MOCK_RELEASE(1,0,10) + /* BL_OFF */ + ZMK_MOCK_PRESS(1,1,10) + ZMK_MOCK_RELEASE(1,1,10) + /* BL_INC */ + ZMK_MOCK_PRESS(0,0,10) + ZMK_MOCK_RELEASE(0,0,10) + /* BL_INC */ + ZMK_MOCK_PRESS(0,0,10) + ZMK_MOCK_RELEASE(0,0,10) + /* BL_OFF */ + ZMK_MOCK_PRESS(1,1,10) + ZMK_MOCK_RELEASE(1,1,10) + /* BL_DEC */ + ZMK_MOCK_PRESS(0,1,10) + ZMK_MOCK_RELEASE(0,1,10) + /* BL_DEC */ + ZMK_MOCK_PRESS(0,1,10) + ZMK_MOCK_RELEASE(0,1,10) + /* BL_DEC */ + ZMK_MOCK_PRESS(0,1,10) + ZMK_MOCK_RELEASE(0,1,10) + /* BL_INC */ + ZMK_MOCK_PRESS(0,0,10) + ZMK_MOCK_RELEASE(0,0,10) + >; +}; diff --git a/docs/docs/features/backlight.md b/docs/docs/features/backlight.md index f43c7357..379cc770 100644 --- a/docs/docs/features/backlight.md +++ b/docs/docs/features/backlight.md @@ -7,9 +7,11 @@ Backlight is a feature used to control array of LEDs, usually placed through or ## Enabling Backlight -To enable backlight on your board or shield, simply enable the `CONFIG_ZMK_BACKLIGHT` configuration values in the `.conf` file of your user config directory as such: +To enable backlight on your board or shield, add the following lines to your `.conf` file of your user config directory as such: ``` +CONFIG_PWM=y +CONFIG_LED_PWM=y CONFIG_ZMK_BACKLIGHT=y ```