-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBuzzer.cpp
52 lines (46 loc) · 967 Bytes
/
Buzzer.cpp
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
#include "Buzzer.h"
Buzzer::Buzzer(int pinId) : DigitalOutput(pinId), m_frequency(0), m_delay(0), m_playing(false)
{
Reset();
}
void Buzzer::Reset()
{
m_playing = false;
m_frequency = 0;
m_delay = 0;
m_duration = 0;
m_stepTime = 0;
m_currentState = LOW;
DigitalOutput::SetState(m_currentState);
}
void Buzzer::Update(long updateInterval)
{
m_duration += updateInterval;
m_stepTime += updateInterval;
if (m_duration >= m_delay)
{
Reset();
}
else
{
if (m_stepTime >= m_frequency)
{
m_stepTime -= m_frequency;
m_currentState = !m_currentState;
DigitalOutput::SetState(m_currentState);
}
}
}
void Buzzer::PlayTone(int frequency, float durationSeconds)
{
m_playing = true;
m_frequency = (int)((1 / frequency) * 1000000);
m_delay = (int)(durationSeconds * 1000000);
m_duration = 0;
m_stepTime = 0;
m_currentState = LOW;
}
bool Buzzer::IsPlaying()
{
return m_playing;
}