From 9c53d05ab1ae4a2a4646de0440982f67607c14ef Mon Sep 17 00:00:00 2001 From: Torrie Fischer Date: Mon, 11 Dec 2023 07:58:17 +0100 Subject: [PATCH] animations: power: implement commands for brightness+power on/off --- src/animations/Power.cpp | 48 ++++++++++++++++++++++++++++++++++++++++ src/animations/Power.h | 14 ++++++++---- 2 files changed, 58 insertions(+), 4 deletions(-) diff --git a/src/animations/Power.cpp b/src/animations/Power.cpp index 6cd1fe1..ecedb61 100644 --- a/src/animations/Power.cpp +++ b/src/animations/Power.cpp @@ -2,6 +2,54 @@ #include "../Static.h" #include +void +doBrightness(Args& args, Print& out) +{ + String nextVal = args[1]; + uint8_t newBrightness = (uint8_t)atoi(nextVal.c_str()); + MainLoop::instance()->dispatch(InputEvent{InputEvent::SetBrightness, newBrightness}); +} + +void +doOn(Args& args, Print& out) +{ + MainLoop::instance()->dispatch(InputEvent{InputEvent::SetPower, 255}); +} + +void +doOff(Args& args, Print& out) +{ + MainLoop::instance()->dispatch(InputEvent{InputEvent::SetPower, 0}); +} + +void +doForceBrightness(Args& args, Print& out) +{ + String nextVal = args[1]; + uint8_t newBrightness = (uint8_t)atoi(nextVal.c_str()); + Static::instance()->forceBrightness(newBrightness); +} + +void +Power::forceBrightness(uint8_t v) +{ + m_forced = true; + FastLED.setBrightness(v); +} + +const std::vector _commands = { + {"brightness", doBrightness}, + {"brightness-force", doForceBrightness}, + {"on", doOn}, + {"off", doOff} +}; + +const std::vector& +Power::commands() const +{ + return _commands; +} + void Power::handleConfigChange(const Configuration& config) { diff --git a/src/animations/Power.h b/src/animations/Power.h index 92c1fb3..938d88c 100644 --- a/src/animations/Power.h +++ b/src/animations/Power.h @@ -10,15 +10,17 @@ public: switch (evt.intent) { case InputEvent::PowerToggle: m_powerState = m_powerState.value() <= 128 ? 255 : 0; - //Log.info("POWER TOGGLE %d", m_powerState.value()); + m_forced = false; + Log.notice("Power toggled to %t", m_powerState); break; case InputEvent::SetPower: m_powerState = evt.asInt() == 0 ? 0 : 255; - Log.notice("Power is now %d", m_powerState); + m_forced = false; + Log.notice("Power state is now %t", m_powerState); break; case InputEvent::SetBrightness: m_brightness = evt.asInt(); - m_brightness = 255; + m_forced = false; break; case InputEvent::Beat: m_beatDecay.set(0, 255); @@ -40,7 +42,7 @@ public: } void render(Display* dpy) const override { - if (F_LIKELY(m_valid)) { + if (F_LIKELY(m_valid && !m_forced)) { const uint8_t decayedBrightness = scale8((uint8_t)m_brightness, m_useBPM ? ease8InOutCubic((uint8_t)m_beatDecay) : 255); const uint8_t clippedBrightness = std::min(decayedBrightness, (uint8_t)255); const uint8_t scaledBrightness = scale8(m_powerState, clippedBrightness); @@ -50,6 +52,9 @@ public: } } + void forceBrightness(uint8_t v); + + const std::vector& commands() const override; private: AnimatedNumber m_powerState = 255; @@ -59,4 +64,5 @@ private: uint16_t m_milliamps = 500; bool m_valid = true; bool m_useBPM = false; + bool m_forced = false; };