ruuvi.drivers.c  ${PROJECT_VERSION}
Drivers for external sensors and peripherals on embedded systems.
Common sensor interface

Functions for setting up and using sensors. More...

Files

file  ruuvi_driver_sensor.h
 Ruuvi sensor interface Lifecycle: Beta
 

Data Structures

struct  rd_sensor_configuration_t
 All sensors must implement configuration functions which accept this struct. More...
 
struct  rd_sensor_data_bitfield_t
 Bitfield to describe related sensor data. More...
 
union  rd_sensor_data_fields_t
 Union to access sensor data. More...
 
struct  rd_sensor_data_t
 Generic sensor data struct. More...
 
struct  rd_sensor_t
 Interface to sensor. Some sensors can implement additional functions. The additional functions are defined in the interface of the sensor. More...
 

Macros

#define RD_SENSOR_INVALID_VALUE   RD_FLOAT_INVALID
 Signal this sensor value is erroneous. More...
 
#define RD_SENSOR_INVALID_TIMSTAMP   RD_UINT64_INVALID
 Signal this timestamp value is erroneous. More...
 
#define RD_SENSOR_CFG_DEFAULT   (0U)
 Default value, always valid for the sensor. More...
 
#define RD_SENSOR_CFG_CUSTOM_1   (0xC9U)
 Configuration range is 0...200, i.e. 0 ... 0xC8. Use C9 ... CF as sensor-specific values. More...
 
#define RD_SENSOR_CFG_CUSTOM_2   (0xCAU)
 Configuration range is 0...200, i.e. 0 ... 0xC8. Use C9 ... CF as sensor-specific values. More...
 
#define RD_SENSOR_CFG_CUSTOM_3   (0xCBU)
 Configuration range is 0...200, i.e. 0 ... 0xC8. Use C9 ... CF as sensor-specific values. More...
 
#define RD_SENSOR_CFG_CUSTOM_4   (0xCCU)
 Configuration range is 0...200, i.e. 0 ... 0xC8. Use C9 ... CF as sensor-specific values. More...
 
#define RD_SENSOR_CFG_CUSTOM_5   (0xCDU)
 Configuration range is 0...200, i.e. 0 ... 0xC8. Use C9 ... CF as sensor-specific values. More...
 
#define RD_SENSOR_CFG_CUSTOM_6   (0xCEU)
 Configuration range is 0...200, i.e. 0 ... 0xC8. Use C9 ... CF as sensor-specific values. More...
 
#define RD_SENSOR_ERR_INVALID   (0xE0U)
 Error code, given parameter is invalid. More...
 
#define RD_SENSOR_ERR_NOT_IMPLEMENTED   (0xE1U)
 Error code, given parameter is not implemented (todo) More...
 
#define RD_SENSOR_ERR_NOT_SUPPORTED   (0xE2U)
 Error code, given parameter is not supported by sensor. More...
 
#define RD_SENSOR_CFG_MIN   (0xF0U)
 Configure smallest supported and implemented value. More...
 
#define RD_SENSOR_CFG_MAX   (0xF1U)
 Configure largest supported and implemented value. More...
 
#define RD_SENSOR_CFG_SLEEP   (0xF2U)
 Sensor should go to sleep immediately. More...
 
#define RD_SENSOR_CFG_SINGLE   (0xF3U)
 Sensor should go to sleep after single measurement. More...
 
#define RD_SENSOR_CFG_CONTINUOUS   (0xF4U)
 Sensor will keep sampling at defined sample rate. More...
 
#define RD_SENSOR_CFG_NO_CHANGE   (0xFFU)
 Do not change configured value. More...
 
#define RD_SENSOR_DSP_LAST   (0U)
 Return last value from sensor. Parameter: No effect. Use default. More...
 
#define RD_SENSOR_DSP_LOW_PASS   (1U<<1U)
 Low pass sensor values Parameter: coefficient. More...
 
#define RD_SENSOR_DSP_HIGH_PASS   (1U<<2U)
 High pass sensor values Parameter: coefficient. More...
 
#define RD_SENSOR_DSP_OS   (1U<<3U)
 Oversample sensor values. Parameter: Number of samples. More...
 
