Chinaunix首页 | 论坛 | 博客
  • 博客访问: 536874
  • 博文数量: 67
  • 博客积分: 1625
  • 博客等级: 上尉
  • 技术积分: 1053
  • 用 户 组: 普通用户
  • 注册时间: 2012-02-04 14:40
文章分类

全部博文(67)

文章存档

2012年(67)

分类: LINUX

2012-07-25 21:22:07

写一自动背光程序,不敢独享,特写此文。

我所讲的自动背光就是在触摸屏一段时间(例:A秒)不进行触摸则自动关闭LCD背光,再次触摸时就点亮背光,这也是所有带LCD显示的电子设备最基本的功能。所以说,嵌入式设备更需要这一特性。

这个程序主要分两块1,当触摸时点亮背光2当触摸时,定时器开始计时A秒,A秒时间到时关闭背光。

两者的条件都是,发生触摸!然而根据友善之臂已经提供的/dev/adc驱动和adc-test应用程序可以看出只有发生触摸时“adc-test”输出为“-1”(如下图)。所以就可以借助这个“-1”的输出做为判断是否发生触摸。

条件有了,那么怎么执行呢?这就得看背光驱动源代码了--mini2440_backlight.c,纵观这个驱动程序我们得出结论:当写入“奇数时(我们取“1”)”会点亮背光,写入“偶数时(我们取“0”)”关闭背光,这样打开或关闭背光就可以实现了。

再一个就是这个定时器了,我用的是"alarm",它非常的适合!具体用法大家可以看《unix环境高级编程》这本书。

到这里,整个流程图就出来了:

唯一的遗憾是在关闭背光状态下,首次触摸时不能仅仅起到点亮背光而不发生触摸动作!

参考资料:《unix环境高级编程》《Linux设备驱动程序》



附上代码:

adc-test.c (友善之臂提供的adc程序点这里看详细注释)


  1. /***************************************************************************
  2.     zhushi : (C) by kangear ^_^
  3.     email : kangear@163.com

  4.  ***************************************************************************/
  5. /***************************************************************************
  6.  * *
  7.  * This program is free software; you can redistribute it and/or modify *
  8.  * it under the terms of the GNU General Public License as published by *
  9.  * the Free Software Foundation; either version 2 of the License, or *
  10.  * (at your option) any later version. *
  11.  * *
  12.  ***************************************************************************/
  13. #include <stdio.h> //sterr sscanf(存储的数据,格式控制字符串,选择性设定字符串)
  14. #include <unistd.h> //鸡肋
  15. #include <stdlib.h>    //鸡肋
  16. #include <sys/types.h>    //鸡肋
  17. #include <sys/stat.h>    //鸡肋
  18. #include <sys/ioctl.h>    //鸡肋
  19. #include <fcntl.h> //open();read();close;
  20. #include <linux/fs.h>    //鸡肋
  21. #include <errno.h>    //鸡肋
  22. #include <string.h>    //鸡肋    

  23. int main(void)
  24. {
  25.     fprintf(stderr, "press Ctrl-C to stop\n");        //相当于printf;
  26.     int fd = open("/dev/adc", 0); //文件编程中的 文件打开
  27.     if (fd < 0) //文件打开失败
  28.     {
  29.         perror("open ADC device:"); //错误信息打印出来
  30.         return 1;
  31.     }
  32.     for(;;) //呃……这个从功能上说是循环,但是有点不懂……!-_-!
  33.   {
  34.         char buffer[30];         //定义一数组
  35.         int len = read(fd, buffer, sizeof buffer -1);        //文件编程中的 文件读 成功返回长度
  36.         if (len > 0) {         //读成功
  37.             buffer[len] = '\0';             //在末尾添加“\0”结束符
  38.             int value = -1;
  39.             sscanf(buffer, "%d", &value);             //将得到的信息中提取整型数
  40.             printf("ADC Value: %d\n", value);             //打印
  41.         }
  42.         else         //读失败
  43.             {
  44.             perror("read ADC device:");             //输出错误信息
  45.             return 1;
  46.          }
  47.         usleep(500* 1000);         //和sleep()功能一样不过是μ秒(相当于睡眠0.5秒)
  48.      }
  49.     
  50.     close(fd); //文件编程中的 文件关闭
  51. }

