buzzer  2.1.0.0
buzzer.h
Go to the documentation of this file.
1 /****************************************************************************
2 ** Copyright (C) 2020 MikroElektronika d.o.o.
3 ** Contact: https://www.mikroe.com/contact
4 **
5 ** Permission is hereby granted, free of charge, to any person obtaining a copy
6 ** of this software and associated documentation files (the "Software"), to deal
7 ** in the Software without restriction, including without limitation the rights
8 ** to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 ** copies of the Software, and to permit persons to whom the Software is
10 ** furnished to do so, subject to the following conditions:
11 ** The above copyright notice and this permission notice shall be
12 ** included in all copies or substantial portions of the Software.
13 **
14 ** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15 ** EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
16 ** OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
17 ** IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
18 ** DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
19 ** OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
20 ** USE OR OTHER DEALINGS IN THE SOFTWARE.
21 ****************************************************************************/
22 
28 #ifndef BUZZER_H
29 #define BUZZER_H
30 
31 #ifdef __cplusplus
32 extern "C"{
33 #endif
34 
39 #ifdef PREINIT_SUPPORTED
40 #include "preinit.h"
41 #endif
42 
43 #ifdef MikroCCoreVersion
44  #if MikroCCoreVersion >= 1
45  #include "delays.h"
46  #endif
47 #endif
48 
49 #include "drv_digital_out.h"
50 #include "drv_digital_in.h"
51 #include "drv_pwm.h"
52 
73 #define BUZZER_DEF_FREQ 6000
74 
79 #define BUZZER_NOTE_C2 65
80 #define BUZZER_NOTE_Db2 69
81 #define BUZZER_NOTE_D2 73
82 #define BUZZER_NOTE_Eb2 78
83 #define BUZZER_NOTE_E2 82
84 #define BUZZER_NOTE_F2 87
85 #define BUZZER_NOTE_Gb2 93
86 #define BUZZER_NOTE_G2 98
87 #define BUZZER_NOTE_Ab2 104
88 #define BUZZER_NOTE_A2 110
89 #define BUZZER_NOTE_Bb2 117
90 #define BUZZER_NOTE_B2 123
91 #define BUZZER_NOTE_C3 131
92 #define BUZZER_NOTE_Db3 139
93 #define BUZZER_NOTE_D3 147
94 #define BUZZER_NOTE_Eb3 156
95 #define BUZZER_NOTE_E3 165
96 #define BUZZER_NOTE_F3 175
97 #define BUZZER_NOTE_Gb3 185
98 #define BUZZER_NOTE_G3 196
99 #define BUZZER_NOTE_Ab3 208
100 #define BUZZER_NOTE_A3 220
101 #define BUZZER_NOTE_AS3 233
102 #define BUZZER_NOTE_B3 247
103 #define BUZZER_NOTE_C4 262
104 #define BUZZER_NOTE_Db4 277
105 #define BUZZER_NOTE_D4 294
106 #define BUZZER_NOTE_Eb4 311
107 #define BUZZER_NOTE_E4 330
108 #define BUZZER_NOTE_F4 349
109 #define BUZZER_NOTE_Gb4 370
110 #define BUZZER_NOTE_G4 392
111 #define BUZZER_NOTE_Ab4 415
112 #define BUZZER_NOTE_A4 440
113 #define BUZZER_NOTE_Bb4 466
114 #define BUZZER_NOTE_B4 494
115 #define BUZZER_NOTE_C5 523
116 #define BUZZER_NOTE_Db5 554
117 #define BUZZER_NOTE_D5 587
118 #define BUZZER_NOTE_Eb5 622
119 #define BUZZER_NOTE_E5 659
120 #define BUZZER_NOTE_F5 698
121 #define BUZZER_NOTE_Gb5 740
122 #define BUZZER_NOTE_G5 784
123 #define BUZZER_NOTE_Ab5 831
124 #define BUZZER_NOTE_A5 880
125 #define BUZZER_NOTE_Bb5 932
126 #define BUZZER_NOTE_B5 988
127 #define BUZZER_NOTE_C6 1047
128 #define BUZZER_NOTE_Db6 1109
129 #define BUZZER_NOTE_D6 1175
130 #define BUZZER_NOTE_Eb6 1245
131 #define BUZZER_NOTE_E6 1319
132 #define BUZZER_NOTE_F6 1397
133 #define BUZZER_NOTE_Gb6 1480
134 #define BUZZER_NOTE_G6 1568
135 #define BUZZER_NOTE_Ab6 1661
136 #define BUZZER_NOTE_A6 1760
137 #define BUZZER_NOTE_Bb6 1865
138 #define BUZZER_NOTE_B6 1976
139 #define BUZZER_NOTE_C7 2093
140 #define BUZZER_NOTE_Db7 2217
141 #define BUZZER_NOTE_D7 2349
142 #define BUZZER_NOTE_Eb7 2489
143 #define BUZZER_NOTE_E7 2637
144 #define BUZZER_NOTE_F7 2794
145 #define BUZZER_NOTE_Gb7 2960
146 #define BUZZER_NOTE_G7 3136
147 #define BUZZER_NOTE_Ab7 3322
148 #define BUZZER_NOTE_A7 3520
149 #define BUZZER_NOTE_Bb7 3729
150 #define BUZZER_NOTE_B7 3951
151 #define BUZZER_NOTE_C8 4186
152 #define BUZZER_NOTE_Db8 4435
153 #define BUZZER_NOTE_D8 4699
154 #define BUZZER_NOTE_Eb8 4978
155 
160 #define BUZZER_NOTE_LEN_WHOLE 1000
161 #define BUZZER_NOTE_LEN_HALF ( BUZZER_NOTE_LEN_WHOLE / 2 )
162 #define BUZZER_NOTE_LEN_QUARTER ( BUZZER_NOTE_LEN_WHOLE / 4 )
163 #define BUZZER_NOTE_LEN_EIGHTH ( BUZZER_NOTE_LEN_WHOLE / 8 )
164 #define BUZZER_NOTE_LEN_SIXTEENTH ( BUZZER_NOTE_LEN_WHOLE / 16 )
165 
170 #define BUZZER_VOLUME_MIN 1
171 #define BUZZER_VOLUME_MAX 100
172  // buzzer_cfg
174 
189 #define BUZZER_MAP_MIKROBUS( cfg, mikrobus ) \
190  cfg.pwm = MIKROBUS( mikrobus, MIKROBUS_PWM );
191  // buzzer_map // buzzer
194 
199 typedef struct
200 {
201  // Modules
202  pwm_t pwm;
204  // ctx variable
205  uint32_t pwm_freq;
207 } buzzer_t;
208 
213 typedef struct
214 {
215  // Communication gpio pins
216  pin_name_t pwm;
218  // Static variable
219  uint32_t dev_pwm_freq;
221 } buzzer_cfg_t;
222 
227 typedef enum
228 {
230  BUZZER_ERROR = -1
231 
233 
250 
264 err_t buzzer_init ( buzzer_t *ctx, buzzer_cfg_t *cfg );
265 
277 err_t buzzer_set_duty_cycle ( buzzer_t *ctx, float duty_cycle );
278 
289 err_t buzzer_pwm_stop ( buzzer_t *ctx );
290 
301 err_t buzzer_pwm_start ( buzzer_t *ctx );
302 
316 err_t buzzer_play_sound ( buzzer_t *ctx, uint16_t freq, uint8_t level, uint16_t duration );
317 
318 #ifdef __cplusplus
319 }
320 #endif
321 #endif // BUZZER_H
322  // buzzer
324 
325 // ------------------------------------------------------------------------ END
buzzer_cfg_setup
void buzzer_cfg_setup(buzzer_cfg_t *cfg)
Buzzer configuration object setup function.
buzzer_set_duty_cycle
err_t buzzer_set_duty_cycle(buzzer_t *ctx, float duty_cycle)
Buzzer sets PWM duty cycle.
buzzer_return_value_t
buzzer_return_value_t
Buzzer Click return value data.
Definition: buzzer.h:228
buzzer_play_sound
err_t buzzer_play_sound(buzzer_t *ctx, uint16_t freq, uint8_t level, uint16_t duration)
Buzzer play sound function.
BUZZER_OK
@ BUZZER_OK
Definition: buzzer.h:229
buzzer_t::pwm
pwm_t pwm
Definition: buzzer.h:202
buzzer_cfg_t::pwm
pin_name_t pwm
Definition: buzzer.h:216
buzzer_t
Buzzer Click context object.
Definition: buzzer.h:200
buzzer_init
err_t buzzer_init(buzzer_t *ctx, buzzer_cfg_t *cfg)
Buzzer initialization function.
buzzer_cfg_t
Buzzer Click configuration object.
Definition: buzzer.h:214
BUZZER_ERROR
@ BUZZER_ERROR
Definition: buzzer.h:230
buzzer_cfg_t::dev_pwm_freq
uint32_t dev_pwm_freq
Definition: buzzer.h:219
buzzer_pwm_start
err_t buzzer_pwm_start(buzzer_t *ctx)
Buzzer start PWM module.
buzzer_t::pwm_freq
uint32_t pwm_freq
Definition: buzzer.h:205
buzzer_pwm_stop
err_t buzzer_pwm_stop(buzzer_t *ctx)
Buzzer stop PWM module.