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.

208 lines
5.7 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. _heartbeat_mode = getSetting("hbMode", HEARTBEAT_MODE).toInt();
  64. _heartbeat_interval = getSetting("hbInterval", HEARTBEAT_INTERVAL).toInt();
  65. }
  66. #if WEB_SUPPORT
  67. bool _systemWebSocketOnReceive(const char * key, JsonVariant& value) {
  68. return (strncmp(key, "hb", 2) == 0);
  69. }
  70. #endif
  71. void systemLoop() {
  72. // -------------------------------------------------------------------------
  73. // User requested reset
  74. // -------------------------------------------------------------------------
  75. if (checkNeedsReset()) {
  76. reset();
  77. }
  78. // -------------------------------------------------------------------------
  79. // Check system stability
  80. // -------------------------------------------------------------------------
  81. #if SYSTEM_CHECK_ENABLED
  82. systemCheckLoop();
  83. #endif
  84. // -------------------------------------------------------------------------
  85. // Heartbeat
  86. // -------------------------------------------------------------------------
  87. if (_system_send_heartbeat && _heartbeat_mode == HEARTBEAT_ONCE) {
  88. heartbeat();
  89. _system_send_heartbeat = false;
  90. } else if (_heartbeat_mode == HEARTBEAT_REPEAT || _heartbeat_mode == HEARTBEAT_REPEAT_STATUS) {
  91. static unsigned long last_hbeat = 0;
  92. #if NTP_SUPPORT
  93. if ((_system_send_heartbeat && ntpSynced()) || (millis() - last_hbeat > _heartbeat_interval * 1000)) {
  94. #else
  95. if (_system_send_heartbeat || (millis() - last_hbeat > _heartbeat_interval * 1000)) {
  96. #endif
  97. last_hbeat = millis();
  98. heartbeat();
  99. _system_send_heartbeat = false;
  100. }
  101. }
  102. // -------------------------------------------------------------------------
  103. // Load Average calculation
  104. // -------------------------------------------------------------------------
  105. static unsigned long last_loadcheck = 0;
  106. static unsigned long load_counter_temp = 0;
  107. load_counter_temp++;
  108. if (millis() - last_loadcheck > LOADAVG_INTERVAL) {
  109. static unsigned long load_counter = 0;
  110. static unsigned long load_counter_max = 1;
  111. load_counter = load_counter_temp;
  112. load_counter_temp = 0;
  113. if (load_counter > load_counter_max) {
  114. load_counter_max = load_counter;
  115. }
  116. _load_average = 100 - (100 * load_counter / load_counter_max);
  117. last_loadcheck = millis();
  118. }
  119. // -------------------------------------------------------------------------
  120. // Power saving delay
  121. // -------------------------------------------------------------------------
  122. delay(_loop_delay);
  123. }
  124. void _systemSetupSpecificHardware() {
  125. //The ESPLive has an ADC MUX which needs to be configured.
  126. #if defined(MANCAVEMADE_ESPLIVE)
  127. pinMode(16, OUTPUT);
  128. digitalWrite(16, HIGH); //Defualt CT input (pin B, solder jumper B)
  129. #endif
  130. // These devices use the hardware UART
  131. // to communicate to secondary microcontrollers
  132. #if defined(ITEAD_SONOFF_RFBRIDGE) || defined(ITEAD_SONOFF_DUAL) || (RELAY_PROVIDER == RELAY_PROVIDER_STM)
  133. Serial.begin(SERIAL_BAUDRATE);
  134. #endif
  135. }
  136. void systemSetup() {
  137. #if SPIFFS_SUPPORT
  138. SPIFFS.begin();
  139. #endif
  140. // Question system stability
  141. #if SYSTEM_CHECK_ENABLED
  142. systemCheck(false);
  143. #endif
  144. #if WEB_SUPPORT
  145. wsOnReceiveRegister(_systemWebSocketOnReceive);
  146. #endif
  147. // Init device-specific hardware
  148. _systemSetupSpecificHardware();
  149. // Cache loop delay value to speed things (recommended max 250ms)
  150. _loop_delay = atol(getSetting("loopDelay", LOOP_DELAY_TIME).c_str());
  151. _loop_delay = constrain(_loop_delay, 0, 300);
  152. // Register Loop
  153. espurnaRegisterLoop(systemLoop);
  154. // Cache Heartbeat values
  155. _systemSetupHeartbeat();
  156. }