Skip to content
Permalink
Branch: master
Find file Copy path
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
163 lines (144 sloc) 4.56 KB
#include "Animation.hpp"
// Warning: This is a code example for the article "guideline for modular firmware".
// It is code in a transition phase, do not use it as it is!
#include "Display.hpp"
namespace Animation {
const uint8_t cModeCount = 4;
const uint8_t cShiftSpeed = 8;
const uint8_t cBlinkSpeed = 10;
Mode gLedMode = Mode::Fade;
uint8_t gLedIndex = 0;
uint8_t gLedCounter = 0;
bool gLedDirection = false;
bool gOrangeDirection = true;
bool gGreenDirection = true;
bool gRedDirection = true;
void initialize()
{
// nothing to do.
}
void initializeMode()
{
if (gLedMode == Mode::Fade) {
Display::setLevel(Display::Color::Orange, 0x0c);
Display::setLevel(Display::Color::Green, 0x12);
Display::setLevel(Display::Color::Red, 0x18);
} else if (gLedMode == Mode::RollLeft) {
gLedIndex = 0;
gLedDirection = true;
Display::setLevel(Display::Color::Orange, (gLedIndex & 0x3) * 8);
Display::setLevel(Display::Color::Green, ((gLedIndex + 1) & 0x3) * 8);
Display::setLevel(Display::Color::Red, ((gLedIndex + 2) & 0x3) * 8);
gLedCounter = 0;
} else if (gLedMode == Mode::RollRight) {
gLedIndex = 0;
gLedDirection = false;
Display::setLevel(Display::Color::Orange, (gLedIndex & 0x3) * 8);
Display::setLevel(Display::Color::Green, ((gLedIndex + 1) & 0x3) * 8);
Display::setLevel(Display::Color::Red, ((gLedIndex + 2) & 0x3) * 8);
gLedCounter = 0;
}
}
void setMode(Mode mode)
{
if (gLedMode != mode) {
gLedMode = mode;
initializeMode();
}
}
void cycleMode(CycleDirection cycleDirection)
{
uint8_t mode = static_cast<uint8_t>(gLedMode);
if (cycleDirection == Next) {
++mode;
} else {
--mode;
}
mode &= 0x3;
setMode(static_cast<Mode>(mode));
}
void progress()
{
if (gLedMode == Mode::Fade) {
if (gOrangeDirection) {
auto level = Display::getLevel(Display::Color::Orange);
++level;
Display::setLevel(Display::Color::Orange, level);
if (level == Display::getMaximumLevel()) {
gOrangeDirection = false;
}
} else {
auto level = Display::getLevel(Display::Color::Orange);
--level;
Display::setLevel(Display::Color::Orange, level);
if (level == 0) {
gOrangeDirection = true;
}
}
if (gGreenDirection) {
auto level = Display::getLevel(Display::Color::Green);
++level;
Display::setLevel(Display::Color::Green, level);
if (level == Display::getMaximumLevel()) {
gGreenDirection = false;
}
} else {
auto level = Display::getLevel(Display::Color::Green);
--level;
Display::setLevel(Display::Color::Green, level);
if (level == 0) {
gGreenDirection = true;
}
}
if (!gRedDirection) {
auto level = Display::getLevel(Display::Color::Red);
--level;
Display::setLevel(Display::Color::Red, level);
if (level == 0) {
gRedDirection = true;
}
} else {
auto level = Display::getLevel(Display::Color::Red);
++level;
Display::setLevel(Display::Color::Red, level);
if (level == Display::getMaximumLevel()) {
gRedDirection = !gRedDirection;
}
}
} else if (gLedMode == Mode::RollRight) {
++gLedCounter;
if (gLedCounter >= cShiftSpeed) {
gLedCounter = 0;
gLedIndex = ((gLedIndex + 1) & 0x3);
Display::setLevel(Display::Color::Green, ((gLedIndex + 1) & 0x3) * 8);
Display::setLevel(Display::Color::Orange, (gLedIndex & 0x3) * 8);
Display::setLevel(Display::Color::Red, ((gLedIndex + 2) & 0x3) * 8);
}
} else if (gLedMode == Mode::RollLeft) {
++gLedCounter;
if (gLedCounter >= cShiftSpeed) {
--gLedIndex;
gLedIndex &= 0x3;
gLedCounter = 0;
Display::setLevel(Display::Color::Orange, (gLedIndex & 0x3) * 8);
Display::setLevel(Display::Color::Green, ((gLedIndex + 1) & 0x3) * 8);
Display::setLevel(Display::Color::Red, ((gLedIndex + 2) & 0x3) * 8);
}
} else if (gLedMode == Mode::Blink) {
++gLedCounter;
if (gLedCounter >= cBlinkSpeed) {
gLedCounter = 0;
auto orangeLevel = Display::getLevel(Display::Color::Orange);
if (orangeLevel != 0) {
Display::setLevel(Display::Color::Orange, 0);
Display::setLevel(Display::Color::Green, 0);
Display::setLevel(Display::Color::Red, 0);
} else {
Display::setLevel(Display::Color::Orange, Display::getMaximumLevel());
Display::setLevel(Display::Color::Green, Display::getMaximumLevel());
Display::setLevel(Display::Color::Red, Display::getMaximumLevel());
}
}
}
}
}
You can’t perform that action at this time.