DIY fertilizer mixer and plant watering machine https://www.xythobuz.de/giessomat.html
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Functionality.cpp 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564
  1. /*
  2. * Copyright (c) 2021 Thomas Buck <thomas@xythobuz.de>
  3. *
  4. * This file is part of Giess-o-mat.
  5. *
  6. * Giess-o-mat is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * Giess-o-mat is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with Giess-o-mat. If not, see <https://www.gnu.org/licenses/>.
  18. */
  19. #include <Arduino.h>
  20. #include "config.h"
  21. #include "config_pins.h"
  22. #include "DebugLog.h"
  23. #include "Functionality.h"
  24. #ifdef FUNCTION_UI
  25. #ifndef FUNCTION_CONTROL
  26. #include <Wire.h>
  27. #include <CircularBuffer.h>
  28. CircularBuffer<int, I2C_BUF_SIZE> keybuffer;
  29. String linebuffer[4];
  30. #endif // ! FUNCTION_CONTROL
  31. #include "SerialLCD.h"
  32. #include "Keymatrix.h"
  33. SerialLCD lcd(SERIAL_LCD_TX_PIN);
  34. Keymatrix keys(KEYMATRIX_ROWS, KEYMATRIX_COLS);
  35. int keymatrix_pins[KEYMATRIX_ROWS + KEYMATRIX_COLS] = { KEYMATRIX_ROW_PINS, KEYMATRIX_COL_PINS };
  36. unsigned long last_input_time = 0;
  37. bool backlight_state = true;
  38. bool doing_multi_input = false;
  39. #endif // FUNCTION_UI
  40. #ifdef FUNCTION_CONTROL
  41. #ifndef FUNCTION_UI
  42. #include <Wire.h>
  43. #endif // ! FUNCTION_UI
  44. #include "Plants.h"
  45. #include "Statemachine.h"
  46. #include "WifiStuff.h"
  47. Plants plants(VALVE_COUNT, PUMP_COUNT, SWITCH_COUNT);
  48. int valve_pins[VALVE_COUNT] = { VALVE_PINS };
  49. int pump_pins[PUMP_COUNT] = { PUMP_PINS };
  50. int switch_pins[SWITCH_COUNT] = { SWITCH_PINS };
  51. Statemachine sm(write_to_all, backspace);
  52. #endif // FUNCTION_CONTROL
  53. // ----------------------------------------------------------------------------
  54. void write_lcd_to_serial(const char *a, const char *b,
  55. const char *c, const char *d) {
  56. #ifdef DEBUG_ENABLE_LCD_OUTPUT_ON_SERIAL
  57. int la = strlen(a);
  58. int lb = strlen(b);
  59. int lc = strlen(c);
  60. int ld = strlen(d);
  61. Serial.println();
  62. Serial.println(" ----------------------");
  63. Serial.print("| ");
  64. Serial.print(a);
  65. if (la < 20) {
  66. for (int i = 0; i < (20 - la); i++) {
  67. Serial.print(' ');
  68. }
  69. }
  70. Serial.println(" |");
  71. Serial.print("| ");
  72. Serial.print(b);
  73. if (lb < 20) {
  74. for (int i = 0; i < (20 - lb); i++) {
  75. Serial.print(' ');
  76. }
  77. }
  78. Serial.println(" |");
  79. Serial.print("| ");
  80. Serial.print(c);
  81. if (lc < 20) {
  82. for (int i = 0; i < (20 - lc); i++) {
  83. Serial.print(' ');
  84. }
  85. }
  86. Serial.println(" |");
  87. Serial.print("| ");
  88. Serial.print(d);
  89. if (ld < 20) {
  90. for (int i = 0; i < (20 - ld); i++) {
  91. Serial.print(' ');
  92. }
  93. }
  94. Serial.println(" |");
  95. Serial.println(" ----------------------");
  96. Serial.println("Please provide keypad input:");
  97. #endif // DEBUG_ENABLE_LCD_OUTPUT_ON_SERIAL
  98. }
  99. void handle_input(int n) {
  100. #ifdef FUNCTION_CONTROL
  101. sm.input(n);
  102. #else
  103. keybuffer.push(n);
  104. #endif // FUNCTION_CONTROL
  105. }
  106. void input_serial(void) {
  107. #ifdef DEBUG_ENABLE_KEYPAD_INPUT_ON_SERIAL
  108. if (Serial.available() > 0) {
  109. #ifdef FUNCTION_UI
  110. last_input_time = millis();
  111. if (!backlight_state) {
  112. backlight_state = true;
  113. lcd.setBacklight(255);
  114. }
  115. #endif // FUNCTION_UI
  116. int c = Serial.read();
  117. if (c == '*') {
  118. Serial.write(c);
  119. Serial.write('\n');
  120. #ifdef FUNCTION_UI
  121. if (doing_multi_input) {
  122. char s[2] = { (char)(c), '\0' };
  123. lcd.write(s);
  124. }
  125. #endif // FUNCTION_UI
  126. handle_input(-1);
  127. } else if (c == '#') {
  128. Serial.write(c);
  129. Serial.write('\n');
  130. #ifdef FUNCTION_UI
  131. if (doing_multi_input) {
  132. char s[2] = { (char)(c), '\0' };
  133. lcd.write(s);
  134. }
  135. #endif // FUNCTION_UI
  136. handle_input(-2);
  137. } else if (c == '\n') {
  138. Serial.write('#');
  139. Serial.write('\n');
  140. #ifdef FUNCTION_UI
  141. if (doing_multi_input) {
  142. char s[2] = { '#', '\0' };
  143. lcd.write(s);
  144. }
  145. #endif // FUNCTION_UI
  146. handle_input(-2);
  147. } else if (c == '\b') {
  148. Serial.write(c);
  149. handle_input(-1);
  150. } else if ((c >= '0') && (c <= '9')) {
  151. Serial.write(c);
  152. #ifdef FUNCTION_UI
  153. if (!doing_multi_input) {
  154. Serial.write('\n');
  155. }
  156. if (doing_multi_input) {
  157. char s[2] = { (char)(c), '\0' };
  158. lcd.write(s);
  159. }
  160. #endif // FUNCTION_UI
  161. handle_input(c - '0');
  162. }
  163. }
  164. #endif // DEBUG_ENABLE_KEYPAD_INPUT_ON_SERIAL
  165. }
  166. // ----------------------------------------------------------------------------
  167. #ifdef FUNCTION_UI
  168. #ifndef FUNCTION_CONTROL
  169. void ui_i2c_request(void) {
  170. if (keybuffer.isEmpty()) {
  171. Wire.write(252);
  172. return;
  173. }
  174. debug.print("ui_i2c_request: ");
  175. while (!keybuffer.isEmpty()) {
  176. int n = keybuffer.shift();
  177. if (n == -1) {
  178. n = 254;
  179. } else if (n == -2) {
  180. n = 253;
  181. } else if ((n < 0) || (n > 9)) {
  182. continue;
  183. }
  184. debug.print(n);
  185. debug.print(", ");
  186. Wire.write(n);
  187. }
  188. debug.println();
  189. }
  190. void ui_i2c_receive(int count) {
  191. char buff[I2C_BUF_SIZE];
  192. for (int i = 0; i < I2C_BUF_SIZE; i++) {
  193. buff[i] = 0;
  194. }
  195. for (int i = 0; (i < count) && (Wire.available()); i++) {
  196. buff[i] = Wire.read();
  197. }
  198. if (count <= 0) {
  199. debug.println("ui_i2c_receive: count is 0");
  200. return;
  201. }
  202. if (buff[0] == 0x01) {
  203. if (count < 3) {
  204. debug.println("ui_i2c_receive: blink lcd too short");
  205. return;
  206. }
  207. int n = buff[1];
  208. int wait = buff[2] * 10;
  209. debug.println("ui_i2c_receive: blink lcd command");
  210. blink_lcd(n, wait);
  211. } else if (buff[0] == 0x02) {
  212. debug.println("ui_i2c_receive: backspace command");
  213. backspace();
  214. } else if (buff[0] == 0x03) {
  215. if (count < 3) {
  216. debug.println("ui_i2c_receive: display far too short");
  217. return;
  218. }
  219. int line = buff[1];
  220. int len = buff[2];
  221. String s;
  222. for (int i = 0; i < len; i++) {
  223. s += buff[3 + i];
  224. }
  225. debug.println("ui_i2c_receive: display command");
  226. linebuffer[line] = s;
  227. } else if (buff[0] == 0x04) {
  228. if (count < 2) {
  229. debug.println("ui_i2c_receive: num input too short");
  230. return;
  231. }
  232. int8_t num_input = buff[1];
  233. debug.println("ui_i2c_receive: num input");
  234. write_to_all(linebuffer[0].c_str(), linebuffer[1].c_str(),
  235. linebuffer[2].c_str(), linebuffer[3].c_str(),
  236. num_input);
  237. } else {
  238. debug.println("ui_i2c_receive: unknown command");
  239. return;
  240. }
  241. }
  242. #endif // ! FUNCTION_CONTROL
  243. void ui_setup(void) {
  244. keys.setPins(keymatrix_pins);
  245. debug.println("Setting up LCD, please wait");
  246. delay(1000); // give LCD some time to boot
  247. lcd.init();
  248. lcd.disableSplash();
  249. #ifdef DEBUG_WAIT_FOR_SERIAL_CONN
  250. lcd.write(0, "Waiting for serial");
  251. lcd.write(1, "connection on debug");
  252. lcd.write(2, "USB port...");
  253. while (!Serial);
  254. lcd.clear();
  255. #endif // DEBUG_WAIT_FOR_SERIAL_CONN
  256. #ifndef FUNCTION_CONTROL
  257. debug.println("Initializing I2C Slave");
  258. Wire.begin(OWN_I2C_ADDRESS);
  259. Wire.onReceive(ui_i2c_receive);
  260. Wire.onRequest(ui_i2c_request);
  261. String a = String("- Giess-o-mat V") + FIRMWARE_VERSION + String(" -");
  262. String b = String(" Address 0x") + String(OWN_I2C_ADDRESS, HEX) + String(" ");
  263. lcd.write(0, a.c_str());
  264. lcd.write(1, " I2C UI Panel ");
  265. lcd.write(2, "Waiting for data....");
  266. lcd.write(3, b.c_str());
  267. #endif // ! FUNCTION_CONTROL
  268. }
  269. void input_keypad(void) {
  270. keys.scan();
  271. while (keys.hasEvent()) {
  272. auto ke = keys.getEvent();
  273. if (ke.getType() == Keymatrix::Event::button_down) {
  274. last_input_time = millis();
  275. if (!backlight_state) {
  276. backlight_state = true;
  277. lcd.setBacklight(255);
  278. // swallow input when used to activate light
  279. continue;
  280. }
  281. int n = ke.getNum();
  282. debug.print("Got keypad input: \"");
  283. if (n < 0) {
  284. debug.print((n == -1) ? '*' : '#');
  285. } else {
  286. debug.print(n);
  287. if (doing_multi_input) {
  288. char s[2] = { (char)(n + '0'), '\0' };
  289. lcd.write(s);
  290. }
  291. }
  292. debug.println("\"");
  293. blink_lcd(1, 100);
  294. handle_input(n);
  295. }
  296. }
  297. }
  298. void ui_run(void) {
  299. input_keypad();
  300. input_serial();
  301. if (backlight_state && (millis() >= (last_input_time + DISPLAY_BACKLIGHT_TIMEOUT))) {
  302. backlight_state = false;
  303. lcd.setBacklight(0);
  304. }
  305. }
  306. void write_to_all(const char *a, const char *b,
  307. const char *c, const char *d, int num_input) {
  308. lcd.clear();
  309. if (num_input >= 0) {
  310. lcd.write(0, a);
  311. if (num_input >= 1) {
  312. lcd.write(1, b);
  313. }
  314. if (num_input >= 2) {
  315. lcd.write(2, c);
  316. }
  317. if (num_input >= 3) {
  318. lcd.write(3, d);
  319. }
  320. lcd.cursor(3);
  321. doing_multi_input = true;
  322. } else {
  323. lcd.write(0, a);
  324. lcd.write(1, b);
  325. lcd.write(2, c);
  326. lcd.write(3, d);
  327. lcd.cursor(0);
  328. doing_multi_input = false;
  329. }
  330. write_lcd_to_serial(a, b, c, d);
  331. }
  332. void backspace(void) {
  333. lcd.write("\b");
  334. }
  335. void blink_lcd(int n, int wait) {
  336. for (int i = 0; i < n; i++) {
  337. lcd.setBacklight(0);
  338. delay(wait);
  339. lcd.setBacklight(255);
  340. if (i < (n - 1))
  341. delay(wait);
  342. }
  343. }
  344. #endif // FUNCTION_UI
  345. // ----------------------------------------------------------------------------
  346. #ifdef FUNCTION_CONTROL
  347. const char *control_state_name(void) {
  348. return sm.getStateName();
  349. }
  350. void control_act_input(int n) {
  351. sm.input(n);
  352. }
  353. Plants *get_plants(void) {
  354. return &plants;
  355. }
  356. void control_setup(void) {
  357. plants.setValvePins(valve_pins);
  358. plants.setPumpPins(pump_pins);
  359. plants.setSwitchPins(switch_pins, true);
  360. #ifndef FUNCTION_UI
  361. debug.println("Initializing I2C Master");
  362. Wire.setClock(I2C_BUS_SPEED);
  363. #if defined(I2C_SDA_PIN) && defined(I2C_SCL_PIN)
  364. Wire.begin(I2C_SDA_PIN, I2C_SCL_PIN);
  365. #else
  366. Wire.begin();
  367. #endif // defined(I2C_SDA_PIN) && defined(I2C_SCL_PIN)
  368. #ifdef DEBUG_WAIT_FOR_SERIAL_CONN
  369. debug.println("Wait for Serial");
  370. while (!Serial);
  371. #endif // DEBUG_WAIT_FOR_SERIAL_CONN
  372. #endif // ! FUNCTION_UI
  373. }
  374. void control_begin(void) {
  375. sm.begin();
  376. }
  377. void control_run(void) {
  378. #ifndef FUNCTION_UI
  379. Wire.requestFrom(OWN_I2C_ADDRESS, I2C_BUF_SIZE);
  380. while (Wire.available()) {
  381. int c = Wire.read();
  382. if (((c >= 0) && (c <= 9)) || (c == 254) || (c == 253)) {
  383. debug.print("control_run: got input '");
  384. debug.print(c);
  385. debug.println("'");
  386. if (c == 254) {
  387. c = -1;
  388. } else if (c == 253) {
  389. c = -2;
  390. }
  391. sm.input(c);
  392. }
  393. }
  394. input_serial();
  395. #endif // ! FUNCTION_UI
  396. sm.act();
  397. }
  398. #ifndef FUNCTION_UI
  399. void blink_lcd(int n, int wait) {
  400. debug.println("blink_lcd i2c");
  401. Wire.beginTransmission(OWN_I2C_ADDRESS);
  402. Wire.write(0x01); // blink command
  403. Wire.write(n); // count
  404. Wire.write(wait / 10); // time
  405. Wire.endTransmission();
  406. }
  407. void backspace(void) {
  408. debug.println("backspace i2c");
  409. Wire.beginTransmission(OWN_I2C_ADDRESS);
  410. Wire.write(0x02); // backspace command
  411. Wire.endTransmission();
  412. }
  413. void write_to_all(const char *a, const char *b,
  414. const char *c, const char *d, int num_input) {
  415. const char *lines[4] = { a, b, c, d };
  416. debug.println("write_to_all i2c");
  417. for (int i = 0; i < 4; i++) {
  418. Wire.beginTransmission(OWN_I2C_ADDRESS);
  419. Wire.write(0x03); // display command
  420. Wire.write(i);
  421. int l = strlen(lines[i]);
  422. Wire.write(l);
  423. for (int n = 0; n < l; n++) {
  424. Wire.write(lines[i][n]);
  425. }
  426. Wire.endTransmission();
  427. }
  428. Wire.beginTransmission(OWN_I2C_ADDRESS);
  429. Wire.write(0x04); // display command
  430. Wire.write((int8_t)num_input);
  431. Wire.endTransmission();
  432. write_lcd_to_serial(a, b, c, d);
  433. #ifdef PLATFORM_ESP
  434. wifi_set_message_buffer(a, b, c, d);
  435. wifi_send_status_broadcast();
  436. #endif // PLATFORM_ESP
  437. }
  438. #endif // ! FUNCTION_UI
  439. #endif // FUNCTION_CONTROL