diff --git a/lib/SparkFun_VL53L1X/src/SparkFun_VL53L1X.cpp b/lib/SparkFun_VL53L1X/src/SparkFun_VL53L1X.cpp index 64a56f7a6..da65b21f8 100644 --- a/lib/SparkFun_VL53L1X/src/SparkFun_VL53L1X.cpp +++ b/lib/SparkFun_VL53L1X/src/SparkFun_VL53L1X.cpp @@ -33,6 +33,12 @@ #include #include "SparkFun_VL53L1X.h" +#include "st_src/RangeSensor.h" +#include "st_src/vl53l1_error_codes.h" +#include "st_src/ComponentObject.h" +#include "st_src/RangeSensor.h" + + SFEVL53L1X::SFEVL53L1X() { _i2cPort = &Wire; diff --git a/lib/SparkFun_VL53L1X/src/SparkFun_VL53L1X.h b/lib/SparkFun_VL53L1X/src/SparkFun_VL53L1X.h index 08b0960aa..af2f61f88 100644 --- a/lib/SparkFun_VL53L1X/src/SparkFun_VL53L1X.h +++ b/lib/SparkFun_VL53L1X/src/SparkFun_VL53L1X.h @@ -33,12 +33,8 @@ #pragma once #include "Arduino.h" -#include "Wire.h" -#include "st_src/RangeSensor.h" -#include "st_src/vl53l1_error_codes.h" + #include "st_src/vl53l1x_class.h" -#include "st_src/ComponentObject.h" -#include "st_src/RangeSensor.h" #define DISTANCE_SHORT 1 #define DISTANCE_LONG 2 diff --git a/src/ESPEasy/eeprom/Helpers/EEPROMExternal.cpp b/src/ESPEasy/eeprom/Helpers/EEPROMExternal.cpp index 3339a43cf..6fd2bf331 100644 --- a/src/ESPEasy/eeprom/Helpers/EEPROMExternal.cpp +++ b/src/ESPEasy/eeprom/Helpers/EEPROMExternal.cpp @@ -1,9 +1,10 @@ #include "../Helpers/EEPROMExternal.h" #if FEATURE_EEPROM_EXTERNAL +# include "../../../ESPEasy_common.h" +# include "../../../src/DataStructs/TimingStats.h" # include "../../../src/Globals/Settings.h" # include "../../../src/Helpers/I2C_access.h" -# include "../../../ESPEasy_common.h" # include "../../../src/Helpers/StringConverter.h" @@ -362,10 +363,17 @@ bool writeEEPROMSlot(uint32_t slot, const uint32_t addr = getEEPROMAddressForSlot(slot); if ((addr != std::numeric_limits::max()) && !isEEPROMExternalWriteProtected()) { - const ESPEASY_RULES_FLOAT_TYPE oldData = EEPROMExternal->readDouble(addr); + ESPEASY_RULES_FLOAT_TYPE oldData{}; + { + START_TIMER; + oldData = EEPROMExternal->readDouble(addr); + STOP_TIMER(READ_EEPROM_SLOT); + } if (!essentiallyEqual(oldData, data)) { + START_TIMER; EEPROMExternal->writeDouble(addr, data); // Always write double size! + STOP_TIMER(WRITE_EEPROM_SLOT); } return true; } @@ -377,15 +385,14 @@ bool writeEEPROMSlot(uint32_t slot, */ ESPEASY_RULES_FLOAT_TYPE readEEPROMSlot(uint32_t slot) { const uint32_t addr = getEEPROMAddressForSlot(slot); + ESPEASY_RULES_FLOAT_TYPE res{}; if (addr != std::numeric_limits::max()) { - return EEPROMExternal->readDouble(addr); + START_TIMER; + res = EEPROMExternal->readDouble(addr); + STOP_TIMER(READ_EEPROM_SLOT); } - # if FEATURE_USE_DOUBLE_AS_ESPEASY_RULES_FLOAT_TYPE - return 0.0; - # else // if FEATURE_USE_DOUBLE_AS_ESPEASY_RULES_FLOAT_TYPE - return 0.0f; - # endif // if FEATURE_USE_DOUBLE_AS_ESPEASY_RULES_FLOAT_TYPE + return res; } } // namespace eeprom diff --git a/src/ESPEasy/eeprom/Helpers/RTCSRAMStorage.cpp b/src/ESPEasy/eeprom/Helpers/RTCSRAMStorage.cpp index 3910c50ee..8f88aa528 100644 --- a/src/ESPEasy/eeprom/Helpers/RTCSRAMStorage.cpp +++ b/src/ESPEasy/eeprom/Helpers/RTCSRAMStorage.cpp @@ -1,5 +1,7 @@ #include "../Helpers/RTCSRAMStorage.h" #if FEATURE_RTC_SRAM_STORAGE + +# include "../../../src/DataStructs/TimingStats.h" # include "../../../src/Globals/Settings.h" # include "../../../src/Helpers/I2C_access.h" # include "../../../ESPEasy_common.h" @@ -170,16 +172,20 @@ bool writeRTCSRAMSlot(uint32_t slot, const ExtTimeSource_e type = Settings.ExtTimeSource(); uint8_t _b[sizeof_rtcsram_slot]{}; + START_TIMER; + switch (type) { case ExtTimeSource_e::DS1307: { RTC_DS1307 rtc; rtc.readnvram(_b, sizeof_rtcsram_slot, addr); + STOP_TIMER(READ_RTC_SLOT); const SRAM_STORAGE_FLOAT_TYPE oldData = *(SRAM_STORAGE_FLOAT_TYPE *)&_b[0]; if (!essentiallyEqual(oldData, data)) { rtc.writenvram(addr, (uint8_t *)&data, sizeof_rtcsram_slot); + STOP_TIMER(WRITE_RTC_SLOT); } return true; } @@ -187,10 +193,12 @@ bool writeRTCSRAMSlot(uint32_t slot, { RTC_DS3231 rtc; rtc.readnvram(_b, sizeof_rtcsram_slot, addr); + STOP_TIMER(READ_RTC_SLOT); const SRAM_STORAGE_FLOAT_TYPE oldData = *(SRAM_STORAGE_FLOAT_TYPE *)&_b[0]; if (!essentiallyEqual(oldData, data)) { rtc.writenvram(addr, (uint8_t *)&data, sizeof_rtcsram_slot); + STOP_TIMER(WRITE_RTC_SLOT); } return true; } @@ -205,10 +213,12 @@ bool writeRTCSRAMSlot(uint32_t slot, } rtc.readnvram(_b, sizeof_rtcsram_slot, addr); + STOP_TIMER(READ_RTC_SLOT); const SRAM_STORAGE_FLOAT_TYPE oldData = *(SRAM_STORAGE_FLOAT_TYPE *)&_b[0]; if (!essentiallyEqual(oldData, data)) { rtc.writenvram(addr, (uint8_t *)&data, sizeof_rtcsram_slot); + STOP_TIMER(WRITE_RTC_SLOT); } return true; } @@ -233,6 +243,7 @@ SRAM_STORAGE_FLOAT_TYPE readRTCSRAMSlot(uint32_t slot) { if ((addr != std::numeric_limits::max()) && (selectRTCSRAMI2CBus() > 0)) { const ExtTimeSource_e type = Settings.ExtTimeSource(); uint8_t _b[sizeof_rtcsram_slot]{}; + START_TIMER; switch (type) { @@ -240,12 +251,14 @@ SRAM_STORAGE_FLOAT_TYPE readRTCSRAMSlot(uint32_t slot) { { RTC_DS1307 rtc; rtc.readnvram(_b, sizeof_rtcsram_slot, addr); + STOP_TIMER(READ_RTC_SLOT); return *(SRAM_STORAGE_FLOAT_TYPE *)&_b[0]; } case ExtTimeSource_e::DS3232: { RTC_DS3231 rtc; rtc.readnvram(_b, sizeof_rtcsram_slot, addr); + STOP_TIMER(READ_RTC_SLOT); return *(SRAM_STORAGE_FLOAT_TYPE *)&_b[0]; } # if FEATURE_EXT_RTC_PCF8583 @@ -259,6 +272,7 @@ SRAM_STORAGE_FLOAT_TYPE readRTCSRAMSlot(uint32_t slot) { } rtc.readnvram(_b, sizeof_rtcsram_slot, addr); + STOP_TIMER(READ_RTC_SLOT); return *(SRAM_STORAGE_FLOAT_TYPE *)&_b[0]; } # endif // if FEATURE_EXT_RTC_PCF8583 diff --git a/src/src/DataStructs/TimingStats.cpp b/src/src/DataStructs/TimingStats.cpp index 22e323278..3ab1445f2 100644 --- a/src/src/DataStructs/TimingStats.cpp +++ b/src/src/DataStructs/TimingStats.cpp @@ -21,9 +21,11 @@ void TimingStats::add(int32_t duration_usec) { _timeTotal += static_cast(duration_usec); ++_count; - if (static_cast(duration_usec) > _maxVal) { _maxVal = duration_usec; } + const uint32_t duration_usec_u(static_cast(duration_usec)); - if (static_cast(duration_usec) < _minVal) { _minVal = duration_usec; } + if (duration_usec_u > _maxVal) { _maxVal = duration_usec_u; } + + if (duration_usec_u < _minVal) { _minVal = duration_usec_u; } } void TimingStats::reset() { @@ -49,10 +51,7 @@ uint32_t TimingStats::getMinMax(uint32_t& minVal, uint32_t& maxVal) const { } bool TimingStats::thresholdExceeded(const uint32_t& threshold) const { - if (_count == 0) { - return false; - } - return _maxVal > threshold; + return (_count > 0u) && (_maxVal > threshold); } /********************************************************************************************\ @@ -208,9 +207,7 @@ bool mustLogCFunction(CPlugin::Function function) { bool mustLogNWFunction(NWPlugin::Function function) { - if (!Settings.EnableTimingStats()) { return false; } - - return true; + return Settings.EnableTimingStats(); } @@ -285,6 +282,16 @@ const __FlashStringHelper* getMiscStatsName_F(TimingStatsElements stat) { case TimingStatsElements::HANDLE_SERVING_WEBPAGE_JSON: return F("handle webpage JSON"); case TimingStatsElements::WIFI_SCAN_ASYNC: return F("WiFi Scan Async"); case TimingStatsElements::WIFI_SCAN_SYNC: return F("WiFi Scan Sync (blocking)"); + // EEPROM/RTC related +#if FEATURE_EEPROM_EXTERNAL + case TimingStatsElements::READ_EEPROM_SLOT: return F("readEEPROMSlot()"); + case TimingStatsElements::WRITE_EEPROM_SLOT: return F("writeEEPROMSlot()"); +#endif +#if FEATURE_RTC_SRAM_STORAGE + case TimingStatsElements::READ_RTC_SLOT: return F("readRTCSlot()"); + case TimingStatsElements::WRITE_RTC_SLOT: return F("writeRTCSlot()"); +#endif + case TimingStatsElements::NTP_SUCCESS: return F("NTP Success"); case TimingStatsElements::NTP_FAIL: return F("NTP Fail"); case TimingStatsElements::SYSTIME_UPDATED: return F("Systime Set"); @@ -338,28 +345,33 @@ String getMiscStatsName(TimingStatsElements stat) { void stopTimerTask(deviceIndex_t T, int F, uint32_t statisticsTimerStart) { - if (mustLogFunction(F)) { pluginStats[static_cast(T.value) * 256 + (F)].add(usecPassedSince_fast(statisticsTimerStart)); } + if (!mustLogFunction(F)) return; + pluginStats[(static_cast(T.value) << 8) + (F)].add(usecPassedSince_fast(statisticsTimerStart)); } void stopTimerController(protocolIndex_t T, CPlugin::Function F, uint32_t statisticsTimerStart) { - if (mustLogCFunction(F)) { controllerStats[static_cast(T) * 256 + static_cast(F)].add(usecPassedSince_fast(statisticsTimerStart)); } + if (!mustLogCFunction(F)) return; + controllerStats[(static_cast(T) << 8) + static_cast(F)].add(usecPassedSince_fast(statisticsTimerStart)); } void stopTimerNetwork(ESPEasy::net::networkDriverIndex_t T, NWPlugin::Function F, uint32_t statisticsTimerStart) { - if (mustLogNWFunction(F)) { networkStats[static_cast(T.value) * 256 + static_cast(F)].add(usecPassedSince_fast(statisticsTimerStart)); } + if (!mustLogNWFunction(F)) return; + networkStats[(static_cast(T.value) << 8) + static_cast(F)].add(usecPassedSince_fast(statisticsTimerStart)); } void stopTimer(TimingStatsElements L, uint32_t statisticsTimerStart) { - if (Settings.EnableTimingStats()) { miscStats[L].add(usecPassedSince_fast(statisticsTimerStart)); } + if (!Settings.EnableTimingStats()) return; + miscStats[L].add(usecPassedSince_fast(statisticsTimerStart)); } void addMiscTimerStat(TimingStatsElements L, int32_t T) { - if (Settings.EnableTimingStats()) { miscStats[L].add(T); } + if (!Settings.EnableTimingStats()) return; + miscStats[L].add(T); } #endif // if FEATURE_TIMING_STATS diff --git a/src/src/DataStructs/TimingStats.h b/src/src/DataStructs/TimingStats.h index 354140a7a..4d48c0839 100644 --- a/src/src/DataStructs/TimingStats.h +++ b/src/src/DataStructs/TimingStats.h @@ -136,6 +136,16 @@ enum class TimingStatsElements { WIFI_SCAN_ASYNC, WIFI_SCAN_SYNC, + // EEPROM/RTC related +#if FEATURE_EEPROM_EXTERNAL + READ_EEPROM_SLOT, + WRITE_EEPROM_SLOT, +#endif +#if FEATURE_RTC_SRAM_STORAGE + READ_RTC_SLOT, + WRITE_RTC_SLOT, +#endif + // Time sync (also network related) NTP_SUCCESS, NTP_FAIL, diff --git a/src/src/Globals/Plugins.cpp b/src/src/Globals/Plugins.cpp index aa1320beb..aaa44768a 100644 --- a/src/src/Globals/Plugins.cpp +++ b/src/src/Globals/Plugins.cpp @@ -207,15 +207,22 @@ uint8_t getTaskI2CAddress(taskIndex_t taskIndex) { // ******************************************************************************** #if FEATURE_I2C bool prepare_I2C_by_taskIndex(taskIndex_t taskIndex, deviceIndex_t DeviceIndex) { - if (!validTaskIndex(taskIndex) || !validDeviceIndex(DeviceIndex)) { +if (!Settings.TaskDeviceEnabled[taskIndex] || + !validTaskIndex(taskIndex) || + !validDeviceIndex(DeviceIndex)) { return false; } if (Device[DeviceIndex].Type != DEVICE_TYPE_I2C) { return true; // No I2C task, so consider all-OK } + #if FEATURE_I2C_MULTIPLE + const uint8_t i2cBus = Settings.getI2CInterface(taskIndex); + #else // if FEATURE_I2C_MULTIPLE + const uint8_t i2cBus = 0; + #endif // if FEATURE_I2C_MULTIPLE - if (!Settings.isI2CEnabled(Settings.getI2CInterface(taskIndex))) { + if (!Settings.isI2CEnabled(i2cBus)) { return false; // Plugin-selected I2C bus is not configured, fail } #if FEATURE_CLEAR_I2C_STUCK @@ -223,11 +230,6 @@ bool prepare_I2C_by_taskIndex(taskIndex_t taskIndex, deviceIndex_t DeviceIndex) return false; // Bus state is not OK, so do not consider task runnable } #endif - #if FEATURE_I2C_MULTIPLE - const uint8_t i2cBus = Settings.getI2CInterface(taskIndex); - #else // if FEATURE_I2C_MULTIPLE - const uint8_t i2cBus = 0; - #endif // if FEATURE_I2C_MULTIPLE if (bitRead(Settings.I2C_SPI_bus_Flags[taskIndex], I2C_FLAGS_SLOW_SPEED)) { I2CSelectLowClockSpeed(i2cBus); // Set to slow, also switch the bus @@ -246,7 +248,9 @@ bool prepare_I2C_by_taskIndex(taskIndex_t taskIndex, deviceIndex_t DeviceIndex) } void post_I2C_by_taskIndex(taskIndex_t taskIndex, deviceIndex_t DeviceIndex) { - if (!validTaskIndex(taskIndex) || !validDeviceIndex(DeviceIndex)) { +if (!Settings.TaskDeviceEnabled[taskIndex] || + !validTaskIndex(taskIndex) || + !validDeviceIndex(DeviceIndex)) { return; } @@ -258,6 +262,11 @@ void post_I2C_by_taskIndex(taskIndex_t taskIndex, deviceIndex_t DeviceIndex) { #else // if FEATURE_I2C_MULTIPLE const uint8_t i2cBus = 0; #endif // ifdef ESP32 + + if (!Settings.isI2CEnabled(i2cBus)) { + return; // Plugin-selected I2C bus is not configured, fail + } + #if FEATURE_I2CMULTIPLEXER I2CMultiplexerOff(i2cBus); #endif // if FEATURE_I2CMULTIPLEXER diff --git a/src/src/PluginStructs/P113_data_struct.cpp b/src/src/PluginStructs/P113_data_struct.cpp index 43bc3eb96..5ce34c7a9 100644 --- a/src/src/PluginStructs/P113_data_struct.cpp +++ b/src/src/PluginStructs/P113_data_struct.cpp @@ -2,6 +2,13 @@ #ifdef USES_P113 +# if P113_USE_ROI +# include "../Static/WebStaticData.h" // Javascript and support functions +# endif // if P113_USE_ROI + +# include + + P113_data_struct::P113_data_struct(uint8_t i2c_addr, int timing, bool range) : i2cAddress(i2c_addr), timing(timing), range(range) { sensor = new (std::nothrow) SFEVL53L1X(); } diff --git a/src/src/PluginStructs/P113_data_struct.h b/src/src/PluginStructs/P113_data_struct.h index 9b81c80cd..26ccadf22 100644 --- a/src/src/PluginStructs/P113_data_struct.h +++ b/src/src/PluginStructs/P113_data_struct.h @@ -23,11 +23,8 @@ # endif // if defined(LIMIT_BUILD_SIZE) && defined(ESP8266) # endif // ifndef P113_USE_ROI -# include +class SFEVL53L1X; // Forward declaration -# if P113_USE_ROI -# include "../Static/WebStaticData.h" // Javascript and support functions -# endif // if P113_USE_ROI # define P113_I2C_ADDRESS PCONFIG(0) # define P113_TIMING PCONFIG(1)