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

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