DIY fertilizer mixer and plant watering machine https://www.xythobuz.de/giessomat.html
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

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