Chinaunix首页 | 论坛 | 博客
  • 博客访问: 497448
  • 博文数量: 92
  • 博客积分: 3146
  • 博客等级: 中校
  • 技术积分: 2314
  • 用 户 组: 普通用户
  • 注册时间: 2010-09-27 10:20
文章分类

全部博文(92)

文章存档

2014年(3)

2013年(17)

2012年(16)

2011年(22)

2010年(34)

分类: 嵌入式

2012-07-11 19:42:17

从上一篇这几个简单的sensors应该可以成功获取数据了,这里就再来看看hal层是如何实现的

点击(此处)折叠或打开

  1. #include <fcntl.h>
  2. #include <errno.h>
  3. #include <math.h>
  4. #include <poll.h>
  5. #include <unistd.h>
  6. #include <dirent.h>
  7. #include <sys/select.h>

  8. //#include <linux/lightsensor.h>

  9. #include <cutils/log.h>

  10. #include "stk_defines.h"
  11. #include "LightSensor.h"


  12. /*****************************************************************************/

  13. static const float sLuxValues[4] =
  14.  {
  15.     
  16.    3200,
  17.   1800,
  18.     1000,
  19.     200,
  20. };

  21. LightSensor::LightSensor()
  22.     : SensorBase(NULL, "light_sensor"),//"light_sensor"必须和你驱动那边的名字对应,通过input找到这个设备
  23.       mEnabled(0),
  24.       mInputReader(4),
  25.       mHasPendingEvent(false)
  26. {
  27.     mPendingEvent.version = sizeof(sensors_event_t);
  28.     mPendingEvent.sensor = ID_L;
  29.     mPendingEvent.type = SENSOR_TYPE_LIGHT;
  30.     memset(mPendingEvent.data, 0, sizeof(mPendingEvent.data));    

  31.     mEnabled=true;
  32.     
  33. }


  34. LightSensor::~LightSensor()
  35. {
  36.     if(mEnabled)    
  37.      enable(ID_L,0);
  38. }

  39. int LightSensor::setInitialState() {

  40.     return 0;
  41. }

  42. int LightSensor::setDelay(int32_t handle, int64_t ns)
  43. {
  44.     LOGD("hbb test LightSensor setDelay handle = %d \n",handle);
  45.     return 0;
  46. }

  47. int LightSensor::enable(int32_t, int en) {

  48.     if(!en)        
  49.         mEnabled=true;    
  50.     else        
  51.         mEnabled=false;
  52.     LOGD("hbb test LightSensor enable mEnabled = %d \n",mEnabled);
  53.     return 0;
  54.     
  55. }




  56. bool LightSensor::hasPendingEvents() const {
  57.     return mHasPendingEvent;
  58. }

  59. int LightSensor::readEvents(sensors_event_t* data, int count)
  60. {
  61.     if (count < 1)
  62.         return -EINVAL;

  63.     if (mHasPendingEvent) {
  64.         mHasPendingEvent = false;
  65.         mPendingEvent.timestamp = getTimestamp();
  66.         *data = mPendingEvent;
  67.         return mEnabled ? 1 : 0;
  68.     }

  69.     ssize_t n = mInputReader.fill(data_fd);
  70.     if (n < 0)
  71.         return n;

  72.     int numEventReceived = 0;
  73.     input_event const* event;
  74.     LOGD("huabinbin test lightsensor count=%d\n",count);    
  75.     while (count && mInputReader.readEvent(&event)) {

  76.     int fd=open("/dev/input/event3", O_RDONLY); //如果你不确定你输入设备是多久,可以在终端查看  ls /dev/input
  77.         if(fd<0)
  78.     {
  79.             LOGD("readEvents: open event3 failed...\n");
  80.             return fd;
  81.         }
  82.         int ret=read(fd,&event,sizeof(event));
  83.         if(ret<sizeof(event))
  84.     {
  85.             LOGD("readEvent read failed....\n");
  86.             return ret;
  87.         }
  88.         close(fd);
  89.         int type=event->type;
  90.         LOGD("HUABINBIN TEST type =%d,value= %d EV_ABS =%d event->code =%d\n",type,event->value,EV_ABS,event->code);
  91.         if(type == EV_ABS)
  92.     {
  93.      // if (event->code == EVENT_TYPE_LIGHT)
  94.         {        

  95.                 mPendingEvent.light = (float)sLuxValues[event->value];
  96.                 //细心的人可能会发现我这里和一般的lightsensors不一样!这里就不要管啦        
  97.          }            
  98.         }
  99.     else if(type==EV_SYN)
  100.     {
  101.             mPendingEvent.timestamp = timevalToNano(event->time);
  102.             //if(mEnabled )
  103.             if(mPreviousLightLux != mPendingEvent.light)
  104.      {
  105.                 *data++ = mPendingEvent;
  106.                 count--;
  107.                 numEventReceived++;
  108.                 mPreviousLightLux = mPendingEvent.light;
  109.                 LOGD("Current lux: %d\n",(int)mPendingEvent.light);
  110.             }
  111.        }
  112.     else{
  113.              LOGE("LightSensor: unknown event (type=%d, code=%d)", type, event->code);
  114.     }
  115.         mInputReader.next();

  116.     }

  117.     return numEventReceived;
  118. }

