beeper, buzzer

This commit is contained in:
Patrick Haßel 2025-03-13 14:47:14 +01:00
parent 15f4e4feb2
commit a09710fa4b
6 changed files with 106 additions and 1 deletions

33
src/beeper.cpp Normal file
View File

@ -0,0 +1,33 @@
#include "beeper.h"
#include <Arduino.h>
#define BEEPER_GPIO 22
#define BEEPER_INVERT true
unsigned long beeperDuration = 0;
unsigned long beeperLast = 0;
void beeperEnable(const bool enabled) {
digitalWrite(BEEPER_GPIO, (enabled ^ BEEPER_INVERT) ? HIGH : LOW);
}
void beeperSetup() {
pinMode(BEEPER_GPIO, OUTPUT);
beeperEnable(false);
}
void beeperLoop() {
const auto now = millis();
if (beeperDuration != 0 && now - beeperLast >= beeperDuration) {
beeperEnable(false);
beeperDuration = 0;
}
}
void beep(const unsigned long duration) {
beeperDuration = duration;
beeperLast = millis();
beeperEnable(beeperDuration > 0);
}

10
src/beeper.h Normal file
View File

@ -0,0 +1,10 @@
#ifndef BEEPER_H
#define BEEPER_H
void beeperSetup();
void beeperLoop();
void beep(unsigned long duration);
#endif

15
src/buzzer.cpp Normal file
View File

@ -0,0 +1,15 @@
#include "buzzer.h"
#include <Arduino.h>
#define BUZZER_GPIO 23
#define BUZZER_INVERT true
void buzzerSetup() {
pinMode(BUZZER_GPIO, OUTPUT);
buzzerEnable(false);
}
void buzzerEnable(const bool enabled) {
digitalWrite(BUZZER_GPIO, (enabled ^ BUZZER_INVERT) ? HIGH : LOW);
}

8
src/buzzer.h Normal file
View File

@ -0,0 +1,8 @@
#ifndef BUZZER_H
#define BUZZER_H
void buzzerSetup();
void buzzerEnable(bool enabled);
#endif

View File

@ -1,10 +1,16 @@
#include "countdown.h"
#include <LittleFS.h>
#include "countdown.h"
#include "beeper.h"
#include "buzzer.h"
#include "display.h"
#define COUNTDOWN_END_SEQUENCE_STEPS 5
#define COUNTDOWN_END_SEQUENCE_REPEAT 3
#define HALF_BEEP_DURATION 1000
#define LAST_10_BEEP_DURATION 100
enum CountdownState {
CONFIG, READY, RUNNING, PAUSED, END
};
@ -25,6 +31,10 @@ unsigned long countdownEndSequenceDelay = 0;
unsigned long countdownEndSequenceLast = 0;
bool countdownHalfTimeBeep = false;
long countdownBeepSeconds = -1;
void setState(const CountdownState newState) {
if (countdownState == newState) {
return;
@ -32,6 +42,7 @@ void setState(const CountdownState newState) {
countdownState = newState;
const char* name;
bool buzzer = false;
switch (countdownState) {
case CONFIG:
countdownRest = countdownMillis;
@ -39,6 +50,8 @@ void setState(const CountdownState newState) {
break;
case READY:
countdownRest = countdownMillis;
countdownHalfTimeBeep = false;
countdownBeepSeconds = -1;
name = "READY";
break;
case RUNNING:
@ -49,6 +62,7 @@ void setState(const CountdownState newState) {
name = "PAUSED";
break;
case END:
buzzer = true;
countdownEndSequenceDelay = 0;
countdownEndSequenceStep = 0;
name = "END";
@ -57,6 +71,7 @@ void setState(const CountdownState newState) {
name = "[???]";
break;
}
buzzerEnable(buzzer);
Serial.printf("Mode: %s\n", name);
}
@ -165,6 +180,22 @@ void updateTime() {
if (countdownLast != 0) {
const auto diff = now - countdownLast;
countdownRest -= static_cast<long>(diff);
if (countdownRest <= countdownMillis / 2) {
if (!countdownHalfTimeBeep) {
beep(HALF_BEEP_DURATION);
countdownHalfTimeBeep = true;
}
}
const auto seconds = static_cast<long>(ceil(countdownRest / 1000.0));
if (seconds <= 10 && seconds > 0) {
if (countdownBeepSeconds != seconds) {
beep(LAST_10_BEEP_DURATION);
countdownBeepSeconds = seconds;
}
}
if (countdownRest <= 0) {
countdownRest = 0;
setState(END);

View File

@ -1,4 +1,6 @@
#include "beeper.h"
#include "button.h"
#include "buzzer.h"
#include "countdown.h"
#include "display.h"
@ -6,12 +8,18 @@ void setup() {
delay(500);
Serial.begin(115200);
Serial.println("\n\n\nstartup");
beeperSetup();
buttonSetup();
buzzerSetup();
displaySetup();
countdownSetup();
}
void loop() {
beeperLoop();
buttonLoop();
countdownLoop();
}