From a2f814e52514989f5f8bd795ff603b81095598c2 Mon Sep 17 00:00:00 2001 From: Pawit Pornkitprasan Date: Fri, 23 Dec 2011 19:33:01 +0700 Subject: libsensors: Update from crespo Light sensor now uses calculation instead of pre-determined values Requires updated kernel --- libsensors/LightSensor.cpp | 50 ++++++++-------------------------------------- 1 file changed, 8 insertions(+), 42 deletions(-) (limited to 'libsensors/LightSensor.cpp') diff --git a/libsensors/LightSensor.cpp b/libsensors/LightSensor.cpp index 9025029..5df7cd5 100644 --- a/libsensors/LightSensor.cpp +++ b/libsensors/LightSensor.cpp @@ -27,17 +27,9 @@ /*****************************************************************************/ -/* The Crespo ADC sends 4 somewhat bogus events after enabling the sensor. - This becomes a problem if the phone is turned off in bright light - and turned back on in the dark. - To avoid this we ignore the first 4 events received after enabling the sensor. - */ -#define FIRST_GOOD_EVENT 5 - LightSensor::LightSensor() - : SensorBase(NULL, "light_sensor"), + : SensorBase(NULL, "lightsensor-level"), mEnabled(0), - mEventsSinceEnable(0), mInputReader(4), mHasPendingEvent(false) { @@ -79,8 +71,6 @@ int LightSensor::setDelay(int32_t handle, int64_t ns) int LightSensor::enable(int32_t handle, int en) { int flags = en ? 1 : 0; - mEventsSinceEnable = 0; - mPreviousLight = -1; if (flags != mEnabled) { int fd; strcpy(&input_sysfs_path[input_sysfs_path_len], "enable"); @@ -131,18 +121,19 @@ int LightSensor::readEvents(sensors_event_t* data, int count) int type = event->type; if (type == EV_ABS) { if (event->code == EVENT_TYPE_LIGHT) { - mPendingEvent.light = indexToValue(event->value); - if (mEventsSinceEnable < FIRST_GOOD_EVENT) - mEventsSinceEnable++; + // Convert adc value to lux assuming: + // I = 10 * log(Ev) uA + // R = 47kOhm + // Max adc value 4095 = 3.3V + // 1/4 of light reaches sensor + mPendingEvent.light = powf(10, event->value * (330.0f / 4095.0f / 47.0f)) * 4; } } else if (type == EV_SYN) { mPendingEvent.timestamp = timevalToNano(event->time); - if (mEnabled && (mPendingEvent.light != mPreviousLight) && - mEventsSinceEnable >= FIRST_GOOD_EVENT) { + if (mEnabled) { *data++ = mPendingEvent; count--; numEventReceived++; - mPreviousLight = mPendingEvent.light; } } else { LOGE("LightSensor: unknown event (type=%d, code=%d)", @@ -153,28 +144,3 @@ int LightSensor::readEvents(sensors_event_t* data, int count) return numEventReceived; } - -float LightSensor::indexToValue(size_t index) const -{ - /* Driver gives a rolling average adc value. We convert it lux levels. */ - static const struct adcToLux { - size_t adc_value; - float lux_value; - } adcToLux[] = { - { 150, 10.0 }, /* from 0 - 150 adc, we map to 10.0 lux */ - { 800, 160.0 }, /* from 151 - 800 adc, we map to 160.0 lux */ - { 900, 225.0 }, /* from 801 - 900 adc, we map to 225.0 lux */ - { 1000, 320.0 }, /* from 901 - 1000 adc, we map to 320.0 lux */ - { 1200, 640.0 }, /* from 1001 - 1200 adc, we map to 640.0 lux */ - { 1400, 1280.0 }, /* from 1201 - 1400 adc, we map to 1280.0 lux */ - { 1600, 2600.0 }, /* from 1401 - 1600 adc, we map to 2600.0 lux */ - { 4095, 10240.0 }, /* from 1601 - 4095 adc, we map to 10240.0 lux */ - }; - size_t i; - for (i = 0; i < ARRAY_SIZE(adcToLux); i++) { - if (index < adcToLux[i].adc_value) { - return adcToLux[i].lux_value; - } - } - return adcToLux[ARRAY_SIZE(adcToLux)-1].lux_value; -} -- cgit v1.1