找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 1258|回复: 0
打印 上一主题 下一主题
收起左侧

ARM上LED控制程序

[复制链接]
跳转到指定楼层
楼主
ID:201115 发表于 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空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享淘帖 顶 踩
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表