从上一篇这几个简单的sensors应该可以成功获取数据了,这里就再来看看hal层是如何实现的
- #include <fcntl.h>
- #include <errno.h>
- #include <math.h>
- #include <poll.h>
- #include <unistd.h>
- #include <dirent.h>
- #include <sys/select.h>
- //#include <linux/lightsensor.h>
- #include <cutils/log.h>
- #include "stk_defines.h"
- #include "LightSensor.h"
- /*****************************************************************************/
- static const float sLuxValues[4] =
- {
-
- 3200,
- 1800,
- 1000,
- 200,
- };
- LightSensor::LightSensor()
- : SensorBase(NULL, "light_sensor"),//"light_sensor"必须和你驱动那边的名字对应,通过input找到这个设备
- mEnabled(0),
- mInputReader(4),
- mHasPendingEvent(false)
- {
- mPendingEvent.version = sizeof(sensors_event_t);
- mPendingEvent.sensor = ID_L;
- mPendingEvent.type = SENSOR_TYPE_LIGHT;
- memset(mPendingEvent.data, 0, sizeof(mPendingEvent.data));
- mEnabled=true;
-
- }
- LightSensor::~LightSensor()
- {
- if(mEnabled)
- enable(ID_L,0);
- }
- int LightSensor::setInitialState() {
- return 0;
- }
- int LightSensor::setDelay(int32_t handle, int64_t ns)
- {
- LOGD("hbb test LightSensor setDelay handle = %d \n",handle);
- return 0;
- }
- int LightSensor::enable(int32_t, int en) {
- if(!en)
- mEnabled=true;
- else
- mEnabled=false;
- LOGD("hbb test LightSensor enable mEnabled = %d \n",mEnabled);
- return 0;
-
- }
- bool LightSensor::hasPendingEvents() const {
- return mHasPendingEvent;
- }
- int LightSensor::readEvents(sensors_event_t* data, int count)
- {
- if (count < 1)
- return -EINVAL;
- if (mHasPendingEvent) {
- mHasPendingEvent = false;
- mPendingEvent.timestamp = getTimestamp();
- *data = mPendingEvent;
- return mEnabled ? 1 : 0;
- }
- ssize_t n = mInputReader.fill(data_fd);
- if (n < 0)
- return n;
- int numEventReceived = 0;
- input_event const* event;
- LOGD("huabinbin test lightsensor count=%d\n",count);
- while (count && mInputReader.readEvent(&event)) {
- int fd=open("/dev/input/event3", O_RDONLY); //如果你不确定你输入设备是多久,可以在终端查看 ls /dev/input
- if(fd<0)
- {
- LOGD("readEvents: open event3 failed...\n");
- return fd;
- }
- int ret=read(fd,&event,sizeof(event));
- if(ret<sizeof(event))
- {
- LOGD("readEvent read failed....\n");
- return ret;
- }
- close(fd);
- int type=event->type;
- LOGD("HUABINBIN TEST type =%d,value= %d EV_ABS =%d event->code =%d\n",type,event->value,EV_ABS,event->code);
- if(type == EV_ABS)
- {
- // if (event->code == EVENT_TYPE_LIGHT)
- {
- mPendingEvent.light = (float)sLuxValues[event->value];
- //细心的人可能会发现我这里和一般的lightsensors不一样!这里就不要管啦
- }
- }
- else if(type==EV_SYN)
- {
- mPendingEvent.timestamp = timevalToNano(event->time);
- //if(mEnabled )
- if(mPreviousLightLux != mPendingEvent.light)
- {
- *data++ = mPendingEvent;
- count--;
- numEventReceived++;
- mPreviousLightLux = mPendingEvent.light;
- LOGD("Current lux: %d\n",(int)mPendingEvent.light);
- }
- }
- else{
- LOGE("LightSensor: unknown event (type=%d, code=%d)", type, event->code);
- }
- mInputReader.next();
- }
- return numEventReceived;
- }
关于驱动更新代码以上传,hal篇也需要相应的更新,具体代码如下:
- #include <fcntl.h>
- #include <errno.h>
- #include <math.h>
- #include <poll.h>
- #include <unistd.h>
- #include <dirent.h>
- #include <sys/select.h>
- //#include <linux/lightsensor.h>
- #include <cutils/log.h>
- #include "stk_defines.h"
- #include "LightSensor.h"
- static const float sLuxValues[4] =
- {
-
- 3200,
- 1800,
- 1000,
- 200,
- };
- #if LS
- /*****************************************************************************/
- #if STK_ENABLE_SENSOR_USE_BINARY_SYSFS
- LightSensor::LightSensor()
- : SensorBase(NULL, "light_sensor"),
- mEnabled(0),
- mInputReader(4),
- mHasPendingEvent(false)
- {
- mPendingEvent.version = sizeof(sensors_event_t);
- mPendingEvent.sensor = ID_L;
- mPendingEvent.type = SENSOR_TYPE_LIGHT;
- memset(mPendingEvent.data, 0, sizeof(mPendingEvent.data));
- open_device(O_RDWR);
-
- if (dev_fd>=0)
- {
- char flags = 0;
- if (read(dev_fd, &flags, 1)==1)
- {
- if (flags)
- {
- mEnabled = 1;
- setInitialState();
- }
- }
- }
- }
- #else
- LightSensor::LightSensor()
- : SensorBase(NULL, "light_sensor"),
- mEnabled(0),
- mInputReader(4),
- mHasPendingEvent(false)
- {
- mPendingEvent.version = sizeof(sensors_event_t);
- mPendingEvent.sensor = ID_L;
- mPendingEvent.type = SENSOR_TYPE_LIGHT;
- memset(mPendingEvent.data, 0, sizeof(mPendingEvent.data));
- int fd = open("/sys/devices/platform/light_sensor/ls_enable",O_RDONLY);
- if (fd>=0)
- {
- char flags = '0';
- if (read(fd, &flags, 1)==1)
- {
- if (flags=='1')
- {
- mEnabled = 1;
- //setInitialState();
- }
- }
- close(fd);
- }
- }
- #endif //STK_ENABLE_SENSOR_USE_BINARY_SYSFS
- LightSensor::~LightSensor()
- {
- enable(ID_L,0);
- }
- #if 1
- int LightSensor::setInitialState() {
- struct input_absinfo absinfo;
- if (!ioctl(data_fd, EVIOCGABS(EVENT_TYPE_LIGHT), &absinfo)) {
- mPendingEvent.light = (float)absinfo.value;
- mHasPendingEvent = true;
- }
- return 0;
- }
- #endif
- #if STK_ENABLE_SENSOR_USE_BINARY_SYSFS
- int LightSensor::enable(int32_t, int en) {
- int err = 0;
- char bEnable = (en?1:0);
- if (bEnable != mEnabled) {
- if (dev_fd>=0)
- {
- lseek(dev_fd,0,SEEK_SET);
- write(dev_fd,&bEnable,sizeof(bEnable));
- mEnabled = bEnable;
- err = 0;
- if (mEnabled)
- setInitialState();
- }
- else
- err = -1;
- }
- return err;
- }
- #else
- int LightSensor::enable(int32_t handle, int en) {
- int err = 0;
- int fd;
- char bEnable = (en?1:0);
- const char* strEnable[] = {"0","1"};
- LOGD("huabinbin test LightSensor::enable handle = %d en =%d\n",handle,en);
- if (bEnable != mEnabled) {
- fd = open("/sys/devices/platform/light_sensor/ls_enable",O_WRONLY);
- if (fd>=0)
- {
- write(fd,en?strEnable[1]:strEnable[0],2);
- mEnabled = bEnable;
- err = 0;
- // if (mEnabled)
- // setInitialState();
- close(fd);
- }
- else
- err = -1;
-
- }
- return err;
- }
- #endif //STK_ENABLE_SENSOR_USE_BINARY_SYSFS
- bool LightSensor::hasPendingEvents() const {
- return mHasPendingEvent;
- }
- int LightSensor::readEvents(sensors_event_t* data, int count)
- {
- if (count < 1)
- return -EINVAL;
- if (mHasPendingEvent) {
- mHasPendingEvent = false;
- mPendingEvent.timestamp = getTimestamp();
- *data = mPendingEvent;
- return mEnabled ? 1 : 0;
- }
- ssize_t n = mInputReader.fill(data_fd);
- if (n < 0)
- return n;
- int numEventReceived = 0;
- input_event const* event;
- while (count && mInputReader.readEvent(&event)) {
- int type=event->type;
- if (type == EV_ABS) {
- if (event->code == EVENT_TYPE_LIGHT) {
- // mPendingEvent.light = (float)sLuxValues[event->value];
- mPendingEvent.light = (float)event->value;
- }
- } else if (type == EV_SYN) {
- mPendingEvent.timestamp = timevalToNano(event->time);
- if (mEnabled) {
- *data++ = mPendingEvent;
- count--;
- numEventReceived++;
- }
- } else {
- LOGE("LightSensor: unknown event (type=%d, code=%d)",
- type, event->code);
- }
- mInputReader.next();
- }
- return numEventReceived;
- }
- #endif // LS
这样就应该可以成功的收到数据了!
附:
如果第一次调试光感的话,可能你会发现到hal层我也能收到数据,也上报了数据,为什么就没有效果呢?看看这里是不是有你所需要的
需要修改config.xml,
framework/base/core/res/res/values/config.xml 改为true,还要修改config_autoBrightnessLevels,config_autoBrightnessLcdBacklightValues加入如下的等级数组;
true
//上报的光感adc值
- 200
- 400
- 1000
- 3000
//根据上报的adc值确定的当前背光值
- 25
- 55
- 70
- 170
- 250
如果你调试磁力传感器偏了90°的话可以修改这里(我的是三星s5pc110)
android2.3.4_GB_T34H\hardware\memsic\adapter :sensors_mag_mmc31xx.c文件中有如下的宏MMC31XX_INSTALL_DIR
你调试重力感应器偏了90°的话可以修改这里(我的是三星s5pc110)
android2.3.4_GB_T34H\hardware\memsic\adapter :sensors_mag_bma150.c文件中有如下的宏BMA150_INSTALL_DIR
阅读(740) | 评论(0) | 转发(0) |