Skip to content

Commit 3c36cd4

Browse files
Delsiangalak
authored andcommitted
drivers: sensor: Add sensor_value_from_double
Add helper function to convert double to struct sensor_value. Signed-off-by: Eug Krashtan <[email protected]>
1 parent 73e3cd5 commit 3c36cd4

File tree

1 file changed

+12
-0
lines changed

1 file changed

+12
-0
lines changed

include/drivers/sensor.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -648,6 +648,18 @@ static inline double sensor_value_to_double(struct sensor_value *val)
648648
return (double)val->val1 + (double)val->val2 / 1000000;
649649
}
650650

651+
/**
652+
* @brief Helper function for converting double to struct sensor_value.
653+
*
654+
* @param val A pointer to a sensor_value struct.
655+
* @param inp The converted value.
656+
*/
657+
static inline void sensor_value_from_double(struct sensor_value *val, double inp)
658+
{
659+
val->val1 = (int32_t) inp;
660+
val->val2 = (int32_t)(inp * 1000000) % 1000000;
661+
}
662+
651663
/**
652664
* @}
653665
*/

0 commit comments

Comments
 (0)