#define RD_GPIO_TO_HANDLE(handle)   ((((handle) >> 3U) & 0xE0U) + ((handle) & 0x1FU))
 convert Ruuvi GPIO into uint8_t More...
 
#define RD_HANDLE_TO_GPIO(handle)   ((((handle) & 0xE0U) << 3U) + ((handle) & 0x1FU))
 convert uint8_t into Ruuvi GPIO More...
 
#define RD_HANDLE_UNUSED   (0xFFU)
 Mark sensor as unused with this handle. More...
 
#define UNUSED_VARIABLE(X)   ((void)(X))
 
#define RD_SENSOR_ACC_X_FIELD   ((rd_sensor_data_fields_t){.datas.acceleration_x_g=1})
 Shorthand for calling rd_sensor_data_parse(p_data, FIELD) More...
 
#define RD_SENSOR_ACC_Y_FIELD   ((rd_sensor_data_fields_t){.datas.acceleration_y_g=1})
 Shorthand for calling rd_sensor_data_parse(p_data, FIELD) More...
 
#define RD_SENSOR_ACC_Z_FIELD   ((rd_sensor_data_fields_t){.datas.acceleration_z_g=1})
 Shorthand for calling rd_sensor_data_parse(p_data, FIELD) More...
 
#define RD_SENSOR_GYR_X_FIELD   ((rd_sensor_data_fields_t){.datas.gyro_x_dps=1})
 Shorthand for calling rd_sensor_data_parse(p_data, FIELD) More...
 
#define RD_SENSOR_GYR_Y_FIELD   ((rd_sensor_data_fields_t){.datas.gyro_y_dps=1})
 Shorthand for calling rd_sensor_data_parse(p_data, FIELD) More...
 
#define RD_SENSOR_GYR_Z_FIELD   ((rd_sensor_data_fields_t){.datas.gyro_z_dps=1})
 Shorthand for calling rd_sensor_data_parse(p_data, FIELD) More...
 
#define RD_SENSOR_HUMI_FIELD   ((rd_sensor_data_fields_t){.datas.humidity_rh=1})
 Shorthand for calling rd_sensor_data_parse(p_data, FIELD) More...
 
#define RD_SENSOR_PRES_FIELD   ((rd_sensor_data_fields_t){.datas.pressure_pa=1})
 Shorthand for calling rd_sensor_data_parse(p_data, FIELD) More...
 
#define RD_SENSOR_TEMP_FIELD   ((rd_sensor_data_fields_t){.datas.temperature_c=1})
 Shorthand for calling rd_sensor_data_parse(p_data, FIELD) More...
 

Typedefs

typedef struct rd_sensor_data_t rd_sensor_data_t
 Generic sensor data struct. More...
 
typedef struct rd_sensor_t rd_sensor_t
 Forward declare type definition of sensor structure. More...
 
typedef 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. The sensor wil be initialized to lowest power state possible. More...
 
typedef 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 was written. More...
 
typedef 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, configured value or error code as output. Modifies input parameters to actual values written on the sensor. DSP functions are run on the sensor HW, not in the platform FW. More...
 
typedef 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, calling this function twice in a row returns same data. Configure sensor in a single-shot mode to take a new sample or leave sensor in a continuous mode to get updated data. More...
 
typedef 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 actual values written on the sensor. More...
 
typedef 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 populates pointer data with them. More...
 
typedef 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 FIFO is filled. It is responsibility of application to know the routing of and polarity of GPIO pins and configure the GPIO to register interrupts. More...
 
typedef rd_status_t(* rd_sensor_level_interrupt_use_fp) (const bool enable, float *limit_g)
 Enable level interrupt on sensor. More...
 
typedef uint64_t(* rd_sensor_timestamp_fp) (void)
 Return number of milliseconds since the start of RTC. More...
 

Enumerations

enum  rd_bus_t {
  RD_BUS_NONE = 0U , RD_BUS_SPI = 1U , RD_BUS_I2C = 2U , RD_BUS_UART = 3U ,
  RD_BUS_PDM = 4U , RD_BUS_FAIL = 5U
}
 Type of bus sensor uses. More...
 

Functions

