Skip to content

Commit

Permalink
Add notification confirmation
Browse files Browse the repository at this point in the history
  • Loading branch information
lo1ol committed Jul 10, 2024
1 parent 16890f9 commit 396c839
Show file tree
Hide file tree
Showing 12 changed files with 83 additions and 29 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
4. Time of auto turn off a lamp in view mode
5. Start with settings or not
6. Default views in test and mask modes
7. Confirmation of notification -- you have to click start btn, to confirm notification

## Modes

Expand Down
1 change: 1 addition & 0 deletions README_RU.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
4. Время автоотключения лампы в режиме просмотра
5. Возможность стартовать с настроек
6. Дефолтный вид в тестовых режимах и режиме маскирования (обычный вид/вид через лог)
7. Подтверждение уведомлений -- нужно ли нажимать на кнопку старта, если сработало уведомление о чем-то

## Режимы

Expand Down
48 changes: 37 additions & 11 deletions src/Beeper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

#include "Tools.h"

constexpr uint16_t kMelodySwitchTimes[] = { 250, 250, 125, 125, 60, 60, 400 };
constexpr uint16_t kMelodySwitchTimes[] = { 250, 250, 125, 125, 60, 60, 400, 800 };

void Beeper::tick() {
uint32_t currentTime = millis();
Expand All @@ -24,8 +24,8 @@ void Beeper::tick() {
m_pinState = false;
}
break;
case State::melody:
processMelody();
case State::alarm:
processAlarm();
break;
}

