周立功EPC-28x-L平台蜂鸣器控制

readme.txt
本程序打开蜂鸣器,并创建一个线程,您会听到蜂鸣器的响声:嘀... 嘀... 嘀... 点击Enter或Space退出程序,。

///

beep_test.c

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <pthread.h>
#include <sys/ioctl.h>

#define tag_log(x, args...) fprintf(stdout, "dtu_beep_test: "x, ##args)
#define BEEP_FILE_NAME ("/sys/class/leds/beep/brightness")

struct beep_context {
    int fd;
    int if_thread_end;
    pthread_t thread_id;
};

void * beep_test_thread(void* pArgs)
{
    int ret = 0;
    struct beep_context *pContext = (struct beep_context *)pArgs;
    while(!pContext->if_thread_end){
        lseek(pContext->fd, 0, SEEK_SET);
        ret = write(pContext->fd, "1", 1);
        usleep(400*1000);

        ret = write(pContext->fd, "0", 1);
        usleep(400*1000);
    }
    ret = write(pContext->fd, "0", 1);
    return 0;
}

int beep_test(void)
{
    int ret = -1;
    char input= 'a';
    struct beep_context context;

    context.if_thread_end = 0;

    context.fd = open(BEEP_FILE_NAME, O_RDWR);
    if (context.fd < 0) {
        perror("open /dev/brightness");
        ret = -2;
        goto clean;
    }

    ret = pthread_create(&(context.thread_id), NULL, &beep_test_thread, &context);
    if(ret < 0){
        perror("create beep thread");
    }

    tag_log("Do you hear beep ?(RETURN / SPACE)\n");
    while(1){
        input = getchar();
        usleep(10*1000);
        if(input == '\n'){
            ret = 1;
            break;
        }else if(input == ' '){
            ret = -6;
            break;
        }else{
            continue;
        }
    }

clean:

    context.if_thread_end = 1;
    pthread_join(context.thread_id, 0);
    close(context.fd);

    return ret;
}

int main(int argc, char *argv[])
{
    beep_test();
    return 0;
}
///Makefile

CFLAGS += -Wall
obj := beep_test
src := beep_test.c
CC = arm-fsl-linux-gnueabi-gcc

LIBS=-lpthread

$(obj): $(src)
    $(CC) $(CFLAGS) $^ -o $@ -g $(LIBS)

.PHONY: clean
clean:
    -rm beep_test

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

大犇犇

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值