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.

203 lines
7.1 KiB

4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
  1. /*
  2. BUTTON MODULE
  3. */
  4. #pragma once
  5. constexpr const unsigned char _buttonPin(unsigned char index) {
  6. return (
  7. (index == 0) ? BUTTON1_PIN :
  8. (index == 1) ? BUTTON2_PIN :
  9. (index == 2) ? BUTTON3_PIN :
  10. (index == 3) ? BUTTON4_PIN :
  11. (index == 4) ? BUTTON5_PIN :
  12. (index == 5) ? BUTTON6_PIN :
  13. (index == 6) ? BUTTON7_PIN :
  14. (index == 7) ? BUTTON8_PIN : GPIO_NONE
  15. );
  16. }
  17. constexpr const unsigned char _buttonMode(unsigned char index) {
  18. return (
  19. (index == 0) ? BUTTON1_MODE :
  20. (index == 1) ? BUTTON2_MODE :
  21. (index == 2) ? BUTTON3_MODE :
  22. (index == 3) ? BUTTON4_MODE :
  23. (index == 4) ? BUTTON5_MODE :
  24. (index == 5) ? BUTTON6_MODE :
  25. (index == 6) ? BUTTON7_MODE :
  26. (index == 7) ? BUTTON8_MODE : (BUTTON_PUSHBUTTON | BUTTON_SET_PULLUP | BUTTON_DEFAULT_HIGH)
  27. );
  28. }
  29. constexpr const unsigned char _buttonPress(unsigned char index) {
  30. return (
  31. (index == 0) ? BUTTON1_PRESS :
  32. (index == 1) ? BUTTON2_PRESS :
  33. (index == 2) ? BUTTON3_PRESS :
  34. (index == 3) ? BUTTON4_PRESS :
  35. (index == 4) ? BUTTON5_PRESS :
  36. (index == 5) ? BUTTON6_PRESS :
  37. (index == 6) ? BUTTON7_PRESS :
  38. (index == 7) ? BUTTON8_PRESS : BUTTON_MODE_NONE
  39. );
  40. }
  41. constexpr const unsigned char _buttonClick(unsigned char index) {
  42. return (
  43. (index == 0) ? BUTTON1_CLICK :
  44. (index == 1) ? BUTTON2_CLICK :
  45. (index == 2) ? BUTTON3_CLICK :
  46. (index == 3) ? BUTTON4_CLICK :
  47. (index == 4) ? BUTTON5_CLICK :
  48. (index == 5) ? BUTTON6_CLICK :
  49. (index == 6) ? BUTTON7_CLICK :
  50. (index == 7) ? BUTTON8_CLICK : BUTTON_MODE_NONE
  51. );
  52. }
  53. constexpr const unsigned char _buttonDoubleClick(unsigned char index) {
  54. return (
  55. (index == 0) ? BUTTON1_DBLCLICK :
  56. (index == 1) ? BUTTON2_DBLCLICK :
  57. (index == 2) ? BUTTON3_DBLCLICK :
  58. (index == 3) ? BUTTON4_DBLCLICK :
  59. (index == 4) ? BUTTON5_DBLCLICK :
  60. (index == 5) ? BUTTON6_DBLCLICK :
  61. (index == 6) ? BUTTON7_DBLCLICK :
  62. (index == 7) ? BUTTON8_DBLCLICK : BUTTON_MODE_NONE
  63. );
  64. }
  65. constexpr const unsigned char _buttonTripleClick(unsigned char index) {
  66. return (
  67. (index == 0) ? BUTTON1_TRIPLECLICK :
  68. (index == 1) ? BUTTON2_TRIPLECLICK :
  69. (index == 2) ? BUTTON3_TRIPLECLICK :
  70. (index == 3) ? BUTTON4_TRIPLECLICK :
  71. (index == 4) ? BUTTON5_TRIPLECLICK :
  72. (index == 5) ? BUTTON6_TRIPLECLICK :
  73. (index == 6) ? BUTTON7_TRIPLECLICK :
  74. (index == 7) ? BUTTON8_TRIPLECLICK : BUTTON_MODE_NONE
  75. );
  76. }
  77. constexpr const unsigned char _buttonLongClick(unsigned char index) {
  78. return (
  79. (index == 0) ? BUTTON1_LNGCLICK :
  80. (index == 1) ? BUTTON2_LNGCLICK :
  81. (index == 2) ? BUTTON3_LNGCLICK :
  82. (index == 3) ? BUTTON4_LNGCLICK :
  83. (index == 4) ? BUTTON5_LNGCLICK :
  84. (index == 5) ? BUTTON6_LNGCLICK :
  85. (index == 6) ? BUTTON7_LNGCLICK :
  86. (index == 7) ? BUTTON8_LNGCLICK : BUTTON_MODE_NONE
  87. );
  88. }
  89. constexpr const unsigned char _buttonLongLongClick(unsigned char index) {
  90. return (
  91. (index == 0) ? BUTTON1_LNGLNGCLICK :
  92. (index == 1) ? BUTTON2_LNGLNGCLICK :
  93. (index == 2) ? BUTTON3_LNGLNGCLICK :
  94. (index == 3) ? BUTTON4_LNGLNGCLICK :
  95. (index == 4) ? BUTTON5_LNGLNGCLICK :
  96. (index == 5) ? BUTTON6_LNGLNGCLICK :
  97. (index == 6) ? BUTTON7_LNGLNGCLICK :
  98. (index == 7) ? BUTTON8_LNGLNGCLICK : BUTTON_MODE_NONE
  99. );
  100. }
  101. constexpr const unsigned char _buttonRelay(unsigned char index) {
  102. return (
  103. (index == 0) ? (BUTTON1_RELAY - 1) :
  104. (index == 1) ? (BUTTON2_RELAY - 1) :
  105. (index == 2) ? (BUTTON3_RELAY - 1) :
  106. (index == 3) ? (BUTTON4_RELAY - 1) :
  107. (index == 4) ? (BUTTON5_RELAY - 1) :
  108. (index == 5) ? (BUTTON6_RELAY - 1) :
  109. (index == 6) ? (BUTTON7_RELAY - 1) :
  110. (index == 7) ? (BUTTON8_RELAY - 1) : RELAY_NONE
  111. );
  112. }
  113. constexpr const unsigned long _buttonDebounceDelay(unsigned char index) {
  114. return (
  115. (index == 0) ? BUTTON1_DEBOUNCE_DELAY :
  116. (index == 1) ? BUTTON2_DEBOUNCE_DELAY :
  117. (index == 2) ? BUTTON3_DEBOUNCE_DELAY :
  118. (index == 3) ? BUTTON4_DEBOUNCE_DELAY :
  119. (index == 4) ? BUTTON5_DEBOUNCE_DELAY :
  120. (index == 5) ? BUTTON6_DEBOUNCE_DELAY :
  121. (index == 6) ? BUTTON7_DEBOUNCE_DELAY :
  122. (index == 7) ? BUTTON8_DEBOUNCE_DELAY : BUTTON_DEBOUNCE_DELAY
  123. );
  124. }
  125. constexpr const unsigned long _buttonDoubleClickDelay(unsigned char index) {
  126. return (
  127. (index == 0) ? BUTTON1_DBLCLICK_DELAY :
  128. (index == 1) ? BUTTON2_DBLCLICK_DELAY :
  129. (index == 2) ? BUTTON3_DBLCLICK_DELAY :
  130. (index == 3) ? BUTTON4_DBLCLICK_DELAY :
  131. (index == 4) ? BUTTON5_DBLCLICK_DELAY :
  132. (index == 5) ? BUTTON6_DBLCLICK_DELAY :
  133. (index == 6) ? BUTTON7_DBLCLICK_DELAY :
  134. (index == 7) ? BUTTON8_DBLCLICK_DELAY : BUTTON_DBLCLICK_DELAY
  135. );
  136. }
  137. constexpr const unsigned long _buttonLongClickDelay(unsigned char index) {
  138. return (
  139. (index == 0) ? BUTTON1_LNGCLICK_DELAY :
  140. (index == 1) ? BUTTON2_LNGCLICK_DELAY :
  141. (index == 2) ? BUTTON3_LNGCLICK_DELAY :
  142. (index == 3) ? BUTTON4_LNGCLICK_DELAY :
  143. (index == 4) ? BUTTON5_LNGCLICK_DELAY :
  144. (index == 5) ? BUTTON6_LNGCLICK_DELAY :
  145. (index == 6) ? BUTTON7_LNGCLICK_DELAY :
  146. (index == 7) ? BUTTON8_LNGCLICK_DELAY : BUTTON_LNGCLICK_DELAY
  147. );
  148. }
  149. constexpr const unsigned long _buttonLongLongClickDelay(unsigned char index) {
  150. return (
  151. (index == 0) ? BUTTON1_LNGLNGCLICK_DELAY :
  152. (index == 1) ? BUTTON2_LNGLNGCLICK_DELAY :
  153. (index == 2) ? BUTTON3_LNGLNGCLICK_DELAY :
  154. (index == 3) ? BUTTON4_LNGLNGCLICK_DELAY :
  155. (index == 4) ? BUTTON5_LNGLNGCLICK_DELAY :
  156. (index == 5) ? BUTTON6_LNGLNGCLICK_DELAY :
  157. (index == 6) ? BUTTON7_LNGLNGCLICK_DELAY :
  158. (index == 7) ? BUTTON8_LNGLNGCLICK_DELAY : BUTTON_LNGLNGCLICK_DELAY
  159. );
  160. }
  161. constexpr const unsigned char _buttonDecodeEventAction(unsigned long actions, unsigned char event) {
  162. return (
  163. (event == BUTTON_EVENT_PRESSED) ? ((actions) & 0x0F) :
  164. (event == BUTTON_EVENT_CLICK) ? ((actions >> 4) & 0x0F) :
  165. (event == BUTTON_EVENT_DBLCLICK) ? ((actions >> 8) & 0x0F) :
  166. (event == BUTTON_EVENT_LNGCLICK) ? ((actions >> 12) & 0x0F) :
  167. (event == BUTTON_EVENT_LNGLNGCLICK) ? ((actions >> 16) & 0x0F) :
  168. (event == BUTTON_EVENT_TRIPLECLICK) ? ((actions >> 20) & 0x0F) : BUTTON_MODE_NONE
  169. );
  170. }
  171. constexpr uint32_t _buttonConstructActions(unsigned long pressed, unsigned long click, unsigned long dblclick, unsigned long lngclick, unsigned long lnglngclick, unsigned long tripleclick) {
  172. return (
  173. (tripleclick << 20) |
  174. (lnglngclick << 16) |
  175. (lngclick << 12) |
  176. (dblclick << 8) |
  177. (click << 4) |
  178. pressed
  179. );
  180. }
  181. constexpr uint32_t _buttonConstructActions(unsigned char id) {
  182. return _buttonConstructActions(_buttonPress(id), _buttonClick(id), _buttonDoubleClick(id), _buttonLongClick(id), _buttonLongLongClick(id), _buttonTripleClick(id));
  183. }