mini2440_backlight.c (友善之臂提供的背光驱动点这里看详细注释 )


  1. #include <linux/errno.h>
  2. #include <linux/kernel.h>
  3. #include <linux/module.h>
  4. #include <linux/slab.h>
  5. #include <linux/input.h>
  6. #include <linux/init.h>
  7. #include <linux/serio.h>
  8. #include <linux/delay.h>
  9. #include <linux/clk.h>
  10. #include <linux/miscdevice.h>
  11. #include <linux/gpio.h>

  12. #include <asm/io.h>
  13. #include <asm/irq.h>
  14. #include <asm/uaccess.h>
  15. #include <mach/regs-clock.h>
  16. #include <plat/regs-timer.h>
  17.     
  18. #include <mach/regs-gpio.h>
  19. #include <linux/cdev.h>

  20. #undef DEBUG
  21. //#define DEBUG
  22. #ifdef DEBUG
  23. #define DPRINTK(x...) {printk(__FUNCTION__"(%d): ",__LINE__);printk(##x);}
  24. #else
  25. #define DPRINTK(x...) (void)(0)
  26. #endif

  27. #define DEVICE_NAME    "backlight"


  28. static unsigned int bl_state;

  29. static inline void set_bl(int state)
  30. {
  31.     bl_state = !!state;
  32.     s3c2410_gpio_setpin(S3C2410_GPG(4), bl_state);
  33. }

  34. static inline unsigned int get_bl(void)
  35. {
  36.     return bl_state;
  37. }

  38. static ssize_t dev_write(struct file *file, const char *buffer, size_t count, loff_t * ppos)
  39. {
  40.     unsigned char ch;
  41.     int ret;
  42.     if (count == 0) {
  43.         return count;
  44.     }
  45.     ret = copy_from_user(&ch, buffer, sizeof ch) ? -EFAULT : 0;
  46.     if (ret) {
  47.         return ret;
  48.     }

  49.     ch &= 0x01;
  50.     set_bl(ch);
  51.         
  52.     return count;
  53. }

  54. static ssize_t dev_read(struct file *filp, char *buffer, size_t count, loff_t *ppos)
  55. {
  56.     int ret;
  57.     unsigned char str[] = {'0', '1' };

  58.     if (count == 0) {
  59.         return 0;
  60.     }

  61.     ret = copy_to_user(buffer, str + get_bl(), sizeof(unsigned char) ) ? -EFAULT : 0;
  62.     if (ret) {
  63.         return ret;
  64.     }

  65.     return sizeof(unsigned char);
  66. }

  67. static struct file_operations dev_fops = {
  68.     owner:    THIS_MODULE,
  69.     read:    dev_read,    
  70.     write:    dev_write,
  71. };

  72. static struct miscdevice misc = {
  73.     .minor = MISC_DYNAMIC_MINOR,
  74.     .name = DEVICE_NAME,
  75.     .fops = &dev_fops,
  76. };

  77. static int __init dev_init(void)
  78. {
  79.     int ret;

  80.     ret = misc_register(&misc);

  81.     printk (DEVICE_NAME"\tinitialized\n");

  82.     s3c2410_gpio_cfgpin(S3C2410_GPG(4), S3C2410_GPIO_OUTPUT);
  83.     set_bl(1);
  84.     return ret;
  85. }


  86. static void __exit dev_exit(void)
  87. {
  88.     misc_deregister(&misc);
  89. }

  90. module_init(dev_init);
  91. module_exit(dev_exit);
  92. MODULE_LICENSE("GPL");
  93. MODULE_AUTHOR("FriendlyARM Inc.");

