Refactor code structure for improved readability and maintainability
This commit is contained in:
@@ -33,7 +33,10 @@ void Settings::saveWiFi(const String& ssid, const String& 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); }
|
||||
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", ""); }
|
||||
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) {
|
||||
String v = tz;
|
||||
v.trim();
|
||||
@@ -61,12 +67,18 @@ void Settings::setTimezone(const String& tz) {
|
||||
_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) {
|
||||
_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) {
|
||||
_prefs.putString("wake", isValidHHMM(hhmm) ? hhmm : "07:00");
|
||||
}
|
||||
|
||||
@@ -1,16 +1,56 @@
|
||||
#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() {
|
||||
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);
|
||||
delay(20);
|
||||
logI2CScan();
|
||||
|
||||
Serial.println("[Display] Initializing SH1106...");
|
||||
_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;
|
||||
setContrast(_contrast);
|
||||
Serial.println("[Display] Running boot animation");
|
||||
bootAnimation();
|
||||
Serial.println("[Display] Ready");
|
||||
}
|
||||
|
||||
void Display::showStatus(const String& line1, const String& line2) {
|
||||
if (!_ok) return;
|
||||
Serial.print("[Display] showStatus: ");
|
||||
Serial.print(line1);
|
||||
Serial.print(" | ");
|
||||
Serial.println(line2);
|
||||
if (!_displayEnabled) setDisplayEnabled(true);
|
||||
_oled.clearDisplay();
|
||||
_oled.setTextSize(1);
|
||||
@@ -25,6 +65,12 @@ void Display::showStatus(const String& line1, const String& line2) {
|
||||
void Display::setContrast(uint8_t contrast) {
|
||||
if (!_ok) return;
|
||||
_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(_contrast);
|
||||
}
|
||||
@@ -34,6 +80,8 @@ void Display::setDisplayEnabled(bool enabled) {
|
||||
if (_displayEnabled == enabled) return;
|
||||
|
||||
_displayEnabled = enabled;
|
||||
Serial.print("[Display] Power -> ");
|
||||
Serial.println(enabled ? "ON" : "OFF");
|
||||
if (enabled) {
|
||||
_oled.oled_command(SH110X_DISPLAYON);
|
||||
setContrast(_contrast);
|
||||
|
||||
Reference in New Issue
Block a user