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.

156 lines
4.2 KiB

6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
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. Module key prefix: esp (shared with others)
  5. */
  6. #include <EEPROM_Rotate.h>
  7. // -----------------------------------------------------------------------------
  8. unsigned long _loop_delay = 0;
  9. bool _system_send_heartbeat = false;
  10. // Calculated load average 0 to 100;
  11. unsigned short int _load_average = 100;
  12. // -----------------------------------------------------------------------------
  13. #if SYSTEM_CHECK_ENABLED
  14. // Call this method on boot with start=true to increase the crash counter
  15. // Call it again once the system is stable to decrease the counter
  16. // If the counter reaches SYSTEM_CHECK_MAX then the system is flagged as unstable
  17. // setting _systemOK = false;
  18. //
  19. // An unstable system will only have serial access, WiFi in AP mode and OTA
  20. bool _systemStable = true;
  21. void systemCheck(bool stable) {
  22. unsigned char value = EEPROMr.read(EEPROM_CRASH_COUNTER);
  23. if (stable) {
  24. value = 0;
  25. DEBUG_MSG_P(PSTR("[MAIN] System OK\n"));
  26. } else {
  27. if (++value > SYSTEM_CHECK_MAX) {
  28. _systemStable = false;
  29. value = 0;
  30. DEBUG_MSG_P(PSTR("[MAIN] System UNSTABLE\n"));
  31. }
  32. }
  33. EEPROMr.write(EEPROM_CRASH_COUNTER, value);
  34. EEPROMr.commit();
  35. }
  36. bool systemCheck() {
  37. return _systemStable;
  38. }
  39. void systemCheckLoop() {
  40. static bool checked = false;
  41. if (!checked && (millis() > SYSTEM_CHECK_TIME)) {
  42. // Check system as stable
  43. systemCheck(true);
  44. checked = true;
  45. }
  46. }
  47. #endif //SYSTEM_CHECK_ENABLED
  48. // -----------------------------------------------------------------------------
  49. void systemSendHeartbeat() {
  50. _system_send_heartbeat = true;
  51. }
  52. unsigned long systemLoopDelay() {
  53. return _loop_delay;
  54. }
  55. unsigned long systemLoadAverage() {
  56. return _load_average;
  57. }
  58. void systemLoop() {
  59. // -------------------------------------------------------------------------
  60. // Check system stability
  61. // -------------------------------------------------------------------------
  62. #if SYSTEM_CHECK_ENABLED
  63. systemCheckLoop();
  64. #endif
  65. // -------------------------------------------------------------------------
  66. // Heartbeat
  67. // -------------------------------------------------------------------------
  68. #if HEARTBEAT_MODE == HEARTBEAT_ONCE
  69. if (_system_send_heartbeat) {
  70. _system_send_heartbeat = false;
  71. heartbeat();
  72. }
  73. #elif HEARTBEAT_MODE == HEARTBEAT_REPEAT
  74. static unsigned long last_hbeat = 0;
  75. if (_system_send_heartbeat || (last_hbeat == 0) || (millis() - last_hbeat > HEARTBEAT_INTERVAL)) {
  76. _system_send_heartbeat = false;
  77. last_hbeat = millis();
  78. heartbeat();
  79. }
  80. #endif // HEARTBEAT_MODE == HEARTBEAT_REPEAT
  81. // -------------------------------------------------------------------------
  82. // Load Average calculation
  83. // -------------------------------------------------------------------------
  84. static unsigned long last_loadcheck = 0;
  85. static unsigned long load_counter_temp = 0;
  86. load_counter_temp++;
  87. if (millis() - last_loadcheck > LOADAVG_INTERVAL) {
  88. static unsigned long load_counter = 0;
  89. static unsigned long load_counter_max = 1;
  90. load_counter = load_counter_temp;
  91. load_counter_temp = 0;
  92. if (load_counter > load_counter_max) {
  93. load_counter_max = load_counter;
  94. }
  95. _load_average = 100 - (100 * load_counter / load_counter_max);
  96. last_loadcheck = millis();
  97. }
  98. // -------------------------------------------------------------------------
  99. // Power saving delay
  100. // -------------------------------------------------------------------------
  101. delay(_loop_delay);
  102. }
  103. void systemSetup() {
  104. #if SPIFFS_SUPPORT
  105. SPIFFS.begin();
  106. #endif
  107. // Question system stability
  108. #if SYSTEM_CHECK_ENABLED
  109. systemCheck(false);
  110. #endif
  111. // Cache loop delay value to speed things (recommended max 250ms)
  112. _loop_delay = atol(getSetting("loopDelay", LOOP_DELAY_TIME).c_str());
  113. _loop_delay = constrain(_loop_delay, 0, 300);
  114. // Register Loop
  115. espurnaRegisterLoop(systemLoop);
  116. }