rd_status_t rd_sensor_configuration_set (const rd_sensor_t *sensor, rd_sensor_configuration_t *config)
 Implementation of ref rd_configuration_fp. More...
 
rd_status_t rd_sensor_configuration_get (const rd_sensor_t *sensor, rd_sensor_configuration_t *config)
 Implementation of ref rd_configuration_fp. More...
 
rd_status_t rd_sensor_timestamp_function_set (const rd_sensor_timestamp_fp timestamp_fp)
 Setup timestamping. Set to NULL to disable timestamps. More...
 
uint64_t rd_sensor_timestamp_get (void)
 Calls the timestamp function and returns its value. More...
 
void rd_sensor_initialize (rd_sensor_t *const p_sensor)
 Initialize sensor struct with non-null pointers which return RD_ERROR_NOT_INITIALIZED. More...
 
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 sensor. More...
 
bool rd_sensor_is_init (const rd_sensor_t *const sensor)
 Check if given sensor structure is already initialized. More...
 
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. More...
 
float rd_sensor_data_parse (const rd_sensor_data_t *const provided, const rd_sensor_data_fields_t requested)
 Parse data from provided struct. More...
 
uint8_t rd_sensor_data_fieldcount (const rd_sensor_data_t *const target)
 Count number of floats required for this data structure. More...
 
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. More...
 
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. More...
 
rd_status_t validate_default_input_get (uint8_t *const input)
 Validate and get input when only allowed value is default. More...
 
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. More...
 
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. More...
 

Detailed Description

Functions for setting up and using sensors.

Macro Definition Documentation

◆ RD_GPIO_TO_HANDLE

#define RD_GPIO_TO_HANDLE (   handle)    ((((handle) >> 3U) & 0xE0U) + ((handle) & 0x1FU))

convert Ruuvi GPIO into uint8_t

Definition at line 91 of file ruuvi_driver_sensor.h.

◆ RD_HANDLE_TO_GPIO

#define RD_HANDLE_TO_GPIO (   handle)    ((((handle) & 0xE0U) << 3U) + ((handle) & 0x1FU))

convert uint8_t into Ruuvi GPIO

Definition at line 93 of file ruuvi_driver_sensor.h.

◆ RD_HANDLE_UNUSED

#define RD_HANDLE_UNUSED   (0xFFU)

Mark sensor as unused with this handle.

Definition at line 95 of file ruuvi_driver_sensor.h.

◆ RD_SENSOR_ACC_X_FIELD

#define RD_SENSOR_ACC_X_FIELD   ((rd_sensor_data_fields_t){.datas.acceleration_x_g=1})

Shorthand for calling rd_sensor_data_parse(p_data, FIELD)

C99 Standard 6.7.8.21 If there are fewer initializers in a brace-enclosed list than there are elements or members of an aggregate, or fewer characters in a string literal used to initialize an array of known size than there are elements in the array, the remainder of the aggregate shall be initialized implicitly the same as objects that have static storage duration.

Definition at line 170 of file ruuvi_driver_sensor.h.

◆ RD_SENSOR_ACC_Y_FIELD

#define RD_SENSOR_ACC_Y_FIELD   ((rd_sensor_data_fields_t){.datas.acceleration_y_g=1})

Shorthand for calling rd_sensor_data_parse(p_data, FIELD)

Definition at line 172 of file ruuvi_driver_sensor.h.

◆ RD_SENSOR_ACC_Z_FIELD

#define RD_SENSOR_ACC_Z_FIELD   ((rd_sensor_data_fields_t){.datas.acceleration_z_g=1})

Shorthand for calling rd_sensor_data_parse(p_data, FIELD)

Definition at line 174 of file ruuvi_driver_sensor.h.

◆ RD_SENSOR_CFG_CONTINUOUS

#define RD_SENSOR_CFG_CONTINUOUS   (0xF4U)

Sensor will keep sampling at defined sample rate.

Definition at line 81 of file ruuvi_driver_sensor.h.

◆ RD_SENSOR_CFG_CUSTOM_1

#define RD_SENSOR_CFG_CUSTOM_1   (0xC9U)

Configuration range is 0...200, i.e. 0 ... 0xC8. Use C9 ... CF as sensor-specific values.

