Skip to content

Commit

Permalink
knob: Make profiles configurable via DTS
Browse files Browse the repository at this point in the history
  • Loading branch information
xingrz committed Oct 20, 2023
1 parent 0da16bd commit ac52685
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 22 deletions.
2 changes: 2 additions & 0 deletions config/boards/arm/hw75_dynamic/hw75_dynamic.dts
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@
torque-limit-mv = <1500>;
velocity-pid = <50 0 0>;
angle-pid = <100000 0 3500>;
minimal-movement-deg = <20>;
};

profile_damped: damped@4 {
Expand Down Expand Up @@ -135,6 +136,7 @@
torque-limit-mv = <1500>;
velocity-pid = <50 0 0>;
angle-pid = <100000 0 3500>;
on-off-distance-deg = <80>;
};
};

Expand Down
4 changes: 2 additions & 2 deletions config/drivers/sensor/knob/include/knob/drivers/profile.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@
((float)DT_INST_PROP_BY_IDX(0, prop, 2) / 1000.0f)

#define KNOB_PROFILE_HAS_VELOCITY_PID DT_INST_NODE_HAS_PROP(0, velocity_pid)
#define KNOB_PROFILE_VELOCITY_PID Z_KNOB_PROFILE_PID(velocity_pid)
#define KNOB_PROFILE_VELOCITY_PID Z_KNOB_PROFILE_PID(velocity_pid)

#define KNOB_PROFILE_HAS_ANGLE_PID DT_INST_NODE_HAS_PROP(0, angle_pid)
#define KNOB_PROFILE_ANGLE_PID Z_KNOB_PROFILE_PID(angle_pid)
#define KNOB_PROFILE_ANGLE_PID Z_KNOB_PROFILE_PID(angle_pid)

