Browse Source

Replacing delay with nice_delay

rfm69
Xose Pérez 6 years ago
parent
commit
8a999bb15a
13 changed files with 51 additions and 31 deletions
  1. +1
    -0
      code/espurna/config/prototypes.h
  2. +2
    -2
      code/espurna/i2c.ino
  3. +1
    -1
      code/espurna/ir.ino
  4. +1
    -1
      code/espurna/nofuss.ino
  5. +1
    -1
      code/espurna/relay.ino
  6. +2
    -2
      code/espurna/sensors/BMX280Sensor.h
  7. +2
    -2
      code/espurna/sensors/DHTSensor.h
  8. +1
    -1
      code/espurna/sensors/EmonADS1X15Sensor.h
  9. +1
    -1
      code/espurna/sensors/GUVAS12SDSensor.h
  10. +1
    -1
      code/espurna/sensors/SHT3XI2CSensor.h
  11. +1
    -1
      code/espurna/sensors/SI7021Sensor.h
  12. +36
    -17
      code/espurna/system.ino
  13. +1
    -1
      code/espurna/utils.ino

+ 1
- 0
code/espurna/config/prototypes.h View File

@ -113,3 +113,4 @@ template<typename T> void domoticzSend(const char * key, T nvalue, const char *
// Utils // Utils
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------
char * ltrim(char * s); char * ltrim(char * s);
void nice_delay(unsigned long ms);

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

@ -32,7 +32,7 @@ int _i2cClearbus(int sda, int scl) {
pinMode(sda, INPUT_PULLUP); pinMode(sda, INPUT_PULLUP);
pinMode(scl, INPUT_PULLUP); pinMode(scl, INPUT_PULLUP);
delay(2500);
nice_delay(2500);
// Wait 2.5 secs. This is strictly only necessary on the first power // Wait 2.5 secs. This is strictly only necessary on the first power
// up of the DS3231 module to allow it to initialize properly, // 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 // 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; int counter = 20;
while (scl_low && (counter > 0)) { while (scl_low && (counter > 0)) {
counter--; counter--;
delay(100);
nice_delay(100);
scl_low = (digitalRead(scl) == LOW); scl_low = (digitalRead(scl) == LOW);
} }


+ 1
- 1
code/espurna/ir.ino View File

@ -47,7 +47,7 @@ void _irProcessCode(unsigned long code) {
if (button_mode == IR_BUTTON_MODE_BRIGHTER) { if (button_mode == IR_BUTTON_MODE_BRIGHTER) {
lightBrightnessStep(button_value ? 1 : -1); lightBrightnessStep(button_value ? 1 : -1);
delay(150); //debounce
nice_delay(150); //debounce
} }
if (button_mode == IR_BUTTON_MODE_RGB) { if (button_mode == IR_BUTTON_MODE_RGB) {


+ 1
- 1
code/espurna/nofuss.ino View File

@ -135,7 +135,7 @@ void nofussSetup() {
#if WEB_SUPPORT #if WEB_SUPPORT
wsSend_P(PSTR("{\"action\": \"reload\"}")); wsSend_P(PSTR("{\"action\": \"reload\"}"));
#endif #endif
delay(100);
nice_delay(100);
} }
if (code == NOFUSS_END) { if (code == NOFUSS_END) {


+ 1
- 1
code/espurna/relay.ino View File

@ -127,7 +127,7 @@ void _relayProviderStatus(unsigned char id, bool status) {
} else { } else {
digitalWrite(_relays[id].reset_pin, pulse); digitalWrite(_relays[id].reset_pin, pulse);
} }
delay(RELAY_LATCHING_PULSE);
nice_delay(RELAY_LATCHING_PULSE);
digitalWrite(_relays[id].pin, !pulse); digitalWrite(_relays[id].pin, !pulse);
digitalWrite(_relays[id].reset_pin, !pulse); digitalWrite(_relays[id].reset_pin, !pulse);
} }


+ 2
- 2
code/espurna/sensors/BMX280Sensor.h View File

@ -183,7 +183,7 @@ class BMX280Sensor : public I2CSensor {
void _init() { void _init() {
// Make sure sensor had enough time to turn on. BMX280 requires 2ms to start up // 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 // No chip ID by default
_chip = 0; _chip = 0;
@ -301,7 +301,7 @@ class BMX280Sensor : public I2CSensor {
value = (value & 0xFC) + 0x01; value = (value & 0xFC) + 0x01;
i2c_write_uint8(_address, BMX280_REGISTER_CONTROL, value); i2c_write_uint8(_address, BMX280_REGISTER_CONTROL, value);
delay(_measurement_delay);
nice_delay(_measurement_delay);
} }


+ 2
- 2
code/espurna/sensors/DHTSensor.h View File

@ -140,13 +140,13 @@ class DHTSensor : public BaseSensor {
if (++_errors > DHT_MAX_ERRORS) { if (++_errors > DHT_MAX_ERRORS) {
_errors = 0; _errors = 0;
digitalWrite(_gpio, HIGH); digitalWrite(_gpio, HIGH);
delay(250);
nice_delay(250);
} }
pinMode(_gpio, OUTPUT); pinMode(_gpio, OUTPUT);
noInterrupts(); noInterrupts();
digitalWrite(_gpio, LOW); digitalWrite(_gpio, LOW);
if (_type == DHT_CHIP_DHT11) { if (_type == DHT_CHIP_DHT11) {
delay(20);
nice_delay(20);
} else { } else {
delayMicroseconds(500); delayMicroseconds(500);
} }


+ 1
- 1
code/espurna/sensors/EmonADS1X15Sensor.h View File

@ -318,7 +318,7 @@ class EmonADS1X15Sensor : public EmonSensor {
setConfigRegistry(channel, true, false); setConfigRegistry(channel, true, false);
setConfigRegistry(channel, false, false); setConfigRegistry(channel, false, false);
setConfigRegistry(channel, false, true); setConfigRegistry(channel, false, true);
delay(10);
nice_delay(10);
readADC(channel); readADC(channel);
previous = channel; previous = channel;
} }


+ 1
- 1
code/espurna/sensors/GUVAS12SDSensor.h View File

@ -125,7 +125,7 @@ class GUVAS12SDSensor : public BaseSensor {
#else #else
for (unsigned int i=0; i < UV_SAMPLE_RATE; i++) { for (unsigned int i=0; i < UV_SAMPLE_RATE; i++) {
_average += analogRead(0); _average += analogRead(0);
delay(2);
nice_delay(2);
} }
_average = (_average / UV_SAMPLE_RATE); _average = (_average / UV_SAMPLE_RATE);
#endif #endif


+ 1
- 1
code/espurna/sensors/SHT3XI2CSensor.h View File

@ -63,7 +63,7 @@ class SHT3XI2CSensor : public I2CSensor {
unsigned char buffer[6]; unsigned char buffer[6];
i2c_write_uint8(_address, 0x2C, 0x06); i2c_write_uint8(_address, 0x2C, 0x06);
delay(500);
nice_delay(500);
i2c_read_buffer(_address, buffer, 6); i2c_read_buffer(_address, buffer, 6);
// cTemp msb, cTemp lsb, cTemp crc, humidity msb, humidity lsb, humidity crc // cTemp msb, cTemp lsb, cTemp crc, humidity msb, humidity lsb, humidity crc


+ 1
- 1
code/espurna/sensors/SI7021Sensor.h View File

@ -145,7 +145,7 @@ class SI7021Sensor : public I2CSensor {
// is needed to wait for the measurement. // is needed to wait for the measurement.
// According to datasheet the max. conversion time is ~22ms // According to datasheet the max. conversion time is ~22ms
unsigned long start = millis(); unsigned long start = millis();
while (millis() - start < 50) delay(1);
nice_delay(50);
// Clear the last to bits of LSB to 00. // Clear the last to bits of LSB to 00.
// According to datasheet LSB of RH is always xxxxxx10 // According to datasheet LSB of RH is always xxxxxx10


+ 36
- 17
code/espurna/system.ino View File

@ -10,7 +10,7 @@ Copyright (C) 2018 by Xose Pérez <xose dot perez at gmail dot com>
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------
unsigned long _loopDelay = 0;
unsigned long _loop_delay = 0;
bool _system_send_heartbeat = false; bool _system_send_heartbeat = false;
// Calculated load average 0 to 100; // Calculated load average 0 to 100;
@ -66,13 +66,24 @@ void systemSendHeartbeat() {
_system_send_heartbeat = true; _system_send_heartbeat = true;
} }
unsigned long systemLoopDelay() {
return _loop_delay;
}
void systemLoop() { void systemLoop() {
// -------------------------------------------------------------------------
// Check system stability // Check system stability
// -------------------------------------------------------------------------
#if SYSTEM_CHECK_ENABLED #if SYSTEM_CHECK_ENABLED
systemCheckLoop(); systemCheckLoop();
#endif #endif
// -------------------------------------------------------------------------
// Heartbeat
// -------------------------------------------------------------------------
#if HEARTBEAT_ENABLED #if HEARTBEAT_ENABLED
// Heartbeat // Heartbeat
static unsigned long last_hbeat = 0; static unsigned long last_hbeat = 0;
@ -83,26 +94,33 @@ void systemLoop() {
} }
#endif // HEARTBEAT_ENABLED #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++; load_counter_temp++;
if (millis() - last_loadcheck > LOADAVG_INTERVAL) { 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 // Power saving delay
delay(_loopDelay);
// -------------------------------------------------------------------------
delay(_loop_delay);
} }
@ -135,7 +153,8 @@ void systemSetup() {
#endif #endif
// Cache loop delay value to speed things (recommended max 250ms) // 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 // Register Loop
espurnaRegisterLoop(systemLoop); espurnaRegisterLoop(systemLoop);
@ -143,5 +162,5 @@ void systemSetup() {
} }
unsigned long getLoadAverage() { unsigned long getLoadAverage() {
return _load_average;
return _load_average;
} }

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

@ -428,7 +428,7 @@ void info() {
DEBUG_MSG_P(PSTR("[INIT] Power: %u mV\n"), ESP.getVcc()); DEBUG_MSG_P(PSTR("[INIT] Power: %u mV\n"), ESP.getVcc());
#endif #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 SYSTEM_CHECK_ENABLED
if (!systemCheck()) DEBUG_MSG_P(PSTR("\n[INIT] Device is in SAFE MODE\n")); if (!systemCheck()) DEBUG_MSG_P(PSTR("\n[INIT] Device is in SAFE MODE\n"));


Loading…
Cancel
Save