Expand Down Expand Up @@ -61,34 +61,56 @@ void Beeper::start(bool silentStart) {

void Beeper::stop() {
m_state = State::off;
m_pinState = false;
processPin();
}

void Beeper::melody() {
m_state = State::melody;
void Beeper::alarm(const char* notification) {
m_state = State::alarm;

m_notification = notification;
m_timer = millis() + kMelodySwitchTimes[0];
m_melodyPhase = 0;
m_pinState = true;

processMelody();
processAlarm();
processPin();
}

void Beeper::processMelody() {
if (m_timer > millis())
void Beeper::processAlarm() {
if (gSettings.confirmAlarm) {
gDisplay[0] << m_notification;
gDisplay[1] << "Click start btn";
// for safety support click on mode btn
if (gStartBtn.click() || gModeSwitchBtn.click()) {
stop();
gStartBtn.clear();
gModeSwitchBtn.clear();
return;
}
}

uint32_t currentTime = millis();

if (m_timer > currentTime)
return;

++m_melodyPhase;

if (m_melodyPhase == sizeof(kMelodySwitchTimes) / sizeof(kMelodySwitchTimes[0])) {
m_pinState = false;
m_state = State::off;
if (gSettings.confirmAlarm) {
m_pinState = true;
m_melodyPhase = 0;
m_timer = currentTime + kMelodySwitchTimes[0];
return;
}

stop();
return;
}

m_pinState = !m_pinState;
m_timer = millis() + kMelodySwitchTimes[m_melodyPhase];
m_timer = currentTime + kMelodySwitchTimes[m_melodyPhase];
}

void Beeper::processPin() const {
Expand All @@ -97,3 +119,7 @@ void Beeper::processPin() const {
else
analogWrite(m_pin, 0);
}

bool Beeper::blocking() const {
return gSettings.confirmAlarm && m_state == State::alarm;
}
9 changes: 6 additions & 3 deletions src/Beeper.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@ class Beeper {
public:
Beeper(uint8_t pin) : m_pin(pin) {}

enum class State { off, on, single, melody };
enum class State { off, on, single, alarm };

void beep();
void melody();
void alarm(const char* notification);

bool blocking() const;

// start beep every second
void start(bool silentStart = false);
Expand All @@ -19,12 +21,13 @@ class Beeper {
void tick();

private:
void processMelody();
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;
};
2 changes: 1 addition & 1 deletion src/Modes/FStopTestMode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ void FStopTestMode::process() {
if (gTimer.stopped()) {
if (m_currentRun == 0) { // don't take into account base time
gTimer.reset();
gBeeper.melody();
gBeeper.alarm("Change filter");
}
++m_currentRun;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Modes/LinearTestMode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ void LinearTestMode::process() {
if (gTimer.stopped()) {
if (m_currentRun == 0) {
gTimer.reset();
gBeeper.melody();
gBeeper.alarm("Change filter");
}
++m_currentRun;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Modes/MaskMode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ void MaskMode::processRun() {

if (gTimer.stopped()) {
if (m_notifyMask & (1 << m_currentMask))
gBeeper.melody();
gBeeper.alarm("Notification");
++m_currentMask;
}
}
Expand Down
3 changes: 3 additions & 0 deletions src/Settings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ Settings::Settings() {
GET_SETTING(beepVolume);
GET_SETTING(backlight);
GET_SETTING(autoFinishViewMinutes);
GET_SETTING(confirmAlarm);
GET_SETTING(startWithSettings);
GET_SETTING(logViewInTests);
GET_SETTING(logViewInMasks);
Expand All @@ -31,6 +32,7 @@ Settings::Settings() {
beepVolume = 3 * 3;
backlight = (3 * MAX_BACKLIGHT) / 10;
autoFinishViewMinutes = 3;
confirmAlarm = false;
startWithSettings = false;
logViewInTests = false;
logViewInMasks = true;
Expand All @@ -51,6 +53,7 @@ void Settings::updateEEPROM() {
PUT_SETTING(beepVolume);
PUT_SETTING(backlight);
PUT_SETTING(autoFinishViewMinutes);
PUT_SETTING(confirmAlarm);
PUT_SETTING(startWithSettings);
PUT_SETTING(logViewInTests);
PUT_SETTING(logViewInMasks);
Expand Down
1 change: 1 addition & 0 deletions src/Settings.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,5 @@ struct Settings {
bool startWithSettings;
bool logViewInTests;
bool logViewInMasks;
bool confirmAlarm;
};
26 changes: 20 additions & 6 deletions src/SettingsSetter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ void SettingsSetter::processSetLagTime() {
m_timer.start(m_lagTime);
}

void SettingsSetter::processSetBeepVolume() {
void SettingsSetter::processSetBeepVolume() const {
gDisplay[0] << "Beep volume";
uint8_t userVolume = min(gSettings.beepVolume, 30) / 3;
if (getInt(userVolume, 1, 10))
Expand All @@ -44,7 +44,7 @@ void SettingsSetter::processSetBeepVolume() {
gDisplay[1] << userVolume;
}

void SettingsSetter::processSetAutoFinishView() {
void SettingsSetter::processSetAutoFinishView() const {
gDisplay[0] << "Auto finish view";
getInt(gSettings.autoFinishViewMinutes, 0, 10);

Expand All @@ -56,7 +56,7 @@ void SettingsSetter::processSetAutoFinishView() {
gDisplay[1] << gSettings.autoFinishViewMinutes << " minute" << (gSettings.autoFinishViewMinutes == 1 ? "" : "s");
}

void SettingsSetter::processSetBacklight() {
void SettingsSetter::processSetBacklight() const {
gDisplay[0] << "Backlight";
uint8_t userBacklight = min(gSettings.backlight, MAX_BACKLIGHT) / (MAX_BACKLIGHT / 10);
getInt(userBacklight, 1, 10);
Expand All @@ -65,7 +65,18 @@ void SettingsSetter::processSetBacklight() {
analogWrite(BACKLIGHT, gSettings.backlight);
}

void SettingsSetter::processStartWithSettings() {
void SettingsSetter::processConfirmAlarm() const {
gDisplay[0] << "Confirm notify";
uint8_t choice = gSettings.confirmAlarm;
getInt(choice, 0, 1);
gSettings.confirmAlarm = choice;
if (choice == 1)
gDisplay[1] << "Yes";
else
gDisplay[1] << "No";
}

void SettingsSetter::processStartWithSettings() const {
gDisplay[0] << "Start with stngs";
uint8_t choice = gSettings.startWithSettings;
getInt(choice, 0, 1);
Expand All @@ -76,7 +87,7 @@ void SettingsSetter::processStartWithSettings() {
gDisplay[1] << "Yes";
}

void SettingsSetter::processSetViewInTests() {
void SettingsSetter::processSetViewInTests() const {
gDisplay[0] << "Dflt test view";
uint8_t choice = gSettings.logViewInTests;
getInt(choice, 0, 1);
Expand All @@ -87,7 +98,7 @@ void SettingsSetter::processSetViewInTests() {
gDisplay[1] << "Log";
}

void SettingsSetter::processSetViewInMasks() {
void SettingsSetter::processSetViewInMasks() const {
gDisplay[0] << "Dflt mask view";
uint8_t choice = gSettings.logViewInMasks;
getInt(choice, 0, 1);
Expand Down Expand Up @@ -134,6 +145,9 @@ void SettingsSetter::process() {
case Step::setAutoFinishView:
processSetAutoFinishView();
break;
case Step::setConfirmAlarm:
processConfirmAlarm();
break;
case Step::setStartWithSettings:
processStartWithSettings();
break;
Expand Down
14 changes: 8 additions & 6 deletions src/SettingsSetter.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ class SettingsSetter final {
setLagTime,
setBacklight,
setBeepVolume,
setConfirmAlarm,
setAutoFinishView,
setStartWithSettings,
setViewInTests,
Expand All @@ -26,12 +27,13 @@ class SettingsSetter final {

private:
void processSetLagTime();
void processSetBacklight();
void processSetBeepVolume();
void processSetAutoFinishView();
void processStartWithSettings();
void processSetViewInTests();
void processSetViewInMasks();
void processSetBacklight() const;
void processSetBeepVolume() const;
void processSetAutoFinishView() const;
void processConfirmAlarm() const;
void processStartWithSettings() const;
void processSetViewInTests() const;
void processSetViewInMasks() const;

Step m_step = Step::setLagTime;
Time m_lagTime;
Expand Down
3 changes: 3 additions & 0 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,9 @@ void loop() {
gSettingBtn.tick(gViewBtn, gModeSwitchBtn);
gDisplay.tick();

if (gBeeper.blocking())
return;

processModeSwitch();
processSettings();
processView();
Expand Down

0 comments on commit 396c839

Please sign in to comment.