6 #define BITS_PER_BYTE (8U)
8 static const char m_init_name[] =
"NOTINIT";
15 if (NULL == sensor || NULL == config) {
return RD_ERROR_NULL; }
20 err_code |= sensor->
mode_set (&sleep);
34 if (NULL == sensor || NULL == config) {
return RD_ERROR_NULL; }
51 millis = timestamp_fp;
66 static rd_status_t rd_fifo_enable_ni (
const bool enable)
71 static rd_status_t rd_fifo_interrupt_enable_ni (
const bool enable)
87 p_sensor,
const rd_bus_t bus,
const uint8_t handle)
92 static rd_status_t rd_setup_ni (uint8_t *
const value)
97 static rd_status_t rd_level_interrupt_use_ni (
const bool enable,
103 static rd_status_t rd_dsp_ni (uint8_t *
const dsp, uint8_t *
const parameter)
116 if (NULL != p_sensor->
name)
118 p_sensor->
name = m_init_name;
123 p_sensor->
data_get = rd_data_get_ni;
129 p_sensor->
init = rd_init_ni;
130 p_sensor->
uninit = rd_init_ni;
149 static inline uint8_t get_index_of_field (
const rd_sensor_data_t *
const target,
153 uint8_t leading_zeros = (uint8_t) __builtin_clz (field.
bitfield);
154 uint32_t mask = UINT32_MAX;
156 uint8_t target_bit = (bitfield_size - leading_zeros);
158 if (target_bit < bitfield_size)
160 mask = (1U << target_bit) - 1U;
164 uint8_t index = (uint8_t) (__builtin_popcount (target->
fields.
bitfield & mask) - 1);
167 if (index > bitfield_size)
181 if ( (NULL != provided)
183 && (1 == __builtin_popcount (requested.
bitfield)))
185 rvalue = provided->
data[get_index_of_field (provided, requested)];
205 else if (1 != __builtin_popcount (field.
bitfield))
212 target->
data[get_index_of_field (target, field)] = value;
221 uint8_t fieldcount = 0;
223 uint8_t target_index = 0U;
234 if (fieldcount > index)
237 for (uint8_t ii = 0U; ii < index; ii++)
240 target_index = (uint8_t) __builtin_ctz (check.
bitfield);
241 mask = 1U << target_index;
245 target_index = (uint8_t) __builtin_ctz (check.
bitfield);
246 mask = 1U << target_index;
257 uint8_t fieldcount = 0;
259 uint8_t target_index = 0U;
268 if (fieldcount > index)
273 for (uint8_t ii = 0U; ii < index; ii++)
276 target_index = (uint8_t) __builtin_ctz (check.
bitfield);
277 mask = 1U << target_index;
281 target_index = (uint8_t) __builtin_ctz (check.
bitfield);
282 mask = 1U << target_index;
294 if ( (NULL != target) && (NULL != provided))
315 const uint8_t index = (uint8_t) __builtin_ctz (available.
bitfield);
401 if ( (NULL != sensor) && (NULL != sensor->
uninit))
403 init = (sensor->
uninit != &rd_init_ni);
#define RD_ERROR_NULL
Null Pointer.
#define RD_ERROR_NOT_INITIALIZED
Driver is not initialized.
uint32_t rd_status_t
bitfield for representing errors
#define RD_FLOAT_INVALID
Signal that value should not be used.
#define RD_ERROR_NOT_SUPPORTED
Not supported.
#define RD_SUCCESS
Internal Error.
#define RD_ERROR_INVALID_STATE
Invalid state, operation disallowed in this state.
bool rd_sensor_is_init(const rd_sensor_t *const sensor)
Check if given sensor structure is already initialized.
#define RD_SENSOR_CFG_SLEEP
Sensor should go to sleep immediately.
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.
#define RD_SENSOR_CFG_MAX
Configure largest supported and implemented value.
#define RD_SENSOR_CFG_DEFAULT
Default value, always valid for the sensor.
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(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 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.
#define RD_SENSOR_INVALID_TIMSTAMP
Signal this timestamp value is erroneous.
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_configuration_set(const rd_sensor_t *sensor, rd_sensor_configuration_t *config)
Implementation of ref rd_configuration_fp.
#define RD_SENSOR_CFG_MIN
Configure smallest supported and implemented value.
#define RD_SENSOR_CFG_NO_CHANGE
Do not change configured value.
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.
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.
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.
Ruuvi error codes and error check function.
#define BITS_PER_BYTE
Number of bits in a byte.
Ruuvi sensor interface Lifecycle: Beta
All sensors must implement configuration functions which accept this struct.
uint8_t resolution
Resolution, in bits.
uint8_t mode
Mode, RD_SENSOR_SLEEP, _SINGLE, _CONTINOUS.
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.
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
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.