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.

147 lines
3.6 KiB

7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
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. 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. // Calculated load average 0 to 100;
  19. unsigned short int _load_average = 100;
  20. void systemCheck(bool stable) {
  21. unsigned char value = EEPROM.read(EEPROM_CRASH_COUNTER);
  22. if (stable) {
  23. value = 0;
  24. DEBUG_MSG_P(PSTR("[MAIN] System OK\n"));
  25. } else {
  26. if (++value > SYSTEM_CHECK_MAX) {
  27. _systemStable = false;
  28. value = 0;
  29. DEBUG_MSG_P(PSTR("[MAIN] System UNSTABLE\n"));
  30. }
  31. }
  32. EEPROM.write(EEPROM_CRASH_COUNTER, value);
  33. EEPROM.commit();
  34. }
  35. bool systemCheck() {
  36. return _systemStable;
  37. }
  38. void systemCheckLoop() {
  39. static bool checked = false;
  40. if (!checked && (millis() > SYSTEM_CHECK_TIME)) {
  41. // Check system as stable
  42. systemCheck(true);
  43. checked = true;
  44. }
  45. }
  46. #endif
  47. // -----------------------------------------------------------------------------
  48. void systemSendHeartbeat() {
  49. _system_send_heartbeat = true;
  50. }
  51. void systemLoop() {
  52. // Check system stability
  53. #if SYSTEM_CHECK_ENABLED
  54. systemCheckLoop();
  55. #endif
  56. #if HEARTBEAT_ENABLED
  57. // Heartbeat
  58. static unsigned long last_hbeat = 0;
  59. if (_system_send_heartbeat || (last_hbeat == 0) || (millis() - last_hbeat > HEARTBEAT_INTERVAL)) {
  60. _system_send_heartbeat = false;
  61. last_hbeat = millis();
  62. heartbeat();
  63. }
  64. #endif // HEARTBEAT_ENABLED
  65. // Increase temporary loop counter
  66. static unsigned long load_counter_temp = 0;
  67. static unsigned long last_loadcheck = 0;
  68. load_counter_temp++;
  69. if (millis() - last_loadcheck > LOADAVG_INTERVAL) {
  70. static unsigned long load_counter = 0;
  71. static unsigned long load_counter_max = 1;
  72. load_counter = load_counter_temp;
  73. load_counter_temp = 0;
  74. if (load_counter > load_counter_max) {
  75. load_counter_max = load_counter;
  76. }
  77. _load_average = 100 - (100 * load_counter / load_counter_max);
  78. last_loadcheck = millis();
  79. }
  80. // Power saving delay
  81. delay(_loopDelay);
  82. }
  83. void systemSetup() {
  84. EEPROM.begin(EEPROM_SIZE);
  85. #if DEBUG_SERIAL_SUPPORT
  86. DEBUG_PORT.begin(SERIAL_BAUDRATE);
  87. #if DEBUG_ESP_WIFI
  88. DEBUG_PORT.setDebugOutput(true);
  89. #endif
  90. #elif defined(SERIAL_BAUDRATE)
  91. Serial.begin(SERIAL_BAUDRATE);
  92. #endif
  93. #if SPIFFS_SUPPORT
  94. SPIFFS.begin();
  95. #endif
  96. // Question system stability
  97. #if SYSTEM_CHECK_ENABLED
  98. systemCheck(false);
  99. #endif
  100. #if defined(ESPLIVE)
  101. //The ESPLive has an ADC MUX which needs to be configured.
  102. pinMode(16, OUTPUT);
  103. digitalWrite(16, HIGH); //Defualt CT input (pin B, solder jumper B)
  104. #endif
  105. // Cache loop delay value to speed things (recommended max 250ms)
  106. _loopDelay = atol(getSetting("loopDelay", LOOP_DELAY_TIME).c_str());
  107. // Register Loop
  108. espurnaRegisterLoop(systemLoop);
  109. }
  110. unsigned long getLoadAverage() {
  111. return _load_average;
  112. }