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

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