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 "./Display.h"
#include "./MainLoop.h"
#include <ArduinoLog.h>
@ -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<Command>&

View File

@ -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;