Files
renderbug-cpp/src/platform/particle/PhotonTelemetry.cpp

70 lines
2.0 KiB
C++

#include "PhotonTelemetry.h"
#include "../../Static.h"
using namespace NSFastLED;
PhotonTelemetry::PhotonTelemetry() : Task("PhotonTelemetry") {}
void
PhotonTelemetry::onConnected()
{
Log.info("Connecting photon telemetry...");
Particle.variable("frame", m_frameIdx);
Particle.variable("brightness", m_currentBrightness);
Particle.variable("fps", m_fps);
Particle.variable("services", m_serviceList);
Particle.variable("localip", m_localIP);
m_online = true;
}
void
PhotonTelemetry::loop()
{
m_frameIdx++;
if (m_rgbPulseFrame == 1) {
m_ledStatus.setActive(false);
} else if (m_rgbPulseFrame > 0) {
m_rgbPulseFrame--;
}
m_currentBrightness = NSFastLED::FastLED.getBrightness();
NSFastLED::FastLED.countFPS();
m_fps = NSFastLED::FastLED.getFPS();
if (m_online) {
EVERY_N_SECONDS(30) {
m_localIP = WiFi.localIP().toString();
char valueBuf[255];
snprintf(valueBuf, sizeof(valueBuf), "{\"fps\": %lu, \"localip\": \"%s\"}", m_fps, m_localIP.c_str());
Log.info("Heartbeat: %s", valueBuf);
Particle.publish("renderbug/heartbeat", valueBuf);
auto sched = MainLoop::instance()->scheduler;
m_serviceList = String{};
for(auto task : sched.tasks) {
m_serviceList.concat(task->name);
m_serviceList.concat(':');
if (task->state == Task::Running) {
m_serviceList.concat(1);
} else {
m_serviceList.concat(0);
}
m_serviceList.concat(',');
}
}
}
}
void
PhotonTelemetry::handleEvent(const InputEvent &evt)
{
if (evt.intent == InputEvent::SetColor) {
NSFastLED::CRGB rgb {evt.asRGB()};
uint32_t color = (rgb.r << 16) + (rgb.g << 8) + (rgb.b << 0);
m_ledStatus.setColor(color);
m_ledStatus.setActive(true);
m_rgbPulseFrame = 1000;
}
}
STATIC_ALLOC(PhotonTelemetry);