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

Plants.cpp 2.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. #include <Arduino.h>
  2. #include "DebugLog.h"
  3. #include "Plants.h"
  4. // valves: no of plants + 1 for water inlet
  5. // pumps: no of fertilizers
  6. // switches: 2, low and high level
  7. Plants::Plants(int valve_count, int pump_count, int switch_count) :
  8. valves(valve_count), pumps(pump_count), switches(switch_count) {
  9. }
  10. GPIOBank *Plants::getValves(void) {
  11. return &valves;
  12. }
  13. GPIOBank *Plants::getPumps(void) {
  14. return &pumps;
  15. }
  16. GPIOBank *Plants::getSwitches(void) {
  17. return &switches;
  18. }
  19. void Plants::setValvePins(int pins[]) {
  20. valves.setPinNumbers(pins);
  21. valves.setOutput();
  22. valves.setAll(false);
  23. }
  24. void Plants::setPumpPins(int pins[]) {
  25. pumps.setPinNumbers(pins);
  26. pumps.setOutput();
  27. pumps.setAll(false);
  28. }
  29. void Plants::setSwitchPins(int pins[], bool pullup) {
  30. switches.setPinNumbers(pins);
  31. switches.setInput(pullup);
  32. }
  33. void Plants::abort(void) {
  34. closeWaterInlet();
  35. stopAllFertilizers();
  36. stopAllPlants();
  37. }
  38. Plants::Waterlevel Plants::getWaterlevel(void) {
  39. bool low = switches.getPin(0);
  40. bool high = switches.getPin(1);
  41. if ((!low) && (!high)) {
  42. return empty;
  43. } else if (low && (!high)) {
  44. return inbetween;
  45. } else if (low && high) {
  46. return full;
  47. } else {
  48. return invalid;
  49. }
  50. }
  51. void Plants::openWaterInlet(void) {
  52. debug.println("Plants::openWaterInlet");
  53. valves.setPin(countPlants(), true);
  54. }
  55. void Plants::closeWaterInlet(void) {
  56. debug.println("Plants::closeWaterInlet");
  57. valves.setPin(countPlants(), false);
  58. }
  59. int Plants::countFertilizers(void) {
  60. return pumps.getSize();
  61. }
  62. void Plants::startFertilizer(int id) {
  63. debug.print("Plants::startFertilizer ");
  64. debug.println(id);
  65. if ((id >= 0) && (id < countFertilizers())) {
  66. pumps.setPin(id, true);
  67. }
  68. }
  69. void Plants::stopFertilizer(int id) {
  70. debug.print("Plants::stopFertilizer ");
  71. debug.println(id);
  72. if ((id >= 0) && (id < countFertilizers())) {
  73. pumps.setPin(id, false);
  74. }
  75. }
  76. void Plants::stopAllFertilizers(void) {
  77. for (int i = 0; i < countFertilizers(); i++) {
  78. stopFertilizer(i);
  79. }
  80. }
  81. int Plants::countPlants(void) {
  82. return valves.getSize() - 1;
  83. }
  84. void Plants::startPlant(int id) {
  85. debug.print("Plants::startPlant ");
  86. debug.println(id);
  87. if ((id >= 0) && (id < countPlants())) {
  88. valves.setPin(id, true);
  89. }
  90. }
  91. void Plants::stopPlant(int id) {
  92. debug.print("Plants::stopPlant ");
  93. debug.println(id);
  94. if ((id >= 0) && (id < countPlants())) {
  95. valves.setPin(id, false);
  96. }
  97. }
  98. void Plants::stopAllPlants(void) {
  99. for (int i = 0; i < countPlants(); i++) {
  100. stopPlant(i);
  101. }
  102. }