Definition at line 68 of file ruuvi_driver_sensor.h.

◆ RD_SENSOR_CFG_CUSTOM_2

#define RD_SENSOR_CFG_CUSTOM_2   (0xCAU)

Configuration range is 0...200, i.e. 0 ... 0xC8. Use C9 ... CF as sensor-specific values.

Definition at line 69 of file ruuvi_driver_sensor.h.

◆ RD_SENSOR_CFG_CUSTOM_3

#define RD_SENSOR_CFG_CUSTOM_3   (0xCBU)

Configuration range is 0...200, i.e. 0 ... 0xC8. Use C9 ... CF as sensor-specific values.

Definition at line 70 of file ruuvi_driver_sensor.h.

◆ RD_SENSOR_CFG_CUSTOM_4

#define RD_SENSOR_CFG_CUSTOM_4   (0xCCU)

Configuration range is 0...200, i.e. 0 ... 0xC8. Use C9 ... CF as sensor-specific values.

Definition at line 71 of file ruuvi_driver_sensor.h.

◆ RD_SENSOR_CFG_CUSTOM_5

#define RD_SENSOR_CFG_CUSTOM_5   (0xCDU)

Configuration range is 0...200, i.e. 0 ... 0xC8. Use C9 ... CF as sensor-specific values.

Definition at line 72 of file ruuvi_driver_sensor.h.

◆ RD_SENSOR_CFG_CUSTOM_6

#define RD_SENSOR_CFG_CUSTOM_6   (0xCEU)

Configuration range is 0...200, i.e. 0 ... 0xC8. Use C9 ... CF as sensor-specific values.

Definition at line 73 of file ruuvi_driver_sensor.h.

◆ RD_SENSOR_CFG_DEFAULT

#define RD_SENSOR_CFG_DEFAULT   (0U)

Default value, always valid for the sensor.

Definition at line 67 of file ruuvi_driver_sensor.h.

◆ RD_SENSOR_CFG_MAX

#define RD_SENSOR_CFG_MAX   (0xF1U)

Configure largest supported and implemented value.

Definition at line 78 of file ruuvi_driver_sensor.h.

◆ RD_SENSOR_CFG_MIN

#define RD_SENSOR_CFG_MIN   (0xF0U)

Configure smallest supported and implemented value.

Definition at line 77 of file ruuvi_driver_sensor.h.

◆ RD_SENSOR_CFG_NO_CHANGE

#define RD_SENSOR_CFG_NO_CHANGE   (0xFFU)

Do not change configured value.

Definition at line 82 of file ruuvi_driver_sensor.h.

◆ RD_SENSOR_CFG_SINGLE

#define RD_SENSOR_CFG_SINGLE   (0xF3U)

Sensor should go to sleep after single measurement.

Definition at line 80 of file ruuvi_driver_sensor.h.

◆ RD_SENSOR_CFG_SLEEP

#define RD_SENSOR_CFG_SLEEP   (0xF2U)

Sensor should go to sleep immediately.

Definition at line 79 of file ruuvi_driver_sensor.h.

◆ RD_SENSOR_DSP_HIGH_PASS

#define RD_SENSOR_DSP_HIGH_PASS   (1U<<2U)

High pass sensor values Parameter: coefficient.

Definition at line 87 of file ruuvi_driver_sensor.h.

◆ RD_SENSOR_DSP_LAST

#define RD_SENSOR_DSP_LAST   (0U)

Return last value from sensor. Parameter: No effect. Use default.

Definition at line 85 of file ruuvi_driver_sensor.h.

◆ RD_SENSOR_DSP_LOW_PASS

#define RD_SENSOR_DSP_LOW_PASS   (1U<<1U)

Low pass sensor values Parameter: coefficient.

Definition at line 86 of file ruuvi_driver_sensor.h.

◆ RD_SENSOR_DSP_OS

#define RD_SENSOR_DSP_OS   (1U<<3U)

Oversample sensor values. Parameter: Number of samples.

Definition at line 88 of file ruuvi_driver_sensor.h.

◆ RD_SENSOR_ERR_INVALID

#define RD_SENSOR_ERR_INVALID   (0xE0U)

Error code, given parameter is invalid.

