Skip to content

Commit 9286b0b

Browse files
JordanYateskartben
authored andcommitted
sensor: additional conversion functions
Add additional conversion helpers for `deci` and `centi` output units. Signed-off-by: Jordan Yates <[email protected]>
1 parent 2f6ac20 commit 9286b0b

File tree

2 files changed

+27
-0
lines changed

2 files changed

+27
-0
lines changed

doc/releases/release-notes-4.2.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,11 @@ New APIs and options
163163

164164
* :kconfig:option:`CONFIG_NET_SOCKETS_INET_RAW`
165165

166+
* Sensor
167+
168+
* :c:func:`sensor_value_to_deci`
169+
* :c:func:`sensor_value_to_centi`
170+
166171
* Stepper
167172

168173
* :c:func:`stepper_stop()`

include/zephyr/drivers/sensor.h

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1468,6 +1468,28 @@ struct sensor_info {
14681468
#define SENSOR_DEVICE_DT_INST_DEFINE(inst, ...) \
14691469
SENSOR_DEVICE_DT_DEFINE(DT_DRV_INST(inst), __VA_ARGS__)
14701470

1471+
/**
1472+
* @brief Helper function for converting struct sensor_value to integer deci units.
1473+
*
1474+
* @param val A pointer to a sensor_value struct.
1475+
* @return The converted value.
1476+
*/
1477+
static inline int64_t sensor_value_to_deci(const struct sensor_value *val)
1478+
{
1479+
return ((int64_t)val->val1 * 10) + val->val2 / 100000;
1480+
}
1481+
1482+
/**
1483+
* @brief Helper function for converting struct sensor_value to integer centi units.
1484+
*
1485+
* @param val A pointer to a sensor_value struct.
1486+
* @return The converted value.
1487+
*/
1488+
static inline int64_t sensor_value_to_centi(const struct sensor_value *val)
1489+
{
1490+
return ((int64_t)val->val1 * 100) + val->val2 / 10000;
1491+
}
1492+
14711493
/**
14721494
* @brief Helper function for converting struct sensor_value to integer milli units.
14731495
*

0 commit comments

Comments
 (0)