refactor(sensors): Use "sensor index" consistently

This commit is contained in:
Peter Johanson
2023-05-16 22:27:01 -07:00
committed by Pete Johanson
parent 8b29f6d345
commit 3a91b32513
6 changed files with 40 additions and 42 deletions

View File

@@ -21,7 +21,7 @@ int zmk_behavior_sensor_rotate_common_data(struct zmk_behavior_binding *binding,
const struct sensor_value value = channel_data[0].value;
int triggers;
int sensor_position = ZMK_SENSOR_POSITION_FROM_VIRTUAL_KEY_POSITION(event.position);
int sensor_index = ZMK_SENSOR_POSITION_FROM_VIRTUAL_KEY_POSITION(event.position);
// Some funky special casing for "old encoder behavior" where ticks where reported in val2 only,
// instead of rotational degrees in val1.
@@ -29,7 +29,7 @@ int zmk_behavior_sensor_rotate_common_data(struct zmk_behavior_binding *binding,
if (value.val1 == 0) {
triggers = value.val2;
} else {
struct sensor_value remainder = data->remainder[sensor_position];
struct sensor_value remainder = data->remainder[sensor_index];
remainder.val1 += value.val1;
remainder.val2 += value.val2;
@@ -43,15 +43,15 @@ int zmk_behavior_sensor_rotate_common_data(struct zmk_behavior_binding *binding,
triggers = remainder.val1 / trigger_degrees;
remainder.val1 %= trigger_degrees;
data->remainder[sensor_position] = remainder;
data->remainder[sensor_index] = remainder;
}
LOG_DBG(
"val1: %d, val2: %d, remainder: %d/%d triggers: %d inc keycode 0x%02X dec keycode 0x%02X",
value.val1, value.val2, data->remainder[sensor_position].val1,
data->remainder[sensor_position].val2, triggers, binding->param1, binding->param2);
value.val1, value.val2, data->remainder[sensor_index].val1,
data->remainder[sensor_index].val2, triggers, binding->param1, binding->param2);
data->triggers[sensor_position] = triggers;
data->triggers[sensor_index] = triggers;
return 0;
}
@@ -62,14 +62,14 @@ int zmk_behavior_sensor_rotate_common_process(struct zmk_behavior_binding *bindi
const struct behavior_sensor_rotate_config *cfg = dev->config;
struct behavior_sensor_rotate_data *data = dev->data;
const int sensor_position = ZMK_SENSOR_POSITION_FROM_VIRTUAL_KEY_POSITION(event.position);
const int sensor_index = ZMK_SENSOR_POSITION_FROM_VIRTUAL_KEY_POSITION(event.position);
if (mode != BEHAVIOR_SENSOR_BINDING_PROCESS_MODE_TRIGGER) {
data->triggers[sensor_position] = 0;
data->triggers[sensor_index] = 0;
return 0;
}
int triggers = data->triggers[sensor_position];
int triggers = data->triggers[sensor_index];
struct zmk_behavior_binding triggered_binding;
if (triggers > 0) {

View File

@@ -252,38 +252,37 @@ int zmk_keymap_position_state_changed(uint8_t source, uint32_t position, bool pr
}
#if ZMK_KEYMAP_HAS_SENSORS
int zmk_keymap_sensor_event(uint8_t sensor_position, size_t channel_data_size,
const struct zmk_sensor_channel_data channel_data[channel_data_size],
int64_t timestamp) {
int zmk_keymap_sensor_event(uint8_t sensor_index,
const struct zmk_sensor_channel_data *channel_data,
size_t channel_data_size, int64_t timestamp) {
bool opaque_response = false;
for (int layer = ZMK_KEYMAP_LAYERS_LEN - 1; layer >= 0; layer--) {
struct zmk_behavior_binding *binding = &zmk_sensor_keymap[layer][sensor_position];
const struct device *behavior;
int ret;
struct zmk_behavior_binding *binding = &zmk_sensor_keymap[layer][sensor_index];
LOG_DBG("layer: %d sensor_position: %d, binding name: %s", layer, sensor_position,
LOG_DBG("layer: %d sensor_index: %d, binding name: %s", layer, sensor_index,
binding->behavior_dev);
behavior = device_get_binding(binding->behavior_dev);
const struct device *behavior = device_get_binding(binding->behavior_dev);
if (!behavior) {
LOG_DBG("No behavior assigned to %d on layer %d", sensor_position, layer);
LOG_DBG("No behavior assigned to %d on layer %d", sensor_index, layer);
continue;
}
struct zmk_behavior_binding_event event = {
.layer = layer,
.position = ZMK_VIRTUAL_KEY_POSITION_SENSOR(sensor_position),
.position = ZMK_VIRTUAL_KEY_POSITION_SENSOR(sensor_index),
.timestamp = timestamp,
};
ret = behavior_sensor_keymap_binding_data(
binding, event, zmk_sensors_get_config_at_position(sensor_position), channel_data_size,
int ret = behavior_sensor_keymap_binding_accept_data(
binding, event, zmk_sensors_get_config_at_index(sensor_index), channel_data_size,
channel_data);
if (ret > 0) {
LOG_DBG("behavior processing to continue to next layer");
if (ret < 0) {
LOG_WRN("behavior data accept for behavior %s returned an error (%d). Processing to "
"continue to next layer",
binding->behavior_dev, ret);
continue;
}
@@ -319,8 +318,8 @@ int keymap_listener(const zmk_event_t *eh) {
#if ZMK_KEYMAP_HAS_SENSORS
const struct zmk_sensor_event *sensor_ev;
if ((sensor_ev = as_zmk_sensor_event(eh)) != NULL) {
return zmk_keymap_sensor_event(sensor_ev->sensor_position, sensor_ev->channel_data_size,
sensor_ev->channel_data, sensor_ev->timestamp);
return zmk_keymap_sensor_event(sensor_ev->sensor_index, sensor_ev->channel_data,
sensor_ev->channel_data_size, sensor_ev->timestamp);
}
#endif /* ZMK_KEYMAP_HAS_SENSORS */

View File

@@ -19,7 +19,7 @@ LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL);
#if ZMK_KEYMAP_HAS_SENSORS
struct sensors_item_cfg {
uint8_t sensor_position;
uint8_t sensor_index;
const struct zmk_sensor_config *config;
const struct device *dev;
struct sensor_trigger trigger;
@@ -57,17 +57,17 @@ static struct sensors_item_cfg sensors[] = {LISTIFY(ZMK_KEYMAP_SENSORS_LEN, SENS
static ATOMIC_DEFINE(pending_sensors, ZMK_KEYMAP_SENSORS_LEN);
const struct zmk_sensor_config *zmk_sensors_get_config_at_position(uint8_t sensor_position) {
if (sensor_position > ARRAY_SIZE(configs)) {
const struct zmk_sensor_config *zmk_sensors_get_config_at_index(uint8_t sensor_index) {
if (sensor_index > ARRAY_SIZE(configs)) {
return NULL;
}
return &configs[sensor_position];
return &configs[sensor_index];
}
static void trigger_sensor_data_for_position(uint32_t sensor_position) {
static void trigger_sensor_data_for_position(uint32_t sensor_index) {
int err;
const struct sensors_item_cfg *item = &sensors[sensor_position];
const struct sensors_item_cfg *item = &sensors[sensor_index];
err = sensor_sample_fetch(item->dev);
if (err) {
@@ -84,7 +84,7 @@ static void trigger_sensor_data_for_position(uint32_t sensor_position) {
}
ZMK_EVENT_RAISE(new_zmk_sensor_event(
(struct zmk_sensor_event){.sensor_position = item->sensor_position,
(struct zmk_sensor_event){.sensor_index = item->sensor_index,
.channel_data = {(struct zmk_sensor_channel_data){
.value = value, .channel = item->trigger.chan}},
.timestamp = k_uptime_get()}));
@@ -122,7 +122,7 @@ static void zmk_sensors_trigger_handler(const struct device *dev,
static void zmk_sensors_init_item(uint8_t i) {
LOG_DBG("Init sensor at index %d", i);
sensors[i].sensor_position = i;
sensors[i].sensor_index = i;
if (!sensors[i].dev) {
LOG_DBG("No local device for %d", i);