software:firmware
MakAir Firmware
buzzer.cpp
Go to the documentation of this file.
1 
8 #pragma once
9 
10 // INCLUDES ===================================================================
11 
12 // Externals
13 #include "Arduino.h"
14 
16 
17 #include "../includes/buzzer.h"
18 #include "../includes/buzzer_control.h"
19 #include "../includes/parameters.h"
20 
21 // PROGRAM =====================================================================
22 
33 #define TIMER_TICK_PER_MS 10
34 #define BIP (100 * TIMER_TICK_PER_MS)
35 #define BIP_PAUSE BIP
36 #define BEEEEP (250 * TIMER_TICK_PER_MS)
37 #define BEEEEP_PAUSE BEEEEP
38 #define PAUSE_120S (120 * 1000 * TIMER_TICK_PER_MS)
39 #define PAUSE_20S (20 * 1000 * TIMER_TICK_PER_MS)
40 #define PAUSE_10S (10 * 1000 * TIMER_TICK_PER_MS)
41 #define PAUSE_1S (1 * 1000 * TIMER_TICK_PER_MS)
43 
44 #define BZ_OFF (0u)
45 #define BZ_ON (1u)
46 
48 #define BUZZER_HIGH_PRIO_SIZE 40
49 
57 
59 #define BUZZER_MEDIUM_PRIO_SIZE 8
60 
65 
67 #define BUZZER_LOW_PRIO_SIZE 8
68 
73 
75 #define BUZZER_BOOT_SIZE 8
76 
81 
82 // INITIALISATION =============================================================
83 
84 const uint32_t* Active_Buzzer = nullptr;
85 uint32_t Active_Buzzer_Index = 0;
86 uint32_t Active_Buzzer_Size = 2;
87 bool Active_Buzzer_Repeat = false;
89 bool Buzzer_Muted = false;
90 
91 HardwareTimer* BuzzerTim;
93 
94 // FUNCTIONS ==================================================================
95 
100 #if (STM32_CORE_VERSION < 0x01090000)
101 // cppcheck-suppress misra-c2012-2.7 ; valid unused parameter
102 void Update_IT_callback(HardwareTimer*) // NOLINT(readability/casting)
103 #else
104 void Update_IT_callback(void)
105 #endif
106 {
107  if (Buzzer_Muted == true) {
108  // If the buzzer was muted, then we must resume the previous alarm
109  Buzzer_Resume();
110  } else if ((Active_Buzzer_Index == 0u) && (Active_Buzzer_Repeat == false)
111  && (Active_Buzzer_Has_Begun == true)) {
112  // If we are at start of pattern, check for repeating mode
113  BuzzerTim->pause();
115  } else {
116  // Previous state is finished, switch to next one
119  } else {
121  }
122  BuzzerTim->setOverflow(Active_Buzzer[Active_Buzzer_Index + 1u], TICK_FORMAT);
125  }
126 }
127 
128 void Buzzer_Init() {
129  // Buzzer HardwareTimer object creation
130  BuzzerTim = new HardwareTimer(BUZZER_TIMER);
131 
133  // CPU Clock down to 10 kHz
134  BuzzerTim->setPrescaleFactor((BuzzerTim->getTimerClkFreq() / (TIMER_TICK_PER_MS * 1000)));
135  BuzzerTim->setOverflow(1); // don't care right now, timer is not started in init
136  BuzzerTim->setMode(BUZZER_TIM_CHANNEL, TIMER_OUTPUT_COMPARE, NC);
137  BuzzerTim->attachInterrupt(Update_IT_callback);
138 }
139 
140 void Buzzer_Start(const uint32_t* Buzzer, uint32_t Size, bool RepeatBuzzer) {
141  BuzzerTim->setCount(0);
142  Active_Buzzer = Buzzer;
144  Active_Buzzer_Size = Size;
145  Active_Buzzer_Repeat = RepeatBuzzer;
146  Active_Buzzer_Has_Begun = false;
147  Buzzer_Muted = false;
148 
149  BuzzerTim->setPreloadEnable(false);
150  BuzzerTim->setOverflow(100, TICK_FORMAT);
151 
152  // Timer starts
153  BuzzerTim->resume();
154 }
155 
156 void Buzzer_Mute() {
157  // If we are in Repeat and not muted, then an alarm is ringing
158  if ((Active_Buzzer_Repeat == true) && (Buzzer_Muted == false)) {
159  // Set the buzzer as muted
160  Buzzer_Muted = true;
161  // Reset the timer
162  BuzzerTim->setCount(0);
163  // Reset the index, so that we will restart after the mute period
165 
166  // Configuration of mute pattern
168  BuzzerTim->setOverflow(PAUSE_120S, TICK_FORMAT);
169 
170  // Timer starts. Required to configure output on GPIO
171  BuzzerTim->resume();
172  }
173 }
174 
176  BuzzerTim->setCount(0);
177  Buzzer_Muted = false;
179 
180  BuzzerTim->setOverflow(Active_Buzzer[Active_Buzzer_Index + 1u], TICK_FORMAT);
181 
182  // Timer starts. Required to configure output on GPIO
183  BuzzerTim->resume();
184 }
185 
187 
190 }
191 
193 
195 
196 void Buzzer_Stop(void) {
197  Active_Buzzer_Repeat = false;
198 
199  // Avoid unexpected infinite buzzing
201  BuzzerTim->pause();
202 }
const uint32_t Buzzer_High_Prio[BUZZER_HIGH_PRIO_SIZE]
High priority alarm buzzer pattern definition, composed of multiple couple of states (Actif/Inactif) ...
Definition: buzzer.cpp:52
#define BUZZER_HIGH_PRIO_SIZE
High priority alarm buzzer pattern size.
Definition: buzzer.cpp:48
bool Active_Buzzer_Repeat
Definition: buzzer.cpp:87
const uint32_t Buzzer_Boot[BUZZER_BOOT_SIZE]
Boot buzzer pattern definition, composed of multiple couple of states (Actif/Inactif) and duration (m...
Definition: buzzer.cpp:79
#define BIP_PAUSE
Definition: buzzer.cpp:35
const uint32_t Buzzer_Low_Prio[BUZZER_LOW_PRIO_SIZE]
Low priority alarm buzzer pattern definition, composed of multiple couple of states (Actif/Inactif) a...
Definition: buzzer.cpp:71
void Update_IT_callback(HardwareTimer *)
When timer period expires, switch to next state in the pattern of the buzzer.
Definition: buzzer.cpp:102
void Buzzer_Resume()
Resume the muted alarm.
Definition: buzzer.cpp:175
const uint32_t * Active_Buzzer
Definition: buzzer.cpp:84
void Buzzer_High_Prio_Start(void)
Activate the buzzer pattern for high priority alarms.
Definition: buzzer.cpp:186
#define BZ_ON
Definition: buzzer.cpp:45
#define PAUSE_20S
Definition: buzzer.cpp:39
bool Buzzer_Muted
Definition: buzzer.cpp:89
#define PAUSE_10S
Definition: buzzer.cpp:40
#define BUZZER_BOOT_SIZE
Boot buzzer pattern size.
Definition: buzzer.cpp:75
void Buzzer_Low_Prio_Start(void)
Activate the buzzer pattern for low priority alarms.
Definition: buzzer.cpp:192
#define BUZZER_LOW_PRIO_SIZE
Low priority alarm buzzer pattern size.
Definition: buzzer.cpp:67
void Buzzer_Start(const uint32_t *Buzzer, uint32_t Size, bool RepeatBuzzer)
Generic function to activate a buzzer.
Definition: buzzer.cpp:140
#define BIP
Definition: buzzer.cpp:34
#define PAUSE_1S
Definition: buzzer.cpp:41
uint32_t Active_Buzzer_Index
Definition: buzzer.cpp:85
#define BEEEEP_PAUSE
Definition: buzzer.cpp:37
bool Active_Buzzer_Has_Begun
Definition: buzzer.cpp:88
void Buzzer_Mute()
Mute the buzzer for 120s.
Definition: buzzer.cpp:156
void Buzzer_Boot_Start(void)
Activate boot bip.
Definition: buzzer.cpp:194
const uint32_t Buzzer_Medium_Prio[BUZZER_MEDIUM_PRIO_SIZE]
Medium priority alarm buzzer pattern definition, composed of multiple couple of states (Actif/Inactif...
Definition: buzzer.cpp:63
#define BZ_OFF
Definition: buzzer.cpp:44
#define TIMER_TICK_PER_MS
Definition: buzzer.cpp:33
HardwareTimer * BuzzerTim
Definition: buzzer.cpp:91
uint32_t Active_Buzzer_Size
Definition: buzzer.cpp:86
#define BUZZER_MEDIUM_PRIO_SIZE
Medium priority alarm buzzer pattern size.
Definition: buzzer.cpp:59
#define BEEEEP
Definition: buzzer.cpp:36
void Buzzer_Stop(void)
Stop Buzzer.
Definition: buzzer.cpp:196
#define PAUSE_120S
Definition: buzzer.cpp:38
void Buzzer_Init()
Initialization of HardwareTimer for buzzer.
Definition: buzzer.cpp:128
uint32_t BuzzerTimerChannel
Definition: buzzer.cpp:92
void Buzzer_Medium_Prio_Start(void)
Activate the buzzer pattern for medium priority alarms.
Definition: buzzer.cpp:188
#define BUZZER_TIMER
Hardware Timer to use for the buzzer.
Definition: buzzer.h:14
#define BUZZER_TIM_CHANNEL
Hardware Timer channel to use for the buzzer.
Definition: buzzer.h:17
void BuzzerControl_On(void)
Switch buzzer ON.
void BuzzerControl_Off(void)
Switch buzzer OFF.