Distanz-/Tiefenmesser mit JSN-SR40T
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

EchoLotSetup.cpp 7.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. /*
  2. * EchoLotSetup.cpp
  3. *
  4. * Created on: 10.01.2026
  5. * Author: FSmilari
  6. */
  7. #include "EchoLotSetup.h"
  8. #include "fonts/titillium_web_regular16pt7b.h"
  9. #include "fonts/FreeSans7pt7b.h"
  10. #define ADDR_AIR_TEMP 0 // WORD
  11. #define ADDR_WATER_TEMP 2 // +2 WORDs
  12. #define ADDR_SONIC_AIR 4 // +2 WORDs
  13. #define ADDR_SALTPERMIL 6 // +2 WORDs
  14. // --------------------------------------------------
  15. // Konstruktor
  16. // --------------------------------------------------
  17. EchoLotSetup::EchoLotSetup(Display &display) : display(display) {
  18. airTemp = 20.0f;
  19. waterTemp = 20.0f;
  20. sonicSpeedAir = 343.0f;
  21. saltPromilleWater = 35;
  22. doInitialization = true;
  23. configStepIndex = 0;
  24. }
  25. // --------------------------------------------------
  26. // Initialisierung
  27. // --------------------------------------------------
  28. void EchoLotSetup::initialize() {
  29. if (doInitialization) {
  30. EEPROM.init();
  31. readFromEEPROM();
  32. configStepIndex = 0;
  33. String stepTxt = ConfigStep[configStepIndex];
  34. String unit = getCfgOptUnitForStepIndex(configStepIndex);
  35. display.setStatusValues(airTemp, waterTemp, sonicSpeedAir, saltPromilleWater);
  36. display.setConfigText(String(airTemp, 1));
  37. display.setConfigOption(stepTxt);
  38. display.setConfigUnit(unit);
  39. doInitialization = false;
  40. }
  41. }
  42. // --------------------------------------------------
  43. // Rotary Switch (kurz)
  44. // --------------------------------------------------
  45. void EchoLotSetup::onRotaryControlerSwitch() {
  46. configStepIndex++;
  47. if (configStepIndex > 3) {
  48. configStepIndex = 0;
  49. }
  50. display.setConfigText(getCfgValueForStepIndex(configStepIndex));
  51. display.setConfigOption(ConfigStep[configStepIndex]);
  52. display.setConfigUnit(getCfgOptUnitForStepIndex(configStepIndex));
  53. }
  54. // --------------------------------------------------
  55. // Rotary Switch (lang)
  56. // --------------------------------------------------
  57. void EchoLotSetup::onRotaryControlerLongSwitch() {
  58. save();
  59. doInitialization = true;
  60. }
  61. // --------------------------------------------------
  62. // Rotary Drehung
  63. // --------------------------------------------------
  64. void EchoLotSetup::onRotaryControlerTurn(RotaryEncoder::Direction turn) {
  65. if (turn != RotaryEncoder::Direction::NOROTATION) {
  66. String value = "";
  67. int index = 0;
  68. int sign = (turn == RotaryEncoder::Direction::CLOCKWISE) ? 1 : -1;
  69. switch (configStepIndex) {
  70. case 0:
  71. setAirTemp(min(max(0.0, getAirTemp() + 0.5 * sign), 40.0));
  72. value = String(getAirTemp(), 1);
  73. break;
  74. case 1:
  75. setWaterTemp(min(max(0.0, getWaterTemp() + 0.5 * sign), 40.0));
  76. value = String(getWaterTemp(), 1);
  77. break;
  78. case 2:
  79. setSonicSpeedAir(min(max(330.0, getSonicSpeedAir() + 0.5 * sign), 343.5));
  80. value = String(getSonicSpeedAir(), 1);
  81. break;
  82. case 3:
  83. setSaltPromilleWater(min(max(0, getSaltPromilleWater() + sign), 50));
  84. value = String(getSaltPromilleWater());
  85. break;
  86. default:
  87. break;
  88. }
  89. display.setConfigText(value);
  90. }
  91. }
  92. // --------------------------------------------------
  93. // Speichern bestätigen
  94. // --------------------------------------------------
  95. void EchoLotSetup::save() {
  96. saveToEEPROM();
  97. }
  98. // --------------------------------------------------
  99. // Abbrechen
  100. // --------------------------------------------------
  101. void EchoLotSetup::cancel() {
  102. doInitialization = true;
  103. }
  104. // --------------------------------------------------
  105. // Display leeren
  106. // --------------------------------------------------
  107. void EchoLotSetup::clear() {
  108. display.clearDisplay();
  109. }
  110. // --------------------------------------------------
  111. // Initial screen zeichnen
  112. // --------------------------------------------------
  113. void EchoLotSetup::drawInitScreen() {
  114. display.clearDisplay();
  115. display.showFrame(INITIALIZATION);
  116. printValues();
  117. }
  118. // --------------------------------------------------
  119. // EEPROM lesen
  120. // --------------------------------------------------
  121. void EchoLotSetup::readFromEEPROM() {
  122. setAirTemp(readFloatFromEEPROM(ADDR_AIR_TEMP));
  123. setWaterTemp(readFloatFromEEPROM(ADDR_WATER_TEMP));
  124. setSonicSpeedAir(readFloatFromEEPROM(ADDR_SONIC_AIR));
  125. setSaltPromilleWater(readIntegerFromEEPROM(ADDR_SALTPERMIL));
  126. }
  127. // --------------------------------------------------
  128. // EEPROM schreiben
  129. // --------------------------------------------------
  130. void EchoLotSetup::saveToEEPROM() {
  131. writeFloatToEEPROM(ADDR_AIR_TEMP, airTemp);
  132. writeFloatToEEPROM(ADDR_WATER_TEMP, waterTemp);
  133. writeFloatToEEPROM(ADDR_SONIC_AIR, sonicSpeedAir);
  134. writeIntegerToEEPROM(ADDR_SALTPERMIL, saltPromilleWater);
  135. }
  136. // --------------------------------------------------
  137. // Werte anzeigen
  138. // --------------------------------------------------
  139. void EchoLotSetup::printValues() {
  140. String value = String(sonicSpeedAir);
  141. Serial.print(String(AIRTEMP) + ": " + String(getAirTemp(), 1) + ", ");
  142. Serial.print(String(WATERTEMP) + ": " + String(getWaterTemp(), 1) + ", ");
  143. Serial.print(String(SONICSPEEDAIR) + ": " + String(getSonicSpeedAir(), 1) + ", ");
  144. Serial.println(String(SALTPROMILLEWATER) + ": " + String(getSaltPromilleWater()));
  145. display.drawText(ConfigStep[0], &FreeSans7pt7b, 1, ST7735_WHITE, 7, 30);
  146. display.drawRightAlignedText(String(getAirTemp(), 1) + " C", &FreeSans7pt7b, 1, ST7735_WHITE, 8, 30);
  147. display.drawCircle(140, 23, 2, ST7735_WHITE);
  148. display.drawText(ConfigStep[1], &FreeSans7pt7b, 1, ST7735_WHITE, 7, 55);
  149. display.drawRightAlignedText(String(getWaterTemp(), 1) + " C", &FreeSans7pt7b, 1, ST7735_WHITE, 8, 55);
  150. display.drawCircle(140, 48, 2, ST7735_WHITE);
  151. display.drawText(ConfigStep[2], &FreeSans7pt7b, 1, ST7735_WHITE, 7, 80);
  152. display.drawRightAlignedText(String(getSonicSpeedAir(), 1) + " m/s", &FreeSans7pt7b, 1, ST7735_WHITE, 8, 80);
  153. display.drawText(ConfigStep[3], &FreeSans7pt7b, 1, ST7735_WHITE, 7, 105);
  154. display.drawRightAlignedText(String(getSaltPromilleWater()) + " / ", &FreeSans7pt7b, 1, ST7735_WHITE, 8, 105);
  155. display.drawCircle(140, 98, 2, ST7735_WHITE);
  156. display.drawCircle(148, 104, 2, ST7735_WHITE);
  157. display.drawCircle(153, 104, 2, ST7735_WHITE);
  158. }
  159. // --------------------------------------------------
  160. // Private Helper
  161. // --------------------------------------------------
  162. String EchoLotSetup::getCfgOptForStepIndex(byte index) {
  163. return ConfigStep[index];
  164. }
  165. String EchoLotSetup::getCfgOptUnitForStepIndex(byte index) {
  166. switch (index) {
  167. case 0:
  168. case 1:
  169. return " C";
  170. case 2:
  171. return "m/s";
  172. default:
  173. return " / "; // for Promille painting
  174. }
  175. }
  176. String EchoLotSetup::getCfgValueForStepIndex(byte configStepIndex) {
  177. switch (configStepIndex) {
  178. case 0:
  179. return String(getAirTemp(), 1);
  180. case 1:
  181. return String(getWaterTemp(), 1);
  182. case 2:
  183. return String(getSonicSpeedAir(), 1);
  184. case 3:
  185. return String(getSaltPromilleWater());
  186. default:
  187. return "- - -";
  188. }
  189. }
  190. float EchoLotSetup::getAirTemp() const {
  191. return airTemp;
  192. }
  193. void EchoLotSetup::setAirTemp(float airTemp) {
  194. this->airTemp = airTemp;
  195. }
  196. float EchoLotSetup::getWaterTemp() const {
  197. return waterTemp;
  198. }
  199. void EchoLotSetup::setWaterTemp(float waterTemp) {
  200. this->waterTemp = waterTemp;
  201. }
  202. float EchoLotSetup::getSonicSpeedAir() const {
  203. return sonicSpeedAir;
  204. }
  205. void EchoLotSetup::setSonicSpeedAir(float sonicSpeedAir) {
  206. this->sonicSpeedAir = sonicSpeedAir;
  207. }
  208. int EchoLotSetup::getSaltPromilleWater() const {
  209. return saltPromilleWater;
  210. }
  211. void EchoLotSetup::setSaltPromilleWater(int saltPromilleWater) {
  212. this->saltPromilleWater = saltPromilleWater;
  213. }
  214. void EchoLotSetup::writeFloatToEEPROM(uint16_t addr, float value) {
  215. uint16_t *p = (uint16_t*) (void*) &value;
  216. // float = 2 x uint16_t
  217. EEPROM.update(addr, p[0]);
  218. EEPROM.update(addr + 1, p[1]);
  219. }
  220. float EchoLotSetup::readFloatFromEEPROM(uint16_t addr) {
  221. float value;
  222. uint16_t *p = (uint16_t*) (void*) &value;
  223. p[0] = EEPROM.read(addr);
  224. p[1] = EEPROM.read(addr + 1);
  225. return value;
  226. }
  227. void EchoLotSetup::writeIntegerToEEPROM(uint16_t addr, int value) {
  228. EEPROM.update(addr, (uint16_t) value);
  229. }
  230. int EchoLotSetup::readIntegerFromEEPROM(uint16_t addr) {
  231. return (int) EEPROM.read(addr);
  232. }