Repository navigation
Expand file tree
/
Copy pathbuzzer.cpp
More file actions
122 lines (103 loc) · 2.3 KB
/
Copy pathbuzzer.cpp
File metadata and controls
122 lines (103 loc) · 2.3 KB
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
#include "buzzer.h"
Buzzer::Buzzer(byte pin) {
m_pin = pin;
Init();
}
void Buzzer::Init() {
pinMode(m_pin, OUTPUT);
m_prevMiliSeconds = 0;
m_count = 0;
m_internvalMiliSeconds = 0;
m_currentHz = 0;
m_endHz= 0;
m_durationHz = 0;
m_stepHz = 0;
m_incrementStep = true;
}
void Buzzer::Play(unsigned int hz, unsigned int duration) {
tone(m_pin, hz, duration);
}
void Buzzer::Stop() {
noTone(m_pin);
Init();
}
bool Buzzer::isFinishedPlaying() {
return m_count <= 0;
}
void Buzzer::PlaySiren() {
for(int hz = 440; hz < 1000; hz+=25)
{
Play(hz, 50);
delay(5);
}
for(int hz = 1000; hz > 440; hz-=25)
{
Play(hz, 50);
delay(5);
}
}
void Buzzer::PlaySirenInterval() {
if (m_currentHz == 0) {
// first time ever
m_currentHz = 440;
m_endHz = 1000;
m_stepHz = 25;
m_durationHz = 50;
m_internvalMiliSeconds = 5;
m_count = 1000;
}
if (isFinishedPlaying()) {
m_count = 1000;
m_incrementStep != m_incrementStep;
if (m_incrementStep) {
m_currentHz = 440;
m_endHz = 1000;
} else {
m_currentHz = 1000;
m_endHz = 440;
}
}
}
void Buzzer::PlayInterval(unsigned int hz, unsigned int endHz, unsigned int dur, int stepHz, bool increment,
unsigned int intervalMiliSeconds,
unsigned long count,
bool overwrite) {
if (overwrite == false) {
if (m_count > 0) {
// do nothing
return;
}
}
m_currentHz = hz;
m_endHz = endHz;
m_durationHz = dur;
m_internvalMiliSeconds = intervalMiliSeconds;
m_count = count;
m_stepHz = stepHz;
m_incrementStep = increment;
}
void Buzzer::OnUpdate(unsigned long miliSeconds) {
if (miliSeconds - m_prevMiliSeconds >= m_internvalMiliSeconds) {
bool shouldContinue = true;
// determine to step up or down
if (m_incrementStep) {
shouldContinue = m_currentHz < m_endHz;
} else {
shouldContinue = m_endHz < m_currentHz;
}
// playing a sound or stop
if (m_count > 0 && shouldContinue) {
Play(m_currentHz, m_durationHz);
if (m_incrementStep) {
m_currentHz += m_stepHz;
} else {
m_currentHz -= m_stepHz;
}
m_count--;
} else {
m_count = 0;
Stop();
}
m_prevMiliSeconds = miliSeconds;
}
}