Skip to content

Commit ac52685

Browse files
committed
knob: Make profiles configurable via DTS
1 parent 0da16bd commit ac52685

File tree

7 files changed

+51
-22
lines changed

7 files changed

+51
-22
lines changed

config/boards/arm/hw75_dynamic/hw75_dynamic.dts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@
104104
torque-limit-mv = <1500>;
105105
velocity-pid = <50 0 0>;
106106
angle-pid = <100000 0 3500>;
107+
minimal-movement-deg = <20>;
107108
};
108109

109110
profile_damped: damped@4 {
@@ -135,6 +136,7 @@
135136
torque-limit-mv = <1500>;
136137
velocity-pid = <50 0 0>;
137138
angle-pid = <100000 0 3500>;
139+
on-off-distance-deg = <80>;
138140
};
139141
};
140142

config/drivers/sensor/knob/include/knob/drivers/profile.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,10 @@
2525
((float)DT_INST_PROP_BY_IDX(0, prop, 2) / 1000.0f)
2626

2727
#define KNOB_PROFILE_HAS_VELOCITY_PID DT_INST_NODE_HAS_PROP(0, velocity_pid)
28-
#define KNOB_PROFILE_VELOCITY_PID Z_KNOB_PROFILE_PID(velocity_pid)
28+
#define KNOB_PROFILE_VELOCITY_PID Z_KNOB_PROFILE_PID(velocity_pid)
2929

3030
#define KNOB_PROFILE_HAS_ANGLE_PID DT_INST_NODE_HAS_PROP(0, angle_pid)
31-
#define KNOB_PROFILE_ANGLE_PID Z_KNOB_PROFILE_PID(angle_pid)
31+
#define KNOB_PROFILE_ANGLE_PID Z_KNOB_PROFILE_PID(angle_pid)
3232

