Fork of the espurna firmware for `mhsw` switches
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.

216 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 "mcp23s08.h"
  9. #if MCP23S08_SUPPORT
  10. #include "mcp23s08_pin.h"
  11. #include <SPI.h>
  12. #include <bitset>
  13. // TODO: check if this needed for SPI operation
  14. #define MCP23S08_CS_PIN 15
  15. // Known commands
  16. #define READ_CMD 0x41
  17. #define WRITE_CMD 0x40
  18. // Registers
  19. #define IODIR 0x00
  20. #define IPOL 0x01
  21. #define GPINTEN 0x02
  22. #define DEFVAL 0x03
  23. #define INTCON 0x04
  24. #define IOCON 0x05
  25. #define GPPU 0x06
  26. #define INTF 0x07
  27. #define INTCAP 0x08
  28. #define GPIO 0x09
  29. #define OLAT 0x0A
  30. static uint8_t _mcp23s08TxData[16] __attribute__((aligned(4)));
  31. static uint8_t _mcp23s08RxData[16] __attribute__((aligned(4)));
  32. namespace {
  33. class GpioMcp23s08 : public GpioBase {
  34. public:
  35. constexpr static size_t Pins { 8ul };
  36. using Pin = McpGpioPin;
  37. using Mask = std::bitset<Pins>;
  38. const char* id() const {
  39. return "mcp23s08";
  40. }
  41. size_t pins() const {
  42. return Pins;
  43. }
  44. bool lock(unsigned char index) const override {
  45. return _lock[index];
  46. }
  47. void lock(unsigned char index, bool value) override {
  48. _lock.set(index, value);
  49. }
  50. bool valid(unsigned char index) const override {
  51. return (index < Pins);
  52. }
  53. BasePinPtr pin(unsigned char index) override {
  54. return std::make_unique<McpGpioPin>(index);
  55. }
  56. private:
  57. Mask _lock;
  58. };
  59. } // namespace
  60. void MCP23S08Setup()
  61. {
  62. DEBUG_MSG_P(PSTR("[MCP23S08] Initialize SPI bus\n"));
  63. // Expander SPI settings
  64. SPI.begin();
  65. SPI.setHwCs(true);
  66. SPI.setFrequency(1000000);
  67. SPI.setDataMode(SPI_MODE0);
  68. pinMode(MCP23S08_CS_PIN, OUTPUT);
  69. digitalWrite(MCP23S08_CS_PIN, HIGH);
  70. }
  71. /**
  72. * @brief Set a expander MCP23S08 the pin direction.
  73. *
  74. * @param pinNumber Pin number for set.
  75. * @param mode direction mode. 0 - INPUT, 1 - OUTPUT.
  76. *
  77. * @return void
  78. */
  79. void MCP23S08SetDirection(uint8_t pinNumber, uint8_t mode)
  80. {
  81. uint8_t registerData = MCP23S08ReadRegister(IODIR);
  82. if (INPUT == mode)
  83. {
  84. registerData |= (1 << pinNumber);
  85. }
  86. else
  87. {
  88. registerData &= ~(1 << pinNumber);
  89. }
  90. MCP23S08WriteRegister(IODIR, registerData);
  91. }
  92. /**
  93. * @brief Read an expander MCP23S08 a register.
  94. *
  95. * @param address A register address.
  96. *
  97. * @return The data from the register.
  98. */
  99. uint8_t MCP23S08ReadRegister(uint8_t address)
  100. {
  101. _mcp23s08TxData[0] = READ_CMD;
  102. _mcp23s08TxData[1] = address;
  103. digitalWrite(MCP23S08_CS_PIN, LOW);
  104. SPI.transferBytes(_mcp23s08TxData, _mcp23s08RxData, 3);
  105. digitalWrite(MCP23S08_CS_PIN, HIGH);
  106. return _mcp23s08RxData[2];
  107. }
  108. /**
  109. * @brief Write data in expander MCP23S08 register.
  110. *
  111. * @param address A register address.
  112. * @param data A byte for write.
  113. *
  114. * @return void.
  115. */
  116. void MCP23S08WriteRegister(uint8_t address, uint8_t data)
  117. {
  118. _mcp23s08TxData[0] = WRITE_CMD;
  119. _mcp23s08TxData[1] = address;
  120. _mcp23s08TxData[2] = data;
  121. digitalWrite(MCP23S08_CS_PIN, LOW);
  122. SPI.transferBytes(_mcp23s08TxData, _mcp23s08RxData, 3);
  123. digitalWrite(MCP23S08_CS_PIN, HIGH);
  124. }
  125. /**
  126. * @brief Set expander MCP23S08 pin state.
  127. *
  128. * @param pinNumber The number of pin to be set.
  129. * @param state The pin state, true - 1, false - 0.
  130. *
  131. * @return void
  132. */
  133. void MCP23S08SetPin(uint8_t pinNumber, bool state)
  134. {
  135. uint8_t registerData = MCP23S08ReadRegister(OLAT);
  136. if (state)
  137. {
  138. registerData |= (1 << pinNumber);
  139. }
  140. else
  141. {
  142. registerData &= ~(1 << pinNumber);
  143. }
  144. MCP23S08WriteRegister(OLAT, registerData);
  145. }
  146. /**
  147. * @brief Get MCP23S08 pin state.
  148. *
  149. * @param pinNumber The number of pin to get.
  150. *
  151. * @return State true - 1, false - 0.
  152. */
  153. bool MCP23S08GetPin(uint8_t pinNumber)
  154. {
  155. uint8_t registerData = MCP23S08ReadRegister(GPIO);
  156. return registerData & (1 << pinNumber);
  157. }
  158. /**
  159. * @brief Ensure pin number is valid.
  160. *
  161. * @param pinNumber The number of pin to be get.
  162. *
  163. * @return State true - 1, false - 0.
  164. */
  165. bool mcpGpioValid(unsigned char gpio)
  166. {
  167. return gpio < McpGpioPins;
  168. }
  169. GpioBase& mcp23s08Gpio() {
  170. static GpioMcp23s08 gpio;
  171. return gpio;
  172. }
  173. #endif // MCP23S08_SUPPORT