nRF Connect SDK API 2.8.99
Loading...
Searching...
No Matches
caf_sensor_common.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2023 Nordic Semiconductor ASA
3 *
4 * SPDX-License-Identifier: LicenseRef-Nordic-5-Clause
5 */
6
7#ifndef _CAF_SENSOR_COMMON_H_
8#define _CAF_SENSOR_COMMON_H_
9
17#ifdef __cplusplus
18extern "C" {
19#endif
20
21#include <zephyr/drivers/sensor.h>
22
23
24/* This value has to be equal to fractional part of the sensor_value. */
25#define FLOAT_TO_SENSOR_VAL_CONST 1000000
26
27#define FLOAT_TO_SENSOR_VALUE(float_val) \
28 { \
29 .val1 = (int32_t)(float_val), \
30 .val2 = (int32_t)(((float_val) - (int32_t)(float_val)) * \
31 FLOAT_TO_SENSOR_VAL_CONST), \
32 }
33
41 enum sensor_channel chan;
43 uint8_t data_cnt;
44};
45
53static inline bool sensor_value_greater_then(struct sensor_value sensor_val1,
54 struct sensor_value sensor_val2)
55{
56 return ((sensor_val1.val1 > sensor_val2.val1) ||
57 ((sensor_val1.val1 == sensor_val2.val1) && (sensor_val1.val2 > sensor_val2.val2)));
58}
59
67static inline struct sensor_value sensor_value_abs_difference(struct sensor_value sensor_val1,
68 struct sensor_value sensor_val2)
69{
70 struct sensor_value result;
71
72 result.val1 = abs(sensor_val1.val1 - sensor_val2.val1);
73 result.val2 = abs(sensor_val1.val2 - sensor_val2.val2);
74 if (result.val2 > FLOAT_TO_SENSOR_VAL_CONST) {
75 result.val2 = result.val2 - FLOAT_TO_SENSOR_VAL_CONST;
76 result.val1++;
77 }
78 return result;
79}
80
81#ifdef __cplusplus
82}
83#endif
84
89#endif /* _CAF_SENSOR_COMMON_H_ */
static struct sensor_value sensor_value_abs_difference(struct sensor_value sensor_val1, struct sensor_value sensor_val2)
Helper function for calculating absolute value of difference of two sensor_values.
Definition caf_sensor_common.h:67
static bool sensor_value_greater_then(struct sensor_value sensor_val1, struct sensor_value sensor_val2)
Helper function for checking if one sensor_value is greater than the other.
Definition caf_sensor_common.h:53
#define FLOAT_TO_SENSOR_VAL_CONST
Definition caf_sensor_common.h:25
enum sensor_channel chan
Channel identifier.
Definition caf_sensor_common.h:41
uint8_t data_cnt
Number of data samples in selected channel.
Definition caf_sensor_common.h:43
Description of single channel.
Definition caf_sensor_common.h:39