基于Dragonboard 410c的声控灯(二)

来源:互联网 发布:zcash 挖矿 linux 编辑:程序博客网 时间:2024/04/27 17:42

        之前在http://blog.csdn.net/weixin_40109283/article/details/78861380博客中已经对基于Dragonboard 410c的声控灯的硬件设计方案进行了介绍,现在给大家介绍一下实现该功能的软件编程设计.

        通过GPIO口来实现声控灯的基本原理:

1) 导入所使用到的GPIO的节点,导入之后才能对GPIO口进行读写;

2) 对GPIO节点进行读写数据进而实现对声音传感器和LED的操作;

3) 完成GPIO节点的读写操作之后,须导出GPIO节点.


 在Linux环境下建立sound_ctrl_led.c文件

具体代码实现如下:

#include <stdio.h>

#include <string.h>

#include <stdlib.h>

#include <fcntl.h>

#include <unistd.h>

 

#define GPIO_SOUND 13

#define GPIO_LED 69

 

#define NUM 100

 

int export_gpio(int gpio)

{

    int fd;

    

    char buf[NUM];

    sprintf(buf, "%d", gpio);

    

    fd = open("/sys/class/gpio/export", O_WRONLY);

    if (fd < 0) {

        printf("Open export file failed!\n");

        return -1;

    } else if (write(fd, buf, strlen(buf)) < 0) {

        printf("Write export file failed!\n");

        return -1;

    }

    

    close(fd);

    

    return 0;

}

 

void unexport_gpio(int gpio)

{

    int fd;

    

    char buf[NUM];

    sprintf(buf, "%d", gpio);

    

    fd = open("/sys/class/gpio/unexport", O_WRONLY);

    if (fd < 0) {

        printf("Open unexport file failed!\n");

    }

    

    write(fd, buf, strlen(buf));

    

    close(fd);

}

 

int get_gpio_value(int gpio)

{

    int fd;

    int value;

    char val;

    

    char buf[NUM];

    sprintf(buf, "/sys/class/gpio/gpio%d/value", gpio);

 

    fd = open(buf, O_RDONLY);

    if (fd < 0) {

        printf("Open value file failed!\n");

        return -1;

    }

 

    read(fd, &val, 1);

    

    value = atoi(&val);

    

    return value;

}

 

int set_gpio_value(int gpio, int value)

{

    int fd;

    char val[1];

 

    char buf[NUM];

    sprintf(buf, "/sys/clas/gpio/gpio%d/direction", gpio);

 

    fd = open(buf, O_WRONLY);

    if (fd < 0) {

        printf("Open dirction file faided!\n");

        return -1;

    }

 

    write(fd, "out", 3);

 

    close(fd);

 

    sprintf(buf, "/sys/class/gpio/gpio%d/value", gpio);

    fd = open(buf, O_WRONLY);

    if (fd) {

        printf("Open value file failed!\n");

        return -1;

    }

    

    sprintf(val, "%d", value);

    write(fd, val, 1);

 

    close(fd);

 

    return 0;

}  

 

int main(void)

{

    int sound_value;

    int led_value;

 

    if (!export_gpio(GPIO_SOUND)) {

        printf("Export GPIO_%d failed!\n", GPIO_SOUND);

        return -1;

    }

 

    if (!export_gpio(GPIO_LED)) {

        printf("Export GPIO_%d failed!\n", GPIO_LED);

        goto err1;

        return -1;

    }

 

    while (1) {

        sound_value = get_gpio_value(GPIO_SOUND);

        if (!sound_value) {

            set_gpio_value(GPIO_LED, 1);

            printf("Open the LED\n");

            sleep(1);

        } else {

            set_gpio_value(GPIO_LED, 0);

            printf("Close the LED\n");

        }

    }

    

err1:

    unexport_gpio(GPIO_SOUND);

    return -1;

 

    unexport_gpio(GPIO_SOUND);

    unexport_gpio(GPIO_LED);

 

    return 0;

 

有了以上的代码之后,Linux环境下通过arm-linux-gcc交叉编译工具来编译代码,生成可执行文件.命令如下:

$ arm-linux-gcc sound_ctrl_led.c -o sound_ctrl_led

 

sound_ctrl_led文件下载到410c开发板,修改权限并执行,命令如下:

# chmod 777 sound_ctrl_led

# ./sound_ctrl_led

 

这样就可以实现声控灯的功能了.