-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
78 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}; |