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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /**
  2. * Marlin 3D Printer Firmware
  3. * Copyright (c) 2020 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  17. *
  18. */
  19. #include "../../inc/MarlinConfig.h"
  20. #if HAS_TOUCH_XPT2046
  21. #include "touch_buttons.h"
  22. #include "../scaled_tft.h"
  23. #include HAL_PATH(../../HAL, tft/xpt2046.h)
  24. XPT2046 touchIO;
  25. #include "../../lcd/ultralcd.h" // For EN_C bit mask
  26. /**
  27. * Draw and Touch processing
  28. *
  29. * LCD_PIXEL_WIDTH/HEIGHT (128x64) is the (emulated DOGM) Pixel Drawing resolution.
  30. * TOUCH_SENSOR_WIDTH/HEIGHT (320x240) is the Touch Area resolution.
  31. * TFT_WIDTH/HEIGHT (320x240 or 480x320) is the Actual (FSMC) Display resolution.
  32. *
  33. * - All native (u8g) drawing is done in LCD_PIXEL_* (128x64)
  34. * - The DOGM pixels are is upscaled 2-3x (as needed) for display.
  35. * - Touch coordinates use TOUCH_SENSOR_* resolution and are converted to
  36. * click and scroll-wheel events (emulating of a common DOGM display).
  37. *
  38. * TOUCH_SCREEN resolution exists to fit our calibration values. The original touch code was made
  39. * and originally calibrated for 320x240. If you decide to change the resolution of the touch code,
  40. * new calibration values will be needed.
  41. *
  42. * The Marlin menus are drawn scaled in the upper region of the screen. The bottom region (in a
  43. * fixed location in TOUCH_SCREEN* coordinate space) is used for 4 general-purpose buttons to
  44. * navigate and select menu items. Both regions are touchable.
  45. *
  46. * The Marlin screen touchable area starts at TFT_PIXEL_OFFSET_X/Y (translated to SCREEN_PCT_LEFT/TOP)
  47. * and spans LCD_PIXEL_WIDTH/HEIGHT (scaled to SCREEN_PCT_WIDTH/HEIGHT).
  48. */
  49. // Touch sensor resolution independent of display resolution
  50. #define TOUCH_SENSOR_WIDTH 320
  51. #define TOUCH_SENSOR_HEIGHT 240
  52. #define SCREEN_PCT_WIDE(X) ((X) * (TOUCH_SENSOR_WIDTH) / (TFT_WIDTH))
  53. #define SCREEN_PCT_HIGH(Y) ((Y) * (TOUCH_SENSOR_HEIGHT) / (TFT_HEIGHT))
  54. #define SCREEN_PCT_LEFT SCREEN_PCT_WIDE(TFT_PIXEL_OFFSET_X)
  55. #define SCREEN_PCT_TOP SCREEN_PCT_HIGH(TFT_PIXEL_OFFSET_Y)
  56. #define SCREEN_PCT_WIDTH SCREEN_PCT_WIDE((GRAPHICAL_TFT_UPSCALE) * (LCD_PIXEL_WIDTH))
  57. #define SCREEN_PCT_HEIGHT SCREEN_PCT_HIGH((GRAPHICAL_TFT_UPSCALE) * (LCD_PIXEL_HEIGHT))
  58. // Coordinates in terms of 240-unit-tall touch area
  59. #define BUTTON_AREA_TOP 175
  60. #define BUTTON_AREA_BOT 234
  61. TouchButtons touch;
  62. void TouchButtons::init() { touchIO.Init(); }
  63. uint8_t TouchButtons::read_buttons() {
  64. #ifdef HAS_WIRED_LCD
  65. int16_t x, y;
  66. if (!touchIO.getRawPoint(&x, &y)) return 0;
  67. x = uint16_t((uint32_t(x) * XPT2046_X_CALIBRATION) >> 16) + XPT2046_X_OFFSET;
  68. y = uint16_t((uint32_t(y) * XPT2046_Y_CALIBRATION) >> 16) + XPT2046_Y_OFFSET;
  69. #if (TFT_ROTATION & TFT_ROTATE_180)
  70. x = TOUCH_SENSOR_WIDTH - x;
  71. y = TOUCH_SENSOR_HEIGHT - y;
  72. #endif
  73. // Touch within the button area simulates an encoder button
  74. if (y > BUTTON_AREA_TOP && y < BUTTON_AREA_BOT)
  75. return WITHIN(x, 14, 77) ? EN_D
  76. : WITHIN(x, 90, 153) ? EN_A
  77. : WITHIN(x, 166, 229) ? EN_B
  78. : WITHIN(x, 242, 305) ? EN_C
  79. : 0;
  80. if ( !WITHIN(x, SCREEN_PCT_LEFT, SCREEN_PCT_LEFT + SCREEN_PCT_WIDTH)
  81. || !WITHIN(y, SCREEN_PCT_TOP, SCREEN_PCT_TOP + SCREEN_PCT_HEIGHT)
  82. ) return 0;
  83. // Column and row above BUTTON_AREA_TOP
  84. int8_t col = (x - (SCREEN_PCT_LEFT)) * (LCD_WIDTH) / (SCREEN_PCT_WIDTH),
  85. row = (y - (SCREEN_PCT_TOP)) * (LCD_HEIGHT) / (SCREEN_PCT_HEIGHT);
  86. // Send the touch to the UI (which will simulate the encoder wheel)
  87. MarlinUI::screen_click(row, col, x, y);
  88. #endif
  89. return 0;
  90. }
  91. #endif // HAS_TOUCH_XPT2046