From c9a379eaae0ed4806abb504b67b321a5944a8edc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Patrick=20Ha=C3=9Fel?= Date: Mon, 17 Feb 2025 20:08:17 +0100 Subject: [PATCH] Fermenter display --- src/Fermenter.cpp | 47 ++++++++++++++++++++++++++++++++--------------- 1 file changed, 32 insertions(+), 15 deletions(-) diff --git a/src/Fermenter.cpp b/src/Fermenter.cpp index f1bc03c..250118e 100644 --- a/src/Fermenter.cpp +++ b/src/Fermenter.cpp @@ -16,34 +16,50 @@ DS18B20 ds18b20("DS18B20", D4); DS18B20Sensor input(ds18b20, 0, ""); -PWMOutput heater(D2, ""); +PWMOutput heater(D2, "", 100); PIDController pid("fermenter", input, heater, UNIT_TEMPERATURE_C, 500, 0.00000002, 0); +auto displayToggleInterval = 2000UL; + auto display = LedControl(13, 14, 15, 1); -void displayDecimal(int *digit, const double value) { - const auto integer = static_cast(value); - const auto decimal = static_cast((value - integer) * 10) % 10; - display.setDigit(0, (*digit)++, decimal, false); - display.setDigit(0, (*digit)++, integer % 10, true); - display.setDigit(0, (*digit)++, integer / 10 % 10, false); +void displayPrintf(const char *format, ...) { + va_list args; + va_start(args, format); + char buffer[9]; + vsnprintf(buffer, sizeof buffer, format, args); + int position = 0; + for (char *b = buffer; *b != 0 && b < buffer + sizeof buffer; b++) { + display.setChar(0, position++, *b, false); + } + va_end(args); } void displayLoop() { - static unsigned long lastDisplayInit = 0; - if (lastDisplayInit == 0 || millis() - lastDisplayInit > 60 * 60 * 1000) { - lastDisplayInit = millis(); + const auto now = millis(); + + static unsigned long lastInit = 0; + if (lastInit == 0 || now - lastInit >= 60 * 60 * 1000) { + lastInit = now; display.shutdown(0, true); display.shutdown(0, false); display.setIntensity(0, 4); display.clearDisplay(0); } - auto digit = 0; - displayDecimal(&digit, input.getValue()); - digit++; - digit++; - displayDecimal(&digit, pid.targetValue); + + static auto mode = true; + static auto lastMode = 0UL; + if (lastMode == 0 || now - lastMode >= displayToggleInterval) { + lastMode = now; + mode = !mode; + } + + if (mode) { + displayPrintf("%3.0f ", heater.getPercent()); + } else { + displayPrintf("%-4.1f%4.1f", input.getValue(), pid.targetValue); + } } void httpStatus(AsyncWebServerRequest *request) { @@ -111,6 +127,7 @@ void patrixLoop() { ds18b20.loop(); input.loop(); pid.loop(); + displayLoop(); } #endif