2022-05-22 21:55:22 -06:00
|
|
|
#include <Arduino.h>
|
|
|
|
#include "polybuzzer.h"
|
2023-02-25 18:10:14 -07:00
|
|
|
#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++) {
|
2023-02-25 18:10:14 -07:00
|
|
|
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) {
|
2023-02-25 18:10:14 -07:00
|
|
|
this->tones[slot] = frequency;
|
2022-05-22 21:55:22 -06:00
|
|
|
this->update();
|
|
|
|
}
|
|
|
|
|
2023-02-25 18:10:14 -07:00
|
|
|
void PolyBuzzer::Note(int slot, uint8_t note) {
|
|
|
|
if (note > 127) {
|
|
|
|
note = 127;
|
2022-05-22 21:55:22 -06:00
|
|
|
}
|
2023-02-25 18:10:14 -07:00
|
|
|
this->Tone(slot, equalTemperamentNote[note]);
|
2022-05-22 21:55:22 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
void PolyBuzzer::NoTone(int slot) {
|
|
|
|
tones[slot] = 0;
|
|
|
|
this->update();
|
|
|
|
}
|
|
|
|
|