3333
#ifdef __cplusplus
3434
extern "C" {

config/drivers/sensor/knob/lib/include/knob/math.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,4 +67,4 @@ static inline float norm_rad(float radian)
6767
/**
6868
* @brief Degree to radian
6969
*/
70-
#define deg_to_rad(deg) (deg / 360.0f * PI2)
70+
#define deg_to_rad(deg) ((float)deg / 360.0f * PI2)

config/drivers/sensor/knob/profile/spring.c

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,22 +16,23 @@
1616
#include <zephyr/logging/log.h>
1717
LOG_MODULE_REGISTER(knob_spring, CONFIG_ZMK_LOG_LEVEL);
1818

19-
#define CENTER (deg_to_rad(180.0f))
20-
#define UP (deg_to_rad(160.0f))
21-
#define DN (deg_to_rad(200.0f))
22-
2319
struct knob_spring_config {
2420
KNOB_PROFILE_CFG_ROM;
21+
uint32_t minimal_movement_deg;
2522
};
2623

2724
struct knob_spring_data {
25+
float center;
26+
float up;
27+
float down;
2828
int32_t value;
2929
int32_t last_report;
3030
};
3131

3232
static int knob_spring_enable(const struct device *dev)
3333
{
3434
const struct knob_spring_config *cfg = dev->config;
35+
struct knob_spring_data *data = dev->data;
3536

3637
motor_set_torque_limit(cfg->motor, KNOB_PROFILE_TORQUE_LIMIT);
3738

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

47+
data->center = deg_to_rad(180);
48+
data->up = data->center - deg_to_rad(cfg->minimal_movement_deg);
49+
data->down = data->center + deg_to_rad(cfg->minimal_movement_deg);
50+
4651
return 0;
4752
}
4853

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

6368
float p = knob_get_position(cfg->knob);
64-
if (p < UP) {
69+
if (p < data->up) {
6570
data->value = 1;
66-
} else if (p > DN) {
71+
} else if (p > data->down) {
6772
data->value = -1;
6873
} else {
6974
data->value = 0;
7075
}
7176

7277
mc->mode = ANGLE;
73-
mc->target = CENTER;
78+
mc->target = data->center;
7479

7580
return 0;
7681
}
@@ -106,7 +111,8 @@ static const struct knob_profile_api knob_spring_api = {
106111

107112
static struct knob_spring_data knob_spring_data;
108113

109-
static const struct knob_spring_config knob_spring_cfg = { KNOB_PROFILE_CFG_INIT };
114+
static const struct knob_spring_config knob_spring_cfg = {
115+
.minimal_movement_deg = DT_INST_PROP(0, minimal_movement_deg), KNOB_PROFILE_CFG_INIT};
110116

111117
DEVICE_DT_INST_DEFINE(0, knob_spring_init, NULL, &knob_spring_data, &knob_spring_cfg, POST_KERNEL,
112118
CONFIG_SENSOR_INIT_PRIORITY, &knob_spring_api);

config/drivers/sensor/knob/profile/switch.c

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,17 @@
1616
#include <zephyr/logging/log.h>
1717
LOG_MODULE_REGISTER(knob_switch, CONFIG_ZMK_LOG_LEVEL);
1818

19-
#define CENTER (deg_to_rad(180.0f))
20-
#define ON (deg_to_rad(140.0f))
21-
#define OFF (deg_to_rad(220.0f))
22-
2319
struct knob_switch_config {
2420
KNOB_PROFILE_CFG_ROM;
21+
uint32_t on_off_distance_deg;
2522
};
2623

2724
struct knob_switch_data {
25+
float center;
26+
float on;
27+
float on_2;
28+
float off;
29+
float off_2;
2830
bool state;
2931
bool last_report;
3032
};
@@ -44,6 +46,12 @@ static int knob_switch_enable(const struct device *dev)
4446
motor_set_angle_pid(cfg->motor, KNOB_PROFILE_ANGLE_PID);
4547
#endif /* KNOB_PROFILE_HAS_ANGLE_PID */
4648

49+
data->center = deg_to_rad(180);
50+
data->off = data->center + deg_to_rad(cfg->on_off_distance_deg) * 0.5f;
51+
data->off_2 = data->center + deg_to_rad(cfg->on_off_distance_deg) * 0.25f;
52+
data->on = data->center - deg_to_rad(cfg->on_off_distance_deg) * 0.5f;
53+
data->on_2 = data->center - deg_to_rad(cfg->on_off_distance_deg) * 0.25f;
54+
4755
data->state = false;
4856

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

6775
float p = knob_get_position(cfg->knob);
68-
if (p < ON + (CENTER - ON) / 2.0f) {
69-
mc->target = ON;
76+
if (p < data->on_2) {
77+
mc->target = data->on;
7078
data->state = true;
71-
} else if (p < OFF - (OFF - CENTER) / 2.0f) {
72-
mc->target = CENTER + (p - CENTER) * 2.0f;
73-
} else {
74-
mc->target = OFF;
79+
} else if (p > data->off_2) {
80+
mc->target = data->off;
7581
data->state = false;
82+
} else {
83+
mc->target = data->center + (p - data->center) * 2.0f;
7684
}
7785

7886
return 0;
@@ -109,7 +117,8 @@ static const struct knob_profile_api knob_switch_api = {
109117

110118
static struct knob_switch_data knob_switch_data;
111119

112-
static const struct knob_switch_config knob_switch_cfg = { KNOB_PROFILE_CFG_INIT };
120+
static const struct knob_switch_config knob_switch_cfg = {
121+
.on_off_distance_deg = DT_INST_PROP(0, on_off_distance_deg), KNOB_PROFILE_CFG_INIT};
113122

114123
DEVICE_DT_INST_DEFINE(0, knob_switch_init, NULL, &knob_switch_data, &knob_switch_cfg, POST_KERNEL,
115124
CONFIG_SENSOR_INIT_PRIORITY, &knob_switch_api);

config/dts/bindings/sensor/knob/profile/zmk,knob-profile-spring.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,9 @@ description: Knob spring profile
66
compatible: "zmk,knob-profile-spring"
77

88
include: knob-profile.yaml
9+
10+
properties:
11+
minimal-movement-deg:
12+
type: int
13+
required: false
14+
default: 20

config/dts/bindings/sensor/knob/profile/zmk,knob-profile-switch.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,9 @@ description: Knob switch profile
66
compatible: "zmk,knob-profile-switch"
77

88
include: knob-profile.yaml
9+
10+
properties:
11+
on-off-distance-deg:
12+
type: int
13+
required: false
14+
default: 80

0 commit comments

Comments
 (0)