refactor(sensors): ec11 rotation sensor value in degrees.
* Add new `steps` property to the `aips,ec11` binding, to make the driver properly report degrees in the rotation delta channel. * Handle old sensor values in sensor rotate behavior.
This commit is contained in:
parent
2244bd3d81
commit
295ed83409
6 changed files with 38 additions and 9 deletions
|
@ -3,6 +3,8 @@
|
||||||
|
|
||||||
menuconfig EC11
|
menuconfig EC11
|
||||||
bool "EC11 Incremental Encoder Sensor"
|
bool "EC11 Incremental Encoder Sensor"
|
||||||
|
default y
|
||||||
|
depends on DT_HAS_ALPS_EC11_ENABLED
|
||||||
depends on GPIO
|
depends on GPIO
|
||||||
help
|
help
|
||||||
Enable driver for EC11 incremental encoder sensors.
|
Enable driver for EC11 incremental encoder sensors.
|
||||||
|
|
|
@ -16,6 +16,8 @@
|
||||||
|
|
||||||
#include "ec11.h"
|
#include "ec11.h"
|
||||||
|
|
||||||
|
#define FULL_ROTATION 360
|
||||||
|
|
||||||
LOG_MODULE_REGISTER(EC11, CONFIG_SENSOR_LOG_LEVEL);
|
LOG_MODULE_REGISTER(EC11, CONFIG_SENSOR_LOG_LEVEL);
|
||||||
|
|
||||||
static int ec11_get_ab_state(const struct device *dev) {
|
static int ec11_get_ab_state(const struct device *dev) {
|
||||||
|
@ -59,9 +61,14 @@ static int ec11_sample_fetch(const struct device *dev, enum sensor_channel chan)
|
||||||
drv_data->pulses += delta;
|
drv_data->pulses += delta;
|
||||||
drv_data->ab_state = val;
|
drv_data->ab_state = val;
|
||||||
|
|
||||||
|
// TODO: Temporary code for backwards compatibility to support
|
||||||
|
// the sensor channel rotation reporting *ticks* instead of delta of degrees.
|
||||||
|
// REMOVE ME
|
||||||
|
if (drv_cfg->steps == 0) {
|
||||||
drv_data->ticks = drv_data->pulses / drv_cfg->resolution;
|
drv_data->ticks = drv_data->pulses / drv_cfg->resolution;
|
||||||
drv_data->delta = delta;
|
drv_data->delta = delta;
|
||||||
drv_data->pulses %= drv_cfg->resolution;
|
drv_data->pulses %= drv_cfg->resolution;
|
||||||
|
}
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
@ -69,13 +76,26 @@ static int ec11_sample_fetch(const struct device *dev, enum sensor_channel chan)
|
||||||
static int ec11_channel_get(const struct device *dev, enum sensor_channel chan,
|
static int ec11_channel_get(const struct device *dev, enum sensor_channel chan,
|
||||||
struct sensor_value *val) {
|
struct sensor_value *val) {
|
||||||
struct ec11_data *drv_data = dev->data;
|
struct ec11_data *drv_data = dev->data;
|
||||||
|
const struct ec11_config *drv_cfg = dev->config;
|
||||||
|
int32_t pulses = drv_data->pulses;
|
||||||
|
|
||||||
if (chan != SENSOR_CHAN_ROTATION) {
|
if (chan != SENSOR_CHAN_ROTATION) {
|
||||||
return -ENOTSUP;
|
return -ENOTSUP;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
drv_data->pulses = 0;
|
||||||
|
|
||||||
|
if (drv_cfg->steps > 0) {
|
||||||
|
val->val1 = (pulses * FULL_ROTATION) / drv_cfg->steps;
|
||||||
|
val->val2 = (pulses * FULL_ROTATION) % drv_cfg->steps;
|
||||||
|
if (val->val2 != 0) {
|
||||||
|
val->val2 *= 1000000;
|
||||||
|
val->val2 /= drv_cfg->steps;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
val->val1 = drv_data->ticks;
|
val->val1 = drv_data->ticks;
|
||||||
val->val2 = drv_data->delta;
|
val->val2 = drv_data->delta;
|
||||||
|
}
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
@ -132,7 +152,8 @@ int ec11_init(const struct device *dev) {
|
||||||
const struct ec11_config ec11_cfg_##n = { \
|
const struct ec11_config ec11_cfg_##n = { \
|
||||||
.a = GPIO_DT_SPEC_INST_GET(n, a_gpios), \
|
.a = GPIO_DT_SPEC_INST_GET(n, a_gpios), \
|
||||||
.b = GPIO_DT_SPEC_INST_GET(n, b_gpios), \
|
.b = GPIO_DT_SPEC_INST_GET(n, b_gpios), \
|
||||||
COND_CODE_0(DT_INST_NODE_HAS_PROP(n, resolution), (1), (DT_INST_PROP(n, resolution))), \
|
.resolution = DT_INST_PROP_OR(n, resolution, 1), \
|
||||||
|
.steps = DT_INST_PROP_OR(n, steps, 0), \
|
||||||
}; \
|
}; \
|
||||||
DEVICE_DT_INST_DEFINE(n, ec11_init, NULL, &ec11_data_##n, &ec11_cfg_##n, POST_KERNEL, \
|
DEVICE_DT_INST_DEFINE(n, ec11_init, NULL, &ec11_data_##n, &ec11_cfg_##n, POST_KERNEL, \
|
||||||
CONFIG_SENSOR_INIT_PRIORITY, &ec11_driver_api);
|
CONFIG_SENSOR_INIT_PRIORITY, &ec11_driver_api);
|
||||||
|
|
|
@ -14,6 +14,7 @@ struct ec11_config {
|
||||||
const struct gpio_dt_spec a;
|
const struct gpio_dt_spec a;
|
||||||
const struct gpio_dt_spec b;
|
const struct gpio_dt_spec b;
|
||||||
|
|
||||||
|
const uint16_t steps;
|
||||||
const uint8_t resolution;
|
const uint8_t resolution;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -18,4 +18,9 @@ properties:
|
||||||
resolution:
|
resolution:
|
||||||
type: int
|
type: int
|
||||||
description: Number of pulses per tick
|
description: Number of pulses per tick
|
||||||
|
deprecated: true
|
||||||
|
required: false
|
||||||
|
steps:
|
||||||
|
type: int
|
||||||
|
description: Number of pulses in one full rotation
|
||||||
required: false
|
required: false
|
||||||
|
|
|
@ -6,11 +6,11 @@
|
||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
|
#include <zephyr/device.h>
|
||||||
#include <zephyr/drivers/sensor.h>
|
#include <zephyr/drivers/sensor.h>
|
||||||
|
|
||||||
#include <zmk/event_manager.h>
|
#include <zmk/event_manager.h>
|
||||||
#include <zmk/sensors.h>
|
#include <zmk/sensors.h>
|
||||||
#include <device.h>
|
|
||||||
|
|
||||||
// TODO: Move to Kconfig when we need more than one channel
|
// TODO: Move to Kconfig when we need more than one channel
|
||||||
#define ZMK_SENSOR_EVENT_MAX_CHANNELS 1
|
#define ZMK_SENSOR_EVENT_MAX_CHANNELS 1
|
||||||
|
|
|
@ -6,7 +6,7 @@
|
||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <drivers/sensor.h>
|
#include <zephyr/drivers/sensor.h>
|
||||||
|
|
||||||
#define _SENSOR_CHILD_LEN(node) 1 +
|
#define _SENSOR_CHILD_LEN(node) 1 +
|
||||||
#define ZMK_KEYMAP_SENSORS_NODE DT_INST(0, zmk_keymap_sensors)
|
#define ZMK_KEYMAP_SENSORS_NODE DT_INST(0, zmk_keymap_sensors)
|
||||||
|
|
Loading…
Add table
Reference in a new issue