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

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