Re: [PATCH] RFT: iio: gp2ap002: Replace LUT with math

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



> 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;
}



[Index of Archives]     [Linux Media Devel]     [Linux USB Devel]     [Video for Linux]     [Linux Audio Users]     [Yosemite News]     [Linux Kernel]     [Linux SCSI]     [Linux Wireless Networking]     [Linux Omap]

  Powered by Linux