Skip to content

Commit

Permalink
fix bug where bell would pop then go silent
Browse files Browse the repository at this point in the history
  • Loading branch information
delan committed Jan 8, 2023
1 parent a8a0cd8 commit 84edbb8
Showing 1 changed file with 15 additions and 3 deletions.
18 changes: 15 additions & 3 deletions src/main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ struct {
uint8_t lastKeys[6];
uint8_t lastButtons;
std::atomic<unsigned long> clickingSince;
std::atomic<bool> bellStarted;
bool inMenu = false;
unsigned selectedMenuItem = 0u;
unsigned topMenuItem = 0u;
Expand Down Expand Up @@ -166,17 +167,28 @@ void buzzerClick() {
// violation of sparc keyboard spec :) but distinguishable from bell!
tone(BUZZER_PIN, 1'000u, state.clickDuration);
state.clickingSince = micros();
state.bellStarted = false;
}

void buzzerUpdate() {
const auto t = micros();
const unsigned long clickingSince = state.clickingSince;
if (state.clickingSince >= state.clickDuration * 1'000uL && t - state.clickingSince < state.clickDuration * 1'000uL)
return;
if (state.bell)
tone(BUZZER_PIN, 1'000'000u / 480u);
else
if (state.bell) {
// starting tone is not entirely idempotent, so avoid restarting it.
// spamming it every 10 ms will just pop and then silence in practice.
// FIXME standard library has no atomic compare exchange
// bool no = false;
// if (state.bellStarted.compare_exchange_strong(no, true))
if (!state.bellStarted) {
state.bellStarted = true;
tone(BUZZER_PIN, 1'000'000u / 480u);
}
} else {
state.bellStarted = false;
noTone(BUZZER_PIN);
}
}

void loop() {
Expand Down

0 comments on commit 84edbb8

Please sign in to comment.