Re: [PATCH] iio: common: hid-sensors: change the precision of scale values

From: Srinivas Pandruvada
Date: Thu Nov 03 2016 - 09:38:42 EST


On Thu, 2016-11-03 at 17:41 +0800, Ooi, Joyce wrote:
> Extension of the scale precision is needed as there are some scale
> values
> that exceed 6 decimal points precision. For example, the scale for
> gyroscope from degrees/second to radians/second is 0.017453. If the
> unit
> exponent for gyroscope is -5, it will be
> 0.017453 -> exp: -5 -> 0.00000017453
>
> However, because the decimal precision is up to 6, the mentioned
> scale
> value will be truncated to 0.000000. To avoid this, changes are made
> to
> extend the precision to 9 decimal points precision.
This change is already submitted by
Song Hongyan <hongyan.song@xxxxxxxxx>

Thanks,
Srinivas
>
> Signed-off-by: Ooi, Joyce <joyce.ooi@xxxxxxxxx>
> ---
> Â.../iio/common/hid-sensors/hid-sensor-attributes.c | 65 +++++++++++-
> ----------
> Â1 file changed, 33 insertions(+), 32 deletions(-)
>
> diff --git a/drivers/iio/common/hid-sensors/hid-sensor-attributes.c
> b/drivers/iio/common/hid-sensors/hid-sensor-attributes.c
> index dc33c1d..c1f8c22 100644
> --- a/drivers/iio/common/hid-sensors/hid-sensor-attributes.c
> +++ b/drivers/iio/common/hid-sensors/hid-sensor-attributes.c
> @@ -30,34 +30,34 @@
> Â u32 usage_id;
> Â int unit; /* 0 for default others from HID sensor spec */
> Â int scale_val0; /* scale, whole number */
> - int scale_val1; /* scale, fraction in micros */
> + int scale_val1; /* scale, fraction in nanos */
> Â} unit_conversion[] = {
> - {HID_USAGE_SENSOR_ACCEL_3D, 0, 9, 806650},
> + {HID_USAGE_SENSOR_ACCEL_3D, 0, 9, 806650000},
> Â {HID_USAGE_SENSOR_ACCEL_3D,
> Â HID_USAGE_SENSOR_UNITS_METERS_PER_SEC_SQRD, 1, 0},
> Â {HID_USAGE_SENSOR_ACCEL_3D,
> - HID_USAGE_SENSOR_UNITS_G, 9, 806650},
> + HID_USAGE_SENSOR_UNITS_G, 9, 806650000},
> Â
> - {HID_USAGE_SENSOR_GYRO_3D, 0, 0, 17453},
> + {HID_USAGE_SENSOR_GYRO_3D, 0, 0, 17453293},
> Â {HID_USAGE_SENSOR_GYRO_3D,
> Â HID_USAGE_SENSOR_UNITS_RADIANS_PER_SECOND, 1, 0},
> Â {HID_USAGE_SENSOR_GYRO_3D,
> - HID_USAGE_SENSOR_UNITS_DEGREES_PER_SECOND, 0,
> 17453},
> + HID_USAGE_SENSOR_UNITS_DEGREES_PER_SECOND, 0,
> 17453293},
> Â
> - {HID_USAGE_SENSOR_COMPASS_3D, 0, 0, 1000},
> + {HID_USAGE_SENSOR_COMPASS_3D, 0, 0, 1000000},
> Â {HID_USAGE_SENSOR_COMPASS_3D, HID_USAGE_SENSOR_UNITS_GAUSS,
> 1, 0},
> Â
> - {HID_USAGE_SENSOR_INCLINOMETER_3D, 0, 0, 17453},
> + {HID_USAGE_SENSOR_INCLINOMETER_3D, 0, 0, 17453293},
> Â {HID_USAGE_SENSOR_INCLINOMETER_3D,
> - HID_USAGE_SENSOR_UNITS_DEGREES, 0, 17453},
> + HID_USAGE_SENSOR_UNITS_DEGREES, 0, 17453293},
> Â {HID_USAGE_SENSOR_INCLINOMETER_3D,
> Â HID_USAGE_SENSOR_UNITS_RADIANS, 1, 0},
> Â
> Â {HID_USAGE_SENSOR_ALS, 0, 1, 0},
> Â {HID_USAGE_SENSOR_ALS, HID_USAGE_SENSOR_UNITS_LUX, 1, 0},
> Â
> - {HID_USAGE_SENSOR_PRESSURE, 0, 100, 0},
> - {HID_USAGE_SENSOR_PRESSURE, HID_USAGE_SENSOR_UNITS_PASCAL,
> 0, 1000},
> + {HID_USAGE_SENSOR_PRESSURE, 0, 100000, 0},
> + {HID_USAGE_SENSOR_PRESSURE, HID_USAGE_SENSOR_UNITS_PASCAL,
> 0, 1000000},
> Â};
> Â
> Âstatic int pow_10(unsigned power)
> @@ -266,16 +266,16 @@ int hid_sensor_write_raw_hyst_value(struct
> hid_sensor_common *st,
> Â/*
> Â * This fuction applies the unit exponent to the scale.
> Â * For example:
> - * 9.806650 ->exp:2-> val0[980]val1[665000]
> - * 9.000806 ->exp:2-> val0[900]val1[80600]
> - * 0.174535 ->exp:2-> val0[17]val1[453500]
> - * 1.001745 ->exp:0-> val0[1]val1[1745]
> - * 1.001745 ->exp:2-> val0[100]val1[174500]
> - * 1.001745 ->exp:4-> val0[10017]val1[450000]
> - * 9.806650 ->exp:-2-> val0[0]val1[98066]
> + * 9.806650000 ->exp:2-> val0[980]val1[665000000]
> + * 9.000806000 ->exp:2-> val0[900]val1[80600000]
> + * 0.17453293 ->exp:2-> val0[17]val1[453293000]
> + * 1.00174532 ->exp:0-> val0[1]val1[1745320]
> + * 1.00174532 ->exp:2-> val0[100]val1[174532000]
> + * 1.00174532 ->exp:4-> val0[10017]val1[453200000]
> + * 9.80665000 ->exp:-2-> val0[0]val1[98066500]
> Â */
> -static void adjust_exponent_micro(int *val0, int *val1, int scale0,
> - ÂÂint scale1, int exp)
> +static void adjust_exponent_nano(int *val0, int *val1, int scale0,
> + Âint scale1, int exp)
> Â{
> Â int i;
> Â int x;
> @@ -285,32 +285,32 @@ static void adjust_exponent_micro(int *val0,
> int *val1, int scale0,
> Â if (exp > 0) {
> Â *val0 = scale0 * pow_10(exp);
> Â res = 0;
> - if (exp > 6) {
> + if (exp > 9) {
> Â *val1 = 0;
> Â return;
> Â }
> Â for (i = 0; i < exp; ++i) {
> - x = scale1 / pow_10(5 - i);
> + x = scale1 / pow_10(8 - i);
> Â res += (pow_10(exp - 1 - i) * x);
> - scale1 = scale1 % pow_10(5 - i);
> + scale1 = scale1 % pow_10(8 - i);
> Â }
> Â *val0 += res;
> Â *val1 = scale1 * pow_10(exp);
> Â } else if (exp < 0) {
> Â exp = abs(exp);
> - if (exp > 6) {
> + if (exp > 9) {
> Â *val0 = *val1 = 0;
> Â return;
> Â }
> Â *val0 = scale0 / pow_10(exp);
> Â rem = scale0 % pow_10(exp);
> Â res = 0;
> - for (i = 0; i < (6 - exp); ++i) {
> - x = scale1 / pow_10(5 - i);
> - res += (pow_10(5 - exp - i) * x);
> - scale1 = scale1 % pow_10(5 - i);
> + for (i = 0; i < (9 - exp); ++i) {
> + x = scale1 / pow_10(8 - i);
> + res += (pow_10(8 - exp - i) * x);
> + scale1 = scale1 % pow_10(8 - i);
> Â }
> - *val1 = rem * pow_10(6 - exp) + res;
> + *val1 = rem * pow_10(9 - exp) + res;
> Â } else {
> Â *val0 = scale0;
> Â *val1 = scale1;
> @@ -332,14 +332,15 @@ int hid_sensor_format_scale(u32 usage_id,
> Â unit_conversion[i].unit == attr_info->units)
> {
> Â expÂÂ= hid_sensor_convert_exponent(
> Â attr_info-
> >unit_expo);
> - adjust_exponent_micro(val0, val1,
> - unit_conversion[i].scale_val
> 0,
> - unit_conversion[i].scale_val
> 1, exp);
> + adjust_exponent_nano(val0, val1,
> + ÂÂÂÂÂunit_conversion[i].scal
> e_val0,
> + ÂÂÂÂÂunit_conversion[i].scal
> e_val1,
> + ÂÂÂÂÂexp);
> Â break;
> Â }
> Â }
> Â
> - return IIO_VAL_INT_PLUS_MICRO;
> + return IIO_VAL_INT_PLUS_NANO;
> Â}
> ÂEXPORT_SYMBOL(hid_sensor_format_scale);
> Â