Skyward boardcore
Loading...
Searching...
No Matches
Buzzer.h
Go to the documentation of this file.
1/* Copyright (c) 2022 Skyward Experimental Rocketry
2 * Author: Alberto Nidasio
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
5 * of this software and associated documentation files (the "Software"), to deal
6 * in the Software without restriction, including without limitation the rights
7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 * copies of the Software, and to permit persons to whom the Software is
9 * furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20 * THE SOFTWARE.
21 */
22
23#pragma once
24
26#include <miosix.h>
27
28namespace Boardcore
29{
30
36class Buzzer
37{
38public:
45 Buzzer(TIM_TypeDef* timer, TimerUtils::Channel channel);
46
52 void on();
53
59 void off();
60
68 void oneTimeToggle(uint16_t ms);
69
76 void continuouslyToggle(uint16_t onTime, uint16_t offTime);
77
78private:
79 GP16bitTimer timer;
80 TimerUtils::Channel channel;
81};
82
83inline Buzzer::Buzzer(TIM_TypeDef* timer, TimerUtils::Channel channel)
84 : timer(timer), channel(channel)
85{
86}
87
88inline void Buzzer::on()
89{
90 // First turn off and reset the timer
91 off();
92
93 // Set the polarity of the channel to low to make the output high
94 timer.setOutputCompareMode(channel,
98 timer.enableCaptureCompareOutput(channel);
99}
100
101inline void Buzzer::off()
102{
103 timer.disable();
104 timer.reset();
105}
106
107inline void Buzzer::oneTimeToggle(uint16_t ms)
108{
109 if (ms == 0)
110 return;
111
112 // First turn off and reset the timer
113 off();
114
115 // Set the prescaler with a 10KHz frequency
116 timer.setPrescaler(
118
119 // Make the timer reload after the specified amount of milliseconds
120 timer.setAutoReloadRegister(ms * 10);
121 timer.setCaptureCompareRegister(channel, 1);
122
123 timer.setOutputCompareMode(channel,
127 timer.enableCaptureCompareOutput(channel);
128
129 // Update the configuration
130 timer.generateUpdate();
131
132 // Use One Pulse Mode to stop the timer after the first update event
133 timer.enableOnePulseMode();
134 timer.enable();
135}
136
137inline void Buzzer::continuouslyToggle(uint16_t onTime, uint16_t offTime)
138{
139 if (onTime == 0 || offTime == 0)
140 return;
141
142 // First turn off and reset the timer
143 off();
144
145 // Set the prescaler with a 10KHz frequency
146 timer.setPrescaler(
148
149 // Make the timer reload after the specified amount of milliseconds
150 timer.setAutoReloadRegister((onTime + offTime) * 10);
151 timer.setCaptureCompareRegister(channel, onTime * 10);
152
153 timer.setOutputCompareMode(channel,
157 timer.enableCaptureCompareOutput(channel);
158
159 // Update the configuration
160 timer.generateUpdate();
161
162 timer.enable();
163}
164
165} // namespace Boardcore
virtual void generateUpdate() final
Re-initializes the timer counter and generate an update of the registers (the prescaler is cleared to...
Definition BasicTimer.h:286
virtual void setPrescaler(uint16_t prescalerValue) final
Updated the prescaler value.
Definition BasicTimer.h:297
TIM_TypeDef * getTimer()
Definition BasicTimer.h:214
virtual void enableOnePulseMode() final
Definition BasicTimer.h:336
This driver does not provide a square wave signal but instead is a simple utility that provides long ...
Definition Buzzer.h:37
void off()
Turns off the buzzer.
Definition Buzzer.h:101
void on()
Turns on the buzzer.
Definition Buzzer.h:88
Buzzer(TIM_TypeDef *timer, TimerUtils::Channel channel)
Definition Buzzer.h:83
void continuouslyToggle(uint16_t onTime, uint16_t offTime)
Turns on and off the buzzer indefinitely with the given timings.
Definition Buzzer.h:137
void oneTimeToggle(uint16_t ms)
Turns on the buzzer for the specified amount of time.
Definition Buzzer.h:107
void enableCaptureCompareOutput(TimerUtils::Channel channel)
void setCaptureCompareRegister(TimerUtils::Channel channel, T value)
void setOutputCompareMode(TimerUtils::Channel channel, TimerUtils::OutputCompareMode modeChannel)
void setCaptureComparePolarity(TimerUtils::Channel channel, TimerUtils::OutputComparePolarity polarity)
void reset() override
Resets the timer configuration to the default state.
void setAutoReloadRegister(T autoReloadValue)
@ PWM_MODE_1
Output is active as long as the counter is smaller than the compare register (reverse when downcounti...
@ FORCE_ACTIVE
Output is forced high.
@ PWM_MODE_2
Output is active as long as the counter is greater than the compare register (reverse when downcounti...
uint16_t computePrescalerValue(TIM_TypeDef *timer, int targetFrequency)
Compute the prescaler value for the specified target frequency.
Definition TimerUtils.h:448
This file includes all the types the logdecoder script will decode.