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.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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(TOUCH_IDLE_SLEEP));
  54. }
  55. uint8_t TouchButtons::read_buttons() {
  56. #ifdef HAS_WIRED_LCD
  57. int16_t x, y;
  58. const bool is_touched = (TERN(TOUCH_SCREEN_CALIBRATION, touch_calibration.calibration.orientation, TOUCH_ORIENTATION) == TOUCH_PORTRAIT ? touchIO.getRawPoint(&y, &x) : touchIO.getRawPoint(&x, &y));
  59. #if HAS_TOUCH_SLEEP
  60. if (is_touched)
  61. wakeUp();
  62. else if (!isSleeping() && ELAPSED(millis(), next_sleep_ms) && ui.on_status_screen())
  63. sleepTimeout();
  64. #endif
  65. if (!is_touched) return 0;
  66. #if ENABLED(TOUCH_SCREEN_CALIBRATION)
  67. const calibrationState state = touch_calibration.get_calibration_state();
  68. if (WITHIN(state, CALIBRATION_TOP_LEFT, CALIBRATION_BOTTOM_RIGHT)) {
  69. if (touch_calibration.handleTouch(x, y)) ui.refresh();
  70. return 0;
  71. }
  72. x = int16_t((int32_t(x) * touch_calibration.calibration.x) >> 16) + touch_calibration.calibration.offset_x;
  73. y = int16_t((int32_t(y) * touch_calibration.calibration.y) >> 16) + touch_calibration.calibration.offset_y;
  74. #else
  75. x = uint16_t((uint32_t(x) * TOUCH_CALIBRATION_X) >> 16) + TOUCH_OFFSET_X;
  76. y = uint16_t((uint32_t(y) * TOUCH_CALIBRATION_Y) >> 16) + TOUCH_OFFSET_Y;
  77. #endif
  78. // Touch within the button area simulates an encoder button
  79. if (y > BUTTON_AREA_TOP && y < BUTTON_AREA_BOT)
  80. return WITHIN(x, BUTTOND_X_LO, BUTTOND_X_HI) ? EN_D
  81. : WITHIN(x, BUTTONA_X_LO, BUTTONA_X_HI) ? EN_A
  82. : WITHIN(x, BUTTONB_X_LO, BUTTONB_X_HI) ? EN_B
  83. : WITHIN(x, BUTTONC_X_LO, BUTTONC_X_HI) ? EN_C
  84. : 0;
  85. if ( !WITHIN(x, DOGM_AREA_LEFT, DOGM_AREA_LEFT + DOGM_AREA_WIDTH)
  86. || !WITHIN(y, DOGM_AREA_TOP, DOGM_AREA_TOP + DOGM_AREA_HEIGHT)
  87. ) return 0;
  88. // Column and row above BUTTON_AREA_TOP
  89. int8_t col = (x - (DOGM_AREA_LEFT)) * (LCD_WIDTH) / (DOGM_AREA_WIDTH),
  90. row = (y - (DOGM_AREA_TOP)) * (LCD_HEIGHT) / (DOGM_AREA_HEIGHT);
  91. // Send the touch to the UI (which will simulate the encoder wheel)
  92. MarlinUI::screen_click(row, col, x, y);
  93. #endif
  94. return 0;
  95. }
  96. #if HAS_TOUCH_SLEEP
  97. void TouchButtons::sleepTimeout() {
  98. #if HAS_LCD_BRIGHTNESS
  99. ui.set_brightness(0);
  100. #elif PIN_EXISTS(TFT_BACKLIGHT)
  101. WRITE(TFT_BACKLIGHT_PIN, LOW);
  102. #endif
  103. next_sleep_ms = TSLP_SLEEPING;
  104. }
  105. void TouchButtons::wakeUp() {
  106. if (isSleeping()) {
  107. #if HAS_LCD_BRIGHTNESS
  108. ui.set_brightness(ui.brightness);
  109. #elif PIN_EXISTS(TFT_BACKLIGHT)
  110. WRITE(TFT_BACKLIGHT_PIN, HIGH);
  111. #endif
  112. }
  113. next_sleep_ms = millis() + SEC_TO_MS(TOUCH_IDLE_SLEEP);
  114. }
  115. #endif // HAS_TOUCH_SLEEP
  116. #endif // HAS_TOUCH_BUTTONS