From 3dd84cfce10be048dbccb731d8ce9991b6989e02 Mon Sep 17 00:00:00 2001 From: Torrie Fischer Date: Sat, 23 Dec 2023 11:06:50 +0100 Subject: [PATCH] figments: renderer: fix brightness scaling to actually work --- lib/Figments/Renderer.cpp | 28 ++++++++++++++++++---------- lib/Figments/Renderer.h | 4 ++-- 2 files changed, 20 insertions(+), 12 deletions(-) diff --git a/lib/Figments/Renderer.cpp b/lib/Figments/Renderer.cpp index ee057db..112dc25 100644 --- a/lib/Figments/Renderer.cpp +++ b/lib/Figments/Renderer.cpp @@ -1,5 +1,6 @@ #include "./Renderer.h" #include "./Display.h" +#include "./MainLoop.h" #include @@ -18,9 +19,6 @@ Renderer::lastFigmentName() void Renderer::loop() { - m_powerState.update(); - m_brightness.update(); - uint16_t totalPower = 0; for(Display* dpy : m_displays) { totalPower += calculate_unscaled_power_mW(dpy->pixelBacking(), dpy->pixelCount()); @@ -43,8 +41,16 @@ Renderer::loop() } }; } - const uint8_t videoBrightness = brighten8_video(m_powerState); - FastLED.show(powerScale(videoBrightness, totalPower)); + static uint8_t videoBrightness = 255; + static uint8_t scaledBrightness = powerScale(videoBrightness, totalPower); + EVERY_N_MILLISECONDS(30) { + m_powerState.update(); + m_brightness.update(); + videoBrightness = brighten8_video(min((uint8_t)m_brightness, (uint8_t)m_powerState)); + scaledBrightness = powerScale(videoBrightness, totalPower); + } + + FastLED.show(scaledBrightness); FastLED.countFPS(); } @@ -62,6 +68,7 @@ Renderer::handleEvent(const InputEvent& evt) break; case InputEvent::SetBrightness: m_brightness = evt.asInt(); + Log.notice("Brightness is now %d (%d requested)", (uint8_t)m_brightness, evt.asInt()); break; } } @@ -70,9 +77,10 @@ uint8_t Renderer::powerScale(uint8_t target, uint32_t totalPower) const { if (m_powerManaged) { + const uint32_t maxPower = m_voltage * m_milliamps; uint32_t requested = ((uint32_t)totalPower * target) / 256; - if (requested > totalPower) { - return (uint32_t)((uint8_t)(target) * (uint32_t)(totalPower)) / ((uint32_t)(requested)); + if (requested > maxPower) { + return (uint32_t)((uint8_t)(target) * (uint32_t)(maxPower)) / ((uint32_t)(requested)); } } @@ -91,19 +99,19 @@ Renderer::onStart() void Renderer::doOn(Args& args, Print& print) { - m_powerState = 100; + MainLoop::instance()->dispatch(InputEvent{InputEvent::SetPower, 255}); } void Renderer::doOff(Args& args, Print& print) { - m_powerState = 0; + MainLoop::instance()->dispatch(InputEvent{InputEvent::SetPower, 0}); } void Renderer::doBrightness(Args& args, Print& print) { - m_brightness = atoi(args[1].c_str()); + MainLoop::instance()->dispatch(InputEvent{InputEvent::SetBrightness, atoi(args[1].c_str())}); } const std::vector& diff --git a/lib/Figments/Renderer.h b/lib/Figments/Renderer.h index cf87877..f5c6cf8 100644 --- a/lib/Figments/Renderer.h +++ b/lib/Figments/Renderer.h @@ -25,8 +25,8 @@ private: AnimatedNumber m_brightness = 255; bool m_powerManaged = true; - uint8_t m_voltage; - uint8_t m_milliamps; + uint16_t m_voltage = 5; + uint16_t m_milliamps = 500; uint8_t powerScale(uint8_t target, uint32_t totalPower) const;