auto_backlight.c (我根据adc-test.c改写自动背光程序)


  1. /***************************************************************************
  2.     zhushi : (C) by kangear ^_^
  3.     email : kangear@163.com

  4.  ***************************************************************************/
  5. /***************************************************************************
  6.  * *
  7.  * This program is free software; you can redistribute it and/or modify *
  8.  * it under the terms of the GNU General Public License as published by *
  9.  * the Free Software Foundation; either version 2 of the License, or *
  10.  * (at your option) any later version. *
  11.  * *
  12.  ***************************************************************************/
  13. #include <stdio.h> //sterr sscanf(存储的数据,格式控制字符串,选择性设定字符串)
  14. #include <unistd.h>
  15. #include <stdlib.h>
  16. #include <sys/types.h>
  17. #include <sys/stat.h>
  18. #include <sys/ioctl.h>
  19. #include <fcntl.h>
  20. #include <linux/fs.h>
  21. #include <errno.h>
  22. #include <string.h>

  23. #include <signal.h>

  24. static void
  25. sig_alrm(int signo)
  26. {
  27.     char buffer2[1] = {'0'};
  28.     int bl_fd = open("/dev/backlight",1);
  29.     if(bl_fd<0){
  30.         perror("open backlight device:");
  31.     }
  32.     write(bl_fd,buffer2,1);
  33.     close(bl_fd);

  34. }
  35. void backlight_wr()
  36. {
  37.     char buffer2[1] = {'1'};
  38.     int bl_fd = open("/dev/backlight",1);
  39.     if(bl_fd<0){
  40.         perror("open backlight device:");
  41.     }
  42.     write(bl_fd,buffer2,1);
  43.     close(bl_fd);
  44. }

  45. int main(void)
  46. {
  47.     fprintf(stderr, "press Ctrl-C to stop\n");        //相当于printf;
  48.     int fd = open("/dev/adc", 2); //文件编程中的 文件打开
  49.     if (fd < 0) //文件打开失败
  50.     {
  51.         perror("open backlight device:"); //错误信息打印出来
  52.         return 1;
  53.     }
  54.     signal(SIGALRM,sig_alrm);
  55.     alarm(5);//定义一数组
  56.     for(;;) //呃……这个从功能上说是循环,但是有点不懂……!-_-!
  57.       {
  58.         char buffer[30];
  59.         int len = read(fd, buffer, sizeof buffer -1);        //文件编程中的 文件读 成功返回长度
  60.         if (len > 0) {         //读成功
  61.             buffer[len] = '\0';             //在末尾添加“\0”结束符
  62.             int value = -1;
  63.             sscanf(buffer, "%d", &value);             //将得到的信息中提取整型数
  64.             printf("backlight Value: %d\n", value);             //打印 printf("backlight Value: %d\n", value);             //打印
  65.             if(value==-1)
  66.             {
  67.                 backlight_wr();
  68.                 alarm(5);
  69.             }
  70.         }
  71.         else         //读失败
  72.             {
  73.             perror("read backlight device:");             //输出错误信息
  74.             return 1;
  75.           }
  76.         usleep(50* 1000);         //和sleep()功能一样不过是μ秒(相当于睡眠0.05秒)
  77.      }

  78.     close(fd); //文件编程中的 文件关闭
  79. }

  80. /*把 signal(SIGALRM,sig_alrm);
  81.             alarm(5);//定义一数组 放到循环之前解决了,首次不自动关背光的问题!
  82. 不过程序显得有点长,0.3解决这个问题*/

auto_backlight (编译好的auto_backlight,可以下载下来到mini2440中就可运行

 auto_backlight.rar   

阅读(4297) | 评论(0) | 转发(2) |
0

上一篇:QML移植到ARM

下一篇:Qml获取当前时间

给主人留下些什么吧!~~