关于驱动更新代码以上传,hal篇也需要相应的更新,具体代码如下:

点击(此处)折叠或打开

  1. #include <fcntl.h>
  2. #include <errno.h>
  3. #include <math.h>
  4. #include <poll.h>
  5. #include <unistd.h>
  6. #include <dirent.h>
  7. #include <sys/select.h>

  8. //#include <linux/lightsensor.h>

  9. #include <cutils/log.h>

  10. #include "stk_defines.h"
  11. #include "LightSensor.h"

  12. static const float sLuxValues[4] =
  13.  {
  14.     
  15.    3200,
  16.   1800,
  17.     1000,
  18.     200,
  19. };

  20. #if LS
  21. /*****************************************************************************/

  22. #if STK_ENABLE_SENSOR_USE_BINARY_SYSFS
  23. LightSensor::LightSensor()
  24.     : SensorBase(NULL, "light_sensor"),
  25.       mEnabled(0),
  26.       mInputReader(4),
  27.       mHasPendingEvent(false)
  28. {
  29.     mPendingEvent.version = sizeof(sensors_event_t);
  30.     mPendingEvent.sensor = ID_L;
  31.     mPendingEvent.type = SENSOR_TYPE_LIGHT;
  32.     memset(mPendingEvent.data, 0, sizeof(mPendingEvent.data));    

  33.     open_device(O_RDWR);
  34.     
  35.     if (dev_fd>=0)
  36.     {
  37.         char flags = 0;
  38.         if (read(dev_fd, &flags, 1)==1)
  39.         {
  40.             if (flags)
  41.             {
  42.                 mEnabled = 1;
  43.                 setInitialState();
  44.             }
  45. }
  46.     }
  47. }
  48. #else
  49. LightSensor::LightSensor()
  50.     : SensorBase(NULL, "light_sensor"),
  51.       mEnabled(0),
  52.       mInputReader(4),
  53.       mHasPendingEvent(false)
  54. {
  55.     mPendingEvent.version = sizeof(sensors_event_t);
  56.     mPendingEvent.sensor = ID_L;
  57.     mPendingEvent.type = SENSOR_TYPE_LIGHT;
  58.     memset(mPendingEvent.data, 0, sizeof(mPendingEvent.data));

  59.     int fd = open("/sys/devices/platform/light_sensor/ls_enable",O_RDONLY);

  60.     if (fd>=0)
  61.     {
  62.         char flags = '0';
  63.         if (read(fd, &flags, 1)==1)
  64.         {
  65.             if (flags=='1')
  66.             {
  67.                 mEnabled = 1;
  68.                 //setInitialState();
  69.             }
  70.         }
  71.         close(fd);
  72.     }
  73. }
  74. #endif //STK_ENABLE_SENSOR_USE_BINARY_SYSFS

  75. LightSensor::~LightSensor()
  76. {
  77.      enable(ID_L,0);
  78. }

  79. #if 1
  80. int LightSensor::setInitialState() {
  81.     struct input_absinfo absinfo;
  82.     if (!ioctl(data_fd, EVIOCGABS(EVENT_TYPE_LIGHT), &absinfo)) {
  83.         mPendingEvent.light = (float)absinfo.value;
  84.         mHasPendingEvent = true;
  85.     }
  86.     return 0;
  87. }
  88. #endif

  89. #if STK_ENABLE_SENSOR_USE_BINARY_SYSFS
  90. int LightSensor::enable(int32_t, int en) {
  91.     int err = 0;
  92.     char bEnable = (en?1:0);

  93.     if (bEnable != mEnabled) {

  94.         if (dev_fd>=0)
  95. {
  96.             lseek(dev_fd,0,SEEK_SET);
  97.             write(dev_fd,&bEnable,sizeof(bEnable));
  98.             mEnabled = bEnable;
  99.             err = 0;
  100.             if (mEnabled)
  101.                 setInitialState();
  102. }
  103.         else
  104.             err = -1;

  105.     }
  106.     return err;
  107. }
  108. #else
  109. int LightSensor::enable(int32_t handle, int en) {
  110.     int err = 0;
  111.     int fd;
  112.     char bEnable = (en?1:0);
  113.     const char* strEnable[] = {"0","1"};
  114.     LOGD("huabinbin test LightSensor::enable handle = %d en =%d\n",handle,en);
  115.     if (bEnable != mEnabled) {
  116.         fd = open("/sys/devices/platform/light_sensor/ls_enable",O_WRONLY);
  117.         if (fd>=0)
  118.         {
  119.             write(fd,en?strEnable[1]:strEnable[0],2);
  120.             mEnabled = bEnable;
  121.             err = 0;
  122.          // if (mEnabled)
  123.               // setInitialState();
  124.             close(fd);
  125.         }
  126.     else        
  127.             err = -1;
  128.     
  129. }
  130.     return err;
  131. }


  132. #endif //STK_ENABLE_SENSOR_USE_BINARY_SYSFS

  133. bool LightSensor::hasPendingEvents() const {
  134.     return mHasPendingEvent;
  135. }

  136. int LightSensor::readEvents(sensors_event_t* data, int count)
  137. {
  138.     if (count < 1)
  139.         return -EINVAL;

  140.     if (mHasPendingEvent) {
  141.         mHasPendingEvent = false;
  142.         mPendingEvent.timestamp = getTimestamp();
  143.         *data = mPendingEvent;
  144.         return mEnabled ? 1 : 0;
  145.     }

  146.     ssize_t n = mInputReader.fill(data_fd);
  147.     if (n < 0)
  148.         return n;

  149.     int numEventReceived = 0;
  150.     input_event const* event;

  151.     while (count && mInputReader.readEvent(&event)) {
  152.         int type=event->type;
  153.         if (type == EV_ABS) {
  154.             if (event->code == EVENT_TYPE_LIGHT) {
  155.                // mPendingEvent.light = (float)sLuxValues[event->value];
  156.                mPendingEvent.light = (float)event->value;
  157.          }            
  158.         } else if (type == EV_SYN) {
  159.             mPendingEvent.timestamp = timevalToNano(event->time);
  160.             if (mEnabled) {
  161.                 *data++ = mPendingEvent;
  162.                 count--;
  163.                 numEventReceived++;
  164.             }
  165.         } else {
  166.             LOGE("LightSensor: unknown event (type=%d, code=%d)",
  167.                     type, event->code);
  168.     }
  169.         mInputReader.next();
  170.     }

  171.     return numEventReceived;
  172. }

  173. #endif // LS


 

 


这样就应该可以成功的收到数据了!
 
附:
     如果第一次调试光感的话,可能你会发现到hal层我也能收到数据,也上报了数据,为什么就没有效果呢?看看这里是不是有你所需要的

需要修改config.xml

framework/base/core/res/res/values/config.xml 改为true,还要修改config_autoBrightnessLevelsconfig_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

 

 

阅读(5369) | 评论(0) | 转发(2) |
给主人留下些什么吧!~~