Definition at line 74 of file ruuvi_driver_sensor.h.

◆ RD_SENSOR_ERR_NOT_IMPLEMENTED

#define RD_SENSOR_ERR_NOT_IMPLEMENTED   (0xE1U)

Error code, given parameter is not implemented (todo)

Definition at line 75 of file ruuvi_driver_sensor.h.

◆ RD_SENSOR_ERR_NOT_SUPPORTED

#define RD_SENSOR_ERR_NOT_SUPPORTED   (0xE2U)

Error code, given parameter is not supported by sensor.

Definition at line 76 of file ruuvi_driver_sensor.h.

◆ RD_SENSOR_GYR_X_FIELD

#define RD_SENSOR_GYR_X_FIELD   ((rd_sensor_data_fields_t){.datas.gyro_x_dps=1})

Shorthand for calling rd_sensor_data_parse(p_data, FIELD)

Definition at line 176 of file ruuvi_driver_sensor.h.

◆ RD_SENSOR_GYR_Y_FIELD

#define RD_SENSOR_GYR_Y_FIELD   ((rd_sensor_data_fields_t){.datas.gyro_y_dps=1})

Shorthand for calling rd_sensor_data_parse(p_data, FIELD)

Definition at line 178 of file ruuvi_driver_sensor.h.

◆ RD_SENSOR_GYR_Z_FIELD

#define RD_SENSOR_GYR_Z_FIELD   ((rd_sensor_data_fields_t){.datas.gyro_z_dps=1})

Shorthand for calling rd_sensor_data_parse(p_data, FIELD)

Definition at line 180 of file ruuvi_driver_sensor.h.

◆ RD_SENSOR_HUMI_FIELD

#define RD_SENSOR_HUMI_FIELD   ((rd_sensor_data_fields_t){.datas.humidity_rh=1})

Shorthand for calling rd_sensor_data_parse(p_data, FIELD)

Definition at line 182 of file ruuvi_driver_sensor.h.

◆ RD_SENSOR_INVALID_TIMSTAMP

#define RD_SENSOR_INVALID_TIMSTAMP   RD_UINT64_INVALID

Signal this timestamp value is erroneous.

Definition at line 64 of file ruuvi_driver_sensor.h.

◆ RD_SENSOR_INVALID_VALUE

#define RD_SENSOR_INVALID_VALUE   RD_FLOAT_INVALID

Signal this sensor value is erroneous.

Definition at line 63 of file ruuvi_driver_sensor.h.

◆ RD_SENSOR_PRES_FIELD

#define RD_SENSOR_PRES_FIELD   ((rd_sensor_data_fields_t){.datas.pressure_pa=1})

Shorthand for calling rd_sensor_data_parse(p_data, FIELD)

Definition at line 184 of file ruuvi_driver_sensor.h.

◆ RD_SENSOR_TEMP_FIELD

#define RD_SENSOR_TEMP_FIELD   ((rd_sensor_data_fields_t){.datas.temperature_c=1})

Shorthand for calling rd_sensor_data_parse(p_data, FIELD)

Definition at line 186 of file ruuvi_driver_sensor.h.

◆ UNUSED_VARIABLE

#define UNUSED_VARIABLE (   X)    ((void)(X))

Definition at line 98 of file ruuvi_driver_sensor.h.

Typedef Documentation

◆ rd_configuration_fp

typedef 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 actual values written on the sensor.

Parameters
[in]p_sensorsensor to configure
[in,out]p_configurationInput: desired configuration. Output: configuration written to sensot.
Return values
RD_SUCCESSif sensor was configured successfully.
RD_ERROR_NULLif one of parameters is NULL
Returns
Error code from driver on other error.

Definition at line 304 of file ruuvi_driver_sensor.h.

◆ rd_sensor_data_fp

typedef 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, calling this function twice in a row returns same data. Configure sensor in a single-shot mode to take a new sample or leave sensor in a continuous mode to get updated data.

p_data may contain, some, none or all of fields sensor is able to provide. Fields which are already marked as valid will not be overwritten, filled fields will get marked as valid.

Parameters
[out]p_dataPointer to sensor data rd_sensor_data_t .
Returns
RD_SUCCESS on success
RD_ERROR_NULL if p_data is NULL.

