Mirror of espurna firmware for wireless switches and more
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.

217 lines
4.4 KiB

providers: relays, lights and buttons refactoring (#2414) - gpio module now tracks the known providers (right now, hardware and mcp expander) - refactored relay struct to use 'Provider' implementing setup,notify,change,boot instead of just BasePin actions - refactored button module to use gpio provider instead of referencing types itself - removed dual & stm code from buttons, migrate both to relay module - added status notify and change callbacks for relayStatus (i.e. 'notify' when relay status was called, but not changed. and 'changed' when it did) - relays runtime configuration keys - relay command now shows configured relays and current & target statuses - refactor the code using relayStatus(0, blah) under LIGHT_PROVIDER check to use lightState instead - remove rfbridge code form relay module. implement through a basic state listener in the rfbridge module, depend on RELAY_SUPPORT - allow to bind rf codes to real relays - drop tuya-specific lights provider, remove tuya code from relays and lights modules - integrate tuya via relay listeners and providers, use lights custom provider - implement channel transitions for tuya. disabled by default, and transition time and step are overridden to 2000 + 100. needs to be set to some value below the total time (i.e. `total transition time / step time == number of steps`, we need to figure out a correct time that serial comms could handle) - lights custom provider (global, not per-pin) and state listeners - remove lights code from relay module. implement through providers & listeners in the lights module, depend on RELAY_SUPPORT - lights per-channel relay provider (unused atm), depend on RELAY_SUPPORT - refactored channel transition - calculate step only once, make sure time + step values are sane, generate quick transitions with very small delay (10ms hardcoded) for transitions during OFF state i.e. we no longer waste 500ms (or whatever transition time is set to) on boot doing nothing - transition time + step parameter for the lightUpdate - report mask parameter for the lightUpdate - minor fixes across the board resolve #2222
3 years ago
providers: relays, lights and buttons refactoring (#2414) - gpio module now tracks the known providers (right now, hardware and mcp expander) - refactored relay struct to use 'Provider' implementing setup,notify,change,boot instead of just BasePin actions - refactored button module to use gpio provider instead of referencing types itself - removed dual & stm code from buttons, migrate both to relay module - added status notify and change callbacks for relayStatus (i.e. 'notify' when relay status was called, but not changed. and 'changed' when it did) - relays runtime configuration keys - relay command now shows configured relays and current & target statuses - refactor the code using relayStatus(0, blah) under LIGHT_PROVIDER check to use lightState instead - remove rfbridge code form relay module. implement through a basic state listener in the rfbridge module, depend on RELAY_SUPPORT - allow to bind rf codes to real relays - drop tuya-specific lights provider, remove tuya code from relays and lights modules - integrate tuya via relay listeners and providers, use lights custom provider - implement channel transitions for tuya. disabled by default, and transition time and step are overridden to 2000 + 100. needs to be set to some value below the total time (i.e. `total transition time / step time == number of steps`, we need to figure out a correct time that serial comms could handle) - lights custom provider (global, not per-pin) and state listeners - remove lights code from relay module. implement through providers & listeners in the lights module, depend on RELAY_SUPPORT - lights per-channel relay provider (unused atm), depend on RELAY_SUPPORT - refactored channel transition - calculate step only once, make sure time + step values are sane, generate quick transitions with very small delay (10ms hardcoded) for transitions during OFF state i.e. we no longer waste 500ms (or whatever transition time is set to) on boot doing nothing - transition time + step parameter for the lightUpdate - report mask parameter for the lightUpdate - minor fixes across the board resolve #2222
3 years ago
providers: relays, lights and buttons refactoring (#2414) - gpio module now tracks the known providers (right now, hardware and mcp expander) - refactored relay struct to use 'Provider' implementing setup,notify,change,boot instead of just BasePin actions - refactored button module to use gpio provider instead of referencing types itself - removed dual & stm code from buttons, migrate both to relay module - added status notify and change callbacks for relayStatus (i.e. 'notify' when relay status was called, but not changed. and 'changed' when it did) - relays runtime configuration keys - relay command now shows configured relays and current & target statuses - refactor the code using relayStatus(0, blah) under LIGHT_PROVIDER check to use lightState instead - remove rfbridge code form relay module. implement through a basic state listener in the rfbridge module, depend on RELAY_SUPPORT - allow to bind rf codes to real relays - drop tuya-specific lights provider, remove tuya code from relays and lights modules - integrate tuya via relay listeners and providers, use lights custom provider - implement channel transitions for tuya. disabled by default, and transition time and step are overridden to 2000 + 100. needs to be set to some value below the total time (i.e. `total transition time / step time == number of steps`, we need to figure out a correct time that serial comms could handle) - lights custom provider (global, not per-pin) and state listeners - remove lights code from relay module. implement through providers & listeners in the lights module, depend on RELAY_SUPPORT - lights per-channel relay provider (unused atm), depend on RELAY_SUPPORT - refactored channel transition - calculate step only once, make sure time + step values are sane, generate quick transitions with very small delay (10ms hardcoded) for transitions during OFF state i.e. we no longer waste 500ms (or whatever transition time is set to) on boot doing nothing - transition time + step parameter for the lightUpdate - report mask parameter for the lightUpdate - minor fixes across the board resolve #2222
3 years ago
providers: relays, lights and buttons refactoring (#2414) - gpio module now tracks the known providers (right now, hardware and mcp expander) - refactored relay struct to use 'Provider' implementing setup,notify,change,boot instead of just BasePin actions - refactored button module to use gpio provider instead of referencing types itself - removed dual & stm code from buttons, migrate both to relay module - added status notify and change callbacks for relayStatus (i.e. 'notify' when relay status was called, but not changed. and 'changed' when it did) - relays runtime configuration keys - relay command now shows configured relays and current & target statuses - refactor the code using relayStatus(0, blah) under LIGHT_PROVIDER check to use lightState instead - remove rfbridge code form relay module. implement through a basic state listener in the rfbridge module, depend on RELAY_SUPPORT - allow to bind rf codes to real relays - drop tuya-specific lights provider, remove tuya code from relays and lights modules - integrate tuya via relay listeners and providers, use lights custom provider - implement channel transitions for tuya. disabled by default, and transition time and step are overridden to 2000 + 100. needs to be set to some value below the total time (i.e. `total transition time / step time == number of steps`, we need to figure out a correct time that serial comms could handle) - lights custom provider (global, not per-pin) and state listeners - remove lights code from relay module. implement through providers & listeners in the lights module, depend on RELAY_SUPPORT - lights per-channel relay provider (unused atm), depend on RELAY_SUPPORT - refactored channel transition - calculate step only once, make sure time + step values are sane, generate quick transitions with very small delay (10ms hardcoded) for transitions during OFF state i.e. we no longer waste 500ms (or whatever transition time is set to) on boot doing nothing - transition time + step parameter for the lightUpdate - report mask parameter for the lightUpdate - minor fixes across the board resolve #2222
3 years ago
  1. /*
  2. MCP23S08 MODULE
  3. Copyright (C) 2020 by Eddi De Pieri <eddi at depieri dot com>
  4. Adapted from https://github.com/kmpelectronics/Arduino
  5. Copyright (C) 2016 Plamen Kovandjiev <p.kovandiev@kmpelectronics.eu> & Dimitar Antonov <d.antonov@kmpelectronics.eu>
  6. (ref. https://github.com/kmpelectronics/Arduino/blob/master/ProDinoWiFiEsp/src/PRODINoESP8266/src/KMPDinoWiFiESP.cpp)
  7. */
  8. #include "espurna.h"
  9. #if MCP23S08_SUPPORT
  10. #include "mcp23s08.h"
  11. #include "mcp23s08_pin.h"
  12. #include <SPI.h>
  13. #include <bitset>
  14. // TODO: check if this needed for SPI operation
  15. #define MCP23S08_CS_PIN 15
  16. // Known commands
  17. #define READ_CMD 0x41
  18. #define WRITE_CMD 0x40
  19. // Registers
  20. #define IODIR 0x00
  21. #define IPOL 0x01
  22. #define GPINTEN 0x02
  23. #define DEFVAL 0x03
  24. #define INTCON 0x04
  25. #define IOCON 0x05
  26. #define GPPU 0x06
  27. #define INTF 0x07
  28. #define INTCAP 0x08
  29. #define GPIO 0x09
  30. #define OLAT 0x0A
  31. static uint8_t _mcp23s08TxData[16] __attribute__((aligned(4)));
  32. static uint8_t _mcp23s08RxData[16] __attribute__((aligned(4)));
  33. namespace {
  34. class GpioMcp23s08 : public GpioBase {
  35. public:
  36. constexpr static size_t Pins { 8ul };
  37. using Pin = McpGpioPin;
  38. using Mask = std::bitset<Pins>;
  39. const char* id() const {
  40. return "mcp23s08";
  41. }
  42. size_t pins() const {
  43. return Pins;
  44. }
  45. bool lock(unsigned char index) const override {
  46. return _lock[index];
  47. }
  48. void lock(unsigned char index, bool value) override {
  49. _lock.set(index, value);
  50. }
  51. bool valid(unsigned char index) const override {
  52. return (index < Pins);
  53. }
  54. BasePinPtr pin(unsigned char index) override {
  55. return std::make_unique<McpGpioPin>(index);
  56. }
  57. private:
  58. Mask _lock;
  59. };
  60. } // namespace
  61. void MCP23S08Setup()
  62. {
  63. DEBUG_MSG_P(PSTR("[MCP23S08] Initialize SPI bus\n"));
  64. // Expander SPI settings
  65. SPI.begin();
  66. SPI.setHwCs(true);
  67. SPI.setFrequency(1000000);
  68. SPI.setDataMode(SPI_MODE0);
  69. pinMode(MCP23S08_CS_PIN, OUTPUT);
  70. digitalWrite(MCP23S08_CS_PIN, HIGH);
  71. }
  72. /**
  73. * @brief Set a expander MCP23S08 the pin direction.
  74. *
  75. * @param pinNumber Pin number for set.
  76. * @param mode direction mode. 0 - INPUT, 1 - OUTPUT.
  77. *
  78. * @return void
  79. */
  80. void MCP23S08SetDirection(uint8_t pinNumber, uint8_t mode)
  81. {
  82. uint8_t registerData = MCP23S08ReadRegister(IODIR);
  83. if (INPUT == mode)
  84. {
  85. registerData |= (1 << pinNumber);
  86. }
  87. else
  88. {
  89. registerData &= ~(1 << pinNumber);
  90. }
  91. MCP23S08WriteRegister(IODIR, registerData);
  92. }
  93. /**
  94. * @brief Read an expander MCP23S08 a register.
  95. *
  96. * @param address A register address.
  97. *
  98. * @return The data from the register.
  99. */
  100. uint8_t MCP23S08ReadRegister(uint8_t address)
  101. {
  102. _mcp23s08TxData[0] = READ_CMD;
  103. _mcp23s08TxData[1] = address;
  104. digitalWrite(MCP23S08_CS_PIN, LOW);
  105. SPI.transferBytes(_mcp23s08TxData, _mcp23s08RxData, 3);
  106. digitalWrite(MCP23S08_CS_PIN, HIGH);
  107. return _mcp23s08RxData[2];
  108. }
  109. /**
  110. * @brief Write data in expander MCP23S08 register.
  111. *
  112. * @param address A register address.
  113. * @param data A byte for write.
  114. *
  115. * @return void.
  116. */
  117. void MCP23S08WriteRegister(uint8_t address, uint8_t data)
  118. {
  119. _mcp23s08TxData[0] = WRITE_CMD;
  120. _mcp23s08TxData[1] = address;
  121. _mcp23s08TxData[2] = data;
  122. digitalWrite(MCP23S08_CS_PIN, LOW);
  123. SPI.transferBytes(_mcp23s08TxData, _mcp23s08RxData, 3);
  124. digitalWrite(MCP23S08_CS_PIN, HIGH);
  125. }
  126. /**
  127. * @brief Set expander MCP23S08 pin state.
  128. *
  129. * @param pinNumber The number of pin to be set.
  130. * @param state The pin state, true - 1, false - 0.
  131. *
  132. * @return void
  133. */
  134. void MCP23S08SetPin(uint8_t pinNumber, bool state)
  135. {
  136. uint8_t registerData = MCP23S08ReadRegister(OLAT);
  137. if (state)
  138. {
  139. registerData |= (1 << pinNumber);
  140. }
  141. else
  142. {
  143. registerData &= ~(1 << pinNumber);
  144. }
  145. MCP23S08WriteRegister(OLAT, registerData);
  146. }
  147. /**
  148. * @brief Get MCP23S08 pin state.
  149. *
  150. * @param pinNumber The number of pin to get.
  151. *
  152. * @return State true - 1, false - 0.
  153. */
  154. bool MCP23S08GetPin(uint8_t pinNumber)
  155. {
  156. uint8_t registerData = MCP23S08ReadRegister(GPIO);
  157. return registerData & (1 << pinNumber);
  158. }
  159. /**
  160. * @brief Ensure pin number is valid.
  161. *
  162. * @param pinNumber The number of pin to be get.
  163. *
  164. * @return State true - 1, false - 0.
  165. */
  166. bool mcpGpioValid(unsigned char gpio)
  167. {
  168. return gpio < McpGpioPins;
  169. }
  170. GpioBase& mcp23s08Gpio() {
  171. static GpioMcp23s08 gpio;
  172. return gpio;
  173. }
  174. #endif // MCP23S08_SUPPORT