From: Jonathan Cameron <jic23@kernel.org>
To: Srinivas Pandruvada <srinivas.pandruvada@linux.intel.com>
Cc: linux-iio@vger.kernel.org
Subject: Re: [PATCH 01/16] iio: hid-sensors: Convert units and exponent
Date: Sat, 03 May 2014 20:24:15 +0100 [thread overview]
Message-ID: <5365425F.9040309@kernel.org> (raw)
In-Reply-To: <535C0A2D.60207@linux.intel.com>
On 26/04/14 20:34, Srinivas Pandruvada wrote:
>
> On 04/25/2014 11:30 AM, Jonathan Cameron wrote:
>> On 19/04/14 00:22, Srinivas Pandruvada wrote:
>>> HID sensor hub specify a default unit and alternative units. This
>>> along with unit exponent can be used adjust scale. This change
>>> change HID sensor data units to IIO defined units for each
>>> sensor type. So in this way user space can use a simply use:
>>> "(data + offset) * scale" to get final result.
>>>
>>> Signed-off-by: Srinivas Pandruvada <srinivas.pandruvada@linux.intel.com>
>> see inline.
Applied to the togreg branch of iio.git with a tweaks to comments as mentioned
(please check I didn't mess them up!)
>>> ---
>>> .../iio/common/hid-sensors/hid-sensor-attributes.c | 114 +++++++++++++++++++++
>>> include/linux/hid-sensor-hub.h | 4 +
>>> 2 files changed, 118 insertions(+)
>>>
>>> diff --git a/drivers/iio/common/hid-sensors/hid-sensor-attributes.c b/drivers/iio/common/hid-sensors/hid-sensor-attributes.c
>>> index 75b5473..451a95b 100644
>>> --- a/drivers/iio/common/hid-sensors/hid-sensor-attributes.c
>>> +++ b/drivers/iio/common/hid-sensors/hid-sensor-attributes.c
>>> @@ -26,6 +26,40 @@
>>> #include <linux/iio/iio.h>
>>> #include <linux/iio/sysfs.h>
>>>
>>> +struct {
>>> + 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 */
>>> +} unit_conversion[] = {
>>> + {HID_USAGE_SENSOR_ACCEL_3D, 0, 9, 806650},
>>> + {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_GYRO_3D, 0, 0, 17453},
>>> + {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_COMPASS_3D, 0, 1000, 0},
>>> + {HID_USAGE_SENSOR_COMPASS_3D, HID_USAGE_SENSOR_UNITS_GAUSS, 1, 0},
>>> +
>>> + {HID_USAGE_SENSOR_INCLINOMETER_3D, 0, 17453, 0},
>>> + {HID_USAGE_SENSOR_INCLINOMETER_3D,
>>> + HID_USAGE_SENSOR_UNITS_DEGREES, 0, 17453},
>>> + {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, 100000, 0},
>>> + {HID_USAGE_SENSOR_PRESSURE, HID_USAGE_SENSOR_UNITS_PASCAL, 1, 0},
>>> +};
>>> +
>>> static int pow_10(unsigned power)
>>> {
>>> int i;
>>> @@ -209,6 +243,86 @@ int hid_sensor_write_raw_hyst_value(struct hid_sensor_common *st,
>>> }
>>> EXPORT_SYMBOL(hid_sensor_write_raw_hyst_value);
>>>
>>> +/*
>>> + * This fuction applies the unit exponent to the scale.
>>> + * For example:
>> Function
>>> + * 9.806650 ->exp:2-> val0[980]val1[6650]
>>> + * 9.000806 ->exp:2-> val0[900]val1[806]
>>> + * 0.174535 ->exp:2-> val0[17]val1[4535]
>>> + * 1.001745 ->exp:0-> val0[1]val1[1745]
>>> + * 1.001745 ->exp:2-> val0[100]val1[1745]
>>> + * 1.001745 ->exp:4-> val0[10017]val1[45]
>>> + * 9.806650 ->exp:-2-> val0[0]val1[98066]
>> It took me a while to get what you meant here, perhaps for the examples it
>> is worth showing the actual value directly as well. Maybe.
>> 9.806650 exp:2->980.6650 -> val0[980]val1[665000] (note the above is I think
>> incorrect due to lack of trailing zeros on val1.
>>> + */
>>> +static void adjust_exponent_micro(int *val0, int *val1, int scale0,
>>> + int scale1, int exp)
>>> +{
>>> + int i;
>>> + int x;
>>> + int res;
>>> + int rem;
>>> +
>>> + if (exp > 0) {
>>> + *val0 = scale0 * pow_10(exp);
>>> + res = 0;
>>> + if (exp > 6) {
>>> + *val1 = 0;
>>> + return;
>>> + }
>> so if example 1 above..
>> val0 = 900 as desired.
>> i = 0;
>> sca1e1 = 806650
>> x = 806650/100000 = 8;
>> res = 80;
>> scale1 = 06650
>> i = 1
>> x = 06650/10000 = 0;
>> res = 80;
>> so 980. Good.
>>> + for (i = 0; i < exp; ++i) {
>>> + x = scale1 / pow_10(5 - i);
>>> + res += (pow_10(exp - 1 - i) * x);
>>> + scale1 = scale1 % pow_10(5 - i);
>>> + }
>>> + *val0 += res;
>>> + *val1 = scale1 * pow_10(exp);
>> val1 = 665000 which is correct with the trailing zeros in val2.
>> So your code is good but examples need those zeros!
> Impressive, Absolutely great review. I will correct comments in the next version.
Given this was the only issue in the patch set I'll just fix it up as I merge
it.
It's been a couple of weeks an noone else has chipped in on this series, so
I'm taking it now.
>
> Thanks,
> Srinivas
>>
>>> + } else if (exp < 0) {
>>> + exp = abs(exp);
>>> + if (exp > 6) {
>>> + *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);
>>> + }
>>> + *val1 = rem * pow_10(6 - exp) + res;
>>> + } else {
>>> + *val0 = scale0;
>>> + *val1 = scale1;
>>> + }
>>> +}
>>> +
>>> +int hid_sensor_format_scale(u32 usage_id,
>>> + struct hid_sensor_hub_attribute_info *attr_info,
>>> + int *val0, int *val1)
>>> +{
>>> + int i;
>>> + int exp;
>>> +
>>> + *val0 = 1;
>>> + *val1 = 0;
>>> +
>>> + for (i = 0; ARRAY_SIZE(unit_conversion); ++i) {
>>> + if (unit_conversion[i].usage_id == 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_val0,
>>> + unit_conversion[i].scale_val1, exp);
>>> + break;
>>> + }
>>> + }
>>> +
>>> + return IIO_VAL_INT_PLUS_MICRO;
>>> +}
>>> +EXPORT_SYMBOL(hid_sensor_format_scale);
>>> +
>>> int hid_sensor_parse_common_attributes(struct hid_sensor_hub_device *hsdev,
>>> u32 usage_id,
>>> struct hid_sensor_common *st)
>>> diff --git a/include/linux/hid-sensor-hub.h b/include/linux/hid-sensor-hub.h
>>> index b70cfd7..89626b2 100644
>>> --- a/include/linux/hid-sensor-hub.h
>>> +++ b/include/linux/hid-sensor-hub.h
>>> @@ -223,4 +223,8 @@ int hid_sensor_read_samp_freq_value(struct hid_sensor_common *st,
>>> int hid_sensor_get_usage_index(struct hid_sensor_hub_device *hsdev,
>>> u32 report_id, int field_index, u32 usage_id);
>>>
>>> +int hid_sensor_format_scale(u32 usage_id,
>>> + struct hid_sensor_hub_attribute_info *attr_info,
>>> + int *val0, int *val1);
>>> +
>>> #endif
>>>
>>
>> --
>> To unsubscribe from this list: send the line "unsubscribe linux-iio" in
>> the body of a message to majordomo@vger.kernel.org
>> More majordomo info at http://vger.kernel.org/majordomo-info.html
>>
>
next prev parent reply other threads:[~2014-05-03 19:22 UTC|newest]
Thread overview: 41+ messages / expand[flat|nested] mbox.gz Atom feed top
2014-04-18 23:22 [PATCH 01/16] iio: hid-sensors: Convert units and exponent Srinivas Pandruvada
2014-04-18 23:22 ` [PATCH 02/16] iio: hid-sensors: Add api to get poll value Srinivas Pandruvada
2014-05-03 19:34 ` Jonathan Cameron
2014-04-18 23:22 ` [PATCH 03/16] iio: hid-sensors: Accelerometer 3D: adjust scale and offset Srinivas Pandruvada
2014-05-03 19:35 ` Jonathan Cameron
2014-04-18 23:22 ` [PATCH 04/16] iio: hid-sensors: Gyro 3D : " Srinivas Pandruvada
2014-05-03 19:35 ` Jonathan Cameron
2014-04-18 23:22 ` [PATCH 05/16] iio: hid-sensors: ALS: " Srinivas Pandruvada
2014-05-03 19:36 ` Jonathan Cameron
2014-04-18 23:22 ` [PATCH 06/16] iio: hid-sensors: Compass 3D: " Srinivas Pandruvada
2014-05-03 19:36 ` Jonathan Cameron
2014-04-18 23:22 ` [PATCH 07/16] iio: hid-sensors: Inclinometer " Srinivas Pandruvada
2014-05-03 19:37 ` Jonathan Cameron
2014-04-18 23:22 ` [PATCH 08/16] iio: hid-sensors: Pressure: " Srinivas Pandruvada
2014-05-03 19:37 ` Jonathan Cameron
2014-04-18 23:22 ` [PATCH 09/16] iio: hid-sensors: Add API to power on/off Srinivas Pandruvada
2014-05-03 19:38 ` Jonathan Cameron
2014-04-18 23:22 ` [PATCH 10/16] iio: hid-sensors: Accelerometer 3D: Raw read support Srinivas Pandruvada
2014-05-03 19:38 ` Jonathan Cameron
2014-04-18 23:22 ` [PATCH 11/16] iio: hid-sensors: Gyro " Srinivas Pandruvada
2014-05-03 19:39 ` Jonathan Cameron
2014-04-18 23:22 ` [PATCH 12/16] iio: hid-sensors: ALS: " Srinivas Pandruvada
2014-05-03 19:39 ` Jonathan Cameron
2014-04-18 23:22 ` [PATCH 13/16] iio: hid-sensors: Proximity: " Srinivas Pandruvada
2014-05-03 19:40 ` Jonathan Cameron
2014-04-18 23:22 ` [PATCH 14/16] iio: hid-sensors: Compass 3D: " Srinivas Pandruvada
2014-05-03 19:41 ` Jonathan Cameron
2014-04-18 23:22 ` [PATCH 15/16] iio: hid-sensors: Inclinometer " Srinivas Pandruvada
2014-05-03 19:41 ` Jonathan Cameron
2014-04-18 23:22 ` [PATCH 16/16] iio: hid-sensors: Pressure: " Srinivas Pandruvada
2014-04-23 21:03 ` Jonathan Cameron
2014-05-03 19:45 ` Jonathan Cameron
2014-04-23 20:57 ` [PATCH 01/16] iio: hid-sensors: Convert units and exponent Jonathan Cameron
2014-04-23 21:17 ` Srinivas Pandruvada
2014-04-25 18:34 ` Jonathan Cameron
2014-04-25 18:30 ` Jonathan Cameron
2014-04-26 19:34 ` Srinivas Pandruvada
2014-05-03 19:24 ` Jonathan Cameron [this message]
2014-05-03 19:32 ` Jonathan Cameron
2014-05-04 14:49 ` Srinivas Pandruvada
[not found] ` <4102328d-88e0-441f-87c9-0b57cbc33ca5@email.android.com>
2014-05-05 1:00 ` Srinivas Pandruvada
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=5365425F.9040309@kernel.org \
--to=jic23@kernel.org \
--cc=linux-iio@vger.kernel.org \
--cc=srinivas.pandruvada@linux.intel.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).