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

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