DIY fertilizer mixer and plant watering machine https://www.xythobuz.de/giessomat.html
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

GPIOBank.cpp 4.9KB

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