5718车载显示控制器外接一个i2c环境光传感器,用来采集外部环境的光亮,从而自动调节控制器的LCD背光
MAX44009环境光传感器提供I²C数字输出
数据手册: https://www.maximintegrated.com/cn/products/interface/sensor-interface/MAX44009.html
MAX44009.pdf
源码下载链接:
https://mirrors.edge.kernel.org/pub/software/utils/i2c-tools/ 使用的是3.0.2的版本
https://www.kernel.org/pub/software/utils/i2c-tools/
修改对应Makefile
PREFIX = /home/siasun/OK5718-SDK-V1.0/OK57xx-linux-fs/usr
CC = arm-linux-gnueabihf-gcc
AR = arm-linux-gnueabihf-ar
STRIP = arm-linux-gnueabihf-strip
i2ctransfer工具
源码:
git clone https://git.kernel.org/pub/scm/utils/i2c-tools/i2c-tools.git
----没弄懂i2ctransfer工具数据怎么写的
疑问: 寄存器地址是怎么确定的、寄存器数据连续读多少个字节又是怎么确定的
i2c_backlight.c
#include
#include
#include
#include
#include
#include
//double类型打印0表示位数,默认6位
//printf(" %.0lf \n",Lux);
/* 背光调节显示
Lux范围 亮度档位
0 - 1300 echo 2 > /sys/class/backlight/backlight/brightness
1301 - 8500 echo 4 > /sys/class/backlight/backlight/brightness
8501 - 4177920 echo 7 > /sys/class/backlight/backlight/brightness
其他值 echo 3 > /sys/class/backlight/backlight/brightness
*/
int main(){
int i = 0, j = 0;
int data[5] = { 0, 0, 0, 0, 0};
char buff_03[10];
char buff_04[5];
char buff_mid[2];
FILE * fp;
while(1){
//加判断是否有I2C设备,如果没有,则停止程序
fp = popen("i2cget -f -y 0 0x4a 0x03", "r");
if (!fp)
{
pclose(fp);
break;
return -1;
}
fgets(buff_03,sizeof(buff_03),fp);
//printf("%s\n",buff_03);
pclose(fp);
fp = popen("i2cget -f -y 0 0x4a 0x04", "r");
if (!fp)
{
pclose(fp);
break;
return -1;
}
fgets(buff_04,sizeof(buff_04),fp);
//printf("%s\n",buff_04);
pclose(fp);
buff_03[4] = buff_04[3];
//printf("===== %s =====\n",buff_03);
//printf("===== %s =====\n\n",buff_04);
fprintf(stderr, "===== %s =====\n",buff_03);
fprintf(stderr, "===== %s =====\n\n",buff_04);
//0x03、0x04地址数据处理---fgets读取上来是一个字符串
for(i=2;i<=4;i++)
{
if(buff_03[i] >= '0' && buff_03[i] <= '9'){
buff_mid[0] = buff_03[i];
data[i] = atoi (buff_mid);
//printf("\n data[%d] = %d \n",i,data[i]);
}else if( (buff_03[i] == 'a' ) || (buff_03[i] == 'A') ){
data[i] = 10;
//printf("\n data[%d] = %d \n",i,data[i]);
}else if( (buff_03[i] == 'b' ) || (buff_03[i] == 'B') ){
data[i] = 11;
//printf("\n data[%d] = %d \n",i,data[i]);
}else if( (buff_03[i] == 'c' ) || (buff_03[i] == 'C') ){
data[i] = 12;
//printf("\n data[%d] = %d \n",i,data[i]);
}else if( (buff_03[i] == 'd' ) || (buff_03[i] == 'D') ){
data[i] = 13;
//printf("\n data[%d] = %d \n",i,data[i]);
}else if( (buff_03[i] == 'e' ) || (buff_03[i] == 'E') ){
data[i] = 14;
//printf("\n data[%d] = %d \n",i,data[i]);
}else if( (buff_03[i] == 'f' ) || (buff_03[i] == 'F') ){
data[i] = 15;
//printf("\n data[%d] = %d \n",i,data[i]);
}else{
data[i] = 5;
//printf("\n data[%d] = %d \n",i,data[i]);
}
}
int mantissa = 0;
int Lux = 1;
mantissa = data[3] * 16 + data[4];
//printf("mantissa = %d \n",mantissa);
if(data[2] == 0){
Lux = mantissa;
//printf("Lux = %d \n",Lux);
}else{
for(j=1;j<=data[2];j++){
Lux = 2 * Lux;
}
Lux = Lux * mantissa;
//printf("Lux = %d \n",Lux);
}
if(Lux >= 0 && Lux <= 200){
system("echo 2 > /sys/class/backlight/backlight/brightness");
printf("backlight-brightness = 2 \n");
}else if(Lux >= 201 && Lux <= 700){
system("echo 4 > /sys/class/backlight/backlight/brightness");
printf("backlight-brightness = 4 \n");
}else if(Lux >= 701 && Lux <= 11200){
system("echo 7 > /sys/class/backlight/backlight/brightness");
printf("backlight-brightness = 7 \n");
}else{
system("echo 3 > /sys/class/backlight/backlight/brightness");
printf("backlight-brightness = 3 \n");
}
sleep(1);
}
return 0;
}
背光突变考虑:
采三次取平均值 {加权平均值}
PID调节
线性调节
i2c_tools工具使用参考链接:
http://wiki.100ask.org/index.php?title=I2C_i2c-tools&action=edit
https://www.cnblogs.com/raina/p/12068485.html