[Cleanup] Add macro NR_ELEMENTS for array counting

This commit is contained in:
TD-er
2023-09-05 16:52:03 +02:00
parent 891ca183d3
commit a8dfc009c0
19 changed files with 36 additions and 33 deletions
+1 -1
View File
@@ -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) );
+1 -1
View File
@@ -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];
}
+1 -1
View File
@@ -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);
+2 -2
View File
@@ -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]) {
+2 -2
View File
@@ -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<int>(P148_data_struct::Tm1621Device::POWR3xxD),
static_cast<int>(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'"));
+1
View File
@@ -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.
+6 -6
View File
@@ -575,15 +575,15 @@ void SettingsStruct_tmpl<N_TASKS>::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<N_TASKS>::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;
+4 -3
View File
@@ -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;
+1 -1
View File
@@ -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;
}
+1 -1
View File
@@ -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];
+1 -1
View File
@@ -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)
+1 -1
View File
@@ -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];
+2 -2
View File
@@ -43,7 +43,7 @@ void sensorTypeHelper_webformLoad_allTypes(struct EventStruct *event, uint8_t pc
#endif
static_cast<uint8_t>(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<uint8_t>(Sensor_VType::SENSOR_TYPE_TRIPLE),
static_cast<uint8_t>(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);
}
+1 -1
View File
@@ -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];
}
+5 -5
View File
@@ -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<size_t>(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<uint8_t>(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<uint8_t>(outputType);
constexpr uint8_t nrElements = sizeof(_cache) / sizeof(_cache[0]);
constexpr uint8_t nrElements = NR_ELEMENTS(_cache);
float res{};
+2 -2
View File
@@ -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<size_t>(uom);
if (!display) { ++index; }
+2 -1
View File
@@ -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());
+1 -1
View File
@@ -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)
{
+1 -1
View File
@@ -210,7 +210,7 @@ void handle_hardware() {
#endif
static_cast<int>(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);