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

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