u8g_dev_tft_upscale_from_128x64.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557
  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. * u8g_dev_tft_320x240_upscale_from_128x64.cpp
  24. *
  25. * Universal 8bit Graphics Library
  26. *
  27. * Copyright (c) 2011, olikraus@gmail.com
  28. * All rights reserved.
  29. *
  30. * Redistribution and use in source and binary forms, with or without modification,
  31. * are permitted provided that the following conditions are met:
  32. *
  33. * Redistributions of source code must retain the above copyright notice, this list
  34. * of conditions and the following disclaimer.
  35. *
  36. * Redistributions in binary form must reproduce the above copyright notice, this
  37. * list of conditions and the following disclaimer in the documentation and/or other
  38. * materials provided with the distribution.
  39. *
  40. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  41. * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  42. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  43. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  44. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
  45. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  46. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  47. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  48. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  49. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  50. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  51. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  52. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  53. */
  54. #include "../../inc/MarlinConfig.h"
  55. #if HAS_MARLINUI_U8GLIB && (PIN_EXISTS(FSMC_CS) || HAS_SPI_GRAPHICAL_TFT || HAS_LTDC_GRAPHICAL_TFT)
  56. #include "HAL_LCD_com_defines.h"
  57. #include "marlinui_DOGM.h"
  58. #include <string.h>
  59. #if ANY(LCD_USE_DMA_FSMC, LCD_USE_DMA_SPI, HAS_LTDC_GRAPHICAL_TFT)
  60. #define HAS_LCD_IO 1
  61. #endif
  62. #include "../tft_io/tft_io.h"
  63. TFT_IO tftio;
  64. #define WIDTH LCD_PIXEL_WIDTH
  65. #define HEIGHT LCD_PIXEL_HEIGHT
  66. #define PAGE_HEIGHT 8
  67. #if ENABLED(TOUCH_SCREEN_CALIBRATION)
  68. #include "../tft_io/touch_calibration.h"
  69. #include "../marlinui.h"
  70. #endif
  71. #include "../touch/touch_buttons.h"
  72. #include "../scaled_tft.h"
  73. #define X_HI (UPSCALE(TFT_PIXEL_OFFSET_X, WIDTH) - 1)
  74. #define Y_HI (UPSCALE(TFT_PIXEL_OFFSET_Y, HEIGHT) - 1)
  75. // RGB565 color picker: https://embeddednotepad.com/page/rgb565-color-picker
  76. // Hex code to color name: https://www.color-name.com/
  77. #define COLOR_BLACK 0x0000 // #000000
  78. #define COLOR_WHITE 0xFFFF // #FFFFFF
  79. #define COLOR_SILVER 0xC618 // #C0C0C0
  80. #define COLOR_GREY 0x7BEF // #808080
  81. #define COLOR_DARKGREY 0x4208 // #404040
  82. #define COLOR_DARKGREY2 0x39E7 // #303030
  83. #define COLOR_DARK 0x0003 // #000019
  84. #define COLOR_RED 0xF800 // #FF0000
  85. #define COLOR_LIME 0x7E00 // #00FF00
  86. #define COLOR_BLUE 0x001F // #0000FF
  87. #define COLOR_YELLOW 0xFFE0 // #FFFF00
  88. #define COLOR_MAGENTA 0xF81F // #FF00FF
  89. #define COLOR_CYAN 0x07FF // #00FFFF
  90. #define COLOR_MAROON 0x7800 // #800000
  91. #define COLOR_GREEN 0x03E0 // #008000
  92. #define COLOR_NAVY 0x000F // #000080
  93. #define COLOR_OLIVE 0x8400 // #808000
  94. #define COLOR_PURPLE 0x8010 // #800080
  95. #define COLOR_TEAL 0x0410 // #008080
  96. #define COLOR_ORANGE 0xFC00 // #FF7F00
  97. #ifndef TFT_MARLINUI_COLOR
  98. #define TFT_MARLINUI_COLOR COLOR_WHITE
  99. #endif
  100. #ifndef TFT_MARLINBG_COLOR
  101. #define TFT_MARLINBG_COLOR COLOR_BLACK
  102. #endif
  103. #ifndef TFT_DISABLED_COLOR
  104. #define TFT_DISABLED_COLOR COLOR_DARK
  105. #endif
  106. #ifndef TFT_BTCANCEL_COLOR
  107. #define TFT_BTCANCEL_COLOR COLOR_RED
  108. #endif
  109. #ifndef TFT_BTARROWS_COLOR
  110. #define TFT_BTARROWS_COLOR COLOR_BLUE
  111. #endif
  112. #ifndef TFT_BTOKMENU_COLOR
  113. #define TFT_BTOKMENU_COLOR COLOR_RED
  114. #endif
  115. static void setWindow(u8g_t *u8g, u8g_dev_t *dev, uint16_t xMin, uint16_t yMin, uint16_t xMax, uint16_t yMax) {
  116. tftio.set_window(xMin, yMin, xMax, yMax);
  117. }
  118. #if HAS_TOUCH_BUTTONS
  119. static const uint8_t buttonD[] = {
  120. B01111111,B11111111,B11111111,B11111110,
  121. B10000000,B00000000,B00000000,B00000001,
  122. B10000000,B00000000,B00000000,B00000001,
  123. B10000000,B00000000,B00000000,B00000001,
  124. B10000000,B00000000,B00000000,B00000001,
  125. B10000000,B00000000,B00000000,B00000001,
  126. B10000000,B00011000,B00110000,B00000001,
  127. B10000000,B00001100,B01100000,B00000001,
  128. B10000000,B00000110,B11000000,B00000001,
  129. B10000000,B00000011,B10000000,B00000001,
  130. B10000000,B00000011,B10000000,B00000001,
  131. B10000000,B00000110,B11000000,B00000001,
  132. B10000000,B00001100,B01100000,B00000001,
  133. B10000000,B00011000,B00110000,B00000001,
  134. B10000000,B00000000,B00000000,B00000001,
  135. B10000000,B00000000,B00000000,B00000001,
  136. B10000000,B00000000,B00000000,B00000001,
  137. B10000000,B00000000,B00000000,B00000001,
  138. B10000000,B00000000,B00000000,B00000001,
  139. B01111111,B11111111,B11111111,B11111110,
  140. };
  141. #if ENABLED(REVERSE_MENU_DIRECTION)
  142. static const uint8_t buttonA[] = {
  143. B01111111,B11111111,B11111111,B11111110,
  144. B10000000,B00000000,B00000000,B00000001,
  145. B10000000,B00000000,B00000000,B00000001,
  146. B10000000,B00000000,B00000000,B00000001,
  147. B10000000,B00000000,B00000000,B00000001,
  148. B10000000,B11100000,B00000000,B00000001,
  149. B10000000,B11100000,B00000000,B00000001,
  150. B10000000,B11100000,B00000000,B00000001,
  151. B10000000,B11100000,B00000000,B00000001,
  152. B10000000,B11100000,B00111111,B11100001,
  153. B10000111,B11111100,B00111111,B11100001,
  154. B10000011,B11111000,B00000000,B00000001,
  155. B10000001,B11110000,B00000000,B00000001,
  156. B10000000,B11100000,B00000000,B00000001,
  157. B10000000,B01000000,B00000000,B00000001,
  158. B10000000,B00000000,B00000000,B00000001,
  159. B10000000,B00000000,B00000000,B00000001,
  160. B10000000,B00000000,B00000000,B00000001,
  161. B10000000,B00000000,B00000000,B00000001,
  162. B01111111,B11111111,B11111111,B11111110,
  163. };
  164. static const uint8_t buttonB[] = {
  165. B01111111,B11111111,B11111111,B11111110,
  166. B10000000,B00000000,B00000000,B00000001,
  167. B10000000,B00000000,B00000000,B00000001,
  168. B10000000,B00000000,B00000000,B00000001,
  169. B10000000,B00000000,B00000000,B00000001,
  170. B10000000,B01100000,B00000010,B00000001,
  171. B10000000,B01100000,B00000111,B00000001,
  172. B10000000,B01100000,B00001111,B10000001,
  173. B10000000,B01100000,B00011111,B11000001,
  174. B10000111,B11111110,B00111111,B11100001,
  175. B10000111,B11111110,B00000111,B00000001,
  176. B10000000,B01100000,B00000111,B00000001,
  177. B10000000,B01100000,B00000111,B00000001,
  178. B10000000,B01100000,B00000111,B00000001,
  179. B10000000,B01100000,B00000111,B00000001,
  180. B10000000,B00000000,B00000000,B00000001,
  181. B10000000,B00000000,B00000000,B00000001,
  182. B10000000,B00000000,B00000000,B00000001,
  183. B10000000,B00000000,B00000000,B00000001,
  184. B01111111,B11111111,B11111111,B11111110,
  185. };
  186. #else
  187. static const uint8_t buttonA[] = {
  188. B01111111,B11111111,B11111111,B11111110,
  189. B10000000,B00000000,B00000000,B00000001,
  190. B10000000,B00000000,B00000000,B00000001,
  191. B10000000,B00000000,B00000000,B00000001,
  192. B10000000,B00000000,B00000000,B00000001,
  193. B10000000,B01000000,B00000000,B00000001,
  194. B10000000,B11100000,B00000000,B00000001,
  195. B10000001,B11110000,B00000000,B00000001,
  196. B10000011,B11111000,B00000000,B00000001,
  197. B10000111,B11111100,B00111111,B11100001,
  198. B10000000,B11100000,B00111111,B11100001,
  199. B10000000,B11100000,B00000000,B00000001,
  200. B10000000,B11100000,B00000000,B00000001,
  201. B10000000,B11100000,B00000000,B00000001,
  202. B10000000,B11100000,B00000000,B00000001,
  203. B10000000,B00000000,B00000000,B00000001,
  204. B10000000,B00000000,B00000000,B00000001,
  205. B10000000,B00000000,B00000000,B00000001,
  206. B10000000,B00000000,B00000000,B00000001,
  207. B01111111,B11111111,B11111111,B11111110,
  208. };
  209. static const uint8_t buttonB[] = {
  210. B01111111,B11111111,B11111111,B11111110,
  211. B10000000,B00000000,B00000000,B00000001,
  212. B10000000,B00000000,B00000000,B00000001,
  213. B10000000,B00000000,B00000000,B00000001,
  214. B10000000,B00000000,B00000000,B00000001,
  215. B10000000,B01100000,B00000111,B00000001,
  216. B10000000,B01100000,B00000111,B00000001,
  217. B10000000,B01100000,B00000111,B00000001,
  218. B10000000,B01100000,B00000111,B00000001,
  219. B10000111,B11111110,B00000111,B00000001,
  220. B10000111,B11111110,B00111111,B11100001,
  221. B10000000,B01100000,B00011111,B11000001,
  222. B10000000,B01100000,B00001111,B10000001,
  223. B10000000,B01100000,B00000111,B00000001,
  224. B10000000,B01100000,B00000010,B00000001,
  225. B10000000,B00000000,B00000000,B00000001,
  226. B10000000,B00000000,B00000000,B00000001,
  227. B10000000,B00000000,B00000000,B00000001,
  228. B10000000,B00000000,B00000000,B00000001,
  229. B01111111,B11111111,B11111111,B11111110,
  230. };
  231. #endif
  232. static const uint8_t buttonC[] = {
  233. B01111111,B11111111,B11111111,B11111110,
  234. B10000000,B00000000,B00000000,B00000001,
  235. B10000000,B00000000,B00000000,B00000001,
  236. B10000000,B00000000,B00000000,B00000001,
  237. B10000000,B00000000,B00000000,B00000001,
  238. B10000000,B00000000,B00000000,B00000001,
  239. B10000000,B00000000,B00011100,B00000001,
  240. B10000000,B00000100,B00011100,B00000001,
  241. B10000000,B00001100,B00011100,B00000001,
  242. B10000000,B00011111,B11111100,B00000001,
  243. B10000000,B00111111,B11111100,B00000001,
  244. B10000000,B00011111,B11111100,B00000001,
  245. B10000000,B00001100,B00000000,B00000001,
  246. B10000000,B00000100,B00000000,B00000001,
  247. B10000000,B00000000,B00000000,B00000001,
  248. B10000000,B00000000,B00000000,B00000001,
  249. B10000000,B00000000,B00000000,B00000001,
  250. B10000000,B00000000,B00000000,B00000001,
  251. B10000000,B00000000,B00000000,B00000001,
  252. B01111111,B11111111,B11111111,B11111110,
  253. };
  254. void drawImage(const uint8_t *data, u8g_t *u8g, u8g_dev_t *dev, uint16_t length, uint16_t height, uint16_t color) {
  255. uint16_t buffer[BUTTON_WIDTH * sq(GRAPHICAL_TFT_UPSCALE)];
  256. if (length > BUTTON_WIDTH) return;
  257. for (uint16_t i = 0; i < height; i++) {
  258. uint16_t k = 0;
  259. for (uint16_t j = 0; j < length; j++) {
  260. uint16_t v = TFT_MARLINBG_COLOR;
  261. if (*(data + (i * (length >> 3) + (j >> 3))) & (0x80 >> (j & 7)))
  262. v = color;
  263. else
  264. v = TFT_MARLINBG_COLOR;
  265. for (uint8_t n = 0; n < GRAPHICAL_TFT_UPSCALE; ++n) buffer[k++] = v;
  266. }
  267. #if HAS_LCD_IO
  268. for (uint8_t n = 1; n < GRAPHICAL_TFT_UPSCALE; ++n)
  269. for (uint16_t l = 0; l < UPSCALE0(length); l++)
  270. buffer[l + n * UPSCALE0(length)] = buffer[l];
  271. tftio.writeSequence(buffer, length * sq(GRAPHICAL_TFT_UPSCALE));
  272. #else
  273. for (uint8_t i = GRAPHICAL_TFT_UPSCALE; i--;)
  274. u8g_WriteSequence(u8g, dev, k << 1, (uint8_t*)buffer);
  275. #endif
  276. }
  277. }
  278. #endif // HAS_TOUCH_BUTTONS
  279. // Used to fill RGB565 (16bits) background
  280. inline void memset2(const void *ptr, uint16_t fill, size_t cnt) {
  281. uint16_t *wptr = (uint16_t*)ptr;
  282. for (size_t i = 0; i < cnt; i += 2) { *wptr = fill; wptr++; }
  283. }
  284. static bool preinit = true;
  285. static uint8_t page;
  286. #if HAS_TOUCH_BUTTONS
  287. static bool redrawTouchButtons = true;
  288. static void drawTouchButtons(u8g_t *u8g, u8g_dev_t *dev) {
  289. if (!redrawTouchButtons) return;
  290. redrawTouchButtons = false;
  291. // Bottom buttons
  292. setWindow(u8g, dev, BUTTOND_X_LO, BUTTON_Y_LO, BUTTOND_X_HI, BUTTON_Y_HI);
  293. drawImage(buttonD, u8g, dev, BUTTON_DRAW_WIDTH, BUTTON_DRAW_HEIGHT, TFT_BTCANCEL_COLOR);
  294. setWindow(u8g, dev, BUTTONA_X_LO, BUTTON_Y_LO, BUTTONA_X_HI, BUTTON_Y_HI);
  295. drawImage(buttonA, u8g, dev, BUTTON_DRAW_WIDTH, BUTTON_DRAW_HEIGHT, TFT_BTARROWS_COLOR);
  296. setWindow(u8g, dev, BUTTONB_X_LO, BUTTON_Y_LO, BUTTONB_X_HI, BUTTON_Y_HI);
  297. drawImage(buttonB, u8g, dev, BUTTON_DRAW_WIDTH, BUTTON_DRAW_HEIGHT, TFT_BTARROWS_COLOR);
  298. setWindow(u8g, dev, BUTTONC_X_LO, BUTTON_Y_LO, BUTTONC_X_HI, BUTTON_Y_HI);
  299. drawImage(buttonC, u8g, dev, BUTTON_DRAW_WIDTH, BUTTON_DRAW_HEIGHT, TFT_BTOKMENU_COLOR);
  300. }
  301. #endif // HAS_TOUCH_BUTTONS
  302. static void u8g_upscale_clear_lcd(u8g_t *u8g, u8g_dev_t *dev, uint16_t *buffer) {
  303. setWindow(u8g, dev, 0, 0, (TFT_WIDTH) - 1, (TFT_HEIGHT) - 1);
  304. #if HAS_LCD_IO
  305. UNUSED(buffer);
  306. tftio.writeMultiple(TFT_MARLINBG_COLOR, (TFT_WIDTH) * (TFT_HEIGHT));
  307. #else
  308. memset2(buffer, TFT_MARLINBG_COLOR, (TFT_WIDTH) / 2);
  309. for (uint16_t i = 0; i < (TFT_HEIGHT) * sq(GRAPHICAL_TFT_UPSCALE); i++)
  310. u8g_WriteSequence(u8g, dev, (TFT_WIDTH) / 2, (uint8_t *)buffer);
  311. #endif
  312. }
  313. static uint8_t msgInitCount = 2; // Ignore all messages until 2nd U8G_COM_MSG_INIT
  314. uint8_t u8g_dev_tft_320x240_upscale_from_128x64_fn(u8g_t *u8g, u8g_dev_t *dev, uint8_t msg, void *arg) {
  315. u8g_pb_t *pb = (u8g_pb_t *)(dev->dev_mem);
  316. #if HAS_LCD_IO
  317. static uint16_t bufferA[WIDTH * sq(GRAPHICAL_TFT_UPSCALE)], bufferB[WIDTH * sq(GRAPHICAL_TFT_UPSCALE)];
  318. uint16_t *buffer = &bufferA[0];
  319. #else
  320. uint16_t buffer[WIDTH * GRAPHICAL_TFT_UPSCALE]; // 16-bit RGB 565 pixel line buffer
  321. #endif
  322. switch (msg) {
  323. case U8G_DEV_MSG_INIT:
  324. dev->com_fn(u8g, U8G_COM_MSG_INIT, U8G_SPI_CLK_CYCLE_NONE, nullptr);
  325. if (preinit) {
  326. preinit = false;
  327. return u8g_dev_pb8v1_base_fn(u8g, dev, msg, arg);
  328. }
  329. if (msgInitCount) return -1;
  330. tftio.init();
  331. tftio.initTFT();
  332. TERN_(TOUCH_SCREEN_CALIBRATION, touch_calibration.calibration_reset());
  333. u8g_upscale_clear_lcd(u8g, dev, buffer);
  334. return 0;
  335. case U8G_DEV_MSG_STOP: preinit = true; break;
  336. case U8G_DEV_MSG_PAGE_FIRST: {
  337. page = 0;
  338. #if HAS_TOUCH_BUTTONS
  339. #if HAS_DISPLAY_SLEEP
  340. static bool sleepCleared;
  341. if (touchBt.isSleeping()) {
  342. if (!sleepCleared) {
  343. sleepCleared = true;
  344. u8g_upscale_clear_lcd(u8g, dev, buffer);
  345. redrawTouchButtons = true;
  346. }
  347. break;
  348. }
  349. else
  350. sleepCleared = false;
  351. #endif
  352. drawTouchButtons(u8g, dev);
  353. #endif
  354. setWindow(u8g, dev, TFT_PIXEL_OFFSET_X, TFT_PIXEL_OFFSET_Y, X_HI, Y_HI);
  355. } break;
  356. case U8G_DEV_MSG_PAGE_NEXT:
  357. #if HAS_TOUCH_BUTTONS && HAS_DISPLAY_SLEEP
  358. if (touchBt.isSleeping()) break;
  359. #endif
  360. if (++page > (HEIGHT / PAGE_HEIGHT)) return 1;
  361. for (uint8_t y = 0; y < PAGE_HEIGHT; ++y) {
  362. uint32_t k = 0;
  363. TERN_(HAS_LCD_IO, buffer = (y & 1) ? bufferB : bufferA);
  364. for (uint16_t i = 0; i < (uint32_t)pb->width; i++) {
  365. const uint8_t b = *(((uint8_t *)pb->buf) + i);
  366. const uint16_t c = TEST(b, y) ? TFT_MARLINUI_COLOR : TFT_MARLINBG_COLOR;
  367. for (uint8_t n = 0; n < GRAPHICAL_TFT_UPSCALE; ++n) buffer[k++] = c;
  368. }
  369. #if HAS_LCD_IO
  370. for (uint8_t n = 1; n < GRAPHICAL_TFT_UPSCALE; ++n)
  371. for (uint16_t l = 0; l < UPSCALE0(WIDTH); l++)
  372. buffer[l + n * UPSCALE0(WIDTH)] = buffer[l];
  373. tftio.writeSequence(buffer, COUNT(bufferA));
  374. #else
  375. uint8_t *bufptr = (uint8_t*) buffer;
  376. for (uint8_t i = GRAPHICAL_TFT_UPSCALE; i--;) {
  377. for (uint8_t n = 0; n < GRAPHICAL_TFT_UPSCALE * 2; ++n) {
  378. u8g_WriteSequence(u8g, dev, WIDTH, &bufptr[WIDTH * n]);
  379. }
  380. }
  381. #endif
  382. }
  383. break;
  384. case U8G_DEV_MSG_SLEEP_ON:
  385. // Enter Sleep Mode (10h)
  386. return 1;
  387. case U8G_DEV_MSG_SLEEP_OFF:
  388. // Sleep Out (11h)
  389. return 1;
  390. }
  391. return u8g_dev_pb8v1_base_fn(u8g, dev, msg, arg);
  392. }
  393. uint8_t u8g_com_hal_tft_fn(u8g_t *u8g, uint8_t msg, uint8_t arg_val, void *arg_ptr) {
  394. if (msgInitCount) {
  395. if (msg == U8G_COM_MSG_INIT) msgInitCount--;
  396. if (msgInitCount) return -1;
  397. }
  398. static uint8_t isCommand;
  399. switch (msg) {
  400. case U8G_COM_MSG_STOP: break;
  401. case U8G_COM_MSG_INIT:
  402. isCommand = 0;
  403. break;
  404. case U8G_COM_MSG_ADDRESS: // define cmd (arg_val = 0) or data mode (arg_val = 1)
  405. isCommand = arg_val == 0 ? 1 : 0;
  406. break;
  407. case U8G_COM_MSG_RESET:
  408. break;
  409. case U8G_COM_MSG_WRITE_BYTE:
  410. tftio.dataTransferBegin(DATASIZE_8BIT);
  411. if (isCommand)
  412. tftio.writeReg(arg_val);
  413. else
  414. tftio.writeData((uint16_t)arg_val);
  415. tftio.dataTransferEnd();
  416. break;
  417. case U8G_COM_MSG_WRITE_SEQ:
  418. tftio.dataTransferBegin(DATASIZE_16BIT);
  419. for (uint8_t i = 0; i < arg_val; i += 2)
  420. tftio.writeData(*(uint16_t *)(((uintptr_t)arg_ptr) + i));
  421. tftio.dataTransferEnd();
  422. break;
  423. }
  424. return 1;
  425. }
  426. U8G_PB_DEV(u8g_dev_tft_320x240_upscale_from_128x64, WIDTH, HEIGHT, PAGE_HEIGHT, u8g_dev_tft_320x240_upscale_from_128x64_fn, U8G_COM_HAL_TFT_FN);
  427. #if ENABLED(TOUCH_SCREEN_CALIBRATION)
  428. static void drawCross(uint16_t x, uint16_t y, uint16_t color) {
  429. tftio.set_window(x - 15, y, x + 15, y);
  430. tftio.writeMultiple(color, 31);
  431. tftio.set_window(x, y - 15, x, y + 15);
  432. tftio.writeMultiple(color, 31);
  433. }
  434. void MarlinUI::touch_calibration_screen() {
  435. uint16_t x, y;
  436. calibrationState stage = touch_calibration.get_calibration_state();
  437. if (stage == CALIBRATION_NONE) {
  438. // start and clear screen
  439. defer_status_screen(true);
  440. stage = touch_calibration.calibration_start();
  441. tftio.set_window(0, 0, (TFT_WIDTH) - 1, (TFT_HEIGHT) - 1);
  442. tftio.writeMultiple(TFT_MARLINBG_COLOR, uint32_t(TFT_WIDTH) * (TFT_HEIGHT));
  443. }
  444. else {
  445. // clear last cross
  446. x = touch_calibration.calibration_points[_MIN(stage - 1, CALIBRATION_BOTTOM_LEFT)].x;
  447. y = touch_calibration.calibration_points[_MIN(stage - 1, CALIBRATION_BOTTOM_LEFT)].y;
  448. drawCross(x, y, TFT_MARLINBG_COLOR);
  449. }
  450. FSTR_P str = nullptr;
  451. if (stage < CALIBRATION_SUCCESS) {
  452. // handle current state
  453. switch (stage) {
  454. case CALIBRATION_TOP_LEFT: str = GET_TEXT_F(MSG_TOP_LEFT); break;
  455. case CALIBRATION_TOP_RIGHT: str = GET_TEXT_F(MSG_TOP_RIGHT); break;
  456. case CALIBRATION_BOTTOM_RIGHT: str = GET_TEXT_F(MSG_BOTTOM_RIGHT); break;
  457. case CALIBRATION_BOTTOM_LEFT: str = GET_TEXT_F(MSG_BOTTOM_LEFT); break;
  458. default: break;
  459. }
  460. x = touch_calibration.calibration_points[stage].x;
  461. y = touch_calibration.calibration_points[stage].y;
  462. drawCross(x, y, TFT_MARLINUI_COLOR);
  463. }
  464. else {
  465. // end calibration
  466. str = stage == CALIBRATION_SUCCESS ? GET_TEXT_F(MSG_CALIBRATION_COMPLETED) : GET_TEXT_F(MSG_CALIBRATION_FAILED);
  467. defer_status_screen(false);
  468. touch_calibration.calibration_end();
  469. TERN_(HAS_TOUCH_BUTTONS, redrawTouchButtons = true);
  470. }
  471. // draw current message
  472. tftio.set_window(TFT_PIXEL_OFFSET_X, TFT_PIXEL_OFFSET_Y, X_HI, Y_HI);
  473. do {
  474. set_font(FONT_MENU);
  475. lcd_put_u8str(0, LCD_PIXEL_HEIGHT / 2, str);
  476. } while (u8g.nextPage());
  477. drawing_screen = false;
  478. safe_delay(250);
  479. if (stage == CALIBRATION_SUCCESS) {
  480. safe_delay(500);
  481. ui.goto_previous_screen();
  482. }
  483. }
  484. #endif // TOUCH_SCREEN_CALIBRATION
  485. #endif // HAS_MARLINUI_U8GLIB && (FSMC_CS_PIN || HAS_SPI_GRAPHICAL_TFT || HAS_LTDC_GRAPHICAL_TFT)