Skip to content

Commit d4d20c5

Browse files
Vincent GenevesMaureenHelm
authored andcommitted
drivers: sensor: add helper functions to convert float
These helper functions can be used to avoid dependency to double type and save some space in ROM. Signed-off-by: Vincent Geneves <[email protected]>
1 parent ff36548 commit d4d20c5

File tree

2 files changed

+48
-1
lines changed

2 files changed

+48
-1
lines changed

include/zephyr/drivers/sensor.h

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -737,6 +737,17 @@ static inline double sensor_value_to_double(const struct sensor_value *val)
737737
return (double)val->val1 + (double)val->val2 / 1000000;
738738
}
739739

740+
/**
741+
* @brief Helper function for converting struct sensor_value to float.
742+
*
743+
* @param val A pointer to a sensor_value struct.
744+
* @return The converted value.
745+
*/
746+
static inline float sensor_value_to_float(const struct sensor_value *val)
747+
{
748+
return (float)val->val1 + (float)val->val2 / 1000000;
749+
}
750+
740751
/**
741752
* @brief Helper function for converting double to struct sensor_value.
742753
*
@@ -762,6 +773,27 @@ static inline int sensor_value_from_double(struct sensor_value *val, double inp)
762773
return 0;
763774
}
764775

776+
/**
777+
* @brief Helper function for converting float to struct sensor_value.
778+
*
779+
* @param val A pointer to a sensor_value struct.
780+
* @param inp The converted value.
781+
* @return 0 if successful, negative errno code if failure.
782+
*/
783+
static inline int sensor_value_from_float(struct sensor_value *val, float inp)
784+
{
785+
float val2 = (inp - (int32_t)inp) * 1000000.0;
786+
787+
if (val2 < INT32_MIN || val2 > (float)(INT32_MAX - 1)) {
788+
return -ERANGE;
789+
}
790+
791+
val->val1 = (int32_t)inp;
792+
val->val2 = (int32_t)val2;
793+
794+
return 0;
795+
}
796+
765797
#ifdef CONFIG_SENSOR_INFO
766798

767799
struct sensor_info {

tests/drivers/sensor/generic/src/main.c

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -306,10 +306,25 @@ ZTEST(sensor_api, test_sensor_unit_conversion)
306306
/* reset test data to positive value */
307307
data.val1 = -data.val1;
308308
data.val2 = -data.val2;
309-
/* Test struct sensor_value to double */
310309
#if defined(CONFIG_FPU)
310+
/* Test struct sensor_value to double and float */
311311
zassert_equal((long long)(sensor_value_to_double(&data) * 1000000LL),
312312
SENSOR_PI, "the data does not match");
313+
zassert_equal((long long)(sensor_value_to_float(&data) * 1000000LL),
314+
SENSOR_PI, "the data does not match");
315+
316+
/* Test struct sensor_value from double and float */
317+
sensor_value_from_double(&data, (double)(SENSOR_PI) / 1000000.0);
318+
zassert_equal(data.val1, SENSOR_PI/1000000LL,
319+
"the data does not match");
320+
zassert_equal(data.val2, SENSOR_PI%(data.val1 * 1000000LL),
321+
"the data does not match");
322+
323+
sensor_value_from_float(&data, (float)(SENSOR_PI) / 1000000.0);
324+
zassert_equal(data.val1, SENSOR_PI/1000000LL,
325+
"the data does not match");
326+
zassert_equal(data.val2, SENSOR_PI%(data.val1 * 1000000LL),
327+
"the data does not match");
313328
#endif
314329
/* reset test data to positive value */
315330
data.val1 = 3;

0 commit comments

Comments
 (0)