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