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