找回密碼
 立即注冊

QQ登錄

只需一步,快速開始

搜索
查看: 1552|回復: 0
打印 上一主題 下一主題
收起左側

ARM上LED控制程序

[復制鏈接]
跳轉到指定樓層
樓主
ID:201115 發(fā)表于 2017-5-15 21:09 | 只看該作者 回帖獎勵 |倒序瀏覽 |閱讀模式
/********************************************************************
Header File Inclusion
*******************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#include <signal.h>
#include <unistd.h>
#include <errno.h>

/********************************************************************
Macros
SH_USR_LED_CNT  : shell commnad for counting user led devices
SH_USR_LED_DEV  : shell command for retrieving user led devices' name
********************************************************************/
#define SH_USR_LED_CNT "find /sys/class/leds/*/ | grep trigger |    \
                        xargs grep \'\\[default-on\\]\' | wc -l"
#define SH_USR_LED_DEV "find /sys/class/leds/*/ | grep trigger |    \
                        xargs grep \'\\[default-on\\]\' |           \
                        awk -F \'/\' '{printf $5\"\\n\" }\'"
#define LEN_NAME    48
#define LEN_CMD    128
#define MAX_COUNT   10

/* quit signal tag */
volatile bool quit = false;

/*=====================restore_devices==================*/
/* set all led devices as [default on] state            */
static int restore_devices(char names[][LEN_NAME], int count) {
    int i;
    char cmd[MAX_COUNT][LEN_CMD];

    for (i = 0; i < count; i++) {
        snprintf(cmd[i], LEN_CMD,
                "echo default-on > /sys/class/leds/%s/trigger", names[i]);
        if (system(cmd[i]) != 0) {
            fprintf(stderr, "fail to restore devices\n");
            return -1;
        }
    }

    return 0;
}

/*=====================turn_off==========================*/
/* turn off all led devices                              */
static int turn_off(char names[][LEN_NAME], int count) {
    int i;
    char cmd[MAX_COUNT][LEN_CMD];

    for (i = 0; i < count; i++) {
        snprintf(cmd[i], LEN_CMD,
                "echo 0 > /sys/class/leds/%s/brightness", names[i]);
        if (system(cmd[i]) != 0) {
            fprintf(stderr, "fail to turn off %s\n", names[i]);
            return -1;
        }
    }

    return 0;
}

/*=====================turn_on===========================*/
/* turn on all led devices                               */
static int turn_on(char names[][LEN_NAME], int count) {
    int i;
    char cmd[MAX_COUNT][LEN_CMD];

    for (i = 0; i < count; i++) {
        snprintf(cmd[i], LEN_CMD,
                "echo 1 > /sys/class/leds/%s/brightness", names[i]);
        if (system(cmd[i]) != 0) {
            fprintf(stderr, "fail to turn on %s\n", names[i]);
            return -1;
        }
    }

    return 0;
}

/*=====================show_devices======================*/
/* show all led devices                                  */
static void show_devices(char names[][LEN_NAME], int count) {
    int i;

    printf("twinkle %d leds\n", count);
    for (i = 0; i < count; i++)
        printf("%d. /sys/class/leds/%s\n", i + 1, names[i]);
}

/*=====================get_devices_names======================*/
/* retrieve user led devices' name                            */
static int get_devices_names(char names[][LEN_NAME], int count) {
    int i;
    FILE *f;

    /* the popen() function opens a process by creating
     * a pipe, forking, and invoking the shell */
    f = popen(SH_USR_LED_DEV, "r");
    if (! f)
        goto err;

    for (i = 0; i < count; i++)
        if (fscanf(f, "%s", names[i]) == EOF)
            goto err;

    pclose(f);
    return 0;

err:
    fprintf(stderr, "something error, maybe there's no led device\n");
    if (f)
        pclose(f);
    return -1;
}

/*=================get_devices_count==================*/
/* retrieve the amount of user led devices            */
static int get_devices_count(int *count) {
    FILE *f;

    /* the popen() function opens a process by creating
     * a pipe, forking, and invoking the shell */
    f = popen(SH_USR_LED_CNT, "r");
    if (! f)
        goto err;

    if(fscanf(f, "%d", count) == EOF)
        goto err;

    if (*count == 0) {
        printf("warning: there's no led device\n");
        return -2;
    }

    if (*count > MAX_COUNT) {
        *count = MAX_COUNT;
        printf("warning: max numbuer of leds is %d, dope tail\n", MAX_COUNT);
    }

    pclose(f);
    return 0;

err:
    fprintf(stderr, "something error, maybe there's no led device\n");
    if (f)
        pclose(f);
    return -1;
}

/*=====================sig_handle======================*/
/* response to ctrl+c                                  */
static void sig_handle(int signo) {
    quit = true;
}

/*==============================main===============================*/
/* this program twinkle the leds which set as [defualt on] during  */
/* the time of kernel booting. you can check it by bellow command: */
/* $ cat /sys/class/leds/${device_name}/trigger                    */
int main(int argc, char **argv) {
    int i;
    int count;
    char names[MAX_COUNT][LEN_NAME];

    /* ctrl+c handler */
    signal(SIGINT, sig_handle);

    if (get_devices_count(&count) < 0)
        return -1;

    if (get_devices_names(names, count) < 0)
        return -1;

    show_devices(names, count);

    /* twinkle
     * 1. turn on all leds
     * 2. sleep 0.5 s
     * 3. tunn off all leds
     * 4. goto 1
     * */
    for (i = 0; i < 10; i++) {
        /* set as true while ctrl+c */
        if (quit)  
            break;

        if (turn_on(names, count) < 0)
            break;

        usleep(500 * 1000);

        if (turn_off(names, count) < 0)
            break;
    }

    restore_devices(names, count);

    return 0;
}


分享到:  QQ好友和群QQ好友和群 QQ空間QQ空間 騰訊微博騰訊微博 騰訊朋友騰訊朋友
收藏收藏 分享淘帖 頂 踩
回復

使用道具 舉報

您需要登錄后才可以回帖 登錄 | 立即注冊

本版積分規(guī)則

手機版|小黑屋|51黑電子論壇 |51黑電子論壇6群 QQ 管理員QQ:125739409;技術交流QQ群281945664

Powered by 單片機教程網(wǎng)

快速回復 返回頂部 返回列表