renderbug-cpp/firmware/platform/particle/PhotonTelemetry.cpp

166 lines
5.5 KiB
C++
Raw Normal View History

2019-05-10 05:17:29 +00:00
#include "PhotonTelemetry.h"
2021-03-28 01:19:55 +00:00
#include "../../Static.h"
2019-05-10 05:17:29 +00:00
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);
2021-03-28 01:19:55 +00:00
Particle.variable("localip", m_localIP);
2019-05-10 05:17:29 +00:00
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) {
2021-03-28 01:19:55 +00:00
m_localIP = WiFi.localIP().toString();
2019-05-10 05:17:29 +00:00
char valueBuf[255];
2021-03-28 01:19:55 +00:00
snprintf(valueBuf, sizeof(valueBuf), "{\"fps\": %lu, \"localip\": \"%s\"}", m_fps, m_localIP.c_str());
2019-05-10 05:17:29 +00:00
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)
{
2021-03-28 01:19:55 +00:00
Serial.flush();
2019-05-10 05:17:29 +00:00
if (evt.intent == InputEvent::NetworkStatus) {
onConnected();
}
if (evt.intent != InputEvent::None) {
const char* sourceName;
switch(evt.intent) {
case InputEvent::PowerToggle:
sourceName = "power-toggle";
break;
case InputEvent::SetPower:
sourceName = "set-power";
break;
case InputEvent::PreviousPattern:
sourceName = "previous-pattern";
break;
case InputEvent::NextPattern:
sourceName = "next-pattern";
break;
case InputEvent::SetPattern:
sourceName = "set-pattern";
break;
case InputEvent::SetColor:
sourceName = "set-color";
break;
case InputEvent::Acceleration:
sourceName = "acceleration";
break;
case InputEvent::UserInput:
sourceName = "user";
break;
case InputEvent::SetBrightness:
sourceName = "set-brightness";
break;
case InputEvent::FirmwareUpdate:
sourceName = "firmware-update";
break;
case InputEvent::NetworkStatus:
sourceName = "network-status";
break;
2021-03-28 01:19:55 +00:00
case InputEvent::NetworkActivity:
sourceName = "network-activity";
break;
2019-05-10 05:17:29 +00:00
case InputEvent::StartThing:
sourceName = "start-thing";
break;
case InputEvent::StopThing:
sourceName = "stop-thing";
break;
2021-03-28 01:19:55 +00:00
case InputEvent::SetDisplayOffset:
sourceName = "set-display-offset";
break;
case InputEvent::SetDisplayLength:
sourceName = "set-display-length";
break;
case InputEvent::SaveConfigurationRequest:
sourceName = "save-configuration";
break;
2019-05-10 05:17:29 +00:00
default:
2021-03-28 01:19:55 +00:00
sourceName = 0;
2019-05-10 05:17:29 +00:00
break;
}
char valueBuf[255];
switch(evt.type) {
case InputEvent::Null:
snprintf(valueBuf, sizeof(valueBuf), "null");break;
case InputEvent::Integer:
2021-03-28 01:19:55 +00:00
snprintf(valueBuf, sizeof(valueBuf), "%d %02x", evt.asInt(), evt.asInt());break;
2019-05-10 05:17:29 +00:00
case InputEvent::String:
snprintf(valueBuf, sizeof(valueBuf), "\"%s\"", evt.asString());break;
case InputEvent::Color:
snprintf(valueBuf, sizeof(valueBuf), "[%d, %d, %d]", evt.asRGB().r, evt.asRGB().g, evt.asRGB().b);break;
}
2021-03-28 01:19:55 +00:00
char buf[255 * 2];
if (sourceName == 0) {
snprintf(buf, sizeof(buf), "{\"intent\": %d, \"value\": %s}", evt.intent, valueBuf);
} else {
snprintf(buf, sizeof(buf), "{\"intent\": \"%s\", \"value\": %s}", sourceName, valueBuf);
}
2019-05-10 05:17:29 +00:00
if (m_online) {
if (evt.intent != m_lastEvent.intent) {
if (m_duplicateEvents > 0) {
2021-03-28 01:19:55 +00:00
Log.info("Suppressed reporting %ld duplicate events.", m_duplicateEvents);
}
Log.info("Event: %s", buf);
m_duplicateEvents = 0;
m_lastEvent = evt;
Particle.publish("renderbug/event", buf, PRIVATE);
} else {
m_duplicateEvents++;
}
2019-05-10 05:17:29 +00:00
} else {
Log.info("[offline] Event: %s", buf);
}
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;
}
}
}
2021-03-28 01:19:55 +00:00
STATIC_ALLOC(PhotonTelemetry);