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 2.6KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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/marlinui.h" // For EN_C bit mask
  26. #define DOGM_AREA_LEFT TFT_PIXEL_OFFSET_X
  27. #define DOGM_AREA_TOP TFT_PIXEL_OFFSET_Y
  28. #define DOGM_AREA_WIDTH (GRAPHICAL_TFT_UPSCALE) * (LCD_PIXEL_WIDTH)
  29. #define DOGM_AREA_HEIGHT (GRAPHICAL_TFT_UPSCALE) * (LCD_PIXEL_HEIGHT)
  30. #define BUTTON_AREA_TOP BUTTON_Y_LO
  31. #define BUTTON_AREA_BOT BUTTON_Y_HI
  32. TouchButtons touch;
  33. void TouchButtons::init() { touchIO.Init(); }
  34. uint8_t TouchButtons::read_buttons() {
  35. #ifdef HAS_WIRED_LCD
  36. int16_t x, y;
  37. if (!touchIO.getRawPoint(&x, &y)) return 0;
  38. x = uint16_t((uint32_t(x) * XPT2046_X_CALIBRATION) >> 16) + XPT2046_X_OFFSET;
  39. y = uint16_t((uint32_t(y) * XPT2046_Y_CALIBRATION) >> 16) + XPT2046_Y_OFFSET;
  40. #if (TFT_ROTATION & TFT_ROTATE_180)
  41. x = TFT_WIDTH - x;
  42. y = TFT_HEIGHT - y;
  43. #endif
  44. // Touch within the button area simulates an encoder button
  45. if (y > BUTTON_AREA_TOP && y < BUTTON_AREA_BOT)
  46. return WITHIN(x, BUTTOND_X_LO, BUTTOND_X_HI) ? EN_D
  47. : WITHIN(x, BUTTONA_X_LO, BUTTONA_X_HI) ? EN_A
  48. : WITHIN(x, BUTTONB_X_LO, BUTTONB_X_HI) ? EN_B
  49. : WITHIN(x, BUTTONC_X_LO, BUTTONC_X_HI) ? EN_C
  50. : 0;
  51. if ( !WITHIN(x, DOGM_AREA_LEFT, DOGM_AREA_LEFT + DOGM_AREA_WIDTH)
  52. || !WITHIN(y, DOGM_AREA_TOP, DOGM_AREA_TOP + DOGM_AREA_HEIGHT)
  53. ) return 0;
  54. // Column and row above BUTTON_AREA_TOP
  55. int8_t col = (x - (DOGM_AREA_LEFT)) * (LCD_WIDTH) / (DOGM_AREA_WIDTH),
  56. row = (y - (DOGM_AREA_TOP)) * (LCD_HEIGHT) / (DOGM_AREA_HEIGHT);
  57. // Send the touch to the UI (which will simulate the encoder wheel)
  58. MarlinUI::screen_click(row, col, x, y);
  59. #endif
  60. return 0;
  61. }
  62. #endif // HAS_TOUCH_XPT2046