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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521
  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. #include "SPI_TFT.h"
  29. #include "tft_lvgl_configuration.h"
  30. #include "draw_ready_print.h"
  31. #include "pic_manager.h"
  32. #include "mks_hardware_test.h"
  33. #include "draw_ui.h"
  34. #include "SPIFlashStorage.h"
  35. #include <lvgl.h>
  36. #include "../../../../MarlinCore.h"
  37. #include "../../../../inc/MarlinConfig.h"
  38. #include HAL_PATH(../../HAL, tft/xpt2046.h)
  39. #include "../../../ultralcd.h"
  40. XPT2046 touch;
  41. #if ENABLED(POWER_LOSS_RECOVERY)
  42. #include "../../../../feature/powerloss.h"
  43. #endif
  44. #include <SPI.h>
  45. #ifndef TFT_WIDTH
  46. #define TFT_WIDTH 480
  47. #endif
  48. #ifndef TFT_HEIGHT
  49. #define TFT_HEIGHT 320
  50. #endif
  51. #if HAS_SPI_FLASH_FONT
  52. extern void init_gb2312_font();
  53. #endif
  54. static lv_disp_buf_t disp_buf;
  55. lv_group_t* g;
  56. #if ENABLED(SDSUPPORT)
  57. extern void UpdateAssets();
  58. #endif
  59. uint16_t DeviceCode = 0x9488;
  60. extern uint8_t sel_id;
  61. extern uint8_t gcode_preview_over, flash_preview_begin, default_preview_flg;
  62. uint8_t bmp_public_buf[17 * 1024];
  63. void SysTick_Callback() {
  64. lv_tick_inc(1);
  65. print_time_count();
  66. #if ENABLED(USE_WIFI_FUNCTION)
  67. if (tips_disp.timer == TIPS_TIMER_START) {
  68. tips_disp.timer_count++;
  69. }
  70. #endif
  71. if (uiCfg.filament_loading_time_flg == 1) {
  72. uiCfg.filament_loading_time_cnt++;
  73. uiCfg.filament_rate = (uint32_t)(((uiCfg.filament_loading_time_cnt / (uiCfg.filament_loading_time * 1000.0)) * 100.0) + 0.5);
  74. if (uiCfg.filament_loading_time_cnt >= (uiCfg.filament_loading_time * 1000)) {
  75. uiCfg.filament_loading_time_cnt = 0;
  76. uiCfg.filament_loading_time_flg = 0;
  77. uiCfg.filament_loading_completed = 1;
  78. }
  79. }
  80. if (uiCfg.filament_unloading_time_flg == 1) {
  81. uiCfg.filament_unloading_time_cnt++;
  82. uiCfg.filament_rate = (uint32_t)(((uiCfg.filament_unloading_time_cnt / (uiCfg.filament_unloading_time * 1000.0)) * 100.0) + 0.5);
  83. if (uiCfg.filament_unloading_time_cnt >= (uiCfg.filament_unloading_time * 1000)) {
  84. uiCfg.filament_unloading_time_cnt = 0;
  85. uiCfg.filament_unloading_time_flg = 0;
  86. uiCfg.filament_unloading_completed = 1;
  87. uiCfg.filament_rate = 100;
  88. }
  89. }
  90. }
  91. extern uint8_t bmp_public_buf[17 * 1024];
  92. void tft_lvgl_init() {
  93. //uint16_t test_id=0;
  94. W25QXX.init(SPI_QUARTER_SPEED);
  95. //test_id=W25QXX.W25QXX_ReadID();
  96. gCfgItems_init();
  97. ui_cfg_init();
  98. disp_language_init();
  99. //init tft first!
  100. SPI_TFT.spi_init(SPI_FULL_SPEED);
  101. SPI_TFT.LCD_init();
  102. //spi_flash_read_test();
  103. #if ENABLED(SDSUPPORT)
  104. UpdateAssets();
  105. #endif
  106. mks_test_get();
  107. touch.Init();
  108. lv_init();
  109. lv_disp_buf_init(&disp_buf, bmp_public_buf, NULL, LV_HOR_RES_MAX * 18); /*Initialize the display buffer*/
  110. lv_disp_drv_t disp_drv; /*Descriptor of a display driver*/
  111. lv_disp_drv_init(&disp_drv); /*Basic initialization*/
  112. disp_drv.flush_cb = my_disp_flush; /*Set your driver function*/
  113. disp_drv.buffer = &disp_buf; /*Assign the buffer to the display*/
  114. lv_disp_drv_register(&disp_drv); /*Finally register the driver*/
  115. lv_indev_drv_t indev_drv;
  116. lv_indev_drv_init(&indev_drv); /*Descriptor of a input device driver*/
  117. indev_drv.type = LV_INDEV_TYPE_POINTER; /*Touch pad is a pointer-like device*/
  118. indev_drv.read_cb = my_touchpad_read; /*Set your driver function*/
  119. lv_indev_drv_register(&indev_drv); /*Finally register the driver*/
  120. #if HAS_ROTARY_ENCODER
  121. g = lv_group_create();
  122. lv_indev_drv_t enc_drv;
  123. lv_indev_drv_init(&enc_drv);
  124. enc_drv.type = LV_INDEV_TYPE_ENCODER;
  125. enc_drv.read_cb = my_mousewheel_read;
  126. lv_indev_t * enc_indev = lv_indev_drv_register(&enc_drv);
  127. lv_indev_set_group(enc_indev, g);
  128. #endif
  129. lv_fs_drv_t spi_flash_drv;
  130. lv_fs_drv_init(&spi_flash_drv);
  131. spi_flash_drv.letter = 'F';
  132. spi_flash_drv.open_cb = spi_flash_open_cb;
  133. spi_flash_drv.close_cb = spi_flash_close_cb;
  134. spi_flash_drv.read_cb = spi_flash_read_cb;
  135. spi_flash_drv.seek_cb = spi_flash_seek_cb;
  136. spi_flash_drv.tell_cb = spi_flash_tell_cb;
  137. lv_fs_drv_register(&spi_flash_drv);
  138. lv_fs_drv_t sd_drv;
  139. lv_fs_drv_init(&sd_drv);
  140. sd_drv.letter = 'S';
  141. sd_drv.open_cb = sd_open_cb;
  142. sd_drv.close_cb = sd_close_cb;
  143. sd_drv.read_cb = sd_read_cb;
  144. sd_drv.seek_cb = sd_seek_cb;
  145. sd_drv.tell_cb = sd_tell_cb;
  146. lv_fs_drv_register(&sd_drv);
  147. systick_attach_callback(SysTick_Callback);
  148. #if HAS_SPI_FLASH_FONT
  149. init_gb2312_font();
  150. #endif
  151. tft_style_init();
  152. filament_pin_setup();
  153. lv_encoder_pin_init();
  154. #if ENABLED(POWER_LOSS_RECOVERY)
  155. recovery.load();
  156. if (recovery.valid()) {
  157. if (gCfgItems.from_flash_pic == 1)
  158. flash_preview_begin = 1;
  159. else
  160. default_preview_flg = 1;
  161. uiCfg.print_state = REPRINTING;
  162. ZERO(public_buf_m);
  163. strncpy(public_buf_m, recovery.info.sd_filename, sizeof(public_buf_m));
  164. card.printLongPath(public_buf_m);
  165. strncpy(list_file.long_name[sel_id], card.longFilename, sizeof(list_file.long_name[sel_id]));
  166. lv_draw_printing();
  167. }
  168. else
  169. #endif
  170. lv_draw_ready_print();
  171. if (mks_test_flag == 0x1E)
  172. mks_gpio_test();
  173. }
  174. void my_disp_flush(lv_disp_drv_t * disp, const lv_area_t * area, lv_color_t * color_p) {
  175. uint16_t i, width, height;
  176. width = area->x2 - area->x1 + 1;
  177. height = area->y2 - area->y1 + 1;
  178. SPI_TFT.setWindow((uint16_t)area->x1, (uint16_t)area->y1, width, height);
  179. for (i = 0; i < height; i++) {
  180. SPI_TFT.tftio.WriteSequence((uint16_t*)(color_p + width * i), width);
  181. }
  182. lv_disp_flush_ready(disp); /* Indicate you are ready with the flushing*/
  183. W25QXX.init(SPI_QUARTER_SPEED);
  184. }
  185. #define TICK_CYCLE 1
  186. unsigned int getTickDiff(unsigned int curTick, unsigned int lastTick) {
  187. return TICK_CYCLE * (lastTick <= curTick ? (curTick - lastTick) : (0xFFFFFFFF - lastTick + curTick));
  188. }
  189. static bool get_point(int16_t *x, int16_t *y) {
  190. bool is_touched = touch.getRawPoint(x, y);
  191. if (is_touched) {
  192. *x = int16_t((int32_t(*x) * XPT2046_X_CALIBRATION) >> 16) + XPT2046_X_OFFSET;
  193. *y = int16_t((int32_t(*y) * XPT2046_Y_CALIBRATION) >> 16) + XPT2046_Y_OFFSET;
  194. }
  195. #if (TFT_ROTATION & TFT_ROTATE_180)
  196. *x = int16_t((TFT_WIDTH) - (int)(*x));
  197. *y = int16_t((TFT_HEIGHT) - (int)(*y));
  198. #endif
  199. return is_touched;
  200. }
  201. bool my_touchpad_read(lv_indev_drv_t * indev_driver, lv_indev_data_t * data) {
  202. static int16_t last_x = 0, last_y = 0;
  203. static uint8_t last_touch_state = LV_INDEV_STATE_REL;
  204. static int32_t touch_time1 = 0;
  205. uint32_t tmpTime, diffTime = 0;
  206. tmpTime = millis();
  207. diffTime = getTickDiff(tmpTime, touch_time1);
  208. /*Save the state and save the pressed coordinate*/
  209. //data->state = TOUCH_PressValid(last_x, last_y) ? LV_INDEV_STATE_PR : LV_INDEV_STATE_REL;
  210. //if (data->state == LV_INDEV_STATE_PR) ADS7843_Rd_Addata((u16 *)&last_x, (u16 *)&last_y);
  211. //touchpad_get_xy(&last_x, &last_y);
  212. /*Save the pressed coordinates and the state*/
  213. if (diffTime > 20) {
  214. if (get_point(&last_x, &last_y)) {
  215. if (last_touch_state == LV_INDEV_STATE_PR) return false;
  216. data->state = LV_INDEV_STATE_PR;
  217. // Set the coordinates (if released use the last-pressed coordinates)
  218. data->point.x = last_x;
  219. data->point.y = last_y;
  220. last_x = last_y = 0;
  221. last_touch_state = LV_INDEV_STATE_PR;
  222. }
  223. else {
  224. if (last_touch_state == LV_INDEV_STATE_PR)
  225. data->state = LV_INDEV_STATE_REL;
  226. last_touch_state = LV_INDEV_STATE_REL;
  227. }
  228. touch_time1 = tmpTime;
  229. }
  230. return false; // Return `false` since no data is buffering or left to read
  231. }
  232. int16_t enc_diff = 0;
  233. lv_indev_state_t state = LV_INDEV_STATE_REL;
  234. bool my_mousewheel_read(lv_indev_drv_t * indev_drv, lv_indev_data_t * data) {
  235. (void) indev_drv; /*Unused*/
  236. data->state = state;
  237. data->enc_diff = enc_diff;
  238. enc_diff = 0;
  239. return false; /*No more data to read so return false*/
  240. }
  241. extern uint8_t currentFlashPage;
  242. //spi_flash
  243. uint32_t pic_read_base_addr = 0, pic_read_addr_offset = 0;
  244. lv_fs_res_t spi_flash_open_cb (lv_fs_drv_t * drv, void * file_p, const char * path, lv_fs_mode_t mode) {
  245. static char last_path_name[30];
  246. if (strcasecmp(last_path_name,path) != 0) {
  247. pic_read_base_addr = lv_get_pic_addr((uint8_t *)path);
  248. ZERO(last_path_name);
  249. strcpy(last_path_name,path);
  250. }
  251. else {
  252. W25QXX.init(SPI_QUARTER_SPEED);
  253. currentFlashPage = 0;
  254. }
  255. pic_read_addr_offset = pic_read_base_addr;
  256. return LV_FS_RES_OK;
  257. }
  258. lv_fs_res_t spi_flash_close_cb (lv_fs_drv_t * drv, void * file_p) {
  259. lv_fs_res_t res = LV_FS_RES_OK;
  260. /* Add your code here*/
  261. pic_read_addr_offset = pic_read_base_addr;
  262. return res;
  263. }
  264. lv_fs_res_t spi_flash_read_cb (lv_fs_drv_t * drv, void * file_p, void * buf, uint32_t btr, uint32_t * br) {
  265. lv_pic_test((uint8_t *)buf, pic_read_addr_offset, btr);
  266. *br = btr;
  267. return LV_FS_RES_OK;
  268. }
  269. lv_fs_res_t spi_flash_seek_cb(lv_fs_drv_t * drv, void * file_p, uint32_t pos) {
  270. #if HAS_SPI_FLASH_COMPRESSION
  271. if (pos == 4) {
  272. uint8_t bmp_header[4];
  273. SPIFlash.beginRead(pic_read_base_addr);
  274. SPIFlash.readData(bmp_header, 4);
  275. currentFlashPage = 1;
  276. }
  277. pic_read_addr_offset = pic_read_base_addr;
  278. #else
  279. pic_read_addr_offset = pic_read_base_addr + pos;
  280. #endif
  281. return LV_FS_RES_OK;
  282. }
  283. lv_fs_res_t spi_flash_tell_cb(lv_fs_drv_t * drv, void * file_p, uint32_t * pos_p) {
  284. *pos_p = pic_read_addr_offset - pic_read_base_addr;
  285. return LV_FS_RES_OK;
  286. }
  287. //sd
  288. char *cur_namefff;
  289. uint32_t sd_read_base_addr = 0,sd_read_addr_offset = 0;
  290. lv_fs_res_t sd_open_cb (lv_fs_drv_t * drv, void * file_p, const char * path, lv_fs_mode_t mode) {
  291. //cur_namefff = strrchr(path, '/');
  292. char name_buf[100];
  293. ZERO(name_buf);
  294. strcat(name_buf,"/");
  295. strcat(name_buf,path);
  296. char *temp = strstr(name_buf,".bin");
  297. if (temp) { strcpy(temp,".GCO"); }
  298. sd_read_base_addr = lv_open_gcode_file((char *)name_buf);
  299. sd_read_addr_offset = sd_read_base_addr;
  300. if (sd_read_addr_offset == 0) return LV_FS_RES_NOT_EX;
  301. return LV_FS_RES_OK;
  302. }
  303. lv_fs_res_t sd_close_cb (lv_fs_drv_t * drv, void * file_p) {
  304. /* Add your code here*/
  305. lv_close_gcode_file();
  306. return LV_FS_RES_OK;
  307. }
  308. lv_fs_res_t sd_read_cb (lv_fs_drv_t * drv, void * file_p, void * buf, uint32_t btr, uint32_t * br) {
  309. if (btr == 200) {
  310. lv_gcode_file_read((uint8_t *)buf);
  311. //pic_read_addr_offset += 208;
  312. *br = 200;
  313. }
  314. else if (btr == 4) {
  315. uint8_t header_pic[4] = { 0x04, 0x90, 0x81, 0x0C };
  316. memcpy(buf, header_pic, 4);
  317. //pic_read_addr_offset += 4;
  318. *br = 4;
  319. }
  320. return LV_FS_RES_OK;
  321. }
  322. lv_fs_res_t sd_seek_cb(lv_fs_drv_t * drv, void * file_p, uint32_t pos) {
  323. sd_read_addr_offset = sd_read_base_addr + (pos - 4) / 200 * 409;
  324. lv_gcode_file_seek(sd_read_addr_offset);
  325. return LV_FS_RES_OK;
  326. }
  327. lv_fs_res_t sd_tell_cb(lv_fs_drv_t * drv, void * file_p, uint32_t * pos_p) {
  328. if (sd_read_addr_offset) *pos_p = 0;
  329. else *pos_p = (sd_read_addr_offset - sd_read_base_addr) / 409 * 200 + 4;
  330. return LV_FS_RES_OK;
  331. }
  332. void lv_encoder_pin_init() {
  333. #if 1 // HAS_DIGITAL_BUTTONS
  334. #if BUTTON_EXISTS(EN1)
  335. SET_INPUT_PULLUP(BTN_EN1);
  336. #endif
  337. #if BUTTON_EXISTS(EN2)
  338. SET_INPUT_PULLUP(BTN_EN2);
  339. #endif
  340. #if BUTTON_EXISTS(ENC)
  341. SET_INPUT_PULLUP(BTN_ENC);
  342. #endif
  343. #if BUTTON_EXISTS(BACK)
  344. SET_INPUT_PULLUP(BTN_BACK);
  345. #endif
  346. #if BUTTON_EXISTS(UP)
  347. SET_INPUT(BTN_UP);
  348. #endif
  349. #if BUTTON_EXISTS(DWN)
  350. SET_INPUT(BTN_DWN);
  351. #endif
  352. #if BUTTON_EXISTS(LFT)
  353. SET_INPUT(BTN_LFT);
  354. #endif
  355. #if BUTTON_EXISTS(RT)
  356. SET_INPUT(BTN_RT);
  357. #endif
  358. #endif // HAS_DIGITAL_BUTTONS
  359. }
  360. #if 1 // HAS_ENCODER_ACTION
  361. //static const int8_t encoderDirection = 1;
  362. //static int16_t enc_Direction;
  363. void lv_update_encoder() {
  364. static uint32_t encoder_time1;
  365. uint32_t tmpTime, diffTime = 0;
  366. tmpTime = millis();
  367. diffTime = getTickDiff(tmpTime, encoder_time1);
  368. if (diffTime > 50) {
  369. #if HAS_ENCODER_WHEEL
  370. #if ANY_BUTTON(EN1, EN2, ENC, BACK)
  371. uint8_t newbutton = 0;
  372. #if BUTTON_EXISTS(EN1)
  373. if (BUTTON_PRESSED(EN1)) newbutton |= EN_A;
  374. #endif
  375. #if BUTTON_EXISTS(EN2)
  376. if (BUTTON_PRESSED(EN2)) newbutton |= EN_B;
  377. #endif
  378. #if BUTTON_EXISTS(ENC)
  379. if (BUTTON_PRESSED(ENC)) newbutton |= EN_C;
  380. #endif
  381. #if BUTTON_EXISTS(BACK)
  382. if (BUTTON_PRESSED(BACK)) newbutton |= EN_D;
  383. #endif
  384. #else
  385. constexpr uint8_t newbutton = 0;
  386. #endif
  387. static uint8_t buttons = newbutton;
  388. static uint8_t lastEncoderBits;
  389. #define encrot0 0
  390. #define encrot1 1
  391. #define encrot2 2
  392. // Manage encoder rotation
  393. //#define ENCODER_SPIN(_E1, _E2) switch (lastEncoderBits) { case _E1: enc_Direction += encoderDirection; break; case _E2: enc_Direction -= encoderDirection; }
  394. uint8_t enc = 0;
  395. if (buttons & EN_A) enc |= B01;
  396. if (buttons & EN_B) enc |= B10;
  397. if (enc != lastEncoderBits) {
  398. switch (enc) {
  399. case encrot1:
  400. if (lastEncoderBits == encrot0) {
  401. enc_diff--;
  402. encoder_time1 = tmpTime;
  403. }
  404. break;
  405. case encrot2:
  406. if (lastEncoderBits == encrot0) {
  407. enc_diff++;
  408. encoder_time1 = tmpTime;
  409. }
  410. break;
  411. }
  412. lastEncoderBits = enc;
  413. }
  414. static uint8_t last_button_state = LV_INDEV_STATE_REL;
  415. const uint8_t enc_c = (buttons & EN_C) ? LV_INDEV_STATE_PR : LV_INDEV_STATE_REL;
  416. if (enc_c != last_button_state) {
  417. state = enc_c ? LV_INDEV_STATE_PR : LV_INDEV_STATE_REL;
  418. last_button_state = enc_c;
  419. }
  420. #endif // HAS_ENCODER_WHEEL
  421. } // next_button_update_ms
  422. }
  423. #endif // HAS_ENCODER_ACTION
  424. #endif // HAS_TFT_LVGL_UI