From 02da4500f68b6106c52c547cfe3b14f93ad1e78f Mon Sep 17 00:00:00 2001 From: Petr Mikhalicin Date: Wed, 10 Jul 2024 09:49:35 +0500 Subject: [PATCH] Move melody into separate class --- src/Beeper.cpp | 38 +++++++++++++++----------------------- src/Beeper.h | 6 +++++- src/Melody.cpp | 42 ++++++++++++++++++++++++++++++++++++++++++ src/Melody.h | 16 ++++++++++++++++ 4 files changed, 78 insertions(+), 24 deletions(-) create mode 100644 src/Melody.cpp create mode 100644 src/Melody.h diff --git a/src/Beeper.cpp b/src/Beeper.cpp index 68539e0..c4799df 100644 --- a/src/Beeper.cpp +++ b/src/Beeper.cpp @@ -2,8 +2,6 @@ #include "Tools.h" -constexpr uint16_t kMelodySwitchTimes[] = { 250, 250, 125, 125, 60, 60, 400, 800 }; - void Beeper::tick() { uint32_t currentTime = millis(); switch (m_state) { @@ -35,6 +33,8 @@ void Beeper::tick() { void Beeper::setup() { pinMode(BEEPER, OUTPUT); analogWrite(BEEPER, 0); + + m_melody = Melody::getMelody(Melody::nice); } void Beeper::beep() { @@ -68,10 +68,8 @@ void Beeper::stop() { void Beeper::alarm(const char* notification) { m_state = State::alarm; + m_melody->init(); m_notification = notification; - m_timer = millis() + kMelodySwitchTimes[0]; - m_melodyPhase = 0; - m_pinState = true; processAlarm(); processPin(); @@ -90,27 +88,14 @@ void Beeper::processAlarm() { } } - uint32_t currentTime = millis(); - - if (m_timer > currentTime) + m_pinState = m_melody->tick(); + if (!m_melody->end()) return; - ++m_melodyPhase; - - if (m_melodyPhase == sizeof(kMelodySwitchTimes) / sizeof(kMelodySwitchTimes[0])) { - if (gSettings.confirmAlarm) { - m_pinState = true; - m_melodyPhase = 0; - m_timer = currentTime + kMelodySwitchTimes[0]; - return; - } - + if (gSettings.confirmAlarm) + m_melody->init(); + else stop(); - return; - } - - m_pinState = !m_pinState; - m_timer = currentTime + kMelodySwitchTimes[m_melodyPhase]; } void Beeper::processPin() const { @@ -123,3 +108,10 @@ void Beeper::processPin() const { bool Beeper::blocking() const { return gSettings.confirmAlarm && m_state == State::alarm; } + +void Beeper::setMelody(Melody::Name melodyName) { + delete m_melody; + m_melody = Melody::getMelody(melodyName); + if (m_state == State::alarm) + alarm(m_notification); +} diff --git a/src/Beeper.h b/src/Beeper.h index cb83412..8f3accc 100644 --- a/src/Beeper.h +++ b/src/Beeper.h @@ -2,6 +2,8 @@ #include +#include "Melody.h" + class Beeper { public: Beeper(uint8_t pin) : m_pin(pin) {} @@ -20,14 +22,16 @@ class Beeper { void setup(); void tick(); + void setMelody(Melody::Name); + private: void processAlarm(); void processPin() const; bool m_pinState = false; - uint8_t m_melodyPhase; State m_state = State::off; uint8_t m_pin; uint32_t m_timer; const char* m_notification; + Melody* m_melody; }; diff --git a/src/Melody.cpp b/src/Melody.cpp new file mode 100644 index 0000000..eeec9d0 --- /dev/null +++ b/src/Melody.cpp @@ -0,0 +1,42 @@ +#include "Melody.h" + +#include + +constexpr uint8_t kNiceMelodySwitchTimes[] = { 25, 25, 12, 12, 6, 6, 40, 80 }; + +class NiceMelody : public Melody { +public: + const char* name() const override { return "nice"; } + + void init() override { + m_melodyPhase = 0; + m_timer = millis() + kNiceMelodySwitchTimes[0] * 10; + } + + bool tick() override { + uint32_t currentTime = millis(); + + if (m_timer <= currentTime) { + ++m_melodyPhase; + + if (end()) + return false; + + m_timer = currentTime + kNiceMelodySwitchTimes[m_melodyPhase] * 10; + } + + return !(m_melodyPhase & 1); + } + + bool end() const override { + return m_melodyPhase >= sizeof(kNiceMelodySwitchTimes) / sizeof(kNiceMelodySwitchTimes[0]); + } + +private: + uint8_t m_melodyPhase; + uint32_t m_timer; +}; + +Melody* Melody::getMelody(Name) { + return new NiceMelody; +} diff --git a/src/Melody.h b/src/Melody.h new file mode 100644 index 0000000..35dcc34 --- /dev/null +++ b/src/Melody.h @@ -0,0 +1,16 @@ +#pragma once + +class Melody { +public: + virtual ~Melody() {} + + enum Name { nice, last_ }; + + static Melody* getMelody(Name); + + virtual const char* name() const = 0; + + virtual void init() = 0; + virtual bool end() const = 0; + virtual bool tick() = 0; +};