Skip to content

Commit

Permalink
Add alarm melody
Browse files Browse the repository at this point in the history
  • Loading branch information
lo1ol committed Jul 10, 2024
1 parent 713ad16 commit fbe811d
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/Beeper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ void Beeper::setup() {
pinMode(BEEPER, OUTPUT);
analogWrite(BEEPER, 0);

m_melody = Melody::getMelody(Melody::nice);
m_melody = Melody::getMelody(Melody::alarm);
}

void Beeper::beep() {
Expand Down
30 changes: 29 additions & 1 deletion src/Melody.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,34 @@ class NiceMelody : public Melody {
uint32_t m_timer;
};

class AlarmMelody : public Melody {
public:
void init() override {
m_melodyPhase = 0;
m_timer = millis() + 60;
}

bool tick() override {
uint32_t currentTime = millis();

if (m_timer < currentTime) {
++m_melodyPhase;
if (m_melodyPhase % 20 == 19)
m_timer = currentTime + 200;
else
m_timer = currentTime + 50;
}

return !(m_melodyPhase & 1);
}

bool end() const override { return m_melodyPhase >= 20; }

private:
uint8_t m_melodyPhase;
uint32_t m_timer;
};

Melody* Melody::getMelody(Name) {
return new NiceMelody;
return new AlarmMelody;
}
2 changes: 1 addition & 1 deletion src/Melody.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ class Melody {
public:
virtual ~Melody() {}

enum Name { nice, last_ };
enum Name { alarm, nice, last_ };

static Melody* getMelody(Name);

Expand Down

0 comments on commit fbe811d

Please sign in to comment.