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.

244 lines
7.1 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
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. RELAY MODULE
  3. */
  4. #pragma once
  5. #include "espurna.h"
  6. namespace relay {
  7. namespace build {
  8. constexpr size_t dummyCount() {
  9. return DUMMY_RELAY_COUNT;
  10. }
  11. constexpr int syncMode() {
  12. return RELAY_SYNC;
  13. }
  14. constexpr float floodWindow() {
  15. return RELAY_FLOOD_WINDOW;
  16. }
  17. static_assert(floodWindow() >= 0.0f, "");
  18. constexpr unsigned long floodWindowMs() {
  19. return static_cast<unsigned long>(floodWindow() * 1000.0f);
  20. }
  21. constexpr unsigned long floodChanges() {
  22. return RELAY_FLOOD_CHANGES;
  23. }
  24. constexpr unsigned long interlockDelay() {
  25. return RELAY_DELAY_INTERLOCK;
  26. }
  27. constexpr float pulseTime(size_t index) {
  28. return (
  29. (index == 0) ? RELAY1_PULSE_TIME :
  30. (index == 1) ? RELAY2_PULSE_TIME :
  31. (index == 2) ? RELAY3_PULSE_TIME :
  32. (index == 3) ? RELAY4_PULSE_TIME :
  33. (index == 4) ? RELAY5_PULSE_TIME :
  34. (index == 5) ? RELAY6_PULSE_TIME :
  35. (index == 6) ? RELAY7_PULSE_TIME :
  36. (index == 7) ? RELAY8_PULSE_TIME : RELAY_PULSE_TIME
  37. );
  38. }
  39. static_assert(pulseTime(0) >= 0.0f, "");
  40. static_assert(pulseTime(1) >= 0.0f, "");
  41. static_assert(pulseTime(2) >= 0.0f, "");
  42. static_assert(pulseTime(3) >= 0.0f, "");
  43. static_assert(pulseTime(4) >= 0.0f, "");
  44. static_assert(pulseTime(5) >= 0.0f, "");
  45. static_assert(pulseTime(6) >= 0.0f, "");
  46. static_assert(pulseTime(7) >= 0.0f, "");
  47. constexpr RelayPulse pulseMode(size_t index) {
  48. return (
  49. (index == 0) ? RELAY1_PULSE_MODE :
  50. (index == 1) ? RELAY2_PULSE_MODE :
  51. (index == 2) ? RELAY3_PULSE_MODE :
  52. (index == 3) ? RELAY4_PULSE_MODE :
  53. (index == 4) ? RELAY5_PULSE_MODE :
  54. (index == 5) ? RELAY6_PULSE_MODE :
  55. (index == 6) ? RELAY7_PULSE_MODE :
  56. (index == 7) ? RELAY8_PULSE_MODE : RELAY_PULSE_NONE
  57. );
  58. }
  59. constexpr unsigned long delayOn(size_t index) {
  60. return (
  61. (index == 0) ? RELAY1_DELAY_ON :
  62. (index == 1) ? RELAY2_DELAY_ON :
  63. (index == 2) ? RELAY3_DELAY_ON :
  64. (index == 3) ? RELAY4_DELAY_ON :
  65. (index == 4) ? RELAY5_DELAY_ON :
  66. (index == 5) ? RELAY6_DELAY_ON :
  67. (index == 6) ? RELAY7_DELAY_ON :
  68. (index == 7) ? RELAY8_DELAY_ON : 0ul
  69. );
  70. }
  71. constexpr unsigned long delayOff(size_t index) {
  72. return (
  73. (index == 0) ? RELAY1_DELAY_OFF :
  74. (index == 1) ? RELAY2_DELAY_OFF :
  75. (index == 2) ? RELAY3_DELAY_OFF :
  76. (index == 3) ? RELAY4_DELAY_OFF :
  77. (index == 4) ? RELAY5_DELAY_OFF :
  78. (index == 5) ? RELAY6_DELAY_OFF :
  79. (index == 6) ? RELAY7_DELAY_OFF :
  80. (index == 7) ? RELAY8_DELAY_OFF : 0ul
  81. );
  82. }
  83. constexpr unsigned char pin(size_t index) {
  84. return (
  85. (index == 0) ? RELAY1_PIN :
  86. (index == 1) ? RELAY2_PIN :
  87. (index == 2) ? RELAY3_PIN :
  88. (index == 3) ? RELAY4_PIN :
  89. (index == 4) ? RELAY5_PIN :
  90. (index == 5) ? RELAY6_PIN :
  91. (index == 6) ? RELAY7_PIN :
  92. (index == 7) ? RELAY8_PIN : GPIO_NONE
  93. );
  94. }
  95. constexpr RelayType type(size_t index) {
  96. return (
  97. (index == 0) ? RELAY1_TYPE :
  98. (index == 1) ? RELAY2_TYPE :
  99. (index == 2) ? RELAY3_TYPE :
  100. (index == 3) ? RELAY4_TYPE :
  101. (index == 4) ? RELAY5_TYPE :
  102. (index == 5) ? RELAY6_TYPE :
  103. (index == 6) ? RELAY7_TYPE :
  104. (index == 7) ? RELAY8_TYPE : RELAY_TYPE_NORMAL
  105. );
  106. }
  107. constexpr GpioType pinType(size_t index) {
  108. return (
  109. (index == 0) ? RELAY1_PIN_TYPE :
  110. (index == 1) ? RELAY2_PIN_TYPE :
  111. (index == 2) ? RELAY3_PIN_TYPE :
  112. (index == 3) ? RELAY4_PIN_TYPE :
  113. (index == 4) ? RELAY5_PIN_TYPE :
  114. (index == 5) ? RELAY6_PIN_TYPE :
  115. (index == 6) ? RELAY7_PIN_TYPE :
  116. (index == 7) ? RELAY8_PIN_TYPE : GPIO_TYPE_NONE
  117. );
  118. }
  119. constexpr unsigned char resetPin(size_t index) {
  120. return (
  121. (index == 0) ? RELAY1_RESET_PIN :
  122. (index == 1) ? RELAY2_RESET_PIN :
  123. (index == 2) ? RELAY3_RESET_PIN :
  124. (index == 3) ? RELAY4_RESET_PIN :
  125. (index == 4) ? RELAY5_RESET_PIN :
  126. (index == 5) ? RELAY6_RESET_PIN :
  127. (index == 6) ? RELAY7_RESET_PIN :
  128. (index == 7) ? RELAY8_RESET_PIN : GPIO_NONE
  129. );
  130. }
  131. constexpr int bootMode(size_t index) {
  132. return (
  133. (index == 0) ? RELAY1_BOOT_MODE :
  134. (index == 1) ? RELAY2_BOOT_MODE :
  135. (index == 2) ? RELAY3_BOOT_MODE :
  136. (index == 3) ? RELAY4_BOOT_MODE :
  137. (index == 4) ? RELAY5_BOOT_MODE :
  138. (index == 5) ? RELAY6_BOOT_MODE :
  139. (index == 6) ? RELAY7_BOOT_MODE :
  140. (index == 7) ? RELAY8_BOOT_MODE : RELAY_BOOT_OFF
  141. );
  142. }
  143. constexpr RelayProvider provider(size_t index) {
  144. return (
  145. (index == 0) ? (RELAY1_PROVIDER) :
  146. (index == 1) ? (RELAY2_PROVIDER) :
  147. (index == 2) ? (RELAY3_PROVIDER) :
  148. (index == 3) ? (RELAY4_PROVIDER) :
  149. (index == 4) ? (RELAY5_PROVIDER) :
  150. (index == 5) ? (RELAY6_PROVIDER) :
  151. (index == 6) ? (RELAY7_PROVIDER) :
  152. (index == 7) ? (RELAY8_PROVIDER) : RELAY_PROVIDER_NONE
  153. );
  154. }
  155. constexpr RelayMqttTopicMode mqttTopicMode(size_t index) {
  156. return (
  157. (index == 0) ? (RELAY1_MQTT_TOPIC_MODE) :
  158. (index == 1) ? (RELAY2_MQTT_TOPIC_MODE) :
  159. (index == 2) ? (RELAY3_MQTT_TOPIC_MODE) :
  160. (index == 3) ? (RELAY4_MQTT_TOPIC_MODE) :
  161. (index == 4) ? (RELAY5_MQTT_TOPIC_MODE) :
  162. (index == 5) ? (RELAY6_MQTT_TOPIC_MODE) :
  163. (index == 6) ? (RELAY7_MQTT_TOPIC_MODE) :
  164. (index == 7) ? (RELAY8_MQTT_TOPIC_MODE) : RELAY_MQTT_TOPIC_MODE
  165. );
  166. }
  167. constexpr const char* const mqttPayloadOn() {
  168. return RELAY_MQTT_ON;
  169. }
  170. constexpr const char* const mqttPayloadOff() {
  171. return RELAY_MQTT_OFF;
  172. }
  173. constexpr const char* const mqttPayloadToggle() {
  174. return RELAY_MQTT_TOGGLE;
  175. }
  176. constexpr const char* const mqttTopicSub(size_t index) {
  177. return (
  178. (index == 0) ? (RELAY1_MQTT_TOPIC_SUB) :
  179. (index == 1) ? (RELAY2_MQTT_TOPIC_SUB) :
  180. (index == 2) ? (RELAY3_MQTT_TOPIC_SUB) :
  181. (index == 3) ? (RELAY4_MQTT_TOPIC_SUB) :
  182. (index == 4) ? (RELAY5_MQTT_TOPIC_SUB) :
  183. (index == 5) ? (RELAY6_MQTT_TOPIC_SUB) :
  184. (index == 6) ? (RELAY7_MQTT_TOPIC_SUB) :
  185. (index == 7) ? (RELAY8_MQTT_TOPIC_SUB) : ""
  186. );
  187. }
  188. constexpr const char* const mqttTopicPub(size_t index) {
  189. return (
  190. (index == 0) ? (RELAY1_MQTT_TOPIC_PUB) :
  191. (index == 1) ? (RELAY2_MQTT_TOPIC_PUB) :
  192. (index == 2) ? (RELAY3_MQTT_TOPIC_PUB) :
  193. (index == 3) ? (RELAY4_MQTT_TOPIC_PUB) :
  194. (index == 4) ? (RELAY5_MQTT_TOPIC_PUB) :
  195. (index == 5) ? (RELAY6_MQTT_TOPIC_PUB) :
  196. (index == 6) ? (RELAY7_MQTT_TOPIC_PUB) :
  197. (index == 7) ? (RELAY8_MQTT_TOPIC_PUB) : ""
  198. );
  199. }
  200. constexpr PayloadStatus mqttDisconnectionStatus(size_t index) {
  201. return (
  202. (index == 0) ? (RELAY1_MQTT_DISCONNECT_STATUS) :
  203. (index == 1) ? (RELAY2_MQTT_DISCONNECT_STATUS) :
  204. (index == 2) ? (RELAY3_MQTT_DISCONNECT_STATUS) :
  205. (index == 3) ? (RELAY4_MQTT_DISCONNECT_STATUS) :
  206. (index == 4) ? (RELAY5_MQTT_DISCONNECT_STATUS) :
  207. (index == 5) ? (RELAY6_MQTT_DISCONNECT_STATUS) :
  208. (index == 6) ? (RELAY7_MQTT_DISCONNECT_STATUS) :
  209. (index == 7) ? (RELAY8_MQTT_DISCONNECT_STATUS) : RELAY_MQTT_DISCONNECT_NONE
  210. );
  211. }
  212. } // namespace build
  213. } // namespace relay