Skip to content

Commit a1d0c7e

Browse files
committed
sys,phydat: fix scale_2_str for multi and non dimensional units
1 parent ce0a8ac commit a1d0c7e

File tree

2 files changed

+24
-3
lines changed

2 files changed

+24
-3
lines changed

sys/include/phydat.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,8 +164,9 @@ const char *phydat_unit_to_str(uint8_t unit);
164164
* etc) otherwise.
165165
*
166166
* @param[in] scale scale factor to convert
167+
* @param[in] unit unit related to scale
167168
*/
168-
char phydat_scale_to_str(int8_t scale);
169+
char phydat_scale_to_str(int8_t scale, uint8_t unit);
169170

170171
#ifdef __cplusplus
171172
}

sys/phydat/phydat_str.c

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ void phydat_dump(phydat_t *data, uint8_t dim)
3232
}
3333
printf("Data:");
3434
for (uint8_t i = 0; i < dim; i++) {
35-
char scale_str = phydat_scale_to_str(data->scale);
35+
char scale_str = phydat_scale_to_str(data->scale, data->unit);
3636

3737
printf("\t[%i] ", (int)i);
3838

@@ -80,8 +80,28 @@ const char *phydat_unit_to_str(uint8_t unit)
8080
}
8181
}
8282

83-
char phydat_scale_to_str(int8_t scale)
83+
char phydat_scale_to_str(int8_t scale, uint8_t unit)
8484
{
85+
/* extra handling for multi and non dimensional units */
86+
switch (unit) {
87+
case UNIT_M2: /* rescale for 2d unit */
88+
if (scale % 2 != 0) {
89+
return '\0';
90+
}
91+
scale /= 2;
92+
break;
93+
case UNIT_M3: /* rescale for 3d unit */
94+
if (scale % 3 != 0) {
95+
return '\0';
96+
}
97+
scale /= 3;
98+
break;
99+
case UNIT_UNDEF:
100+
case UNIT_NONE:
101+
case UNIT_PERCENT:
102+
return '\0'; /* no scaling */
103+
}
104+
85105
switch (scale) {
86106
case -3: return 'm';
87107
case -6: return 'u';

0 commit comments

Comments
 (0)