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.

tft_lvgl_configuration.cpp 20KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761
  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. /**
  23. * @file tft_lvgl_configuration.cpp
  24. * @date 2020-02-21
  25. */
  26. #include "../../../../inc/MarlinConfigPre.h"
  27. #if HAS_TFT_LVGL_UI
  28. #if ENABLED(TFT_LVGL_UI_SPI)
  29. #include "SPI_TFT.h"
  30. #endif
  31. #include "tft_lvgl_configuration.h"
  32. #include "draw_ready_print.h"
  33. #include "W25Qxx.h"
  34. #include "pic_manager.h"
  35. #include "mks_hardware_test.h"
  36. #include "draw_ui.h"
  37. #include <lvgl.h>
  38. #include "../../../../MarlinCore.h"
  39. #include "../../../../feature/touch/xpt2046.h"
  40. #if ENABLED(POWER_LOSS_RECOVERY)
  41. #include "../../../../feature/powerloss.h"
  42. #endif
  43. #include <SPI.h>
  44. #if HAS_SPI_FLASH_FONT
  45. extern void init_gb2312_font();
  46. #endif
  47. static lv_disp_buf_t disp_buf;
  48. #if ENABLED(SDSUPPORT)
  49. extern void UpdateAssets();
  50. #endif
  51. uint16_t DeviceCode = 0x9488;
  52. extern uint8_t sel_id;
  53. #define SetCs
  54. #define ClrCs
  55. #define HDP 799 // Horizontal Display Period
  56. #define HT 1000 // Horizontal Total
  57. #define HPS 51 // LLINE Pulse Start Position
  58. #define LPS 3 // Horizontal Display Period Start Position
  59. #define HPW 8 // LLINE Pulse Width
  60. #define VDP 479 // Vertical Display Period
  61. #define VT 530 // Vertical Total
  62. #define VPS 24 // LFRAME Pulse Start Position
  63. #define FPS 23 // Vertical Display Period Start Positio
  64. #define VPW 3 // LFRAME Pulse Width
  65. #define MAX_HZ_POSX HDP+1
  66. #define MAX_HZ_POSY VDP+1
  67. extern uint8_t gcode_preview_over, flash_preview_begin, default_preview_flg;
  68. void SysTick_Callback() {
  69. lv_tick_inc(1);
  70. print_time_count();
  71. }
  72. #if DISABLED(TFT_LVGL_UI_SPI)
  73. extern void LCD_IO_Init(uint8_t cs, uint8_t rs);
  74. extern void LCD_IO_WriteData(uint16_t RegValue);
  75. extern void LCD_IO_WriteReg(uint16_t Reg);
  76. extern void LCD_IO_WriteMultiple(uint16_t color, uint32_t count);
  77. void tft_set_cursor(uint16_t x, uint16_t y) {
  78. LCD_IO_WriteReg(0x002A);
  79. LCD_IO_WriteData(x >> 8);
  80. LCD_IO_WriteData(x & 0x00FF);
  81. LCD_IO_WriteData(x >> 8);
  82. LCD_IO_WriteData(x & 0x00FF);
  83. //ILI9488_WriteData(0x01);
  84. //ILI9488_WriteData(0xDF);
  85. LCD_IO_WriteReg(0x002B);
  86. LCD_IO_WriteData(y >> 8);
  87. LCD_IO_WriteData(y & 0x00FF);
  88. LCD_IO_WriteData(y >> 8);
  89. LCD_IO_WriteData(y & 0x00FF);
  90. //ILI9488_WriteData(0x01);
  91. //ILI9488_WriteData(0x3F);
  92. }
  93. void LCD_WriteRAM_Prepare(void) {
  94. #if 0
  95. if ((DeviceCode == 0x9325) || (DeviceCode == 0x9328) || (DeviceCode == 0x8989)) {
  96. ClrCs
  97. LCD->LCD_REG = R34;
  98. SetCs
  99. }
  100. else {
  101. LCD_WrtReg(0x002C);
  102. }
  103. #else
  104. LCD_IO_WriteReg(0x002C);
  105. #endif
  106. }
  107. void tft_set_point(uint16_t x, uint16_t y, uint16_t point) {
  108. //if (DeviceCode == 0x9488) {
  109. if ((x > 480) || (y > 320)) return;
  110. //}
  111. //**if ( (x>320)||(y>240) ) return;
  112. tft_set_cursor(x, y);
  113. LCD_WriteRAM_Prepare();
  114. //LCD_WriteRAM(point);
  115. LCD_IO_WriteData(point);
  116. }
  117. void LCD_WriteReg(uint16_t LCD_Reg, uint16_t LCD_RegValue) {
  118. /* Write 16-bit Index, then Write Reg */
  119. ClrCs
  120. LCD_IO_WriteReg(LCD_Reg);
  121. /* Write 16-bit Reg */
  122. LCD_IO_WriteData(LCD_RegValue);
  123. SetCs
  124. }
  125. void ili9320_SetWindows(uint16_t StartX, uint16_t StartY, uint16_t width, uint16_t heigh) {
  126. uint16_t s_h, s_l, e_h, e_l;
  127. uint16_t xEnd, yEnd;
  128. xEnd = StartX + width;
  129. yEnd = StartY + heigh - 1;
  130. if (DeviceCode == 0x8989) {
  131. /*LCD_WriteReg(0x0044, (StartX & 0xFF) | (xEnd << 8));
  132. LCD_WriteReg(0x0045, StartY);
  133. LCD_WriteReg(0x0046, yEnd);*/
  134. LCD_WriteReg(0x0044, (StartY & 0xFF) | (yEnd << 8));
  135. LCD_WriteReg(0x0045, StartX);
  136. LCD_WriteReg(0x0046, xEnd);
  137. }
  138. else if (DeviceCode == 0x9488) {
  139. s_h = (StartX >> 8) & 0x00ff;
  140. s_l = StartX & 0x00ff;
  141. e_h = ((StartX + width - 1) >> 8) & 0x00ff;
  142. e_l = (StartX + width - 1) & 0x00ff;
  143. LCD_IO_WriteReg(0x002A);
  144. LCD_IO_WriteData(s_h);
  145. LCD_IO_WriteData(s_l);
  146. LCD_IO_WriteData(e_h);
  147. LCD_IO_WriteData(e_l);
  148. s_h = (StartY >> 8) & 0x00ff;
  149. s_l = StartY & 0x00ff;
  150. e_h = ((StartY + heigh - 1) >> 8) & 0x00ff;
  151. e_l = (StartY + heigh - 1) & 0x00ff;
  152. LCD_IO_WriteReg(0x002B);
  153. LCD_IO_WriteData(s_h);
  154. LCD_IO_WriteData(s_l);
  155. LCD_IO_WriteData(e_h);
  156. LCD_IO_WriteData(e_l);
  157. }
  158. else if ((DeviceCode == 0x9325) || (DeviceCode == 0x9328) || (DeviceCode == 0x1505)) {
  159. /* LCD_WriteReg(0x0050, StartX);
  160. LCD_WriteReg(0x0052, StartY);
  161. LCD_WriteReg(0x0051, xEnd);
  162. LCD_WriteReg(0x0053, yEnd);*/
  163. LCD_WriteReg(0x0050, StartY); //Specify the start/end positions of the window address in the horizontal direction by an address unit
  164. LCD_WriteReg(0x0051, yEnd); //Specify the start positions of the window address in the vertical direction by an address unit
  165. LCD_WriteReg(0x0052, 320 - xEnd);
  166. LCD_WriteReg(0x0053, 320 - StartX - 1); //Specify the end positions of the window address in the vertical direction by an address unit
  167. }
  168. else {
  169. s_h = (StartX >> 8) & 0xFF;
  170. s_l = StartX & 0xFF;
  171. e_h = ((StartX + width - 1) >> 8) & 0xFF;
  172. e_l = (StartX + width - 1) & 0xFF;
  173. LCD_IO_WriteReg(0x2A);
  174. LCD_IO_WriteData(s_h);
  175. LCD_IO_WriteData(s_l);
  176. LCD_IO_WriteData(e_h);
  177. LCD_IO_WriteData(e_l);
  178. s_h = (StartY >> 8) & 0xFF;
  179. s_l = StartY & 0xFF;
  180. e_h = ((StartY + heigh - 1) >> 8) & 0xFF;
  181. e_l = (StartY + heigh - 1) & 0xFF;
  182. LCD_IO_WriteReg(0x2B);
  183. LCD_IO_WriteData(s_h);
  184. LCD_IO_WriteData(s_l);
  185. LCD_IO_WriteData(e_h);
  186. LCD_IO_WriteData(e_l);
  187. }
  188. }
  189. void LCD_Clear(uint16_t Color) {
  190. uint32_t index = 0;
  191. unsigned int count;
  192. if (DeviceCode == 0x9488) {
  193. tft_set_cursor(0, 0);
  194. ili9320_SetWindows(0, 0, 480, 320);
  195. LCD_WriteRAM_Prepare();
  196. #ifdef LCD_USE_DMA_FSMC
  197. LCD_IO_WriteMultiple(Color, LCD_FULL_PIXEL_WIDTH * LCD_FULL_PIXEL_HEIGHT);
  198. #else
  199. //index = (160*480);
  200. for (index = 0; index < 320 * 480; index++)
  201. LCD_IO_WriteData(Color);
  202. #endif
  203. //LCD_IO_WriteMultiple(Color, (480*320));
  204. //while(index --) LCD_IO_WriteData(Color);
  205. }
  206. else if (DeviceCode == 0x5761) {
  207. LCD_IO_WriteReg(0x002a);
  208. LCD_IO_WriteData(0);
  209. LCD_IO_WriteData(0);
  210. LCD_IO_WriteData(HDP >> 8);
  211. LCD_IO_WriteData(HDP & 0x00ff);
  212. LCD_IO_WriteReg(0x002b);
  213. LCD_IO_WriteData(0);
  214. LCD_IO_WriteData(0);
  215. LCD_IO_WriteData(VDP >> 8);
  216. LCD_IO_WriteData(VDP & 0x00ff);
  217. LCD_IO_WriteReg(0x002c);
  218. LCD_IO_WriteReg(0x002c);
  219. for (count = 0; count < (HDP + 1) * (VDP + 1); count++)
  220. LCD_IO_WriteData(Color);
  221. }
  222. else {
  223. tft_set_cursor(0, 0);
  224. LCD_WriteRAM_Prepare(); /* Prepare to write GRAM */
  225. for (index = 0; index < 76800; index++)
  226. LCD_IO_WriteData(Color);
  227. }
  228. }
  229. extern uint16_t ILI9488_ReadRAM();
  230. void init_tft() {
  231. uint16_t i;
  232. //************* Start Initial Sequence **********//
  233. //start lcd pins and dma
  234. #if PIN_EXISTS(LCD_BACKLIGHT)
  235. OUT_WRITE(LCD_BACKLIGHT_PIN, DISABLED(DELAYED_BACKLIGHT_INIT)); // Illuminate after reset or right away
  236. #endif
  237. #if PIN_EXISTS(LCD_RESET)
  238. // Perform a clean hardware reset with needed delays
  239. OUT_WRITE(LCD_RESET_PIN, LOW);
  240. _delay_ms(5);
  241. WRITE(LCD_RESET_PIN, HIGH);
  242. _delay_ms(5);
  243. #endif
  244. #if PIN_EXISTS(LCD_BACKLIGHT) && ENABLED(DELAYED_BACKLIGHT_INIT)
  245. WRITE(LCD_BACKLIGHT_PIN, HIGH);
  246. #endif
  247. TERN_(HAS_LCD_CONTRAST, refresh_contrast());
  248. #ifdef LCD_USE_DMA_FSMC
  249. dma_init(FSMC_DMA_DEV);
  250. dma_disable(FSMC_DMA_DEV, FSMC_DMA_CHANNEL);
  251. dma_set_priority(FSMC_DMA_DEV, FSMC_DMA_CHANNEL, DMA_PRIORITY_MEDIUM);
  252. #endif
  253. LCD_IO_Init(FSMC_CS_PIN, FSMC_RS_PIN);
  254. _delay_ms(5);
  255. LCD_IO_WriteReg(0x00D3);
  256. DeviceCode = ILI9488_ReadRAM(); //dummy read
  257. DeviceCode = ILI9488_ReadRAM();
  258. DeviceCode = ILI9488_ReadRAM();
  259. DeviceCode <<= 8;
  260. DeviceCode |= ILI9488_ReadRAM();
  261. if (DeviceCode == 0x9488) {
  262. LCD_IO_WriteReg(0x00E0);
  263. LCD_IO_WriteData(0x0000);
  264. LCD_IO_WriteData(0x0007);
  265. LCD_IO_WriteData(0x000f);
  266. LCD_IO_WriteData(0x000D);
  267. LCD_IO_WriteData(0x001B);
  268. LCD_IO_WriteData(0x000A);
  269. LCD_IO_WriteData(0x003c);
  270. LCD_IO_WriteData(0x0078);
  271. LCD_IO_WriteData(0x004A);
  272. LCD_IO_WriteData(0x0007);
  273. LCD_IO_WriteData(0x000E);
  274. LCD_IO_WriteData(0x0009);
  275. LCD_IO_WriteData(0x001B);
  276. LCD_IO_WriteData(0x001e);
  277. LCD_IO_WriteData(0x000f);
  278. LCD_IO_WriteReg(0x00E1);
  279. LCD_IO_WriteData(0x0000);
  280. LCD_IO_WriteData(0x0022);
  281. LCD_IO_WriteData(0x0024);
  282. LCD_IO_WriteData(0x0006);
  283. LCD_IO_WriteData(0x0012);
  284. LCD_IO_WriteData(0x0007);
  285. LCD_IO_WriteData(0x0036);
  286. LCD_IO_WriteData(0x0047);
  287. LCD_IO_WriteData(0x0047);
  288. LCD_IO_WriteData(0x0006);
  289. LCD_IO_WriteData(0x000a);
  290. LCD_IO_WriteData(0x0007);
  291. LCD_IO_WriteData(0x0030);
  292. LCD_IO_WriteData(0x0037);
  293. LCD_IO_WriteData(0x000f);
  294. LCD_IO_WriteReg(0x00C0);
  295. LCD_IO_WriteData(0x0010);
  296. LCD_IO_WriteData(0x0010);
  297. LCD_IO_WriteReg(0x00C1);
  298. LCD_IO_WriteData(0x0041);
  299. LCD_IO_WriteReg(0x00C5);
  300. LCD_IO_WriteData(0x0000);
  301. LCD_IO_WriteData(0x0022);
  302. LCD_IO_WriteData(0x0080);
  303. LCD_IO_WriteReg(0x0036);
  304. //ILI9488_WriteData(0x0068);
  305. //if (gCfgItems.overturn_180 != 0xEE) {
  306. LCD_IO_WriteData(0x0068);
  307. //}
  308. //else {
  309. //ILI9488_WriteData(0x00A8);
  310. //}
  311. LCD_IO_WriteReg(0x003A); //Interface Mode Control
  312. LCD_IO_WriteData(0x0055);
  313. LCD_IO_WriteReg(0x00B0); //Interface Mode Control
  314. LCD_IO_WriteData(0x0000);
  315. LCD_IO_WriteReg(0x00B1); //Frame rate 70HZ
  316. LCD_IO_WriteData(0x00B0);
  317. LCD_IO_WriteData(0x0011);
  318. LCD_IO_WriteReg(0x00B4);
  319. LCD_IO_WriteData(0x0002);
  320. LCD_IO_WriteReg(0x00B6); //RGB/MCU Interface Control
  321. LCD_IO_WriteData(0x0002);
  322. LCD_IO_WriteData(0x0042);
  323. LCD_IO_WriteReg(0x00B7);
  324. LCD_IO_WriteData(0x00C6);
  325. //WriteComm(0xBE);
  326. //WriteData(0x00);
  327. //WriteData(0x04);
  328. LCD_IO_WriteReg(0x00E9);
  329. LCD_IO_WriteData(0x0000);
  330. LCD_IO_WriteReg(0x00F7);
  331. LCD_IO_WriteData(0x00A9);
  332. LCD_IO_WriteData(0x0051);
  333. LCD_IO_WriteData(0x002C);
  334. LCD_IO_WriteData(0x0082);
  335. LCD_IO_WriteReg(0x0011);
  336. for (i = 0; i < 65535; i++);
  337. LCD_IO_WriteReg(0x0029);
  338. ili9320_SetWindows(0, 0, 480, 320);
  339. LCD_Clear(0x0000);
  340. OUT_WRITE(LCD_BACKLIGHT_PIN, HIGH);
  341. }
  342. }
  343. #endif // !TFT_LVGL_UI_SPI
  344. extern uint8_t bmp_public_buf[17 * 1024];
  345. void tft_lvgl_init() {
  346. //uint16_t test_id=0;
  347. W25QXX.init(SPI_QUARTER_SPEED);
  348. //test_id=W25QXX.W25QXX_ReadID();
  349. gCfgItems_init();
  350. ui_cfg_init();
  351. disp_language_init();
  352. //init tft first!
  353. #if ENABLED(TFT_LVGL_UI_SPI)
  354. SPI_TFT.spi_init(SPI_FULL_SPEED);
  355. SPI_TFT.LCD_init();
  356. #else
  357. init_tft();
  358. #endif
  359. #if ENABLED(SDSUPPORT)
  360. UpdateAssets();
  361. #endif
  362. mks_test_get();
  363. //spi_flash_read_test();
  364. TERN_(TOUCH_BUTTONS, touch.init());
  365. lv_init();
  366. lv_disp_buf_init(&disp_buf, bmp_public_buf, NULL, LV_HOR_RES_MAX * 18); /*Initialize the display buffer*/
  367. lv_disp_drv_t disp_drv; /*Descriptor of a display driver*/
  368. lv_disp_drv_init(&disp_drv); /*Basic initialization*/
  369. disp_drv.flush_cb = my_disp_flush; /*Set your driver function*/
  370. disp_drv.buffer = &disp_buf; /*Assign the buffer to the display*/
  371. lv_disp_drv_register(&disp_drv); /*Finally register the driver*/
  372. lv_indev_drv_t indev_drv;
  373. lv_indev_drv_init(&indev_drv); /*Descriptor of a input device driver*/
  374. indev_drv.type = LV_INDEV_TYPE_POINTER; /*Touch pad is a pointer-like device*/
  375. indev_drv.read_cb = my_touchpad_read; /*Set your driver function*/
  376. lv_indev_drv_register(&indev_drv); /*Finally register the driver*/
  377. systick_attach_callback(SysTick_Callback);
  378. #if HAS_SPI_FLASH_FONT
  379. init_gb2312_font();
  380. #endif
  381. tft_style_init();
  382. filament_pin_setup();
  383. #if ENABLED(POWER_LOSS_RECOVERY)
  384. if (recovery.valid()) {
  385. if (gCfgItems.from_flash_pic == 1)
  386. flash_preview_begin = 1;
  387. else
  388. default_preview_flg = 1;
  389. uiCfg.print_state = REPRINTING;
  390. ZERO(public_buf_m);
  391. strncpy(public_buf_m, recovery.info.sd_filename, sizeof(public_buf_m));
  392. card.printLongPath(public_buf_m);
  393. strncpy(list_file.long_name[sel_id], card.longFilename, sizeof(list_file.long_name[sel_id]));
  394. lv_draw_printing();
  395. }
  396. else
  397. #endif
  398. lv_draw_ready_print();
  399. if (mks_test_flag == 0x1E)
  400. mks_gpio_test();
  401. }
  402. void my_disp_flush(lv_disp_drv_t * disp, const lv_area_t * area, lv_color_t * color_p) {
  403. #if ENABLED(TFT_LVGL_UI_SPI)
  404. uint16_t i, width, height;
  405. uint16_t clr_temp;
  406. uint8_t tbuf[480 * 2];
  407. SPI_TFT.spi_init(SPI_FULL_SPEED);
  408. width = area->x2 - area->x1 + 1;
  409. height = area->y2 - area->y1 + 1;
  410. for (int j = 0; j < height; j++) {
  411. SPI_TFT.SetCursor(0, 0);
  412. SPI_TFT.SetWindows((uint16_t)area->x1, (uint16_t)area->y1 + j, width, 1);
  413. SPI_TFT.LCD_WriteRAM_Prepare();
  414. for (i = 0; i < width * 2;) {
  415. clr_temp = (uint16_t)(((uint16_t)color_p->ch.red << 11)
  416. | ((uint16_t)color_p->ch.green << 5)
  417. | ((uint16_t)color_p->ch.blue));
  418. tbuf[i] = clr_temp >> 8;
  419. tbuf[i + 1] = clr_temp;
  420. i += 2;
  421. color_p++;
  422. }
  423. SPI_TFT_CS_L;
  424. SPI_TFT_DC_H;
  425. SPI.dmaSend(tbuf, width * 2, true);
  426. SPI_TFT_CS_H;
  427. }
  428. lv_disp_flush_ready(disp); /* Indicate you are ready with the flushing*/
  429. W25QXX.init(SPI_QUARTER_SPEED);
  430. #else // !TFT_LVGL_UI_SPI
  431. #if 1
  432. uint16_t i, width, height;
  433. uint16_t clr_temp;
  434. width = area->x2 - area->x1 + 1;
  435. height = area->y2 - area->y1 + 1;
  436. ili9320_SetWindows((uint16_t)area->x1, (uint16_t)area->y1, width, height);
  437. LCD_WriteRAM_Prepare();
  438. for (i = 0; i < width * height - 2; i++) {
  439. clr_temp = (uint16_t)(((uint16_t)color_p->ch.red << 11)
  440. | ((uint16_t)color_p->ch.green << 5)
  441. | ((uint16_t)color_p->ch.blue));
  442. LCD_IO_WriteData(clr_temp);
  443. color_p++;
  444. }
  445. lv_disp_flush_ready(disp); /* Indicate you are ready with the flushing*/
  446. #endif
  447. #endif // !TFT_LVGL_UI_SPI
  448. }
  449. #define TICK_CYCLE 1
  450. static int32_t touch_time1 = 0;
  451. unsigned int getTickDiff(unsigned int curTick, unsigned int lastTick) {
  452. return TICK_CYCLE * (lastTick <= curTick ? (curTick - lastTick) : (0xFFFFFFFF - lastTick + curTick));
  453. }
  454. #if ENABLED(TFT_LVGL_UI_SPI)
  455. #ifndef USE_XPT2046
  456. #define USE_XPT2046 1
  457. #define XPT2046_XY_SWAP 1
  458. #define XPT2046_X_INV 1
  459. #define XPT2046_Y_INV 0
  460. #endif
  461. #if USE_XPT2046
  462. #define XPT2046_HOR_RES 480
  463. #define XPT2046_VER_RES 320
  464. #define XPT2046_X_MIN 201
  465. #define XPT2046_Y_MIN 164
  466. #define XPT2046_X_MAX 3919
  467. #define XPT2046_Y_MAX 3776
  468. #define XPT2046_AVG 4
  469. #define XPT2046_INV 1
  470. #endif
  471. #else
  472. #ifndef USE_XPT2046
  473. #define USE_XPT2046 1
  474. #ifndef XPT2046_XY_SWAP
  475. #define XPT2046_XY_SWAP 1
  476. #endif
  477. #ifndef XPT2046_X_INV
  478. #define XPT2046_X_INV 0
  479. #endif
  480. #ifndef XPT2046_Y_INV
  481. #define XPT2046_Y_INV 1
  482. #endif
  483. #endif
  484. #if USE_XPT2046
  485. #ifndef XPT2046_HOR_RES
  486. #define XPT2046_HOR_RES 480
  487. #endif
  488. #ifndef XPT2046_VER_RES
  489. #define XPT2046_VER_RES 320
  490. #endif
  491. #ifndef XPT2046_X_MIN
  492. #define XPT2046_X_MIN 201
  493. #endif
  494. #ifndef XPT2046_Y_MIN
  495. #define XPT2046_Y_MIN 164
  496. #endif
  497. #ifndef XPT2046_X_MAX
  498. #define XPT2046_X_MAX 3919
  499. #endif
  500. #ifndef XPT2046_Y_MAX
  501. #define XPT2046_Y_MAX 3776
  502. #endif
  503. #ifndef XPT2046_AVG
  504. #define XPT2046_AVG 4
  505. #endif
  506. #ifndef XPT2046_INV
  507. #define XPT2046_INV 0
  508. #endif
  509. #endif
  510. #endif
  511. static void xpt2046_corr(uint16_t *x, uint16_t *y) {
  512. #if XPT2046_XY_SWAP
  513. int16_t swap_tmp;
  514. swap_tmp = *x;
  515. *x = *y;
  516. *y = swap_tmp;
  517. #endif
  518. if ((*x) > XPT2046_X_MIN) (*x) -= XPT2046_X_MIN; else (*x) = 0;
  519. if ((*y) > XPT2046_Y_MIN) (*y) -= XPT2046_Y_MIN; else (*y) = 0;
  520. (*x) = uint32_t(uint32_t(*x) * XPT2046_HOR_RES) / (XPT2046_X_MAX - XPT2046_X_MIN);
  521. (*y) = uint32_t(uint32_t(*y) * XPT2046_VER_RES) / (XPT2046_Y_MAX - XPT2046_Y_MIN);
  522. #if XPT2046_X_INV
  523. (*x) = XPT2046_HOR_RES - (*x);
  524. #endif
  525. #if XPT2046_Y_INV
  526. (*y) = XPT2046_VER_RES - (*y);
  527. #endif
  528. }
  529. #define times 4
  530. #define CHX 0x90
  531. #define CHY 0xD0
  532. int SPI2_ReadWrite2Bytes(void) {
  533. #define SPI_READ_WRITE_BYTE(B) TERN(TFT_LVGL_UI_SPI, SPI_TFT.spi_read_write_byte, W25QXX.spi_flash_read_write_byte)(B)
  534. const uint16_t t1 = SPI_READ_WRITE_BYTE(0xFF),
  535. t2 = SPI_READ_WRITE_BYTE(0xFF);
  536. return (((t1 << 8) | t2) >> 3) & 0x0FFF;
  537. }
  538. uint16_t x_addata[times], y_addata[times];
  539. void XPT2046_Rd_Addata(uint16_t *X_Addata, uint16_t *Y_Addata) {
  540. uint16_t i, j, k;
  541. TERN(TFT_LVGL_UI_SPI, SPI_TFT.spi_init, W25QXX.init)(SPI_SPEED_6);
  542. for (i = 0; i < times; i++) {
  543. #if ENABLED(TFT_LVGL_UI_SPI)
  544. OUT_WRITE(TOUCH_CS_PIN, LOW);
  545. SPI_TFT.spi_read_write_byte(CHX);
  546. y_addata[i] = SPI2_ReadWrite2Bytes();
  547. WRITE(TOUCH_CS_PIN, HIGH);
  548. OUT_WRITE(TOUCH_CS_PIN, LOW);
  549. SPI_TFT.spi_read_write_byte(CHY);
  550. x_addata[i] = SPI2_ReadWrite2Bytes();
  551. WRITE(TOUCH_CS_PIN, HIGH);
  552. #else // #if ENABLED(TOUCH_BUTTONS)
  553. OUT_WRITE(TOUCH_CS_PIN, LOW);
  554. W25QXX.spi_flash_read_write_byte(CHX);
  555. y_addata[i] = SPI2_ReadWrite2Bytes();
  556. WRITE(TOUCH_CS_PIN, HIGH);
  557. OUT_WRITE(TOUCH_CS_PIN, LOW);
  558. W25QXX.spi_flash_read_write_byte(CHY);
  559. x_addata[i] = SPI2_ReadWrite2Bytes();
  560. WRITE(TOUCH_CS_PIN, HIGH);
  561. #endif
  562. }
  563. TERN(TFT_LVGL_UI_SPI,,W25QXX.init(SPI_QUARTER_SPEED));
  564. for (i = 0; i < times; i++)
  565. for (j = i + 1; j < times; j++)
  566. if (x_addata[j] > x_addata[i]) {
  567. k = x_addata[j];
  568. x_addata[j] = x_addata[i];
  569. x_addata[i] = k;
  570. }
  571. if (x_addata[times / 2 - 1] - x_addata[times / 2] > 50) {
  572. *X_Addata = *Y_Addata = 0;
  573. return;
  574. }
  575. *X_Addata = (x_addata[times / 2 - 1] + x_addata[times / 2]) / 2;
  576. for (i = 0; i < times; i++)
  577. for (j = i + 1; j < times; j++)
  578. if (y_addata[j] > y_addata[i]) {
  579. k = y_addata[j];
  580. y_addata[j] = y_addata[i];
  581. y_addata[i] = k;
  582. }
  583. if (y_addata[times / 2 - 1] - y_addata[times / 2] > 50) {
  584. *X_Addata = *Y_Addata = 0;
  585. return;
  586. }
  587. *Y_Addata = (y_addata[times / 2 - 1] + y_addata[times / 2]) / 2;
  588. }
  589. #define ADC_VALID_OFFSET 10
  590. uint8_t TOUCH_PressValid(uint16_t _usX, uint16_t _usY) {
  591. if ( (_usX <= ADC_VALID_OFFSET)
  592. || (_usY <= ADC_VALID_OFFSET)
  593. || (_usX >= 4095 - ADC_VALID_OFFSET)
  594. || (_usY >= 4095 - ADC_VALID_OFFSET)
  595. ) return 0;
  596. return 1;
  597. }
  598. static lv_coord_t last_x = 0, last_y = 0;
  599. bool my_touchpad_read(lv_indev_drv_t * indev_driver, lv_indev_data_t * data) {
  600. uint32_t tmpTime, diffTime = 0;
  601. tmpTime = millis();
  602. diffTime = getTickDiff(tmpTime, touch_time1);
  603. /*Save the state and save the pressed coordinate*/
  604. //data->state = TOUCH_PressValid(last_x, last_y) ? LV_INDEV_STATE_PR : LV_INDEV_STATE_REL;
  605. //if (data->state == LV_INDEV_STATE_PR) ADS7843_Rd_Addata((u16 *)&last_x, (u16 *)&last_y);
  606. //touchpad_get_xy(&last_x, &last_y);
  607. /*Save the pressed coordinates and the state*/
  608. if (diffTime > 10) {
  609. //use marlin touch code if enabled
  610. #if ENABLED(TOUCH_BUTTONS)
  611. touch.getTouchPoint(reinterpret_cast<uint16_t&>(last_x), reinterpret_cast<uint16_t&>(last_y));
  612. #else
  613. XPT2046_Rd_Addata((uint16_t *)&last_x, (uint16_t *)&last_y);
  614. #endif
  615. if (TOUCH_PressValid(last_x, last_y)) {
  616. data->state = LV_INDEV_STATE_PR;
  617. /* Set the coordinates (if released use the last pressed coordinates) */
  618. // SERIAL_ECHOLNPAIR("antes X: ", last_x, ", y: ", last_y);
  619. xpt2046_corr((uint16_t *)&last_x, (uint16_t *)&last_y);
  620. // SERIAL_ECHOLNPAIR("X: ", last_x, ", y: ", last_y);
  621. data->point.x = last_x;
  622. data->point.y = last_y;
  623. last_x = 0;
  624. last_y = 0;
  625. }
  626. else {
  627. data->state = LV_INDEV_STATE_REL;
  628. }
  629. touch_time1 = tmpTime;
  630. }
  631. return false; /*Return `false` because we are not buffering and no more data to read*/
  632. }
  633. #endif // HAS_TFT_LVGL_UI