Skip to content

Commit

Permalink
Move melody into separate class
Browse files Browse the repository at this point in the history
  • Loading branch information
lo1ol committed Jul 10, 2024
1 parent d86985f commit 02da450
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 24 deletions.
38 changes: 15 additions & 23 deletions src/Beeper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -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();
Expand All @@ -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 {
Expand All @@ -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);
}
6 changes: 5 additions & 1 deletion src/Beeper.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

#include <stdint.h>

#include "Melody.h"

class Beeper {
public:
Beeper(uint8_t pin) : m_pin(pin) {}
Expand All @@ -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;
};
42 changes: 42 additions & 0 deletions src/Melody.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#include "Melody.h"

#include <Arduino.h>

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;
}
16 changes: 16 additions & 0 deletions src/Melody.h
Original file line number Diff line number Diff line change
@@ -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;
};

0 comments on commit 02da450

Please sign in to comment.