Simulated sensor driver
The simulated sensor driver implements a simulated sensor that is compatible with Zephyr’s Sensors. The sensor provides readouts for predefined set of sensor channels and supports sensor triggers.
Configuration
You can define instances of the sensor simulator on Devicetree like this:
sensor-sim {
compatible = "nordic,sensor-sim";
acc-signal = "toggle";
};
The sensor driver will automatically be built if CONFIG_SENSOR=y
and any of its instances in Devicetree have status set to okay
.
Configuration of generated readouts
The algorithms used to generate simulated sensor readouts are configurable. The following sensor channels and configuration options are available:
Ambient temperature (
SENSOR_CHAN_AMBIENT_TEMP
) - The value is generated as the sum of the value of the Devicetree propertybase-temperature
a pseudo-random number between-1
and1
.Humidity (
SENSOR_CHAN_HUMIDITY
) - The value is generated as the sum of the value of the Devicetree propertybase-humidity
and a pseudo-random number between-1
and1
.Pressure (
SENSOR_CHAN_PRESS
) - The value is generated as the sum of the value of the Devicetree propertybase-pressure
and a pseudo-random number between-1
and1
.Acceleration in X, Y, and Z axes (
SENSOR_CHAN_ACCEL_X
,SENSOR_CHAN_ACCEL_Y
,SENSOR_CHAN_ACCEL_Z
, for each axis respectively, andSENSOR_CHAN_ACCEL_XYZ
for all axes at once). The acceleration is generated depending on the selected Devicetreeacc-signal
property:toggle
- With this choice, the acceleration is toggled on fetch between statically defined values.wave
- With this choice, the acceleration is generated as value of a periodic wave signal. The wave signal value is generated using the Wave generator library. You can use thesensor_sim_set_wave_param()
function to configure generated waves. By default, the function generates a sine wave.
Configuration of sensor triggers
Use CONFIG_SENSOR_SIM_TRIGGER
to enable the sensor trigger.
The simulated sensor supports the SENSOR_TRIG_DATA_READY
trigger.
You can configure the event that generates the trigger in Devicetree by using the trigger-gpios
or trigger-timeout
options.
For both triggers, the handler function is called by a dedicated thread. The thread has the following configuration options:
CONFIG_SENSOR_SIM_THREAD_PRIORITY
- This Kconfig option defines the priority.CONFIG_SENSOR_SIM_THREAD_STACK_SIZE
- This Kconfig option defines the stack size.
API documentation
include/drivers/sensor_sim.h
drivers/sensor/sensor_sim/sensor_sim.c
- group sensor_sim
Simulated sensor device driver.
Functions
-
int sensor_sim_set_wave_param(const struct device *dev, enum sensor_channel chan, const struct wave_gen_param *set_params)
Set simulated acceleration parameters.
Note
This function can be used only if acceleration is generated as wave signal. Moreover, although it is thread-safe, it cannot be used in interrupts.
- Parameters
dev – [in] Sensor device instance.
chan – [in] Selected sensor channel.
set_params – [in] Parameters of wave signal.
- Return values
0 – If the operation was successful. Otherwise, a (negative) error code is returned.
-
int sensor_sim_set_wave_param(const struct device *dev, enum sensor_channel chan, const struct wave_gen_param *set_params)