-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuzzer.h
237 lines (204 loc) · 5.71 KB
/
buzzer.h
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
/**
* @file buzzer.h
* @author Pablo Jean Rozario ([email protected])
* @brief This library is to handle Buzzer devices, without
* chipset depedency, can be used in any microcontroller
* @version 1.0
* @date 2022-12-01
*
* @copyright Copyright (c) 2022
*
*/
#ifndef APPLICATION_BUZZER_H_
#define APPLICATION_BUZZER_H_
#include <stdint.h>
#include <stddef.h>
#include <string.h>
#include "notes.h"
#include "ringtones.h"
/*
* Enumerates
*/
/**
* @brief Defines the buzzer type
*
* BUZZER_TYPE_PASSIVE are devices that need PWM to work.
* Can reproduce any frequency
* BUZZER_TYPE_ACTIVE work only with a simple GPIO
* has a fixed frequency
*/
typedef enum{
BUZZER_TYPE_PASSIVE,
BUZZER_TYPE_ACTIVE
}buzzer_type_e;
/**
* @brief when call buzzer_play_loop, you can play only for a fixed
* time, or play intermittently
* BUZZER_LOOP_OFF will only play for the provided time
* BUZZER_LOOP_ON will turnon and turnoff with privided time
*/
typedef enum{
BUZZER_LOOP_OFF,
BUZZER_LOOP_ON
}buzzer_loop_e;
/**
* @brief indicate if a logic is handle the buzzer. Not necesary
* indicate if buzzer is vibrating.
*
* BUZZER_IS_NOT_ACTIVE: no routine is applied on buzzer
* BUZZER_IS_ACTIVE buzzer is working with a logic
*/
typedef enum{
BUZZER_IS_NOT_ACTIVE,
BUZZER_IS_ACTIVE
}buzzer_active_e;
/**
* BUZZER_ERR_OK : Eveything is OK
* BUZZER_ERR_FAIL : failed to initialize the buzzer
* BUZZER_ERR_PARAMS : some parameter is missing or incorrect
*
*/
typedef enum{
BUZZER_ERR_OK,
BUZZER_ERR_FAIL,
BUZZER_ERR_PARAMS,
BUZZER_ERR_UNKNOWN = 0xFF
}buzzer_err_e;
/*
* Functions typedefs
*/
/**
* @brief Function pointers. For Pwm or GPIO
*
*/
typedef void (*pwmOutFx)(uint32_t freq);
typedef void (*gpioOutFx)(uint32_t val);
/*
* Structs and Unions
*/
typedef struct{
// user must define these parameters
struct{
/**
* @brief Function to handle with PWM frequency
* 0 value must turnoff the PWM, don't forget this
* case
* USE THIS FOR PASSIVE DEVICES
*/
pwmOutFx pwmOut;
/**
* @brief Function to handle with GPIO that will
* handle the Buzzer.
* USE THIS FOR ACTIVE DEVICES
*/
gpioOutFx gpioOut;
// Jut pick only one function, the other must be NULL
}fnx;
// the interrupt period that you will call buzzer_proc()
// necessary for buzzer_play_loop() and buzzer_play_loop_array()
uint_fast16_t interruptMs;
// internal library variables, no need to work with these
uint8_t started;
buzzer_type_e type;
buzzer_active_e active;
uint_fast16_t counting;
struct{
uint16_t *pTimes;
uint16_t *pFreq;
uint_fast16_t i;
uint_fast16_t len;
int_fast32_t time;
uint_fast16_t freq;
buzzer_loop_e loop;
}play_param;
}buzzer_t;
/*
* Functions Prototypes
*/
/**
* @brief Initialize the buzzer
*
* @param buzzer : pointer to the handle of the buzzer
* @return buzzer_err_e
*/
buzzer_err_e buzzer_init(buzzer_t *buzzer);
/**
* @brief Turnoff the buzzer imediatly
*
* @param buzzer : pointer to the handle of the buzzer
*/
void buzzer_stop(buzzer_t *buzzer);
/**
* @brief Turnon buzzer immediately
*
* @param buzzer : pointer to the handle of the buzzer
* @param freq : frequency that will inserted on buzzer.
*
* @note freq parameters is relevant only in passive devices
*/
void buzzer_turn_on(buzzer_t *buzzer, uint16_t freq);
/**
* Start buzzer for a determined period
* If loop is BUZZER_LOOP_ON the buzzer will turn on and turn off
* with the period
* buzzer_proc must be working
*/
/**
* @brief Start buzzer for a determined period
* If loop is BUZZER_LOOP_ON the buzzer will turn on and turn off
* with the period
* buzzer_proc must be working
*
* @param buzzer : pointer to the handle of the buzzer
* @param freq : frequency of the device
* @param period : period that buzzer will stay on, or period
* of beeps
* @param loop : BUZZER_LOOP_ON buzzer will turned on and off with
* provided period
* BUZZER_LOOP_OFF will keep device vibrating only for period prived.
*
* @note : freq is relevant only for Passive devices
*/
void buzzer_play_loop(buzzer_t *buzzer, uint16_t freq, uint16_t period, buzzer_loop_e loop);
void buzzer_play_times(buzzer_t *buzzer, uint16_t freq, uint16_t period, uint8_t count);
/**
* Start to play a array of period and frequencies, generally used to play ringtones
* buzzer_proc must be working
*/
/**
* @brief : Start to play a array of period and frequencies, generally used to play ringtones
* buzzer_proc must be working
*
* @param buzzer : pointer to the handle of the buzzer
* @param pPeriod : array of period
* @param pFreq : array of frequency values, must have the same len of pPeriod
* @param len : number of values on pFreq and/or pPeriod
*
* @note pFreq is relevant only for Passive devices
*/
void buzzer_play_loop_array(buzzer_t *buzzer, uint16_t *pPeriod, uint16_t *pFreq, uint16_t len);
/**
* Return if Buzzer is active
*/
/**
* @brief Return if Buzzer is active
*
* @param buzzer : pointer to the handle of the buzzer
* @return buzzer_active_e
*/
buzzer_active_e buzzer_is_active(buzzer_t *buzzer);
/**
* @brief call this function in a periodic timing, if you're using a RTOS
* we suggest you to use a task to handle this with a sleep
* the period of interrupt is configured on buzzer_t structure
*
* @param buzzer : pointer to the handle of the buzzer
*/
void buzzer_proc(buzzer_t *buzzer);
/**
* @brief callback when an array has your execution finished
*
* @param buzzer : pointer to the handle of the buzzer
*/
void buzzer_end_callback(buzzer_t *buzzer);
#endif /* APPLICATION_BUZZER_H_ */