This commit is contained in:
KemoNine 2021-07-31 23:26:28 +10:00 committed by GitHub
commit 3466a9001d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 97 additions and 6 deletions

View file

@ -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)
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_sleep.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_momentary_layer.c)

View file

@ -14,3 +14,4 @@
#include <behaviors/bluetooth.dtsi>
#include <behaviors/ext_power.dtsi>
#include <behaviors/outputs.dtsi>
#include <behaviors/sleep.dtsi>

View file

@ -0,0 +1,15 @@
/*
* Copyright (c) 2020 The ZMK Contributors
*
* SPDX-License-Identifier: MIT
*/
/ {
behaviors {
/omit-if-no-ref/ sleep: behavior_sleep {
compatible = "zmk,behavior-sleep";
label = "SLEEP";
#binding-cells = <0>;
};
};
};

View 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

View file

@ -9,3 +9,4 @@
enum zmk_activity_state { ZMK_ACTIVITY_ACTIVE, ZMK_ACTIVITY_IDLE, ZMK_ACTIVITY_SLEEP };
enum zmk_activity_state zmk_activity_get_state();
int zmk_activity_set_state(enum zmk_activity_state state);

View file

@ -34,7 +34,8 @@ int raise_event() {
(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)
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) {
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) {
int32_t current = k_uptime_get();
int32_t inactive_time = current - activity_last_uptime;
#if IS_ENABLED(CONFIG_ZMK_SLEEP)
if (inactive_time > MAX_SLEEP_MS) {
set_state(ZMK_ACTIVITY_SLEEP);
// Second half of || statement is to ensure a user using &sleep doesn't lose sleep state when
// the idle or sleep timers expire
if (inactive_time > MAX_SLEEP_MS || activity_state == ZMK_ACTIVITY_SLEEP) {
activity_set_state(ZMK_ACTIVITY_SLEEP);
} else
#endif /* IS_ENABLED(CONFIG_ZMK_SLEEP) */
if (inactive_time > MAX_IDLE_MS) {
set_state(ZMK_ACTIVITY_IDLE);
activity_set_state(ZMK_ACTIVITY_IDLE);
}
}

View file

@ -0,0 +1,43 @@
/*
* 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>
#if DT_HAS_COMPAT_STATUS_OKAY(DT_DRV_COMPAT)
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) {
#if IS_ENABLED(CONFIG_ZMK_SLEEP)
return activity_set_state(ZMK_ACTIVITY_SLEEP);
#endif
return ZMK_BEHAVIOR_OPAQUE;
}
static int on_keymap_binding_pressed(struct zmk_behavior_binding *binding,
struct zmk_behavior_binding_event event) {
return ZMK_BEHAVIOR_OPAQUE;
}
static const struct behavior_driver_api behavior_sleep_driver_api = {
.binding_released = on_keymap_binding_released,
.binding_pressed = on_keymap_binding_pressed,
};
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);
#endif /* DT_HAS_COMPAT_STATUS_OKAY(DT_DRV_COMPAT) */

View file

@ -40,3 +40,22 @@ Example:
```
&none
```
## Sleep
The sleep behavior simply puts your keyboard to sleep. This behavior has the same effect as letting the sleep timer expire.
:::note
You'll need to enable `CONFIG_ZMK_SLEEP` in your boards config for this keycode to work.
:::
### Behavior Binding
- Reference: `&sleep`
- Parameters: None
Example:
```
&sleep
```