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.

172 lines
5.8 KiB

  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 char _buttonDecodeEventAction(unsigned long actions, unsigned char event) {
  114. return (
  115. (event == BUTTON_EVENT_PRESSED) ? ((actions) & 0x0F) :
  116. (event == BUTTON_EVENT_CLICK) ? ((actions >> 4) & 0x0F) :
  117. (event == BUTTON_EVENT_DBLCLICK) ? ((actions >> 8) & 0x0F) :
  118. (event == BUTTON_EVENT_LNGCLICK) ? ((actions >> 12) & 0x0F) :
  119. (event == BUTTON_EVENT_LNGLNGCLICK) ? ((actions >> 16) & 0x0F) :
  120. (event == BUTTON_EVENT_TRIPLECLICK) ? ((actions >> 20) & 0x0F) : BUTTON_MODE_NONE
  121. );
  122. }
  123. constexpr const uint8_t _buttonMapReleased(uint8_t count, uint16_t length) {
  124. return (
  125. (1 == count) ? (
  126. (length > BUTTON_LNGLNGCLICK_DELAY) ? BUTTON_EVENT_LNGLNGCLICK :
  127. (length > BUTTON_LNGCLICK_DELAY) ? BUTTON_EVENT_LNGCLICK : BUTTON_EVENT_CLICK
  128. ) :
  129. (2 == count) ? BUTTON_EVENT_DBLCLICK :
  130. (3 == count) ? BUTTON_EVENT_TRIPLECLICK :
  131. BUTTON_EVENT_NONE
  132. );
  133. }
  134. constexpr const uint8_t _buttonMapEvent(uint8_t event, uint8_t count, uint16_t length) {
  135. return (
  136. (event == EVENT_PRESSED) ? BUTTON_EVENT_PRESSED :
  137. (event == EVENT_CHANGED) ? BUTTON_EVENT_CLICK :
  138. (event == EVENT_RELEASED) ? _buttonMapReleased(count, length) :
  139. BUTTON_EVENT_NONE
  140. );
  141. }
  142. constexpr uint32_t _buttonConstructActions(unsigned long pressed, unsigned long click, unsigned long dblclick, unsigned long lngclick, unsigned long lnglngclick, unsigned long tripleclick) {
  143. return (
  144. (tripleclick << 20) |
  145. (lnglngclick << 16) |
  146. (lngclick << 12) |
  147. (dblclick << 8) |
  148. (click << 4) |
  149. pressed
  150. );
  151. }
  152. constexpr uint32_t _buttonConstructActions(unsigned char id) {
  153. return _buttonConstructActions(_buttonPress(id), _buttonClick(id), _buttonDoubleClick(id), _buttonLongClick(id), _buttonLongLongClick(id), _buttonTripleClick(id));
  154. }