Compare commits

..

2 Commits

Author SHA1 Message Date
a09710fa4b beeper, buzzer 2025-03-13 14:47:14 +01:00
15f4e4feb2 integrated endSequence into mainloop (instead of delay) 2 2025-03-13 14:08:44 +01:00
6 changed files with 112 additions and 4 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,8 +1,15 @@
#include "countdown.h"
#include <LittleFS.h> #include <LittleFS.h>
#include "countdown.h"
#include "beeper.h"
#include "buzzer.h"
#include "display.h" #include "display.h"
#define COUNTDOWN_END_SEQUENCE_REPEAT 2 #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 { enum CountdownState {
CONFIG, READY, RUNNING, PAUSED, END CONFIG, READY, RUNNING, PAUSED, END
@ -24,6 +31,10 @@ unsigned long countdownEndSequenceDelay = 0;
unsigned long countdownEndSequenceLast = 0; unsigned long countdownEndSequenceLast = 0;
bool countdownHalfTimeBeep = false;
long countdownBeepSeconds = -1;
void setState(const CountdownState newState) { void setState(const CountdownState newState) {
if (countdownState == newState) { if (countdownState == newState) {
return; return;
@ -31,6 +42,7 @@ void setState(const CountdownState newState) {
countdownState = newState; countdownState = newState;
const char* name; const char* name;
bool buzzer = false;
switch (countdownState) { switch (countdownState) {
case CONFIG: case CONFIG:
countdownRest = countdownMillis; countdownRest = countdownMillis;
@ -38,6 +50,8 @@ void setState(const CountdownState newState) {
break; break;
case READY: case READY:
countdownRest = countdownMillis; countdownRest = countdownMillis;
countdownHalfTimeBeep = false;
countdownBeepSeconds = -1;
name = "READY"; name = "READY";
break; break;
case RUNNING: case RUNNING:
@ -48,6 +62,7 @@ void setState(const CountdownState newState) {
name = "PAUSED"; name = "PAUSED";
break; break;
case END: case END:
buzzer = true;
countdownEndSequenceDelay = 0; countdownEndSequenceDelay = 0;
countdownEndSequenceStep = 0; countdownEndSequenceStep = 0;
name = "END"; name = "END";
@ -56,6 +71,7 @@ void setState(const CountdownState newState) {
name = "[???]"; name = "[???]";
break; break;
} }
buzzerEnable(buzzer);
Serial.printf("Mode: %s\n", name); Serial.printf("Mode: %s\n", name);
} }
@ -64,12 +80,14 @@ void drawSequence() {
if (countdownEndSequenceLast != 0 && now - countdownEndSequenceLast < countdownEndSequenceDelay) { if (countdownEndSequenceLast != 0 && now - countdownEndSequenceLast < countdownEndSequenceDelay) {
return; return;
} }
switch (countdownEndSequenceStep % 3) { switch (countdownEndSequenceStep % COUNTDOWN_END_SEQUENCE_STEPS) {
case 0: case 0:
case 2:
drawAll(WHITE); drawAll(WHITE);
countdownEndSequenceDelay = 100; countdownEndSequenceDelay = 100;
break; break;
case 1: case 1:
case 3:
drawBlack(); drawBlack();
countdownEndSequenceDelay = 100; countdownEndSequenceDelay = 100;
break; break;
@ -80,7 +98,7 @@ void drawSequence() {
} }
countdownEndSequenceStep++; countdownEndSequenceStep++;
countdownEndSequenceLast = now; countdownEndSequenceLast = now;
if (countdownEndSequenceStep >= 3 * COUNTDOWN_END_SEQUENCE_REPEAT) { if (countdownEndSequenceStep >= COUNTDOWN_END_SEQUENCE_STEPS * COUNTDOWN_END_SEQUENCE_REPEAT) {
setState(READY); setState(READY);
} }
} }
@ -162,6 +180,22 @@ void updateTime() {
if (countdownLast != 0) { if (countdownLast != 0) {
const auto diff = now - countdownLast; const auto diff = now - countdownLast;
countdownRest -= static_cast<long>(diff); 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) { if (countdownRest <= 0) {
countdownRest = 0; countdownRest = 0;
setState(END); setState(END);

View File

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