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.

172 lines
4.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
  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
  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_ENABLED
  69. // Heartbeat
  70. static unsigned long last_hbeat = 0;
  71. if (_system_send_heartbeat || (last_hbeat == 0) || (millis() - last_hbeat > HEARTBEAT_INTERVAL)) {
  72. _system_send_heartbeat = false;
  73. last_hbeat = millis();
  74. heartbeat();
  75. }
  76. #endif // HEARTBEAT_ENABLED
  77. // -------------------------------------------------------------------------
  78. // Load Average calculation
  79. // -------------------------------------------------------------------------
  80. static unsigned long last_loadcheck = 0;
  81. static unsigned long load_counter_temp = 0;
  82. load_counter_temp++;
  83. if (millis() - last_loadcheck > LOADAVG_INTERVAL) {
  84. static unsigned long load_counter = 0;
  85. static unsigned long load_counter_max = 1;
  86. load_counter = load_counter_temp;
  87. load_counter_temp = 0;
  88. if (load_counter > load_counter_max) {
  89. load_counter_max = load_counter;
  90. }
  91. _load_average = 100 - (100 * load_counter / load_counter_max);
  92. last_loadcheck = millis();
  93. }
  94. // -------------------------------------------------------------------------
  95. // Power saving delay
  96. // -------------------------------------------------------------------------
  97. delay(_loop_delay);
  98. }
  99. void _systemSetupSpecificHardware() {
  100. //The ESPLive has an ADC MUX which needs to be configured.
  101. #if defined(MANCAVEMADE_ESPLIVE)
  102. pinMode(16, OUTPUT);
  103. digitalWrite(16, HIGH); //Defualt CT input (pin B, solder jumper B)
  104. #endif
  105. // These devices use the hardware UART
  106. // to communicate to secondary microcontrollers
  107. #if defined(ITEAD_SONOFF_RFBRIDGE) || defined(ITEAD_SONOFF_DUAL) || defined(STM_RELAY)
  108. Serial.begin(SERIAL_BAUDRATE);
  109. #endif
  110. }
  111. void systemSetup() {
  112. #if SPIFFS_SUPPORT
  113. SPIFFS.begin();
  114. #endif
  115. // Question system stability
  116. #if SYSTEM_CHECK_ENABLED
  117. systemCheck(false);
  118. #endif
  119. // Init device-specific hardware
  120. _systemSetupSpecificHardware();
  121. // Cache loop delay value to speed things (recommended max 250ms)
  122. _loop_delay = atol(getSetting("loopDelay", LOOP_DELAY_TIME).c_str());
  123. _loop_delay = constrain(_loop_delay, 0, 300);
  124. // Register Loop
  125. espurnaRegisterLoop(systemLoop);
  126. }