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.

touch_buttons.cpp 4.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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. #include "../../inc/MarlinConfig.h"
  23. #if HAS_TOUCH_BUTTONS
  24. #include "touch_buttons.h"
  25. #include "../scaled_tft.h"
  26. #if ENABLED(TFT_TOUCH_DEVICE_GT911)
  27. #include HAL_PATH(../../HAL, tft/gt911.h)
  28. GT911 touchIO;
  29. #elif ENABLED(TFT_TOUCH_DEVICE_XPT2046)
  30. #include HAL_PATH(../../HAL, tft/xpt2046.h)
  31. XPT2046 touchIO;
  32. #else
  33. #error "Unknown Touch Screen Type."
  34. #endif
  35. #if ENABLED(TOUCH_SCREEN_CALIBRATION)
  36. #include "../tft_io/touch_calibration.h"
  37. #endif
  38. #if HAS_TOUCH_SLEEP
  39. millis_t TouchButtons::next_sleep_ms;
  40. #endif
  41. #include "../buttons.h" // For EN_C bit mask
  42. #include "../marlinui.h" // For ui.refresh
  43. #include "../tft_io/tft_io.h"
  44. #define DOGM_AREA_LEFT TFT_PIXEL_OFFSET_X
  45. #define DOGM_AREA_TOP TFT_PIXEL_OFFSET_Y
  46. #define DOGM_AREA_WIDTH (GRAPHICAL_TFT_UPSCALE) * (LCD_PIXEL_WIDTH)
  47. #define DOGM_AREA_HEIGHT (GRAPHICAL_TFT_UPSCALE) * (LCD_PIXEL_HEIGHT)
  48. #define BUTTON_AREA_TOP BUTTON_Y_LO
  49. #define BUTTON_AREA_BOT BUTTON_Y_HI
  50. TouchButtons touchBt;
  51. void TouchButtons::init() {
  52. touchIO.Init();
  53. TERN_(HAS_TOUCH_SLEEP, next_sleep_ms = millis() + SEC_TO_MS(ui.sleep_timeout_minutes * 60));
  54. }
  55. uint8_t TouchButtons::read_buttons() {
  56. #ifdef HAS_WIRED_LCD
  57. int16_t x, y;
  58. #if ENABLED(TFT_TOUCH_DEVICE_XPT2046)
  59. const bool is_touched = (TERN(TOUCH_SCREEN_CALIBRATION, touch_calibration.calibration.orientation, TOUCH_ORIENTATION) == TOUCH_PORTRAIT ? touchIO.getRawPoint(&y, &x) : touchIO.getRawPoint(&x, &y));
  60. #if HAS_TOUCH_SLEEP
  61. if (is_touched)
  62. wakeUp();
  63. else if (!isSleeping() && ELAPSED(millis(), next_sleep_ms) && ui.on_status_screen())
  64. sleepTimeout();
  65. #endif
  66. if (!is_touched) return 0;
  67. #if ENABLED(TOUCH_SCREEN_CALIBRATION)
  68. const calibrationState state = touch_calibration.get_calibration_state();
  69. if (WITHIN(state, CALIBRATION_TOP_LEFT, CALIBRATION_BOTTOM_RIGHT)) {
  70. if (touch_calibration.handleTouch(x, y)) ui.refresh();
  71. return 0;
  72. }
  73. x = int16_t((int32_t(x) * touch_calibration.calibration.x) >> 16) + touch_calibration.calibration.offset_x;
  74. y = int16_t((int32_t(y) * touch_calibration.calibration.y) >> 16) + touch_calibration.calibration.offset_y;
  75. #else
  76. x = uint16_t((uint32_t(x) * TOUCH_CALIBRATION_X) >> 16) + TOUCH_OFFSET_X;
  77. y = uint16_t((uint32_t(y) * TOUCH_CALIBRATION_Y) >> 16) + TOUCH_OFFSET_Y;
  78. #endif
  79. #elif ENABLED(TFT_TOUCH_DEVICE_GT911)
  80. bool is_touched = (TOUCH_ORIENTATION == TOUCH_PORTRAIT ? touchIO.getPoint(&y, &x) : touchIO.getPoint(&x, &y));
  81. if (!is_touched) return 0;
  82. #endif
  83. // Touch within the button area simulates an encoder button
  84. if (y > BUTTON_AREA_TOP && y < BUTTON_AREA_BOT)
  85. return WITHIN(x, BUTTOND_X_LO, BUTTOND_X_HI) ? EN_D
  86. : WITHIN(x, BUTTONA_X_LO, BUTTONA_X_HI) ? EN_A
  87. : WITHIN(x, BUTTONB_X_LO, BUTTONB_X_HI) ? EN_B
  88. : WITHIN(x, BUTTONC_X_LO, BUTTONC_X_HI) ? EN_C
  89. : 0;
  90. if ( !WITHIN(x, DOGM_AREA_LEFT, DOGM_AREA_LEFT + DOGM_AREA_WIDTH)
  91. || !WITHIN(y, DOGM_AREA_TOP, DOGM_AREA_TOP + DOGM_AREA_HEIGHT)
  92. ) return 0;
  93. // Column and row above BUTTON_AREA_TOP
  94. int8_t col = (x - (DOGM_AREA_LEFT)) * (LCD_WIDTH) / (DOGM_AREA_WIDTH),
  95. row = (y - (DOGM_AREA_TOP)) * (LCD_HEIGHT) / (DOGM_AREA_HEIGHT);
  96. // Send the touch to the UI (which will simulate the encoder wheel)
  97. MarlinUI::screen_click(row, col, x, y);
  98. #endif
  99. return 0;
  100. }
  101. #if HAS_TOUCH_SLEEP
  102. void TouchButtons::sleepTimeout() {
  103. #if HAS_LCD_BRIGHTNESS
  104. ui.set_brightness(0);
  105. #elif PIN_EXISTS(TFT_BACKLIGHT)
  106. WRITE(TFT_BACKLIGHT_PIN, LOW);
  107. #endif
  108. next_sleep_ms = TSLP_SLEEPING;
  109. }
  110. void TouchButtons::wakeUp() {
  111. if (isSleeping()) {
  112. #if HAS_LCD_BRIGHTNESS
  113. ui.set_brightness(ui.brightness);
  114. #elif PIN_EXISTS(TFT_BACKLIGHT)
  115. WRITE(TFT_BACKLIGHT_PIN, HIGH);
  116. #endif
  117. }
  118. next_sleep_ms = millis() + SEC_TO_MS(ui.sleep_timeout_minutes * 60);
  119. }
  120. #endif // HAS_TOUCH_SLEEP
  121. #endif // HAS_TOUCH_BUTTONS