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

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