正点原子Linux C 应用编程 点亮LED学习代码(基于IMX6ULL开发板),教程使用的是系统调用,我使用的是标准I/O
#include "stdio.h" //标准I/O库和printf函数需使用
#include "string.h" //strcmp函数需使用
char tigger_file_addr[] = "/sys/class/leds/sys-led/trigger";
char brightness_file_addr[] = "/sys/class/leds/sys-led/brightness";
FILE *file1 = NULL;
FILE *file2 = NULL;
int ret = 0;
/*
*需要给main函数传参时的写法
*argc:参数数量
*argv[]:参数储存数组
*/
int main(int argc, char *argv[])
{
__uint8_t i = 0;
printf(" 传入参数个数=%d\n",argc);
for(i=0; i<argc; i++)
{
printf(" 参数%d的内容为:%s\n",i,argv[i]);
}
/*以可读可写方式打开文件 trigger */
file1 = fopen(tigger_file_addr, "r+");
if (file1 == NULL)//文件打开失败
{
perror(" fopen tigger_file");
return 1;
}
printf(" tigger_file open success\n");
/*以可读可写方式打开文件 brightness */
file2 = fopen(brightness_file_addr, "r+");
if (file2 == NULL)//文件打开失败
{
perror(" fopen brightness_file");
return 1;
}
printf(" brightness_file open success\n");
if (argc == 2)
{
if( (strcmp("on",argv[1]) == 0) || (strcmp("off",argv[1]) == 0))//如果传入参数为on/off
{
printf(" 执行%s操作\n",argv[1]);
ret = fwrite("none", 1, 4, file1);//将触发方式改为none
if(ret != 4)
{
if(ferror(file1) != 0)
printf(" write none to tigger_file error!\n");
return 1;
}
else
printf(" write none to tigger_file success!\n");
fclose(file1);//关闭trigger文件
if( strcmp("on",argv[1]) == 0)//如果传入参数为on
{
ret = fwrite("1", 1, 1 ,file2);//要写入的字符串、每个数据项的大小,数据项的数量
if(ret != 1)
{
if(ferror(file2) != 0)
printf(" write 1 to brightness_file error!\n");
return 1;
}
else
printf(" write 1 to brightness_file success!\n");
}
else
{
ret = fwrite("0", 1, 1 ,file2);//要写入的字符串、每个数据项的大小,数据项的数量
if(ret != 1)
{
if(ferror(file2) != 0)
printf(" write 0 to brightness_file error!\n");
return 1;
}
else
printf(" write 0 to brightness_file success!\n");
}
fclose(file2);//关闭brightness文件
}
else if( strcmp("heartbeat",argv[1]) == 0)//如果传入参数为on
{
printf(" 执行%s操作\n",argv[1]);
ret = fwrite("heartbeat", 1, 9, file1);//将触发方式改为none
if(ret != 9)
{
if(ferror(file1) != 0)
printf(" write heartbeat to brightness_file error!\n");
return 1;
}
else
printf(" write heartbeat to brightness_file success!\n");
fclose(file1);//关闭trigger文件
fclose(file2);//关闭brightness文件
}
}
return 0;
}