Skip to content

Commit

Permalink
Add Beeper class to easy control buzzer
Browse files Browse the repository at this point in the history
  • Loading branch information
lo1ol committed Jul 7, 2024
1 parent a26b1a1 commit 76ae0c6
Show file tree
Hide file tree
Showing 9 changed files with 123 additions and 39 deletions.
66 changes: 66 additions & 0 deletions src/Beeper.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
#include "Beeper.h"

#include "Tools.h"

void Beeper::tick() {
uint32_t currentTime = millis();
switch (m_state) {
case State::off:
break;
case State::on:
if (m_timer <= currentTime) {
m_pinState = !m_pinState;
if (m_pinState)
m_timer = currentTime + 100;
else
m_timer = currentTime + 900;
}
break;
case State::single:
if (m_timer <= currentTime) {
m_state = State::off;
m_pinState = false;
}
break;
}

if (m_pinState)
analogWrite(m_pin, gSettings.beepVolume);
else
analogWrite(m_pin, 0);
}

void Beeper::setup() {
pinMode(BEEPER, OUTPUT);
analogWrite(BEEPER, 0);
}

void Beeper::beep() {
m_state = State::single;

m_pinState = true;
m_timer = millis() + 100;
analogWrite(m_pinState, gSettings.beepVolume);
}

void Beeper::start(bool silentStart) {
m_state = State::on;

if (silentStart) {
m_pinState = false;
m_timer = millis() + 1000;
} else {
m_pinState = true;
m_timer = millis() + 100;
}

if (m_pinState)
analogWrite(m_pin, gSettings.beepVolume);
else
analogWrite(m_pin, 0);
}

void Beeper::stop() {
m_state = State::off;
analogWrite(m_pinState, 0);
}
25 changes: 25 additions & 0 deletions src/Beeper.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#pragma once

#include <stdint.h>

class Beeper {
public:
Beeper(uint8_t pin) : m_pin(pin) {}

enum class State { off, on, single };

void beep();

// start beep every second
void start(bool silentStart = false);
void stop();

void setup();
void tick();

private:
bool m_pinState = false;
State m_state = State::off;
uint8_t m_pin;
uint32_t m_timer;
};
18 changes: 8 additions & 10 deletions src/SettingsSetter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ namespace {
constexpr Time kMaxLagTime = 20_ts;
} // namespace

SettingsSetter::SettingsSetter() : m_lagTime(gSettings.lagTime), m_demoStartBeep(millis()), m_timer(BEEPER, RELAY) {}
SettingsSetter::SettingsSetter() : m_lagTime(gSettings.lagTime) {}

SettingsSetter::~SettingsSetter() {
analogWrite(BEEPER, 0);
gBeeper.stop();
gSettings.lagTime = m_lagTime;
gSettings.updateEEPROM();
}
Expand Down Expand Up @@ -39,14 +39,9 @@ void SettingsSetter::processSetBeepVolume() {
gDisplay[0] << "Beep volume";
uint8_t userVolume = min(gSettings.beepVolume, 30) / 3;
if (getInt(userVolume, 1, 10))
m_demoStartBeep = millis();
gBeeper.start();
gSettings.beepVolume = userVolume * 3;
gDisplay[1] << userVolume;

if ((millis() - m_demoStartBeep) % 1000 < 100)
analogWrite(BEEPER, gSettings.beepVolume);
else
analogWrite(BEEPER, 0);
}

