vail-adapter/polybuzzer.cpp

45 lines
1006 B
C++
Raw Permalink Normal View History

2022-05-22 21:55:22 -06:00
#include <Arduino.h>
#include "polybuzzer.h"
#include "equal_temperament.h"
2022-05-22 21:55:22 -06:00
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]);
2022-05-22 21:55:22 -06:00
}
2022-06-26 10:55:28 -06:00
return;
2022-05-22 21:55:22 -06:00
}
}
this->playing = 0;
noTone(this->pin);
}
void PolyBuzzer::Tone(int slot, unsigned int frequency) {
this->tones[slot] = frequency;
2022-05-22 21:55:22 -06:00
this->update();
}
void PolyBuzzer::Note(int slot, uint8_t note) {
if (note > 127) {
note = 127;
2022-05-22 21:55:22 -06:00
}
this->Tone(slot, equalTemperamentNote[note]);
2022-05-22 21:55:22 -06:00
}
void PolyBuzzer::NoTone(int slot) {
tones[slot] = 0;
this->update();
}