2021-03-31 18:50:00 +00:00
|
|
|
#include "Platform.h"
|
|
|
|
#include <ArduinoLog.h>
|
|
|
|
#include "Static.h"
|
2022-06-11 09:02:27 +00:00
|
|
|
#include <time.h>
|
2021-03-31 18:50:00 +00:00
|
|
|
|
|
|
|
#ifdef BOARD_ESP32
|
2022-06-11 09:02:27 +00:00
|
|
|
#ifdef CONFIG_WIFI
|
2021-03-31 18:50:00 +00:00
|
|
|
#include <WiFi.h>
|
2022-06-11 09:02:27 +00:00
|
|
|
#endif
|
2021-04-10 18:10:25 +00:00
|
|
|
#include <esp_task_wdt.h>
|
2021-03-31 18:50:00 +00:00
|
|
|
#elif defined(BOARD_ESP8266)
|
2022-06-11 09:02:27 +00:00
|
|
|
#ifdef CONFIG_WIFI
|
2021-03-31 18:50:00 +00:00
|
|
|
#include <ESP8266WiFi.h>
|
|
|
|
#include <WiFiUdp.h>
|
|
|
|
#include <NTPClient.h>
|
|
|
|
#include <ctime>
|
|
|
|
|
|
|
|
WiFiUDP wifiUdp;
|
2021-04-10 18:10:25 +00:00
|
|
|
//NTPClient timeClient(wifiUdp, "pool.ntp.org", 3600 * -7);
|
|
|
|
NTPClient timeClient(wifiUdp, "10.0.0.1", 3600 * -7);
|
2021-03-31 18:50:00 +00:00
|
|
|
#endif
|
2022-06-11 09:02:27 +00:00
|
|
|
#endif
|
2021-03-31 18:50:00 +00:00
|
|
|
|
|
|
|
#ifdef PLATFORM_PHOTON
|
|
|
|
STARTUP(BootOptions::initPins());
|
|
|
|
#else
|
2022-06-11 09:02:27 +00:00
|
|
|
#ifdef CONFIG_MQTT
|
2021-03-31 18:50:00 +00:00
|
|
|
#include "platform/arduino/MQTTTelemetry.h"
|
2022-06-11 09:02:27 +00:00
|
|
|
#endif
|
2023-03-03 17:04:04 +00:00
|
|
|
void printNewline(Print* logOutput, int logLevel)
|
2021-04-10 18:10:25 +00:00
|
|
|
{
|
2023-03-03 17:04:04 +00:00
|
|
|
(void)logLevel; // unused
|
2021-03-31 18:50:00 +00:00
|
|
|
logOutput->print("\r\n");
|
|
|
|
}
|
2021-04-10 18:10:25 +00:00
|
|
|
int printEspLog(const char* fmt, va_list args)
|
|
|
|
{
|
|
|
|
Log.notice(fmt, args);
|
|
|
|
return 1;
|
|
|
|
}
|
2021-03-31 18:50:00 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
int Platform::s_timezone = 0;
|
2022-06-11 09:02:27 +00:00
|
|
|
Platform::TaskRegistration* Platform::firstTask = NULL;
|
|
|
|
Platform::TaskRegistration* Platform::lastTask = NULL;
|
2021-03-31 18:50:00 +00:00
|
|
|
|
|
|
|
const char*
|
|
|
|
Platform::name()
|
|
|
|
{
|
|
|
|
#ifdef PLATFORM_PHOTON
|
|
|
|
return "Photon";
|
2021-04-10 18:10:25 +00:00
|
|
|
#elif defined(BOARD_ESP8266)
|
|
|
|
return "ESP8266";
|
|
|
|
#elif defined(BOARD_ESP32)
|
|
|
|
return "ESP32";
|
2021-03-31 18:50:00 +00:00
|
|
|
#else
|
|
|
|
return "Unknown!";
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
const char*
|
|
|
|
Platform::version()
|
|
|
|
{
|
|
|
|
#ifdef PLATFORM_PHOTON
|
|
|
|
return System.version().c_str();
|
2021-04-10 18:10:25 +00:00
|
|
|
#elif defined(BOARD_ESP32)
|
|
|
|
return ESP.getSdkVersion();
|
2021-03-31 18:50:00 +00:00
|
|
|
#else
|
|
|
|
return "Unknown!";
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2023-02-18 14:43:15 +00:00
|
|
|
int
|
|
|
|
Platform::freeRam()
|
|
|
|
{
|
|
|
|
#ifdef BOARD_ESP8266
|
|
|
|
return ESP.getFreeHeap();
|
|
|
|
#endif
|
2023-02-19 17:44:26 +00:00
|
|
|
#ifdef BOARD_ESP32
|
|
|
|
return ESP.getFreeHeap();
|
|
|
|
#endif
|
2023-02-18 14:43:15 +00:00
|
|
|
}
|
|
|
|
|
2021-03-31 18:50:00 +00:00
|
|
|
void
|
|
|
|
Platform::preSetup()
|
|
|
|
{
|
|
|
|
Serial.begin(115200);
|
2021-04-10 18:10:25 +00:00
|
|
|
delay(5000);
|
2021-03-31 18:50:00 +00:00
|
|
|
#ifdef PLATFORM_PHOTON
|
|
|
|
System.enableFeature(FEATURE_RETAINED_MEMORY);
|
|
|
|
if (bootopts.isFlash) {
|
|
|
|
System.dfu();
|
|
|
|
}
|
|
|
|
if (bootopts.isSerial) {
|
|
|
|
bootopts.waitForRelease();
|
|
|
|
while(!Serial.isConnected()) {
|
|
|
|
Particle.process();
|
|
|
|
}
|
|
|
|
Log.notice("\xf0\x9f\x94\x8c Serial connected");
|
|
|
|
}
|
|
|
|
#else
|
2022-06-11 09:02:27 +00:00
|
|
|
#ifdef CONFIG_MQTT
|
|
|
|
Log.begin(LOG_LEVEL_TRACE, Static<MQTTTelemetry>::instance()->logPrinter());
|
2023-02-18 15:30:41 +00:00
|
|
|
Static<MQTTTelemetry>::instance()->setSequencer(Static<Sequencer>::instance());
|
2022-06-11 09:02:27 +00:00
|
|
|
#else
|
|
|
|
Log.begin(LOG_LEVEL_TRACE, &Serial);
|
|
|
|
#endif
|
2021-03-31 18:50:00 +00:00
|
|
|
Log.setSuffix(printNewline);
|
|
|
|
#endif
|
2021-04-10 18:10:25 +00:00
|
|
|
|
|
|
|
#ifdef BOARD_ESP32
|
|
|
|
esp_task_wdt_init(10, true);
|
|
|
|
esp_task_wdt_add(NULL);
|
|
|
|
esp_log_set_vprintf(printEspLog);
|
|
|
|
#endif
|
|
|
|
#ifdef BOARD_ESP8266
|
|
|
|
ESP.wdtEnable(0);
|
|
|
|
#endif
|
2021-03-31 18:50:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Platform::setup()
|
|
|
|
{
|
|
|
|
#ifdef PLATFORM_PHOTON
|
|
|
|
Time.zone(Static<Platform>::instance()->getTimezone());
|
|
|
|
#elif defined(BOARD_ESP32)
|
|
|
|
constexpr int dst = 1;
|
|
|
|
configTime(s_timezone* 3600, 3600 * dst, "pool.ntp.org");
|
|
|
|
#elif defined(BOARD_ESP8266)
|
2022-06-11 09:02:27 +00:00
|
|
|
#ifdef CONFIG_WIFI
|
2021-03-31 18:50:00 +00:00
|
|
|
timeClient.begin();
|
|
|
|
#endif
|
2022-06-11 09:02:27 +00:00
|
|
|
#endif
|
2021-03-31 18:50:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Platform::bootSplash()
|
|
|
|
{
|
|
|
|
#ifdef PLATFORM_PHOTON
|
|
|
|
Log.notice(u8" Boot pin configuration:");
|
|
|
|
Log.notice(u8" 2: Setup - %d", bootopts.isSetup);
|
|
|
|
Log.notice(u8" 3: Serial - %d", bootopts.isSerial);
|
|
|
|
Log.notice(u8" 4: Flash - %d", bootopts.isFlash);
|
|
|
|
#endif
|
2022-06-11 09:02:27 +00:00
|
|
|
|
|
|
|
Log.trace("Registered tasks:");
|
|
|
|
auto it = beginTasks();
|
|
|
|
while (it != endTasks()) {
|
2023-02-18 15:18:57 +00:00
|
|
|
if ((*it)->isFigment()) {
|
|
|
|
Log.trace(" Figment: %s", (*it)->name);
|
|
|
|
} else {
|
|
|
|
Log.trace(" %s", (*it)->name);
|
|
|
|
}
|
2022-06-11 09:02:27 +00:00
|
|
|
++it;
|
|
|
|
}
|
2021-03-31 18:50:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Platform::loop()
|
|
|
|
{
|
|
|
|
#ifdef BOARD_ESP8266
|
2022-06-11 09:02:27 +00:00
|
|
|
#ifdef CONFIG_WIFI
|
2021-04-10 18:10:25 +00:00
|
|
|
if (WiFi.status() == WL_CONNECTED) {
|
|
|
|
timeClient.update();
|
|
|
|
}
|
2022-06-11 09:02:27 +00:00
|
|
|
#endif
|
2021-04-10 18:10:25 +00:00
|
|
|
ESP.wdtFeed();
|
|
|
|
#elif defined(BOARD_ESP32)
|
|
|
|
esp_task_wdt_reset();
|
2021-03-31 18:50:00 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
Platform::getLocalTime(struct tm* timedata)
|
|
|
|
{
|
|
|
|
#ifdef PLATFORM_PHOTON
|
|
|
|
if (Time.isValid()) {
|
|
|
|
timedata->tm_hour = Time.hour();
|
|
|
|
timedata->tm_min = Time.minute();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
#elif defined(BOARD_ESP32)
|
2021-04-10 18:10:25 +00:00
|
|
|
time_t rawtime;
|
2023-03-03 17:33:07 +00:00
|
|
|
memset(&rawtime, sizeof(rawtime), 0);
|
2021-04-10 18:10:25 +00:00
|
|
|
time(&rawtime);
|
|
|
|
(*timedata) = (*localtime(&rawtime));
|
|
|
|
return (timedata->tm_year > (2016-1990));
|
2021-03-31 18:50:00 +00:00
|
|
|
#else
|
2022-06-11 09:02:27 +00:00
|
|
|
#ifdef CONFIG_WIFI
|
2021-03-31 18:50:00 +00:00
|
|
|
timedata->tm_hour = timeClient.getHours();
|
|
|
|
timedata->tm_min = timeClient.getMinutes();
|
2022-06-11 09:02:27 +00:00
|
|
|
#else
|
|
|
|
memset(timedata, sizeof(struct tm), 0);
|
|
|
|
return false;
|
|
|
|
#endif
|
2021-03-31 18:50:00 +00:00
|
|
|
return true;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
const char*
|
|
|
|
Platform::deviceID()
|
|
|
|
{
|
|
|
|
uint64_t chipid;
|
|
|
|
#ifdef BOARD_ESP32
|
|
|
|
chipid = ESP.getEfuseMac();
|
|
|
|
#elif defined(BOARD_ESP8266)
|
|
|
|
chipid = ESP.getChipId();
|
|
|
|
#endif
|
|
|
|
snprintf(s_deviceID, sizeof(s_deviceID), "%08X", (uint32_t)chipid);
|
|
|
|
return s_deviceID;
|
|
|
|
}
|
|
|
|
|
2023-02-19 17:44:26 +00:00
|
|
|
void
|
|
|
|
Platform::addLEDs(CRGB* leds, unsigned int ledCount) {
|
|
|
|
FastLED.addLeds<WS2812B, RENDERBUG_LED_PIN, RENDERBUG_LED_PACKING>(leds, ledCount);
|
|
|
|
}
|
|
|
|
|
|
|
|
const String
|
|
|
|
Platform::model()
|
|
|
|
{
|
|
|
|
static String modelName = String("Renderbug " ) + Platform::name();
|
|
|
|
return modelName;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Platform::restart() {
|
|
|
|
#ifdef BOARD_ESP8266
|
|
|
|
ESP.wdtDisable();
|
|
|
|
ESP.restart();
|
|
|
|
#elif defined(BOARD_ESP32)
|
|
|
|
ESP.restart();
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2021-03-31 18:50:00 +00:00
|
|
|
BootOptions
|
|
|
|
Platform::bootopts;
|
|
|
|
|
|
|
|
char
|
|
|
|
Platform::s_deviceID[15];
|
2021-04-10 18:10:25 +00:00
|
|
|
|
|
|
|
STATIC_ALLOC(Platform);
|
2022-06-11 09:02:27 +00:00
|
|
|
STATIC_TASK(Platform);
|