From 4c6a161a06aa82e0af6a49db20813d07488af2ee Mon Sep 17 00:00:00 2001 From: XiNGRZ Date: Sun, 15 Jan 2023 00:07:04 +0800 Subject: [PATCH] dynamic: Do power management on Knob --- .../arm/hw75_dynamic/app/CMakeLists.txt | 3 + config/boards/arm/hw75_dynamic/app/knob_app.c | 62 +++++++++++++++++++ config/drivers/sensor/knob/lib/knob.c | 10 +-- 3 files changed, 67 insertions(+), 8 deletions(-) create mode 100644 config/boards/arm/hw75_dynamic/app/knob_app.c diff --git a/config/boards/arm/hw75_dynamic/app/CMakeLists.txt b/config/boards/arm/hw75_dynamic/app/CMakeLists.txt index 27619911..2a8211c5 100644 --- a/config/boards/arm/hw75_dynamic/app/CMakeLists.txt +++ b/config/boards/arm/hw75_dynamic/app/CMakeLists.txt @@ -9,3 +9,6 @@ zephyr_library_include_directories(${ZEPHYR_BASE}/lib/gui/lvgl) zephyr_library_include_directories(${CMAKE_SOURCE_DIR}/include) zephyr_library_sources_ifdef(CONFIG_ZMK_DISPLAY_STATUS_SCREEN_CUSTOM status_screen.c) zephyr_library_sources_ifdef(CONFIG_ZMK_DISPLAY_STATUS_SCREEN_CUSTOM widgets/layer_status.c) + +zephyr_library_include_directories_ifdef(CONFIG_KNOB ${ZMK_CONFIG}/drivers/sensor/knob/include) +zephyr_library_sources_ifdef(CONFIG_KNOB knob_app.c) diff --git a/config/boards/arm/hw75_dynamic/app/knob_app.c b/config/boards/arm/hw75_dynamic/app/knob_app.c new file mode 100644 index 00000000..6409eeec --- /dev/null +++ b/config/boards/arm/hw75_dynamic/app/knob_app.c @@ -0,0 +1,62 @@ +/* + * Copyright (c) 2022-2023 XiNGRZ + * SPDX-License-Identifier: MIT + */ + +#include +#include + +#include +LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL); + +#include + +#include +#include +#include +#include +#include + +static const struct device *knob; + +static int knob_app_init(const struct device *dev) +{ + ARG_UNUSED(dev); + + knob = device_get_binding("KNOB"); + if (knob) { + LOG_INF("Found Knob device"); + } else { + LOG_ERR("Knob device not found"); + return -EINVAL; + } + + knob_set_mode(knob, KNOB_ENCODER); + knob_set_encoder_report(knob, true); + + return 0; +} + +static int knob_app_event_listener(const zmk_event_t *eh) +{ + bool active, hid_ready; + + if (!knob) { + return -ENODEV; + } + + if (as_zmk_activity_state_changed(eh) || as_zmk_usb_conn_state_changed(eh)) { + active = zmk_activity_get_state() == ZMK_ACTIVITY_ACTIVE; + hid_ready = zmk_usb_is_hid_ready(); + knob_set_enable(knob, active && hid_ready); + return 0; + } + + return -ENOTSUP; +} + +ZMK_LISTENER(knob_app, knob_app_event_listener); +ZMK_SUBSCRIPTION(knob_app, zmk_activity_state_changed); +ZMK_SUBSCRIPTION(knob_app, zmk_usb_conn_state_changed); + +SYS_INIT(knob_app_init, APPLICATION, CONFIG_APPLICATION_INIT_PRIORITY); diff --git a/config/drivers/sensor/knob/lib/knob.c b/config/drivers/sensor/knob/lib/knob.c index c3d3ab23..a3bf2ce7 100644 --- a/config/drivers/sensor/knob/lib/knob.c +++ b/config/drivers/sensor/knob/lib/knob.c @@ -190,7 +190,7 @@ void knob_set_mode(const struct device *dev, enum knob_mode mode) void knob_set_enable(const struct device *dev, bool enable) { struct knob_data *data = dev->data; - motor_set_enable(&data->motor, enable); + motor_set_enable(&data->motor, enable && data->mode != KNOB_DISABLE); } void knob_set_encoder_report(const struct device *dev, bool report) @@ -325,16 +325,10 @@ int knob_init(const struct device *dev) pid_set(&data->motor.pid_velocity, 0.1f, 0.0f, 0.0f); pid_set(&data->motor.pid_angle, 80.0f, 0.0f, 0.7f); -#if 1 if (!knob_calibrate_auto(dev)) { return -EIO; } - knob_set_enable(dev, true); - knob_set_encoder_report(dev, true); - knob_set_mode(dev, KNOB_ENCODER); -#endif - k_thread_create(&data->thread, data->thread_stack, CONFIG_KNOB_THREAD_STACK_SIZE, (k_thread_entry_t)knob_thread, (void *)dev, 0, NULL, K_PRIO_COOP(CONFIG_KNOB_THREAD_PRIORITY), 0, K_NO_WAIT); @@ -347,7 +341,7 @@ int knob_init(const struct device *dev) .mode = KNOB_DISABLE, \ .position_min = 3.3f, \ .position_max = 5.1f, \ - .encoder_report = true, \ + .encoder_report = false, \ .encoder_ppr = DT_INST_PROP_OR(n, ppr, 24), \ }; \ \