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.

122 lines
2.9 KiB

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