Skip to content

Commit

Permalink
sensor: remove sensor value type
Browse files Browse the repository at this point in the history
Remove the type field from the sensor value structure. All values will
have the type previously defined by SENSOR_VALUE_TYPE_INT_PLUS_MICRO.

This simplifies the interface, as apps will know what value type to
expect. Apps that prefer to use double values can optain them using the
sensor_value_to_double function.

Change-Id: I3588d74258030eb16c3f89d8eead13cca4606b18
Signed-off-by: Bogdan Davidoaia <[email protected]>
  • Loading branch information
bogdan-davidoaia authored and Anas Nashif committed Jan 15, 2017
1 parent 662a2d6 commit 30162ae
Show file tree
Hide file tree
Showing 41 changed files with 44 additions and 253 deletions.
12 changes: 4 additions & 8 deletions doc/subsystems/sensor.rst
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,11 @@ Values
======

Sensor devices return results as :c:type:`struct sensor_value`. This
representation avoids use of floating point operations on setups where they
are not supported or desired.
representation avoids use of floating point values as they may not be
supported on certain setups.

Most sensor values have a type of :c:macro:`SENSOR_TYPE_INT_PLUS_MICRO`.
Other possible representations are listed below. Applications are
responsible for correctly interpreting the :c:data:`type` field of a
returned value.

.. doxygenenum:: sensor_value_type
.. doxygenstruct:: sensor_value
:members:

