-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuzzer_sound.hh
119 lines (94 loc) · 3.35 KB
/
buzzer_sound.hh
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
#ifndef BUZZER_SOUND_HH
#define BUZZER_SOUND_HH
#include <stdint.h>
class BuzzerSound
{
public:
typedef enum {
SoundTypeOff,
SoundTypeBeep,
SoundTypeSweep
} SoundType;
typedef enum {
SoundPriorityLow = 0,
SoundPriorityMedium = 1,
SoundPriorityHigh = 2,
} SoundPriority;
BuzzerSound();
/// Replace data in this if replacement has same or higher priority
/// or if this is not enabled
void ReplaceIfHigherPriorityOrActive(const BuzzerSound replacement);
/// Step/tick the sound and get next frequency to be set to the buzzer
uint16_t StepAndGetFrequency();
/// Set volume for the sound
/// \param[in] volume Volume. 0 = quiet, 255 = loud
void SetVolume(uint8_t volume);
/// \return Volume for the sound
uint8_t GetVolume();
/// Set priority
/// \param[in] priority Priority for this sound
void SetPriority(const SoundPriority priority);
/// Set type of the sound
/// \param[in] type Type of the sound
void SetType(const SoundType type);
/// Set beep hald period
/// \param[in] half_period Longer periods give slower beeps
void SetBeepHalfPeriod(const uint8_t half_period);
/// \param[in] num_beeps Number of beeps to generate
/// \param[in] priority Priority of the beeps
/// \return Beep
static BuzzerSound Beep(const uint8_t num_beeps = 1,
const SoundPriority priority = SoundPriorityLow);
/// Create sweep sound
/// \param[in] priority Priority of the sweep
/// \return Sweep sound
static BuzzerSound Sweep(const SoundPriority priority = SoundPriorityHigh);
/// Create constant beeping
/// \parma[in] hz Aprroximate frequency of the beeping (1-50)
/// \param[in] priority Priority of the beeping
/// \return Constant beeping
static BuzzerSound ConstantBeeping(const uint8_t hz,
const SoundPriority priority = SoundPriorityMedium);
/// Set if the sound is active (playing) or not
/// \param[in] active If true, sound could be played
void SetActive(const bool active);
/// \return True if active (playing)
bool IsActive() const;
/// Set beep buzz frequency
void SetBeepBuzzFrequency(const uint16_t freq);
private:
/// Step sweep and
/// \return New frequency for the sound
uint16_t StepSweep();
/// Step beep and
/// \return New frequency for the sound
uint16_t StepBeep();
private:
// True if the sound is still active (/playing)
bool active_;
// Priority of the sound
SoundPriority priority_;
// Type of the sound
SoundType type_;
// Volume of the sound (0=quiet, 255=loud)
uint8_t volume_;
// Current frequency of the sound
uint16_t frequency_;
// Sweep related
// Minimum frequency of the sweep sound in Hz
uint16_t sweep_freqz_min_;
// Maximum frequency of the sweep sound in Hz
uint16_t sweep_freqz_max_;
// Frequency step of the sweep in Hz
int16_t sweep_freqz_step_;
// Beep related
// How many beeps are left
uint8_t num_beeps_left_;
// Length of beep on/off period
uint8_t beep_half_period_length_;
// At what position of th ebeep period we are currently
uint8_t beep_period_position_;
// Frequency of the beep
uint16_t beep_frequency_;
};
#endif //ifndef BUZZER_SOUND_HH