sensor: Fix samples that assume incorrect sensor_value type.
Various sensor samples are hardwired to expect returned sensor values are represented as doubles. In each case this assumption is incorrect. Introduce a generic sensor_value to double helper function and adjust the samples to use it. Change-Id: I89c788686576562b84e07a36064640231340c33b Signed-off-by: Marcus Shawcroft <marcus.shawcroft@arm.com>
This commit is contained in:
parent
e54e66a3d0
commit
f0da59b062
@ -458,6 +458,24 @@ static inline void sensor_degrees_to_rad(int32_t d, struct sensor_value *rad)
|
||||
rad->val2 = ((int64_t)d * SENSOR_PI / 180LL) % 1000000LL;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Helper function for converting struct sensor_value to double.
|
||||
*
|
||||
* @param val A pointer to a sensor_value struct.
|
||||
* @return The converted value.
|
||||
*/
|
||||
static inline double sensor_value_to_double(struct sensor_value *val)
|
||||
{
|
||||
switch (val->type) {
|
||||
case SENSOR_VALUE_TYPE_INT_PLUS_MICRO:
|
||||
return (double)val->val1 + (double)val->val2 / 1000000;
|
||||
case SENSOR_VALUE_TYPE_DOUBLE:
|
||||
return val->dval;
|
||||
}
|
||||
|
||||
return 0.0;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief configuration parameters for sensor triggers.
|
||||
*/
|
||||
|
||||
@ -36,9 +36,8 @@ void main(void)
|
||||
sensor_sample_fetch(dev);
|
||||
sensor_channel_get(dev, SENSOR_CHAN_LIGHT, &lux);
|
||||
|
||||
printf("lux: %f\n", lux.dval);
|
||||
printf("lux: %f\n", sensor_value_to_double(&lux));
|
||||
|
||||
k_sleep(SLEEP_TIME);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -67,13 +67,14 @@ void main(void)
|
||||
|
||||
/* display temperature on LCD */
|
||||
glcd_cursor_pos_set(glcd, 0, 0);
|
||||
sprintf(row, "T:%.2f%cC", temp.dval, 223 /* degree symbol */);
|
||||
sprintf(row, "T:%.2f%cC",
|
||||
sensor_value_to_double(&temp),
|
||||
223 /* degree symbol */);
|
||||
glcd_print(glcd, row, strlen(row));
|
||||
|
||||
#endif
|
||||
printf("Temperature: %.2f C\n", temp.dval);
|
||||
printf("Temperature: %.2f C\n", sensor_value_to_double(&temp));
|
||||
|
||||
k_sleep(SLEEP_TIME);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -35,7 +35,10 @@ static void do_main(struct device *dev)
|
||||
ret = sensor_channel_get(dev, SENSOR_CHAN_MAGN_X, &value_x);
|
||||
ret = sensor_channel_get(dev, SENSOR_CHAN_MAGN_Y, &value_y);
|
||||
ret = sensor_channel_get(dev, SENSOR_CHAN_MAGN_Z, &value_z);
|
||||
printf("( x y z ) = ( %f %f %f )\n", value_x.dval, value_y.dval, value_z.dval);
|
||||
printf("( x y z ) = ( %f %f %f )\n",
|
||||
sensor_value_to_double(&value_x),
|
||||
sensor_value_to_double(&value_y),
|
||||
sensor_value_to_double(&value_z));
|
||||
|
||||
k_sleep(500);
|
||||
}
|
||||
|
||||
@ -50,7 +50,7 @@ void main(void)
|
||||
return;
|
||||
}
|
||||
|
||||
lum = val.dval;
|
||||
lum = val.val1;
|
||||
printk("sensor: lum reading: %d\n", lum);
|
||||
|
||||
k_sleep(4000);
|
||||
|
||||
@ -99,12 +99,14 @@ void main(void)
|
||||
|
||||
/* display temperature on LCD */
|
||||
glcd_cursor_pos_set(glcd, 0, 0);
|
||||
sprintf(row, "T:%.1f%cC", val[0].dval, 223 /* degree symbol */);
|
||||
sprintf(row, "T:%.1f%cC", sensor_value_to_double(val),
|
||||
223 /* degree symbol */);
|
||||
glcd_print(glcd, row, strlen(row));
|
||||
|
||||
/* display himidity on LCD */
|
||||
glcd_cursor_pos_set(glcd, 17 - strlen(row), 0);
|
||||
sprintf(row, "RH:%.0f%c", val[1].dval, 37 /* percent symbol */);
|
||||
sprintf(row, "RH:%.0f%c", sensor_value_to_double(val + 1),
|
||||
37 /* percent symbol */);
|
||||
glcd_print(glcd, row, strlen(row));
|
||||
|
||||
#endif
|
||||
|
||||
Loading…
Reference in New Issue
Block a user