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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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. #include "config.h"
  22. #include "config_pins.h"
  23. #ifdef PLATFORM_ESP
  24. #include <PCF8574.h>
  25. #include <Wire.h>
  26. #include "WifiStuff.h"
  27. #endif // PLATFORM_ESP
  28. //#define GPIO_HIGH_AS_INPUT
  29. // ----------------------------------------------------------------------------
  30. #if (I2C_GPIO_EXPANDER_COUNT > 0)
  31. static PCF8574 expand[I2C_GPIO_EXPANDER_COUNT];
  32. #endif
  33. void gpio_i2c_init(void) {
  34. #if (I2C_GPIO_EXPANDER_COUNT > 0)
  35. for (int i = 0; i < I2C_GPIO_EXPANDER_COUNT; i++) {
  36. expand[i].begin(0xFF);
  37. }
  38. #endif
  39. }
  40. static void gpio_pinMode(int pin, int value) {
  41. if (pin < 100) {
  42. pinMode(pin, value);
  43. } else {
  44. pin -= 100;
  45. int ex = pin / 8;
  46. pin = pin % 8;
  47. if (ex < I2C_GPIO_EXPANDER_COUNT) {
  48. uint8_t mask = expand[ex].getButtonMask();
  49. if (value == OUTPUT) {
  50. mask &= ~(1 << pin);
  51. } else {
  52. mask |= (1 << pin);
  53. }
  54. expand[ex].setButtonMask(mask);
  55. }
  56. }
  57. }
  58. static void gpio_digitalWrite(int pin, int value) {
  59. if (pin < 100) {
  60. digitalWrite(pin, value);
  61. } else {
  62. pin -= 100;
  63. int ex = pin / 8;
  64. pin = pin % 8;
  65. if (ex < I2C_GPIO_EXPANDER_COUNT) {
  66. expand[ex].write(pin, value);
  67. }
  68. }
  69. }
  70. static int gpio_digitalRead(int pin) {
  71. if (pin < 100) {
  72. return digitalRead(pin);
  73. } else {
  74. pin -= 100;
  75. int ex = pin / 8;
  76. pin = pin % 8;
  77. if (ex < I2C_GPIO_EXPANDER_COUNT) {
  78. return expand[ex].readButton(pin);
  79. } else {
  80. return 0;
  81. }
  82. }
  83. }
  84. // ----------------------------------------------------------------------------
  85. GPIOBank::GPIOBank(int _size) {
  86. size = _size;
  87. pins = new int[size];
  88. out_state = new bool[size];
  89. is_output = false;
  90. }
  91. GPIOBank::~GPIOBank(void) {
  92. delete pins;
  93. delete out_state;
  94. }
  95. void GPIOBank::setPinNumbers(int _pins[]) {
  96. for (int i = 0; i < size; i++) {
  97. pins[i] = _pins[i];
  98. }
  99. }
  100. void GPIOBank::setOutput(void) {
  101. for (int i = 0; i < size; i++) {
  102. #ifdef GPIO_HIGH_AS_INPUT
  103. gpio_pinMode(pins[i], INPUT);
  104. #else
  105. gpio_pinMode(pins[i], OUTPUT);
  106. gpio_digitalWrite(pins[i], HIGH);
  107. #endif
  108. out_state[i] = true;
  109. }
  110. is_output = true;
  111. }
  112. void GPIOBank::setInput(bool pullup) {
  113. for (int i = 0; i < size; i++) {
  114. if (pullup) {
  115. gpio_pinMode(pins[i], INPUT_PULLUP);
  116. } else {
  117. gpio_pinMode(pins[i], INPUT);
  118. }
  119. }
  120. is_output = false;
  121. }
  122. int GPIOBank::getSize(void) {
  123. return size;
  124. }
  125. void GPIOBank::setPin(int n, bool state) {
  126. if (!is_output) {
  127. return;
  128. }
  129. if ((n >= 0) && (n < size)) {
  130. #ifdef GPIO_HIGH_AS_INPUT
  131. if (state) {
  132. gpio_pinMode(pins[n], OUTPUT);
  133. gpio_digitalWrite(pins[n], LOW);
  134. } else {
  135. gpio_pinMode(pins[n], INPUT);
  136. }
  137. #else
  138. gpio_digitalWrite(pins[n], (!state) ? HIGH : LOW);
  139. #endif
  140. out_state[n] = !state;
  141. #ifdef PLATFORM_ESP
  142. wifi_schedule_websocket();
  143. #endif // PLATFORM_ESP
  144. }
  145. }
  146. void GPIOBank::setAll(bool state) {
  147. for (int i = 0; i < size; i++) {
  148. setPin(i, state);
  149. }
  150. }
  151. bool GPIOBank::getPin(int n) {
  152. if ((n >= 0) && (n < size)) {
  153. if (is_output) {
  154. return !out_state[n];
  155. } else {
  156. return (!gpio_digitalRead(pins[n]));
  157. }
  158. } else {
  159. return false;
  160. }
  161. }