Definition at line 291 of file ruuvi_driver_sensor.h.

◆ rd_sensor_data_t

Generic sensor data struct.

The data sensor struct contains a timestamp relative to sensor boot, a list of fields contained within the sensor data and a pointer to array of floats which contain the actual data.

◆ rd_sensor_dsp_fp

typedef 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, configured value or error code as output. Modifies input parameters to actual values written on the sensor. DSP functions are run on the sensor HW, not in the platform FW.

Parameters
[in,out]dsp_functionDSP function to run on sensor. Can be a combination of several functions.
[in,out]dsp_parameterParameter to DSP function(s)
Returns
RD_SUCCESS on success
RD_ERROR_NULL if either parameter is NULL
RD_ERROR_NOT_SUPPORTED if sensor doesn't support given DSP
RD_ERROR_NOT_IMPLEMENTED if sensor supports given DSP, but driver does not implement it
RD_ERROR_INVALID_PARAM if parameter is invalid for any reason.

Definition at line 273 of file ruuvi_driver_sensor.h.

◆ rd_sensor_fifo_enable_fp

typedef 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 FIFO is filled. It is responsibility of application to know the routing of and polarity of GPIO pins and configure the GPIO to register interrupts.

Parameters
[in]enableTrue to enable interrupt, false to disable interrupt
Returns
RD_SUCCESS on success, error code from stack otherwise.

Definition at line 333 of file ruuvi_driver_sensor.h.

◆ rd_sensor_fifo_read_fp

typedef 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 populates pointer data with them.

Parameters
[in,out]num_elementsInput: number of elements in data. Output: Number of elements placed in data.
[out]dataarray of with num_elements slots.
Return values
RD_SUCCESSon success.
RD_ERROR_NULLif either parameter is NULL.
RD_ERROR_INVALID_STATEif FIFO is not in use.
RD_ERROR_NOT_SUPPORTEDif the sensor does not have FIFO.
Returns
error code from stack on error.

Definition at line 321 of file ruuvi_driver_sensor.h.

◆ rd_sensor_init_fp

typedef 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. The sensor wil be initialized to lowest power state possible.

Parameters
[in,out]p_sensorpointer to sensor structure
[in]busbus to use, i.r. I2C or SPI
[in]handlefor the sensor, for example I2C address or SPI chip select pin
Returns
RD_SUCCESS on success
RD_ERROR_NULL if p_sensor is NULL
RD_ERROR_NOT_FOUND if there is no response from sensor or if ID of a sensor read over bus does not match expected value
RD_ERROR_SELFTEST if sensor is found but it does not pass selftest
RD_ERROR_INVALID_STATE if trying to initialize sensor which already has been initialized.

Definition at line 243 of file ruuvi_driver_sensor.h.

◆ rd_sensor_level_interrupt_use_fp

typedef rd_status_t(* rd_sensor_level_interrupt_use_fp) (const bool enable, float *limit_g)

Enable level interrupt on sensor.

Triggers as ACTIVE HIGH interrupt while detected data is above threshold.

Trigger is symmetric, i.e. threshold is valid for above positive or below negative of given value.

On accelerometer data is high-passed to filter out gravity. Axes are examined individually, compound data won't trigger the interrupt. e.g. accelerometer showing 0.8 G along X, Y, Z axes won't trigger at threshold of 1 G, even though the vector sum of axes is larger than 1 G.

It is responsibility of application to know the GPIO routing and register GPIO interrupts.

Parameters
[in]enableTrue to enable interrupt, false to disable interrupt
[in,out]limit_gInput: Desired acceleration to trigger the interrupt. Is considered as "at least", the acceleration is rounded up to next value. Output: written with value that was set to interrupt
Return values
RD_SUCCESSon success.
RD_INVALID_STATEif data limit is higher than maximum scale.
Returns
error code from stack on error.

Definition at line 361 of file ruuvi_driver_sensor.h.

◆ rd_sensor_setup_fp

typedef 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 was written.

