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.

Plants.cpp 4.3KB

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