diff --git a/code/espurna/config/prototypes.h b/code/espurna/config/prototypes.h index d9ac791f..27c6c773 100644 --- a/code/espurna/config/prototypes.h +++ b/code/espurna/config/prototypes.h @@ -113,3 +113,4 @@ template void domoticzSend(const char * key, T nvalue, const char * // Utils // ----------------------------------------------------------------------------- char * ltrim(char * s); +void nice_delay(unsigned long ms); diff --git a/code/espurna/i2c.ino b/code/espurna/i2c.ino index 9dc0f882..b828754b 100644 --- a/code/espurna/i2c.ino +++ b/code/espurna/i2c.ino @@ -32,7 +32,7 @@ int _i2cClearbus(int sda, int scl) { pinMode(sda, INPUT_PULLUP); pinMode(scl, INPUT_PULLUP); - delay(2500); + nice_delay(2500); // Wait 2.5 secs. This is strictly only necessary on the first power // up of the DS3231 module to allow it to initialize properly, // but is also assists in reliable programming of FioV3 boards as it gives the @@ -68,7 +68,7 @@ int _i2cClearbus(int sda, int scl) { int counter = 20; while (scl_low && (counter > 0)) { counter--; - delay(100); + nice_delay(100); scl_low = (digitalRead(scl) == LOW); } diff --git a/code/espurna/ir.ino b/code/espurna/ir.ino index ae951baf..241c6fa2 100644 --- a/code/espurna/ir.ino +++ b/code/espurna/ir.ino @@ -47,7 +47,7 @@ void _irProcessCode(unsigned long code) { if (button_mode == IR_BUTTON_MODE_BRIGHTER) { lightBrightnessStep(button_value ? 1 : -1); - delay(150); //debounce + nice_delay(150); //debounce } if (button_mode == IR_BUTTON_MODE_RGB) { diff --git a/code/espurna/nofuss.ino b/code/espurna/nofuss.ino index 776f4cf4..60b41b72 100644 --- a/code/espurna/nofuss.ino +++ b/code/espurna/nofuss.ino @@ -135,7 +135,7 @@ void nofussSetup() { #if WEB_SUPPORT wsSend_P(PSTR("{\"action\": \"reload\"}")); #endif - delay(100); + nice_delay(100); } if (code == NOFUSS_END) { diff --git a/code/espurna/relay.ino b/code/espurna/relay.ino index b9f845d7..4611c0e9 100644 --- a/code/espurna/relay.ino +++ b/code/espurna/relay.ino @@ -127,7 +127,7 @@ void _relayProviderStatus(unsigned char id, bool status) { } else { digitalWrite(_relays[id].reset_pin, pulse); } - delay(RELAY_LATCHING_PULSE); + nice_delay(RELAY_LATCHING_PULSE); digitalWrite(_relays[id].pin, !pulse); digitalWrite(_relays[id].reset_pin, !pulse); } diff --git a/code/espurna/sensors/BMX280Sensor.h b/code/espurna/sensors/BMX280Sensor.h index 9782c95e..68914be2 100644 --- a/code/espurna/sensors/BMX280Sensor.h +++ b/code/espurna/sensors/BMX280Sensor.h @@ -183,7 +183,7 @@ class BMX280Sensor : public I2CSensor { void _init() { // Make sure sensor had enough time to turn on. BMX280 requires 2ms to start up - delay(10); + nice_delay(10); // No chip ID by default _chip = 0; @@ -301,7 +301,7 @@ class BMX280Sensor : public I2CSensor { value = (value & 0xFC) + 0x01; i2c_write_uint8(_address, BMX280_REGISTER_CONTROL, value); - delay(_measurement_delay); + nice_delay(_measurement_delay); } diff --git a/code/espurna/sensors/DHTSensor.h b/code/espurna/sensors/DHTSensor.h index fe590b84..2651eadb 100644 --- a/code/espurna/sensors/DHTSensor.h +++ b/code/espurna/sensors/DHTSensor.h @@ -140,13 +140,13 @@ class DHTSensor : public BaseSensor { if (++_errors > DHT_MAX_ERRORS) { _errors = 0; digitalWrite(_gpio, HIGH); - delay(250); + nice_delay(250); } pinMode(_gpio, OUTPUT); noInterrupts(); digitalWrite(_gpio, LOW); if (_type == DHT_CHIP_DHT11) { - delay(20); + nice_delay(20); } else { delayMicroseconds(500); } diff --git a/code/espurna/sensors/EmonADS1X15Sensor.h b/code/espurna/sensors/EmonADS1X15Sensor.h index 26f18c40..56bbb164 100644 --- a/code/espurna/sensors/EmonADS1X15Sensor.h +++ b/code/espurna/sensors/EmonADS1X15Sensor.h @@ -318,7 +318,7 @@ class EmonADS1X15Sensor : public EmonSensor { setConfigRegistry(channel, true, false); setConfigRegistry(channel, false, false); setConfigRegistry(channel, false, true); - delay(10); + nice_delay(10); readADC(channel); previous = channel; } diff --git a/code/espurna/sensors/GUVAS12SDSensor.h b/code/espurna/sensors/GUVAS12SDSensor.h index 1fb214ba..1589196d 100644 --- a/code/espurna/sensors/GUVAS12SDSensor.h +++ b/code/espurna/sensors/GUVAS12SDSensor.h @@ -125,7 +125,7 @@ class GUVAS12SDSensor : public BaseSensor { #else for (unsigned int i=0; i < UV_SAMPLE_RATE; i++) { _average += analogRead(0); - delay(2); + nice_delay(2); } _average = (_average / UV_SAMPLE_RATE); #endif diff --git a/code/espurna/sensors/SHT3XI2CSensor.h b/code/espurna/sensors/SHT3XI2CSensor.h index b3f9e8ca..46ea2cc3 100644 --- a/code/espurna/sensors/SHT3XI2CSensor.h +++ b/code/espurna/sensors/SHT3XI2CSensor.h @@ -63,7 +63,7 @@ class SHT3XI2CSensor : public I2CSensor { unsigned char buffer[6]; i2c_write_uint8(_address, 0x2C, 0x06); - delay(500); + nice_delay(500); i2c_read_buffer(_address, buffer, 6); // cTemp msb, cTemp lsb, cTemp crc, humidity msb, humidity lsb, humidity crc diff --git a/code/espurna/sensors/SI7021Sensor.h b/code/espurna/sensors/SI7021Sensor.h index 677d8b2d..991f082b 100644 --- a/code/espurna/sensors/SI7021Sensor.h +++ b/code/espurna/sensors/SI7021Sensor.h @@ -145,7 +145,7 @@ class SI7021Sensor : public I2CSensor { // is needed to wait for the measurement. // According to datasheet the max. conversion time is ~22ms unsigned long start = millis(); - while (millis() - start < 50) delay(1); + nice_delay(50); // Clear the last to bits of LSB to 00. // According to datasheet LSB of RH is always xxxxxx10 diff --git a/code/espurna/system.ino b/code/espurna/system.ino index cccd09db..866f03f3 100644 --- a/code/espurna/system.ino +++ b/code/espurna/system.ino @@ -10,7 +10,7 @@ Copyright (C) 2018 by Xose PĂ©rez // ----------------------------------------------------------------------------- -unsigned long _loopDelay = 0; +unsigned long _loop_delay = 0; bool _system_send_heartbeat = false; // Calculated load average 0 to 100; @@ -66,13 +66,24 @@ void systemSendHeartbeat() { _system_send_heartbeat = true; } +unsigned long systemLoopDelay() { + return _loop_delay; +} + void systemLoop() { + // ------------------------------------------------------------------------- // Check system stability + // ------------------------------------------------------------------------- + #if SYSTEM_CHECK_ENABLED systemCheckLoop(); #endif + // ------------------------------------------------------------------------- + // Heartbeat + // ------------------------------------------------------------------------- + #if HEARTBEAT_ENABLED // Heartbeat static unsigned long last_hbeat = 0; @@ -83,26 +94,33 @@ void systemLoop() { } #endif // HEARTBEAT_ENABLED - // Increase temporary loop counter - static unsigned long load_counter_temp = 0; - static unsigned long last_loadcheck = 0; + // ------------------------------------------------------------------------- + // Load Average calculation + // ------------------------------------------------------------------------- + static unsigned long last_loadcheck = 0; + static unsigned long load_counter_temp = 0; + static unsigned long load_counter = 0; + static unsigned long load_counter_max = 1; load_counter_temp++; + if (millis() - last_loadcheck > LOADAVG_INTERVAL) { - static unsigned long load_counter = 0; - static unsigned long load_counter_max = 1; - - load_counter = load_counter_temp; - load_counter_temp = 0; - if (load_counter > load_counter_max) { - load_counter_max = load_counter; - } - _load_average = 100 - (100 * load_counter / load_counter_max); - last_loadcheck = millis(); + + load_counter = load_counter_temp; + load_counter_temp = 0; + if (load_counter > load_counter_max) { + load_counter_max = load_counter; + } + _load_average = 100 - (100 * load_counter / load_counter_max); + last_loadcheck = millis(); + } + // ------------------------------------------------------------------------- // Power saving delay - delay(_loopDelay); + // ------------------------------------------------------------------------- + + delay(_loop_delay); } @@ -135,7 +153,8 @@ void systemSetup() { #endif // Cache loop delay value to speed things (recommended max 250ms) - _loopDelay = atol(getSetting("loopDelay", LOOP_DELAY_TIME).c_str()); + _loop_delay = atol(getSetting("loopDelay", LOOP_DELAY_TIME).c_str()); + _loop_delay = constrain(_loop_delay, 0, 300); // Register Loop espurnaRegisterLoop(systemLoop); @@ -143,5 +162,5 @@ void systemSetup() { } unsigned long getLoadAverage() { - return _load_average; + return _load_average; } diff --git a/code/espurna/utils.ino b/code/espurna/utils.ino index 7ff9f4fb..11524bc4 100644 --- a/code/espurna/utils.ino +++ b/code/espurna/utils.ino @@ -428,7 +428,7 @@ void info() { DEBUG_MSG_P(PSTR("[INIT] Power: %u mV\n"), ESP.getVcc()); #endif - DEBUG_MSG_P(PSTR("[INIT] Power saving delay value: %lu ms\n"), _loopDelay); + DEBUG_MSG_P(PSTR("[INIT] Power saving delay value: %lu ms\n"), systemLoopDelay()); #if SYSTEM_CHECK_ENABLED if (!systemCheck()) DEBUG_MSG_P(PSTR("\n[INIT] Device is in SAFE MODE\n"));