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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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) :
  28. valves(valve_count), pumps(pump_count), switches(switch_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. void Plants::setValvePins(int pins[]) {
  40. valves.setPinNumbers(pins);
  41. valves.setOutput();
  42. valves.setAll(false);
  43. }
  44. void Plants::setPumpPins(int pins[]) {
  45. pumps.setPinNumbers(pins);
  46. pumps.setOutput();
  47. pumps.setAll(false);
  48. }
  49. void Plants::setSwitchPins(int pins[], bool pullup) {
  50. switches.setPinNumbers(pins);
  51. switches.setInput(pullup);
  52. }
  53. void Plants::abort(void) {
  54. closeWaterInlet();
  55. stopAllFertilizers();
  56. stopAllPlants();
  57. }
  58. Plants::Waterlevel Plants::getWaterlevel(void) {
  59. bool low = switches.getPin(0);
  60. bool high = switches.getPin(1);
  61. #ifdef INVERT_SENSOR_BOTTOM
  62. low = !low;
  63. #endif // INVERT_SENSOR_BOTTOM
  64. #ifdef INVERT_SENSOR_TOP
  65. high = !high;
  66. #endif // INVERT_SENSOR_TOP
  67. if ((!low) && (!high)) {
  68. return empty;
  69. } else if (low && (!high)) {
  70. return inbetween;
  71. } else if (low && high) {
  72. return full;
  73. } else {
  74. return invalid;
  75. }
  76. }
  77. void Plants::openWaterInlet(void) {
  78. debug.println("Plants::openWaterInlet");
  79. valves.setPin(countPlants(), true);
  80. }
  81. void Plants::closeWaterInlet(void) {
  82. debug.println("Plants::closeWaterInlet");
  83. valves.setPin(countPlants(), false);
  84. }
  85. int Plants::countFertilizers(void) {
  86. return pumps.getSize();
  87. }
  88. void Plants::startFertilizer(int id) {
  89. debug.print("Plants::startFertilizer ");
  90. debug.println(id);
  91. if ((id >= 0) && (id < countFertilizers())) {
  92. pumps.setPin(id, true);
  93. }
  94. }
  95. void Plants::stopFertilizer(int id) {
  96. debug.print("Plants::stopFertilizer ");
  97. debug.println(id);
  98. if ((id >= 0) && (id < countFertilizers())) {
  99. pumps.setPin(id, false);
  100. }
  101. }
  102. void Plants::stopAllFertilizers(void) {
  103. for (int i = 0; i < countFertilizers(); i++) {
  104. stopFertilizer(i);
  105. }
  106. }
  107. int Plants::countPlants(void) {
  108. return valves.getSize() - 1;
  109. }
  110. void Plants::startPlant(int id) {
  111. debug.print("Plants::startPlant ");
  112. debug.println(id);
  113. if ((id >= 0) && (id < countPlants())) {
  114. valves.setPin(id, true);
  115. }
  116. }
  117. void Plants::stopPlant(int id) {
  118. debug.print("Plants::stopPlant ");
  119. debug.println(id);
  120. if ((id >= 0) && (id < countPlants())) {
  121. valves.setPin(id, false);
  122. }
  123. }
  124. void Plants::stopAllPlants(void) {
  125. for (int i = 0; i < countPlants(); i++) {
  126. stopPlant(i);
  127. }
  128. }