casaroli commented on code in PR #21537:
URL: https://github.com/apache/nuttx/pull/21537#discussion_r4501772648


##########
drivers/sensors/Kconfig:
##########
@@ -29,6 +29,7 @@ config SENSORS_USE_FLOAT
 
 config SENSORS_USE_B16
        bool "Use b16_t as data type for sensors"
+       select LIBC_BFORMAT_B16

Review Comment:
   With this option every `%hf` in a binary format is a `b16_t`, also for a 
real `float` member. `struct sensor_eng` has `float voltage[4]` 
(include/nuttx/uorb.h:1102), and apps/system/uorb/sensor/eng.c prints it with 
`eng0:%hf`..`eng3:%hf`, so a B16 build now prints 3300.0 as 17742.25, and 
uorb_generator stores `b16_t` bits into the float.
   Fix: change `voltage` to `sensor_data_t voltage[4]` in include/nuttx/uorb.h 
in this PR, as all the other sensor members.



##########
libs/libc/stream/lib_libvscanf.c:
##########
@@ -1101,7 +1111,36 @@ static int vscanf_internal(FAR struct lib_instream_s 
*stream, FAR int *lastc,
                           /* Return the float value */
 
 #  ifdef CONFIG_HAVE_FLOAT
-                          *pf = fvalue;
+#    ifdef CONFIG_LIBC_BFORMAT_B16
+                          if (modifier == H_MOD && !varg)
+                            {
+                              /* Binary format: %hf is a b16_t, rounded to
+                               * the nearest value and saturated.
+                               */
+
+                              FAR b16_t *pb16 = (FAR b16_t *)pf;
+
+                              if (fvalue >= 32768.0f)
+                                {
+                                  *pb16 = b16MAX;
+                                }
+                              else if (fvalue <= -32768.0f)
+                                {
+                                  *pb16 = b16MIN;
+                                }
+                              else
+                                {
+                                  float r = fvalue * 65536.0f;

Review Comment:
   The product is computed in `float` (24-bit mantissa), so above 256 the 
result is not the nearest `b16_t`: 300.0001 gives 19660806 (nearest is 
19660807) and 32767.99 is 15 LSB off.
   Fix: under `CONFIG_HAVE_DOUBLE`, convert with `strtod()` and round `dvalue * 
65536.0`, or say "rounded in float" in the commit message and the PR body.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to