#include // Lokalny tag logów static const char *TAG_SETTINGS = "SETTINGS"; Settings::Settings(Display &display, RTC_DS3231 &rtc): display_(display), rtc_(rtc){} Settings::~Settings() {} void Settings::begin() { pinMode(BTN_UP, INPUT_PULLUP); pinMode(BTN_OK, INPUT_PULLUP); pinMode(BTN_DOWN, INPUT_PULLUP); } // Zwraca true, jeśli przycisk jest wciśnięty bool Settings::isPressed(uint8_t btnIndex) { //ESP_LOGI(TAG_SETTINGS, "BTN check"); switch (btnIndex) { case 1: return digitalRead(BTN_UP) == LOW; case 2: return digitalRead(BTN_OK) == LOW; case 3: return digitalRead(BTN_DOWN) == LOW; default: return false; } } // Kombinacja 1 i 3 jednocześnie bool Settings::isBtnReset() { ESP_LOGI(TAG_SETTINGS, "BTN reset check"); return (digitalRead(BTN_UP) == LOW && digitalRead(BTN_DOWN) == LOW); } bool Settings::isSetClock() { ESP_LOGI(TAG_SETTINGS, "BTN set clock"); return (digitalRead(BTN_OK) == LOW); } /* Ustawienie opcji */ void Settings::setConfigDevice() { ESP_LOGI(TAG_SETTINGS, "Set config device"); display_.clear(); delay(300); config.duration = editField(config.duration, 1, 20, "Test duration"); uint16_t dur = config.pause/1000; dur = editField(dur, 1, 20, "Pause between"); config.pause = dur * 1000; config.measure = editField(config.measure, 0, 1, "Autorun test"); ESP_LOGI(TAG_SETTINGS, "Config finish"); } void Settings::finishConfigDevice(){ display_.clear(); display_.textCenter(1, "CONFIG UPDATED"); display_.textCenter(2, "RESTARTING"); delay(1000); display_.clear(); ESP.restart(); } /* -------------------------------------------------------------------- * Ustawianie czasu RTC: * Kolejność: rok -> miesiąc -> dzień -> godzina -> minuta -> sekunda * ------------------------------------------------------------------*/ void Settings::setTimeRTC() { DateTime now = rtc_.now(); RtcDateTime t; t.year = now.year(); t.month = now.month(); t.day = now.day(); t.hour = now.hour(); t.minute = now.minute(); t.second = now.second(); ESP_LOGI(TAG_SETTINGS, "Start RTC edit: %04u-%02u-%02u %02u:%02u:%02u", t.year, t.month, t.day, t.hour, t.minute, t.second); display_.clear(); delay(300); // Kolejne pola t.year = editField(t.year, 2024, 2099, "YEAR"); t.month = editField(t.month, 1, 12, "MONTH"); // uproszczenie: 1–31 bez sprawdzania długości miesiąca t.day = editField(t.day, 1, 31, "DAY"); t.hour = editField(t.hour, 0, 23, "HOUR"); t.minute = editField(t.minute, 0, 59, "MINUTE"); t.second = editField(t.second, 0, 59, "SECOND"); // Zapis do RTC rtc_.adjust(DateTime(t.year, t.month, t.day, t.hour, t.minute, t.second)); ESP_LOGI(TAG_SETTINGS, "RTC set to: %04u-%02u-%02u %02u:%02u:%02u", t.year, t.month, t.day, t.hour, t.minute, t.second); display_.clear(); display_.textCenter(1, "RTC UPDATED"); display_.textCenter(2, "RESTARTING"); //display_.textCenter(2, "OK TO CONTINUE"); delay(1000); display_.clear(); ESP.restart(); } // Edycja jednej wartości (rok/miesiąc/dzień/godz/min/sek) int Settings::editField(int value, int minVal, int maxVal, const char *label) { bool lastUp = false; bool lastDown = false; bool lastOk = false; constrainValue(value, minVal, maxVal); printField(label, value); while (true) { bool up = readBtnUp(); bool ok = readBtnOk(); bool down = readBtnDown(); // Zmiana przy puszczeniu/wciśnięciu (zbocze narastające) if (up && !lastUp) { value++; if (value > maxVal) value = minVal; printField(label, value); } if (down && !lastDown) { value--; if (value < minVal) value = maxVal; printField(label, value); } if (ok && !lastOk) { ESP_LOGI(TAG_SETTINGS, "[OK] %s = %d", label, value); // mały debounce na wyjście z pola delay(200); return value; } lastUp = up; lastDown = down; lastOk = ok; delay(80); //debouncing } } void Settings::constrainValue(int &value, int minVal, int maxVal) { if (value < minVal) value = minVal; if (value > maxVal) value = maxVal; } // Wyświetlanie aktualnie edytowanego pola na LCD void Settings::printField(const char *label, int value) { char buf[21]; display_.clear(); // Linia 0: nazwa pola snprintf(buf, sizeof(buf), "%s:", label); display_.text(0, buf); // Linia 1: wartość snprintf(buf, sizeof(buf), "%d", value); display_.text(1, buf); // Linia 3: podpowiedź display_.textStatus("UP/DOWN, OK=Next"); ESP_LOGI(TAG_SETTINGS, "%s = %d", label, value); }