renderbug-cpp/lib/Figments/Renderer.cpp

114 lines
2.7 KiB
C++
Raw Normal View History

2019-05-10 05:17:29 +00:00
#include "./Renderer.h"
#include "./Display.h"
2021-03-29 08:10:55 +00:00
#include <ArduinoLog.h>
#ifndef __NOINIT_ATTR // Pre-defined on esp32
#define __NOINIT_ATTR __attribute__ ((section (".noinit")))
#endif
__NOINIT_ATTR const char* s_lastFigmentName;
const char*
Renderer::lastFigmentName()
{
return s_lastFigmentName;
}
2019-05-10 05:17:29 +00:00
void
Renderer::loop()
{
m_powerState.update();
m_brightness.update();
uint16_t totalPower = 0;
2019-05-10 05:17:29 +00:00
for(Display* dpy : m_displays) {
totalPower += calculate_unscaled_power_mW(dpy->pixelBacking(), dpy->pixelCount());
2019-05-10 05:17:29 +00:00
for(Figment* figment : m_figments) {
if (figment->state == Task::Running) {
#if defined(BOARD_ESP32) or defined(BOARD_ESP8266)
2021-04-10 18:10:25 +00:00
unsigned int frameStart = ESP.getCycleCount();
#endif
Log.verbose("Render %s", figment->name);
s_lastFigmentName = figment->name;
2019-05-10 05:17:29 +00:00
figment->render(dpy);
#if defined(BOARD_ESP32) or defined(BOARD_ESP8266)
2021-04-10 18:10:25 +00:00
unsigned int runtime = (ESP.getCycleCount() - frameStart) / 160000;
#else
unsigned int runtime = 0;
#endif
2021-04-10 18:10:25 +00:00
if (runtime >= 8) {
Log.warning("SLOW RENDER: %s took %dms!", figment->name, runtime);
2021-04-10 18:10:25 +00:00
}
2019-05-10 05:17:29 +00:00
}
};
}
const uint8_t videoBrightness = brighten8_video(m_powerState);
FastLED.show(powerScale(videoBrightness, totalPower));
2021-03-29 08:10:55 +00:00
FastLED.countFPS();
2019-05-10 05:17:29 +00:00
}
void
Renderer::handleEvent(const InputEvent& evt)
{
switch (evt.intent) {
case InputEvent::PowerToggle:
m_powerState = m_powerState.value() <= 128 ? 255 : 0;
Log.notice("Power toggled to %t", m_powerState);
break;
case InputEvent::SetPower:
m_powerState = evt.asInt() == 0 ? 0 : 255;
Log.notice("Power state is now %t", m_powerState);
break;
case InputEvent::SetBrightness:
m_brightness = evt.asInt();
break;
}
}
uint8_t
Renderer::powerScale(uint8_t target, uint32_t totalPower) const
{
if (m_powerManaged) {
uint32_t requested = ((uint32_t)totalPower * target) / 256;
if (requested > totalPower) {
return (uint32_t)((uint8_t)(target) * (uint32_t)(totalPower)) / ((uint32_t)(requested));
}
}
return target;
}
2019-05-10 05:17:29 +00:00
void
Renderer::onStart()
{
for(Display* dpy : m_displays) {
dpy->clear();
}
2021-03-29 08:10:55 +00:00
FastLED.show();
2019-05-10 05:17:29 +00:00
}
void
Renderer::doOn(Args& args, Print& print)
{}
void
Renderer::doOff(Args& args, Print& print)
{}
void
Renderer::doBrightness(Args& args, Print& print)
{}
const std::vector<Command>&
Renderer::commands() const
{
static const std::vector<Command> _commands = {
Command{"brightness", &Renderer::doBrightness},
Command{"on", &Renderer::doOn},
Command{"off", &Renderer::doOff}
};
return _commands;
}