linux-input.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Gregor Riepl <onitake@gmail.com>
To: Jonathan Bakker <xc-racer2@live.ca>,
	Linus Walleij <linus.walleij@linaro.org>
Cc: Jonathan Cameron <jic23@kernel.org>,
	linux-iio <linux-iio@vger.kernel.org>,
	Hartmut Knaack <knaack.h@gmx.de>,
	Lars-Peter Clausen <lars@metafoo.de>,
	Peter Meerwald-Stadler <pmeerw@pmeerw.net>,
	Linux Input <linux-input@vger.kernel.org>
Subject: Re: [PATCH] RFT: iio: gp2ap002: Replace LUT with math
Date: Tue, 11 Feb 2020 08:15:53 +0100	[thread overview]
Message-ID: <9797aa15-eed1-932c-3cd6-64d7ccbf9d5e@gmail.com> (raw)
In-Reply-To: <BYAPR10MB34797AABF2536F03BC3B4065A3190@BYAPR10MB3479.namprd10.prod.outlook.com>

> Just an FYI - the ADC_MIN should probably be 0 for full darkness,
> but I understand the concept and like it :)
> 
> I believe the light sensor part to be a Sharp GA1A light detector or similar,
> based on the fact that DigiKey had a page (1) that
> mentions both together and that the specs for the GA1A1S202WP (2) line up quite well with
> those of the GP2AP002.  Note that the datasheet for the GA1A1S202WP even mentions the
> illuminance = 10 ^ (current / 10) formula, re-arranged as
> current = 10 * log(illuminance), although it specifies uA as opposed to mA which is the same
> as the Android libsensors (both the Nexus S (crespo) (3) and Galaxy Nexus (tuna) (4)) versions.
> I suspect that this should be adjusted after the call to iio_read_channel_processed().

How about this, then?
With a full-range lookup table (47 values), it's even possible to avoid
additional constants and simply clamp to the size of the table.


/*
 * This array maps current and lux.
 *
 * Ambient light sensing range is 3 to 55000 lux.
 *
 * This mapping is based on the following formula.
 * illuminance = 10 ^ (current[mA] / 10)
 *
 * When the ADC measures 0, return 0 lux.
 */
static const u16 gp2ap002_illuminance_table[] = {
	0, 1, 1, 2, 2, 3, 4, 5, 6, 8, 10, 12, 16, 20, 25, 32, 40, 50, 63, 79,
	100, 126, 158, 200, 251, 316, 398, 501, 631, 794, 1000, 1259, 1585,
	1995, 2512, 3162, 3981, 5012, 6310, 7943, 10000, 12589, 15849, 19953,
	25119, 31623, 39811, 50119,
};

static int gp2ap002_get_lux(struct gp2ap002 *gp2ap002)
{
	const struct gp2ap002_illuminance *ill1;
	const struct gp2ap002_illuminance *ill2;
	int ret, res;
	u16 lux;

	ret = iio_read_channel_processed(gp2ap002->alsout, &res);
	if (ret < 0)
		return ret;

	dev_dbg(gp2ap002->dev, "read %d mA from ADC\n", res);

	/* ensure we don't under/overflow */
	clamp(res, 0, ARRAY_SIZE(gp2ap002_illuminance_table) - 1);
	lux = gp2ap002_illuminance_table[res];

	return (int)lux;
}

  reply	other threads:[~2020-02-11  7:15 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-02-08 12:33 [PATCH] RFT: iio: gp2ap002: Replace LUT with math Linus Walleij
2020-02-09 10:27 ` Gregor Riepl
2020-02-10 13:28   ` Linus Walleij
2020-02-10 19:33     ` Gregor Riepl
2020-02-10 23:19       ` Jonathan Bakker
2020-02-11  7:15         ` Gregor Riepl [this message]
2020-02-11  7:51           ` Linus Walleij
2020-02-11  8:09             ` Gregor Riepl
2020-02-10 22:53 ` Jonathan Bakker

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=9797aa15-eed1-932c-3cd6-64d7ccbf9d5e@gmail.com \
    --to=onitake@gmail.com \
    --cc=jic23@kernel.org \
    --cc=knaack.h@gmx.de \
    --cc=lars@metafoo.de \
    --cc=linus.walleij@linaro.org \
    --cc=linux-iio@vger.kernel.org \
    --cc=linux-input@vger.kernel.org \
    --cc=pmeerw@pmeerw.net \
    --cc=xc-racer2@live.ca \
    /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).