#include <linux/unistd.h>
#include <termios.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <pthread.h>
#include <sys/ioctl.h>
#include <fcntl.h>
typedef struct stu
{
int a;
int b;
}stu_t;
#define PWM_IOCTL_SET_FREQ 1
#define PWM_IOCTL_STOP 0
int adc_start_flag = 0;
int led_flash_flag = 0;
int button_fd, led_fd, pwm_fd=-1;
void led_ioctl(int led, int io_level)
{
if(led_fd >0)
{//第二个参数代表亮或暗 第三个参数代表操作哪个灯。
printf("led=%d,io_level=%d\r\n",led,io_level);
ioctl(led_fd, io_level, led);
}
return;
}
int open_led()
{
led_fd = open("/dev/leds",O_RDWR);
if ( led_fd < 0) {
printf("open dev/mini2440_led fail=%d\n",led_fd);
return -1;
}
return 0;
}
void close_led()
{
close(led_fd);
led_fd = -1;
return;
}
int open_buttons()
{
button_fd = open("/dev/buttons",O_RDWR);
if ( button_fd < 0) {
printf("open dev/button_fd fail=%d\n",button_fd);
return -1;
}
return 0;
}
void close_buttons()
{
close(button_fd);
button_fd = -1;
return;
}
void *client_main(void *arg)
{
stu_t b = *(stu_t *)arg;
int led=0;
int led_last = 0;
int i;
int led_on = 1;
//for(i=0;i<10;i++)
open_led();
for(i=0;i<4;i++)
{
led_ioctl(i,0);
}
while(1)
{
printf("hello=%d,%d\r\n",b.a, b.b);
sleep(1);
//open_led();
led_ioctl(led_last,0);
led_ioctl(led,1);
led_last = led;
led = (led+1)%4;
//close_led();
}
close_led();
return NULL;
}
static void open_buzzer(void) //打开蜂鸣器
{
if(pwm_fd < 0)
{
pwm_fd = open("/dev/pwm",O_RDWR | O_NONBLOCK);
if (pwm_fd < 0) {
perror("open pwm_buzzer device"); //打开错误,则终止进程。退出参数为1
exit(1);
}
}
}
static void close_buzzer(void) //关闭蜂鸣器
{
if (pwm_fd >= 0) {
ioctl(pwm_fd, PWM_IOCTL_STOP,0); //停止蜂鸣器
close(pwm_fd); //关闭设备驱动文件
pwm_fd= -1;
}
}
static void set_buzzer_freq(int freq)
{
int ret = ioctl(pwm_fd, PWM_IOCTL_SET_FREQ, freq); //设置频率
}
static void stop_buzzer(void) //关闭蜂鸣器
{
int ret = ioctl(pwm_fd, PWM_IOCTL_STOP);
if(ret < 0) {
perror("stop the buzzer");
exit(1);
}
}
void delay1(double n)
{
long i=2000000;
long ii=1000000*n;
while(i>0)
{
while(ii>0)
{
ii--;
}
i--;
}
}
void delay(unsigned int t)
{
unsigned int t1;
unsigned long t2;
for(t1=0;t1<t*137;t1++)
{
for(t2=0;t2<58000;t2++);
}
}
#if 1
int freq[]={262,294,330,349,392,440,494, \
523,587,659,698,784,880,988, \
1046,1175,1318,1397,1568,1760,1976};
//则当使用某一音调时调用相应数组中的元素即可。
//将一首歌的简谱中的音调和节拍信息按照一个音符三个数字进行编码,
//其中第一位代表DO、RE、MI、FA、SO、LA、SI不同的音调,第二位代表低、中、高音,
//第三位代表半拍数。如1,1,2代表2个半拍的低音Do,
// 3,2,3代表3个半拍的中音Mi,7,3,4代表4个半拍的高音Si。
//则利用该编码方式则可将一首歌如《新年好》编码如下,
//歌曲以音符0,0,0代表结束。
//double music1[] = {1,2,1, 1,2,1,1,2,2,5,1,5,3,2,1,3,2,1,3,2,2,1,2,5,1,2,1,3,2,1,5,2,2,5,2,5,4,2,1,3,2,1,2,2,6,2,2,
// 1,3,2,1,4,2,2,4,2,5,3,2,1,2,2,1,3,2,2,1,2,5,1,2,1,3,2,1,2,2,2,5,1,5,7,1,1,2,2,1,1,2,6,0,0,0};//歌曲《新年好》
double music2[] = {1,2,2, 2,2,2,3,2,2,4,1,2,5,2,2,6,2,2,7,2,2, 1,3,2, 2,3,2,3,3,2,4,3,2,5,3,2,6,3,2,7,3,2, 0,0,0};
double music1[]={6,2,1, 7,2,1, 1,3,3, 7,2,1, 1,3,2, 3,3,2, 7,2,6,
3,2,1, 3,2,1, 6,2,3, 5,2,1, 6,2,2,1,3,2, 5,2,6,
3,2,2, 4,2,3, 3,2,1, 4,2,2, 1,3,2, 3,2,5,1,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5,2,8, 5,2,2,
int music_flag = 0;
int music_test(double *music)
{
double beats=0.5;//半拍的时间
int i=0;
int j=0;
open_buzzer(); //打开蜂鸣器s
while(music[i]>0 && music_flag == 1)//未到歌曲结尾时循环
{
j=music[i]+(music[i+1]-1)*7-1; //计算音符对应频率数组中的位置
set_buzzer_freq(freq[j]);//设置蜂鸣器工作频率为对应音符的频率
delay(music[i+2]); //播放相应的节拍数
stop_buzzer();//关闭蜂鸣器
delay(beats); //暂停一个半拍,准备下一个音符的播放
i=i+3;//下一个音符对应开始的位置
if(music[i] == 0)//循环播放
{
i = 0;
}
}
close_buzzer();
}
#endif
int led_flag =0;
void *led_main(void *arg)
{
open_led();
while(led_flag == 1)
//while(1)
{
led_ioctl(0,1);
sleep(1);
led_ioctl(0,0);
}
close_led();
return NULL;
}
void *beep_main(void *arg)
{
music_test(music1);
return NULL;
}
int main(int argc, char * argv[])
{
int ret;
int i;
char key[6];
pthread_t led_thread;
pthread_t beep_thread;
pthread_attr_t attr;
#if 0
pthread_t client_thread;
//int ret = -1;
stu_t c;
c.a = 1;
c.b = 3;
printf("main start\r\n");
music_test(music2);
return 0;
ret = pthread_create(&client_thread,NULL,client_main, &c);
if(ret)
{
printf("pthread_create client_connect_thread fail");
return -1;
}
while(1)
{
sleep(1);
}
printf("main end\r\n");
return 0;
#endif
pthread_attr_init( &attr );
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
open_buttons();
open_led();
while(1)
{
ret = read(button_fd, key, sizeof(key));
for(i=0; i<ret; i++)
{
printf("key[%d]=%c,%x",i, key[i],key[i]);
}
if(key[0] == '1')
{
led_flag = 1;
music_flag = 1;
ret = pthread_create(&led_thread,&attr,led_main, NULL);
ret = pthread_create(&beep_thread,&attr,beep_main, NULL);
}
else if(key[1] == '1')
{
led_flag = 0;
music_flag = 0;
//pthread_cancel(led_thread);
//led_ioctl(0,0);
}
printf("\r\n");
}
close_buttons();
close_led();
return 0;
}
评论0