summaryrefslogtreecommitdiff
path: root/app
diff options
context:
space:
mode:
authorPete Johanson <peter@peterjohanson.com>2020-12-01 00:52:32 -0500
committerPete Johanson <peter@peterjohanson.com>2020-12-14 15:31:10 -0500
commitf7c16dfe69eb551fa0eb50b8ebcf6f00e23c2bad (patch)
tree4deae4fc19973e64cc7d3bb0322e384d42608432 /app
parent8a529163fcabf7e63aa79e2116b96763039cfdca (diff)
refactor(power): Extract activity/idle detection.
* Refactor power to extract more general purpose activity detection/events. * Use activity state to implement PM callback.
Diffstat (limited to 'app')
-rw-r--r--app/CMakeLists.txt2
-rw-r--r--app/Kconfig6
-rw-r--r--app/include/zmk/activity.h11
-rw-r--r--app/include/zmk/events/activity-state-changed.h26
-rw-r--r--app/src/activity.c80
-rw-r--r--app/src/events/activity_state_changed.c10
-rw-r--r--app/src/power.c30
7 files changed, 136 insertions, 29 deletions
diff --git a/app/CMakeLists.txt b/app/CMakeLists.txt
index 7f374f7..e6887dd 100644
--- a/app/CMakeLists.txt
+++ b/app/CMakeLists.txt
@@ -23,12 +23,14 @@ zephyr_linker_sources(RODATA include/linker/zmk-events.ld)
# find_package(Zephyr) which defines the target.
target_include_directories(app PRIVATE include)
target_sources_ifdef(CONFIG_ZMK_SLEEP app PRIVATE src/power.c)
+target_sources(app PRIVATE src/activity.c)
target_sources(app PRIVATE src/kscan.c)
target_sources(app PRIVATE src/matrix_transform.c)
target_sources(app PRIVATE src/hid.c)
target_sources(app PRIVATE src/sensors.c)
target_sources(app PRIVATE src/event_manager.c)
target_sources_ifdef(CONFIG_ZMK_EXT_POWER app PRIVATE src/ext_power_generic.c)
+target_sources(app PRIVATE src/events/activity_state_changed.c)
target_sources(app PRIVATE src/events/position_state_changed.c)
target_sources(app PRIVATE src/events/layer_state_changed.c)
target_sources(app PRIVATE src/events/keycode_state_changed.c)
diff --git a/app/Kconfig b/app/Kconfig
index fc7f289..ec043bf 100644
--- a/app/Kconfig
+++ b/app/Kconfig
@@ -228,6 +228,10 @@ endmenu
menu "Power Management"
+config ZMK_IDLE_TIMEOUT
+ int "Milliseconds of inactivity before entering idle state (OLED shutoff, etc)"
+ default 30000
+
config ZMK_SLEEP
bool "Enable deep sleep support"
imply USB
@@ -242,7 +246,7 @@ choice SYS_PM_POLICY
endchoice
config ZMK_IDLE_SLEEP_TIMEOUT
- int "Milliseconds to wait to sleep when going idle"
+ int "Milliseconds of inactivity before entering deep sleep"
default 900000
#ZMK_SLEEP
diff --git a/app/include/zmk/activity.h b/app/include/zmk/activity.h
new file mode 100644
index 0000000..9c858b1
--- /dev/null
+++ b/app/include/zmk/activity.h
@@ -0,0 +1,11 @@
+/*
+ * Copyright (c) 2020 The ZMK Contributors
+ *
+ * SPDX-License-Identifier: MIT
+ */
+
+#pragma once
+
+enum zmk_activity_state { ZMK_ACTIVITY_ACTIVE, ZMK_ACTIVITY_IDLE, ZMK_ACTIVITY_SLEEP };
+
+enum zmk_activity_state zmk_activity_get_state(); \ No newline at end of file
diff --git a/app/include/zmk/events/activity-state-changed.h b/app/include/zmk/events/activity-state-changed.h
new file mode 100644
index 0000000..cd4c618
--- /dev/null
+++ b/app/include/zmk/events/activity-state-changed.h
@@ -0,0 +1,26 @@
+/*
+ * Copyright (c) 2020 The ZMK Contributors
+ *
+ * SPDX-License-Identifier: MIT
+ */
+
+#pragma once
+
+#include <zephyr.h>
+#include <zmk/event-manager.h>
+#include <zmk/activity.h>
+
+struct activity_state_changed {
+ struct zmk_event_header header;
+ enum zmk_activity_state state;
+};
+
+ZMK_EVENT_DECLARE(activity_state_changed);
+
+static inline struct activity_state_changed *
+create_activity_state_changed(enum zmk_activity_state state) {
+ struct activity_state_changed *ev = new_activity_state_changed();
+ ev->state = state;
+
+ return ev;
+} \ No newline at end of file
diff --git a/app/src/activity.c b/app/src/activity.c
new file mode 100644
index 0000000..c441fa9
--- /dev/null
+++ b/app/src/activity.c
@@ -0,0 +1,80 @@
+/*
+ * Copyright (c) 2020 The ZMK Contributors
+ *
+ * SPDX-License-Identifier: MIT
+ */
+
+#include <device.h>
+#include <init.h>
+#include <kernel.h>
+
+#include <logging/log.h>
+
+LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL);
+
+#include <zmk/event-manager.h>
+#include <zmk/events/activity-state-changed.h>
+#include <zmk/events/position-state-changed.h>
+#include <zmk/events/sensor-event.h>
+
+#include <zmk/activity.h>
+
+static enum zmk_activity_state activity_state;
+
+static uint32_t activity_last_uptime;
+
+#define MAX_IDLE_MS CONFIG_ZMK_IDLE_TIMEOUT
+
+#if IS_ENABLED(CONFIG_ZMK_SLEEP)
+#define MAX_SLEEP_MS CONFIG_ZMK_IDLE_SLEEP_TIMEOUT
+#endif
+
+int raise_event() { return ZMK_EVENT_RAISE(create_activity_state_changed(activity_state)); }
+
+int set_state(enum zmk_activity_state state) {
+ if (activity_state == state)
+ return 0;
+
+ activity_state = state;
+ return raise_event();
+}
+
+enum zmk_activity_state zmk_activity_get_state() { return activity_state; }
+
+int activity_event_listener(const struct zmk_event_header *eh) {
+ activity_last_uptime = k_uptime_get();
+
+ return 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);
+ } else
+#endif /* IS_ENABLED(CONFIG_ZMK_SLEEP) */
+ if (inactive_time > MAX_IDLE_MS) {
+ set_state(ZMK_ACTIVITY_IDLE);
+ }
+}
+
+K_WORK_DEFINE(activity_work, activity_work_handler);
+
+void activity_expiry_function() { k_work_submit(&activity_work); }
+
+K_TIMER_DEFINE(activity_timer, activity_expiry_function, NULL);
+
+int activity_init() {
+ activity_last_uptime = k_uptime_get();
+
+ k_timer_start(&activity_timer, K_SECONDS(1), K_SECONDS(1));
+ return 0;
+}
+
+ZMK_LISTENER(activity, activity_event_listener);
+ZMK_SUBSCRIPTION(activity, position_state_changed);
+ZMK_SUBSCRIPTION(activity, sensor_event);
+
+SYS_INIT(activity_init, APPLICATION, CONFIG_APPLICATION_INIT_PRIORITY);
diff --git a/app/src/events/activity_state_changed.c b/app/src/events/activity_state_changed.c
new file mode 100644
index 0000000..47aefea
--- /dev/null
+++ b/app/src/events/activity_state_changed.c
@@ -0,0 +1,10 @@
+/*
+ * Copyright (c) 2020 The ZMK Contributors
+ *
+ * SPDX-License-Identifier: MIT
+ */
+
+#include <kernel.h>
+#include <zmk/events/activity-state-changed.h>
+
+ZMK_EVENT_IMPL(activity_state_changed); \ No newline at end of file
diff --git a/app/src/power.c b/app/src/power.c
index 2330a3e..d014a52 100644
--- a/app/src/power.c
+++ b/app/src/power.c
@@ -13,13 +13,7 @@
LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL);
#include <zmk/usb.h>
-#include <zmk/event-manager.h>
-#include <zmk/events/position-state-changed.h>
-#include <zmk/events/sensor-event.h>
-
-static uint32_t power_last_uptime;
-
-#define MAX_IDLE_MS CONFIG_ZMK_IDLE_SLEEP_TIMEOUT
+#include <zmk/activity.h>
bool is_usb_power_present() {
#ifdef CONFIG_USB
@@ -32,9 +26,7 @@ bool is_usb_power_present() {
enum power_states sys_pm_policy_next_state(int32_t ticks) {
#ifdef CONFIG_SYS_POWER_DEEP_SLEEP_STATES
#ifdef CONFIG_HAS_SYS_POWER_STATE_DEEP_SLEEP_1
- int32_t current = k_uptime_get();
- if (power_last_uptime > 0 && !is_usb_power_present() &&
- current - power_last_uptime > MAX_IDLE_MS) {
+ if (zmk_activity_get_state() == ZMK_ACTIVITY_SLEEP && !is_usb_power_present()) {
return SYS_POWER_STATE_DEEP_SLEEP_1;
}
#endif /* CONFIG_HAS_SYS_POWER_STATE_DEEP_SLEEP_1 */
@@ -42,21 +34,3 @@ enum power_states sys_pm_policy_next_state(int32_t ticks) {
return SYS_POWER_STATE_ACTIVE;
}
-
-int power_event_listener(const struct zmk_event_header *eh) {
- power_last_uptime = k_uptime_get();
-
- return 0;
-}
-
-int power_init() {
- power_last_uptime = k_uptime_get();
-
- return 0;
-}
-
-ZMK_LISTENER(power, power_event_listener);
-ZMK_SUBSCRIPTION(power, position_state_changed);
-ZMK_SUBSCRIPTION(power, sensor_event);
-
-SYS_INIT(power_init, APPLICATION, CONFIG_APPLICATION_INIT_PRIORITY); \ No newline at end of file