linux-iio.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Lars-Peter Clausen <lars@metafoo.de>
To: Jonathan Cameron <jic23@kernel.org>
Cc: Hartmut Knaack <knaack.h@gmx.de>,
	Peter Meerwald <pmeerw@pmeerw.net>,
	linux-iio@vger.kernel.org, Lars-Peter Clausen <lars@metafoo.de>
Subject: [PATCH 2/9] iio: Add inverse unit conversion macros
Date: Wed,  5 Aug 2015 15:38:14 +0200	[thread overview]
Message-ID: <1438781901-28000-3-git-send-email-lars@metafoo.de> (raw)
In-Reply-To: <1438781901-28000-1-git-send-email-lars@metafoo.de>

Add inverse unit conversion macro to convert from standard IIO units to
units that might be used by some devices.

Those are useful in combination with scale factors that are specified as
IIO_VAL_FRACTIONAL. Typically the denominator for those specifications will
contain the maximum raw value the sensor will generate and the numerator
the value it maps to in a specific unit. Sometimes datasheets specify those
in different units than the standard IIO units (e.g. degree/s instead of
rad/s) and so we need to do a unit conversion.

>From a mathematical point of view it does not make a difference whether we
apply the unit conversion to the numerator or the inverse unit conversion
to the denominator since (x / y) / z = x / (y * z). But as the denominator
is typically a larger value and we are rounding both the numerator and
denominator to integer values using the later method gives us a better
precision (E.g. the relative error is smaller if we round 8000.3 to 8000
rather than rounding 8.3 to 8).

This is where in inverse unit conversion macros will be used.

Signed-off-by: Lars-Peter Clausen <lars@metafoo.de>
---
 include/linux/iio/iio.h | 17 +++++++++++++++++
 1 file changed, 17 insertions(+)

diff --git a/include/linux/iio/iio.h b/include/linux/iio/iio.h
index f791482..6bcc9537 100644
--- a/include/linux/iio/iio.h
+++ b/include/linux/iio/iio.h
@@ -645,6 +645,15 @@ int iio_str_to_fixpoint(const char *str, int fract_mult, int *integer,
 #define IIO_DEGREE_TO_RAD(deg) (((deg) * 314159ULL + 9000000ULL) / 18000000ULL)
 
 /**
+ * IIO_RAD_TO_DEGREE() - Convert rad to degree
+ * @rad: A value in rad
+ *
+ * Returns the given value converted from rad to degree
+ */
+#define IIO_RAD_TO_DEGREE(rad) \
+	(((rad) * 18000000ULL + 314159ULL / 2) / 314159ULL)
+
+/**
  * IIO_G_TO_M_S_2() - Convert g to meter / second**2
  * @g: A value in g
  *
@@ -652,4 +661,12 @@ int iio_str_to_fixpoint(const char *str, int fract_mult, int *integer,
  */
 #define IIO_G_TO_M_S_2(g) ((g) * 980665ULL / 100000ULL)
 
+/**
+ * IIO_M_S_2_TO_G() - Convert meter / second**2 to g
+ * @ms2: A value in meter / second**2
+ *
+ * Returns the given value converted from meter / second**2 to g
+ */
+#define IIO_M_S_2_TO_G(ms2) (((ms2) * 100000ULL + 980665ULL / 2) / 980665ULL)
+
 #endif /* _INDUSTRIAL_IO_H_ */
-- 
2.1.4


  parent reply	other threads:[~2015-08-05 13:38 UTC|newest]

Thread overview: 19+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-08-05 13:38 [PATCH 0/9] iio: adis updates Lars-Peter Clausen
2015-08-05 13:38 ` [PATCH 1/9] iio: adis16400: Fix adis16448 gyroscope scale Lars-Peter Clausen
2015-08-08 11:48   ` Jonathan Cameron
2015-08-05 13:38 ` Lars-Peter Clausen [this message]
2015-08-08 11:51   ` [PATCH 2/9] iio: Add inverse unit conversion macros Jonathan Cameron
2015-08-05 13:38 ` [PATCH 3/9] iio: adis16480: Fix scale factors Lars-Peter Clausen
2015-08-08 11:52   ` Jonathan Cameron
2015-08-05 13:38 ` [PATCH 4/9] iio: adis16400: adis16300 has product ID and serial number Lars-Peter Clausen
2015-08-08 11:54   ` Jonathan Cameron
2015-08-05 13:38 ` [PATCH 5/9] iio: adis16400: Add ADIS16305 support Lars-Peter Clausen
2015-08-08 11:54   ` Jonathan Cameron
2015-08-05 13:38 ` [PATCH 6/9] iio: adis16260: Add ADIS16266 support Lars-Peter Clausen
2015-08-08 11:58   ` Jonathan Cameron
2015-08-05 13:38 ` [PATCH 7/9] iio: adis16400: Add ADIS16367 support Lars-Peter Clausen
2015-08-08 14:08   ` Jonathan Cameron
2015-08-05 13:38 ` [PATCH 8/9] iio: adis16400: Add ADIS16445 support Lars-Peter Clausen
2015-08-08 14:09   ` Jonathan Cameron
2015-08-05 13:38 ` [PATCH 9/9] iio: adis16136: Add ADIS16137 support Lars-Peter Clausen
2015-08-08 14:10   ` Jonathan Cameron

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=1438781901-28000-3-git-send-email-lars@metafoo.de \
    --to=lars@metafoo.de \
    --cc=jic23@kernel.org \
    --cc=knaack.h@gmx.de \
    --cc=linux-iio@vger.kernel.org \
    --cc=pmeerw@pmeerw.net \
    /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).