diff --git a/src/_P005_DHT.ino b/src/_P005_DHT.ino index d0d503e77..68315c259 100644 --- a/src/_P005_DHT.ino +++ b/src/_P005_DHT.ino @@ -66,7 +66,7 @@ boolean Plugin_005(uint8_t function, struct EventStruct *event, String& string) const __FlashStringHelper * options[] = { F("DHT 11"), F("DHT 22"), F("DHT 12"), F("Sonoff am2301"), F("Sonoff si7021"), F("Sonoff MS01") }; const int indices[] = { P005_DHT11, P005_DHT22, P005_DHT12, P005_AM2301, P005_SI7021, P005_MS01 }; - constexpr size_t nrElements = sizeof(indices) / sizeof(indices[0]); + constexpr size_t nrElements = NR_ELEMENTS(indices); addFormSelector(F("Sensor model"), F("dhttype"), nrElements, options, indices, PCONFIG(0) ); diff --git a/src/_P026_Sysinfo.ino b/src/_P026_Sysinfo.ino index 188891355..9139efece 100644 --- a/src/_P026_Sysinfo.ino +++ b/src/_P026_Sysinfo.ino @@ -42,7 +42,7 @@ const __FlashStringHelper* Plugin_026_valuename(uint8_t value_nr, bool displaySt F("Free 2nd Heap"), F("free2ndheap") }; const size_t index = (2* value_nr) + (displayString ? 0 : 1); - constexpr size_t nrStrings = sizeof(strings) / sizeof(strings[0]); + constexpr size_t nrStrings = NR_ELEMENTS(strings); if (index < nrStrings) { return strings[index]; } diff --git a/src/_P076_HLW8012.ino b/src/_P076_HLW8012.ino index b19fafa98..1925b3283 100644 --- a/src/_P076_HLW8012.ino +++ b/src/_P076_HLW8012.ino @@ -173,7 +173,7 @@ boolean Plugin_076(uint8_t function, struct EventStruct *event, String& string) P076_Gosund, P076_Shelly_PLUG_S }; - constexpr int nrElements = sizeof(predefinedId) / sizeof(predefinedId[0]); + constexpr int nrElements = NR_ELEMENTS(predefinedId); addFormSelector(F("Device"), F("preDefDevSel"), nrElements, predefinedNames, predefinedId, devicePinSettings); diff --git a/src/_P078_Eastron.ino b/src/_P078_Eastron.ino index e25b56233..9e6dc3689 100644 --- a/src/_P078_Eastron.ino +++ b/src/_P078_Eastron.ino @@ -178,7 +178,7 @@ boolean Plugin_078(uint8_t function, struct EventStruct *event, String& string) F("SDM72_V2"), F("SDM320C") }; - constexpr size_t nrOptions = sizeof(options_model) / sizeof(options_model[0]); + constexpr size_t nrOptions = NR_ELEMENTS(options_model); addFormSelector(F("Model Type"), P078_MODEL_LABEL, nrOptions, options_model, nullptr, P078_MODEL); addFormNote(F("Submit after changing the modell to update Output Configuration.")); } @@ -378,7 +378,7 @@ boolean Plugin_078(uint8_t function, struct EventStruct *event, String& string) if (new_baud > 5) { const int baudrates[] = { 2400, 4800, 9600, 19200, 38400, 1200 }; - constexpr int nrBaudRates = sizeof(baudrates) / sizeof(baudrates[0]); + constexpr int nrBaudRates = NR_ELEMENTS(baudrates); for (int i = 0; i < nrBaudRates && new_baud > 5; ++i) { if (new_baud == baudrates[i]) { diff --git a/src/_P148_POWRxxD_THR3xxD.ino b/src/_P148_POWRxxD_THR3xxD.ino index 0ab3a394d..bb90e7573 100644 --- a/src/_P148_POWRxxD_THR3xxD.ino +++ b/src/_P148_POWRxxD_THR3xxD.ino @@ -71,7 +71,7 @@ boolean Plugin_148(uint8_t function, struct EventStruct *event, String& string) P148_GPIO_TM1621_RD, P148_GPIO_TM1621_CS }; - constexpr size_t nrElements = sizeof(values) / sizeof(values[0]); + constexpr size_t nrElements = NR_ELEMENTS(values); for (size_t i = 0; i < nrElements; ++i) { if (i != 0) { addHtml(event->String1); } @@ -126,7 +126,7 @@ boolean Plugin_148(uint8_t function, struct EventStruct *event, String& string) static_cast(P148_data_struct::Tm1621Device::POWR3xxD), static_cast(P148_data_struct::Tm1621Device::THR3xxD) }; - constexpr size_t nrElements = sizeof(optionValues) / sizeof(optionValues[0]); + constexpr size_t nrElements = NR_ELEMENTS(optionValues); addFormSelector(F("Device Template"), F("devtmpl"), nrElements, options, optionValues, P148_DEVICE_SELECTOR); addFormNote(F("GPIO settings will be ignored when selecting other than 'Custom'")); diff --git a/src/include/ESPEasy_config.h b/src/include/ESPEasy_config.h index f78935bd3..5c1bd4932 100644 --- a/src/include/ESPEasy_config.h +++ b/src/include/ESPEasy_config.h @@ -82,6 +82,7 @@ #define ZERO_FILL(S) memset((S), 0, sizeof(S)) #define ZERO_TERMINATE(S) S[sizeof(S) - 1] = 0 +#define NR_ELEMENTS(ARR) sizeof(ARR) / sizeof(ARR[0]) #ifdef ESP32 // Special macros to disable interrupts from within an ISR function. diff --git a/src/src/DataStructs_templ/SettingsStruct.cpp b/src/src/DataStructs_templ/SettingsStruct.cpp index 0c232fc58..78bc968d1 100644 --- a/src/src/DataStructs_templ/SettingsStruct.cpp +++ b/src/src/DataStructs_templ/SettingsStruct.cpp @@ -575,15 +575,15 @@ void SettingsStruct_tmpl::clearMisc() { // Here we initialize all data to 0, so this is the ONLY reason why PinBootStates // can now be directly accessed. // In all other use cases, use the get and set functions for it. - constexpr uint8_t maxStates = sizeof(PinBootStates) / sizeof(PinBootStates[0]); + constexpr size_t maxStates = NR_ELEMENTS(PinBootStates); - for (uint8_t i = 0; i < maxStates; ++i) { + for (size_t i = 0; i < maxStates; ++i) { PinBootStates[i] = 0; } # ifdef ESP32 - constexpr uint8_t maxStatesesp32 = sizeof(PinBootStates_ESP32) / sizeof(PinBootStates_ESP32[0]); + constexpr size_t maxStatesesp32 = NR_ELEMENTS(PinBootStates_ESP32); - for (uint8_t i = 0; i < maxStatesesp32; ++i) { + for (size_t i = 0; i < maxStatesesp32; ++i) { PinBootStates_ESP32[i] = 0; } # endif // ifdef ESP32 @@ -710,14 +710,14 @@ bool SettingsStruct_tmpl::getPinBootStateIndex( if (!GPIO_IS_VALID_GPIO(gpio_pin)) { return false; } # endif // ifdef ESP32 - constexpr uint8_t maxStates = sizeof(PinBootStates) / sizeof(PinBootStates[0]); + constexpr uint8_t maxStates = NR_ELEMENTS(PinBootStates); if (gpio_pin < maxStates) { index_low = gpio_pin; return true; } # ifdef ESP32 - constexpr uint8_t maxStatesesp32 = sizeof(PinBootStates_ESP32) / sizeof(PinBootStates_ESP32[0]); + constexpr uint8_t maxStatesesp32 = NR_ELEMENTS(PinBootStates_ESP32); index_high = gpio_pin - maxStates; diff --git a/src/src/Helpers/Convert.cpp b/src/src/Helpers/Convert.cpp index c68cb7806..e40851b60 100644 --- a/src/src/Helpers/Convert.cpp +++ b/src/src/Helpers/Convert.cpp @@ -25,7 +25,7 @@ const __FlashStringHelper * getBearing(int degrees) F("NW"), F("NNW") }; - constexpr size_t nrDirections = sizeof(directions) / sizeof(directions[0]); + constexpr size_t nrDirections = NR_ELEMENTS(directions); const float stepsize = (360.0f / nrDirections); if (degrees < 0) { degrees += 360; } // Allow for bearing -360 .. 359 @@ -38,14 +38,15 @@ const __FlashStringHelper * getBearing(int degrees) } float CelsiusToFahrenheit(float celsius) { - return celsius * (9.0f / 5.0f) + 32; + constexpr float ratio = 9.0f / 5.0f; + return celsius * ratio + 32; } int m_secToBeaufort(float m_per_sec) { // Use ints wit 0.1 m/sec resolution to reduce size. const uint16_t dm_per_sec = 10 * m_per_sec; const uint16_t speeds[]{3, 16, 34, 55, 80, 108, 139, 172, 208, 245, 285, 326}; - constexpr int nrElements = sizeof(speeds) / sizeof(speeds[0]); + constexpr int nrElements = NR_ELEMENTS(speeds); for (int bft = 0; bft < nrElements; ++bft) { if (dm_per_sec < speeds[bft]) return bft; diff --git a/src/src/Helpers/ESPEasy_Storage.cpp b/src/src/Helpers/ESPEasy_Storage.cpp index 5864d35d7..d486392c3 100644 --- a/src/src/Helpers/ESPEasy_Storage.cpp +++ b/src/src/Helpers/ESPEasy_Storage.cpp @@ -354,7 +354,7 @@ bool BuildFixes() } if (Settings.Build < 20111) { #ifdef ESP32 - constexpr uint8_t maxStatesesp32 = sizeof(Settings.PinBootStates_ESP32) / sizeof(Settings.PinBootStates_ESP32[0]); + constexpr uint8_t maxStatesesp32 = NR_ELEMENTS(Settings.PinBootStates_ESP32); for (uint8_t i = 0; i < maxStatesesp32; ++i) { Settings.PinBootStates_ESP32[i] = 0; } diff --git a/src/src/Helpers/Rules_calculate.cpp b/src/src/Helpers/Rules_calculate.cpp index f5fe0034f..1a93ecb76 100644 --- a/src/src/Helpers/Rules_calculate.cpp +++ b/src/src/Helpers/Rules_calculate.cpp @@ -647,7 +647,7 @@ String RulesCalculate_t::preProces(const String& input) }; - constexpr size_t nrOperators = sizeof(operators) / sizeof(operators[0]); + constexpr size_t nrOperators = NR_ELEMENTS(operators); for (size_t i = 0; i < nrOperators; ++i) { const UnaryOperator op = operators[i]; diff --git a/src/src/Helpers/_CPlugin_init.cpp b/src/src/Helpers/_CPlugin_init.cpp index a2741f7c4..a2e14abc5 100644 --- a/src/src/Helpers/_CPlugin_init.cpp +++ b/src/src/Helpers/_CPlugin_init.cpp @@ -2073,7 +2073,7 @@ protocolIndex_t CPlugin_id_to_ProtocolIndex[CPLUGIN_MAX + 1]{}; constexpr size_t ProtocolIndex_to_CPlugin_id_size = sizeof(ProtocolIndex_to_CPlugin_id); // constexpr size_t Plugin_ptr_size = sizeof(Plugin_ptr); -constexpr size_t CPlugin_id_to_ProtocolIndex_size = sizeof(CPlugin_id_to_ProtocolIndex) / sizeof(CPlugin_id_to_ProtocolIndex[0]); +constexpr size_t CPlugin_id_to_ProtocolIndex_size = NR_ELEMENTS(CPlugin_id_to_ProtocolIndex); protocolIndex_t getProtocolIndex_from_CPluginID_(cpluginID_t cpluginID) diff --git a/src/src/Helpers/_Plugin_Helper_serial.cpp b/src/src/Helpers/_Plugin_Helper_serial.cpp index 72d396f30..1a2628bd3 100644 --- a/src/src/Helpers/_Plugin_Helper_serial.cpp +++ b/src/src/Helpers/_Plugin_Helper_serial.cpp @@ -272,7 +272,7 @@ void serialHelper_webformLoad(ESPEasySerialPort port, int rxPinDef, int txPinDef #endif // if USES_I2C_SC16IS752 }; - constexpr int NR_ESPEASY_SERIAL_TYPES = sizeof(ids) / sizeof(ids[1]); + constexpr int NR_ESPEASY_SERIAL_TYPES = NR_ELEMENTS(ids); String options[NR_ESPEASY_SERIAL_TYPES]; // String attr[NR_ESPEASY_SERIAL_TYPES]; diff --git a/src/src/Helpers/_Plugin_SensorTypeHelper.cpp b/src/src/Helpers/_Plugin_SensorTypeHelper.cpp index 686299d40..c42b7edda 100644 --- a/src/src/Helpers/_Plugin_SensorTypeHelper.cpp +++ b/src/src/Helpers/_Plugin_SensorTypeHelper.cpp @@ -43,7 +43,7 @@ void sensorTypeHelper_webformLoad_allTypes(struct EventStruct *event, uint8_t pc #endif static_cast(Sensor_VType::SENSOR_TYPE_STRING) }; - constexpr int optionCount = sizeof(optionValues) / sizeof(optionValues[0]); + constexpr int optionCount = NR_ELEMENTS(optionValues); sensorTypeHelper_webformLoad(event, pconfigIndex, optionCount, optionValues); } @@ -56,7 +56,7 @@ void sensorTypeHelper_webformLoad_simple(struct EventStruct *event, uint8_t pcon static_cast(Sensor_VType::SENSOR_TYPE_TRIPLE), static_cast(Sensor_VType::SENSOR_TYPE_QUAD) }; - constexpr int optionCount = sizeof(optionValues) / sizeof(optionValues[0]); + constexpr int optionCount = NR_ELEMENTS(optionValues); sensorTypeHelper_webformLoad(event, pconfigIndex, optionCount, optionValues); } diff --git a/src/src/PluginStructs/P052_data_struct.cpp b/src/src/PluginStructs/P052_data_struct.cpp index 363cab2e3..a362c2eaa 100644 --- a/src/src/PluginStructs/P052_data_struct.cpp +++ b/src/src/PluginStructs/P052_data_struct.cpp @@ -30,7 +30,7 @@ const __FlashStringHelper * P052_data_struct::Plugin_052_valuename(uint8_t value F("Error Status"), F("err") }; const size_t index = (2* value_nr) + (displayString ? 0 : 1); - constexpr size_t nrStrings = sizeof(strings) / sizeof(strings[0]); + constexpr size_t nrStrings = NR_ELEMENTS(strings); if (index < nrStrings) { return strings[index]; } diff --git a/src/src/PluginStructs/P077_data_struct.cpp b/src/src/PluginStructs/P077_data_struct.cpp index 4e5f93846..2a201f00a 100644 --- a/src/src/PluginStructs/P077_data_struct.cpp +++ b/src/src/PluginStructs/P077_data_struct.cpp @@ -17,7 +17,7 @@ const __FlashStringHelper* Plugin_077_valuename(P077_query value_nr, bool displa F("Power Factor"), F("pf"), F("Reactive Power"), F("VAR") }; - constexpr size_t nrStrings = sizeof(strings) / sizeof(strings[0]); + constexpr size_t nrStrings = NR_ELEMENTS(strings); const size_t index = static_cast(value_nr) * 2 + (displayString ? 0 : 1); if (index < nrStrings) { @@ -71,7 +71,7 @@ bool P077_data_struct::init(ESPEasySerialPort port, const int16_t serial_rx, con uint32_t P077_data_struct::get_24bit_value(uint8_t offset) const { uint32_t res{}; - constexpr size_t bufsize = sizeof(serial_in_buffer) / sizeof(serial_in_buffer[0]); + constexpr size_t bufsize = NR_ELEMENTS(serial_in_buffer); if ((offset + 2u) < bufsize) { res = serial_in_buffer[offset] << 16 | @@ -311,7 +311,7 @@ bool P077_data_struct::checksumMatch() const } bool P077_data_struct::plugin_read(struct EventStruct *event) { - constexpr uint8_t nrElements = sizeof(_cache) / sizeof(_cache[0]); + constexpr uint8_t nrElements = NR_ELEMENTS(_cache); for (uint8_t i = 0; i < P077_NR_OUTPUT_VALUES; ++i) { const uint8_t pconfigIndex = i + P077_QUERY1_CONFIG_POS; @@ -414,7 +414,7 @@ int P077_data_struct::serial_Available() { void P077_data_struct::setOutputValue(struct EventStruct *event, P077_query outputType, float value) { const uint8_t index = static_cast(outputType); - constexpr uint8_t nrElements = sizeof(_cache) / sizeof(_cache[0]); + constexpr uint8_t nrElements = NR_ELEMENTS(_cache); if (index < nrElements) { _cache[index].add(value); @@ -443,7 +443,7 @@ void P077_data_struct::setOutputValue(struct EventStruct *event, P077_query outp float P077_data_struct::getValue(P077_query outputType) const { const uint8_t index = static_cast(outputType); - constexpr uint8_t nrElements = sizeof(_cache) / sizeof(_cache[0]); + constexpr uint8_t nrElements = NR_ELEMENTS(_cache); float res{}; diff --git a/src/src/PluginStructs/P078_data_struct.cpp b/src/src/PluginStructs/P078_data_struct.cpp index 48e8dfc08..f4a50c5eb 100644 --- a/src/src/PluginStructs/P078_data_struct.cpp +++ b/src/src/PluginStructs/P078_data_struct.cpp @@ -130,7 +130,7 @@ constexpr p078_register_description register_description_list[] = { }; // *INDENT-ON* -constexpr int register_description_list_size = sizeof(register_description_list) / sizeof(register_description_list[0]); +constexpr int register_description_list_size = NR_ELEMENTS(register_description_list); const __FlashStringHelper* SDM_UOMtoString(SDM_UOM uom, bool display) { const __FlashStringHelper *strings[] = { @@ -148,7 +148,7 @@ const __FlashStringHelper* SDM_UOMtoString(SDM_UOM uom, bool display) { F("Apparent Energy"), F("kVAh"), F("Reactive Energy"), F("kVArh") }; - constexpr size_t nrStrings = sizeof(strings) / sizeof(strings[0]); + constexpr size_t nrStrings = NR_ELEMENTS(strings); size_t index = 2 * static_cast(uom); if (!display) { ++index; } diff --git a/src/src/WebServer/AdvancedConfigPage.cpp b/src/src/WebServer/AdvancedConfigPage.cpp index 135f74de9..71ae6ed6c 100644 --- a/src/src/WebServer/AdvancedConfigPage.cpp +++ b/src/src/WebServer/AdvancedConfigPage.cpp @@ -321,9 +321,10 @@ void handle_advanced() { F("Dark"), }; const int cssModeOptions[] = { 0, 1, 2}; + constexpr int nrCssModeOptions = NR_ELEMENTS(cssModeOptions); addFormSelector(getLabel(LabelType::ENABLE_AUTO_DARK_MODE), getInternalLabel(LabelType::ENABLE_AUTO_DARK_MODE), - sizeof(cssModeOptions) / sizeof(int), + nrCssModeOptions, cssModeNames, cssModeOptions, Settings.getCssMode()); diff --git a/src/src/WebServer/ESPEasy_WebServer.cpp b/src/src/WebServer/ESPEasy_WebServer.cpp index bb1dd1106..58b98bbf6 100644 --- a/src/src/WebServer/ESPEasy_WebServer.cpp +++ b/src/src/WebServer/ESPEasy_WebServer.cpp @@ -320,7 +320,7 @@ void WebServerInit() // List of headers to be recorded // "If-None-Match" is used to see whether we need to serve a static file, or simply can reply with a 304 (not modified) const char * headerkeys[] = {"If-None-Match"}; - const size_t headerkeyssize = sizeof(headerkeys)/sizeof(char*); + constexpr size_t headerkeyssize = NR_ELEMENTS(headerkeys); web_server.collectHeaders(headerkeys, headerkeyssize ); #if defined(ESP8266) || defined(ESP32) { diff --git a/src/src/WebServer/HardwarePage.cpp b/src/src/WebServer/HardwarePage.cpp index da92c03c9..9f970b4f0 100644 --- a/src/src/WebServer/HardwarePage.cpp +++ b/src/src/WebServer/HardwarePage.cpp @@ -210,7 +210,7 @@ void handle_hardware() { #endif static_cast(SPI_Options_e::UserDefined) }; - constexpr size_t nrOptions = sizeof(spi_index) / sizeof(spi_index[0]); + constexpr size_t nrOptions = NR_ELEMENTS(spi_index); addFormSelector_script(F("Init SPI"), F("initspi"), nrOptions, spi_options, spi_index, nullptr, Settings.InitSPI, F("spiOptionChanged(this)")); // User-defined pins addFormPinSelect(PinSelectPurpose::SPI, formatGpioName_output(F("CLK")), F("spipinsclk"), Settings.SPI_SCLK_pin);