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.3 KiB

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