Skip to content

Commit

Permalink
dynamic: Do power management on Knob
Browse files Browse the repository at this point in the history
  • Loading branch information
xingrz committed Jan 14, 2023
1 parent 07cf8fa commit 4c6a161
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 8 deletions.
3 changes: 3 additions & 0 deletions config/boards/arm/hw75_dynamic/app/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
62 changes: 62 additions & 0 deletions config/boards/arm/hw75_dynamic/app/knob_app.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
* Copyright (c) 2022-2023 XiNGRZ
* SPDX-License-Identifier: MIT
*/

#include <kernel.h>
#include <device.h>

#include <logging/log.h>
LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL);

#include <knob/drivers/knob.h>

#include <zmk/activity.h>
#include <zmk/usb.h>
#include <zmk/event_manager.h>
#include <zmk/events/activity_state_changed.h>
#include <zmk/events/usb_conn_state_changed.h>

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);
10 changes: 2 additions & 8 deletions config/drivers/sensor/knob/lib/knob.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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);
Expand All @@ -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), \
}; \
\
Expand Down

0 comments on commit 4c6a161

Please sign in to comment.