[RFC] Sensor API for devices with multiple channels of the same type

Bogdan Davidoaia


The current Zephyr Sensor API assumes that devices have a single channel of a particular type. Although this may be the most common case for most devices, it is not a rule (e.g. SI1153 can expose 4 proximity channels to be used for gesture recognition).

An improvement to the API to support multiple channels of the same type is needed. The proposal contain the following:

int sensor_channel_count(struct device *dev, enum sensor_channel chan) - new function; returns number of channels of a particular type supported by the driver

int sensor_channel_get_by_idx(struct device *dev, enum sensor_channel chan, int chan_idx, struct sensor_value *val) - new function; gets the sensor value for a channel specified by type and index within that type

typedef int (*sensor_channel_get_t)(struct device *, enum sensor_channel chan, int chan_idx, struct sensor_value *val) - update previous sensor_channel_get_t to also accept channel index

int sensor_channel_get(struct device *, enum sensor_channel chan, struct sensor_value *val) - old function; will return values from all channels of the specified type; for sensors with a single channel per type, the behavior will be the same as before

Regarding the implementation of sensor_channel_count, right now I can think of 2 approaches:

1. Add a sensor_channel_count function to the the driver API, and have each driver write a function that returns the right value for each channel type it supports.

2. Add a .sensor.info section in which each sensor keeps a structure containing an array of (channel_type, count). sensor_channel_count would be implemented similar to device_get_binding, as loop for finding the right structure in the .sensor.info section would be requrired.

With approach 2 the driver only needs to declare structure in the .sensor.info section and not write a function with if/else or switch statements for the driver API structure, however sensor_channel_count would contain 2 for statements (to iterate over sensors and channels).

I would appreciate your thoughts on the change in general and on the best way to implement sensor_channel_count.


Join devel@lists.zephyrproject.org to automatically receive all group messages.