#ifdef __cplusplus
extern "C" {
Expand Down
2 changes: 1 addition & 1 deletion config/drivers/sensor/knob/lib/include/knob/math.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,4 +67,4 @@ static inline float norm_rad(float radian)
/**
* @brief Degree to radian
*/
#define deg_to_rad(deg) (deg / 360.0f * PI2)
#define deg_to_rad(deg) ((float)deg / 360.0f * PI2)
22 changes: 14 additions & 8 deletions config/drivers/sensor/knob/profile/spring.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,23 @@
#include <zephyr/logging/log.h>
LOG_MODULE_REGISTER(knob_spring, CONFIG_ZMK_LOG_LEVEL);

#define CENTER (deg_to_rad(180.0f))
#define UP (deg_to_rad(160.0f))
#define DN (deg_to_rad(200.0f))

struct knob_spring_config {
KNOB_PROFILE_CFG_ROM;
uint32_t minimal_movement_deg;
};

struct knob_spring_data {
float center;
float up;
float down;
int32_t value;
int32_t last_report;
};

static int knob_spring_enable(const struct device *dev)
{
const struct knob_spring_config *cfg = dev->config;
struct knob_spring_data *data = dev->data;

motor_set_torque_limit(cfg->motor, KNOB_PROFILE_TORQUE_LIMIT);

Expand All @@ -43,6 +44,10 @@ static int knob_spring_enable(const struct device *dev)
motor_set_angle_pid(cfg->motor, KNOB_PROFILE_ANGLE_PID);
#endif /* KNOB_PROFILE_HAS_ANGLE_PID */

data->center = deg_to_rad(180);
data->up = data->center - deg_to_rad(cfg->minimal_movement_deg);
data->down = data->center + deg_to_rad(cfg->minimal_movement_deg);

return 0;
}

Expand All @@ -61,16 +66,16 @@ static int knob_spring_tick(const struct device *dev, struct motor_control *mc)
ARG_UNUSED(mc);

float p = knob_get_position(cfg->knob);
if (p < UP) {
if (p < data->up) {
data->value = 1;
} else if (p > DN) {
} else if (p > data->down) {
data->value = -1;
} else {
data->value = 0;
}

mc->mode = ANGLE;
mc->target = CENTER;
mc->target = data->center;

return 0;
}
Expand Down Expand Up @@ -106,7 +111,8 @@ static const struct knob_profile_api knob_spring_api = {

static struct knob_spring_data knob_spring_data;

static const struct knob_spring_config knob_spring_cfg = { KNOB_PROFILE_CFG_INIT };
static const struct knob_spring_config knob_spring_cfg = {
.minimal_movement_deg = DT_INST_PROP(0, minimal_movement_deg), KNOB_PROFILE_CFG_INIT};

DEVICE_DT_INST_DEFINE(0, knob_spring_init, NULL, &knob_spring_data, &knob_spring_cfg, POST_KERNEL,
CONFIG_SENSOR_INIT_PRIORITY, &knob_spring_api);
31 changes: 20 additions & 11 deletions config/drivers/sensor/knob/profile/switch.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,17 @@
#include <zephyr/logging/log.h>
LOG_MODULE_REGISTER(knob_switch, CONFIG_ZMK_LOG_LEVEL);

#define CENTER (deg_to_rad(180.0f))
#define ON (deg_to_rad(140.0f))
#define OFF (deg_to_rad(220.0f))

struct knob_switch_config {
KNOB_PROFILE_CFG_ROM;
uint32_t on_off_distance_deg;
};

struct knob_switch_data {
float center;
float on;
float on_2;
float off;
float off_2;
bool state;
bool last_report;
};
Expand All @@ -44,6 +46,12 @@ static int knob_switch_enable(const struct device *dev)
motor_set_angle_pid(cfg->motor, KNOB_PROFILE_ANGLE_PID);
#endif /* KNOB_PROFILE_HAS_ANGLE_PID */

data->center = deg_to_rad(180);
data->off = data->center + deg_to_rad(cfg->on_off_distance_deg) * 0.5f;
data->off_2 = data->center + deg_to_rad(cfg->on_off_distance_deg) * 0.25f;
data->on = data->center - deg_to_rad(cfg->on_off_distance_deg) * 0.5f;
data->on_2 = data->center - deg_to_rad(cfg->on_off_distance_deg) * 0.25f;

data->state = false;

return 0;
Expand All @@ -65,14 +73,14 @@ static int knob_switch_tick(const struct device *dev, struct motor_control *mc)
mc->mode = ANGLE;

float p = knob_get_position(cfg->knob);
if (p < ON + (CENTER - ON) / 2.0f) {
mc->target = ON;
if (p < data->on_2) {
mc->target = data->on;
data->state = true;
} else if (p < OFF - (OFF - CENTER) / 2.0f) {
mc->target = CENTER + (p - CENTER) * 2.0f;
} else {
mc->target = OFF;
} else if (p > data->off_2) {
mc->target = data->off;
data->state = false;
} else {
mc->target = data->center + (p - data->center) * 2.0f;
}

return 0;
Expand Down Expand Up @@ -109,7 +117,8 @@ static const struct knob_profile_api knob_switch_api = {

static struct knob_switch_data knob_switch_data;

static const struct knob_switch_config knob_switch_cfg = { KNOB_PROFILE_CFG_INIT };
static const struct knob_switch_config knob_switch_cfg = {
.on_off_distance_deg = DT_INST_PROP(0, on_off_distance_deg), KNOB_PROFILE_CFG_INIT};

DEVICE_DT_INST_DEFINE(0, knob_switch_init, NULL, &knob_switch_data, &knob_switch_cfg, POST_KERNEL,
CONFIG_SENSOR_INIT_PRIORITY, &knob_switch_api);
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,9 @@ description: Knob spring profile
compatible: "zmk,knob-profile-spring"

include: knob-profile.yaml

properties:
minimal-movement-deg:
type: int
required: false
default: 20
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,9 @@ description: Knob switch profile
compatible: "zmk,knob-profile-switch"

include: knob-profile.yaml

properties:
on-off-distance-deg:
type: int
required: false
default: 80

0 comments on commit ac52685

Please sign in to comment.