sensors: shell: implement rounding for q31_t
Since the sensor shell command was converted to use qt31_t, all the integer values started to show up as rounded up by a fractional unit when displayed, due to the conversion always rounding down. Fix that by using the recently introduced DIV_ROUND_CLOSEST and handling rounding up to next integer explicitly. Before: channel idx=44 gauge_state_of_charge value=83.999999 after: channel idx=44 gauge_state_of_charge value=84.000000 Signed-off-by: Fabio Baltieri <fabiobaltieri@google.com>
This commit is contained in:
parent
409b15c7ce
commit
13dfa0ac27
|
@ -14,6 +14,7 @@
|
|||
#include <zephyr/rtio/rtio.h>
|
||||
#include <zephyr/shell/shell.h>
|
||||
#include <zephyr/sys/iterable_sections.h>
|
||||
#include <zephyr/sys/util.h>
|
||||
|
||||
LOG_MODULE_REGISTER(sensor_shell);
|
||||
|
||||
|
@ -286,8 +287,14 @@ static void sensor_shell_processing_callback(int result, uint8_t *buf, uint32_t
|
|||
|
||||
scaled_value = llabs(scaled_value);
|
||||
numerator = (int)FIELD_GET(GENMASK64(31 + shift, 31), scaled_value);
|
||||
denominator =
|
||||
(int)((FIELD_GET(GENMASK64(30, 0), scaled_value) * 1000000) / INT32_MAX);
|
||||
denominator = (int)DIV_ROUND_CLOSEST(
|
||||
FIELD_GET(GENMASK64(30, 0), scaled_value) * 1000000,
|
||||
INT32_MAX);
|
||||
|
||||
if (denominator == 1000000) {
|
||||
numerator++;
|
||||
denominator = 0;
|
||||
}
|
||||
|
||||
if (channel >= ARRAY_SIZE(sensor_channel_name)) {
|
||||
shell_print(ctx->sh, "channel idx=%d value=%s%d.%06d", channel,
|
||||
|
|
Loading…
Reference in a new issue