Skip to content

Commit

Permalink
buzzer: stop pwm tones at precise times with alarms
Browse files Browse the repository at this point in the history
  • Loading branch information
delan committed Feb 17, 2024
1 parent 328efc6 commit 7085b44
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 19 deletions.
40 changes: 21 additions & 19 deletions src/buzzer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,20 @@
#include "settings.h"
#include "state.h"

static int64_t alarm(alarm_id_t, void *) {
buzzer.update();
return 0; // don’t reschedule
}

void Buzzer::pwmTone(unsigned int pitch, std::optional<unsigned long> duration) {
analogWriteRange(100);
analogWriteFreq(pitch);
analogWrite(BUZZER_PIN, 50);
if (duration.has_value()) {
auto unused = add_alarm_in_ms(*duration, alarm, nullptr, true);
}
}

void Buzzer::update0() {
const auto t = micros();

Expand All @@ -28,10 +42,8 @@ void Buzzer::update0() {
if (!isExpired(t, plugDuration)) {
return;
} else {
analogWriteRange(100);
analogWriteFreq(plugPitch2);
analogWrite(BUZZER_PIN, 50);
setCurrent(t, Buzzer::_::PLUG2);
pwmTone(plugPitch2, plugDuration);
return;
}
break;
Expand All @@ -44,10 +56,8 @@ void Buzzer::update0() {
if (!isExpired(t, plugDuration)) {
return;
} else {
analogWriteRange(100);
analogWriteFreq(plugPitch);
analogWrite(BUZZER_PIN, 50);
setCurrent(t, Buzzer::_::UNPLUG2);
pwmTone(plugPitch, plugDuration);
return;
}
break;
Expand All @@ -58,13 +68,11 @@ void Buzzer::update0() {
break;
}
if (state.bell) {
analogWriteRange(100);
analogWriteFreq(bellPitch);
analogWrite(BUZZER_PIN, 50);
setCurrent(t, Buzzer::_::BELL);
pwmTone(bellPitch);
} else if (current != Buzzer::_::NONE) {
digitalWrite(BUZZER_PIN, false);
setCurrent(t, Buzzer::_::NONE);
digitalWrite(BUZZER_PIN, false);
}
}

Expand Down Expand Up @@ -99,29 +107,23 @@ void Buzzer::click() {

if (current <= Buzzer::_::CLICK) {
// violation of sparc keyboard spec :) but distinguishable from bell!
analogWriteRange(100);
analogWriteFreq(1'000u);
analogWrite(BUZZER_PIN, 50);
setCurrent(micros(), Buzzer::_::CLICK);
pwmTone(1'000u, settings.clickDuration());
}
}

void Buzzer::plug() {
CoreMutex m{&buzzerMutex};
if (current <= Buzzer::_::PLUG2) {
analogWriteRange(100);
analogWriteFreq(plugPitch);
analogWrite(BUZZER_PIN, 50);
setCurrent(micros(), Buzzer::_::PLUG);
pwmTone(plugPitch, plugDuration);
}
}

void Buzzer::unplug() {
CoreMutex m{&buzzerMutex};
if (current <= Buzzer::_::UNPLUG2) {
analogWriteRange(100);
analogWriteFreq(plugPitch2);
analogWrite(BUZZER_PIN, 50);
setCurrent(micros(), Buzzer::_::UNPLUG);
pwmTone(plugPitch2, plugDuration);
}
}
2 changes: 2 additions & 0 deletions src/buzzer.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#define USB3SUN_BUZZER_H

#include <atomic>
#include <optional>

#include <CoreMutex.h>

Expand Down Expand Up @@ -34,6 +35,7 @@ struct Buzzer {
bool isExpired(unsigned long t, unsigned long duration);
void setCurrent(unsigned long t, State value);
void update0();
void pwmTone(unsigned int pitch, std::optional<unsigned long> duration = {});
};

extern Buzzer buzzer;
Expand Down

0 comments on commit 7085b44

Please sign in to comment.