Skip to content

Commit

Permalink
knob_app: Do calibration at dedicated thread
Browse files Browse the repository at this point in the history
So it won't hang the first draw of the screen
  • Loading branch information
xingrz committed Jan 25, 2023
1 parent 9346bf9 commit 230d446
Showing 1 changed file with 32 additions and 11 deletions.
43 changes: 32 additions & 11 deletions config/boards/arm/hw75_dynamic/app/knob_app.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,35 @@ LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL);
#include "knob_app.h"
#include "indicator_app.h"

#define KNOB_APP_THREAD_STACK_SIZE 1024
#define KNOB_APP_THREAD_PRIORITY 10

static const struct device *knob;
static const struct device *motor;

static bool motor_demo = false;

K_THREAD_STACK_DEFINE(knob_work_stack_area, KNOB_APP_THREAD_STACK_SIZE);
static struct k_work_q knob_work_q;

static void knob_app_calibrate(struct k_work *work)
{
indicator_set_bits(INDICATOR_KNOB_CALIBRATING);

int ret = motor_calibrate_auto(motor);
if (ret != 0) {
LOG_ERR("Motor is not calibrated");
return;
}

knob_set_mode(knob, KNOB_ENCODER);
knob_set_encoder_report(knob, true);

indicator_clear_bits(INDICATOR_KNOB_CALIBRATING);
}

K_WORK_DEFINE(calibrate_work, knob_app_calibrate);

static int knob_app_init(const struct device *dev)
{
ARG_UNUSED(dev);
Expand All @@ -40,24 +64,21 @@ static int knob_app_init(const struct device *dev)
return -ENODEV;
}

indicator_set_bits(INDICATOR_KNOB_CALIBRATING);

int ret = motor_calibrate_auto(motor);
if (ret != 0) {
LOG_ERR("Motor is not calibrated");
return -EIO;
}

knob_set_mode(knob, KNOB_ENCODER);
knob_set_encoder_report(knob, true);
k_work_queue_start(&knob_work_q, knob_work_stack_area,
K_THREAD_STACK_SIZEOF(knob_work_stack_area), KNOB_APP_THREAD_PRIORITY,
NULL);

indicator_clear_bits(INDICATOR_KNOB_CALIBRATING);
k_work_submit_to_queue(&knob_work_q, &calibrate_work);

return 0;
}

static void knob_app_update_motor_state(void)
{
if (!knob || !motor) {
return;
}

if (motor_is_calibrated(motor) && !motor_demo) {
knob_set_enable(knob, zmk_activity_get_state() == ZMK_ACTIVITY_ACTIVE);
}
Expand Down

0 comments on commit 230d446

Please sign in to comment.