My Marlin configs for Fabrikator Mini and CTC i3 Pro B
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.

buttons.h 6.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. /**
  2. * Marlin 3D Printer Firmware
  3. * Copyright (c) 2020 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
  4. *
  5. * Based on Sprinter and grbl.
  6. * Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm
  7. *
  8. * This program is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation, either version 3 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  20. *
  21. */
  22. #pragma once
  23. #include "../inc/MarlinConfig.h"
  24. #if ((!HAS_ADC_BUTTONS && IS_NEWPANEL) || BUTTONS_EXIST(EN1, EN2)) && !IS_TFTGLCD_PANEL
  25. #define HAS_ENCODER_WHEEL 1
  26. #endif
  27. #if (HAS_ENCODER_WHEEL || ANY_BUTTON(ENC, BACK, UP, DOWN, LEFT, RIGHT)) && DISABLED(TOUCH_UI_FTDI_EVE)
  28. #define HAS_DIGITAL_BUTTONS 1
  29. #endif
  30. #if !HAS_ADC_BUTTONS && (IS_RRW_KEYPAD || (HAS_WIRED_LCD && !IS_NEWPANEL))
  31. #define HAS_SHIFT_ENCODER 1
  32. #endif
  33. // I2C buttons must be read in the main thread
  34. #if ANY(LCD_I2C_VIKI, LCD_I2C_PANELOLU2, IS_TFTGLCD_PANEL)
  35. #define HAS_SLOW_BUTTONS 1
  36. #endif
  37. #if HAS_ENCODER_WHEEL
  38. #define ENCODER_PHASE_0 0
  39. #define ENCODER_PHASE_1 2
  40. #define ENCODER_PHASE_2 3
  41. #define ENCODER_PHASE_3 1
  42. #endif
  43. #if EITHER(HAS_DIGITAL_BUTTONS, HAS_DWIN_E3V2)
  44. // Wheel spin pins where BA is 00, 10, 11, 01 (1 bit always changes)
  45. #define BLEN_A 0
  46. #define BLEN_B 1
  47. #define EN_A _BV(BLEN_A)
  48. #define EN_B _BV(BLEN_B)
  49. #define _BUTTON_PRESSED(BN) !READ(BTN_##BN)
  50. #if BUTTON_EXISTS(ENC) || HAS_TOUCH_BUTTONS
  51. #define BLEN_C 2
  52. #define EN_C _BV(BLEN_C)
  53. #endif
  54. #if ENABLED(LCD_I2C_VIKI)
  55. #include <LiquidTWI2.h>
  56. #define B_I2C_BTN_OFFSET 3 // (the first three bit positions reserved for EN_A, EN_B, EN_C)
  57. // button and encoder bit positions within 'buttons'
  58. #define B_LE (BUTTON_LEFT << B_I2C_BTN_OFFSET) // The remaining normalized buttons are all read via I2C
  59. #define B_UP (BUTTON_UP << B_I2C_BTN_OFFSET)
  60. #define B_MI (BUTTON_SELECT << B_I2C_BTN_OFFSET)
  61. #define B_DW (BUTTON_DOWN << B_I2C_BTN_OFFSET)
  62. #define B_RI (BUTTON_RIGHT << B_I2C_BTN_OFFSET)
  63. #if BUTTON_EXISTS(ENC) // The pause/stop/restart button is connected to BTN_ENC when used
  64. #define B_ST (EN_C) // Map the pause/stop/resume button into its normalized functional name
  65. #define BUTTON_CLICK() (buttons & (B_MI|B_RI|B_ST)) // Pause/stop also acts as click until a proper pause/stop is implemented.
  66. #else
  67. #define BUTTON_CLICK() (buttons & (B_MI|B_RI))
  68. #endif
  69. // I2C buttons take too long to read inside an interrupt context and so we read them during lcd_update
  70. #elif ENABLED(LCD_I2C_PANELOLU2)
  71. #if !BUTTON_EXISTS(ENC) // Use I2C if not directly connected to a pin
  72. #define B_I2C_BTN_OFFSET 3 // (the first three bit positions reserved for EN_A, EN_B, EN_C)
  73. #define B_MI (PANELOLU2_ENCODER_C << B_I2C_BTN_OFFSET) // requires LiquidTWI2 library v1.2.3 or later
  74. #define BUTTON_CLICK() (buttons & B_MI)
  75. #endif
  76. #endif
  77. #else
  78. #undef BUTTON_EXISTS
  79. #define BUTTON_EXISTS(...) false
  80. // Dummy button, never pressed
  81. #define _BUTTON_PRESSED(BN) false
  82. // Shift register bits correspond to buttons:
  83. #define BL_LE 7 // Left
  84. #define BL_UP 6 // Up
  85. #define BL_MI 5 // Middle
  86. #define BL_DW 4 // Down
  87. #define BL_RI 3 // Right
  88. #define BL_ST 2 // Red Button
  89. #define B_LE _BV(BL_LE)
  90. #define B_UP _BV(BL_UP)
  91. #define B_MI _BV(BL_MI)
  92. #define B_DW _BV(BL_DW)
  93. #define B_RI _BV(BL_RI)
  94. #define B_ST _BV(BL_ST)
  95. #ifndef BUTTON_CLICK
  96. #if EN_C
  97. #define BUTTON_CLICK() (buttons & (B_MI|B_ST))
  98. #endif
  99. #endif
  100. #endif
  101. #if IS_RRW_KEYPAD
  102. #define BTN_OFFSET 0 // Bit offset into buttons for shift register values
  103. #define BLEN_KEYPAD_F3 0
  104. #define BLEN_KEYPAD_F2 1
  105. #define BLEN_KEYPAD_F1 2
  106. #define BLEN_KEYPAD_DOWN 3
  107. #define BLEN_KEYPAD_RIGHT 4
  108. #define BLEN_KEYPAD_MIDDLE 5
  109. #define BLEN_KEYPAD_UP 6
  110. #define BLEN_KEYPAD_LEFT 7
  111. #define EN_KEYPAD_F1 _BV(BTN_OFFSET + BLEN_KEYPAD_F1)
  112. #define EN_KEYPAD_F2 _BV(BTN_OFFSET + BLEN_KEYPAD_F2)
  113. #define EN_KEYPAD_F3 _BV(BTN_OFFSET + BLEN_KEYPAD_F3)
  114. #define EN_KEYPAD_DOWN _BV(BTN_OFFSET + BLEN_KEYPAD_DOWN)
  115. #define EN_KEYPAD_RIGHT _BV(BTN_OFFSET + BLEN_KEYPAD_RIGHT)
  116. #define EN_KEYPAD_MIDDLE _BV(BTN_OFFSET + BLEN_KEYPAD_MIDDLE)
  117. #define EN_KEYPAD_UP _BV(BTN_OFFSET + BLEN_KEYPAD_UP)
  118. #define EN_KEYPAD_LEFT _BV(BTN_OFFSET + BLEN_KEYPAD_LEFT)
  119. #define RRK(B) (keypad_buttons & (B))
  120. #ifdef EN_C
  121. #define BUTTON_CLICK() ((buttons & EN_C) || RRK(EN_KEYPAD_MIDDLE))
  122. #else
  123. #define BUTTON_CLICK() RRK(EN_KEYPAD_MIDDLE)
  124. #endif
  125. #endif
  126. #ifndef EN_A
  127. #define EN_A 0
  128. #endif
  129. #ifndef EN_B
  130. #define EN_B 0
  131. #endif
  132. #ifndef EN_C
  133. #define EN_C 0
  134. #endif
  135. #if BUTTON_EXISTS(BACK) || EITHER(HAS_TOUCH_BUTTONS, IS_TFTGLCD_PANEL)
  136. #define BLEN_D 3
  137. #define EN_D _BV(BLEN_D)
  138. #else
  139. #define EN_D 0
  140. #endif
  141. #define BUTTON_PRESSED(BN) (_BUTTON_PRESSED_##BN)
  142. #if BUTTON_EXISTS(EN1)
  143. #define _BUTTON_PRESSED_EN1 _BUTTON_PRESSED(EN1)
  144. #else
  145. #define _BUTTON_PRESSED_EN1 false
  146. #endif
  147. #if BUTTON_EXISTS(EN2)
  148. #define _BUTTON_PRESSED_EN2 _BUTTON_PRESSED(EN2)
  149. #else
  150. #define _BUTTON_PRESSED_EN2 false
  151. #endif
  152. #if BUTTON_EXISTS(ENC_EN)
  153. #define _BUTTON_PRESSED_ENC_EN _BUTTON_PRESSED(ENC_EN)
  154. #else
  155. #define _BUTTON_PRESSED_ENC_EN false
  156. #endif
  157. #if BUTTON_EXISTS(ENC)
  158. #define _BUTTON_PRESSED_ENC _BUTTON_PRESSED(ENC)
  159. #else
  160. #define _BUTTON_PRESSED_ENC false
  161. #endif
  162. #if BUTTON_EXISTS(UP)
  163. #define _BUTTON_PRESSED_UP _BUTTON_PRESSED(UP)
  164. #else
  165. #define _BUTTON_PRESSED_UP false
  166. #endif
  167. #if BUTTON_EXISTS(DOWN)
  168. #define _BUTTON_PRESSED_DOWN _BUTTON_PRESSED(DOWN)
  169. #else
  170. #define _BUTTON_PRESSED_DOWN false
  171. #endif
  172. #if BUTTON_EXISTS(LEFT)
  173. #define _BUTTON_PRESSED_LEFT _BUTTON_PRESSED(LEFT)
  174. #else
  175. #define _BUTTON_PRESSED_LEFT false
  176. #endif
  177. #if BUTTON_EXISTS(RIGHT)
  178. #define _BUTTON_PRESSED_RIGHT _BUTTON_PRESSED(RIGHT)
  179. #else
  180. #define _BUTTON_PRESSED_RIGHT false
  181. #endif
  182. #if BUTTON_EXISTS(BACK)
  183. #define _BUTTON_PRESSED_BACK _BUTTON_PRESSED(BACK)
  184. #else
  185. #define _BUTTON_PRESSED_BACK false
  186. #endif
  187. #ifndef BUTTON_CLICK
  188. #if EN_C > 0
  189. #define BUTTON_CLICK() (buttons & EN_C)
  190. #else
  191. #define BUTTON_CLICK() false
  192. #endif
  193. #endif
  194. #if EN_D > 0
  195. #define LCD_BACK_CLICKED() (buttons & EN_D)
  196. #else
  197. #define LCD_BACK_CLICKED() false
  198. #endif