| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276 |
- /*
- * EchoLotSetup.cpp
- *
- * Created on: 10.01.2026
- * Author: FSmilari
- */
-
- #include "EchoLotSetup.h"
- #include "fonts/titillium_web_regular16pt7b.h"
- #include "fonts/FreeSans7pt7b.h"
-
- #define ADDR_AIR_TEMP 0 // WORD
- #define ADDR_WATER_TEMP 2 // +2 WORDs
- #define ADDR_SONIC_AIR 4 // +2 WORDs
- #define ADDR_SALTPERMIL 6 // +2 WORDs
-
- // --------------------------------------------------
- // Konstruktor
- // --------------------------------------------------
- EchoLotSetup::EchoLotSetup(Display &display) : display(display) {
- airTemp = 20.0f;
- waterTemp = 20.0f;
- sonicSpeedAir = 343.0f;
- saltPromilleWater = 35;
-
- doInitialization = true;
- configStepIndex = 0;
- }
-
- // --------------------------------------------------
- // Initialisierung
- // --------------------------------------------------
- void EchoLotSetup::initialize() {
- if (doInitialization) {
- EEPROM.init();
- readFromEEPROM();
- configStepIndex = 0;
-
- String stepTxt = ConfigStep[configStepIndex];
- String unit = getCfgOptUnitForStepIndex(configStepIndex);
- display.setStatusValues(airTemp, waterTemp, sonicSpeedAir, saltPromilleWater);
- display.setConfigText(String(airTemp, 1));
- display.setConfigOption(stepTxt);
- display.setConfigUnit(unit);
- doInitialization = false;
- }
- }
-
- // --------------------------------------------------
- // Rotary Switch (kurz)
- // --------------------------------------------------
- void EchoLotSetup::onRotaryControlerSwitch() {
- configStepIndex++;
- if (configStepIndex > 3) {
- configStepIndex = 0;
- }
-
- display.setConfigText(getCfgValueForStepIndex(configStepIndex));
- display.setConfigOption(ConfigStep[configStepIndex]);
- display.setConfigUnit(getCfgOptUnitForStepIndex(configStepIndex));
- }
-
- // --------------------------------------------------
- // Rotary Switch (lang)
- // --------------------------------------------------
- void EchoLotSetup::onRotaryControlerLongSwitch() {
- save();
- doInitialization = true;
- }
-
- // --------------------------------------------------
- // Rotary Drehung
- // --------------------------------------------------
- void EchoLotSetup::onRotaryControlerTurn(RotaryEncoder::Direction turn) {
-
- if (turn != RotaryEncoder::Direction::NOROTATION) {
-
- String value = "";
- int index = 0;
- int sign = (turn == RotaryEncoder::Direction::CLOCKWISE) ? 1 : -1;
-
- switch (configStepIndex) {
- case 0:
- setAirTemp(min(max(0.0, getAirTemp() + 0.5 * sign), 40.0));
- value = String(getAirTemp(), 1);
- break;
- case 1:
- setWaterTemp(min(max(0.0, getWaterTemp() + 0.5 * sign), 40.0));
- value = String(getWaterTemp(), 1);
- break;
- case 2:
- setSonicSpeedAir(min(max(330.0, getSonicSpeedAir() + 0.5 * sign), 343.5));
- value = String(getSonicSpeedAir(), 1);
- break;
- case 3:
- setSaltPromilleWater(min(max(0, getSaltPromilleWater() + sign), 50));
- value = String(getSaltPromilleWater());
- break;
- default:
- break;
- }
-
- display.setConfigText(value);
- }
-
- }
-
- // --------------------------------------------------
- // Speichern bestätigen
- // --------------------------------------------------
- void EchoLotSetup::save() {
- saveToEEPROM();
- }
-
- // --------------------------------------------------
- // Abbrechen
- // --------------------------------------------------
- void EchoLotSetup::cancel() {
- doInitialization = true;
- }
-
- // --------------------------------------------------
- // Display leeren
- // --------------------------------------------------
- void EchoLotSetup::clear() {
- display.clearDisplay();
- }
-
- // --------------------------------------------------
- // Initial screen zeichnen
- // --------------------------------------------------
- void EchoLotSetup::drawInitScreen() {
- display.clearDisplay();
- display.showFrame(INITIALIZATION);
- printValues();
- }
-
- // --------------------------------------------------
- // EEPROM lesen
- // --------------------------------------------------
- void EchoLotSetup::readFromEEPROM() {
- setAirTemp(readFloatFromEEPROM(ADDR_AIR_TEMP));
- setWaterTemp(readFloatFromEEPROM(ADDR_WATER_TEMP));
- setSonicSpeedAir(readFloatFromEEPROM(ADDR_SONIC_AIR));
- setSaltPromilleWater(readIntegerFromEEPROM(ADDR_SALTPERMIL));
- }
-
- // --------------------------------------------------
- // EEPROM schreiben
- // --------------------------------------------------
- void EchoLotSetup::saveToEEPROM() {
- writeFloatToEEPROM(ADDR_AIR_TEMP, airTemp);
- writeFloatToEEPROM(ADDR_WATER_TEMP, waterTemp);
- writeFloatToEEPROM(ADDR_SONIC_AIR, sonicSpeedAir);
- writeIntegerToEEPROM(ADDR_SALTPERMIL, saltPromilleWater);
- }
-
- // --------------------------------------------------
- // Werte anzeigen
- // --------------------------------------------------
- void EchoLotSetup::printValues() {
- String value = String(sonicSpeedAir);
- Serial.print(String(AIRTEMP) + ": " + String(getAirTemp(), 1) + ", ");
- Serial.print(String(WATERTEMP) + ": " + String(getWaterTemp(), 1) + ", ");
- Serial.print(String(SONICSPEEDAIR) + ": " + String(getSonicSpeedAir(), 1) + ", ");
- Serial.println(String(SALTPROMILLEWATER) + ": " + String(getSaltPromilleWater()));
-
- display.drawText(ConfigStep[0], &FreeSans7pt7b, 1, ST7735_WHITE, 7, 30);
- display.drawRightAlignedText(String(getAirTemp(), 1) + " C", &FreeSans7pt7b, 1, ST7735_WHITE, 8, 30);
- display.drawCircle(140, 23, 2, ST7735_WHITE);
-
- display.drawText(ConfigStep[1], &FreeSans7pt7b, 1, ST7735_WHITE, 7, 55);
- display.drawRightAlignedText(String(getWaterTemp(), 1) + " C", &FreeSans7pt7b, 1, ST7735_WHITE, 8, 55);
- display.drawCircle(140, 48, 2, ST7735_WHITE);
-
- display.drawText(ConfigStep[2], &FreeSans7pt7b, 1, ST7735_WHITE, 7, 80);
- display.drawRightAlignedText(String(getSonicSpeedAir(), 1) + " m/s", &FreeSans7pt7b, 1, ST7735_WHITE, 8, 80);
-
- display.drawText(ConfigStep[3], &FreeSans7pt7b, 1, ST7735_WHITE, 7, 105);
- display.drawRightAlignedText(String(getSaltPromilleWater()) + " / ", &FreeSans7pt7b, 1, ST7735_WHITE, 8, 105);
- display.drawCircle(140, 98, 2, ST7735_WHITE);
- display.drawCircle(148, 104, 2, ST7735_WHITE);
- display.drawCircle(153, 104, 2, ST7735_WHITE);
- }
-
- // --------------------------------------------------
- // Private Helper
- // --------------------------------------------------
- String EchoLotSetup::getCfgOptForStepIndex(byte index) {
- return ConfigStep[index];
- }
-
- String EchoLotSetup::getCfgOptUnitForStepIndex(byte index) {
- switch (index) {
- case 0:
- case 1:
- return " C";
- case 2:
- return "m/s";
- default:
- return " / "; // for Promille painting
- }
- }
-
- String EchoLotSetup::getCfgValueForStepIndex(byte configStepIndex) {
- switch (configStepIndex) {
- case 0:
- return String(getAirTemp(), 1);
- case 1:
- return String(getWaterTemp(), 1);
- case 2:
- return String(getSonicSpeedAir(), 1);
- case 3:
- return String(getSaltPromilleWater());
- default:
- return "- - -";
- }
- }
-
- float EchoLotSetup::getAirTemp() const {
- return airTemp;
- }
-
- void EchoLotSetup::setAirTemp(float airTemp) {
- this->airTemp = airTemp;
- }
-
- float EchoLotSetup::getWaterTemp() const {
- return waterTemp;
- }
-
- void EchoLotSetup::setWaterTemp(float waterTemp) {
- this->waterTemp = waterTemp;
- }
-
- float EchoLotSetup::getSonicSpeedAir() const {
- return sonicSpeedAir;
- }
-
- void EchoLotSetup::setSonicSpeedAir(float sonicSpeedAir) {
- this->sonicSpeedAir = sonicSpeedAir;
- }
-
- int EchoLotSetup::getSaltPromilleWater() const {
- return saltPromilleWater;
- }
-
- void EchoLotSetup::setSaltPromilleWater(int saltPromilleWater) {
- this->saltPromilleWater = saltPromilleWater;
- }
-
- void EchoLotSetup::writeFloatToEEPROM(uint16_t addr, float value) {
- uint16_t *p = (uint16_t*) (void*) &value;
-
- // float = 2 x uint16_t
- EEPROM.update(addr, p[0]);
- EEPROM.update(addr + 1, p[1]);
- }
-
- float EchoLotSetup::readFloatFromEEPROM(uint16_t addr) {
- float value;
- uint16_t *p = (uint16_t*) (void*) &value;
-
- p[0] = EEPROM.read(addr);
- p[1] = EEPROM.read(addr + 1);
-
- return value;
- }
-
- void EchoLotSetup::writeIntegerToEEPROM(uint16_t addr, int value) {
- EEPROM.update(addr, (uint16_t) value);
- }
-
- int EchoLotSetup::readIntegerFromEEPROM(uint16_t addr) {
- return (int) EEPROM.read(addr);
- }
|