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.

182 lines
4.9 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. */
  5. #include <EEPROM_Rotate.h>
  6. // -----------------------------------------------------------------------------
  7. unsigned long _loop_delay = 0;
  8. bool _system_send_heartbeat = false;
  9. // Calculated load average 0 to 100;
  10. unsigned short int _load_average = 100;
  11. // -----------------------------------------------------------------------------
  12. #if SYSTEM_CHECK_ENABLED
  13. // Call this method on boot with start=true to increase the crash counter
  14. // Call it again once the system is stable to decrease the counter
  15. // If the counter reaches SYSTEM_CHECK_MAX then the system is flagged as unstable
  16. // setting _systemOK = false;
  17. //
  18. // An unstable system will only have serial access, WiFi in AP mode and OTA
  19. bool _systemStable = true;
  20. void systemCheck(bool stable) {
  21. unsigned char value = EEPROMr.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. EEPROMr.write(EEPROM_CRASH_COUNTER, value);
  33. eepromCommit();
  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. unsigned long systemLoopDelay() {
  52. return _loop_delay;
  53. }
  54. unsigned long systemLoadAverage() {
  55. return _load_average;
  56. }
  57. void systemLoop() {
  58. // -------------------------------------------------------------------------
  59. // User requested reset
  60. // -------------------------------------------------------------------------
  61. if (checkNeedsReset()) {
  62. reset();
  63. }
  64. // -------------------------------------------------------------------------
  65. // Check system stability
  66. // -------------------------------------------------------------------------
  67. #if SYSTEM_CHECK_ENABLED
  68. systemCheckLoop();
  69. #endif
  70. // -------------------------------------------------------------------------
  71. // Heartbeat
  72. // -------------------------------------------------------------------------
  73. #if HEARTBEAT_MODE == HEARTBEAT_ONCE
  74. if (_system_send_heartbeat) {
  75. _system_send_heartbeat = false;
  76. heartbeat();
  77. }
  78. #elif HEARTBEAT_MODE == HEARTBEAT_REPEAT
  79. static unsigned long last_hbeat = 0;
  80. if (_system_send_heartbeat || (last_hbeat == 0) || (millis() - last_hbeat > HEARTBEAT_INTERVAL)) {
  81. _system_send_heartbeat = false;
  82. last_hbeat = millis();
  83. heartbeat();
  84. }
  85. #endif // HEARTBEAT_MODE == HEARTBEAT_REPEAT
  86. // -------------------------------------------------------------------------
  87. // Load Average calculation
  88. // -------------------------------------------------------------------------
  89. static unsigned long last_loadcheck = 0;
  90. static unsigned long load_counter_temp = 0;
  91. load_counter_temp++;
  92. if (millis() - last_loadcheck > LOADAVG_INTERVAL) {
  93. static unsigned long load_counter = 0;
  94. static unsigned long load_counter_max = 1;
  95. load_counter = load_counter_temp;
  96. load_counter_temp = 0;
  97. if (load_counter > load_counter_max) {
  98. load_counter_max = load_counter;
  99. }
  100. _load_average = 100 - (100 * load_counter / load_counter_max);
  101. last_loadcheck = millis();
  102. }
  103. // -------------------------------------------------------------------------
  104. // Power saving delay
  105. // -------------------------------------------------------------------------
  106. delay(_loop_delay);
  107. }
  108. void _systemSetupSpecificHardware() {
  109. //The ESPLive has an ADC MUX which needs to be configured.
  110. #if defined(MANCAVEMADE_ESPLIVE)
  111. pinMode(16, OUTPUT);
  112. digitalWrite(16, HIGH); //Defualt CT input (pin B, solder jumper B)
  113. #endif
  114. // These devices use the hardware UART
  115. // to communicate to secondary microcontrollers
  116. #if defined(ITEAD_SONOFF_RFBRIDGE) || defined(ITEAD_SONOFF_DUAL) || (RELAY_PROVIDER == RELAY_PROVIDER_STM)
  117. Serial.begin(SERIAL_BAUDRATE);
  118. #endif
  119. }
  120. void systemSetup() {
  121. #if SPIFFS_SUPPORT
  122. SPIFFS.begin();
  123. #endif
  124. // Question system stability
  125. #if SYSTEM_CHECK_ENABLED
  126. systemCheck(false);
  127. #endif
  128. // Init device-specific hardware
  129. _systemSetupSpecificHardware();
  130. // Cache loop delay value to speed things (recommended max 250ms)
  131. _loop_delay = atol(getSetting("loopDelay", LOOP_DELAY_TIME).c_str());
  132. _loop_delay = constrain(_loop_delay, 0, 300);
  133. // Register Loop
  134. espurnaRegisterLoop(systemLoop);
  135. }