My Marlin configs for Fabrikator Mini and CTC i3 Pro B
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

tft_lvgl_configuration.cpp 15KB

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