Parameters
[in,out]parametervalue to write to sensor configuration. Actual value written to sensor as output
Returns
RD_SUCCESS on success
RD_ERROR_NULL if parameter is NULL
RD_ERROR_NOT_SUPPORTED if sensor cannot support given parameter
RD_ERROR_NOT_IMPLEMENTED if the sensor could support parameter, but it's not implemented in fw.

Definition at line 256 of file ruuvi_driver_sensor.h.

◆ rd_sensor_t

typedef struct rd_sensor_t rd_sensor_t

Forward declare type definition of sensor structure.

Interface to sensor. Some sensors can implement additional functions. The additional functions are defined in the interface of the sensor.

Definition at line 1 of file ruuvi_driver_sensor.h.

◆ rd_sensor_timestamp_fp

typedef uint64_t(* rd_sensor_timestamp_fp) (void)

Return number of milliseconds since the start of RTC.

Returns
milliseconds since start of RTC.
RD_UINT64T_INVALID if RTC is not running

Definition at line 370 of file ruuvi_driver_sensor.h.

Enumeration Type Documentation

◆ rd_bus_t

enum rd_bus_t

Type of bus sensor uses.

Enumerator
RD_BUS_NONE 

No bus, internal to IC.

RD_BUS_SPI 

SPI bus.

RD_BUS_I2C 

I2C bus.

RD_BUS_UART 

UART bus.

RD_BUS_PDM 

PDM bus.

RD_BUS_FAIL 

Test behaviour on invalid bus with this value.

Definition at line 120 of file ruuvi_driver_sensor.h.

Function Documentation

◆ rd_sensor_configuration_get()

rd_status_t rd_sensor_configuration_get ( const rd_sensor_t sensor,
rd_sensor_configuration_t config 
)

Implementation of ref rd_configuration_fp.

Definition at line 29 of file ruuvi_driver_sensor.c.

◆ rd_sensor_configuration_set()

rd_status_t rd_sensor_configuration_set ( const rd_sensor_t sensor,
rd_sensor_configuration_t config 
)

Implementation of ref rd_configuration_fp.

Definition at line 10 of file ruuvi_driver_sensor.c.

◆ rd_sensor_data_fieldcount()

uint8_t rd_sensor_data_fieldcount ( const rd_sensor_data_t *const  target)
inline

Count number of floats required for this data structure.

Parameters
[in]targetStructure to count number of fields from.
Returns
Number of floats required to store the sensor data.

Definition at line 333 of file ruuvi_driver_sensor.c.

◆ rd_sensor_data_parse()

float rd_sensor_data_parse ( const rd_sensor_data_t *const  provided,
const rd_sensor_data_fields_t  requested 
)

Parse data from provided struct.

Parameters
[in]providedData to be parsed.
[in]requestedOne data field to be parsed.
Returns
sensor value if found, RD_FLOAT_INVALID if the provided data didn't have a valid value.

Definition at line 175 of file ruuvi_driver_sensor.c.

◆ rd_sensor_data_populate()

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.

This function looks up the appropriate assigments on each data field in given target and populates it with provided data if caller requested the field to be populated. Populated fields are marked as valid.

Example: Board can have these sensors in this order of priority:

  • TMP117 (temperature)
  • SHTC3 (temperature, humidity)
  • DPS310 (temperature, pressure)
  • LIS2DH12 (acceleration, temperature)

If a target with fields for temperature, humidity, pressure and acceleration is created and populated from data of the sensors end result will be:

-> Temperature, timestamp from TMP117 -> Humidity from SHTC3 -> Pressure from DPS310 -> Acceleration from LIS2DH12

If same firmware is run on a board with only LIS2DH12 populated, end result will be

-> Temperature, timestamp, acceleration from LIS2DH12 -> RD_FLOAT_INVALID on humidity and pressure.

Parameters
[out]targetData to be populated. Fields must be initially populated with RD_FLOAT_INVALID.
[in]providedData provided by sensor.
[in]requestedFields to be filled if possible.

Definition at line 290 of file ruuvi_driver_sensor.c.

◆ rd_sensor_data_set()

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.

This function looks up the appropriate assigments on each data field in given target and populates it with provided data. Does nothing if there is no appropriate slot in target data.

This is a shorthand for rd_sensor_data_populate for only one data field, without setting timestamp.

