1 #ifndef RUUVI_DRIVER_SENSOR_H
2 #define RUUVI_DRIVER_SENSOR_H
63 #define RD_SENSOR_INVALID_VALUE RD_FLOAT_INVALID
64 #define RD_SENSOR_INVALID_TIMSTAMP RD_UINT64_INVALID
67 #define RD_SENSOR_CFG_DEFAULT (0U)
68 #define RD_SENSOR_CFG_CUSTOM_1 (0xC9U)
69 #define RD_SENSOR_CFG_CUSTOM_2 (0xCAU)
70 #define RD_SENSOR_CFG_CUSTOM_3 (0xCBU)
71 #define RD_SENSOR_CFG_CUSTOM_4 (0xCCU)
72 #define RD_SENSOR_CFG_CUSTOM_5 (0xCDU)
73 #define RD_SENSOR_CFG_CUSTOM_6 (0xCEU)
74 #define RD_SENSOR_ERR_INVALID (0xE0U)
75 #define RD_SENSOR_ERR_NOT_IMPLEMENTED (0xE1U)
76 #define RD_SENSOR_ERR_NOT_SUPPORTED (0xE2U)
77 #define RD_SENSOR_CFG_MIN (0xF0U)
78 #define RD_SENSOR_CFG_MAX (0xF1U)
79 #define RD_SENSOR_CFG_SLEEP (0xF2U)
80 #define RD_SENSOR_CFG_SINGLE (0xF3U)
81 #define RD_SENSOR_CFG_CONTINUOUS (0xF4U)
82 #define RD_SENSOR_CFG_NO_CHANGE (0xFFU)
85 #define RD_SENSOR_DSP_LAST (0U)
86 #define RD_SENSOR_DSP_LOW_PASS (1U<<1U)
87 #define RD_SENSOR_DSP_HIGH_PASS (1U<<2U)
88 #define RD_SENSOR_DSP_OS (1U<<3U)
91 #define RD_GPIO_TO_HANDLE(handle) ((((handle) >> 3U) & 0xE0U) + ((handle) & 0x1FU))
93 #define RD_HANDLE_TO_GPIO(handle) ((((handle) & 0xE0U) << 3U) + ((handle) & 0x1FU))
95 #define RD_HANDLE_UNUSED (0xFFU)
97 #ifndef UNUSED_VARIABLE
98 # define UNUSED_VARIABLE(X) ((void)(X))
104 typedef struct __attribute__ ( (packed, aligned (4)))
170 #define RD_SENSOR_ACC_X_FIELD ((rd_sensor_data_fields_t){.datas.acceleration_x_g=1})
172 #define RD_SENSOR_ACC_Y_FIELD ((rd_sensor_data_fields_t){.datas.acceleration_y_g=1})
174 #define RD_SENSOR_ACC_Z_FIELD ((rd_sensor_data_fields_t){.datas.acceleration_z_g=1})
176 #define RD_SENSOR_GYR_X_FIELD ((rd_sensor_data_fields_t){.datas.gyro_x_dps=1})
178 #define RD_SENSOR_GYR_Y_FIELD ((rd_sensor_data_fields_t){.datas.gyro_y_dps=1})
180 #define RD_SENSOR_GYR_Z_FIELD ((rd_sensor_data_fields_t){.datas.gyro_z_dps=1})
182 #define RD_SENSOR_HUMI_FIELD ((rd_sensor_data_fields_t){.datas.humidity_rh=1})
184 #define RD_SENSOR_PRES_FIELD ((rd_sensor_data_fields_t){.datas.pressure_pa=1})
186 #define RD_SENSOR_TEMP_FIELD ((rd_sensor_data_fields_t){.datas.temperature_c=1})
244 p_sensor,
const rd_bus_t bus,
const uint8_t handle);
274 uint8_t * dsp_parameter);
600 const uint8_t index);
622 const uint8_t index);
uint32_t rd_status_t
bitfield for representing errors
rd_status_t(* rd_sensor_init_fp)(rd_sensor_t *const p_sensor, const rd_bus_t bus, const uint8_t handle)
Initialize and uninitialize sensor. Init and uninit will setup sensor with function pointers....
bool rd_sensor_is_init(const rd_sensor_t *const sensor)
Check if given sensor structure is already initialized.
rd_status_t(* rd_sensor_level_interrupt_use_fp)(const bool enable, float *limit_g)
Enable level interrupt on sensor.
bool rd_sensor_has_valid_data(const rd_sensor_data_t *const target, const uint8_t index)
Check if sensor has valid data at given index.
rd_status_t(* rd_sensor_dsp_fp)(uint8_t *dsp_function, uint8_t *dsp_parameter)
Configure sensor digital signal processing. Takes DSP function and a DSP parameter as input,...
rd_status_t(* rd_sensor_data_fp)(rd_sensor_data_t *const p_data)
Read latest data from sensor registers Return latest data from sensor. Does not take a new sample,...
uint8_t rd_sensor_data_fieldcount(const rd_sensor_data_t *const target)
Count number of floats required for this data structure.
void rd_sensor_uninitialize(rd_sensor_t *const p_sensor)
Mark sensor as uninitialized by calling the generic initialization. Will not clear the name of the se...
rd_sensor_data_bitfield_t rd_sensor_field_type(const rd_sensor_data_t *const target, const uint8_t index)
Check the type of data at given index.
rd_status_t rd_sensor_timestamp_function_set(const rd_sensor_timestamp_fp timestamp_fp)
Setup timestamping. Set to NULL to disable timestamps.
rd_status_t rd_sensor_configuration_get(const rd_sensor_t *sensor, rd_sensor_configuration_t *config)
Implementation of ref rd_configuration_fp.
void rd_sensor_initialize(rd_sensor_t *const p_sensor)
Initialize sensor struct with non-null pointers which return RD_ERROR_NOT_INITIALIZED.
rd_status_t(* rd_sensor_fifo_read_fp)(size_t *const num_elements, rd_sensor_data_t *const data)
Read First-in-first-out (FIFO) buffer in sensor. Reads up to num_elements data points from FIFO and p...
rd_status_t validate_default_input_set(uint8_t *const input, const uint8_t mode)
Validate that given setting can be set on a sensor which supports only default value.
rd_status_t(* rd_sensor_setup_fp)(uint8_t *parameter)
Setup a parameter of a sensor. The function will modify the pointed data to the actual value which wa...
void rd_sensor_data_populate(rd_sensor_data_t *const target, const rd_sensor_data_t *const provided, const rd_sensor_data_fields_t requested)
Populate given target data with data provided by sensor as requested.
rd_status_t(* rd_sensor_fifo_enable_fp)(const bool enable)
Enable FIFO or FIFO interrupt full interrupt on sensor. FIFO interrupt Triggers an interrupt once FIF...
rd_status_t(* rd_configuration_fp)(const rd_sensor_t *const p_sensor, rd_sensor_configuration_t *const p_configuration)
Convenience function to write/read entire configuration in one call. Modifies input parameters to act...
rd_status_t rd_sensor_configuration_set(const rd_sensor_t *sensor, rd_sensor_configuration_t *config)
Implementation of ref rd_configuration_fp.
rd_bus_t
Type of bus sensor uses.
void rd_sensor_data_set(rd_sensor_data_t *const target, const rd_sensor_data_fields_t field, const float value)
Set a desired value to target data.
struct rd_sensor_t rd_sensor_t
Forward declare type definition of sensor structure.
uint64_t rd_sensor_timestamp_get(void)
Calls the timestamp function and returns its value.
rd_status_t validate_default_input_get(uint8_t *const input)
Validate and get input when only allowed value is default.
struct rd_sensor_data_t rd_sensor_data_t
Generic sensor data struct.
float rd_sensor_data_parse(const rd_sensor_data_t *const provided, const rd_sensor_data_fields_t requested)
Parse data from provided struct.
uint64_t(* rd_sensor_timestamp_fp)(void)
Return number of milliseconds since the start of RTC.
@ RD_BUS_NONE
No bus, internal to IC.
@ RD_BUS_FAIL
Test behaviour on invalid bus with this value.
Ruuvi error codes and error check function.
All sensors must implement configuration functions which accept this struct.
uint8_t reserved1
Reserved for future use.
uint8_t resolution
Resolution, in bits.
uint8_t mode
Mode, RD_SENSOR_SLEEP, _SINGLE, _CONTINOUS.
uint8_t reserved0
Reserved for future use.
uint8_t samplerate
Samplerate, in Hz.
uint8_t scale
Scale, in relevant Si-unit.
uint8_t dsp_parameter
Parameter to DSP functions.
uint8_t dsp_function
DSP function, one of RD_SENSOR_DSP_*.
Bitfield to describe related sensor data.
unsigned int luminosity
Light level, dimensionless. Comparable only between identical devices.
unsigned int reserved
Reserved bits, force remainder of bitfield to 0.
unsigned int humidity_rh
Relative humidity, %.
unsigned int magnetometer_y_g
Magnetic flux along Y-axis, Gauss.
unsigned int gyro_z_dps
Rotation along Z-axis, degrees per second.
unsigned int acceleration_x_g
Acceleration along X-axis, gravities.
unsigned int pm_10_ugm3
Coarse particulate matter, microgram per m^3.
unsigned int pm_4_ugm3
Medium particulate matter, microgram per m^3.
unsigned int magnetometer_z_g
Magnetic flux along Z-axis, Gauss.
unsigned int pm_1_ugm3
Ultra-fine particulate matter, microgram per m^3.
unsigned int temperature_c
Temperature, celcius.
unsigned int voltage_v
Voltage, volts.
unsigned int gyro_y_dps
Rotation along Y-axis, degrees per second.
unsigned int gyro_x_dps
Rotation along X-axis, degrees per second.
unsigned int pressure_pa
Pressure, pascals.
unsigned int co2_ppm
CO2, Parts per million.
unsigned int pm_2_ugm3
Fine particulate matter, microgram per m^3.
unsigned int magnetometer_x_g
Magnetic flux along X-axis, Gauss.
unsigned int acceleration_z_g
Acceleration along Z-axis, gravities.
unsigned int acceleration_y_g
Acceleration along Y-axis, gravities.
unsigned int voltage_ratio
Voltage, ratio to maximum.
unsigned int spl_dbz
Unweighted sound pressure level.
unsigned int voc_ppm
Volatile organic compounds, parts per million.
Generic sensor data struct.
float * data
Data of sensor. Must contain as many elements as fields has bits set.
uint64_t timestamp_ms
Timestamp of the event, rd_sensor_timestamp_get.
rd_sensor_data_fields_t valid
Listing of valid data in this sample.
rd_sensor_data_fields_t fields
Description of datafields which may be contained in this sample.
Interface to sensor. Some sensors can implement additional functions. The additional functions are de...
rd_sensor_data_fp data_get
rd_sensor_data_fp
rd_configuration_fp configuration_set
rd_configuration_fp
rd_sensor_setup_fp samplerate_get
rd_sensor_setup_fp
rd_sensor_setup_fp resolution_get
rd_sensor_setup_fp
rd_sensor_init_fp uninit
rd_sensor_init_fp
rd_configuration_fp configuration_get
rd_configuration_fp
void * p_ctx
handle for sensor internal context
rd_sensor_dsp_fp dsp_get
rd_sensor_dsp_fp
rd_sensor_data_fields_t provides
Description of data fields the sensor is able to provide.
rd_sensor_dsp_fp dsp_set
rd_sensor_dsp_fp
rd_sensor_setup_fp mode_set
rd_sensor_setup_fp
const char * name
Sensor human-readable name. Should be at most 8 bytes long.
rd_sensor_fifo_read_fp fifo_read
rd_sensor_level_interrupt_use_fp
rd_sensor_fifo_enable_fp fifo_interrupt_enable
rd_sensor_level_interrupt_use_fp
rd_sensor_level_interrupt_use_fp level_interrupt_set
rd_sensor_level_interrupt_use_fp
rd_sensor_setup_fp mode_get
rd_sensor_setup_fp
rd_sensor_setup_fp samplerate_set
rd_sensor_setup_fp
rd_sensor_fifo_enable_fp fifo_enable
rd_sensor_fifo_enable_fp
rd_sensor_init_fp init
rd_sensor_init_fp
rd_sensor_setup_fp scale_set
rd_sensor_setup_fp
rd_sensor_setup_fp resolution_set
rd_sensor_setup_fp
rd_sensor_setup_fp scale_get
rd_sensor_setup_fp
Union to access sensor data.
uint32_t bitfield
Bitfield used to access sensor data.
rd_sensor_data_bitfield_t datas
Structured data field.