今天,我們就站在巨人的肩膀上,利用內(nèi)核開發(fā)者已經(jīng)寫好的驅(qū)動(dòng)來實(shí)現(xiàn)我們想要的功能。
應(yīng)用層控制
除了可以在shell中通過echo
、cat
的方式控制Led,我們也可以在寫一個(gè)應(yīng)用層程序來操作/sys/class/leds/
下的節(jié)點(diǎn),應(yīng)用層代碼:
#include < stdio.h >
#include < stdlib.h >
#include < unistd.h >
#include < errno.h >
#include < fcntl.h >
#include < string.h >
#include < sys/stat.h >
#include < sys/types.h >
#define LED_DEV_PATH "/sys/class/leds/led%d/brightness"
#define ON
#define OFF
int fs4412_set_led(unsigned int lednum, unsigned int mode)
{
int fd;
int ret;
char devpath[128];
char *on = "1n";
char *off = "0n";
char *m = NULL;
snprintf(devpath, sizeof(devpath), LED_DEV_PATH, lednum);
fd = open(devpath, O_WRONLY);
if (fd == -1) {
perror("fsled- >open");
return -1;
}
if (mode == ON)
m = on;
else
m = off;
ret = write(fd, m, strlen(m));
if (ret == -1) {
perror("fsled- >wrtie");
close(fd);
return -1;
}
close(fd);
return 0;
}
int main(int argc, char *argv[])
{
unsigned int lednum = 2;
while(1){
fs4412_set_led(lednum, on);
usleep(500000);
fs4412_set_led(lednum, OFF);
usleep(500000);
lednum++;
if (lednum > 5)
lednum = 2;
}
return 0;
}
上述應(yīng)用層代碼執(zhí)行后,led2會(huì)閃爍。
-
led
+關(guān)注
關(guān)注
242文章
23355瀏覽量
663187 -
驅(qū)動(dòng)
+關(guān)注
關(guān)注
12文章
1851瀏覽量
85638 -
Linux
+關(guān)注
關(guān)注
87文章
11345瀏覽量
210391 -
應(yīng)用層
+關(guān)注
關(guān)注
0文章
46瀏覽量
11544
發(fā)布評(píng)論請(qǐng)先 登錄
相關(guān)推薦
評(píng)論