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.

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