Fetching Values
===============
Expand Down
1 change: 0 additions & 1 deletion drivers/grove/light_sensor.c
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ static int gls_channel_get(struct device *dev,
ldr_val = (1023.0 - analog_val) * 10.0 / analog_val;
dval = 10000.0 / pow(ldr_val * 15.0, 4.0/3.0);

val->type = SENSOR_VALUE_TYPE_INT_PLUS_MICRO;
val->val1 = (int32_t)dval;
val->val2 = ((int32_t)(dval * 1000000)) % 1000000;

Expand Down
1 change: 0 additions & 1 deletion drivers/grove/temperature_sensor.c
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@ static int gts_channel_get(struct device *dev,
dval = 1 / (log(1023.0 / analog_val - 1.0) / B_CONST +
1 / 298.15) - 273.15;

val->type = SENSOR_VALUE_TYPE_INT_PLUS_MICRO;
val->val1 = (int32_t)dval;
val->val2 = ((int32_t)(dval * 1000000)) % 1000000;

Expand Down
1 change: 0 additions & 1 deletion drivers/sensor/ak8975/ak8975.c
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ static void ak8975_convert(struct sensor_value *val, int16_t sample,

conv_val = sample * AK8975_MICRO_GAUSS_PER_BIT *
((uint16_t)adjustment + 128) / 256;
val->type = SENSOR_VALUE_TYPE_INT_PLUS_MICRO;
val->val1 = conv_val / 1000000;
val->val2 = conv_val % 1000000;
}
Expand Down
2 changes: 0 additions & 2 deletions drivers/sensor/bma280/bma280.c
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@ static void bma280_channel_accel_convert(struct sensor_value *val,
* accel_val = (sample * BMA280_PMU_FULL_RAGE) /
* (2^data_width * 10^6)
*/
val->type = SENSOR_VALUE_TYPE_INT_PLUS_MICRO;
raw_val = (raw_val * BMA280_PMU_FULL_RANGE) /
(1 << (8 + BMA280_ACCEL_LSB_BITS));
val->val1 = raw_val / 1000000;
Expand Down Expand Up @@ -100,7 +99,6 @@ static int bma280_channel_get(struct device *dev,
bma280_channel_accel_convert(val + 2, drv_data->z_sample);
} else if (chan == SENSOR_CHAN_TEMP) {
/* temperature_val = 23 + sample / 2 */
val->type = SENSOR_VALUE_TYPE_INT_PLUS_MICRO;
val->val1 = (drv_data->temp_sample >> 1) + 23;
val->val2 = 500000 * (drv_data->temp_sample & 1);
return 0;
Expand Down
12 changes: 0 additions & 12 deletions drivers/sensor/bmc150_magn/bmc150_magn.c
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,6 @@ static int bmc150_magn_sample_fetch(struct device *dev,
static void bmc150_magn_convert(struct sensor_value *val, int raw_val)
{
/* val = raw_val / 1600 */
val->type = SENSOR_VALUE_TYPE_INT_PLUS_MICRO;
val->val1 = raw_val / 1600;
val->val2 = ((int32_t)raw_val * (1000000 / 1600)) % 1000000;
}
Expand Down Expand Up @@ -446,11 +445,6 @@ static int bmc150_magn_attr_set(struct device *dev,
switch (attr) {
#if defined(CONFIG_BMC150_MAGN_SAMPLING_RATE_RUNTIME)
case SENSOR_ATTR_SAMPLING_FREQUENCY:
if (val->type != SENSOR_VALUE_TYPE_INT_PLUS_MICRO) {
SYS_LOG_DBG("invalid parameter type");
return -ENOTSUP;
}

if (data->max_odr <= 0) {
if (bmc150_magn_compute_max_odr(dev, 0, 0,
&data->max_odr) < 0) {
Expand All @@ -470,13 +464,7 @@ static int bmc150_magn_attr_set(struct device *dev,
#endif
#if defined(BMC150_MAGN_SET_ATTR_REP)
case SENSOR_ATTR_OVERSAMPLING:
if (val->type != SENSOR_VALUE_TYPE_INT_PLUS_MICRO) {
SYS_LOG_DBG("invalid parameter type");
return -ENOTSUP;
}

bmc150_magn_attr_set_rep(dev, chan, val);

break;
#endif
default:
Expand Down
3 changes: 0 additions & 3 deletions drivers/sensor/bme280/bme280.c
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,6 @@ static int bme280_channel_get(struct device *dev,
* data->comp_temp has a resolution of 0.01 degC. So
* 5123 equals 51.23 degC.
*/
val->type = SENSOR_VALUE_TYPE_INT_PLUS_MICRO;
val->val1 = data->comp_temp / 100;
val->val2 = data->comp_temp % 100 * 10000;
break;
Expand All @@ -143,7 +142,6 @@ static int bme280_channel_get(struct device *dev,
* fractional. Output value of 24674867 represents
* 24674867/256 = 96386.2 Pa = 963.862 hPa
*/
val->type = SENSOR_VALUE_TYPE_INT_PLUS_MICRO;
val->val1 = (data->comp_press >> 8) / 1000;
val->val2 = (data->comp_press >> 8) % 1000 * 1000 +
(((data->comp_press & 0xff) * 1000) >> 8);
Expand All @@ -154,7 +152,6 @@ static int bme280_channel_get(struct device *dev,
* fractional. Output value of 47445 represents
* 47445/1024 = 46.333 %RH
*/
val->type = SENSOR_VALUE_TYPE_INT_PLUS_MICRO;
val->val1 = (data->comp_humidity >> 10);
val->val2 = (((data->comp_humidity & 0x3ff) * 1000 * 1000) >> 10);
val->val1 = val->val1 * 1000 + (val->val2 * 1000) / 1000000;
Expand Down
10 changes: 0 additions & 10 deletions drivers/sensor/bmg160/bmg160.c
Original file line number Diff line number Diff line change
Expand Up @@ -149,10 +149,6 @@ static int bmg160_attr_set(struct device *dev, enum sensor_channel chan,

switch (attr) {
case SENSOR_ATTR_FULL_SCALE:
if (val->type != SENSOR_VALUE_TYPE_INT_PLUS_MICRO) {
return -ENOTSUP;
}

range_dps = sensor_rad_to_degrees(val);

idx = bmg160_is_val_valid(range_dps,
Expand All @@ -171,10 +167,6 @@ static int bmg160_attr_set(struct device *dev, enum sensor_channel chan,
return 0;

case SENSOR_ATTR_SAMPLING_FREQUENCY:
if (val->type != SENSOR_VALUE_TYPE_INT_PLUS_MICRO) {
return -ENOTSUP;
}

idx = bmg160_is_val_valid(val->val1,
bmg160_sampling_freq_map,
BMG160_SAMPLING_FREQ_MAP_SIZE);
Expand Down Expand Up @@ -233,8 +225,6 @@ static void bmg160_to_fixed_point(struct bmg160_device_data *bmg160,
enum sensor_channel chan, int16_t raw,
struct sensor_value *val)
{
val->type = SENSOR_VALUE_TYPE_INT_PLUS_MICRO;

if (chan == SENSOR_CHAN_TEMP) {
val->val1 = 23 + (raw / 2);
val->val2 = (raw % 2) * 500000;
Expand Down
8 changes: 0 additions & 8 deletions drivers/sensor/bmg160/bmg160_trigger.c
Original file line number Diff line number Diff line change
Expand Up @@ -87,10 +87,6 @@ int bmg160_slope_config(struct device *dev, enum sensor_attribute attr,
uint16_t any_th_dps, range_dps;
uint8_t any_th_reg_val;

if (val->type != SENSOR_VALUE_TYPE_INT_PLUS_MICRO) {
return -EINVAL;
}

any_th_dps = sensor_rad_to_degrees(val);
range_dps = BMG160_SCALE_TO_RANGE(bmg160->scale);
any_th_reg_val = any_th_dps * 2000 / range_dps;
Expand All @@ -103,10 +99,6 @@ int bmg160_slope_config(struct device *dev, enum sensor_attribute attr,
return bmg160_write_byte(dev, BMG160_REG_THRES,
any_th_dps & BMG160_THRES_MASK);
} else if (attr == SENSOR_ATTR_SLOPE_DUR) {
if (val->type != SENSOR_VALUE_TYPE_INT_PLUS_MICRO) {
return -EINVAL;
}

/* slope duration can be 4, 8, 12 or 16 samples */
if (val->val1 != 4 && val->val1 != 8 &&
val->val1 != 12 && val->val1 != 16) {
Expand Down
23 changes: 0 additions & 23 deletions drivers/sensor/bmi160/bmi160.c
Original file line number Diff line number Diff line change
Expand Up @@ -369,10 +369,6 @@ static int bmi160_acc_ofs_set(struct device *dev, enum sensor_channel chan,
}

for (i = 0; i < 3; i++, ofs++) {
if (ofs->type != SENSOR_VALUE_TYPE_INT_PLUS_MICRO) {
return -EINVAL;
}

/* convert ofset to micro m/s^2 */
ofs_u = ofs->val1 * 1000000ULL + ofs->val2;
reg_val = ofs_u / BMI160_ACC_OFS_LSB;
Expand Down Expand Up @@ -446,18 +442,10 @@ static int bmi160_acc_config(struct device *dev, enum sensor_channel chan,
switch (attr) {
#if defined(CONFIG_BMI160_ACCEL_RANGE_RUNTIME)
case SENSOR_ATTR_FULL_SCALE:
if (val->type != SENSOR_VALUE_TYPE_INT_PLUS_MICRO) {
return -EINVAL;
}

return bmi160_acc_range_set(dev, sensor_ms2_to_g(val));
#endif
#if defined(CONFIG_BMI160_ACCEL_ODR_RUNTIME)
case SENSOR_ATTR_SAMPLING_FREQUENCY:
if (val->type != SENSOR_VALUE_TYPE_INT_PLUS_MICRO) {
return -EINVAL;
}

return bmi160_acc_odr_set(dev, val->val1, val->val2 / 1000);
#endif
case SENSOR_ATTR_OFFSET:
Expand Down Expand Up @@ -610,18 +598,10 @@ static int bmi160_gyr_config(struct device *dev, enum sensor_channel chan,
switch (attr) {
#if defined(CONFIG_BMI160_GYRO_RANGE_RUNTIME)
case SENSOR_ATTR_FULL_SCALE:
if (val->type != SENSOR_VALUE_TYPE_INT_PLUS_MICRO) {
return -EINVAL;
}

return bmi160_gyr_range_set(dev, sensor_rad_to_degrees(val));
#endif
#if defined(CONFIG_BMI160_GYRO_ODR_RUNTIME)
case SENSOR_ATTR_SAMPLING_FREQUENCY:
if (val->type != SENSOR_VALUE_TYPE_INT_PLUS_MICRO) {
return -EINVAL;
}

return bmi160_gyr_odr_set(dev, val->val1, val->val2 / 1000);
#endif
case SENSOR_ATTR_OFFSET:
Expand Down Expand Up @@ -701,8 +681,6 @@ static void bmi160_to_fixed_point(int16_t raw_val, uint16_t scale,
{
int32_t converted_val;

val->type = SENSOR_VALUE_TYPE_INT_PLUS_MICRO;

/*
* maximum converted value we can get is: max(raw_val) * max(scale)
* max(raw_val) = +/- 2^15
Expand Down Expand Up @@ -786,7 +764,6 @@ static int bmi160_temp_channel_get(struct device *dev, struct sensor_value *val)
/* the scale is 1/2^9/LSB = 1953 micro degrees */
temp_micro = BMI160_TEMP_OFFSET * 1000000ULL + temp_raw * 1953ULL;

val->type = SENSOR_VALUE_TYPE_INT_PLUS_MICRO;
val->val1 = temp_micro / 1000000ULL;
val->val2 = temp_micro % 1000000ULL;

Expand Down
8 changes: 0 additions & 8 deletions drivers/sensor/bmi160/bmi160_trigger.c
Original file line number Diff line number Diff line change
Expand Up @@ -209,10 +209,6 @@ int bmi160_acc_slope_config(struct device *dev, enum sensor_attribute attr,
uint32_t slope_th_ums2;

if (attr == SENSOR_ATTR_SLOPE_TH) {
if (val->type != SENSOR_VALUE_TYPE_INT_PLUS_MICRO) {
return -EINVAL;
}

if (bmi160_byte_read(dev, BMI160_REG_ACC_RANGE, &reg_val) < 0) {
return -EIO;
}
Expand All @@ -233,10 +229,6 @@ int bmi160_acc_slope_config(struct device *dev, enum sensor_attribute attr,
return -EIO;
}
} else { /* SENSOR_ATTR_SLOPE_DUR */
if (val->type != SENSOR_VALUE_TYPE_INT_PLUS_MICRO) {
return -EINVAL;
}

/* slope duration is measured in number of samples */
if (val->val1 < 1 || val->val1 > 4) {
return -ENOTSUP;
Expand Down
2 changes: 0 additions & 2 deletions drivers/sensor/dht/dht.c
Original file line number Diff line number Diff line change
Expand Up @@ -174,8 +174,6 @@ static int dht_channel_get(struct device *dev,

__ASSERT_NO_MSG(chan == SENSOR_CHAN_TEMP || chan == SENSOR_CHAN_HUMIDITY);

val->type = SENSOR_VALUE_TYPE_INT_PLUS_MICRO;

/* see data calculation example from datasheet */
#if defined(CONFIG_DHT_CHIP_DHT11)
/* use only integral data byte */
Expand Down
2 changes: 0 additions & 2 deletions drivers/sensor/fxos8700/fxos8700.c
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,6 @@ static void fxos8700_accel_convert(struct sensor_value *val, int16_t raw,
*/
val->val1 = (int32_t) micro_ms2 / 1000000;
val->val2 = (int32_t) micro_ms2 % 1000000;
val->type = SENSOR_VALUE_TYPE_INT_PLUS_MICRO;
}

static void fxos8700_magn_convert(struct sensor_value *val, int16_t raw)
Expand All @@ -109,7 +108,6 @@ static void fxos8700_magn_convert(struct sensor_value *val, int16_t raw)

val->val1 = micro_g / 1000000;
val->val2 = micro_g % 1000000;
val->type = SENSOR_VALUE_TYPE_INT_PLUS_MICRO;
}

static int fxos8700_channel_get(struct device *dev, enum sensor_channel chan,
Expand Down
2 changes: 0 additions & 2 deletions drivers/sensor/hdc1008/hdc1008.c
Original file line number Diff line number Diff line change
Expand Up @@ -80,13 +80,11 @@ static int hdc1008_channel_get(struct device *dev,
if (chan == SENSOR_CHAN_TEMP) {
/* val = -40 + 165 * sample / 2^16 */
tmp = 165 * (uint64_t)drv_data->t_sample;
val->type = SENSOR_VALUE_TYPE_INT_PLUS_MICRO;
val->val1 = (int32_t)(tmp >> 16) - 40;
val->val2 = (1000000 * (tmp & 0xFFFF)) >> 16;
} else if (chan == SENSOR_CHAN_HUMIDITY) {
/* val = 100000 * sample / 2^16 */
tmp = 100000 * (uint64_t)drv_data->rh_sample;
val->type = SENSOR_VALUE_TYPE_INT_PLUS_MICRO;
val->val1 = tmp >> 16;
val->val2 = (1000000 * (tmp & 0xFFFF)) >> 16;
} else {
Expand Down
1 change: 0 additions & 1 deletion drivers/sensor/hmc5883l/hmc5883l.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ static void hmc5883l_convert(struct sensor_value *val, int16_t raw_val,
uint16_t divider)
{
/* val = raw_val / divider */
val->type = SENSOR_VALUE_TYPE_INT_PLUS_MICRO;
val->val1 = raw_val / divider;
val->val2 = (((int64_t)raw_val % divider) * 1000000L) / divider;
}
Expand Down
6 changes: 0 additions & 6 deletions drivers/sensor/hp206c/hp206c.c
Original file line number Diff line number Diff line change
Expand Up @@ -153,10 +153,6 @@ static int hp206c_attr_set(struct device *dev, enum sensor_channel chan,
enum sensor_attribute attr,
const struct sensor_value *val)
{
if (val->type != SENSOR_VALUE_TYPE_INT_PLUS_MICRO) {
return -EINVAL;
}

#ifdef CONFIG_HP206C_OSR_RUNTIME
if (attr == SENSOR_ATTR_OVERSAMPLING) {
return hp206c_osr_set(dev, val->val1);
Expand Down Expand Up @@ -238,8 +234,6 @@ static int hp206c_val_get(struct device *dev,
temp = hp206c_buf_convert(buf, false);
}

val->type = SENSOR_VALUE_TYPE_INT_PLUS_MICRO;

if (cmd == HP206C_CMD_READ_P) {
val->val1 = temp / 1000;
val->val2 = temp % 1000 * 1000;
Expand Down
2 changes: 0 additions & 2 deletions drivers/sensor/hts221/hts221.c
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ static int hts221_channel_get(struct device *dev,
drv_data->t0_degc_x8;

/* convert temperature x8 to degrees Celsius */
val->type = SENSOR_VALUE_TYPE_INT_PLUS_MICRO;
val->val1 = conv_val / 8;
val->val2 = (conv_val % 8) * (1000000 / 8);
} else { /* SENSOR_CHAN_HUMIDITY */
Expand All @@ -54,7 +53,6 @@ static int hts221_channel_get(struct device *dev,
drv_data->h0_rh_x2;

/* convert humidity x2 to mili-percent */
val->type = SENSOR_VALUE_TYPE_INT_PLUS_MICRO;
val->val1 = conv_val * 500;
val->val2 = 0;
}
Expand Down
2 changes: 0 additions & 2 deletions drivers/sensor/isl29035/isl29035.c
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,11 @@ static int isl29035_channel_get(struct device *dev,
#if CONFIG_ISL29035_MODE_ALS
/* val = sample_val * lux_range / (2 ^ adc_data_bits) */
tmp = (uint64_t)drv_data->data_sample * ISL29035_LUX_RANGE;
val->type = SENSOR_VALUE_TYPE_INT_PLUS_MICRO;
val->val1 = tmp >> ISL29035_ADC_DATA_BITS;
tmp = (tmp & ISL29035_ADC_DATA_MASK) * 1000000;
val->val2 = tmp >> ISL29035_ADC_DATA_BITS;
#elif CONFIG_ISL29035_MODE_IR
ARG_UNUSED(tmp);
val->type = SENSOR_VALUE_TYPE_INT_INT_PLUS_MICRO;
val->val1 = drv_data->data_sample;
val->val2 = 0;
#endif
Expand Down
1 change: 0 additions & 1 deletion drivers/sensor/lis3dh/lis3dh.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
static void lis3dh_convert(struct sensor_value *val, int64_t raw_val)
{
/* val = raw_val * LIS3DH_ACCEL_SCALE / (10^6 * (2^16 - 1)) */
val->type = SENSOR_VALUE_TYPE_INT_PLUS_MICRO;
raw_val = raw_val * LIS3DH_ACCEL_SCALE / 1000000;
val->val1 = raw_val / 0xFFFF;
val->val2 = (raw_val % 0xFFFF) * 1000000 / 0xFFFF;
Expand Down
1 change: 0 additions & 1 deletion drivers/sensor/lis3mdl/lis3mdl.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ static void lis3mdl_convert(struct sensor_value *val, int16_t raw_val,
uint16_t divider)
{
/* val = raw_val / divider */
val->type = SENSOR_VALUE_TYPE_INT_PLUS_MICRO;
val->val1 = raw_val / divider;
val->val2 = (((int64_t)raw_val % divider) * 1000000L) / divider;
}
Expand Down
2 changes: 0 additions & 2 deletions drivers/sensor/lps25hb/lps25hb.c
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,6 @@ static inline void lps25hb_press_convert(struct sensor_value *val,
int32_t raw_val)
{
/* val = raw_val / 40960 */
val->type = SENSOR_VALUE_TYPE_INT_PLUS_MICRO;
val->val1 = raw_val / 40960;
val->val2 = ((int32_t)raw_val * 1000000 / 40960) % 1000000;
}
Expand All @@ -90,7 +89,6 @@ static inline void lps25hb_temp_convert(struct sensor_value *val,
int32_t uval;

/* val = raw_val / 480 + 42.5 */
val->type = SENSOR_VALUE_TYPE_INT_PLUS_MICRO;
uval = (int32_t)raw_val * 1000000 / 480 + 42500000;
val->val1 = (raw_val * 10 / 480 + 425) / 10;
val->val2 = uval % 1000000;
Expand Down
Loading

0 comments on commit 30162ae

Please sign in to comment.