void SettingsSetter::processSetAutoFinishView() {
Expand Down Expand Up @@ -116,8 +111,11 @@ void SettingsSetter::process() {
gEncoder.clear();
m_step = ADD_TO_ENUM(Step, m_step, shift);

analogWrite(BEEPER, 0);
m_demoStartBeep = millis();
if (m_step == Step::setBeepVolume)
gBeeper.start();
else
gBeeper.stop();

gSettings.lagTime = m_lagTime;
gSettings.updateEEPROM();
}
Expand Down
1 change: 0 additions & 1 deletion src/SettingsSetter.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,5 @@ class SettingsSetter final {

Step m_step = Step::setLagTime;
Time m_lagTime;
uint32_t m_demoStartBeep;
Timer m_timer;
};
34 changes: 15 additions & 19 deletions src/Timer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,6 @@
#include "DisplayLine.h"
#include "Tools.h"

void Timer::setup() {
pinMode(m_beepPin, OUTPUT);
pinMode(m_controlPin, OUTPUT);
analogWrite(m_beepPin, 0);
analogWrite(m_controlPin, 0);
}

void Timer::tick() {
m_currentTime = millis();
m_justStopped = false;
Expand All @@ -24,34 +17,35 @@ void Timer::tick() {

updateAfterLastResume();

// beep when we press start btn and every second
uint32_t passed = afterResume() / 100;
if ((m_currentTime - m_resumeTime <= 100) || (passed > 5 && !(passed % 10)))
analogWrite(m_beepPin, gSettings.beepVolume);
else
analogWrite(m_beepPin, 0);
if (!m_lagPassed && afterResume() / 100) {
m_lagPassed = true;
gBeeper.start(true);
}
}

void Timer::start(Time time) {
gBeeper.beep();

if (time == 0_ts) {
m_justStopped = true;
return;
}

m_lagPassed = false;
m_leftTime = time.toMillis();
m_resumeTime = m_currentTime;
m_status = RUNNING;
digitalWrite(m_controlPin, HIGH);
digitalWrite(RELAY, HIGH);
}

bool Timer::pause() {
if (m_status == RUNNING) {
analogWrite(m_beepPin, 0);
gBeeper.stop();
updateAfterLastResume();
m_total += afterResume();
m_leftTime -= afterResume();
m_status = PAUSED;
digitalWrite(m_controlPin, LOW);
digitalWrite(RELAY, LOW);

return m_currentTime > (m_resumeTime + gSettings.lagTime.toMillis());
}
Expand All @@ -61,23 +55,25 @@ bool Timer::pause() {

void Timer::resume() {
if (m_status == PAUSED) {
gBeeper.beep();
m_lagPassed = false;
m_resumeTime = m_currentTime;
m_status = RUNNING;
digitalWrite(m_controlPin, HIGH);
digitalWrite(RELAY, HIGH);
}
}

void Timer::stop() {
if (m_status != STOPPED) {
analogWrite(m_beepPin, 0);
gBeeper.stop();
updateAfterLastResume();
if (m_currentTime >= realStopTime())
m_total += m_leftTime;
else
m_total += afterResume();
m_status = STOPPED;
m_leftTime = 0;
digitalWrite(m_controlPin, LOW);
digitalWrite(RELAY, LOW);
}
}

Expand Down
8 changes: 2 additions & 6 deletions src/Timer.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,7 @@ class Timer {
public:
enum State { STOPPED, RUNNING, PAUSED };

Timer(uint8_t beepPin, uint8_t controlPin) : m_beepPin(beepPin), m_controlPin(controlPin) {}

void setup();
Timer() = default;

void tick();
void start(Time ms);
Expand Down Expand Up @@ -43,12 +41,10 @@ class Timer {
uint32_t realStopTime() const;

bool m_justStopped;
bool m_lagPassed;

State m_status = STOPPED;

uint8_t m_beepPin;
uint8_t m_controlPin;

Time m_afterLastResume;

uint32_t m_currentTime;
Expand Down
3 changes: 2 additions & 1 deletion src/Tools.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@ Button gModeSwitchBtn(MODE_SWITCH_BTN);
Button gStartBtn(START_BTN);
Button gExtraBtn(EXTRA_BTN);
Button gViewBtn(VIEW_BTN);
Timer gTimer(BEEPER, RELAY);
Timer gTimer;
Settings gSettings;
Display gDisplay(LiquidCrystal(LCD_RS, LCD_EN, LCD_D4, LCD_D5, LCD_D6, LCD_D7));
Beeper gBeeper(BEEPER);

void isr() {
gEncoder.tickISR();
Expand Down
2 changes: 2 additions & 0 deletions src/Tools.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

#include <EncButton.h>

#include "Beeper.h"
#include "Display.h"
#include "Settings.h"
#include "Time.h"
Expand All @@ -27,3 +28,4 @@ extern Button gViewBtn;
extern Timer gTimer;
extern Settings gSettings;
extern Display gDisplay;
extern Beeper gBeeper;
5 changes: 3 additions & 2 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -175,21 +175,22 @@ void processMode() {
}

void setup() {
gTimer.setup();

setupEncoder();

setMode(ModeId::testFStops);

pinMode(BACKLIGHT, OUTPUT);
analogWrite(BACKLIGHT, gSettings.backlight);
pinMode(RELAY, OUTPUT);
analogWrite(RELAY, 0);

if (gSettings.startWithSettings)
gSettingsSetter = new SettingsSetter;
}

void loop() {
gTimer.tick();
gBeeper.tick();
gEncoder.tick();
gStartBtn.tick();
gExtraBtn.tick();
Expand Down

0 comments on commit 76ae0c6

Please sign in to comment.