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

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