animations: power: make configurable

This commit is contained in:
Torrie Fischer 2023-02-19 18:45:28 +01:00
parent 8223688d7b
commit d824dbfa45
3 changed files with 48 additions and 27 deletions

View File

@ -0,0 +1,30 @@
#include "./Power.h"
#include "../Static.h"
#include <ArduinoJson.h>
void
Power::handleConfigChange(const InputEvent& event)
{
const JsonObject& cfg = *event.as<JsonObject>();
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);

View File

@ -1,8 +1,7 @@
#pragma once
#include <Figments.h>
template<uint8_t MaxBrightness = 255, uint32_t MaxMilliAmps = 500, uint32_t Voltage = 5>
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);
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, Watts);
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;
};

View File

@ -13,30 +13,14 @@
#include <time.h>
#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<ConfigService>::instance()->coordMap());
// Setup power management
Power<MAX_BRIGHTNESS, PSU_MILLIAMPS> power;
REGISTER_TASK(power);
// FIXME: rewrite as static task
/*FigmentFunc configDisplay([](Display* dpy) {
uint8_t brightness = brighten8_video(beatsin8(60));