更多详情请看:
Mini2440之LCD自动背光程序设计/***************************************************************************
(C) by kangear ^_^
email :
kangear@163.com ***************************************************************************/
/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
#include <stdio.h> //sterr sscanf(存储的数据,格式控制字符串,选择性设定字符串)
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
#include <fcntl.h>
#include <linux/fs.h>
#include <errno.h>
#include <string.h>
#include <signal.h>
static void
sig_alrm(int signo)
{
char buffer2[1] = {'0'};
int bl_fd = open("/dev/backlight",1);
if(bl_fd<0){
perror("open backlight device:");
}
write(bl_fd,buffer2,1);
close(bl_fd);
}
void backlight_wr()
{
char buffer2[1] = {'1'};
int bl_fd = open("/dev/backlight",1);
if(bl_fd<0){
perror("open backlight device:");
}
write(bl_fd,buffer2,1);
close(bl_fd);
}
int main(void)
{
fprintf(stderr, "press Ctrl-C to stop\n"); //相当于printf;
int fd = open("/dev/adc", 2); //文件编程中的 文件打开
if (fd < 0) //文件打开失败
{
perror("open backlight device:"); //错误信息打印出来
return 1;
}
signal(SIGALRM,sig_alrm);
alarm(5);//定义一数组
for(;;)
{
char buffer[30];
int len = read(fd, buffer, sizeof buffer -1); //文件编程中的 文件读 成功返回长度
if (len > 0) { //读成功
buffer[len] = '\0'; //在末尾添加“\0”结束符
int value = -1;
sscanf(buffer, "%d", &value); //将得到的信息中提取整型数
printf("backlight Value: %d\n", value); //打印 printf("backlight Value: %d\n", value); //打印
if(value==-1)
{
backlight_wr();
alarm(5);
}
}
else //读失败
{
perror("read backlight device:"); //输出错误信息
return 1;
}
usleep(50* 1000); //和sleep()功能一样不过是μ秒(相当于睡眠0.05秒)
}
close(fd); //文件编程中的 文件关闭
}
/*把 signal(SIGALRM,sig_alrm);
alarm(5);//定义一数组 放到循环之前解决了,首次不自动关背光的问题!
不过程序显得有点长,0.3解决这个问题*/