update
This commit is contained in:
165
firmware/platform/particle/PhotonTelemetry.cpp
Normal file
165
firmware/platform/particle/PhotonTelemetry.cpp
Normal file
@ -0,0 +1,165 @@
|
||||
#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)
|
||||
{
|
||||
Serial.flush();
|
||||
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;
|
||||
case InputEvent::NetworkActivity:
|
||||
sourceName = "network-activity";
|
||||
break;
|
||||
case InputEvent::StartThing:
|
||||
sourceName = "start-thing";
|
||||
break;
|
||||
case InputEvent::StopThing:
|
||||
sourceName = "stop-thing";
|
||||
break;
|
||||
case InputEvent::SetDisplayOffset:
|
||||
sourceName = "set-display-offset";
|
||||
break;
|
||||
case InputEvent::SetDisplayLength:
|
||||
sourceName = "set-display-length";
|
||||
break;
|
||||
case InputEvent::SaveConfigurationRequest:
|
||||
sourceName = "save-configuration";
|
||||
break;
|
||||
default:
|
||||
sourceName = 0;
|
||||
break;
|
||||
}
|
||||
char valueBuf[255];
|
||||
switch(evt.type) {
|
||||
case InputEvent::Null:
|
||||
snprintf(valueBuf, sizeof(valueBuf), "null");break;
|
||||
case InputEvent::Integer:
|
||||
snprintf(valueBuf, sizeof(valueBuf), "%d %02x", evt.asInt(), evt.asInt());break;
|
||||
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;
|
||||
}
|
||||
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);
|
||||
}
|
||||
if (m_online) {
|
||||
if (evt.intent != m_lastEvent.intent) {
|
||||
if (m_duplicateEvents > 0) {
|
||||
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++;
|
||||
}
|
||||
} 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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
STATIC_ALLOC(PhotonTelemetry);
|
25
firmware/platform/particle/PhotonTelemetry.h
Normal file
25
firmware/platform/particle/PhotonTelemetry.h
Normal file
@ -0,0 +1,25 @@
|
||||
#pragma once
|
||||
#include "./Figments/Figments.h"
|
||||
|
||||
class PhotonTelemetry: public Task {
|
||||
public:
|
||||
PhotonTelemetry();
|
||||
void loop() override;
|
||||
void handleEvent(const InputEvent& evt) override;
|
||||
|
||||
private:
|
||||
void onConnected();
|
||||
|
||||
int m_frameIdx;
|
||||
String m_serviceList;
|
||||
String m_localIP;
|
||||
uint32_t m_currentBrightness;
|
||||
LEDStatus m_ledStatus = LEDStatus(0, LED_PATTERN_FADE, LED_SPEED_FAST);
|
||||
uint32_t m_rgbPulseFrame = 0;
|
||||
uint32_t m_pixelCount = 0;
|
||||
uint32_t m_startPixel = 0;
|
||||
uint32_t m_fps = 0;
|
||||
uint32_t m_duplicateEvents = 0;
|
||||
InputEvent m_lastEvent;
|
||||
bool m_online = false;
|
||||
};
|
28
firmware/platform/particle/inputs/CloudStatus.cpp
Normal file
28
firmware/platform/particle/inputs/CloudStatus.cpp
Normal file
@ -0,0 +1,28 @@
|
||||
#include "CloudStatus.h"
|
||||
#include "../../../Static.h"
|
||||
|
||||
void
|
||||
CloudStatus::onStart()
|
||||
{
|
||||
SINGLE_THREADED_BLOCK() {
|
||||
if (Particle.connected()) {
|
||||
initNetwork(0, cloud_status_connected);
|
||||
} else {
|
||||
System.on(cloud_status, &CloudStatus::initNetwork);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
CloudStatus::initNetwork(system_event_t event, int param) {
|
||||
if (param == cloud_status_connected) {
|
||||
Log.info("Connected to T H E C L O U D");
|
||||
MainLoop::instance()->dispatch(InputEvent{InputEvent::NetworkStatus, true});
|
||||
} else if (param == cloud_status_disconnected) {
|
||||
Log.info("Lost cloud connection!!");
|
||||
MainLoop::instance()->dispatch(InputEvent{InputEvent::NetworkStatus, false});
|
||||
}
|
||||
}
|
||||
|
||||
STATIC_ALLOC(CloudStatus);
|
11
firmware/platform/particle/inputs/CloudStatus.h
Normal file
11
firmware/platform/particle/inputs/CloudStatus.h
Normal file
@ -0,0 +1,11 @@
|
||||
#include "../../../Figments/Figments.h"
|
||||
|
||||
class CloudStatus: public Task {
|
||||
public:
|
||||
CloudStatus() : Task("Cloud") {}
|
||||
void loop() override {}
|
||||
void onStart() override;
|
||||
|
||||
private:
|
||||
static void initNetwork(system_event_t event, int param);
|
||||
};
|
144
firmware/platform/particle/inputs/Photon.cpp
Normal file
144
firmware/platform/particle/inputs/Photon.cpp
Normal file
@ -0,0 +1,144 @@
|
||||
#include "Particle.h"
|
||||
#include "../../../Figments/Figments.h"
|
||||
#include "../../../colors.h"
|
||||
#include "../../../Static.h"
|
||||
#include "./Photon.h"
|
||||
|
||||
void
|
||||
PhotonInput::onConnected()
|
||||
{
|
||||
Log.info("Connecting photon input...");
|
||||
Particle.function("save", &PhotonInput::save, this);
|
||||
Particle.function("power", &PhotonInput::setPower, this);
|
||||
Particle.function("next", &PhotonInput::nextPattern, this);
|
||||
Particle.function("input", &PhotonInput::input, this);
|
||||
Particle.function("previous", &PhotonInput::previousPattern, this);
|
||||
Particle.function("pattern", &PhotonInput::setPattern, this);
|
||||
Particle.function("setHue", &PhotonInput::setHue, this);
|
||||
Particle.function("brightness", &PhotonInput::setBrightness, this);
|
||||
Particle.function("reboot", &PhotonInput::reboot, this);
|
||||
Particle.function("start", &PhotonInput::startThing, this);
|
||||
Particle.function("stop", &PhotonInput::stopThing, this);
|
||||
}
|
||||
|
||||
int
|
||||
PhotonInput::startThing(String command)
|
||||
{
|
||||
command.toCharArray(m_commandBuf, sizeof(m_commandBuf));
|
||||
setEvent(InputEvent(InputEvent::StartThing, m_commandBuf));
|
||||
return 0;
|
||||
}
|
||||
|
||||
int
|
||||
PhotonInput::stopThing(String command)
|
||||
{
|
||||
command.toCharArray(m_commandBuf, sizeof(m_commandBuf));
|
||||
setEvent(InputEvent(InputEvent::StopThing, m_commandBuf));
|
||||
return 0;
|
||||
}
|
||||
|
||||
void
|
||||
PhotonInput::onStart()
|
||||
{
|
||||
System.on(firmware_update, &PhotonInput::onFirmwareUpdate);
|
||||
System.on(button_click, &PhotonInput::onButtonClick);
|
||||
System.on(reset, &PhotonInput::onReset);
|
||||
}
|
||||
|
||||
void
|
||||
PhotonInput::handleEvent(const InputEvent &evt)
|
||||
{
|
||||
if (evt.intent == InputEvent::NetworkStatus) {
|
||||
onConnected();
|
||||
}
|
||||
}
|
||||
|
||||
int
|
||||
PhotonInput::reboot(String command)
|
||||
{
|
||||
System.reset();
|
||||
return 0;
|
||||
}
|
||||
|
||||
void
|
||||
PhotonInput::onReset(system_event_t event, int param)
|
||||
{
|
||||
NSFastLED::FastLED.clear();
|
||||
}
|
||||
|
||||
void
|
||||
PhotonInput::onButtonClick(system_event_t event, int param)
|
||||
{
|
||||
Static<PhotonInput>::instance()->setEvent(InputEvent{InputEvent::NextPattern, param});
|
||||
}
|
||||
|
||||
void
|
||||
PhotonInput::onFirmwareUpdate(system_event_t event, int param)
|
||||
{
|
||||
Static<PhotonInput>::instance()->setEvent(InputEvent{InputEvent::FirmwareUpdate, param});
|
||||
}
|
||||
|
||||
int
|
||||
PhotonInput::input(String command)
|
||||
{
|
||||
command.toCharArray(m_commandBuf, sizeof(m_commandBuf));
|
||||
setEvent(InputEvent(InputEvent::UserInput, m_commandBuf));
|
||||
return 0;
|
||||
}
|
||||
|
||||
int
|
||||
PhotonInput::setPattern(String command)
|
||||
{
|
||||
command.toCharArray(m_commandBuf, sizeof(m_commandBuf));
|
||||
setEvent(InputEvent(InputEvent::SetPattern, m_commandBuf));
|
||||
return 0;
|
||||
}
|
||||
|
||||
int
|
||||
PhotonInput::setHue(String colorName)
|
||||
{
|
||||
ColorInfo nextColor = colorForName(colorName);
|
||||
setEvent(InputEvent(InputEvent::SetColor, nextColor.rgb));
|
||||
return 0;
|
||||
}
|
||||
|
||||
int
|
||||
PhotonInput::nextPattern(String command)
|
||||
{
|
||||
setEvent(InputEvent(InputEvent::NextPattern));
|
||||
return 0;
|
||||
}
|
||||
|
||||
int
|
||||
PhotonInput::previousPattern(String command)
|
||||
{
|
||||
setEvent(InputEvent(InputEvent::PreviousPattern));
|
||||
return 0;
|
||||
}
|
||||
|
||||
int
|
||||
PhotonInput::save(String command) {
|
||||
setEvent(InputEvent::SaveConfigurationRequest);
|
||||
}
|
||||
|
||||
int
|
||||
PhotonInput::setPower(String command)
|
||||
{
|
||||
if (command == "off") {
|
||||
setEvent(InputEvent(InputEvent::SetPower, 0));
|
||||
} else if (command == "on") {
|
||||
setEvent(InputEvent(InputEvent::SetPower, 1));
|
||||
} else {
|
||||
setEvent(InputEvent(InputEvent::PowerToggle));
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
int
|
||||
PhotonInput::setBrightness(String command)
|
||||
{
|
||||
setEvent(InputEvent(InputEvent::SetBrightness, command.toInt()));
|
||||
return 0;
|
||||
}
|
||||
|
||||
STATIC_ALLOC(PhotonInput);
|
30
firmware/platform/particle/inputs/Photon.h
Normal file
30
firmware/platform/particle/inputs/Photon.h
Normal file
@ -0,0 +1,30 @@
|
||||
#include "Particle.h"
|
||||
#include "../../../Figments/Figments.h"
|
||||
|
||||
class PhotonInput: public BufferedInputSource {
|
||||
public:
|
||||
PhotonInput() : BufferedInputSource("PhotonInput") {}
|
||||
void onStart() override;
|
||||
void handleEvent(const InputEvent &evt) override;
|
||||
|
||||
private:
|
||||
char m_commandBuf[16];
|
||||
|
||||
void onConnected();
|
||||
int reboot(String command);
|
||||
int input(String command);
|
||||
int setPattern(String command);
|
||||
int setHue(String colorName);
|
||||
int nextPattern(String command);
|
||||
int previousPattern(String command);
|
||||
int setPower(String command);
|
||||
int setBrightness(String command);
|
||||
int save(String command);
|
||||
|
||||
int startThing(String command);
|
||||
int stopThing(String command);
|
||||
|
||||
static void onReset(system_event_t event, int param);
|
||||
static void onButtonClick(system_event_t event, int param);
|
||||
static void onFirmwareUpdate(system_event_t event, int param);
|
||||
};
|
Reference in New Issue
Block a user