build: clean up the mess of ifdefs from platform into a scons-configured hal
This commit is contained in:
@@ -31,7 +31,7 @@ const MQTTDevice Device{
|
||||
Platform::deviceID(),
|
||||
Platform::deviceName(),
|
||||
Platform::model(),
|
||||
#ifdef BOARD_ESP8266
|
||||
#ifdef ESP8266
|
||||
ESP.getSketchMD5(),
|
||||
#else
|
||||
"",
|
||||
@@ -95,10 +95,6 @@ const MQTTEntity Lightswitch {
|
||||
"light", Device, "lightswitch"
|
||||
};
|
||||
|
||||
const MQTTEntity flashlightSwitch {
|
||||
"switch", Device, "flashlight"
|
||||
};
|
||||
|
||||
const MQTTEntity FPSSensor {
|
||||
"sensor", Device, "fps"
|
||||
};
|
||||
@@ -124,27 +120,28 @@ MQTTTelemetry::handleEventOnline(const InputEvent& evt)
|
||||
|
||||
m_json.clear();
|
||||
Lightswitch.toJson(m_json);
|
||||
|
||||
int i = 0;
|
||||
for(const Sequencer::Scene& scene : m_sequencer->scenes()) {
|
||||
m_json["fx_list"][i++] = scene.name;
|
||||
}
|
||||
m_json["brightness"] = true;
|
||||
m_json["rgb"] = true;
|
||||
|
||||
publishDoc(Lightswitch.configTopic().c_str(), true);
|
||||
|
||||
//Log.verbose("Publish %s %s", Lightswitch.configTopic().c_str(), buf);
|
||||
//m_mqtt.publish(Lightswitch.configTopic().c_str(), (uint8_t*)buf, strlen(buf), true);
|
||||
m_mqtt.subscribe(Lightswitch.commandTopic().c_str());
|
||||
|
||||
m_json.clear();
|
||||
flashlightSwitch.toJson(m_json, false);
|
||||
m_json["cmd_t"] = "~/set";
|
||||
m_json["ret"] = true;
|
||||
publishDoc(flashlightSwitch.configTopic().c_str(), true);
|
||||
//m_mqtt.publish(flashlightSwitch.configTopic().c_str(), (uint8_t*)buf, strlen(buf), true);
|
||||
m_mqtt.subscribe(flashlightSwitch.commandTopic().c_str());
|
||||
for(auto scene : Static<Sequencer>::instance()->scenes()) {
|
||||
m_json.clear();
|
||||
String strName{scene.name};
|
||||
MQTTEntity sceneObj {
|
||||
"scene",
|
||||
Device,
|
||||
strName
|
||||
};
|
||||
sceneObj.toJson(m_json, false);
|
||||
m_json["cmd_t"] = "~/set";
|
||||
m_json["ret"] = false;
|
||||
m_json["payload_on"] = "active";
|
||||
publishDoc(sceneObj.configTopic().c_str(), false);
|
||||
m_mqtt.subscribe(sceneObj.commandTopic().c_str());
|
||||
Log.info("Published scene %s", sceneObj.configTopic().c_str());
|
||||
}
|
||||
|
||||
m_json.clear();
|
||||
FPSSensor.toJson(m_json, false);
|
||||
@@ -168,13 +165,7 @@ MQTTTelemetry::handleEventOnline(const InputEvent& evt)
|
||||
}
|
||||
} else {
|
||||
String statTopic = Lightswitch.statTopic();
|
||||
if (evt.intent == InputEvent::StopThing && String(evt.asString()) == "Flashlight") {
|
||||
String flashlightStatTopic = flashlightSwitch.statTopic();
|
||||
m_mqtt.publish(flashlightStatTopic.c_str(), "OFF");
|
||||
} else if (evt.intent == InputEvent::StartThing && String(evt.asString()) == "Flashlight") {
|
||||
String flashlightStatTopic = flashlightSwitch.statTopic();
|
||||
m_mqtt.publish(flashlightStatTopic.c_str(), "ON");
|
||||
} else if (evt.intent == InputEvent::SetPower) {
|
||||
if (evt.intent == InputEvent::SetPower) {
|
||||
m_json.clear();
|
||||
m_isOn = evt.asInt() ? true : false;
|
||||
m_json["state"] = m_isOn ? "ON" : "OFF";
|
||||
@@ -192,7 +183,7 @@ MQTTTelemetry::handleEventOnline(const InputEvent& evt)
|
||||
m_json["color"]["b"] = color.b;
|
||||
m_json["state"] = m_isOn ? "ON" : "OFF";
|
||||
publishDoc(statTopic.c_str());
|
||||
} else if (evt.intent == InputEvent::SetPattern) {
|
||||
} else if (evt.intent == InputEvent::SetScene) {
|
||||
m_json.clear();
|
||||
m_json["effect"] = evt.asString();
|
||||
m_json["state"] = m_isOn ? "ON" : "OFF";
|
||||
@@ -280,17 +271,7 @@ void
|
||||
MQTTTelemetry::callback(char* topic, const char* payload)
|
||||
{
|
||||
setEvent(InputEvent::NetworkActivity);
|
||||
if (flashlightSwitch.isCommandTopic(topic)) {
|
||||
if (!strncmp((char*)payload, "ON", sizeof("ON"))) {
|
||||
Log.notice("Turning on flashlight");
|
||||
setEvent(InputEvent{InputEvent::SetPower, true});
|
||||
setEvent(InputEvent{InputEvent::SetPattern, "Flashlight"});
|
||||
setEvent(InputEvent{InputEvent::SetBrightness, 255});
|
||||
} else if (!strncmp((char*)payload, "OFF", sizeof("OFF"))) {
|
||||
Log.notice("Turning off flashlight");
|
||||
setEvent(InputEvent{InputEvent::SetPattern, "Idle"});
|
||||
}
|
||||
} else if (Lightswitch.isCommandTopic(topic)) {
|
||||
if (Lightswitch.isCommandTopic(topic)) {
|
||||
deserializeJson(m_json, payload);
|
||||
|
||||
if (m_json.containsKey("state")) {
|
||||
@@ -299,7 +280,7 @@ MQTTTelemetry::callback(char* topic, const char* payload)
|
||||
setEvent(InputEvent{InputEvent::SetPower, true});
|
||||
} else if (m_json["state"] == "OFF") {
|
||||
Log.notice("Turning off power");
|
||||
setEvent(InputEvent{InputEvent::SetPattern, "Idle"});
|
||||
setEvent(InputEvent{InputEvent::SetScene, "Idle"});
|
||||
setEvent(InputEvent{InputEvent::SetPower, false});
|
||||
}
|
||||
}
|
||||
@@ -352,7 +333,7 @@ MQTTTelemetry::callback(char* topic, const char* payload)
|
||||
|
||||
if (m_json.containsKey("effect")) {
|
||||
strcpy(m_patternBuf, m_json["effect"].as<const char*>());
|
||||
setEvent(InputEvent{InputEvent::SetPattern, m_patternBuf});
|
||||
setEvent(InputEvent{InputEvent::SetScene, m_patternBuf});
|
||||
}
|
||||
|
||||
if (m_json.containsKey("color")) {
|
||||
@@ -367,6 +348,19 @@ MQTTTelemetry::callback(char* topic, const char* payload)
|
||||
}
|
||||
|
||||
Log.notice("Event done.");
|
||||
} else {
|
||||
for(auto scene : Static<Sequencer>::instance()->scenes()) {
|
||||
String strName{scene.name};
|
||||
MQTTEntity sceneObj {
|
||||
"scene",
|
||||
Device,
|
||||
strName
|
||||
};
|
||||
if (sceneObj.isCommandTopic(topic)) {
|
||||
setEvent(InputEvent{InputEvent::SetScene, scene.name});
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@@ -7,9 +7,9 @@
|
||||
|
||||
#include "../../Sequencer.h"
|
||||
|
||||
#ifdef BOARD_ESP8266
|
||||
#ifdef ESP8266
|
||||
#include <ESP8266WiFi.h>
|
||||
#elif defined(BOARD_ESP32)
|
||||
#elif defined(ESP32)
|
||||
#include <WiFi.h>
|
||||
#endif
|
||||
|
||||
|
@@ -1,10 +1,10 @@
|
||||
#include <Input.h>
|
||||
#include <ArduinoLog.h>
|
||||
|
||||
#ifdef BOARD_ESP8266
|
||||
#ifdef ESP8266
|
||||
#include <ESP8266WiFi.h>
|
||||
#endif
|
||||
#ifdef BOARD_ESP32
|
||||
#ifdef ESP32
|
||||
#include <WiFi.h>
|
||||
#endif
|
||||
#include "Static.h"
|
||||
|
106
src/platform/arduino/esp32/Platform.cpp
Normal file
106
src/platform/arduino/esp32/Platform.cpp
Normal file
@@ -0,0 +1,106 @@
|
||||
#include "../../../Platform.h"
|
||||
|
||||
#include <WiFi.h>
|
||||
#include <esp_task_wdt.h>
|
||||
|
||||
__NOINIT_ATTR static uint8_t s_rebootCount;
|
||||
__NOINIT_ATTR static uint16_t s_forceSafeMode;
|
||||
#define SAFE_MODE_MAGIC 6942
|
||||
|
||||
template<>
|
||||
void
|
||||
PlatformImpl<HAL_ESP32>::initBootOptions(BootOptions& opts)
|
||||
{
|
||||
opts.resetReason = esp_reset_reason();
|
||||
opts.crashCount = s_rebootCount;
|
||||
if (opts.resetReason >= 4) { // TODO: These values are defined in
|
||||
// esp32/rom/rtc.h, but not sure if that's included
|
||||
// on platformio builds
|
||||
if (opts.crashCount++ >= 3) {
|
||||
// Boot into safe mode if the watchdog reset us three times in a row.
|
||||
isSafeMode = true;
|
||||
}
|
||||
} else {
|
||||
opts.crashCount = 0;
|
||||
}
|
||||
s_rebootCount = crashCount;
|
||||
}
|
||||
|
||||
template<>
|
||||
const char*
|
||||
PlatformImpl<HAL_ESP32>::name()
|
||||
{
|
||||
return "ESP32";
|
||||
}
|
||||
|
||||
template<>
|
||||
const char*
|
||||
PlatformImpl<HAL_ESP32>::version()
|
||||
{
|
||||
return ESP.getSdkVersion();
|
||||
}
|
||||
|
||||
template<>
|
||||
const char*
|
||||
PlatformImpl<HAL_ESP32>::deviceID()
|
||||
{
|
||||
uint64_t chipid;
|
||||
chipid = ESP.getEfuseMac();
|
||||
snprintf(s_deviceID, sizeof(s_deviceID), "%08X", (uint32_t)chipid);
|
||||
return s_deviceID;
|
||||
}
|
||||
|
||||
template<>
|
||||
int
|
||||
PlatformImpl<HAL_ESP32>::freeRam()
|
||||
{
|
||||
return ESP.getFreeHeap();
|
||||
}
|
||||
|
||||
int printEspLog(const char* fmt, va_list args)
|
||||
{
|
||||
Log.notice(fmt, args);
|
||||
return 1;
|
||||
}
|
||||
|
||||
template<>
|
||||
void
|
||||
PlatformImpl<HAL_ESP32>::startWatchdog()
|
||||
{
|
||||
esp_task_wdt_init(10, true);
|
||||
esp_task_wdt_add(NULL);
|
||||
esp_log_set_vprintf(printEspLog);
|
||||
}
|
||||
|
||||
template<>
|
||||
void
|
||||
PlatformImpl<HAL_ESP32>::startNTP()
|
||||
{
|
||||
constexpr int dst = 1;
|
||||
configTime(0, 3600 * dst, "pool.ntp.org");
|
||||
}
|
||||
|
||||
template<>
|
||||
void
|
||||
PlatformImpl<HAL_ESP32>::loop()
|
||||
{
|
||||
esp_task_wdt_reset();
|
||||
}
|
||||
|
||||
template<>
|
||||
bool
|
||||
PlatformImpl<HAL_ESP32>::getLocalTime(struct tm* timedata, int timezone)
|
||||
{
|
||||
time_t rawtime;
|
||||
memset(&rawtime, 0, sizeof(rawtime));
|
||||
time(&rawtime);
|
||||
(*timedata) = (*localtime(&rawtime));
|
||||
return (timedata->tm_year > (2016-1990));
|
||||
}
|
||||
|
||||
template<>
|
||||
void
|
||||
PlatformImpl<HAL_ESP32>::restart()
|
||||
{
|
||||
ESP.restart();
|
||||
}
|
134
src/platform/arduino/esp8266/Platform.cpp
Normal file
134
src/platform/arduino/esp8266/Platform.cpp
Normal file
@@ -0,0 +1,134 @@
|
||||
#include "../../../Platform.h"
|
||||
#include <ESP8266WiFi.h>
|
||||
#include <WiFiUdp.h>
|
||||
#include <NTPClient.h>
|
||||
#include <ctime>
|
||||
|
||||
#define __NOINIT_ATTR __attribute__ ((section (".noinit")))
|
||||
__NOINIT_ATTR static uint8_t s_rebootCount;
|
||||
__NOINIT_ATTR static uint16_t s_forceSafeMode;
|
||||
#define SAFE_MODE_MAGIC 6942
|
||||
|
||||
WiFiUDP wifiUdp;
|
||||
NTPClient timeClient(wifiUdp, "pool.ntp.org", 3600 * -7);
|
||||
|
||||
template<>
|
||||
const char*
|
||||
PlatformImpl<HAL_ESP8266>::name()
|
||||
{
|
||||
return "ESP8266";
|
||||
}
|
||||
|
||||
template<>
|
||||
const char*
|
||||
PlatformImpl<HAL_ESP8266>::version()
|
||||
{
|
||||
return ESP.getSdkVersion();
|
||||
}
|
||||
|
||||
template<>
|
||||
int
|
||||
PlatformImpl<HAL_ESP8266>::freeRam()
|
||||
{
|
||||
return ESP.getFreeHeap();
|
||||
}
|
||||
|
||||
template<>
|
||||
void
|
||||
PlatformImpl<HAL_ESP8266>::startWatchdog()
|
||||
{
|
||||
ESP.wdtEnable(0);
|
||||
}
|
||||
|
||||
template<>
|
||||
bool
|
||||
PlatformImpl<HAL_ESP8266>::getLocalTime(struct tm* timedata, int timezone)
|
||||
{
|
||||
timedata->tm_hour = (timeClient.getHours() + timezone) % 23;
|
||||
timedata->tm_min = timeClient.getMinutes();
|
||||
return true;
|
||||
}
|
||||
|
||||
template<>
|
||||
void
|
||||
PlatformImpl<HAL_ESP8266>::startNTP()
|
||||
{
|
||||
timeClient.begin();
|
||||
}
|
||||
|
||||
template<>
|
||||
void
|
||||
PlatformImpl<HAL_ESP8266>::loop()
|
||||
{
|
||||
if (WiFi.status() == WL_CONNECTED) {
|
||||
timeClient.update();
|
||||
}
|
||||
ESP.wdtFeed();
|
||||
}
|
||||
|
||||
template<>
|
||||
const char*
|
||||
PlatformImpl<HAL_ESP8266>::deviceID()
|
||||
{
|
||||
static char s_deviceID[15];
|
||||
static uint16_t chipid = ESP.getChipId();
|
||||
snprintf(s_deviceID, sizeof(s_deviceID), "%08X", (uint32_t)chipid);
|
||||
return s_deviceID;
|
||||
}
|
||||
|
||||
template<>
|
||||
void
|
||||
PlatformImpl<HAL_ESP8266>::restart()
|
||||
{
|
||||
ESP.wdtDisable();
|
||||
ESP.restart();
|
||||
}
|
||||
|
||||
template<>
|
||||
void
|
||||
PlatformImpl<HAL_ESP8266>::bootSplash()
|
||||
{
|
||||
Log.notice("ESP8266!");
|
||||
}
|
||||
|
||||
template<>
|
||||
void
|
||||
PlatformImpl<HAL_ESP8266>::printCrashInfo()
|
||||
{
|
||||
auto rInfo = ESP.getResetInfoPtr();
|
||||
if (Platform::bootopts.resetReason == REASON_EXCEPTION_RST) {
|
||||
Log.error("Fatal exception (%d):", rInfo->exccause);
|
||||
}
|
||||
Log.error("epc1=%X, epc2=%X, epc3=%X, excvaddr=%X, depc=%X",
|
||||
rInfo->epc1, rInfo->epc2, rInfo->epc3, rInfo->excvaddr, rInfo->depc);
|
||||
}
|
||||
|
||||
template<>
|
||||
void
|
||||
PlatformImpl<HAL_ESP8266>::initBootOptions(BootOptions& opts)
|
||||
{
|
||||
struct rst_info resetInfo = *ESP.getResetInfoPtr();
|
||||
opts.resetReason = resetInfo.reason;
|
||||
opts.crashCount = s_rebootCount;
|
||||
if (resetInfo.reason == REASON_SOFT_WDT_RST || resetInfo.reason == REASON_WDT_RST || resetInfo.reason == REASON_EXCEPTION_RST) {
|
||||
if (opts.crashCount++ >= 3) {
|
||||
// Boot into safe mode if the watchdog reset us three times in a row.
|
||||
opts.isSafeMode = true;
|
||||
}
|
||||
} else {
|
||||
opts.crashCount = 0;
|
||||
}
|
||||
s_rebootCount = opts.crashCount;
|
||||
|
||||
if (resetInfo.reason > 0 && s_forceSafeMode == SAFE_MODE_MAGIC) {
|
||||
opts.isSafeMode = true;
|
||||
s_forceSafeMode = 0;
|
||||
}
|
||||
}
|
||||
|
||||
template<>
|
||||
void
|
||||
PlatformImpl<HAL_ESP8266>::forceSafeMode()
|
||||
{
|
||||
s_forceSafeMode = SAFE_MODE_MAGIC;
|
||||
}
|
Reference in New Issue
Block a user