renderbug-cpp/src/BootOptions.cpp

91 lines
2.4 KiB
C++

#include "BootOptions.h"
#include "Config.h"
#include <EEPROM.h>
#ifdef BOARD_ESP8266
#include <ESP8266WiFi.h>
#endif
#ifdef PLATFORM_PHOTON
LEDStatus serialStatus = LEDStatus(RGB_COLOR_ORANGE, LED_PATTERN_FADE, LED_SPEED_FAST, LED_PRIORITY_BACKGROUND);
LEDStatus configStatus = LEDStatus(RGB_COLOR_YELLOW, LED_PATTERN_FADE, LED_SPEED_NORMAL, LED_PRIORITY_IMPORTANT);
retained bool LAST_BOOT_WAS_FLASH;
retained bool LAST_BOOT_WAS_SERIAL;
#endif
#ifdef BOARD_ESP32
__NOINIT_ATTR uint8_t s_rebootCount = 0;
#endif
void
BootOptions::initPins()
{
#ifdef PLATFORM_PHOTON
pinMode(2, INPUT_PULLDOWN);
pinMode(3, INPUT_PULLDOWN);
pinMode(4, INPUT_PULLDOWN);
#endif
}
BootOptions::BootOptions()
{
#ifdef PLATFORM_PHOTON
isSetup = digitalRead(2) == HIGH;
isSerial = digitalRead(3) == HIGH || LAST_BOOT_WAS_SERIAL;
isFlash = digitalRead(4) == HIGH;
LAST_BOOT_WAS_FLASH = isFlash;
LAST_BOOT_WAS_SERIAL |= isSerial;
lastBootWasFlash = LAST_BOOT_WAS_FLASH;
configStatus.setActive(isSetup);
serialStatus.setActive(isSerial);
#endif
#ifdef BOARD_ESP32
resetReason = esp_reset_reason();
crashCount = s_rebootCount;
if (resetReason >= 4) { // TODO: These values are defined in
// esp32/rom/rtc.h, but not sure if that's included
// on platformio builds
if (crashCount++ >= 3) {
// Boot into safe mode if the watchdog reset us three times in a row.
isSafeMode = true;
}
} else {
crashCount = 0;
}
s_rebootCount = crashCount;
#endif
#ifdef BOARD_ESP8266
struct rst_info resetInfo = *ESP.getResetInfoPtr();
resetReason = resetInfo.reason;
EEPROM.begin(sizeof(crashCount));
EEPROM.get(sizeof(HardwareConfig) + 32, crashCount);
EEPROM.end();
if (resetInfo.reason == REASON_WDT_RST || resetInfo.reason == REASON_EXCEPTION_RST) {
if (crashCount++ >= 3) {
// Boot into safe mode if the watchdog reset us three times in a row.
isSafeMode = true;
} else {
EEPROM.begin(sizeof(crashCount));
EEPROM.put(sizeof(HardwareConfig) + 32, crashCount);
EEPROM.end();
}
} else if (crashCount != 0) {
crashCount = 0;
EEPROM.begin(sizeof(crashCount));
EEPROM.put(sizeof(HardwareConfig) + 32, crashCount);
EEPROM.end();
}
#endif
}
void
BootOptions::waitForRelease()
{
#ifdef PLATFORM_PHOTON
while(digitalRead(2) == HIGH || digitalRead(3) == HIGH) {};
#endif
}