Browse Source

Safer loop delay default (#1574, #1631, #1699)

* Call delay() after callbacks
* Use safer loop delay value
rules-rpn
Max Prokhorov 5 years ago
committed by GitHub
parent
commit
3f37556cdd
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 21 additions and 18 deletions
  1. +5
    -1
      code/espurna/config/general.h
  2. +14
    -0
      code/espurna/espurna.ino
  3. +0
    -15
      code/espurna/system.ino
  4. +2
    -2
      code/espurna/utils.ino

+ 5
- 1
code/espurna/config/general.h View File

@ -21,7 +21,11 @@
#endif #endif
#ifndef LOOP_DELAY_TIME #ifndef LOOP_DELAY_TIME
#define LOOP_DELAY_TIME 1 // Delay for this millis in the main loop [0-250] (see https://github.com/xoseperez/espurna/issues/1541)
#define LOOP_DELAY_TIME 10 // Delay for the main loop, in millis [0-250]
// Recommended minimum is 10, see:
// https://github.com/xoseperez/espurna/issues/1541
// https://github.com/xoseperez/espurna/issues/1631
// https://github.com/esp8266/Arduino/issues/5825
#endif #endif
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------


+ 14
- 0
code/espurna/espurna.ino View File

@ -25,6 +25,8 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
std::vector<void (*)()> _loop_callbacks; std::vector<void (*)()> _loop_callbacks;
std::vector<void (*)()> _reload_callbacks; std::vector<void (*)()> _reload_callbacks;
unsigned long _loop_delay = 0;
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------
// GENERAL CALLBACKS // GENERAL CALLBACKS
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------
@ -43,6 +45,10 @@ void espurnaReload() {
} }
} }
unsigned long espurnaLoopDelay() {
return _loop_delay;
}
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------
// BOOTING // BOOTING
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------
@ -208,6 +214,11 @@ void setup() {
// Prepare configuration for version 2.0 // Prepare configuration for version 2.0
migrate(); migrate();
// Set up delay() after loop callbacks are finished
// Note: should be after settingsSetup()
_loop_delay = atol(getSetting("loopDelay", LOOP_DELAY_TIME).c_str());
_loop_delay = constrain(_loop_delay, 0, 300);
saveSettings(); saveSettings();
} }
@ -219,4 +230,7 @@ void loop() {
(_loop_callbacks[i])(); (_loop_callbacks[i])();
} }
// Power saving delay
if (_loop_delay) delay(_loop_delay);
} }

+ 0
- 15
code/espurna/system.ino View File

@ -10,8 +10,6 @@ Copyright (C) 2019 by Xose Pérez <xose dot perez at gmail dot com>
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------
unsigned long _loop_delay = 0;
bool _system_send_heartbeat = false; bool _system_send_heartbeat = false;
unsigned char _heartbeat_mode = HEARTBEAT_MODE; unsigned char _heartbeat_mode = HEARTBEAT_MODE;
unsigned long _heartbeat_interval = HEARTBEAT_INTERVAL; unsigned long _heartbeat_interval = HEARTBEAT_INTERVAL;
@ -73,10 +71,6 @@ bool systemGetHeartbeat() {
return _system_send_heartbeat; return _system_send_heartbeat;
} }
unsigned long systemLoopDelay() {
return _loop_delay;
}
unsigned long systemLoadAverage() { unsigned long systemLoadAverage() {
return _load_average; return _load_average;
} }
@ -153,11 +147,6 @@ void systemLoop() {
} }
// -------------------------------------------------------------------------
// Power saving delay
// -------------------------------------------------------------------------
if (_loop_delay) delay(_loop_delay);
} }
void _systemSetupSpecificHardware() { void _systemSetupSpecificHardware() {
@ -194,10 +183,6 @@ void systemSetup() {
// Init device-specific hardware // Init device-specific hardware
_systemSetupSpecificHardware(); _systemSetupSpecificHardware();
// Cache loop delay value to speed things (recommended max 250ms)
_loop_delay = atol(getSetting("loopDelay", LOOP_DELAY_TIME).c_str());
_loop_delay = constrain(_loop_delay, 0, 300);
// Register Loop // Register Loop
espurnaRegisterLoop(systemLoop); espurnaRegisterLoop(systemLoop);


+ 2
- 2
code/espurna/utils.ino View File

@ -500,8 +500,8 @@ void info() {
#if ADC_MODE_VALUE == ADC_VCC #if ADC_MODE_VALUE == ADC_VCC
DEBUG_MSG_P(PSTR("[MAIN] Power: %u mV\n"), ESP.getVcc()); DEBUG_MSG_P(PSTR("[MAIN] Power: %u mV\n"), ESP.getVcc());
#endif #endif
if (systemLoopDelay()) {
DEBUG_MSG_P(PSTR("[MAIN] Power saving delay value: %lu ms\n"), systemLoopDelay());
if (espurnaLoopDelay()) {
DEBUG_MSG_P(PSTR("[MAIN] Power saving delay value: %lu ms\n"), espurnaLoopDelay());
} }
const WiFiSleepType_t sleep_mode = WiFi.getSleepMode(); const WiFiSleepType_t sleep_mode = WiFi.getSleepMode();


Loading…
Cancel
Save