guowenxue
2019-06-25 02294c1c430ab80bd4543af28f867c5c2927e5c1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
/*********************************************************************************
 *      Copyright:  (C) 2018 LingYun IoT System Studio
 *                  All rights reserved.
 *
 *       Filename:  led.c
 *    Description:  This file is used to control Passive buzzer or Active buzzer
 *
 *     pi@raspberrypi:~ $ gpio readall show BCM and wPi pinmap  
 *
 *                  VCC ---- 5V/3.3V
 *                  GND ---- GND
 *                  I/O ---- GPIO.18  ---- GPIO.1    
 *                           BCM           wPi 
 *                 
 *        Version:  1.0.0(2018/10/14)
 *         Author:  Guo Wenxue <guowenxue@gmail.com>
 *      ChangeLog:  1, Release initial version on "2018/10/14 12:13:26"
 *                 
 ********************************************************************************/
 
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <dirent.h>
#include <string.h>
#include <time.h>
#include <errno.h>
 
#include <wiringPi.h>
#include "beep.h"
 
/*+-----------------------------+
 *|  Turn buzzer on or off API  |
 *+-----------------------------+*/
 
int turn_passive_beep(int cmd, int freq)
{
    int range;
 
    if(OFF == cmd)
    {
        pwmWrite(PWM_PIN, 0);
        pinMode(PWM_PIN, INPUT) ;
    }
    else
    {
 
        if(freq<2000 || freq>5000)
        {
            printf("Beep set invalid PWM frequency[%d]!\n", freq);
            return -1;
        }
 
        /* Set GPIO as PWM output mode */
        pinMode(PWM_PIN, PWM_OUTPUT) ;
 
        /* Set PWM mode as ms mode */
        pwmSetMode(PWM_MODE_MS);
 
        /* Set PWM clock: 19.2MHz/32=600KHz */
        pwmSetClock(32);
 
        /* Set PWM frequency */
        range=600000/freq;
        pwmSetRange(range);
 
        /* Set PWM duty 50% */
        pwmWrite(PWM_PIN, range/2);
    }
}
 
 
/* Turn ON/OFF buzzer, Active Buzzer can not set frequency */
int turn_active_beep(int cmd)
    /* Active buzzer VCC connect to:
       5V: Both high and low level will be on 
       3.3V: Low level be on and High level be off 
       So we use INPUT or OUTPUT to control the Beeper
    */
    if(OFF == cmd)
    {
        pinMode(PWM_PIN, INPUT);
    }
    else
    {
        pinMode(PWM_PIN, OUTPUT);
        digitalWrite(PWM_PIN, LOW);
    }
 
    return 0;
}
 
 
/*+---------------------------------+
 *|  Play Tone OD,RE,MI....XI,DO1   |
 *+---------------------------------+*/
 
enum
{
    UNUSED=0,
    DO,
    RE,
    MI,
    FA,
    SO,
    LA,
    XI,
    DO1,
    RI1,
    TONE_MAX,
};
 
#define msleep(x) usleep( 1000*(x) )
 
static int tone_freq[TONE_MAX]={0, 2093, 2349, 2637, 2794, 3136, 3520, 3952, 4186, 4698 };
//static int tone_freq[TONE_MAX]={0, 2000, 2130, 2250, 2360, 2450, 2530, 2620, 2700, 2780};
 
static inline void play_tone(int tone, int delay)
{
    if(tone<DO || tone>RI1)
        return ;
 
           turn_passive_beep(ON, tone_freq[tone]);
    msleep(delay);
           turn_passive_beep(OFF, 0);
}
 
void play_tone_freq(void)
{
    int            i;
 
    for(i=DO; i<TONE_MAX; i++)
    {
        play_tone( i, 500 );
        msleep(500);
    }
}
 
 
/*+------------------------------+
 *|  Play song "Little Start"    |
 *+------------------------------+*/
 
typedef struct tone_s
{
    int     tone;
    int     delay_ms;   
 
} tone_t;
 
#define DEF_DELAY      350 
static tone_t little_start_notation[]=
{
    {DO, DEF_DELAY},
    {DO, DEF_DELAY},
    {SO, DEF_DELAY},
    {SO, DEF_DELAY},
    {LA, DEF_DELAY},
    {LA, DEF_DELAY},
    {SO, DEF_DELAY*2},
 
    {FA, DEF_DELAY},
    {FA, DEF_DELAY},
    {MI, DEF_DELAY},
    {MI, DEF_DELAY},
    {RE, DEF_DELAY},
    {RE, DEF_DELAY},
    {DO, DEF_DELAY*2},
 
 
    {SO, DEF_DELAY},
    {SO, DEF_DELAY},
    {FA, DEF_DELAY},
    {FA, DEF_DELAY},
    {MI, DEF_DELAY},
    {MI, DEF_DELAY},
    {RE, DEF_DELAY*2},
 
 
    {SO, DEF_DELAY},
    {SO, DEF_DELAY},
    {FA, DEF_DELAY},
    {FA, DEF_DELAY},
    {MI, DEF_DELAY},
    {MI, DEF_DELAY},
    {RE, DEF_DELAY*2},
};
 
#define ARRAY_SIZE(x) (sizeof(x)/sizeof(x[0]))
void play_little_star(void)
{
    int        i;
 
    for(i=0; i<ARRAY_SIZE(little_start_notation); i++)
    {
        play_tone(little_start_notation[i].tone, little_start_notation[i].delay_ms);
        msleep(30);
    }
}