Parameters
[out]target
[in]fieldQuantity to set, exactly one must be set to true.
[in]valueValue of quantity,

Definition at line 191 of file ruuvi_driver_sensor.c.

◆ rd_sensor_field_type()

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.

This function is used to determine what type of data given index has.

Typical usage:

if(1 == type.temperature_c)
{
do_stuff_with_temperature (p_data->data[index])
}
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.
Bitfield to describe related sensor data.
unsigned int temperature_c
Temperature, celcius.
Parameters
[in]targetData to check
[in]indexIndex of field to check.
Returns
rd_sensor_data_bitfield_t with field corresponding to index set, or 0 if target doesn't have any data type at given index.

Definition at line 254 of file ruuvi_driver_sensor.c.

◆ rd_sensor_has_valid_data()

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.

Data is considered valid if target->fields and target->valid both are set. Index is referred to number of fields.

Typical usage:

const uint8_t fieldcount = rd_sensor_data_fieldcount(p_data);
for(uint8_t ii = 0; ii < fieldcount; ii++)
{
if(rd_sensor_has_valid_data(p_data, ii)
{
do_stuff(p_data->data[ii], rd_sensor_field_type(p_data, ii));
}
}
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.
uint8_t rd_sensor_data_fieldcount(const rd_sensor_data_t *const target)
Count number of floats required for this data structure.
Parameters
[in]targetPointer to data to check.
[in]indexindex of data to check.
Return values
trueIf data at target->data[index] has a valid value.
falseIf target is NULL, index is higher than fields in data or data at index is not marked as valid.
Note
To determine the type of data, use rd_sensor_field_type.

Definition at line 218 of file ruuvi_driver_sensor.c.

◆ rd_sensor_initialize()

void rd_sensor_initialize ( rd_sensor_t *const  p_sensor)

Initialize sensor struct with non-null pointers which return RD_ERROR_NOT_INITIALIZED.

This function is to ensure that NULL function pointers won't be called. If name was NULL before calling this, name will point to "NOTINIT". If name was already set, it won't be changed.

Parameters
[out]p_sensorpointer to sensor struct to initialize.

Definition at line 114 of file ruuvi_driver_sensor.c.

◆ rd_sensor_is_init()

bool rd_sensor_is_init ( const rd_sensor_t *const  sensor)

Check if given sensor structure is already initialized.

Parameters
[in]sensorSensor interface to check.
Returns
true if structure is initialized, false otherwise.

Definition at line 397 of file ruuvi_driver_sensor.c.

◆ rd_sensor_timestamp_function_set()

rd_status_t rd_sensor_timestamp_function_set ( const rd_sensor_timestamp_fp  timestamp_fp)

Setup timestamping. Set to NULL to disable timestamps.

Parameters
[in]timestamp_fpFunction pointer to rd_sensor_timestamp_fp implementation
Return values
RD_SUCCESS.

Definition at line 48 of file ruuvi_driver_sensor.c.

◆ rd_sensor_timestamp_get()

uint64_t rd_sensor_timestamp_get ( void  )

Calls the timestamp function and returns its value.

Returns
milliseconds since the start of RTC.
Return values
RD_UINT64_INVALIDif timestamp function is NULL

Definition at line 56 of file ruuvi_driver_sensor.c.

◆ rd_sensor_uninitialize()

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 sensor.

Parameters
[out]p_sensorpointer to sensor struct to uninitialize.

Definition at line 143 of file ruuvi_driver_sensor.c.

◆ validate_default_input_get()

rd_status_t validate_default_input_get ( uint8_t *const  input)

Validate and get input when only allowed value is default.

Parameters
[out]inputSetting of sensor to get. Will be RD_SENSOR_CFG_DEFAULT.
Return values
RD_SUCCESSif input is not NULL.
RD_ERROR_NULLif input is NULL.

Definition at line 381 of file ruuvi_driver_sensor.c.

◆ validate_default_input_set()

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.

Parameters
[in,out]inputInput: Must be RD_SENSOR_CFG_DEFAULT, _NO_CHANGE, _MIN or _MAX. Output: _DEFAULT
[in]modeMode sensor is currently in. Must be sleep to configure sensor.

Definition at line 338 of file ruuvi_driver_sensor.c.