Refactor code structure for improved readability and maintainability

This commit is contained in:
Joshua King
2026-02-21 22:25:59 -05:00
parent df00d77ce1
commit a80a6b59d1
4 changed files with 679 additions and 7 deletions

608
compile_commands.json Normal file

File diff suppressed because one or more lines are too long

View File

@@ -13,8 +13,12 @@ lib_deps =
adafruit/Adafruit MAX1704X adafruit/Adafruit MAX1704X
bblanchon/ArduinoJson bblanchon/ArduinoJson
board_upload.flash_size = 16MB
board_build.partitions = default_16MB.csv
board_build.arduino.memory_type = qio_opi
board_build.extra_flags = -DBOARD_HAS_PSRAM
; board_build.filesystem = spiffs ; board_build.filesystem = spiffs
; board_build.partitions = default.csv
build_flags = build_flags =
-D PB_VERSION=\"1.0.0\" -D PB_VERSION=\"1.0.0\"

View File

@@ -33,7 +33,10 @@ void Settings::saveWiFi(const String& ssid, const String& pass) {
} }
void Settings::clearWiFi() { _prefs.remove("wifi_ssid"); _prefs.remove("wifi_pass"); } void Settings::clearWiFi() { _prefs.remove("wifi_ssid"); _prefs.remove("wifi_pass"); }
String Settings::plantProfile() const { return _prefs.getString("plant_profile", "house"); } String Settings::plantProfile() const {
if (!_prefs.isKey("plant_profile")) return "house";
return _prefs.getString("plant_profile", "house");
}
void Settings::setPlantProfile(const String& key) { _prefs.putString("plant_profile", key); } void Settings::setPlantProfile(const String& key) { _prefs.putString("plant_profile", key); }
PlantThresholds Settings::thresholds() const { return thresholdsForProfile(plantProfile()); } PlantThresholds Settings::thresholds() const { return thresholdsForProfile(plantProfile()); }
@@ -53,7 +56,10 @@ void Settings::setWebhookEnabled(bool v) { _prefs.putBool("wh_en", v); }
String Settings::webhookUrl() const { return _prefs.getString("wh_url", ""); } String Settings::webhookUrl() const { return _prefs.getString("wh_url", ""); }
void Settings::setWebhookUrl(const String& url) { _prefs.putString("wh_url", url); } void Settings::setWebhookUrl(const String& url) { _prefs.putString("wh_url", url); }
String Settings::timezone() const { return _prefs.getString("tz", String(PB_TZ)); } String Settings::timezone() const {
if (!_prefs.isKey("tz")) return String(PB_TZ);
return _prefs.getString("tz", String(PB_TZ));
}
void Settings::setTimezone(const String& tz) { void Settings::setTimezone(const String& tz) {
String v = tz; String v = tz;
v.trim(); v.trim();
@@ -61,12 +67,18 @@ void Settings::setTimezone(const String& tz) {
_prefs.putString("tz", v); _prefs.putString("tz", v);
} }
String Settings::bedtime() const { return _prefs.getString("bed", "22:00"); } String Settings::bedtime() const {
if (!_prefs.isKey("bed")) return "23:00";
return _prefs.getString("bed", "23:00");
}
void Settings::setBedtime(const String& hhmm) { void Settings::setBedtime(const String& hhmm) {
_prefs.putString("bed", isValidHHMM(hhmm) ? hhmm : "22:00"); _prefs.putString("bed", isValidHHMM(hhmm) ? hhmm : "23:00");
} }
String Settings::wakeTime() const { return _prefs.getString("wake", "07:00"); } String Settings::wakeTime() const {
if (!_prefs.isKey("wake")) return "07:00";
return _prefs.getString("wake", "07:00");
}
void Settings::setWakeTime(const String& hhmm) { void Settings::setWakeTime(const String& hhmm) {
_prefs.putString("wake", isValidHHMM(hhmm) ? hhmm : "07:00"); _prefs.putString("wake", isValidHHMM(hhmm) ? hhmm : "07:00");
} }

View File

@@ -1,16 +1,56 @@
#include "Display.h" #include "Display.h"
static void logI2CScan() {
Serial.println("[Display] Scanning I2C bus...");
uint8_t found = 0;
for (uint8_t addr = 1; addr < 127; addr++) {
Wire.beginTransmission(addr);
uint8_t err = Wire.endTransmission();
if (err == 0) {
Serial.print("[Display] I2C device @ 0x");
if (addr < 16) Serial.print('0');
Serial.println(addr, HEX);
found++;
}
}
if (found == 0) Serial.println("[Display] No I2C devices found");
}
void Display::begin() { void Display::begin() {
Serial.println("[Display] begin()");
Serial.print("[Display] SDA=");
Serial.print(PIN_SDA);
Serial.print(" SCL=");
Serial.print(PIN_SCL);
Serial.print(" addr=0x");
if (OLED_ADDR < 16) Serial.print('0');
Serial.println(OLED_ADDR, HEX);
Wire.begin(PIN_SDA, PIN_SCL); Wire.begin(PIN_SDA, PIN_SCL);
delay(20);
logI2CScan();
Serial.println("[Display] Initializing SH1106...");
_ok = _oled.begin(OLED_ADDR, true); _ok = _oled.begin(OLED_ADDR, true);
if (!_ok) return; Serial.print("[Display] SH1106 begin() -> ");
Serial.println(_ok ? "OK" : "FAIL");
if (!_ok) {
Serial.println("[Display] Hint: verify address (0x3C/0x3D), power, and SH1106 wiring");
return;
}
_displayEnabled = true; _displayEnabled = true;
setContrast(_contrast); setContrast(_contrast);
Serial.println("[Display] Running boot animation");
bootAnimation(); bootAnimation();
Serial.println("[Display] Ready");
} }
void Display::showStatus(const String& line1, const String& line2) { void Display::showStatus(const String& line1, const String& line2) {
if (!_ok) return; if (!_ok) return;
Serial.print("[Display] showStatus: ");
Serial.print(line1);
Serial.print(" | ");
Serial.println(line2);
if (!_displayEnabled) setDisplayEnabled(true); if (!_displayEnabled) setDisplayEnabled(true);
_oled.clearDisplay(); _oled.clearDisplay();
_oled.setTextSize(1); _oled.setTextSize(1);
@@ -25,6 +65,12 @@ void Display::showStatus(const String& line1, const String& line2) {
void Display::setContrast(uint8_t contrast) { void Display::setContrast(uint8_t contrast) {
if (!_ok) return; if (!_ok) return;
_contrast = contrast; _contrast = contrast;
static uint8_t lastLogged = 0xFF;
if (lastLogged == 0xFF || abs((int)_contrast - (int)lastLogged) >= 16) {
Serial.print("[Display] Contrast -> ");
Serial.println(_contrast);
lastLogged = _contrast;
}
_oled.oled_command(SH110X_SETCONTRAST); _oled.oled_command(SH110X_SETCONTRAST);
_oled.oled_command(_contrast); _oled.oled_command(_contrast);
} }
@@ -34,6 +80,8 @@ void Display::setDisplayEnabled(bool enabled) {
if (_displayEnabled == enabled) return; if (_displayEnabled == enabled) return;
_displayEnabled = enabled; _displayEnabled = enabled;
Serial.print("[Display] Power -> ");
Serial.println(enabled ? "ON" : "OFF");
if (enabled) { if (enabled) {
_oled.oled_command(SH110X_DISPLAYON); _oled.oled_command(SH110X_DISPLAYON);
setContrast(_contrast); setContrast(_contrast);