I.MX6 Android backlight modify by C demo

/**************************************************************************
 *             I.MX6 Android backlight modify by C demo
 * 说明:
 *     因为一些特殊情况,需要添加一个这个简单的控制程序来控制android背光
 * 亮度,个人感觉是没有必要的,但是应要求还是加上。
 *
 *                                      2016-5-14 深圳 南山平山村 曾剑锋
 *************************************************************************/

#include <stdio.h>  
#include <stdlib.h>  
#include <unistd.h>  
#include <sys/types.h>  
#include <sys/stat.h>  
#include <fcntl.h>      
#include <termios.h>    
#include <string.h>
#include <errno.h>  

#define BACKLIGHT0_PATH "/sys/class/backlight/pwm-backlight.0/brightness"
#define BACKLIGHT1_PATH "/sys/class/backlight/pwm-backlight.1/brightness"

//#define BACKLIGHT0_PATH "brightness0"
//#define BACKLIGHT1_PATH "brightness1"

int help ( int argc );
int isDigitalStr(char *str);
int file_exists(char *filename);
void writeStringToFile(char *filePath, char *string);

int main(int argc, char **argv)  
{  
    int bl0 = 0;
    int bl1 = 0;
    
    if ( help( argc ) != 0)
        return -1;

    if ( !isDigitalStr(argv[1]) ) {
        printf("Please give a numeric string.\n");
        return -1;
    }   

    writeStringToFile(BACKLIGHT0_PATH, argv[1]);
    writeStringToFile(BACKLIGHT1_PATH, argv[1]);
}

void writeStringToFile(char *filePath, char *string) 
{
    int fd = 0;

    if ( file_exists(filePath) ) {

        fd = open(filePath, O_RDWR);

        ftruncate(fd, 0);
        write(fd, string, strlen(string));

        close(fd);

    }
}

int isDigitalStr(char *str)
{   
    int len = strlen(str);
    char *s = str;
    int i = 0;
 
    while( '0' <= *s && *s <= '9' && i < len){
        s++;
        i++;
    }

    if(i == len)
        return 1;
    else 
        return 0;       
}

int file_exists(char *filename) 
{ 
    if (access(filename, F_OK) == 0) {
        return 1;
    } else {
        printf("%s is not exist.\n", filename);
        return 0;
    }
}

int help( int argc ) 
{
    if ( argc != 2 ) {
        printf ( "USAGE:\n" );
        printf ( "    backlight <value>\n" );
        printf ( "    example:\n" );
        printf ( "        backlight 0\n" );
        return -1;
    }

    return 0;
}

 

posted on 2016-05-14 10:03  zengjf  阅读(279)  评论(0编辑  收藏  举报

导航