From d824dbfa456f02f8679945cfcf7526c0364c50bb Mon Sep 17 00:00:00 2001 From: Torrie Fischer Date: Sun, 19 Feb 2023 18:45:28 +0100 Subject: [PATCH] animations: power: make configurable --- src/animations/Power.cpp | 30 ++++++++++++++++++++++++++++++ src/animations/Power.h | 29 ++++++++++++++++++----------- src/main.cpp | 16 ---------------- 3 files changed, 48 insertions(+), 27 deletions(-) diff --git a/src/animations/Power.cpp b/src/animations/Power.cpp index e69de29..24a0044 100644 --- a/src/animations/Power.cpp +++ b/src/animations/Power.cpp @@ -0,0 +1,30 @@ +#include "./Power.h" +#include "../Static.h" +#include + +void +Power::handleConfigChange(const InputEvent& event) +{ + const JsonObject& cfg = *event.as(); + if (cfg.containsKey("power.milliamps")) { + m_milliamps = cfg["power.milliamps"]; + } + if (cfg.containsKey("power.volts")) { + m_voltage = cfg["power.volts"]; + } + if (cfg.containsKey("power.useBPM")) { + m_useBPM = cfg["power.useBPM"]; + } + if (m_voltage == 0 || m_milliamps == 0) { + Log.notice("power: Impossible power config: %dma @ %dv", m_milliamps, m_voltage); + m_valid = false; + } else { + Log.notice("power: Configured to use %dma @ %dv", m_milliamps, m_voltage); + m_valid = true; + FastLED.setMaxPowerInVoltsAndMilliamps(m_voltage, m_milliamps); + } +} + + +STATIC_ALLOC(Power); +STATIC_TASK(Power); diff --git a/src/animations/Power.h b/src/animations/Power.h index b1c404c..f05cd51 100644 --- a/src/animations/Power.h +++ b/src/animations/Power.h @@ -1,8 +1,7 @@ #pragma once #include -template -class Power: public Figment { +class Power: public Figment, ConfigTaskMixin { public: Power() : Figment("Power") {state = Task::Running;} @@ -24,11 +23,14 @@ public: m_beatDecay.set(0, 255); break; default: - return; + ConfigTaskMixin::handleEvent(evt); } } + void handleConfigChange(const InputEvent& event) override; + void loop() override { + ConfigTaskMixin::loop(); m_powerState.update(); m_brightness.update(); EVERY_N_MILLISECONDS(20) { @@ -37,18 +39,23 @@ public: } void render(Display* dpy) const override { - const uint8_t decayedBrightness = scale8((uint8_t)m_brightness, ease8InOutCubic((uint8_t)m_beatDecay)); - const uint8_t clippedBrightness = std::min(decayedBrightness, MaxBrightness); - const uint8_t scaledBrightness = scale8(m_powerState, clippedBrightness); - const uint8_t videoBrightness = brighten8_video(scaledBrightness); - const uint8_t powerBrightness = calculate_max_brightness_for_power_mW(videoBrightness, Watts); - FastLED.setBrightness(powerBrightness); + if (F_LIKELY(m_valid)) { + 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); + const uint8_t videoBrightness = brighten8_video(scaledBrightness); + const uint8_t powerBrightness = calculate_max_brightness_for_power_mW(videoBrightness, m_voltage * m_milliamps); + FastLED.setBrightness(powerBrightness); + } } - static constexpr uint32_t Watts = Voltage * MaxMilliAmps; private: AnimatedNumber m_powerState = 255; - AnimatedNumber m_brightness = MaxBrightness; + AnimatedNumber m_brightness = 255; AnimatedNumber m_beatDecay = 255; + uint8_t m_voltage = 5; + uint16_t m_milliamps = 500; + bool m_valid = true; + bool m_useBPM = false; }; diff --git a/src/main.cpp b/src/main.cpp index 66e2473..d774f9c 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -13,30 +13,14 @@ #include -#include "animations/Power.h" - #include "inputs/ColorCycle.h" #include "inputs/Buttons.h" #include "SafeMode.h" -#define MAX_BRIGHTNESS 255 -//#define PSU_MILLIAMPS 4800 -//#define PSU_MILLIAMPS 500 -//#define PSU_MILLIAMPS 1000 -#define PSU_MILLIAMPS 1000 - -// Enable system thread, so rendering happens while booting -//SYSTEM_THREAD(ENABLED); - - // Setup FastLED and the display CRGB leds[HardwareConfig::MAX_LED_NUM]; Display dpy(leds, HardwareConfig::MAX_LED_NUM, Static::instance()->coordMap()); -// Setup power management -Power power; -REGISTER_TASK(power); - // FIXME: rewrite as static task /*FigmentFunc configDisplay([](Display* dpy) { uint8_t brightness = brighten8_video(beatsin8(60));