cleanup main.cpp, split platform code into a Platform object
This commit is contained in:
139
src/Platform.cpp
Normal file
139
src/Platform.cpp
Normal file
@@ -0,0 +1,139 @@
|
||||
#include "Platform.h"
|
||||
#include <ArduinoLog.h>
|
||||
#include "Static.h"
|
||||
|
||||
#ifdef BOARD_ESP32
|
||||
#include <WiFi.h>
|
||||
#elif defined(BOARD_ESP8266)
|
||||
#include <ESP8266WiFi.h>
|
||||
#include <WiFiUdp.h>
|
||||
#include <NTPClient.h>
|
||||
#include <ctime>
|
||||
|
||||
WiFiUDP wifiUdp;
|
||||
NTPClient timeClient(wifiUdp, "pool.ntp.org", 3600 * -7);
|
||||
#endif
|
||||
|
||||
#ifdef PLATFORM_PHOTON
|
||||
STARTUP(BootOptions::initPins());
|
||||
#else
|
||||
#include "platform/arduino/MQTTTelemetry.h"
|
||||
void printNewline(Print* logOutput) {
|
||||
logOutput->print("\r\n");
|
||||
}
|
||||
#endif
|
||||
|
||||
int Platform::s_timezone = 0;
|
||||
|
||||
const char*
|
||||
Platform::name()
|
||||
{
|
||||
#ifdef PLATFORM_PHOTON
|
||||
return "Photon";
|
||||
#else
|
||||
return "Unknown!";
|
||||
#endif
|
||||
}
|
||||
|
||||
const char*
|
||||
Platform::version()
|
||||
{
|
||||
#ifdef PLATFORM_PHOTON
|
||||
return System.version().c_str();
|
||||
#else
|
||||
return "Unknown!";
|
||||
#endif
|
||||
}
|
||||
|
||||
void
|
||||
Platform::preSetup()
|
||||
{
|
||||
Serial.begin(115200);
|
||||
#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
|
||||
Log.begin(LOG_LEVEL_VERBOSE, Static<MQTTTelemetry>::instance()->logPrinter());
|
||||
Log.setSuffix(printNewline);
|
||||
#endif
|
||||
}
|
||||
|
||||
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)
|
||||
timeClient.begin();
|
||||
#endif
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
void
|
||||
Platform::loop()
|
||||
{
|
||||
#ifdef BOARD_ESP8266
|
||||
timeClient.update();
|
||||
#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)
|
||||
return getLocalTime(timedata);
|
||||
#else
|
||||
timeClient.update();
|
||||
timedata->tm_hour = timeClient.getHours();
|
||||
timedata->tm_min = timeClient.getMinutes();
|
||||
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;
|
||||
}
|
||||
|
||||
BootOptions
|
||||
Platform::bootopts;
|
||||
|
||||
char
|
||||
Platform::s_deviceID[15];
|
Reference in New Issue
Block a user