vail-adapter

Firmware for USB morse code key adapter
git clone https://git.woozle.org/neale/vail-adapter.git

WrathPak  ·  2025-04-24

polybuzzer.cpp

 1#include <Arduino.h>
 2#include "polybuzzer.h"
 3#include "equal_temperament.h"
 4
 5PolyBuzzer::PolyBuzzer(uint8_t pin) {
 6        for (int i = 0; i < POLYBUZZER_MAX_TONES; i++) {
 7            this->tones[i] = 0;
 8        }
 9        this->playing = 0;
10        this->pin = pin;
11        pinMode(pin, OUTPUT);
12    }
13
14void PolyBuzzer::update() {
15    for (int i = 0; i < POLYBUZZER_MAX_TONES; i++) {
16        if (this->tones[i]) {
17            if (this->playing != this->tones[i]) {
18                this->playing = this->tones[i];
19                Serial.print("Buzzer playing frequency: ");
20                Serial.println(this->playing);
21                tone(this->pin, this->playing);
22            }
23            return;
24        }
25    }
26    this->playing = 0;
27    Serial.println("Buzzer stopped");
28    noTone(this->pin);
29}
30
31void PolyBuzzer::Tone(int slot, unsigned int frequency) {
32    Serial.print("Setting tone in slot ");
33    Serial.print(slot);
34    Serial.print(" to frequency: ");
35    Serial.println(frequency);
36    
37    this->tones[slot] = frequency;
38    this->update();
39}
40
41void PolyBuzzer::Note(int slot, uint8_t note) {
42    if (note > 127) {
43        note = 127;
44    }
45    
46    Serial.print("Setting note in slot ");
47    Serial.print(slot);
48    Serial.print(" to MIDI note #");
49    Serial.print(note);
50    Serial.print(" (frequency: ");
51    Serial.print(equalTemperamentNote[note]);
52    Serial.println("Hz)");
53    
54    this->Tone(slot, equalTemperamentNote[note]);
55}
56
57void PolyBuzzer::NoTone(int slot) {
58    Serial.print("Clearing tone in slot ");
59    Serial.println(slot);
60    
61    tones[slot] = 0;
62    this->update();
63}