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.

284 lines
9.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
  1. /*
  2. BUTTON MODULE
  3. */
  4. #pragma once
  5. #include "espurna.h"
  6. namespace ButtonMask {
  7. constexpr int Pushbutton { 1 << 0 };
  8. constexpr int Switch { 1 << 1 };
  9. constexpr int DefaultLow { 1 << 2 };
  10. constexpr int DefaultHigh { 1 << 3 };
  11. constexpr int DefaultBoot { 1 << 4 };
  12. constexpr int SetPullup { 1 << 5 };
  13. constexpr int SetPulldown { 1 << 6 };
  14. } // namespace ButtonMask
  15. constexpr unsigned char _buttonPin(unsigned char index) {
  16. return (
  17. (index == 0) ? BUTTON1_PIN :
  18. (index == 1) ? BUTTON2_PIN :
  19. (index == 2) ? BUTTON3_PIN :
  20. (index == 3) ? BUTTON4_PIN :
  21. (index == 4) ? BUTTON5_PIN :
  22. (index == 5) ? BUTTON6_PIN :
  23. (index == 6) ? BUTTON7_PIN :
  24. (index == 7) ? BUTTON8_PIN : GPIO_NONE
  25. );
  26. }
  27. constexpr GpioType _buttonPinType(unsigned char index) {
  28. return (
  29. (index == 0) ? BUTTON1_PIN_TYPE :
  30. (index == 1) ? BUTTON2_PIN_TYPE :
  31. (index == 2) ? BUTTON3_PIN_TYPE :
  32. (index == 3) ? BUTTON4_PIN_TYPE :
  33. (index == 4) ? BUTTON5_PIN_TYPE :
  34. (index == 5) ? BUTTON6_PIN_TYPE :
  35. (index == 6) ? BUTTON7_PIN_TYPE :
  36. (index == 7) ? BUTTON8_PIN_TYPE : GPIO_TYPE_NONE
  37. );
  38. }
  39. constexpr int _buttonConfigBitmask(unsigned char index) {
  40. return (
  41. (index == 0) ? (BUTTON1_CONFIG) :
  42. (index == 1) ? (BUTTON2_CONFIG) :
  43. (index == 2) ? (BUTTON3_CONFIG) :
  44. (index == 3) ? (BUTTON4_CONFIG) :
  45. (index == 4) ? (BUTTON5_CONFIG) :
  46. (index == 5) ? (BUTTON6_CONFIG) :
  47. (index == 6) ? (BUTTON7_CONFIG) :
  48. (index == 7) ? (BUTTON8_CONFIG) : (BUTTON_PUSHBUTTON | BUTTON_SET_PULLUP | BUTTON_DEFAULT_HIGH)
  49. );
  50. }
  51. constexpr ButtonAction _buttonRelease(unsigned char index) {
  52. return (
  53. (index == 0) ? BUTTON1_RELEASE :
  54. (index == 1) ? BUTTON2_RELEASE :
  55. (index == 2) ? BUTTON3_RELEASE :
  56. (index == 3) ? BUTTON4_RELEASE :
  57. (index == 4) ? BUTTON5_RELEASE :
  58. (index == 5) ? BUTTON6_RELEASE :
  59. (index == 6) ? BUTTON7_RELEASE :
  60. (index == 7) ? BUTTON8_RELEASE : BUTTON_ACTION_NONE
  61. );
  62. }
  63. constexpr ButtonAction _buttonPress(unsigned char index) {
  64. return (
  65. (index == 0) ? BUTTON1_PRESS :
  66. (index == 1) ? BUTTON2_PRESS :
  67. (index == 2) ? BUTTON3_PRESS :
  68. (index == 3) ? BUTTON4_PRESS :
  69. (index == 4) ? BUTTON5_PRESS :
  70. (index == 5) ? BUTTON6_PRESS :
  71. (index == 6) ? BUTTON7_PRESS :
  72. (index == 7) ? BUTTON8_PRESS : BUTTON_ACTION_NONE
  73. );
  74. }
  75. constexpr ButtonAction _buttonClick(unsigned char index) {
  76. return (
  77. (index == 0) ? BUTTON1_CLICK :
  78. (index == 1) ? BUTTON2_CLICK :
  79. (index == 2) ? BUTTON3_CLICK :
  80. (index == 3) ? BUTTON4_CLICK :
  81. (index == 4) ? BUTTON5_CLICK :
  82. (index == 5) ? BUTTON6_CLICK :
  83. (index == 6) ? BUTTON7_CLICK :
  84. (index == 7) ? BUTTON8_CLICK : BUTTON_ACTION_NONE
  85. );
  86. }
  87. constexpr ButtonAction _buttonDoubleClick(unsigned char index) {
  88. return (
  89. (index == 0) ? BUTTON1_DBLCLICK :
  90. (index == 1) ? BUTTON2_DBLCLICK :
  91. (index == 2) ? BUTTON3_DBLCLICK :
  92. (index == 3) ? BUTTON4_DBLCLICK :
  93. (index == 4) ? BUTTON5_DBLCLICK :
  94. (index == 5) ? BUTTON6_DBLCLICK :
  95. (index == 6) ? BUTTON7_DBLCLICK :
  96. (index == 7) ? BUTTON8_DBLCLICK : BUTTON_ACTION_NONE
  97. );
  98. }
  99. constexpr ButtonAction _buttonTripleClick(unsigned char index) {
  100. return (
  101. (index == 0) ? BUTTON1_TRIPLECLICK :
  102. (index == 1) ? BUTTON2_TRIPLECLICK :
  103. (index == 2) ? BUTTON3_TRIPLECLICK :
  104. (index == 3) ? BUTTON4_TRIPLECLICK :
  105. (index == 4) ? BUTTON5_TRIPLECLICK :
  106. (index == 5) ? BUTTON6_TRIPLECLICK :
  107. (index == 6) ? BUTTON7_TRIPLECLICK :
  108. (index == 7) ? BUTTON8_TRIPLECLICK : BUTTON_ACTION_NONE
  109. );
  110. }
  111. constexpr ButtonAction _buttonLongClick(unsigned char index) {
  112. return (
  113. (index == 0) ? BUTTON1_LNGCLICK :
  114. (index == 1) ? BUTTON2_LNGCLICK :
  115. (index == 2) ? BUTTON3_LNGCLICK :
  116. (index == 3) ? BUTTON4_LNGCLICK :
  117. (index == 4) ? BUTTON5_LNGCLICK :
  118. (index == 5) ? BUTTON6_LNGCLICK :
  119. (index == 6) ? BUTTON7_LNGCLICK :
  120. (index == 7) ? BUTTON8_LNGCLICK : BUTTON_ACTION_NONE
  121. );
  122. }
  123. constexpr ButtonAction _buttonLongLongClick(unsigned char index) {
  124. return (
  125. (index == 0) ? BUTTON1_LNGLNGCLICK :
  126. (index == 1) ? BUTTON2_LNGLNGCLICK :
  127. (index == 2) ? BUTTON3_LNGLNGCLICK :
  128. (index == 3) ? BUTTON4_LNGLNGCLICK :
  129. (index == 4) ? BUTTON5_LNGLNGCLICK :
  130. (index == 5) ? BUTTON6_LNGLNGCLICK :
  131. (index == 6) ? BUTTON7_LNGLNGCLICK :
  132. (index == 7) ? BUTTON8_LNGLNGCLICK : BUTTON_ACTION_NONE
  133. );
  134. }
  135. constexpr unsigned char _buttonRelay(unsigned char index) {
  136. return (
  137. (index == 0) ? (BUTTON1_RELAY - 1) :
  138. (index == 1) ? (BUTTON2_RELAY - 1) :
  139. (index == 2) ? (BUTTON3_RELAY - 1) :
  140. (index == 3) ? (BUTTON4_RELAY - 1) :
  141. (index == 4) ? (BUTTON5_RELAY - 1) :
  142. (index == 5) ? (BUTTON6_RELAY - 1) :
  143. (index == 6) ? (BUTTON7_RELAY - 1) :
  144. (index == 7) ? (BUTTON8_RELAY - 1) : RELAY_NONE
  145. );
  146. }
  147. constexpr unsigned long _buttonDebounceDelay() {
  148. return BUTTON_DEBOUNCE_DELAY;
  149. }
  150. constexpr unsigned long _buttonDebounceDelay(unsigned char index) {
  151. return (
  152. (index == 0) ? BUTTON1_DEBOUNCE_DELAY :
  153. (index == 1) ? BUTTON2_DEBOUNCE_DELAY :
  154. (index == 2) ? BUTTON3_DEBOUNCE_DELAY :
  155. (index == 3) ? BUTTON4_DEBOUNCE_DELAY :
  156. (index == 4) ? BUTTON5_DEBOUNCE_DELAY :
  157. (index == 5) ? BUTTON6_DEBOUNCE_DELAY :
  158. (index == 6) ? BUTTON7_DEBOUNCE_DELAY :
  159. (index == 7) ? BUTTON8_DEBOUNCE_DELAY : _buttonDebounceDelay()
  160. );
  161. }
  162. constexpr unsigned long _buttonRepeatDelay() {
  163. return BUTTON_REPEAT_DELAY;
  164. }
  165. constexpr unsigned long _buttonRepeatDelay(unsigned char index) {
  166. return (
  167. (index == 0) ? BUTTON1_REPEAT_DELAY :
  168. (index == 1) ? BUTTON2_REPEAT_DELAY :
  169. (index == 2) ? BUTTON3_REPEAT_DELAY :
  170. (index == 3) ? BUTTON4_REPEAT_DELAY :
  171. (index == 4) ? BUTTON5_REPEAT_DELAY :
  172. (index == 5) ? BUTTON6_REPEAT_DELAY :
  173. (index == 6) ? BUTTON7_REPEAT_DELAY :
  174. (index == 7) ? BUTTON8_REPEAT_DELAY : _buttonRepeatDelay()
  175. );
  176. }
  177. constexpr unsigned long _buttonLongClickDelay() {
  178. return BUTTON_LNGCLICK_DELAY;
  179. }
  180. constexpr unsigned long _buttonLongClickDelay(unsigned char index) {
  181. return (
  182. (index == 0) ? BUTTON1_LNGCLICK_DELAY :
  183. (index == 1) ? BUTTON2_LNGCLICK_DELAY :
  184. (index == 2) ? BUTTON3_LNGCLICK_DELAY :
  185. (index == 3) ? BUTTON4_LNGCLICK_DELAY :
  186. (index == 4) ? BUTTON5_LNGCLICK_DELAY :
  187. (index == 5) ? BUTTON6_LNGCLICK_DELAY :
  188. (index == 6) ? BUTTON7_LNGCLICK_DELAY :
  189. (index == 7) ? BUTTON8_LNGCLICK_DELAY : _buttonLongClickDelay()
  190. );
  191. }
  192. constexpr unsigned long _buttonLongLongClickDelay() {
  193. return BUTTON_LNGLNGCLICK_DELAY;
  194. }
  195. constexpr unsigned long _buttonLongLongClickDelay(unsigned char index) {
  196. return (
  197. (index == 0) ? BUTTON1_LNGLNGCLICK_DELAY :
  198. (index == 1) ? BUTTON2_LNGLNGCLICK_DELAY :
  199. (index == 2) ? BUTTON3_LNGLNGCLICK_DELAY :
  200. (index == 3) ? BUTTON4_LNGLNGCLICK_DELAY :
  201. (index == 4) ? BUTTON5_LNGLNGCLICK_DELAY :
  202. (index == 5) ? BUTTON6_LNGLNGCLICK_DELAY :
  203. (index == 6) ? BUTTON7_LNGLNGCLICK_DELAY :
  204. (index == 7) ? BUTTON8_LNGLNGCLICK_DELAY : _buttonLongLongClickDelay()
  205. );
  206. }
  207. constexpr bool _buttonMqttSendAllEvents(unsigned char index) {
  208. return (
  209. (index == 0) ? (1 == BUTTON1_MQTT_SEND_ALL_EVENTS) :
  210. (index == 1) ? (1 == BUTTON2_MQTT_SEND_ALL_EVENTS) :
  211. (index == 2) ? (1 == BUTTON3_MQTT_SEND_ALL_EVENTS) :
  212. (index == 3) ? (1 == BUTTON4_MQTT_SEND_ALL_EVENTS) :
  213. (index == 4) ? (1 == BUTTON5_MQTT_SEND_ALL_EVENTS) :
  214. (index == 5) ? (1 == BUTTON6_MQTT_SEND_ALL_EVENTS) :
  215. (index == 6) ? (1 == BUTTON7_MQTT_SEND_ALL_EVENTS) :
  216. (index == 7) ? (1 == BUTTON8_MQTT_SEND_ALL_EVENTS) : (1 == BUTTON_MQTT_SEND_ALL_EVENTS)
  217. );
  218. }
  219. constexpr bool _buttonMqttRetain(unsigned char index) {
  220. return (
  221. (index == 0) ? (1 == BUTTON1_MQTT_RETAIN) :
  222. (index == 1) ? (1 == BUTTON2_MQTT_RETAIN) :
  223. (index == 2) ? (1 == BUTTON3_MQTT_RETAIN) :
  224. (index == 3) ? (1 == BUTTON4_MQTT_RETAIN) :
  225. (index == 4) ? (1 == BUTTON5_MQTT_RETAIN) :
  226. (index == 5) ? (1 == BUTTON6_MQTT_RETAIN) :
  227. (index == 6) ? (1 == BUTTON7_MQTT_RETAIN) :
  228. (index == 7) ? (1 == BUTTON8_MQTT_RETAIN) : (1 == BUTTON_MQTT_RETAIN)
  229. );
  230. }
  231. constexpr ButtonProvider _buttonProvider(unsigned char index) {
  232. return (
  233. (index == 0) ? (BUTTON1_PROVIDER) :
  234. (index == 1) ? (BUTTON2_PROVIDER) :
  235. (index == 2) ? (BUTTON3_PROVIDER) :
  236. (index == 3) ? (BUTTON4_PROVIDER) :
  237. (index == 4) ? (BUTTON5_PROVIDER) :
  238. (index == 5) ? (BUTTON6_PROVIDER) :
  239. (index == 6) ? (BUTTON7_PROVIDER) :
  240. (index == 7) ? (BUTTON8_PROVIDER) : BUTTON_PROVIDER_NONE
  241. );
  242. }
  243. constexpr int _buttonAnalogLevel(unsigned char index) {
  244. return (
  245. (index == 0) ? (BUTTON1_ANALOG_LEVEL) :
  246. (index == 1) ? (BUTTON2_ANALOG_LEVEL) :
  247. (index == 2) ? (BUTTON3_ANALOG_LEVEL) :
  248. (index == 3) ? (BUTTON4_ANALOG_LEVEL) :
  249. (index == 4) ? (BUTTON5_ANALOG_LEVEL) :
  250. (index == 5) ? (BUTTON6_ANALOG_LEVEL) :
  251. (index == 6) ? (BUTTON7_ANALOG_LEVEL) :
  252. (index == 7) ? (BUTTON8_ANALOG_LEVEL) : 0
  253. );
  254. }