sensor: remove sensor value type

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 <bogdan.davidoaia@linaro.org>
This commit is contained in:
Bogdan Davidoaia 2017-01-12 15:28:25 +02:00 committed by Anas Nashif
commit 30162aedf1
41 changed files with 44 additions and 253 deletions

View file

@ -31,15 +31,11 @@ Values
====== ======
Sensor devices return results as :c:type:`struct sensor_value`. This Sensor devices return results as :c:type:`struct sensor_value`. This
representation avoids use of floating point operations on setups where they representation avoids use of floating point values as they may not be
are not supported or desired. supported on certain setups.
Most sensor values have a type of :c:macro:`SENSOR_TYPE_INT_PLUS_MICRO`. .. doxygenstruct:: sensor_value
Other possible representations are listed below. Applications are :members:
responsible for correctly interpreting the :c:data:`type` field of a
returned value.
.. doxygenenum:: sensor_value_type
Fetching Values Fetching Values
=============== ===============

View file

@ -59,7 +59,6 @@ static int gls_channel_get(struct device *dev,
ldr_val = (1023.0 - analog_val) * 10.0 / analog_val; ldr_val = (1023.0 - analog_val) * 10.0 / analog_val;
dval = 10000.0 / pow(ldr_val * 15.0, 4.0/3.0); 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->val1 = (int32_t)dval;
val->val2 = ((int32_t)(dval * 1000000)) % 1000000; val->val2 = ((int32_t)(dval * 1000000)) % 1000000;

View file

@ -66,7 +66,6 @@ static int gts_channel_get(struct device *dev,
dval = 1 / (log(1023.0 / analog_val - 1.0) / B_CONST + dval = 1 / (log(1023.0 / analog_val - 1.0) / B_CONST +
1 / 298.15) - 273.15; 1 / 298.15) - 273.15;
val->type = SENSOR_VALUE_TYPE_INT_PLUS_MICRO;
val->val1 = (int32_t)dval; val->val1 = (int32_t)dval;
val->val2 = ((int32_t)(dval * 1000000)) % 1000000; val->val2 = ((int32_t)(dval * 1000000)) % 1000000;

View file

@ -59,7 +59,6 @@ static void ak8975_convert(struct sensor_value *val, int16_t sample,
conv_val = sample * AK8975_MICRO_GAUSS_PER_BIT * conv_val = sample * AK8975_MICRO_GAUSS_PER_BIT *
((uint16_t)adjustment + 128) / 256; ((uint16_t)adjustment + 128) / 256;
val->type = SENSOR_VALUE_TYPE_INT_PLUS_MICRO;
val->val1 = conv_val / 1000000; val->val1 = conv_val / 1000000;
val->val2 = conv_val % 1000000; val->val2 = conv_val % 1000000;
} }

View file

@ -65,7 +65,6 @@ static void bma280_channel_accel_convert(struct sensor_value *val,
* accel_val = (sample * BMA280_PMU_FULL_RAGE) / * accel_val = (sample * BMA280_PMU_FULL_RAGE) /
* (2^data_width * 10^6) * (2^data_width * 10^6)
*/ */
val->type = SENSOR_VALUE_TYPE_INT_PLUS_MICRO;
raw_val = (raw_val * BMA280_PMU_FULL_RANGE) / raw_val = (raw_val * BMA280_PMU_FULL_RANGE) /
(1 << (8 + BMA280_ACCEL_LSB_BITS)); (1 << (8 + BMA280_ACCEL_LSB_BITS));
val->val1 = raw_val / 1000000; val->val1 = raw_val / 1000000;
@ -100,7 +99,6 @@ static int bma280_channel_get(struct device *dev,
bma280_channel_accel_convert(val + 2, drv_data->z_sample); bma280_channel_accel_convert(val + 2, drv_data->z_sample);
} else if (chan == SENSOR_CHAN_TEMP) { } else if (chan == SENSOR_CHAN_TEMP) {
/* temperature_val = 23 + sample / 2 */ /* temperature_val = 23 + sample / 2 */
val->type = SENSOR_VALUE_TYPE_INT_PLUS_MICRO;
val->val1 = (drv_data->temp_sample >> 1) + 23; val->val1 = (drv_data->temp_sample >> 1) + 23;
val->val2 = 500000 * (drv_data->temp_sample & 1); val->val2 = 500000 * (drv_data->temp_sample & 1);
return 0; return 0;

View file

@ -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) static void bmc150_magn_convert(struct sensor_value *val, int raw_val)
{ {
/* val = raw_val / 1600 */ /* val = raw_val / 1600 */
val->type = SENSOR_VALUE_TYPE_INT_PLUS_MICRO;
val->val1 = raw_val / 1600; val->val1 = raw_val / 1600;
val->val2 = ((int32_t)raw_val * (1000000 / 1600)) % 1000000; val->val2 = ((int32_t)raw_val * (1000000 / 1600)) % 1000000;
} }
@ -446,11 +445,6 @@ static int bmc150_magn_attr_set(struct device *dev,
switch (attr) { switch (attr) {
#if defined(CONFIG_BMC150_MAGN_SAMPLING_RATE_RUNTIME) #if defined(CONFIG_BMC150_MAGN_SAMPLING_RATE_RUNTIME)
case SENSOR_ATTR_SAMPLING_FREQUENCY: 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 (data->max_odr <= 0) {
if (bmc150_magn_compute_max_odr(dev, 0, 0, if (bmc150_magn_compute_max_odr(dev, 0, 0,
&data->max_odr) < 0) { &data->max_odr) < 0) {
@ -470,13 +464,7 @@ static int bmc150_magn_attr_set(struct device *dev,
#endif #endif
#if defined(BMC150_MAGN_SET_ATTR_REP) #if defined(BMC150_MAGN_SET_ATTR_REP)
case SENSOR_ATTR_OVERSAMPLING: 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); bmc150_magn_attr_set_rep(dev, chan, val);
break; break;
#endif #endif
default: default:

View file

@ -133,7 +133,6 @@ static int bme280_channel_get(struct device *dev,
* data->comp_temp has a resolution of 0.01 degC. So * data->comp_temp has a resolution of 0.01 degC. So
* 5123 equals 51.23 degC. * 5123 equals 51.23 degC.
*/ */
val->type = SENSOR_VALUE_TYPE_INT_PLUS_MICRO;
val->val1 = data->comp_temp / 100; val->val1 = data->comp_temp / 100;
val->val2 = data->comp_temp % 100 * 10000; val->val2 = data->comp_temp % 100 * 10000;
break; break;
@ -143,7 +142,6 @@ static int bme280_channel_get(struct device *dev,
* fractional. Output value of 24674867 represents * fractional. Output value of 24674867 represents
* 24674867/256 = 96386.2 Pa = 963.862 hPa * 24674867/256 = 96386.2 Pa = 963.862 hPa
*/ */
val->type = SENSOR_VALUE_TYPE_INT_PLUS_MICRO;
val->val1 = (data->comp_press >> 8) / 1000; val->val1 = (data->comp_press >> 8) / 1000;
val->val2 = (data->comp_press >> 8) % 1000 * 1000 + val->val2 = (data->comp_press >> 8) % 1000 * 1000 +
(((data->comp_press & 0xff) * 1000) >> 8); (((data->comp_press & 0xff) * 1000) >> 8);
@ -154,7 +152,6 @@ static int bme280_channel_get(struct device *dev,
* fractional. Output value of 47445 represents * fractional. Output value of 47445 represents
* 47445/1024 = 46.333 %RH * 47445/1024 = 46.333 %RH
*/ */
val->type = SENSOR_VALUE_TYPE_INT_PLUS_MICRO;
val->val1 = (data->comp_humidity >> 10); val->val1 = (data->comp_humidity >> 10);
val->val2 = (((data->comp_humidity & 0x3ff) * 1000 * 1000) >> 10); val->val2 = (((data->comp_humidity & 0x3ff) * 1000 * 1000) >> 10);
val->val1 = val->val1 * 1000 + (val->val2 * 1000) / 1000000; val->val1 = val->val1 * 1000 + (val->val2 * 1000) / 1000000;

View file

@ -149,10 +149,6 @@ static int bmg160_attr_set(struct device *dev, enum sensor_channel chan,
switch (attr) { switch (attr) {
case SENSOR_ATTR_FULL_SCALE: case SENSOR_ATTR_FULL_SCALE:
if (val->type != SENSOR_VALUE_TYPE_INT_PLUS_MICRO) {
return -ENOTSUP;
}
range_dps = sensor_rad_to_degrees(val); range_dps = sensor_rad_to_degrees(val);
idx = bmg160_is_val_valid(range_dps, idx = bmg160_is_val_valid(range_dps,
@ -171,10 +167,6 @@ static int bmg160_attr_set(struct device *dev, enum sensor_channel chan,
return 0; return 0;
case SENSOR_ATTR_SAMPLING_FREQUENCY: case SENSOR_ATTR_SAMPLING_FREQUENCY:
if (val->type != SENSOR_VALUE_TYPE_INT_PLUS_MICRO) {
return -ENOTSUP;
}
idx = bmg160_is_val_valid(val->val1, idx = bmg160_is_val_valid(val->val1,
bmg160_sampling_freq_map, bmg160_sampling_freq_map,
BMG160_SAMPLING_FREQ_MAP_SIZE); BMG160_SAMPLING_FREQ_MAP_SIZE);
@ -233,8 +225,6 @@ static void bmg160_to_fixed_point(struct bmg160_device_data *bmg160,
enum sensor_channel chan, int16_t raw, enum sensor_channel chan, int16_t raw,
struct sensor_value *val) struct sensor_value *val)
{ {
val->type = SENSOR_VALUE_TYPE_INT_PLUS_MICRO;
if (chan == SENSOR_CHAN_TEMP) { if (chan == SENSOR_CHAN_TEMP) {
val->val1 = 23 + (raw / 2); val->val1 = 23 + (raw / 2);
val->val2 = (raw % 2) * 500000; val->val2 = (raw % 2) * 500000;

View file

@ -87,10 +87,6 @@ int bmg160_slope_config(struct device *dev, enum sensor_attribute attr,
uint16_t any_th_dps, range_dps; uint16_t any_th_dps, range_dps;
uint8_t any_th_reg_val; 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); any_th_dps = sensor_rad_to_degrees(val);
range_dps = BMG160_SCALE_TO_RANGE(bmg160->scale); range_dps = BMG160_SCALE_TO_RANGE(bmg160->scale);
any_th_reg_val = any_th_dps * 2000 / range_dps; any_th_reg_val = any_th_dps * 2000 / range_dps;
@ -103,10 +99,6 @@ int bmg160_slope_config(struct device *dev, enum sensor_attribute attr,
return bmg160_write_byte(dev, BMG160_REG_THRES, return bmg160_write_byte(dev, BMG160_REG_THRES,
any_th_dps & BMG160_THRES_MASK); any_th_dps & BMG160_THRES_MASK);
} else if (attr == SENSOR_ATTR_SLOPE_DUR) { } 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 */ /* slope duration can be 4, 8, 12 or 16 samples */
if (val->val1 != 4 && val->val1 != 8 && if (val->val1 != 4 && val->val1 != 8 &&
val->val1 != 12 && val->val1 != 16) { val->val1 != 12 && val->val1 != 16) {

View file

@ -369,10 +369,6 @@ static int bmi160_acc_ofs_set(struct device *dev, enum sensor_channel chan,
} }
for (i = 0; i < 3; i++, ofs++) { 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 */ /* convert ofset to micro m/s^2 */
ofs_u = ofs->val1 * 1000000ULL + ofs->val2; ofs_u = ofs->val1 * 1000000ULL + ofs->val2;
reg_val = ofs_u / BMI160_ACC_OFS_LSB; reg_val = ofs_u / BMI160_ACC_OFS_LSB;
@ -446,18 +442,10 @@ static int bmi160_acc_config(struct device *dev, enum sensor_channel chan,
switch (attr) { switch (attr) {
#if defined(CONFIG_BMI160_ACCEL_RANGE_RUNTIME) #if defined(CONFIG_BMI160_ACCEL_RANGE_RUNTIME)
case SENSOR_ATTR_FULL_SCALE: 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)); return bmi160_acc_range_set(dev, sensor_ms2_to_g(val));
#endif #endif
#if defined(CONFIG_BMI160_ACCEL_ODR_RUNTIME) #if defined(CONFIG_BMI160_ACCEL_ODR_RUNTIME)
case SENSOR_ATTR_SAMPLING_FREQUENCY: 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); return bmi160_acc_odr_set(dev, val->val1, val->val2 / 1000);
#endif #endif
case SENSOR_ATTR_OFFSET: case SENSOR_ATTR_OFFSET:
@ -610,18 +598,10 @@ static int bmi160_gyr_config(struct device *dev, enum sensor_channel chan,
switch (attr) { switch (attr) {
#if defined(CONFIG_BMI160_GYRO_RANGE_RUNTIME) #if defined(CONFIG_BMI160_GYRO_RANGE_RUNTIME)
case SENSOR_ATTR_FULL_SCALE: 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)); return bmi160_gyr_range_set(dev, sensor_rad_to_degrees(val));
#endif #endif
#if defined(CONFIG_BMI160_GYRO_ODR_RUNTIME) #if defined(CONFIG_BMI160_GYRO_ODR_RUNTIME)
case SENSOR_ATTR_SAMPLING_FREQUENCY: 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); return bmi160_gyr_odr_set(dev, val->val1, val->val2 / 1000);
#endif #endif
case SENSOR_ATTR_OFFSET: case SENSOR_ATTR_OFFSET:
@ -701,8 +681,6 @@ static void bmi160_to_fixed_point(int16_t raw_val, uint16_t scale,
{ {
int32_t converted_val; int32_t converted_val;
val->type = SENSOR_VALUE_TYPE_INT_PLUS_MICRO;
/* /*
* maximum converted value we can get is: max(raw_val) * max(scale) * maximum converted value we can get is: max(raw_val) * max(scale)
* max(raw_val) = +/- 2^15 * max(raw_val) = +/- 2^15
@ -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 */ /* the scale is 1/2^9/LSB = 1953 micro degrees */
temp_micro = BMI160_TEMP_OFFSET * 1000000ULL + temp_raw * 1953ULL; temp_micro = BMI160_TEMP_OFFSET * 1000000ULL + temp_raw * 1953ULL;
val->type = SENSOR_VALUE_TYPE_INT_PLUS_MICRO;
val->val1 = temp_micro / 1000000ULL; val->val1 = temp_micro / 1000000ULL;
val->val2 = temp_micro % 1000000ULL; val->val2 = temp_micro % 1000000ULL;

View file

@ -209,10 +209,6 @@ int bmi160_acc_slope_config(struct device *dev, enum sensor_attribute attr,
uint32_t slope_th_ums2; uint32_t slope_th_ums2;
if (attr == SENSOR_ATTR_SLOPE_TH) { 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) { if (bmi160_byte_read(dev, BMI160_REG_ACC_RANGE, &reg_val) < 0) {
return -EIO; return -EIO;
} }
@ -233,10 +229,6 @@ int bmi160_acc_slope_config(struct device *dev, enum sensor_attribute attr,
return -EIO; return -EIO;
} }
} else { /* SENSOR_ATTR_SLOPE_DUR */ } else { /* SENSOR_ATTR_SLOPE_DUR */
if (val->type != SENSOR_VALUE_TYPE_INT_PLUS_MICRO) {
return -EINVAL;
}
/* slope duration is measured in number of samples */ /* slope duration is measured in number of samples */
if (val->val1 < 1 || val->val1 > 4) { if (val->val1 < 1 || val->val1 > 4) {
return -ENOTSUP; return -ENOTSUP;

View file

@ -174,8 +174,6 @@ static int dht_channel_get(struct device *dev,
__ASSERT_NO_MSG(chan == SENSOR_CHAN_TEMP || chan == SENSOR_CHAN_HUMIDITY); __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 */ /* see data calculation example from datasheet */
#if defined(CONFIG_DHT_CHIP_DHT11) #if defined(CONFIG_DHT_CHIP_DHT11)
/* use only integral data byte */ /* use only integral data byte */

View file

@ -95,7 +95,6 @@ static void fxos8700_accel_convert(struct sensor_value *val, int16_t raw,
*/ */
val->val1 = (int32_t) micro_ms2 / 1000000; val->val1 = (int32_t) micro_ms2 / 1000000;
val->val2 = (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) static void fxos8700_magn_convert(struct sensor_value *val, int16_t raw)
@ -109,7 +108,6 @@ static void fxos8700_magn_convert(struct sensor_value *val, int16_t raw)
val->val1 = micro_g / 1000000; val->val1 = micro_g / 1000000;
val->val2 = 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, static int fxos8700_channel_get(struct device *dev, enum sensor_channel chan,

View file

@ -80,13 +80,11 @@ static int hdc1008_channel_get(struct device *dev,
if (chan == SENSOR_CHAN_TEMP) { if (chan == SENSOR_CHAN_TEMP) {
/* val = -40 + 165 * sample / 2^16 */ /* val = -40 + 165 * sample / 2^16 */
tmp = 165 * (uint64_t)drv_data->t_sample; tmp = 165 * (uint64_t)drv_data->t_sample;
val->type = SENSOR_VALUE_TYPE_INT_PLUS_MICRO;
val->val1 = (int32_t)(tmp >> 16) - 40; val->val1 = (int32_t)(tmp >> 16) - 40;
val->val2 = (1000000 * (tmp & 0xFFFF)) >> 16; val->val2 = (1000000 * (tmp & 0xFFFF)) >> 16;
} else if (chan == SENSOR_CHAN_HUMIDITY) { } else if (chan == SENSOR_CHAN_HUMIDITY) {
/* val = 100000 * sample / 2^16 */ /* val = 100000 * sample / 2^16 */
tmp = 100000 * (uint64_t)drv_data->rh_sample; tmp = 100000 * (uint64_t)drv_data->rh_sample;
val->type = SENSOR_VALUE_TYPE_INT_PLUS_MICRO;
val->val1 = tmp >> 16; val->val1 = tmp >> 16;
val->val2 = (1000000 * (tmp & 0xFFFF)) >> 16; val->val2 = (1000000 * (tmp & 0xFFFF)) >> 16;
} else { } else {

View file

@ -27,7 +27,6 @@ static void hmc5883l_convert(struct sensor_value *val, int16_t raw_val,
uint16_t divider) uint16_t divider)
{ {
/* val = raw_val / divider */ /* val = raw_val / divider */
val->type = SENSOR_VALUE_TYPE_INT_PLUS_MICRO;
val->val1 = raw_val / divider; val->val1 = raw_val / divider;
val->val2 = (((int64_t)raw_val % divider) * 1000000L) / divider; val->val2 = (((int64_t)raw_val % divider) * 1000000L) / divider;
} }

View file

@ -153,10 +153,6 @@ static int hp206c_attr_set(struct device *dev, enum sensor_channel chan,
enum sensor_attribute attr, enum sensor_attribute attr,
const struct sensor_value *val) const struct sensor_value *val)
{ {
if (val->type != SENSOR_VALUE_TYPE_INT_PLUS_MICRO) {
return -EINVAL;
}
#ifdef CONFIG_HP206C_OSR_RUNTIME #ifdef CONFIG_HP206C_OSR_RUNTIME
if (attr == SENSOR_ATTR_OVERSAMPLING) { if (attr == SENSOR_ATTR_OVERSAMPLING) {
return hp206c_osr_set(dev, val->val1); return hp206c_osr_set(dev, val->val1);
@ -238,8 +234,6 @@ static int hp206c_val_get(struct device *dev,
temp = hp206c_buf_convert(buf, false); temp = hp206c_buf_convert(buf, false);
} }
val->type = SENSOR_VALUE_TYPE_INT_PLUS_MICRO;
if (cmd == HP206C_CMD_READ_P) { if (cmd == HP206C_CMD_READ_P) {
val->val1 = temp / 1000; val->val1 = temp / 1000;
val->val2 = temp % 1000 * 1000; val->val2 = temp % 1000 * 1000;

View file

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

View file

@ -56,13 +56,11 @@ static int isl29035_channel_get(struct device *dev,
#if CONFIG_ISL29035_MODE_ALS #if CONFIG_ISL29035_MODE_ALS
/* val = sample_val * lux_range / (2 ^ adc_data_bits) */ /* val = sample_val * lux_range / (2 ^ adc_data_bits) */
tmp = (uint64_t)drv_data->data_sample * ISL29035_LUX_RANGE; 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; val->val1 = tmp >> ISL29035_ADC_DATA_BITS;
tmp = (tmp & ISL29035_ADC_DATA_MASK) * 1000000; tmp = (tmp & ISL29035_ADC_DATA_MASK) * 1000000;
val->val2 = tmp >> ISL29035_ADC_DATA_BITS; val->val2 = tmp >> ISL29035_ADC_DATA_BITS;
#elif CONFIG_ISL29035_MODE_IR #elif CONFIG_ISL29035_MODE_IR
ARG_UNUSED(tmp); ARG_UNUSED(tmp);
val->type = SENSOR_VALUE_TYPE_INT_INT_PLUS_MICRO;
val->val1 = drv_data->data_sample; val->val1 = drv_data->data_sample;
val->val2 = 0; val->val2 = 0;
#endif #endif

View file

@ -24,7 +24,6 @@
static void lis3dh_convert(struct sensor_value *val, int64_t raw_val) static void lis3dh_convert(struct sensor_value *val, int64_t raw_val)
{ {
/* val = raw_val * LIS3DH_ACCEL_SCALE / (10^6 * (2^16 - 1)) */ /* 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; raw_val = raw_val * LIS3DH_ACCEL_SCALE / 1000000;
val->val1 = raw_val / 0xFFFF; val->val1 = raw_val / 0xFFFF;
val->val2 = (raw_val % 0xFFFF) * 1000000 / 0xFFFF; val->val2 = (raw_val % 0xFFFF) * 1000000 / 0xFFFF;

View file

@ -27,7 +27,6 @@ static void lis3mdl_convert(struct sensor_value *val, int16_t raw_val,
uint16_t divider) uint16_t divider)
{ {
/* val = raw_val / divider */ /* val = raw_val / divider */
val->type = SENSOR_VALUE_TYPE_INT_PLUS_MICRO;
val->val1 = raw_val / divider; val->val1 = raw_val / divider;
val->val2 = (((int64_t)raw_val % divider) * 1000000L) / divider; val->val2 = (((int64_t)raw_val % divider) * 1000000L) / divider;
} }

View file

@ -79,7 +79,6 @@ static inline void lps25hb_press_convert(struct sensor_value *val,
int32_t raw_val) int32_t raw_val)
{ {
/* val = raw_val / 40960 */ /* val = raw_val / 40960 */
val->type = SENSOR_VALUE_TYPE_INT_PLUS_MICRO;
val->val1 = raw_val / 40960; val->val1 = raw_val / 40960;
val->val2 = ((int32_t)raw_val * 1000000 / 40960) % 1000000; val->val2 = ((int32_t)raw_val * 1000000 / 40960) % 1000000;
} }
@ -90,7 +89,6 @@ static inline void lps25hb_temp_convert(struct sensor_value *val,
int32_t uval; int32_t uval;
/* val = raw_val / 480 + 42.5 */ /* val = raw_val / 480 + 42.5 */
val->type = SENSOR_VALUE_TYPE_INT_PLUS_MICRO;
uval = (int32_t)raw_val * 1000000 / 480 + 42500000; uval = (int32_t)raw_val * 1000000 / 480 + 42500000;
val->val1 = (raw_val * 10 / 480 + 425) / 10; val->val1 = (raw_val * 10 / 480 + 425) / 10;
val->val2 = uval % 1000000; val->val2 = uval % 1000000;

View file

@ -251,7 +251,6 @@ static inline void lsm6ds0_accel_convert(struct sensor_value *val, int raw_val,
double dval; double dval;
dval = (double)(raw_val) * scale / 32767.0; dval = (double)(raw_val) * scale / 32767.0;
val->type = SENSOR_VALUE_TYPE_INT_PLUS_MICRO;
val->val1 = (int32_t)dval; val->val1 = (int32_t)dval;
val->val2 = ((int32_t)(dval * 1000000)) % 1000000; val->val2 = ((int32_t)(dval * 1000000)) % 1000000;
} }
@ -309,7 +308,6 @@ static inline void lsm6ds0_gyro_convert(struct sensor_value *val, int raw_val,
double dval; double dval;
dval = (double)(raw_val) * numerator / 1000.0 * SENSOR_DEG2RAD_DOUBLE; dval = (double)(raw_val) * numerator / 1000.0 * SENSOR_DEG2RAD_DOUBLE;
val->type = SENSOR_VALUE_TYPE_INT_PLUS_MICRO;
val->val1 = (int32_t)dval; val->val1 = (int32_t)dval;
val->val2 = ((int32_t)(dval * 1000000)) % 1000000; val->val2 = ((int32_t)(dval * 1000000)) % 1000000;
} }
@ -366,7 +364,6 @@ static void lsm6ds0_gyro_channel_get_temp(struct sensor_value *val,
struct lsm6ds0_data *data) struct lsm6ds0_data *data)
{ {
/* val = temp_sample / 16 + 25 */ /* val = temp_sample / 16 + 25 */
val->type = SENSOR_VALUE_TYPE_INT_PLUS_MICRO;
val->val1 = data->temp_sample / 16 + 25; val->val1 = data->temp_sample / 16 + 25;
val->val2 = (data->temp_sample % 16) * (1000000 / 16); val->val2 = (data->temp_sample % 16) * (1000000 / 16);
} }

View file

@ -165,7 +165,6 @@ static inline void lsm9ds0_gyro_convert(struct sensor_value *val, int raw_val,
double dval; double dval;
dval = (double)(raw_val) * numerator / 1000.0 * DEG2RAD; dval = (double)(raw_val) * numerator / 1000.0 * DEG2RAD;
val->type = SENSOR_VALUE_TYPE_INT_PLUS_MICRO;
val->val1 = (int32_t)dval; val->val1 = (int32_t)dval;
val->val2 = ((int32_t)(dval * 1000000)) % 1000000; val->val2 = ((int32_t)(dval * 1000000)) % 1000000;
} }
@ -231,10 +230,6 @@ static int lsm9ds0_gyro_attr_set(struct device *dev,
switch (attr) { switch (attr) {
#if defined(CONFIG_LSM9DS0_GYRO_FULLSCALE_RUNTIME) #if defined(CONFIG_LSM9DS0_GYRO_FULLSCALE_RUNTIME)
case SENSOR_ATTR_FULL_SCALE: case SENSOR_ATTR_FULL_SCALE:
if (val->type != SENSOR_VALUE_TYPE_INT_PLUS_MICRO) {
return -ENOTSUP;
}
if (lsm9ds0_gyro_set_fs(dev, sensor_rad_to_degrees(val)) < 0) { if (lsm9ds0_gyro_set_fs(dev, sensor_rad_to_degrees(val)) < 0) {
SYS_LOG_DBG("full-scale value not supported"); SYS_LOG_DBG("full-scale value not supported");
return -EIO; return -EIO;
@ -243,10 +238,6 @@ static int lsm9ds0_gyro_attr_set(struct device *dev,
#endif #endif
#if defined(CONFIG_LSM9DS0_GYRO_SAMPLING_RATE_RUNTIME) #if defined(CONFIG_LSM9DS0_GYRO_SAMPLING_RATE_RUNTIME)
case SENSOR_ATTR_SAMPLING_FREQUENCY: case SENSOR_ATTR_SAMPLING_FREQUENCY:
if (val->type != SENSOR_VALUE_TYPE_INT_PLUS_MICRO) {
return -ENOTSUP;
}
if (lsm9ds0_gyro_set_odr(dev, val->val1) < 0) { if (lsm9ds0_gyro_set_odr(dev, val->val1) < 0) {
SYS_LOG_DBG("sampling frequency value not supported"); SYS_LOG_DBG("sampling frequency value not supported");
return -EIO; return -EIO;

View file

@ -401,7 +401,6 @@ static inline void lsm9ds0_mfd_convert_accel(struct sensor_value *val,
double dval; double dval;
dval = (double)(raw_val) * scale; dval = (double)(raw_val) * scale;
val->type = SENSOR_VALUE_TYPE_INT_PLUS_MICRO;
val->val1 = (int32_t)dval; val->val1 = (int32_t)dval;
val->val2 = ((int32_t)(dval * 1000000)) % 1000000; val->val2 = ((int32_t)(dval * 1000000)) % 1000000;
} }
@ -488,7 +487,6 @@ static inline void lsm9ds0_mfd_convert_magn(struct sensor_value *val,
double dval; double dval;
dval = (double)(raw_val) * scale; dval = (double)(raw_val) * scale;
val->type = SENSOR_VALUE_TYPE_INT_PLUS_MICRO;
val->val1 = (int32_t)dval; val->val1 = (int32_t)dval;
val->val2 = ((int32_t)(dval * 1000000)) % 1000000; val->val2 = ((int32_t)(dval * 1000000)) % 1000000;
} }
@ -582,7 +580,6 @@ static int lsm9ds0_mfd_channel_get(struct device *dev,
#endif #endif
#if !defined(LSM9DS0_MFD_TEMP_DISABLED) #if !defined(LSM9DS0_MFD_TEMP_DISABLED)
case SENSOR_CHAN_TEMP: case SENSOR_CHAN_TEMP:
val->type = SENSOR_VALUE_TYPE_INT_PLUS_MICRO;
val->val1 = data->sample_temp; val->val1 = data->sample_temp;
val->val2 = 0; val->val2 = 0;
return 0; return 0;
@ -600,18 +597,10 @@ static inline int lsm9ds0_mfd_attr_set_accel(struct device *dev,
switch (attr) { switch (attr) {
#if defined(CONFIG_LSM9DS0_MFD_ACCEL_SAMPLING_RATE_RUNTIME) #if defined(CONFIG_LSM9DS0_MFD_ACCEL_SAMPLING_RATE_RUNTIME)
case SENSOR_ATTR_SAMPLING_FREQUENCY: case SENSOR_ATTR_SAMPLING_FREQUENCY:
if (val->type != SENSOR_VALUE_TYPE_INT_PLUS_MICRO) {
return -EINVAL;
}
return lsm9ds0_mfd_accel_set_odr(dev, val); return lsm9ds0_mfd_accel_set_odr(dev, val);
#endif #endif
#if defined(CONFIG_LSM9DS0_MFD_ACCEL_FULL_SCALE_RUNTIME) #if defined(CONFIG_LSM9DS0_MFD_ACCEL_FULL_SCALE_RUNTIME)
case SENSOR_ATTR_FULL_SCALE: case SENSOR_ATTR_FULL_SCALE:
if (val->type != SENSOR_VALUE_TYPE_INT_PLUS_MICRO) {
return -EINVAL;
}
return lsm9ds0_mfd_accel_set_fs(dev, sensor_ms2_to_g(val)); return lsm9ds0_mfd_accel_set_fs(dev, sensor_ms2_to_g(val));
#endif #endif
default: default:
@ -630,18 +619,10 @@ static inline int lsm9ds0_mfd_attr_set_magn(struct device *dev,
switch (attr) { switch (attr) {
#if defined(CONFIG_LSM9DS0_MFD_MAGN_SAMPLING_RATE_RUNTIME) #if defined(CONFIG_LSM9DS0_MFD_MAGN_SAMPLING_RATE_RUNTIME)
case SENSOR_ATTR_SAMPLING_FREQUENCY: case SENSOR_ATTR_SAMPLING_FREQUENCY:
if (val->type != SENSOR_VALUE_TYPE_INT_PLUS_MICRO) {
return -EINVAL;
}
return lsm9ds0_mfd_magn_set_odr(dev, val); return lsm9ds0_mfd_magn_set_odr(dev, val);
#endif #endif
#if defined(CONFIG_LSM9DS0_MFD_MAGN_FULL_SCALE_RUNTIME) #if defined(CONFIG_LSM9DS0_MFD_MAGN_FULL_SCALE_RUNTIME)
case SENSOR_ATTR_FULL_SCALE: case SENSOR_ATTR_FULL_SCALE:
if (val->type != SENSOR_VALUE_TYPE_INT_PLUS_MICRO) {
return -EINVAL;
}
return lsm9ds0_mfd_magn_set_fs(dev, val); return lsm9ds0_mfd_magn_set_fs(dev, val);
#endif #endif
default: default:

View file

@ -87,10 +87,6 @@ static int max44009_attr_set(struct device *dev, enum sensor_channel chan,
switch (attr) { switch (attr) {
case SENSOR_ATTR_SAMPLING_FREQUENCY: case SENSOR_ATTR_SAMPLING_FREQUENCY:
if (val->type != SENSOR_VALUE_TYPE_INT_PLUS_MICRO) {
return -ENOTSUP;
}
/* convert rate to mHz */ /* convert rate to mHz */
cr = val->val1 * 1000 + val->val2 / 1000; cr = val->val1 * 1000 + val->val2 / 1000;
@ -165,7 +161,6 @@ static int max44009_channel_get(struct device *dev, enum sensor_channel chan,
uval = uval << (drv_data->sample >> MAX44009_SAMPLE_EXPONENT_SHIFT); uval = uval << (drv_data->sample >> MAX44009_SAMPLE_EXPONENT_SHIFT);
/* lux is the integer of sample output multiplied by 0.045. */ /* lux is the integer of sample output multiplied by 0.045. */
val->type = SENSOR_VALUE_TYPE_INT_PLUS_MICRO;
val->val1 = (uval * 45) / 1000; val->val1 = (uval * 45) / 1000;
val->val2 = ((uval * 45) % 1000) * 1000; val->val2 = ((uval * 45) % 1000) * 1000;

View file

@ -69,8 +69,6 @@ static int mcp9808_channel_get(struct device *dev,
__ASSERT_NO_MSG(chan == SENSOR_CHAN_TEMP); __ASSERT_NO_MSG(chan == SENSOR_CHAN_TEMP);
val->type = SENSOR_VALUE_TYPE_INT_PLUS_MICRO;
val->val1 = (data->reg_val & MCP9808_TEMP_INT_MASK) >> val->val1 = (data->reg_val & MCP9808_TEMP_INT_MASK) >>
MCP9808_TEMP_INT_SHIFT; MCP9808_TEMP_INT_SHIFT;
val->val2 = (data->reg_val & MCP9808_TEMP_FRAC_MASK) * 62500; val->val2 = (data->reg_val & MCP9808_TEMP_FRAC_MASK) * 62500;

View file

@ -74,16 +74,12 @@ int mcp9808_attr_set(struct device *dev, enum sensor_channel chan,
__ASSERT_NO_MSG(chan == SENSOR_CHAN_TEMP); __ASSERT_NO_MSG(chan == SENSOR_CHAN_TEMP);
if (val->type == SENSOR_VALUE_TYPE_INT_PLUS_MICRO) { val2 = val->val2;
val2 = val->val2; while (val2 > 0) {
while (val2 > 0) { reg_val += (1 << 2);
reg_val += (1 << 2); val2 -= 250000;
val2 -= 250000;
}
reg_val |= val->val1 << 4;
} else {
return -EINVAL;
} }
reg_val |= val->val1 << 4;
switch (attr) { switch (attr) {
case SENSOR_ATTR_LOWER_THRESH: case SENSOR_ATTR_LOWER_THRESH:

View file

@ -28,7 +28,6 @@ static void mpu6050_convert_accel(struct sensor_value *val, int16_t raw_val,
int64_t conv_val; int64_t conv_val;
conv_val = ((int64_t)raw_val * SENSOR_G) >> sensitivity_shift; conv_val = ((int64_t)raw_val * SENSOR_G) >> sensitivity_shift;
val->type = SENSOR_VALUE_TYPE_INT_PLUS_MICRO;
val->val1 = conv_val / 1000000; val->val1 = conv_val / 1000000;
val->val2 = conv_val % 1000000; val->val2 = conv_val % 1000000;
} }
@ -41,7 +40,6 @@ static void mpu6050_convert_gyro(struct sensor_value *val, int16_t raw_val,
conv_val = ((int64_t)raw_val * SENSOR_PI * 10) / conv_val = ((int64_t)raw_val * SENSOR_PI * 10) /
(180 * sensitivity_x10); (180 * sensitivity_x10);
val->type = SENSOR_VALUE_TYPE_INT_PLUS_MICRO;
val->val1 = conv_val / 1000000; val->val1 = conv_val / 1000000;
val->val2 = conv_val % 1000000; val->val2 = conv_val % 1000000;
} }
@ -50,7 +48,6 @@ static void mpu6050_convert_gyro(struct sensor_value *val, int16_t raw_val,
static inline void mpu6050_convert_temp(struct sensor_value *val, static inline void mpu6050_convert_temp(struct sensor_value *val,
int16_t raw_val) int16_t raw_val)
{ {
val->type = SENSOR_VALUE_TYPE_INT_PLUS_MICRO;
val->val1 = raw_val / 340 + 36; val->val1 = raw_val / 340 + 36;
val->val2 = ((int64_t)(raw_val % 340) * 1000000) / 340 + 530000; val->val2 = ((int64_t)(raw_val % 340) * 1000000) / 340 + 530000;

View file

@ -84,7 +84,6 @@ static int temp_nrf5_channel_get(struct device *dev,
} }
uval = data->sample * TEMP_NRF5_TEMP_SCALE; uval = data->sample * TEMP_NRF5_TEMP_SCALE;
val->type = SENSOR_VALUE_TYPE_INT_PLUS_MICRO;
val->val1 = uval / 1000000; val->val1 = uval / 1000000;
val->val2 = uval % 1000000; val->val2 = uval % 1000000;

View file

@ -133,13 +133,11 @@ static int sht3xd_channel_get(struct device *dev,
if (chan == SENSOR_CHAN_TEMP) { if (chan == SENSOR_CHAN_TEMP) {
/* val = -45 + 175 * sample / (2^16 -1) */ /* val = -45 + 175 * sample / (2^16 -1) */
tmp = 175 * (uint64_t)drv_data->t_sample; tmp = 175 * (uint64_t)drv_data->t_sample;
val->type = SENSOR_VALUE_TYPE_INT_PLUS_MICRO;
val->val1 = (int32_t)(tmp / 0xFFFF) - 45; val->val1 = (int32_t)(tmp / 0xFFFF) - 45;
val->val2 = (1000000 * (tmp % 0xFFFF)) / 0xFFFF; val->val2 = (1000000 * (tmp % 0xFFFF)) / 0xFFFF;
} else if (chan == SENSOR_CHAN_HUMIDITY) { } else if (chan == SENSOR_CHAN_HUMIDITY) {
/* val = 100000 * sample / (2^16 -1) */ /* val = 100000 * sample / (2^16 -1) */
tmp = 100000 * (uint64_t)drv_data->rh_sample; tmp = 100000 * (uint64_t)drv_data->rh_sample;
val->type = SENSOR_VALUE_TYPE_INT_PLUS_MICRO;
val->val1 = tmp / 0xFFFF; val->val1 = tmp / 0xFFFF;
val->val2 = (1000000 * (tmp % 0xFFFF)) / 0xFFFF; val->val2 = (1000000 * (tmp % 0xFFFF)) / 0xFFFF;
} else { } else {

View file

@ -47,10 +47,6 @@ int sht3xd_attr_set(struct device *dev,
struct sht3xd_data *drv_data = dev->driver_data; struct sht3xd_data *drv_data = dev->driver_data;
uint16_t set_cmd, clear_cmd, reg_val, temp, rh; uint16_t set_cmd, clear_cmd, reg_val, temp, rh;
if (val->type != SENSOR_VALUE_TYPE_INT_PLUS_MICRO) {
return -ENOTSUP;
}
if (attr == SENSOR_ATTR_LOWER_THRESH) { if (attr == SENSOR_ATTR_LOWER_THRESH) {
if (chan == SENSOR_CHAN_TEMP) { if (chan == SENSOR_CHAN_TEMP) {
drv_data->t_low = sht3xd_temp_processed_to_raw(val); drv_data->t_low = sht3xd_temp_processed_to_raw(val);

View file

@ -68,7 +68,6 @@ static int sx9500_channel_get(struct device *dev,
__ASSERT_NO_MSG(chan == SENSOR_CHAN_PROX); __ASSERT_NO_MSG(chan == SENSOR_CHAN_PROX);
val->type = SENSOR_VALUE_TYPE_INT_PLUS_MICRO;
val->val1 = !!(data->prox_stat & val->val1 = !!(data->prox_stat &
(1 << (4 + CONFIG_SX9500_PROX_CHANNEL))); (1 << (4 + CONFIG_SX9500_PROX_CHANNEL)));
val->val2 = 0; val->val2 = 0;

View file

@ -115,12 +115,10 @@ static int th02_channel_get(struct device *dev, enum sensor_channel chan,
if (chan == SENSOR_CHAN_TEMP) { if (chan == SENSOR_CHAN_TEMP) {
/* val = sample / 32 - 50 */ /* val = sample / 32 - 50 */
val->type = SENSOR_VALUE_TYPE_INT_PLUS_MICRO;
val->val1 = drv_data->t_sample / 32 - 50; val->val1 = drv_data->t_sample / 32 - 50;
val->val2 = (drv_data->t_sample % 32) * (1000000 / 32); val->val2 = (drv_data->t_sample % 32) * (1000000 / 32);
} else { } else {
/* val = sample / 16 -24 */ /* val = sample / 16 -24 */
val->type = SENSOR_VALUE_TYPE_INT_PLUS_MICRO;
val->val1 = drv_data->rh_sample / 16 - 24; val->val1 = drv_data->rh_sample / 16 - 24;
val->val2 = (drv_data->rh_sample % 16) * (1000000 / 16); val->val2 = (drv_data->rh_sample % 16) * (1000000 / 16);
} }

View file

@ -105,7 +105,6 @@ static int tmp007_channel_get(struct device *dev,
} }
uval = (int32_t)drv_data->sample * TMP007_TEMP_SCALE; uval = (int32_t)drv_data->sample * TMP007_TEMP_SCALE;
val->type = SENSOR_VALUE_TYPE_INT_PLUS_MICRO;
val->val1 = uval / 1000000; val->val1 = uval / 1000000;
val->val2 = uval % 1000000; val->val2 = uval % 1000000;

View file

@ -109,11 +109,6 @@ static int tmp112_attr_set(struct device *dev,
switch (attr) { switch (attr) {
case SENSOR_ATTR_FULL_SCALE: case SENSOR_ATTR_FULL_SCALE:
if (val->type != SENSOR_VALUE_TYPE_INT_PLUS_MICRO) {
return -ENOTSUP;
}
/* the sensor supports two ranges -55 to 128 and -55 to 150 */ /* the sensor supports two ranges -55 to 128 and -55 to 150 */
/* the value contains the upper limit */ /* the value contains the upper limit */
if (val->val1 == 128) { if (val->val1 == 128) {
@ -133,11 +128,6 @@ static int tmp112_attr_set(struct device *dev,
return 0; return 0;
case SENSOR_ATTR_SAMPLING_FREQUENCY: case SENSOR_ATTR_SAMPLING_FREQUENCY:
if (val->type != SENSOR_VALUE_TYPE_INT_PLUS_MICRO) {
return -ENOTSUP;
}
/* conversion rate in mHz */ /* conversion rate in mHz */
cr = val->val1 * 1000 + val->val2 / 1000; cr = val->val1 * 1000 + val->val2 / 1000;
@ -212,7 +202,6 @@ static int tmp112_channel_get(struct device *dev,
} }
uval = (int32_t)drv_data->sample * TMP112_TEMP_SCALE; uval = (int32_t)drv_data->sample * TMP112_TEMP_SCALE;
val->type = SENSOR_VALUE_TYPE_INT_PLUS_MICRO;
val->val1 = uval / 1000000; val->val1 = uval / 1000000;
val->val2 = uval % 1000000; val->val2 = uval % 1000000;

View file

@ -37,32 +37,17 @@ extern "C" {
#include <device.h> #include <device.h>
#include <errno.h> #include <errno.h>
/** @brief Sensor value types. */
enum sensor_value_type {
/**
* val1 contains an integer value, val2 is the fractional value.
* To obtain the final value, use the formula: val1 + val2 *
* 10^(-6).
*/
SENSOR_VALUE_TYPE_INT_PLUS_MICRO,
/** @brief dval contains a floating point value. */
SENSOR_VALUE_TYPE_DOUBLE,
};
/** /**
* @brief Representation of a sensor readout value. * @brief Representation of a sensor readout value.
* *
* The meaning of the fields is dictated by the type field. * The value is represented as having an integer and a fractional part,
* and can be obtained using the formula val1 + val2 * 10^(-6).
*/ */
struct sensor_value { struct sensor_value {
enum sensor_value_type type; /** Integer part of the value. */
union { int32_t val1;
struct { /** Fractional part of the value. */
int32_t val1; int32_t val2;
int32_t val2;
};
double dval;
};
}; };
/** /**
@ -422,7 +407,6 @@ static inline int32_t sensor_ms2_to_g(const struct sensor_value *ms2)
*/ */
static inline void sensor_g_to_ms2(int32_t g, struct sensor_value *ms2) static inline void sensor_g_to_ms2(int32_t g, struct sensor_value *ms2)
{ {
ms2->type = SENSOR_VALUE_TYPE_INT_PLUS_MICRO;
ms2->val1 = ((int64_t)g * SENSOR_G) / 1000000LL; ms2->val1 = ((int64_t)g * SENSOR_G) / 1000000LL;
ms2->val2 = ((int64_t)g * SENSOR_G) % 1000000LL; ms2->val2 = ((int64_t)g * SENSOR_G) % 1000000LL;
} }
@ -453,7 +437,6 @@ static inline int32_t sensor_rad_to_degrees(const struct sensor_value *rad)
*/ */
static inline void sensor_degrees_to_rad(int32_t d, struct sensor_value *rad) static inline void sensor_degrees_to_rad(int32_t d, struct sensor_value *rad)
{ {
rad->type = SENSOR_VALUE_TYPE_INT_PLUS_MICRO;
rad->val1 = ((int64_t)d * SENSOR_PI / 180LL) / 1000000LL; rad->val1 = ((int64_t)d * SENSOR_PI / 180LL) / 1000000LL;
rad->val2 = ((int64_t)d * SENSOR_PI / 180LL) % 1000000LL; rad->val2 = ((int64_t)d * SENSOR_PI / 180LL) % 1000000LL;
} }
@ -466,14 +449,7 @@ static inline void sensor_degrees_to_rad(int32_t d, struct sensor_value *rad)
*/ */
static inline double sensor_value_to_double(struct sensor_value *val) static inline double sensor_value_to_double(struct sensor_value *val)
{ {
switch (val->type) { return (double)val->val1 + (double)val->val2 / 1000000;
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;
} }
/** /**

View file

@ -114,7 +114,6 @@ static void test_trigger_mode(struct device *bmg160)
} }
/* set slope duration to 4 samples */ /* set slope duration to 4 samples */
attr.type = SENSOR_VALUE_TYPE_INT_PLUS_MICRO;
attr.val1 = 4; attr.val1 = 4;
attr.val2 = 0; attr.val2 = 0;
@ -144,7 +143,6 @@ static void test_trigger_mode(struct device *bmg160)
printf("Gyro: Testing data ready trigger.\n"); printf("Gyro: Testing data ready trigger.\n");
attr.type = SENSOR_VALUE_TYPE_INT_PLUS_MICRO;
attr.val1 = 100; attr.val1 = 100;
attr.val2 = 0; attr.val2 = 0;

View file

@ -45,9 +45,9 @@
* X = +2.349435, Y = +0.488070. Z = -1.351970 * X = +2.349435, Y = +0.488070. Z = -1.351970
*/ */
static struct sensor_value accel_offsets[] = { static struct sensor_value accel_offsets[] = {
{SENSOR_VALUE_TYPE_INT_PLUS_MICRO, { {2, 349435} } }, /* X */ {2, 349435}, /* X */
{SENSOR_VALUE_TYPE_INT_PLUS_MICRO, { {0, 488070} } }, /* Y */ {0, 488070}, /* Y */
{SENSOR_VALUE_TYPE_INT_PLUS_MICRO, { {-1, -351970} } }, /* Z */ {-1, -351970}, /* Z */
}; };
/* /*
@ -55,9 +55,9 @@ static struct sensor_value accel_offsets[] = {
* converge to 0 (with the device standing still). * converge to 0 (with the device standing still).
*/ */
static struct sensor_value gyro_offsets[] = { static struct sensor_value gyro_offsets[] = {
{SENSOR_VALUE_TYPE_INT_PLUS_MICRO, { {0, 3195} } }, /* X */ {0, 3195}, /* X */
{SENSOR_VALUE_TYPE_INT_PLUS_MICRO, { {0, 3195} } }, /* Y */ {0, 3195}, /* Y */
{SENSOR_VALUE_TYPE_INT_PLUS_MICRO, { {0, -4260} } },/* Z */ {0, -4260},/* Z */
}; };
static int manual_calibration(struct device *bmi160) static int manual_calibration(struct device *bmi160)
@ -89,9 +89,9 @@ static int manual_calibration(struct device *bmi160)
* device has to stay still for about 500ms = 250ms(accel) + 250ms(gyro). * device has to stay still for about 500ms = 250ms(accel) + 250ms(gyro).
*/ */
struct sensor_value acc_calib[] = { struct sensor_value acc_calib[] = {
{SENSOR_VALUE_TYPE_INT_PLUS_MICRO, { {0, 0} } }, /* X */ {0, 0}, /* X */
{SENSOR_VALUE_TYPE_INT_PLUS_MICRO, { {0, 0} } }, /* Y */ {0, 0}, /* Y */
{SENSOR_VALUE_TYPE_INT_PLUS_MICRO, { {9, 806650} } }, /* Z */ {9, 806650}, /* Z */
}; };
static int auto_calibration(struct device *bmi160) static int auto_calibration(struct device *bmi160)
{ {
@ -134,33 +134,26 @@ static inline int sensor_value_snprintf(char *buf, size_t len,
{ {
int32_t val1, val2; int32_t val1, val2;
switch (val->type) { if (val->val2 == 0) {
case SENSOR_VALUE_TYPE_INT_PLUS_MICRO: return snprintf(buf, len, "%d", val->val1);
if (val->val2 == 0) { }
return snprintf(buf, len, "%d", val->val1);
}
/* normalize value */ /* normalize value */
if (val->val1 < 0 && val->val2 > 0) { if (val->val1 < 0 && val->val2 > 0) {
val1 = val->val1 + 1; val1 = val->val1 + 1;
val2 = val->val2 - 1000000; val2 = val->val2 - 1000000;
} else { } else {
val1 = val->val1; val1 = val->val1;
val2 = val->val2; val2 = val->val2;
} }
/* print value to buffer */ /* print value to buffer */
if (val1 > 0 || (val1 == 0 && val2 > 0)) { if (val1 > 0 || (val1 == 0 && val2 > 0)) {
return snprintf(buf, len, "%d.%06d", val1, val2); return snprintf(buf, len, "%d.%06d", val1, val2);
} else if (val1 == 0 && val2 < 0) { } else if (val1 == 0 && val2 < 0) {
return snprintf(buf, len, "-0.%06d", -val2); return snprintf(buf, len, "-0.%06d", -val2);
} else { } else {
return snprintf(buf, len, "%d.%06d", val1, -val2); return snprintf(buf, len, "%d.%06d", val1, -val2);
}
case SENSOR_VALUE_TYPE_DOUBLE:
return snprintf(buf, len, "%f", val->dval);
default:
return 0;
} }
} }
@ -225,7 +218,6 @@ static void test_polling_mode(struct device *bmi160)
#if defined(CONFIG_BMI160_ACCEL_ODR_RUNTIME) #if defined(CONFIG_BMI160_ACCEL_ODR_RUNTIME)
/* set sampling frequency to 800Hz for accel */ /* set sampling frequency to 800Hz for accel */
attr.type = SENSOR_VALUE_TYPE_INT_PLUS_MICRO;
attr.val1 = 800; attr.val1 = 800;
attr.val2 = 0; attr.val2 = 0;
@ -238,7 +230,6 @@ static void test_polling_mode(struct device *bmi160)
#if defined(CONFIG_BMI160_GYRO_ODR_RUNTIME) #if defined(CONFIG_BMI160_GYRO_ODR_RUNTIME)
/* set sampling frequency to 3200Hz for gyro */ /* set sampling frequency to 3200Hz for gyro */
attr.type = SENSOR_VALUE_TYPE_INT_PLUS_MICRO;
attr.val1 = 3200; attr.val1 = 3200;
attr.val2 = 0; attr.val2 = 0;
@ -312,7 +303,6 @@ static void test_anymotion_trigger(struct device *bmi160)
* bigger than half the range. For example, for a 16G range, the * bigger than half the range. For example, for a 16G range, the
* threshold must not exceed 8G. * threshold must not exceed 8G.
*/ */
attr.type = SENSOR_VALUE_TYPE_INT_PLUS_MICRO;
attr.val1 = 0; attr.val1 = 0;
attr.val2 = 980665; attr.val2 = 980665;
if (sensor_attr_set(bmi160, SENSOR_CHAN_ACCEL_ANY, if (sensor_attr_set(bmi160, SENSOR_CHAN_ACCEL_ANY,
@ -327,7 +317,6 @@ static void test_anymotion_trigger(struct device *bmi160)
* *
* Allowed values are from 1 to 4. * Allowed values are from 1 to 4.
*/ */
attr.type = SENSOR_VALUE_TYPE_INT_PLUS_MICRO;
attr.val1 = 2; attr.val1 = 2;
attr.val2 = 0; attr.val2 = 0;
if (sensor_attr_set(bmi160, SENSOR_CHAN_ACCEL_ANY, if (sensor_attr_set(bmi160, SENSOR_CHAN_ACCEL_ANY,
@ -397,7 +386,6 @@ static void test_trigger_mode(struct device *bmi160)
#if defined(CONFIG_BMI160_ACCEL_ODR_RUNTIME) #if defined(CONFIG_BMI160_ACCEL_ODR_RUNTIME)
/* set sampling frequency to 100Hz for accel */ /* set sampling frequency to 100Hz for accel */
attr.type = SENSOR_VALUE_TYPE_INT_PLUS_MICRO;
attr.val1 = 100; attr.val1 = 100;
attr.val2 = 0; attr.val2 = 0;
@ -410,7 +398,6 @@ static void test_trigger_mode(struct device *bmi160)
#if defined(CONFIG_BMI160_GYRO_ODR_RUNTIME) #if defined(CONFIG_BMI160_GYRO_ODR_RUNTIME)
/* set sampling frequency to 100Hz for gyro */ /* set sampling frequency to 100Hz for gyro */
attr.type = SENSOR_VALUE_TYPE_INT_PLUS_MICRO;
attr.val1 = 100; attr.val1 = 100;
attr.val2 = 0; attr.val2 = 0;

View file

@ -49,7 +49,6 @@ void main(void)
struct sensor_value val; struct sensor_value val;
struct sensor_trigger trig; struct sensor_trigger trig;
val.type = SENSOR_VALUE_TYPE_INT_PLUS_MICRO;
val.val1 = 26; val.val1 = 26;
val.val2 = 0; val.val2 = 0;

View file

@ -20,13 +20,7 @@
static double convert_to_double(struct sensor_value *v) static double convert_to_double(struct sensor_value *v)
{ {
switch (v->type) { return v->val1 + ((double)v->val2 / 1000000);
case SENSOR_VALUE_TYPE_INT_PLUS_MICRO:
return v->val1 + ((double)v->val2 / 1000000);
case SENSOR_VALUE_TYPE_DOUBLE:
return v->dval;
}
return 0;
} }
void main(void) void main(void)

View file

@ -26,7 +26,6 @@ static void do_main(struct device *dev)
struct sensor_value temp_value; struct sensor_value temp_value;
struct sensor_value attr; struct sensor_value attr;
attr.type = SENSOR_VALUE_TYPE_INT_PLUS_MICRO;
attr.val1 = 150; attr.val1 = 150;
attr.val2 = 0; attr.val2 = 0;
ret = sensor_attr_set(dev, SENSOR_CHAN_TEMP, ret = sensor_attr_set(dev, SENSOR_CHAN_TEMP,
@ -36,7 +35,6 @@ static void do_main(struct device *dev)
return; return;
} }
attr.type = SENSOR_VALUE_TYPE_INT_PLUS_MICRO;
attr.val1 = 8; attr.val1 = 8;
attr.val2 = 0; attr.val2 = 0;
ret = sensor_attr_set(dev, SENSOR_CHAN_TEMP, ret = sensor_attr_set(dev, SENSOR_CHAN_TEMP,