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.

200 lines
5.6 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
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_Rotate.h>
  6. // -----------------------------------------------------------------------------
  7. unsigned long _loop_delay = 0;
  8. bool _system_send_heartbeat = false;
  9. unsigned char _heartbeat_mode = HEARTBEAT_MODE;
  10. unsigned long _heartbeat_interval = HEARTBEAT_INTERVAL;
  11. // Calculated load average 0 to 100;
  12. unsigned short int _load_average = 100;
  13. // -----------------------------------------------------------------------------
  14. #if SYSTEM_CHECK_ENABLED
  15. // Call this method on boot with start=true to increase the crash counter
  16. // Call it again once the system is stable to decrease the counter
  17. // If the counter reaches SYSTEM_CHECK_MAX then the system is flagged as unstable
  18. // setting _systemOK = false;
  19. //
  20. // An unstable system will only have serial access, WiFi in AP mode and OTA
  21. bool _systemStable = true;
  22. void systemCheck(bool stable) {
  23. unsigned char value = EEPROMr.read(EEPROM_CRASH_COUNTER);
  24. if (stable) {
  25. value = 0;
  26. DEBUG_MSG_P(PSTR("[MAIN] System OK\n"));
  27. } else {
  28. if (++value > SYSTEM_CHECK_MAX) {
  29. _systemStable = false;
  30. value = 0;
  31. DEBUG_MSG_P(PSTR("[MAIN] System UNSTABLE\n"));
  32. }
  33. }
  34. EEPROMr.write(EEPROM_CRASH_COUNTER, value);
  35. eepromCommit();
  36. }
  37. bool systemCheck() {
  38. return _systemStable;
  39. }
  40. void systemCheckLoop() {
  41. static bool checked = false;
  42. if (!checked && (millis() > SYSTEM_CHECK_TIME)) {
  43. // Check system as stable
  44. systemCheck(true);
  45. checked = true;
  46. }
  47. }
  48. #endif
  49. // -----------------------------------------------------------------------------
  50. void systemSendHeartbeat() {
  51. _system_send_heartbeat = true;
  52. }
  53. bool systemGetHeartbeat() {
  54. return _system_send_heartbeat;
  55. }
  56. unsigned long systemLoopDelay() {
  57. return _loop_delay;
  58. }
  59. unsigned long systemLoadAverage() {
  60. return _load_average;
  61. }
  62. void _systemSetupHeartbeat() {
  63. if (getSetting("hbMode").length() == 0) setSetting("hbMode", HEARTBEAT_MODE);
  64. if (getSetting("hbInterval").length() == 0) setSetting("hbInterval", HEARTBEAT_INTERVAL);
  65. _heartbeat_mode = getSetting("hbMode", HEARTBEAT_MODE).toInt();
  66. _heartbeat_interval = getSetting("hbInterval", HEARTBEAT_INTERVAL).toInt();
  67. }
  68. void systemLoop() {
  69. // -------------------------------------------------------------------------
  70. // User requested reset
  71. // -------------------------------------------------------------------------
  72. if (checkNeedsReset()) {
  73. reset();
  74. }
  75. // -------------------------------------------------------------------------
  76. // Check system stability
  77. // -------------------------------------------------------------------------
  78. #if SYSTEM_CHECK_ENABLED
  79. systemCheckLoop();
  80. #endif
  81. // -------------------------------------------------------------------------
  82. // Heartbeat
  83. // -------------------------------------------------------------------------
  84. if (_system_send_heartbeat && _heartbeat_mode == HEARTBEAT_ONCE) {
  85. heartbeat();
  86. _system_send_heartbeat = false;
  87. } else if (_heartbeat_mode == HEARTBEAT_REPEAT || _heartbeat_mode == HEARTBEAT_REPEAT_STATUS) {
  88. static unsigned long last_hbeat = 0;
  89. #if NTP_SUPPORT
  90. if ((_system_send_heartbeat && ntpSynced()) || (millis() - last_hbeat > _heartbeat_interval)) {
  91. #else
  92. if (_system_send_heartbeat || (millis() - last_hbeat > _heartbeat_interval)) {
  93. #endif
  94. last_hbeat = millis();
  95. heartbeat();
  96. _system_send_heartbeat = false;
  97. }
  98. }
  99. // -------------------------------------------------------------------------
  100. // Load Average calculation
  101. // -------------------------------------------------------------------------
  102. static unsigned long last_loadcheck = 0;
  103. static unsigned long load_counter_temp = 0;
  104. load_counter_temp++;
  105. if (millis() - last_loadcheck > LOADAVG_INTERVAL) {
  106. static unsigned long load_counter = 0;
  107. static unsigned long load_counter_max = 1;
  108. load_counter = load_counter_temp;
  109. load_counter_temp = 0;
  110. if (load_counter > load_counter_max) {
  111. load_counter_max = load_counter;
  112. }
  113. _load_average = 100 - (100 * load_counter / load_counter_max);
  114. last_loadcheck = millis();
  115. }
  116. // -------------------------------------------------------------------------
  117. // Power saving delay
  118. // -------------------------------------------------------------------------
  119. delay(_loop_delay);
  120. }
  121. void _systemSetupSpecificHardware() {
  122. //The ESPLive has an ADC MUX which needs to be configured.
  123. #if defined(MANCAVEMADE_ESPLIVE)
  124. pinMode(16, OUTPUT);
  125. digitalWrite(16, HIGH); //Defualt CT input (pin B, solder jumper B)
  126. #endif
  127. // These devices use the hardware UART
  128. // to communicate to secondary microcontrollers
  129. #if defined(ITEAD_SONOFF_RFBRIDGE) || defined(ITEAD_SONOFF_DUAL) || (RELAY_PROVIDER == RELAY_PROVIDER_STM)
  130. Serial.begin(SERIAL_BAUDRATE);
  131. #endif
  132. }
  133. void systemSetup() {
  134. #if SPIFFS_SUPPORT
  135. SPIFFS.begin();
  136. #endif
  137. // Question system stability
  138. #if SYSTEM_CHECK_ENABLED
  139. systemCheck(false);
  140. #endif
  141. // Init device-specific hardware
  142. _systemSetupSpecificHardware();
  143. // Cache loop delay value to speed things (recommended max 250ms)
  144. _loop_delay = atol(getSetting("loopDelay", LOOP_DELAY_TIME).c_str());
  145. _loop_delay = constrain(_loop_delay, 0, 300);
  146. // Register Loop
  147. espurnaRegisterLoop(systemLoop);
  148. // Cache Heartbeat values
  149. _systemSetupHeartbeat();
  150. }