figments: renderer: fix brightness scaling to actually work

This commit is contained in:
Torrie Fischer 2023-12-23 11:06:50 +01:00
parent 3e9d4eb08f
commit 3dd84cfce1
2 changed files with 20 additions and 12 deletions

View File

@ -1,5 +1,6 @@
#include "./Renderer.h" #include "./Renderer.h"
#include "./Display.h" #include "./Display.h"
#include "./MainLoop.h"
#include <ArduinoLog.h> #include <ArduinoLog.h>
@ -18,9 +19,6 @@ Renderer::lastFigmentName()
void void
Renderer::loop() Renderer::loop()
{ {
m_powerState.update();
m_brightness.update();
uint16_t totalPower = 0; uint16_t totalPower = 0;
for(Display* dpy : m_displays) { for(Display* dpy : m_displays) {
totalPower += calculate_unscaled_power_mW(dpy->pixelBacking(), dpy->pixelCount()); totalPower += calculate_unscaled_power_mW(dpy->pixelBacking(), dpy->pixelCount());
@ -43,8 +41,16 @@ Renderer::loop()
} }
}; };
} }
const uint8_t videoBrightness = brighten8_video(m_powerState); static uint8_t videoBrightness = 255;
FastLED.show(powerScale(videoBrightness, totalPower)); 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(); FastLED.countFPS();
} }
@ -62,6 +68,7 @@ Renderer::handleEvent(const InputEvent& evt)
break; break;
case InputEvent::SetBrightness: case InputEvent::SetBrightness:
m_brightness = evt.asInt(); m_brightness = evt.asInt();
Log.notice("Brightness is now %d (%d requested)", (uint8_t)m_brightness, evt.asInt());
break; break;
} }
} }
@ -70,9 +77,10 @@ uint8_t
Renderer::powerScale(uint8_t target, uint32_t totalPower) const Renderer::powerScale(uint8_t target, uint32_t totalPower) const
{ {
if (m_powerManaged) { if (m_powerManaged) {
const uint32_t maxPower = m_voltage * m_milliamps;
uint32_t requested = ((uint32_t)totalPower * target) / 256; uint32_t requested = ((uint32_t)totalPower * target) / 256;
if (requested > totalPower) { if (requested > maxPower) {
return (uint32_t)((uint8_t)(target) * (uint32_t)(totalPower)) / ((uint32_t)(requested)); return (uint32_t)((uint8_t)(target) * (uint32_t)(maxPower)) / ((uint32_t)(requested));
} }
} }
@ -91,19 +99,19 @@ Renderer::onStart()
void void
Renderer::doOn(Args& args, Print& print) Renderer::doOn(Args& args, Print& print)
{ {
m_powerState = 100; MainLoop::instance()->dispatch(InputEvent{InputEvent::SetPower, 255});
} }
void void
Renderer::doOff(Args& args, Print& print) Renderer::doOff(Args& args, Print& print)
{ {
m_powerState = 0; MainLoop::instance()->dispatch(InputEvent{InputEvent::SetPower, 0});
} }
void void
Renderer::doBrightness(Args& args, Print& print) 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<Command>& const std::vector<Command>&

View File

@ -25,8 +25,8 @@ private:
AnimatedNumber m_brightness = 255; AnimatedNumber m_brightness = 255;
bool m_powerManaged = true; bool m_powerManaged = true;
uint8_t m_voltage; uint16_t m_voltage = 5;
uint8_t m_milliamps; uint16_t m_milliamps = 500;
uint8_t powerScale(uint8_t target, uint32_t totalPower) const; uint8_t powerScale(uint8_t target, uint32_t totalPower) const;