summaryrefslogtreecommitdiff
path: root/app
diff options
context:
space:
mode:
authorAlessandro Bortolin <bortolin.alessandro@outlook.it>2022-01-22 11:57:51 +0100
committerPete Johanson <peter@peterjohanson.com>2022-02-05 23:40:18 -0500
commit13a45153004a63dccc421463ddac9ac9075e32cd (patch)
treea4885abb38b2c7ced1a7fcde268fecb9397129e0 /app
parent2c0fe3934d461fe8b565f90f3c5d092ba231caf1 (diff)
feat(backlight): add command to cycle brightness
Diffstat (limited to 'app')
-rw-r--r--app/include/dt-bindings/zmk/backlight.h4
-rw-r--r--app/include/zmk/backlight.h1
-rw-r--r--app/src/backlight.c8
-rw-r--r--app/src/behaviors/behavior_backlight.c8
-rw-r--r--app/tests/backlight/cycle/events.patterns1
-rw-r--r--app/tests/backlight/cycle/keycode_events.snapshot14
-rw-r--r--app/tests/backlight/cycle/native_posix.conf14
-rw-r--r--app/tests/backlight/cycle/native_posix.keymap78
8 files changed, 127 insertions, 1 deletions
diff --git a/app/include/dt-bindings/zmk/backlight.h b/app/include/dt-bindings/zmk/backlight.h
index fa6dc9b..0802e2c 100644
--- a/app/include/dt-bindings/zmk/backlight.h
+++ b/app/include/dt-bindings/zmk/backlight.h
@@ -9,11 +9,13 @@
#define BL_TOG_CMD 2
#define BL_INC_CMD 3
#define BL_DEC_CMD 4
-#define BL_SET_CMD 5
+#define BL_CYCLE_CMD 5
+#define BL_SET_CMD 6
#define BL_ON BL_ON_CMD 0
#define BL_OFF BL_OFF_CMD 0
#define BL_TOG BL_TOG_CMD 0
#define BL_INC BL_INC_CMD 0
#define BL_DEC BL_DEC_CMD 0
+#define BL_CYCLE BL_CYCLE_CMD 0
#define BL_SET BL_SET_CMD
diff --git a/app/include/zmk/backlight.h b/app/include/zmk/backlight.h
index dd7d966..a0f5243 100644
--- a/app/include/zmk/backlight.h
+++ b/app/include/zmk/backlight.h
@@ -14,3 +14,4 @@ bool zmk_backlight_is_on();
int zmk_backlight_set_brt(uint8_t brightness);
uint8_t zmk_backlight_get_brt();
uint8_t zmk_backlight_calc_brt(int direction);
+uint8_t zmk_backlight_calc_brt_cycle();
diff --git a/app/src/backlight.c b/app/src/backlight.c
index 46e3c9e..e870864 100644
--- a/app/src/backlight.c
+++ b/app/src/backlight.c
@@ -137,6 +137,14 @@ uint8_t zmk_backlight_calc_brt(int direction) {
return CLAMP(brt, 0, BRT_MAX);
}
+uint8_t zmk_backlight_calc_brt_cycle() {
+ if (state.brightness == BRT_MAX) {
+ return 0;
+ } else {
+ return zmk_backlight_calc_brt(1);
+ }
+}
+
#if IS_ENABLED(CONFIG_ZMK_BACKLIGHT_AUTO_OFF_IDLE) || IS_ENABLED(CONFIG_ZMK_BACKLIGHT_AUTO_OFF_USB)
static int backlight_auto_state(bool *prev_state, bool new_state) {
if (state.on == new_state) {
diff --git a/app/src/behaviors/behavior_backlight.c b/app/src/behaviors/behavior_backlight.c
index 8dd6ee5..bdad828 100644
--- a/app/src/behaviors/behavior_backlight.c
+++ b/app/src/behaviors/behavior_backlight.c
@@ -35,6 +35,10 @@ on_keymap_binding_convert_central_state_dependent_params(struct zmk_behavior_bin
binding->param1 = BL_SET_CMD;
binding->param2 = zmk_backlight_calc_brt(-1);
break;
+ case BL_CYCLE_CMD:
+ binding->param1 = BL_SET_CMD;
+ binding->param2 = zmk_backlight_calc_brt_cycle();
+ break;
default:
return 0;
}
@@ -61,6 +65,10 @@ static int on_keymap_binding_pressed(struct zmk_behavior_binding *binding,
uint8_t brt = zmk_backlight_calc_brt(-1);
return zmk_backlight_set_brt(brt);
}
+ case BL_CYCLE_CMD: {
+ uint8_t brt = zmk_backlight_calc_brt_cycle();
+ return zmk_backlight_set_brt(brt);
+ }
case BL_SET_CMD:
return zmk_backlight_set_brt(binding->param2);
default:
diff --git a/app/tests/backlight/cycle/events.patterns b/app/tests/backlight/cycle/events.patterns
new file mode 100644
index 0000000..bb11bc1
--- /dev/null
+++ b/app/tests/backlight/cycle/events.patterns
@@ -0,0 +1 @@
+s/.*zmk_backlight_update: //p
diff --git a/app/tests/backlight/cycle/keycode_events.snapshot b/app/tests/backlight/cycle/keycode_events.snapshot
new file mode 100644
index 0000000..70d0988
--- /dev/null
+++ b/app/tests/backlight/cycle/keycode_events.snapshot
@@ -0,0 +1,14 @@
+Update backlight brightness: 40%
+Update backlight brightness: 60%
+Update backlight brightness: 80%
+Update backlight brightness: 100%
+Update backlight brightness: 0%
+Update backlight brightness: 20%
+Update backlight brightness: 40%
+Update backlight brightness: 60%
+Update backlight brightness: 80%
+Update backlight brightness: 100%
+Update backlight brightness: 0%
+Update backlight brightness: 20%
+Update backlight brightness: 40%
+Update backlight brightness: 60%
diff --git a/app/tests/backlight/cycle/native_posix.conf b/app/tests/backlight/cycle/native_posix.conf
new file mode 100644
index 0000000..565121d
--- /dev/null
+++ b/app/tests/backlight/cycle/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/cycle/native_posix.keymap b/app/tests/backlight/cycle/native_posix.keymap
new file mode 100644
index 0000000..a2f3c83
--- /dev/null
+++ b/app/tests/backlight/cycle/native_posix.keymap
@@ -0,0 +1,78 @@
+#include <behaviors.dtsi>
+#include <dt-bindings/zmk/keys.h>
+#include <dt-bindings/zmk/kscan_mock.h>
+#include <dt-bindings/zmk/backlight.h>
+
+/ {
+ 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_CYCLE &none
+ &none &none
+ >;
+ };
+ };
+};
+
+&kscan {
+ events = <
+ /* BL_CYCLE */
+ ZMK_MOCK_PRESS(0,0,10)
+ ZMK_MOCK_RELEASE(0,0,10)
+ /* BL_CYCLE */
+ ZMK_MOCK_PRESS(0,0,10)
+ ZMK_MOCK_RELEASE(0,0,10)
+ /* BL_CYCLE */
+ ZMK_MOCK_PRESS(0,0,10)
+ ZMK_MOCK_RELEASE(0,0,10)
+ /* BL_CYCLE */
+ ZMK_MOCK_PRESS(0,0,10)
+ ZMK_MOCK_RELEASE(0,0,10)
+ /* BL_CYCLE */
+ ZMK_MOCK_PRESS(0,0,10)
+ ZMK_MOCK_RELEASE(0,0,10)
+ /* BL_CYCLE */
+ ZMK_MOCK_PRESS(0,0,10)
+ ZMK_MOCK_RELEASE(0,0,10)
+ /* BL_CYCLE */
+ ZMK_MOCK_PRESS(0,0,10)
+ ZMK_MOCK_RELEASE(0,0,10)
+ /* BL_CYCLE */
+ ZMK_MOCK_PRESS(0,0,10)
+ ZMK_MOCK_RELEASE(0,0,10)
+ /* BL_CYCLE */
+ ZMK_MOCK_PRESS(0,0,10)
+ ZMK_MOCK_RELEASE(0,0,10)
+ /* BL_CYCLE */
+ ZMK_MOCK_PRESS(0,0,10)
+ ZMK_MOCK_RELEASE(0,0,10)
+ /* BL_CYCLE */
+ ZMK_MOCK_PRESS(0,0,10)
+ ZMK_MOCK_RELEASE(0,0,10)
+ /* BL_CYCLE */
+ ZMK_MOCK_PRESS(0,0,10)
+ ZMK_MOCK_RELEASE(0,0,10)
+ /* BL_CYCLE */
+ ZMK_MOCK_PRESS(0,0,10)
+ ZMK_MOCK_RELEASE(0,0,10)
+ >;
+};