From 84fe7acf161c09fb5559ba2db3e0ad68771818ca Mon Sep 17 00:00:00 2001 From: Petr Mikhalicin Date: Wed, 10 Jul 2024 19:48:10 +0500 Subject: [PATCH] Add ability to set melody in settings --- src/Melody.cpp | 22 ++++++++++++++++++++-- src/Melody.h | 1 + src/Settings.cpp | 3 +++ src/Settings.h | 2 ++ src/SettingsSetter.cpp | 24 ++++++++++++++++++++++++ src/SettingsSetter.h | 2 ++ 6 files changed, 52 insertions(+), 2 deletions(-) diff --git a/src/Melody.cpp b/src/Melody.cpp index daa575c..e893b90 100644 --- a/src/Melody.cpp +++ b/src/Melody.cpp @@ -63,6 +63,24 @@ class AlarmMelody : public Melody { uint32_t m_timer; }; -Melody* Melody::getMelody(Name) { - return new AlarmMelody; +Melody* Melody::getMelody(Name name) { + switch(name) { + case Name::alarm: + return new AlarmMelody; + case Name::nice: + return new NiceMelody; + } + + return nullptr; +} + +const char* Melody::getMelodyName(Name name) { + switch(name) { + case Name::alarm: + return "alarm"; + case Name::nice: + return "nice"; + } + + return nullptr; } diff --git a/src/Melody.h b/src/Melody.h index 1945200..455486e 100644 --- a/src/Melody.h +++ b/src/Melody.h @@ -7,6 +7,7 @@ class Melody { enum Name { alarm, nice, last_ }; static Melody* getMelody(Name); + static const char* getMelodyName(Name); virtual void init() = 0; virtual bool end() const = 0; diff --git a/src/Settings.cpp b/src/Settings.cpp index d7747b4..319a8de 100644 --- a/src/Settings.cpp +++ b/src/Settings.cpp @@ -21,6 +21,7 @@ Settings::Settings() { GET_SETTING(startWithSettings); GET_SETTING(logViewInTests); GET_SETTING(logViewInMasks); + GET_SETTING(melody); uint32_t hash = crc32.finalize(); uint32_t storedHash; @@ -36,6 +37,7 @@ Settings::Settings() { startWithSettings = false; logViewInTests = true; logViewInMasks = true; + melody = Melody::nice; updateEEPROM(); return; } @@ -57,6 +59,7 @@ void Settings::updateEEPROM() { PUT_SETTING(startWithSettings); PUT_SETTING(logViewInTests); PUT_SETTING(logViewInMasks); + PUT_SETTING(melody); PUT_SETTING(crc32.finalize()); #undef PUT_SETTING } diff --git a/src/Settings.h b/src/Settings.h index 7156696..c51dc88 100644 --- a/src/Settings.h +++ b/src/Settings.h @@ -3,6 +3,7 @@ #include #include "Time.h" +#include "Melody.h" struct Settings { Settings(); @@ -16,4 +17,5 @@ struct Settings { bool logViewInTests; bool logViewInMasks; bool confirmAlarm; + Melody::Name melody; }; diff --git a/src/SettingsSetter.cpp b/src/SettingsSetter.cpp index 667deff..80846b6 100644 --- a/src/SettingsSetter.cpp +++ b/src/SettingsSetter.cpp @@ -109,6 +109,22 @@ void SettingsSetter::processSetViewInMasks() const { gDisplay[1] << "Log"; } +void SettingsSetter::processSetMelody() const { + gDisplay[0] << "Notify melody"; + uint8_t choice = gSettings.melody; + bool changed = getInt(choice, 0, Melody::last_ - 1); + + gDisplay[1] << Melody::getMelodyName(gSettings.melody); + + if (!changed) + return; + + gSettings.melody = static_cast(choice); + + gBeeper.setMelody(gSettings.melody); + gBeeper.alarm(); +} + void SettingsSetter::process() { if (m_timer.state() != Timer::RUNNING) { int8_t shift = 0; @@ -127,6 +143,11 @@ void SettingsSetter::process() { else gBeeper.stop(); + if (m_step == Step::setMelody) + gBeeper.alarm(); + else + gBeeper.stop(); + gSettings.lagTime = m_lagTime; gSettings.updateEEPROM(); } @@ -157,6 +178,9 @@ void SettingsSetter::process() { case Step::setViewInMasks: processSetViewInMasks(); break; + case Step::setMelody: + processSetMelody(); + break; } } diff --git a/src/SettingsSetter.h b/src/SettingsSetter.h index a5bd912..86e9d4a 100644 --- a/src/SettingsSetter.h +++ b/src/SettingsSetter.h @@ -15,6 +15,7 @@ class SettingsSetter final { setStartWithSettings, setViewInTests, setViewInMasks, + setMelody, last_ }; @@ -34,6 +35,7 @@ class SettingsSetter final { void processStartWithSettings() const; void processSetViewInTests() const; void processSetViewInMasks() const; + void processSetMelody() const; Step m_step = Step::setLagTime; Time m_lagTime;