Skip to content

Commit

Permalink
Fixed cppcheck findings (#101)
Browse files Browse the repository at this point in the history
* Fix for some cppcheck findings

* Fixed the rest of the "fixable" cppcheck findings
  • Loading branch information
Martinius79 authored Nov 8, 2024
1 parent 2e1db8c commit 5527205
Show file tree
Hide file tree
Showing 11 changed files with 40 additions and 46 deletions.
2 changes: 1 addition & 1 deletion EleksTubeHAX_pio/src/Buttons.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

class Button {
public:
Button(uint8_t bpin, uint8_t active_state=LOW, uint32_t long_press_ms=500)
explicit Button(uint8_t bpin, uint8_t active_state=LOW, uint32_t long_press_ms=500)
: bpin(bpin), active_state(active_state), long_press_ms(long_press_ms),
down_last_time(false), state_changed(false), millis_at_last_transition(0), button_state(idle) {}

Expand Down
35 changes: 17 additions & 18 deletions EleksTubeHAX_pio/src/ChipSelect.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,32 +18,31 @@ class ChipSelect {
// So 0 is disabled, 1 is enabled (even though CS is active low, this gets mapped.)
// So bit 0 (LSB), is index 0, is SECONDS_ONES
// Translation to what the 74HC595 uses is done in update()
void setDigitMap(uint8_t map, bool update_=true) { digits_map = map; if (update_) update(); }
uint8_t getDigitMap() { return digits_map; }
void setDigitMap(uint8_t map, bool update_=true) { digits_map = map; if (update_) update(); }
uint8_t getDigitMap() { return digits_map; }

// Helper functions
// Sets just the one digit by digit number
void setDigit(uint8_t digit, bool update_=true) { setDigitMap(0x01 << digit, update_); }
void setAll(bool update_=true) { setDigitMap(all_on, update_); }
void clear(bool update_=true) { setDigitMap(all_off, update_); }
void setSecondsOnes() { setDigit(SECONDS_ONES); }
void setSecondsTens() { setDigit(SECONDS_TENS); }
void setMinutesOnes() { setDigit(MINUTES_ONES); }
void setMinutesTens() { setDigit(MINUTES_TENS); }
void setHoursOnes() { setDigit(HOURS_ONES); }
void setHoursTens() { setDigit(HOURS_TENS); }
bool isSecondsOnes() { return (digits_map&SECONDS_ONES_MAP > 0); }
bool isSecondsTens() { return (digits_map&SECONDS_TENS_MAP > 0); }
bool isMinutesOnes() { return (digits_map&MINUTES_ONES_MAP > 0); }
bool isMinutesTens() { return (digits_map&MINUTES_TENS_MAP > 0); }
bool isHoursOnes() { return (digits_map&HOURS_ONES_MAP > 0); }
bool isHoursTens() { return (digits_map&HOURS_TENS_MAP > 0); }
void setDigit(uint8_t digit, bool update_ = true) { setDigitMap(0x01 << digit, update_); }
void setAll(bool update_ = true) { setDigitMap(all_on, update_); }
void clear(bool update_ = true) { setDigitMap(all_off, update_); }
void setSecondsOnes() { setDigit(SECONDS_ONES); }
void setSecondsTens() { setDigit(SECONDS_TENS); }
void setMinutesOnes() { setDigit(MINUTES_ONES); }
void setMinutesTens() { setDigit(MINUTES_TENS); }
void setHoursOnes() { setDigit(HOURS_ONES); }
void setHoursTens() { setDigit(HOURS_TENS); }
bool isSecondsOnes() { return ((digits_map & SECONDS_ONES_MAP) > 0); }
bool isSecondsTens() { return ((digits_map & SECONDS_TENS_MAP) > 0); }
bool isMinutesOnes() { return ((digits_map & MINUTES_ONES_MAP) > 0); }
bool isMinutesTens() { return ((digits_map & MINUTES_TENS_MAP) > 0); }
bool isHoursOnes() { return ((digits_map & HOURS_ONES_MAP) > 0); }
bool isHoursTens() { return ((digits_map & HOURS_TENS_MAP) > 0); }

private:
uint8_t digits_map;
const uint8_t all_on = 0x3F;
const uint8_t all_off = 0x00;
};


#endif // CHIP_SELECT_H
4 changes: 2 additions & 2 deletions EleksTubeHAX_pio/src/Clock.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ void Clock::loop() {
// Static methods used for sync provider to TimeLib library.
time_t Clock::syncProvider() {
Serial.println("syncProvider()");
time_t ntp_now, rtc_now;
time_t rtc_now;
rtc_now = RtcGet();

if (millis() - millis_last_ntp > refresh_ntp_every_ms || millis_last_ntp == 0) {
Expand All @@ -110,7 +110,7 @@ time_t Clock::syncProvider() {
// ntpTimeClient.forceUpdate(); // maybe this breaks the NTP requests as this should not be done more than every minute.
if (ntpTimeClient.update()) {
Serial.print(".");
ntp_now = ntpTimeClient.getEpochTime();
time_t ntp_now = ntpTimeClient.getEpochTime();
Serial.println("NTP query done.");
Serial.print("NTP time = ");
Serial.println(ntpTimeClient.getFormattedTime());
Expand Down
2 changes: 1 addition & 1 deletion EleksTubeHAX_pio/src/IPGeolocation_AO.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ struct IPGeo {
class IPGeolocation
{
public:
IPGeolocation(String Key);
explicit IPGeolocation(String Key);
IPGeolocation(String Key, String API); // Use IPG for api.ipgeolocation.io and ABSTRACT for app.abstractapi.com/api/ip-geolocation
bool updateStatus(IPGeo *I);
String getResponse();
Expand Down
2 changes: 1 addition & 1 deletion EleksTubeHAX_pio/src/Menu.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

class Menu {
public:
Menu() : state(idle), change(0), millis_last_button_press(0) {}
Menu() : state(idle), change(0), millis_last_button_press(0), state_changed(false) {}
void begin() {}
void loop(Buttons &buttons);

Expand Down
2 changes: 1 addition & 1 deletion EleksTubeHAX_pio/src/NTPClient_AO.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ bool NTPClient::forceUpdate() {
if( _packetBuffer[16] == 0 && _packetBuffer[17] == 0 &&
_packetBuffer[18] == 0 && _packetBuffer[19] == 0 &&
_packetBuffer[20] == 0 && _packetBuffer[21] == 0 &&
_packetBuffer[22] == 0 && _packetBuffer[22] == 0) //Check for ReferenceTimestamp != 0
_packetBuffer[22] == 0 && _packetBuffer[23] == 0) //Check for ReferenceTimestamp != 0
{
#ifdef DEBUG_NTPClient
Serial.println("err: Incorrect NTP Ref Timestamp");
Expand Down
6 changes: 3 additions & 3 deletions EleksTubeHAX_pio/src/NTPClient_AO.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ class NTPClient {
bool sendNTPPacket();

public:
NTPClient(UDP& udp);
NTPClient(UDP& udp, long timeOffset);
NTPClient(UDP& udp, const char* poolServerName);
explicit NTPClient(UDP& udp);
explicit NTPClient(UDP& udp, long timeOffset);
explicit NTPClient(UDP& udp, const char* poolServerName);
NTPClient(UDP& udp, const char* poolServerName, long timeOffset);
NTPClient(UDP& udp, const char* poolServerName, long timeOffset, unsigned long updateInterval);

Expand Down
2 changes: 1 addition & 1 deletion EleksTubeHAX_pio/src/StoredConfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

class StoredConfig {
public:
StoredConfig() : prefs(), config_size(sizeof(config)), loaded(false) {}
StoredConfig() : prefs(), config_size(sizeof(config)), loaded(false), config() {}
void begin() { prefs.begin(SAVED_CONFIG_NAMESPACE, false); Serial.print("Config size: "); Serial.println(config_size); }
void load() { prefs.getBytes(SAVED_CONFIG_NAMESPACE, &config, config_size); loaded = true; }
void save() { prefs.putBytes(SAVED_CONFIG_NAMESPACE, &config, config_size); }
Expand Down
14 changes: 7 additions & 7 deletions EleksTubeHAX_pio/src/TFTs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -470,7 +470,7 @@ void TFTs::DrawImage(uint8_t file_index) {

bool oldSwapBytes = getSwapBytes();
setSwapBytes(true);
pushImage(0,0, TFT_WIDTH, TFT_HEIGHT, (uint16_t *)UnpackedImageBuffer);
pushImage(0, 0, TFT_WIDTH, TFT_HEIGHT, reinterpret_cast<uint16_t *>(UnpackedImageBuffer));
setSwapBytes(oldSwapBytes);

#ifdef DEBUG_OUTPUT
Expand All @@ -486,17 +486,17 @@ void TFTs::DrawImage(uint8_t file_index) {

uint16_t TFTs::read16(fs::File &f) {
uint16_t result;
((uint8_t *)&result)[0] = f.read(); // LSB
((uint8_t *)&result)[1] = f.read(); // MSB
reinterpret_cast<uint8_t*>(&result)[0] = f.read(); // LSB
reinterpret_cast<uint8_t*>(&result)[1] = f.read(); // MSB
return result;
}

uint32_t TFTs::read32(fs::File &f) {
uint32_t result;
((uint8_t *)&result)[0] = f.read(); // LSB
((uint8_t *)&result)[1] = f.read();
((uint8_t *)&result)[2] = f.read();
((uint8_t *)&result)[3] = f.read(); // MSB
reinterpret_cast<uint8_t*>(&result)[0] = f.read(); // LSB
reinterpret_cast<uint8_t*>(&result)[1] = f.read();
reinterpret_cast<uint8_t*>(&result)[2] = f.read();
reinterpret_cast<uint8_t*>(&result)[3] = f.read(); // MSB
return result;
}

Expand Down
14 changes: 5 additions & 9 deletions EleksTubeHAX_pio/src/WiFi_WPS.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -166,8 +166,8 @@ void WifiReconnect() {
#ifdef WIFI_USE_WPS //// WPS code
void WiFiStartWps() {
// erase settings
sprintf(stored_config.config.wifi.ssid, "");
sprintf(stored_config.config.wifi.password, "");
snprintf(stored_config.config.wifi.ssid, sizeof(stored_config.config.wifi.ssid), "%s", WiFi.SSID().c_str());
stored_config.config.wifi.password[0] = '\0'; // empty string as password
stored_config.config.wifi.WPS_connected = 0x11; // invalid = different than 0x55
Serial.println(""); Serial.print("Saving config! Triggered from WPS start...");
stored_config.save();
Expand Down Expand Up @@ -200,12 +200,9 @@ void WiFiStartWps() {
Serial.print(".");
}
tfts.setTextColor(TFT_WHITE, TFT_BLACK);
sprintf(stored_config.config.wifi.ssid, "%s", WiFi.SSID());
// memset(stored_config.config.wifi.ssid, '\0', sizeof(stored_config.config.wifi.ssid));
// strcpy(stored_config.config.wifi.ssid, WiFi.SSID());

sprintf(stored_config.config.wifi.password, ""); // can't save a password from WPS
stored_config.config.wifi.WPS_connected = StoredConfig::valid;
snprintf(stored_config.config.wifi.ssid, sizeof(stored_config.config.wifi.ssid), "%s", WiFi.SSID().c_str()); // Copy the SSID into the stored configuration safely
stored_config.config.wifi.password[0] = '\0'; // Since the password cannot be retrieved from WPS, set it to an empty string
stored_config.config.wifi.WPS_connected = StoredConfig::valid; // Mark the configuration as valid
Serial.println(); Serial.print("Saving config! Triggered from WPS success...");
stored_config.save();
Serial.println(" WPS finished.");
Expand All @@ -220,7 +217,6 @@ void WiFiStartWps() {
bool GetGeoLocationTimeZoneOffset() {
Serial.println("Starting Geolocation query...");
// https://app.abstractapi.com/api/ip-geolocation/ // free for 5k loopkups per month.
// Live test: https://ipgeolocation.abstractapi.com/v1/?api_key=e11dc0f9bab446bfa9957aad2c4ad064
IPGeolocation location(GEOLOCATION_API_KEY,"ABSTRACT");
IPGeo IPG;
if (location.updateStatus(&IPG)) {
Expand Down
3 changes: 1 addition & 2 deletions EleksTubeHAX_pio/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -490,8 +490,7 @@ void loop() {
if (menu_change != 0) {
// calculate the new offset
time_t newOffsetAdjustmentValue = menu_change * 3600;
time_t newOffset = currOffset + newOffsetAdjustmentValue;
double newOffsetInHours = static_cast<double>(newOffset) / 3600;
time_t newOffset = currOffset + newOffsetAdjustmentValue;

// check if the new offset is within the allowed range of -12 to +12 hours
// If the minutes part of the offset is 0, we want to change from +12 to -12 or vice versa (without changing the shown time on the displays)
Expand Down

0 comments on commit 5527205

Please sign in to comment.