Working encoder detection.

This commit is contained in:
Pete Johanson 2020-07-21 13:16:15 -04:00
parent b6b982fcef
commit 7da9a1039f
2 changed files with 14 additions and 3 deletions

View file

@ -18,6 +18,13 @@
LOG_MODULE_REGISTER(EC11, CONFIG_SENSOR_LOG_LEVEL); LOG_MODULE_REGISTER(EC11, CONFIG_SENSOR_LOG_LEVEL);
static int ec11_get_ab_state(struct device *dev)
{
struct ec11_data *drv_data = dev->driver_data;
const struct ec11_config *drv_cfg = dev->config_info;
return (gpio_pin_get(drv_data->a, drv_cfg->a_pin) << 1) | gpio_pin_get(drv_data->b, drv_cfg->b_pin);
}
static int ec11_sample_fetch(struct device *dev, enum sensor_channel chan) static int ec11_sample_fetch(struct device *dev, enum sensor_channel chan)
{ {
@ -28,15 +35,15 @@ static int ec11_sample_fetch(struct device *dev, enum sensor_channel chan)
__ASSERT_NO_MSG(chan == SENSOR_CHAN_ALL || chan == SENSOR_CHAN_ROTATION); __ASSERT_NO_MSG(chan == SENSOR_CHAN_ALL || chan == SENSOR_CHAN_ROTATION);
val = (gpio_pin_get(drv_data->a, drv_cfg->a_pin) << 1) | gpio_pin_get(drv_data->b, drv_cfg->b_pin); val = ec11_get_ab_state(dev);
LOG_DBG("prev: %d, new: %d", drv_data->ab_state, val); LOG_DBG("prev: %d, new: %d", drv_data->ab_state, val);
switch(val | (drv_data->ab_state << 2)) { switch(val | (drv_data->ab_state << 2)) {
case 0b0001: case 0b0111: case 0b1110: case 0b1000: case 0b0010: case 0b0100: case 0b1101: case 0b1011:
delta = 1; delta = 1;
break; break;
case 0b0010: case 0b0100: case 0b1101: case 0b1011: case 0b0001: case 0b0111: case 0b1110: case 0b1000:
delta = -1; delta = -1;
break; break;
default: default:
@ -106,6 +113,8 @@ int ec11_init(struct device *dev)
} }
#endif #endif
drv_data->ab_state = ec11_get_ab_state(dev);
return 0; return 0;
} }

View file

@ -39,6 +39,8 @@ void encoder_change(struct device *dev, struct sensor_trigger *trigger)
LOG_DBG("Failed to get the sample for EC11 %d", err); LOG_DBG("Failed to get the sample for EC11 %d", err);
return; return;
} }
LOG_DBG("val1 %d val2 %d", value.val1, value.val2);
} }
void init_sensor() void init_sensor()