-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathpolybuzzer.cpp
44 lines (38 loc) · 1006 Bytes
/
polybuzzer.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
#include <Arduino.h>
#include "polybuzzer.h"
#include "equal_temperament.h"
PolyBuzzer::PolyBuzzer(uint8_t pin) {
for (int i = 0; i < POLYBUZZER_MAX_TONES; i++) {
this->tones[i] = 0;
}
this->playing = 0;
this->pin = pin;
pinMode(pin, OUTPUT);
}
void PolyBuzzer::update() {
for (int i = 0; i < POLYBUZZER_MAX_TONES; i++) {
if (this->tones[i]) {
if (this->playing != this->tones[i]) {
this->playing = this->tones[i];
tone(this->pin, this->tones[i]);
}
return;
}
}
this->playing = 0;
noTone(this->pin);
}
void PolyBuzzer::Tone(int slot, unsigned int frequency) {
this->tones[slot] = frequency;
this->update();
}
void PolyBuzzer::Note(int slot, uint8_t note) {
if (note > 127) {
note = 127;
}
this->Tone(slot, equalTemperamentNote[note]);
}
void PolyBuzzer::NoTone(int slot) {
tones[slot] = 0;
this->update();
}