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.

329 lines
10 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
  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. namespace button {
  16. namespace build {
  17. constexpr size_t pin(size_t index) {
  18. return (
  19. (index == 0) ? BUTTON1_PIN :
  20. (index == 1) ? BUTTON2_PIN :
  21. (index == 2) ? BUTTON3_PIN :
  22. (index == 3) ? BUTTON4_PIN :
  23. (index == 4) ? BUTTON5_PIN :
  24. (index == 5) ? BUTTON6_PIN :
  25. (index == 6) ? BUTTON7_PIN :
  26. (index == 7) ? BUTTON8_PIN : GPIO_NONE
  27. );
  28. }
  29. constexpr GpioType pinType(size_t index) {
  30. return (
  31. (index == 0) ? BUTTON1_PIN_TYPE :
  32. (index == 1) ? BUTTON2_PIN_TYPE :
  33. (index == 2) ? BUTTON3_PIN_TYPE :
  34. (index == 3) ? BUTTON4_PIN_TYPE :
  35. (index == 4) ? BUTTON5_PIN_TYPE :
  36. (index == 5) ? BUTTON6_PIN_TYPE :
  37. (index == 6) ? BUTTON7_PIN_TYPE :
  38. (index == 7) ? BUTTON8_PIN_TYPE : GPIO_TYPE_NONE
  39. );
  40. }
  41. constexpr int configBitmask(size_t index) {
  42. return (
  43. (index == 0) ? (BUTTON1_CONFIG) :
  44. (index == 1) ? (BUTTON2_CONFIG) :
  45. (index == 2) ? (BUTTON3_CONFIG) :
  46. (index == 3) ? (BUTTON4_CONFIG) :
  47. (index == 4) ? (BUTTON5_CONFIG) :
  48. (index == 5) ? (BUTTON6_CONFIG) :
  49. (index == 6) ? (BUTTON7_CONFIG) :
  50. (index == 7) ? (BUTTON8_CONFIG) : (BUTTON_PUSHBUTTON | BUTTON_SET_PULLUP | BUTTON_DEFAULT_HIGH)
  51. );
  52. }
  53. namespace internal {
  54. constexpr debounce_event::types::Config decode(int bitmask) {
  55. return {
  56. ((bitmask & ButtonMask::Pushbutton)
  57. ? debounce_event::types::Mode::Pushbutton
  58. : debounce_event::types::Mode::Switch),
  59. ((bitmask & ButtonMask::DefaultLow) ? debounce_event::types::PinValue::Low
  60. : (bitmask & ButtonMask::DefaultHigh) ? debounce_event::types::PinValue::High
  61. : (bitmask & ButtonMask::DefaultBoot) ? debounce_event::types::PinValue::Initial
  62. : debounce_event::types::PinValue::Low),
  63. ((bitmask & ButtonMask::SetPullup) ? debounce_event::types::PinMode::InputPullup
  64. : (bitmask & ButtonMask::SetPulldown) ? debounce_event::types::PinMode::InputPulldown
  65. : debounce_event::types::PinMode::Input)
  66. };
  67. }
  68. } // namespace internal
  69. constexpr debounce_event::types::Mode mode(size_t index) {
  70. return internal::decode(configBitmask(index)).mode;
  71. }
  72. constexpr debounce_event::types::PinValue defaultValue(size_t index) {
  73. return internal::decode(configBitmask(index)).default_value;
  74. }
  75. constexpr debounce_event::types::PinMode pinMode(size_t index) {
  76. return internal::decode(configBitmask(index)).pin_mode;
  77. }
  78. constexpr ButtonAction release(size_t index) {
  79. return (
  80. (index == 0) ? BUTTON1_RELEASE :
  81. (index == 1) ? BUTTON2_RELEASE :
  82. (index == 2) ? BUTTON3_RELEASE :
  83. (index == 3) ? BUTTON4_RELEASE :
  84. (index == 4) ? BUTTON5_RELEASE :
  85. (index == 5) ? BUTTON6_RELEASE :
  86. (index == 6) ? BUTTON7_RELEASE :
  87. (index == 7) ? BUTTON8_RELEASE : BUTTON_ACTION_NONE
  88. );
  89. }
  90. constexpr ButtonAction press(size_t index) {
  91. return (
  92. (index == 0) ? BUTTON1_PRESS :
  93. (index == 1) ? BUTTON2_PRESS :
  94. (index == 2) ? BUTTON3_PRESS :
  95. (index == 3) ? BUTTON4_PRESS :
  96. (index == 4) ? BUTTON5_PRESS :
  97. (index == 5) ? BUTTON6_PRESS :
  98. (index == 6) ? BUTTON7_PRESS :
  99. (index == 7) ? BUTTON8_PRESS : BUTTON_ACTION_NONE
  100. );
  101. }
  102. constexpr ButtonAction click(size_t index) {
  103. return (
  104. (index == 0) ? BUTTON1_CLICK :
  105. (index == 1) ? BUTTON2_CLICK :
  106. (index == 2) ? BUTTON3_CLICK :
  107. (index == 3) ? BUTTON4_CLICK :
  108. (index == 4) ? BUTTON5_CLICK :
  109. (index == 5) ? BUTTON6_CLICK :
  110. (index == 6) ? BUTTON7_CLICK :
  111. (index == 7) ? BUTTON8_CLICK : BUTTON_ACTION_NONE
  112. );
  113. }
  114. constexpr ButtonAction doubleClick(size_t index) {
  115. return (
  116. (index == 0) ? BUTTON1_DBLCLICK :
  117. (index == 1) ? BUTTON2_DBLCLICK :
  118. (index == 2) ? BUTTON3_DBLCLICK :
  119. (index == 3) ? BUTTON4_DBLCLICK :
  120. (index == 4) ? BUTTON5_DBLCLICK :
  121. (index == 5) ? BUTTON6_DBLCLICK :
  122. (index == 6) ? BUTTON7_DBLCLICK :
  123. (index == 7) ? BUTTON8_DBLCLICK : BUTTON_ACTION_NONE
  124. );
  125. }
  126. constexpr ButtonAction tripleClick(size_t index) {
  127. return (
  128. (index == 0) ? BUTTON1_TRIPLECLICK :
  129. (index == 1) ? BUTTON2_TRIPLECLICK :
  130. (index == 2) ? BUTTON3_TRIPLECLICK :
  131. (index == 3) ? BUTTON4_TRIPLECLICK :
  132. (index == 4) ? BUTTON5_TRIPLECLICK :
  133. (index == 5) ? BUTTON6_TRIPLECLICK :
  134. (index == 6) ? BUTTON7_TRIPLECLICK :
  135. (index == 7) ? BUTTON8_TRIPLECLICK : BUTTON_ACTION_NONE
  136. );
  137. }
  138. constexpr ButtonAction longClick(size_t index) {
  139. return (
  140. (index == 0) ? BUTTON1_LNGCLICK :
  141. (index == 1) ? BUTTON2_LNGCLICK :
  142. (index == 2) ? BUTTON3_LNGCLICK :
  143. (index == 3) ? BUTTON4_LNGCLICK :
  144. (index == 4) ? BUTTON5_LNGCLICK :
  145. (index == 5) ? BUTTON6_LNGCLICK :
  146. (index == 6) ? BUTTON7_LNGCLICK :
  147. (index == 7) ? BUTTON8_LNGCLICK : BUTTON_ACTION_NONE
  148. );
  149. }
  150. constexpr ButtonAction longLongClick(size_t index) {
  151. return (
  152. (index == 0) ? BUTTON1_LNGLNGCLICK :
  153. (index == 1) ? BUTTON2_LNGLNGCLICK :
  154. (index == 2) ? BUTTON3_LNGLNGCLICK :
  155. (index == 3) ? BUTTON4_LNGLNGCLICK :
  156. (index == 4) ? BUTTON5_LNGLNGCLICK :
  157. (index == 5) ? BUTTON6_LNGLNGCLICK :
  158. (index == 6) ? BUTTON7_LNGLNGCLICK :
  159. (index == 7) ? BUTTON8_LNGLNGCLICK : BUTTON_ACTION_NONE
  160. );
  161. }
  162. constexpr size_t relay(size_t index) {
  163. return (
  164. (index == 0) ? (BUTTON1_RELAY - 1) :
  165. (index == 1) ? (BUTTON2_RELAY - 1) :
  166. (index == 2) ? (BUTTON3_RELAY - 1) :
  167. (index == 3) ? (BUTTON4_RELAY - 1) :
  168. (index == 4) ? (BUTTON5_RELAY - 1) :
  169. (index == 5) ? (BUTTON6_RELAY - 1) :
  170. (index == 6) ? (BUTTON7_RELAY - 1) :
  171. (index == 7) ? (BUTTON8_RELAY - 1) : RELAY_NONE
  172. );
  173. }
  174. constexpr unsigned long debounceDelay() {
  175. return BUTTON_DEBOUNCE_DELAY;
  176. }
  177. constexpr unsigned long debounceDelay(size_t index) {
  178. return (
  179. (index == 0) ? BUTTON1_DEBOUNCE_DELAY :
  180. (index == 1) ? BUTTON2_DEBOUNCE_DELAY :
  181. (index == 2) ? BUTTON3_DEBOUNCE_DELAY :
  182. (index == 3) ? BUTTON4_DEBOUNCE_DELAY :
  183. (index == 4) ? BUTTON5_DEBOUNCE_DELAY :
  184. (index == 5) ? BUTTON6_DEBOUNCE_DELAY :
  185. (index == 6) ? BUTTON7_DEBOUNCE_DELAY :
  186. (index == 7) ? BUTTON8_DEBOUNCE_DELAY : debounceDelay()
  187. );
  188. }
  189. constexpr unsigned long repeatDelay() {
  190. return BUTTON_REPEAT_DELAY;
  191. }
  192. constexpr unsigned long repeatDelay(size_t index) {
  193. return (
  194. (index == 0) ? BUTTON1_REPEAT_DELAY :
  195. (index == 1) ? BUTTON2_REPEAT_DELAY :
  196. (index == 2) ? BUTTON3_REPEAT_DELAY :
  197. (index == 3) ? BUTTON4_REPEAT_DELAY :
  198. (index == 4) ? BUTTON5_REPEAT_DELAY :
  199. (index == 5) ? BUTTON6_REPEAT_DELAY :
  200. (index == 6) ? BUTTON7_REPEAT_DELAY :
  201. (index == 7) ? BUTTON8_REPEAT_DELAY : repeatDelay()
  202. );
  203. }
  204. constexpr unsigned long longClickDelay() {
  205. return BUTTON_LNGCLICK_DELAY;
  206. }
  207. constexpr unsigned long longClickDelay(size_t index) {
  208. return (
  209. (index == 0) ? BUTTON1_LNGCLICK_DELAY :
  210. (index == 1) ? BUTTON2_LNGCLICK_DELAY :
  211. (index == 2) ? BUTTON3_LNGCLICK_DELAY :
  212. (index == 3) ? BUTTON4_LNGCLICK_DELAY :
  213. (index == 4) ? BUTTON5_LNGCLICK_DELAY :
  214. (index == 5) ? BUTTON6_LNGCLICK_DELAY :
  215. (index == 6) ? BUTTON7_LNGCLICK_DELAY :
  216. (index == 7) ? BUTTON8_LNGCLICK_DELAY : longClickDelay()
  217. );
  218. }
  219. constexpr unsigned long longLongClickDelay() {
  220. return BUTTON_LNGLNGCLICK_DELAY;
  221. }
  222. constexpr unsigned long longLongClickDelay(size_t index) {
  223. return (
  224. (index == 0) ? BUTTON1_LNGLNGCLICK_DELAY :
  225. (index == 1) ? BUTTON2_LNGLNGCLICK_DELAY :
  226. (index == 2) ? BUTTON3_LNGLNGCLICK_DELAY :
  227. (index == 3) ? BUTTON4_LNGLNGCLICK_DELAY :
  228. (index == 4) ? BUTTON5_LNGLNGCLICK_DELAY :
  229. (index == 5) ? BUTTON6_LNGLNGCLICK_DELAY :
  230. (index == 6) ? BUTTON7_LNGLNGCLICK_DELAY :
  231. (index == 7) ? BUTTON8_LNGLNGCLICK_DELAY : longLongClickDelay()
  232. );
  233. }
  234. constexpr bool mqttSendAllEvents() {
  235. return (1 == BUTTON_MQTT_SEND_ALL_EVENTS);
  236. }
  237. constexpr bool mqttSendAllEvents(size_t index) {
  238. return (
  239. (index == 0) ? (1 == BUTTON1_MQTT_SEND_ALL_EVENTS) :
  240. (index == 1) ? (1 == BUTTON2_MQTT_SEND_ALL_EVENTS) :
  241. (index == 2) ? (1 == BUTTON3_MQTT_SEND_ALL_EVENTS) :
  242. (index == 3) ? (1 == BUTTON4_MQTT_SEND_ALL_EVENTS) :
  243. (index == 4) ? (1 == BUTTON5_MQTT_SEND_ALL_EVENTS) :
  244. (index == 5) ? (1 == BUTTON6_MQTT_SEND_ALL_EVENTS) :
  245. (index == 6) ? (1 == BUTTON7_MQTT_SEND_ALL_EVENTS) :
  246. (index == 7) ? (1 == BUTTON8_MQTT_SEND_ALL_EVENTS) : mqttSendAllEvents()
  247. );
  248. }
  249. constexpr bool mqttRetain() {
  250. return (1 == BUTTON_MQTT_RETAIN);
  251. }
  252. constexpr bool mqttRetain(size_t index) {
  253. return (
  254. (index == 0) ? (1 == BUTTON1_MQTT_RETAIN) :
  255. (index == 1) ? (1 == BUTTON2_MQTT_RETAIN) :
  256. (index == 2) ? (1 == BUTTON3_MQTT_RETAIN) :
  257. (index == 3) ? (1 == BUTTON4_MQTT_RETAIN) :
  258. (index == 4) ? (1 == BUTTON5_MQTT_RETAIN) :
  259. (index == 5) ? (1 == BUTTON6_MQTT_RETAIN) :
  260. (index == 6) ? (1 == BUTTON7_MQTT_RETAIN) :
  261. (index == 7) ? (1 == BUTTON8_MQTT_RETAIN) : mqttRetain()
  262. );
  263. }
  264. constexpr ButtonProvider provider(size_t index) {
  265. return (
  266. (index == 0) ? (BUTTON1_PROVIDER) :
  267. (index == 1) ? (BUTTON2_PROVIDER) :
  268. (index == 2) ? (BUTTON3_PROVIDER) :
  269. (index == 3) ? (BUTTON4_PROVIDER) :
  270. (index == 4) ? (BUTTON5_PROVIDER) :
  271. (index == 5) ? (BUTTON6_PROVIDER) :
  272. (index == 6) ? (BUTTON7_PROVIDER) :
  273. (index == 7) ? (BUTTON8_PROVIDER) : BUTTON_PROVIDER_NONE
  274. );
  275. }
  276. constexpr int analogLevel(size_t index) {
  277. return (
  278. (index == 0) ? (BUTTON1_ANALOG_LEVEL) :
  279. (index == 1) ? (BUTTON2_ANALOG_LEVEL) :
  280. (index == 2) ? (BUTTON3_ANALOG_LEVEL) :
  281. (index == 3) ? (BUTTON4_ANALOG_LEVEL) :
  282. (index == 4) ? (BUTTON5_ANALOG_LEVEL) :
  283. (index == 5) ? (BUTTON6_ANALOG_LEVEL) :
  284. (index == 6) ? (BUTTON7_ANALOG_LEVEL) :
  285. (index == 7) ? (BUTTON8_ANALOG_LEVEL) : 0
  286. );
  287. }
  288. } // namespace build
  289. } // namespace button