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

Plants.cpp 4.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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 "DebugLog.h"
  21. #include "Plants.h"
  22. #include "config.h"
  23. #include "config_pins.h"
  24. // valves: no of plants + 1 for water inlet
  25. // pumps: no of fertilizers
  26. // switches: 2, low and high level
  27. Plants::Plants(int valve_count, int pump_count, int switch_count, int aux_count) :
  28. valves(valve_count), pumps(pump_count), switches(switch_count), aux(aux_count),
  29. kickstart(valve_count - 1) {
  30. }
  31. GPIOBank *Plants::getValves(void) {
  32. return &valves;
  33. }
  34. GPIOBank *Plants::getPumps(void) {
  35. return &pumps;
  36. }
  37. GPIOBank *Plants::getSwitches(void) {
  38. return &switches;
  39. }
  40. GPIOBank *Plants::getAux(void) {
  41. return &aux;
  42. }
  43. GPIOBank *Plants::getKickstart(void) {
  44. return &kickstart;
  45. }
  46. void Plants::setValvePins(int pins[]) {
  47. valves.setPinNumbers(pins);
  48. valves.setOutput();
  49. valves.setAll(false);
  50. }
  51. void Plants::setPumpPins(int pins[]) {
  52. pumps.setPinNumbers(pins);
  53. pumps.setOutput();
  54. pumps.setAll(false);
  55. }
  56. void Plants::setSwitchPins(int pins[], bool pullup) {
  57. switches.setPinNumbers(pins);
  58. switches.setInput(pullup);
  59. }
  60. void Plants::setAuxPins(int pins[]) {
  61. aux.setPinNumbers(pins);
  62. aux.setOutput();
  63. aux.setAll(false);
  64. }
  65. void Plants::setKickstartPins(int pins[]) {
  66. kickstart.setPinNumbers(pins);
  67. kickstart.setOutput();
  68. kickstart.setAll(false);
  69. }
  70. void Plants::abort(void) {
  71. closeWaterInlet();
  72. stopAllFertilizers();
  73. stopAllPlants();
  74. stopAllAux();
  75. }
  76. Plants::Waterlevel Plants::getWaterlevel(void) {
  77. bool low = switches.getPin(0);
  78. bool high = switches.getPin(1);
  79. #ifdef INVERT_SENSOR_BOTTOM
  80. low = !low;
  81. #endif // INVERT_SENSOR_BOTTOM
  82. #ifdef INVERT_SENSOR_TOP
  83. high = !high;
  84. #endif // INVERT_SENSOR_TOP
  85. if ((!low) && (!high)) {
  86. return empty;
  87. } else if (low && (!high)) {
  88. return inbetween;
  89. } else if (low && high) {
  90. return full;
  91. } else {
  92. return invalid;
  93. }
  94. }
  95. void Plants::openWaterInlet(void) {
  96. debug.println("Plants::openWaterInlet");
  97. valves.setPin(countPlants(), true);
  98. }
  99. void Plants::closeWaterInlet(void) {
  100. debug.println("Plants::closeWaterInlet");
  101. valves.setPin(countPlants(), false);
  102. }
  103. int Plants::countFertilizers(void) {
  104. return pumps.getSize();
  105. }
  106. void Plants::startFertilizer(int id) {
  107. debug.print("Plants::startFertilizer ");
  108. debug.println(id);
  109. if ((id >= 0) && (id < countFertilizers())) {
  110. pumps.setPin(id, true);
  111. }
  112. }
  113. void Plants::stopFertilizer(int id) {
  114. debug.print("Plants::stopFertilizer ");
  115. debug.println(id);
  116. if ((id >= 0) && (id < countFertilizers())) {
  117. pumps.setPin(id, false);
  118. }
  119. }
  120. void Plants::stopAllFertilizers(void) {
  121. for (int i = 0; i < countFertilizers(); i++) {
  122. stopFertilizer(i);
  123. }
  124. }
  125. int Plants::countPlants(void) {
  126. return valves.getSize() - 1;
  127. }
  128. void Plants::startPlant(int id, bool do_kickstart) {
  129. debug.print("Plants::startPlant ");
  130. debug.print(id);
  131. debug.print(", ");
  132. debug.println(do_kickstart);
  133. if ((id >= 0) && (id < countPlants())) {
  134. if (do_kickstart) {
  135. valves.setPin(id, false);
  136. kickstart.setPin(id, true);
  137. } else {
  138. kickstart.setPin(id, false);
  139. valves.setPin(id, true);
  140. }
  141. }
  142. }
  143. void Plants::stopPlant(int id) {
  144. debug.print("Plants::stopPlant ");
  145. debug.println(id);
  146. if ((id >= 0) && (id < countPlants())) {
  147. valves.setPin(id, false);
  148. kickstart.setPin(id, false);
  149. }
  150. }
  151. void Plants::stopAllPlants(void) {
  152. for (int i = 0; i < countPlants(); i++) {
  153. stopPlant(i);
  154. }
  155. }
  156. int Plants::countAux(void) {
  157. return aux.getSize();
  158. }
  159. void Plants::startAux(int id) {
  160. debug.print("Plants::startAux ");
  161. debug.println(id);
  162. if ((id >= 0) && (id < countAux())) {
  163. aux.setPin(id, true);
  164. }
  165. }
  166. void Plants::stopAux(int id) {
  167. debug.print("Plants::stopAux ");
  168. debug.println(id);
  169. if ((id >= 0) && (id < countAux())) {
  170. aux.setPin(id, false);
  171. }
  172. }
  173. void Plants::stopAllAux(void) {
  174. for (int i = 0; i < countAux(); i++) {
  175. stopAux(i);
  176. }
  177. }