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.

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