Flying Silicon Sensor
sounds.cpp
Go to the documentation of this file.
1
24// Utility functions for buzzer sounds
25
26#include "pindefs.h"
27#include "sounds.h"
28#include <Arduino.h>
29
30
31namespace Sounds {
32
34static int buzzerPin = 0;
35
37static const int WholeNote = (60000 * 4) / 90;
38
39
44void setBuzzer(int pin) {
45
46 buzzerPin = pin;
47}
48
55static void sound(int freq, int ms) {
56
57 if (buzzerPin <= 0)
58 return;
59
60 if (freq < BUZZER_MIN_HZ)
61 freq = BUZZER_MIN_HZ;
62 else if (freq > BUZZER_MAX_HZ)
63 freq = BUZZER_MAX_HZ;
64
65 if (ms < BUZZER_MIN_MS)
66 ms = BUZZER_MIN_MS;
67
68 tone(buzzerPin, freq, ms);
69}
70
74static void silence(void) {
75
76 if (buzzerPin <= 0)
77 return;
78
79 noTone(buzzerPin);
80 digitalWrite(buzzerPin, LOW);
81}
82
88void beep(int freq, int ms) {
89
90 if (buzzerPin <= 0)
91 return;
92
93 sound(freq, ms);
94 delay(ms);
95 silence();
96}
97
105void playMelody(const int melody[]) {
106
107 int noteDuration = 0;
108 int i = 0;
109 while (melody[i] >= BUZZER_MIN_HZ) {
110
111 const int divider = melody[i + 1];
112
113 if (divider == 0) {
114 continue;
115 }
116 else if (divider > 0) {
117 // regular note, just proceed
118 noteDuration = WholeNote / divider;
119 }
120 else if (divider < 0) {
121 // dotted notes are represented with negative durations!
122 noteDuration = WholeNote / abs(divider);
123 noteDuration *= 1.5; // increases the duration in half for dotted notes
124 }
125
126 beep(melody[i], noteDuration * 0.9);
127
128 i += 2;
129 }
130}
131
135void playCharging(void) {
136
137 beep(NOTE_A1, 100);
138 delay(100);
139 beep(NOTE_A2, 150);
140}
141
146void playBattery(float batPct) {
147
148 const int PauseMs = 200;
149 const int ToneMs = 150;
150
151 int n; // number of beep repetitions
152 if (batPct > 90) {
153 // fully charged, 5 beeps, 5 bars in display
154 for (n = 0; n < 5; n++) {
155 beep(NOTE_A2, ToneMs);
156 delay(PauseMs);
157 }
158 }
159 else if (batPct > 78) {
160 // ~80% left
161 for (n = 0; n < 4; n++) {
162 beep(NOTE_A2, ToneMs);
163 delay(PauseMs);
164 }
165 }
166 else if (batPct > 58) {
167 // ~60% left
168 for (n = 0; n < 3; n++) {
169 beep(NOTE_A2, ToneMs);
170 delay(PauseMs);
171 }
172 }
173 else if (batPct > 38) {
174 // ~40% left
175 for (n = 0; n < 2; n++) {
176 beep(NOTE_A2, ToneMs);
177 delay(PauseMs);
178 }
179 }
180 else if (batPct > 18) {
181 // ~20% left
182 for (n = 0; n < 1; n++) {
183 beep(NOTE_A2, ToneMs);
184 delay(PauseMs);
185 }
186 }
187 else {
188 // discharged, 1 low beep
189 beep(NOTE_A1, 250);
190 delay(PauseMs);
191 }
192}
193
197void playConnect(void) {
198
199 beep(NOTE_G3, 100);
200 delay(100);
201 beep(NOTE_G3, 100);
202}
203
207void playDisconnect(void) {
208
209 beep(NOTE_A2, 100);
210 delay(100);
211 beep(NOTE_A2, 100);
212 delay(100);
213 beep(NOTE_A2, 100);
214}
215
219void playSwitchOff(void) {
220
221 beep(NOTE_A1, 500);
222}
223
224} // namespace Sounds
void playConnect(void)
Definition sounds.cpp:197
static const int WholeNote
Duration of a whole note.
Definition sounds.cpp:37
void playDisconnect(void)
Definition sounds.cpp:207
static void sound(int freq, int ms)
Definition sounds.cpp:55
void playSwitchOff(void)
Definition sounds.cpp:219
static void silence(void)
Definition sounds.cpp:74
void beep(int freq, int ms)
Definition sounds.cpp:88
static int buzzerPin
Buzzer pin to be used.
Definition sounds.cpp:34
void playMelody(const int melody[])
Definition sounds.cpp:105
void playCharging(void)
Definition sounds.cpp:135
void playBattery(float batPct)
Definition sounds.cpp:146
void setBuzzer(int pin)
Definition sounds.cpp:44
Pin definitions for Seeed XIAO nRF52840 2.9.2 with Adafruit bluefruit library.
Varios methods to produce the buzzer sounds.
#define BUZZER_MAX_HZ
Definition sounds.h:31
#define NOTE_G3
Definition sounds.h:58
#define BUZZER_MIN_MS
Definition sounds.h:33
#define BUZZER_MIN_HZ
Definition sounds.h:29
#define NOTE_A2
Definition sounds.h:69
#define NOTE_A1
Definition sounds.h:82