Fork of the espurna firmware for `mhsw` switches
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

116 lines
2.7 KiB

7 years ago
  1. /*
  2. SYSTEM MODULE
  3. Copyright (C) 2018 by Xose Pérez <xose dot perez at gmail dot com>
  4. */
  5. #include <EEPROM.h>
  6. // -----------------------------------------------------------------------------
  7. unsigned long _loopDelay = 0;
  8. // -----------------------------------------------------------------------------
  9. #if SYSTEM_CHECK_ENABLED
  10. // Call this method on boot with start=true to increase the crash counter
  11. // Call it again once the system is stable to decrease the counter
  12. // If the counter reaches SYSTEM_CHECK_MAX then the system is flagged as unstable
  13. // setting _systemOK = false;
  14. //
  15. // An unstable system will only have serial access, WiFi in AP mode and OTA
  16. bool _systemStable = true;
  17. void systemCheck(bool stable) {
  18. unsigned char value = EEPROM.read(EEPROM_CRASH_COUNTER);
  19. if (stable) {
  20. value = 0;
  21. DEBUG_MSG_P(PSTR("[MAIN] System OK\n"));
  22. } else {
  23. if (++value > SYSTEM_CHECK_MAX) {
  24. _systemStable = false;
  25. value = 0;
  26. DEBUG_MSG_P(PSTR("[MAIN] System UNSTABLE\n"));
  27. }
  28. }
  29. EEPROM.write(EEPROM_CRASH_COUNTER, value);
  30. EEPROM.commit();
  31. }
  32. bool systemCheck() {
  33. return _systemStable;
  34. }
  35. void systemCheckLoop() {
  36. static bool checked = false;
  37. if (!checked && (millis() > SYSTEM_CHECK_TIME)) {
  38. // Check system as stable
  39. systemCheck(true);
  40. checked = true;
  41. }
  42. }
  43. #endif
  44. // -----------------------------------------------------------------------------
  45. void systemLoop() {
  46. // Check system stability
  47. #if SYSTEM_CHECK_ENABLED
  48. systemCheckLoop();
  49. #endif
  50. #if HEARTBEAT_ENABLED
  51. // Heartbeat
  52. static unsigned long last = 0;
  53. if ((last == 0) || (millis() - last > HEARTBEAT_INTERVAL)) {
  54. last = millis();
  55. heartbeat();
  56. }
  57. #endif // HEARTBEAT_ENABLED
  58. // Power saving delay
  59. delay(_loopDelay);
  60. }
  61. void systemSetup() {
  62. EEPROM.begin(EEPROM_SIZE);
  63. #if DEBUG_SERIAL_SUPPORT
  64. DEBUG_PORT.begin(SERIAL_BAUDRATE);
  65. #if DEBUG_ESP_WIFI
  66. DEBUG_PORT.setDebugOutput(true);
  67. #endif
  68. #elif defined(SERIAL_BAUDRATE)
  69. Serial.begin(SERIAL_BAUDRATE);
  70. #endif
  71. #if SPIFFS_SUPPORT
  72. SPIFFS.begin();
  73. #endif
  74. // Question system stability
  75. #if SYSTEM_CHECK_ENABLED
  76. systemCheck(false);
  77. #endif
  78. #if defined(ESPLIVE)
  79. //The ESPLive has an ADC MUX which needs to be configured.
  80. pinMode(16, OUTPUT);
  81. digitalWrite(16, HIGH); //Defualt CT input (pin B, solder jumper B)
  82. #endif
  83. // Cache loop delay value to speed things (recommended max 250ms)
  84. _loopDelay = atol(getSetting("loopDelay", LOOP_DELAY_TIME).c_str());
  85. // Register Loop
  86. espurnaRegisterLoop(systemLoop);
  87. }