vail-adapter/polybuzzer.cpp

45 lines
1.0 KiB
C++
Raw Normal View History

2022-05-22 21:55:22 -06:00
#include <Arduino.h>
#include "polybuzzer.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 (tones[i]) {
if (playing != tones[i]) {
playing = tones[i];
tone(this->pin, tones[i]);
}
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) {
tones[slot] = frequency;
this->update();
}
void PolyBuzzer::Note(int slot, int note) {
unsigned int frequency = 8.18; // MIDI note 0
for (int i = 0; i < note; i++) {
frequency *= 1.0594630943592953; // equal temperament half step
}
this->Tone(slot, frequency);
}
void PolyBuzzer::NoTone(int slot) {
tones[slot] = 0;
this->update();
}