From 6fc9dccfe83158f5900c0ec68a2e1e060cd79538 Mon Sep 17 00:00:00 2001 From: Theo Arends <11044339+arendst@users.noreply.github.com> Date: Wed, 30 Oct 2019 14:08:43 +0100 Subject: [PATCH] Some CppCheck optimizations --- tasmota/settings.ino | 4 +--- tasmota/support.ino | 10 ++++------ tasmota/support_button.ino | 6 ++---- tasmota/support_command.ino | 5 ++--- tasmota/support_float.ino | 3 +-- tasmota/support_rtc.ino | 3 --- tasmota/support_switch.ino | 6 ++---- 7 files changed, 12 insertions(+), 25 deletions(-) diff --git a/tasmota/settings.ino b/tasmota/settings.ino index d29ea206a..c05935208 100644 --- a/tasmota/settings.ino +++ b/tasmota/settings.ino @@ -522,8 +522,6 @@ void SettingsErase(uint8_t type) */ #ifndef FIRMWARE_MINIMAL - bool result; - uint32_t _sectorStart = (ESP.getSketchSize() / SPI_FLASH_SEC_SIZE) + 1; uint32_t _sectorEnd = ESP.getFlashChipRealSize() / SPI_FLASH_SEC_SIZE; if (1 == type) { @@ -540,7 +538,7 @@ void SettingsErase(uint8_t type) AddLog_P2(LOG_LEVEL_DEBUG, PSTR(D_LOG_APPLICATION D_ERASE " %d " D_UNIT_SECTORS), _sectorEnd - _sectorStart); for (uint32_t _sector = _sectorStart; _sector < _sectorEnd; _sector++) { - result = ESP.flashEraseSector(_sector); + bool result = ESP.flashEraseSector(_sector); if (_serialoutput) { Serial.print(F(D_LOG_APPLICATION D_ERASED_SECTOR " ")); Serial.print(_sector); diff --git a/tasmota/support.ino b/tasmota/support.ino index 6588f523e..a2895e9a1 100644 --- a/tasmota/support.ino +++ b/tasmota/support.ino @@ -74,8 +74,8 @@ void OsWatchLoop(void) String GetResetReason(void) { - char buff[32]; if (oswatch_blocked_loop) { + char buff[32]; strncpy_P(buff, PSTR(D_JSON_BLOCKED_LOOP), sizeof(buff)); return String(buff); } else { @@ -288,12 +288,11 @@ char* ulltoa(unsigned long long value, char *str, int radix) char digits[64]; char *dst = str; int i = 0; - int n = 0; // if (radix < 2 || radix > 36) { radix = 10; } do { - n = value % radix; + int n = value % radix; digits[i++] = (n < 10) ? (char)n+'0' : (char)n-10+'A'; value /= radix; } while (value != 0); @@ -599,10 +598,9 @@ bool NewerVersion(char* version_str) char* GetPowerDevice(char* dest, uint32_t idx, size_t size, uint32_t option) { - char sidx[8]; - strncpy_P(dest, S_RSLT_POWER, size); // POWER if ((devices_present + option) > 1) { + char sidx[8]; snprintf_P(sidx, sizeof(sidx), PSTR("%d"), idx); // x strncat(dest, sidx, size - strlen(dest) -1); // POWERx } @@ -1545,7 +1543,6 @@ void GetLog(uint32_t idx, char** entry_pp, size_t* len_p) void Syslog(void) { // Destroys log_data - char syslog_preamble[64]; // Hostname + Id uint32_t current_hash = GetHash(Settings.syslog_host, strlen(Settings.syslog_host)); if (syslog_host_hash != current_hash) { @@ -1553,6 +1550,7 @@ void Syslog(void) WiFi.hostByName(Settings.syslog_host, syslog_host_addr); // If sleep enabled this might result in exception so try to do it once using hash } if (PortUdp.beginPacket(syslog_host_addr, Settings.syslog_port)) { + char syslog_preamble[64]; // Hostname + Id snprintf_P(syslog_preamble, sizeof(syslog_preamble), PSTR("%s ESP-"), my_hostname); memmove(log_data + strlen(syslog_preamble), log_data, sizeof(log_data) - strlen(syslog_preamble)); log_data[sizeof(log_data) -1] = '\0'; diff --git a/tasmota/support_button.ino b/tasmota/support_button.ino index d4a269017..d1367eca7 100644 --- a/tasmota/support_button.ino +++ b/tasmota/support_button.ino @@ -109,8 +109,6 @@ void ButtonHandler(void) { if (uptime < 4) { return; } // Block GPIO for 4 seconds after poweron to workaround Wemos D1 / Obi RTS circuit - uint8_t button = NOT_PRESSED; - uint8_t button_present = 0; uint8_t hold_time_extent = IMMINENT_RESET_FACTOR; // Extent hold time factor in case of iminnent Reset command uint16_t loops_per_second = 1000 / Settings.button_debounce; // ButtonDebounce (50) char scmnd[20]; @@ -118,8 +116,8 @@ void ButtonHandler(void) // uint8_t maxdev = (devices_present > MAX_KEYS) ? MAX_KEYS : devices_present; // for (uint32_t button_index = 0; button_index < maxdev; button_index++) { for (uint32_t button_index = 0; button_index < MAX_KEYS; button_index++) { - button = NOT_PRESSED; - button_present = 0; + uint8_t button = NOT_PRESSED; + uint8_t button_present = 0; if (!button_index && ((SONOFF_DUAL == my_module_type) || (CH4 == my_module_type))) { button_present = 1; diff --git a/tasmota/support_command.ino b/tasmota/support_command.ino index 3915e9ea7..3b657c375 100644 --- a/tasmota/support_command.ino +++ b/tasmota/support_command.ino @@ -148,11 +148,11 @@ void CommandHandler(char* topicBuf, char* dataBuf, uint32_t data_len) char *type = strrchr(topicBuf, '/'); // Last part of received topic is always the command (type) - uint32_t i = 0; uint32_t index = 1; bool user_index = false; if (type != nullptr) { type++; + uint32_t i; for (i = 0; i < strlen(type); i++) { type[i] = toupper(type[i]); } @@ -902,11 +902,10 @@ void CmndGpios(void) { myio cmodule; ModuleGpios(&cmodule); - uint32_t midx; uint32_t lines = 1; bool jsflg = false; for (uint32_t i = 0; i < sizeof(kGpioNiceList); i++) { - midx = pgm_read_byte(kGpioNiceList + i); + uint32_t midx = pgm_read_byte(kGpioNiceList + i); if ((XdrvMailbox.payload != 255) && GetUsedInModule(midx, cmodule.io)) { continue; } if (!jsflg) { Response_P(PSTR("{\"" D_CMND_GPIOS "%d\":{"), lines); diff --git a/tasmota/support_float.ino b/tasmota/support_float.ino index 9ad556354..e6e788f3a 100644 --- a/tasmota/support_float.ino +++ b/tasmota/support_float.ino @@ -124,10 +124,9 @@ double TaylorLog(double x) double step = ((x - 1) * (x - 1)) / ((x + 1) * (x + 1)); // Store step to not have to calculate it each time double totalValue = 0; double powe = 1; - double y; for (uint32_t count = 0; count < 10; count++) { // Experimental number of 10 iterations z *= step; - y = (1 / powe) * z; + double y = (1 / powe) * z; totalValue = totalValue + y; powe = powe + 2; } diff --git a/tasmota/support_rtc.ino b/tasmota/support_rtc.ino index 94ea9bebe..62f2581c8 100644 --- a/tasmota/support_rtc.ino +++ b/tasmota/support_rtc.ino @@ -283,9 +283,6 @@ void BreakTime(uint32_t time_input, TIME_T &tm) time -= days; // now it is days in this year, starting at 0 tm.day_of_year = time; - days = 0; - month = 0; - month_length = 0; for (month = 0; month < 12; month++) { if (1 == month) { // february if (LEAP_YEAR(year)) { diff --git a/tasmota/support_switch.ino b/tasmota/support_switch.ino index 5415c1210..af085468a 100644 --- a/tasmota/support_switch.ino +++ b/tasmota/support_switch.ino @@ -133,8 +133,6 @@ void SwitchHandler(uint8_t mode) { if (uptime < 4) { return; } // Block GPIO for 4 seconds after poweron to workaround Wemos D1 / Obi RTS circuit - uint8_t button = NOT_PRESSED; - uint8_t switchflag; uint16_t loops_per_second = 1000 / Settings.switch_debounce; for (uint32_t i = 0; i < MAX_SWITCHES; i++) { @@ -147,12 +145,12 @@ void SwitchHandler(uint8_t mode) } } - button = Switch.virtual_state[i]; + uint8_t button = Switch.virtual_state[i]; // enum SwitchModeOptions {TOGGLE, FOLLOW, FOLLOW_INV, PUSHBUTTON, PUSHBUTTON_INV, PUSHBUTTONHOLD, PUSHBUTTONHOLD_INV, PUSHBUTTON_TOGGLE, MAX_SWITCH_OPTION}; if (button != Switch.last_state[i]) { - switchflag = POWER_TOGGLE +1; + uint8_t switchflag = POWER_TOGGLE +1; switch (Settings.switchmode[i]) { case TOGGLE: switchflag = POWER_TOGGLE; // Toggle