android下调试光感、温度、气压计(驱动篇)

前一段时间刚好要调试几个sensors,最开始手头上啥代码也没有,最后花了几天的时间熟悉了这一块,最
后感觉sensors相对其他的模块还是要简单的多!
 
第一步:
     完成相关驱动的编写,这里最开始我挺犹豫的,我驱动的数据如何上报??通过ioctl??我不想那么麻烦,最好在网上找到一个关于用input机制上报对应的值,这样就简单多了,先贴下代码

点击(此处)折叠或打开

  1. #include <linux/platform_device.h>
  2. #include <linux/input-polldev.h>
  3. #include <linux/kernel.h>
  4. #include <linux/module.h>
  5. #include <mach/adc.h>
  6. //#include <linux/saradc.h>

  7. #define POLL_INTERVAL    2000     /* poll for input every 2s*/
  8. #define LUX_LEVEL    4                    /* 0~4 report 5 levels*/

  9. static struct platform_device *pdev;
  10. static struct input_dev *idev;
  11. static struct input_polled_dev *light_sensor_idev;



  12. static const int sAdcValues[LUX_LEVEL] = {
  13.     200,
  14.       1000,
  15.     1800,
  16.     3400,
  17. };

  18. struct light_sensor_info {
  19.     int lux_level;
  20.     int suspend;
  21. };
  22. static struct light_sensor_info ls_info;

  23. /* Sysfs Files */
  24. static ssize_t light_sensor_lux_level_show(struct device *dev,
  25.                  struct device_attribute *attr, char *buf)
  26. {
  27.     return sprintf(buf, "(%d)\n",get_s3c_adc_convert(3));
  28. }

  29. static DEVICE_ATTR(lux_level, 0444, light_sensor_lux_level_show, NULL);

  30. static struct attribute *light_sensor_attributes[] = {
  31.     &dev_attr_lux_level.attr,
  32.     NULL,
  33. };

  34. static struct attribute_group light_sensor_attribute_group = {
  35.     .attrs = light_sensor_attributes,
  36. };


  37. /* Device model stuff */
  38. static int light_sensor_probe(struct platform_device *dev)
  39. {
  40.     printk(KERN_INFO "light_sensor: device successfully initialized.\n");
  41.     return 0;
  42. }

  43. #ifdef CONFIG_PM
  44. static int light_sensor_suspend(struct platform_device *dev, pm_message_t state)
  45. {
  46.         ls_info.suspend = 1;
  47.     return 0;
  48. }

  49. static int light_sensor_resume(struct platform_device *dev)
  50. {
  51.         ls_info.suspend = 0;
  52.     return 0;
  53. }
  54. #else
  55.     #define light_sensor_suspend NULL
  56.     #define light_sensor_resume NULL
  57. #endif

  58. static struct platform_driver light_sensor_driver = {
  59.     .probe = light_sensor_probe,
  60.     .resume = light_sensor_resume,
  61.     .suspend = light_sensor_suspend,
  62.     .driver    = {
  63.         .name = "light_sensor",
  64.         .owner = THIS_MODULE,
  65.     },
  66. };

  67. static void light_sensor_dev_poll(struct input_polled_dev *dev)
  68. {
  69.         int adc_val, i;

  70.     adc_val = get_s3c_adc_convert(3);
  71.     for(i = 0; i < LUX_LEVEL; i++) {
  72.         if(adc_val < sAdcValues[i])
  73.             break;
  74.     }

  75.    
  76.     
  77.     if(ls_info.lux_level != i) {
  78.         ls_info.lux_level = i;    
  79.         input_report_abs(idev, ABS_X, ls_info.lux_level);
  80.         input_sync(idev);
  81.         
  82.     }    

  83. }

  84. /* Module stuff */
  85. static int __init light_sensor_init(void)
  86. {
  87.     
  88.     int ret;

  89.     ret = platform_driver_register(&light_sensor_driver);
  90.     if (ret)
  91.         goto out;

  92.     pdev = platform_device_register_simple("light_sensor", -1, NULL, 0);
  93.     if (IS_ERR(pdev)) {
  94.         ret = PTR_ERR(pdev);
  95.         goto out_driver;
  96.     
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值