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.

GPIOBank.cpp 2.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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 "GPIOBank.h"
  21. #ifdef PLATFORM_ESP
  22. #include "WifiStuff.h"
  23. #endif // PLATFORM_ESP
  24. //#define GPIO_HIGH_AS_INPUT
  25. GPIOBank::GPIOBank(int _size) {
  26. size = _size;
  27. pins = new int[size];
  28. out_state = new bool[size];
  29. is_output = false;
  30. }
  31. GPIOBank::~GPIOBank(void) {
  32. delete pins;
  33. delete out_state;
  34. }
  35. void GPIOBank::setPinNumbers(int _pins[]) {
  36. for (int i = 0; i < size; i++) {
  37. pins[i] = _pins[i];
  38. }
  39. }
  40. void GPIOBank::setOutput(void) {
  41. for (int i = 0; i < size; i++) {
  42. #ifdef GPIO_HIGH_AS_INPUT
  43. pinMode(pins[i], INPUT);
  44. #else
  45. pinMode(pins[i], OUTPUT);
  46. digitalWrite(pins[i], HIGH);
  47. #endif
  48. out_state[i] = true;
  49. }
  50. is_output = true;
  51. }
  52. void GPIOBank::setInput(bool pullup) {
  53. for (int i = 0; i < size; i++) {
  54. if (pullup) {
  55. pinMode(pins[i], INPUT_PULLUP);
  56. } else {
  57. pinMode(pins[i], INPUT);
  58. }
  59. }
  60. is_output = false;
  61. }
  62. int GPIOBank::getSize(void) {
  63. return size;
  64. }
  65. void GPIOBank::setPin(int n, bool state) {
  66. if (!is_output) {
  67. return;
  68. }
  69. if ((n >= 0) && (n < size)) {
  70. #ifdef GPIO_HIGH_AS_INPUT
  71. if (state) {
  72. pinMode(pins[n], OUTPUT);
  73. digitalWrite(pins[n], LOW);
  74. } else {
  75. pinMode(pins[n], INPUT);
  76. }
  77. #else
  78. digitalWrite(pins[n], (!state) ? HIGH : LOW);
  79. #endif
  80. out_state[n] = !state;
  81. #ifdef PLATFORM_ESP
  82. wifi_schedule_websocket();
  83. #endif // PLATFORM_ESP
  84. }
  85. }
  86. void GPIOBank::setAll(bool state) {
  87. for (int i = 0; i < size; i++) {
  88. setPin(i, state);
  89. }
  90. }
  91. bool GPIOBank::getPin(int n) {
  92. if ((n >= 0) && (n < size)) {
  93. if (is_output) {
  94. return !out_state[n];
  95. } else {
  96. return (!digitalRead(pins[n]));
  97. }
  98. } else {
  99. return false;
  100. }
  101. }