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

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