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 14KB

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