feat(behaviors): implement a &sleep behavior that'll put a keyboard to sleep prior to timeout
This commit is contained in:
parent
4ef11ac4aa
commit
471d693f3a
7 changed files with 65 additions and 6 deletions
|
@ -44,6 +44,7 @@ target_sources_ifdef(CONFIG_USB app PRIVATE src/events/usb_conn_state_changed.c)
|
||||||
if ((NOT CONFIG_ZMK_SPLIT) OR CONFIG_ZMK_SPLIT_BLE_ROLE_CENTRAL)
|
if ((NOT CONFIG_ZMK_SPLIT) OR CONFIG_ZMK_SPLIT_BLE_ROLE_CENTRAL)
|
||||||
target_sources(app PRIVATE src/behaviors/behavior_key_press.c)
|
target_sources(app PRIVATE src/behaviors/behavior_key_press.c)
|
||||||
target_sources(app PRIVATE src/behaviors/behavior_reset.c)
|
target_sources(app PRIVATE src/behaviors/behavior_reset.c)
|
||||||
|
target_sources(app PRIVATE src/behaviors/behavior_sleep.c)
|
||||||
target_sources(app PRIVATE src/behaviors/behavior_hold_tap.c)
|
target_sources(app PRIVATE src/behaviors/behavior_hold_tap.c)
|
||||||
target_sources(app PRIVATE src/behaviors/behavior_sticky_key.c)
|
target_sources(app PRIVATE src/behaviors/behavior_sticky_key.c)
|
||||||
target_sources(app PRIVATE src/behaviors/behavior_momentary_layer.c)
|
target_sources(app PRIVATE src/behaviors/behavior_momentary_layer.c)
|
||||||
|
|
|
@ -14,3 +14,4 @@
|
||||||
#include <behaviors/bluetooth.dtsi>
|
#include <behaviors/bluetooth.dtsi>
|
||||||
#include <behaviors/ext_power.dtsi>
|
#include <behaviors/ext_power.dtsi>
|
||||||
#include <behaviors/outputs.dtsi>
|
#include <behaviors/outputs.dtsi>
|
||||||
|
#include <behaviors/sleep.dtsi>
|
||||||
|
|
15
app/dts/behaviors/sleep.dtsi
Normal file
15
app/dts/behaviors/sleep.dtsi
Normal file
|
@ -0,0 +1,15 @@
|
||||||
|
/*
|
||||||
|
* Copyright (c) 2020 The ZMK Contributors
|
||||||
|
*
|
||||||
|
* SPDX-License-Identifier: MIT
|
||||||
|
*/
|
||||||
|
|
||||||
|
/ {
|
||||||
|
behaviors {
|
||||||
|
sleep: behavior_sleep {
|
||||||
|
compatible = "zmk,behavior-sleep";
|
||||||
|
label = "SLEEP";
|
||||||
|
#binding-cells = <0>;
|
||||||
|
};
|
||||||
|
};
|
||||||
|
};
|
8
app/dts/bindings/behaviors/zmk,behavior-sleep.yaml
Normal file
8
app/dts/bindings/behaviors/zmk,behavior-sleep.yaml
Normal file
|
@ -0,0 +1,8 @@
|
||||||
|
# Copyright (c) 2020 The ZMK Contributors
|
||||||
|
# SPDX-License-Identifier: MIT
|
||||||
|
|
||||||
|
description: Sleep Binding Behavior
|
||||||
|
|
||||||
|
compatible: "zmk,behavior-sleep"
|
||||||
|
|
||||||
|
include: zero_param.yaml
|
|
@ -9,3 +9,4 @@
|
||||||
enum zmk_activity_state { ZMK_ACTIVITY_ACTIVE, ZMK_ACTIVITY_IDLE, ZMK_ACTIVITY_SLEEP };
|
enum zmk_activity_state { ZMK_ACTIVITY_ACTIVE, ZMK_ACTIVITY_IDLE, ZMK_ACTIVITY_SLEEP };
|
||||||
|
|
||||||
enum zmk_activity_state zmk_activity_get_state();
|
enum zmk_activity_state zmk_activity_get_state();
|
||||||
|
int activity_set_state(enum zmk_activity_state state);
|
||||||
|
|
|
@ -34,7 +34,8 @@ int raise_event() {
|
||||||
(struct zmk_activity_state_changed){.state = activity_state}));
|
(struct zmk_activity_state_changed){.state = activity_state}));
|
||||||
}
|
}
|
||||||
|
|
||||||
int set_state(enum zmk_activity_state state) {
|
int activity_set_state(enum zmk_activity_state state) {
|
||||||
|
LOG_DBG("Setting activity state: %i", state);
|
||||||
if (activity_state == state)
|
if (activity_state == state)
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
|
@ -47,19 +48,21 @@ enum zmk_activity_state zmk_activity_get_state() { return activity_state; }
|
||||||
int activity_event_listener(const zmk_event_t *eh) {
|
int activity_event_listener(const zmk_event_t *eh) {
|
||||||
activity_last_uptime = k_uptime_get();
|
activity_last_uptime = k_uptime_get();
|
||||||
|
|
||||||
return set_state(ZMK_ACTIVITY_ACTIVE);
|
return activity_set_state(ZMK_ACTIVITY_ACTIVE);
|
||||||
}
|
}
|
||||||
|
|
||||||
void activity_work_handler(struct k_work *work) {
|
void activity_work_handler(struct k_work *work) {
|
||||||
int32_t current = k_uptime_get();
|
int32_t current = k_uptime_get();
|
||||||
int32_t inactive_time = current - activity_last_uptime;
|
int32_t inactive_time = current - activity_last_uptime;
|
||||||
#if IS_ENABLED(CONFIG_ZMK_SLEEP)
|
#if IS_ENABLED(CONFIG_ZMK_SLEEP)
|
||||||
if (inactive_time > MAX_SLEEP_MS) {
|
// Second half of || statement is to ensure a user using &sleep doesn't lose sleep state when
|
||||||
set_state(ZMK_ACTIVITY_SLEEP);
|
// the idle or sleep timers expire
|
||||||
|
if (inactive_time > MAX_SLEEP_MS || activity_state == ZMK_ACTIVITY_SLEEP) {
|
||||||
|
activity_set_state(ZMK_ACTIVITY_SLEEP);
|
||||||
} else
|
} else
|
||||||
#endif /* IS_ENABLED(CONFIG_ZMK_SLEEP) */
|
#endif /* IS_ENABLED(CONFIG_ZMK_SLEEP) */
|
||||||
if (inactive_time > MAX_IDLE_MS) {
|
if (inactive_time > MAX_IDLE_MS) {
|
||||||
set_state(ZMK_ACTIVITY_IDLE);
|
activity_set_state(ZMK_ACTIVITY_IDLE);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
30
app/src/behaviors/behavior_sleep.c
Normal file
30
app/src/behaviors/behavior_sleep.c
Normal file
|
@ -0,0 +1,30 @@
|
||||||
|
/*
|
||||||
|
* Copyright (c) 2020 The ZMK Contributors
|
||||||
|
*
|
||||||
|
* SPDX-License-Identifier: MIT
|
||||||
|
*/
|
||||||
|
|
||||||
|
#define DT_DRV_COMPAT zmk_behavior_sleep
|
||||||
|
|
||||||
|
#include <device.h>
|
||||||
|
#include <drivers/behavior.h>
|
||||||
|
#include <logging/log.h>
|
||||||
|
|
||||||
|
#include <zmk/behavior.h>
|
||||||
|
#include <zmk/activity.h>
|
||||||
|
|
||||||
|
LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL);
|
||||||
|
|
||||||
|
static int behavior_sleep_init(const struct device *dev) { return 0; };
|
||||||
|
|
||||||
|
static int on_keymap_binding_released(struct zmk_behavior_binding *binding,
|
||||||
|
struct zmk_behavior_binding_event event) {
|
||||||
|
return activity_set_state(ZMK_ACTIVITY_SLEEP);
|
||||||
|
}
|
||||||
|
|
||||||
|
static const struct behavior_driver_api behavior_sleep_driver_api = {
|
||||||
|
.binding_released = on_keymap_binding_released,
|
||||||
|
};
|
||||||
|
|
||||||
|
DEVICE_AND_API_INIT(behavior_sleep, DT_INST_LABEL(0), behavior_sleep_init, NULL, NULL, APPLICATION,
|
||||||
|
CONFIG_KERNEL_INIT_PRIORITY_DEFAULT, &behavior_sleep_driver_api);
|
Loading…
Add table
Reference in a new issue