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.

u8g_dev_tft_upscale_from_128x64.cpp 20KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559
  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. #if HAS_TOUCH_BUTTONS && HAS_TOUCH_SLEEP
  72. #define HAS_TOUCH_BUTTONS_SLEEP 1
  73. #endif
  74. #include "../touch/touch_buttons.h"
  75. #include "../scaled_tft.h"
  76. #define X_HI (UPSCALE(TFT_PIXEL_OFFSET_X, WIDTH) - 1)
  77. #define Y_HI (UPSCALE(TFT_PIXEL_OFFSET_Y, HEIGHT) - 1)
  78. // 16 bit color generator: https://ee-programming-notepad.blogspot.com/2016/10/16-bit-color-generator-picker.html
  79. // RGB565 color picker: https://trolsoft.ru/en/articles/rgb565-color-picker
  80. #define COLOR_BLACK 0x0000 // #000000
  81. #define COLOR_WHITE 0xFFFF // #FFFFFF
  82. #define COLOR_SILVER 0xC618 // #C0C0C0
  83. #define COLOR_GREY 0x7BEF // #808080
  84. #define COLOR_DARKGREY 0x4208 // #404040
  85. #define COLOR_DARKGREY2 0x39E7 // #303030
  86. #define COLOR_DARK 0x0003 // #000019
  87. #define COLOR_RED 0xF800 // #FF0000
  88. #define COLOR_LIME 0x7E00 // #00FF00
  89. #define COLOR_BLUE 0x001F // #0000FF
  90. #define COLOR_YELLOW 0xFFE0 // #FFFF00
  91. #define COLOR_MAGENTA 0xF81F // #FF00FF
  92. #define COLOR_FUCHSIA 0xF81F // #FF00FF
  93. #define COLOR_CYAN 0x07FF // #00FFFF
  94. #define COLOR_AQUA 0x07FF // #00FFFF
  95. #define COLOR_MAROON 0x7800 // #800000
  96. #define COLOR_GREEN 0x03E0 // #008000
  97. #define COLOR_NAVY 0x000F // #000080
  98. #define COLOR_OLIVE 0x8400 // #808000
  99. #define COLOR_PURPLE 0x8010 // #800080
  100. #define COLOR_TEAL 0x0410 // #008080
  101. #define COLOR_ORANGE 0xFC00 // #FF7F00
  102. #ifndef TFT_MARLINUI_COLOR
  103. #define TFT_MARLINUI_COLOR COLOR_WHITE
  104. #endif
  105. #ifndef TFT_MARLINBG_COLOR
  106. #define TFT_MARLINBG_COLOR COLOR_BLACK
  107. #endif
  108. #ifndef TFT_DISABLED_COLOR
  109. #define TFT_DISABLED_COLOR COLOR_DARK
  110. #endif
  111. #ifndef TFT_BTCANCEL_COLOR
  112. #define TFT_BTCANCEL_COLOR COLOR_RED
  113. #endif
  114. #ifndef TFT_BTARROWS_COLOR
  115. #define TFT_BTARROWS_COLOR COLOR_BLUE
  116. #endif
  117. #ifndef TFT_BTOKMENU_COLOR
  118. #define TFT_BTOKMENU_COLOR COLOR_RED
  119. #endif
  120. static void setWindow(u8g_t *u8g, u8g_dev_t *dev, uint16_t Xmin, uint16_t Ymin, uint16_t Xmax, uint16_t Ymax) {
  121. tftio.set_window(Xmin, Ymin, Xmax, Ymax);
  122. }
  123. #if HAS_TOUCH_BUTTONS
  124. static const uint8_t buttonD[] = {
  125. B01111111,B11111111,B11111111,B11111110,
  126. B10000000,B00000000,B00000000,B00000001,
  127. B10000000,B00000000,B00000000,B00000001,
  128. B10000000,B00000000,B00000000,B00000001,
  129. B10000000,B00000000,B00000000,B00000001,
  130. B10000000,B00000000,B00000000,B00000001,
  131. B10000000,B00011000,B00110000,B00000001,
  132. B10000000,B00001100,B01100000,B00000001,
  133. B10000000,B00000110,B11000000,B00000001,
  134. B10000000,B00000011,B10000000,B00000001,
  135. B10000000,B00000011,B10000000,B00000001,
  136. B10000000,B00000110,B11000000,B00000001,
  137. B10000000,B00001100,B01100000,B00000001,
  138. B10000000,B00011000,B00110000,B00000001,
  139. B10000000,B00000000,B00000000,B00000001,
  140. B10000000,B00000000,B00000000,B00000001,
  141. B10000000,B00000000,B00000000,B00000001,
  142. B10000000,B00000000,B00000000,B00000001,
  143. B10000000,B00000000,B00000000,B00000001,
  144. B01111111,B11111111,B11111111,B11111110,
  145. };
  146. #if ENABLED(REVERSE_MENU_DIRECTION)
  147. static const uint8_t buttonA[] = {
  148. B01111111,B11111111,B11111111,B11111110,
  149. B10000000,B00000000,B00000000,B00000001,
  150. B10000000,B00000000,B00000000,B00000001,
  151. B10000000,B00000000,B00000000,B00000001,
  152. B10000000,B00000000,B00000000,B00000001,
  153. B10000000,B11100000,B00000000,B00000001,
  154. B10000000,B11100000,B00000000,B00000001,
  155. B10000000,B11100000,B00000000,B00000001,
  156. B10000000,B11100000,B00000000,B00000001,
  157. B10000000,B11100000,B00111111,B11100001,
  158. B10000111,B11111100,B00111111,B11100001,
  159. B10000011,B11111000,B00000000,B00000001,
  160. B10000001,B11110000,B00000000,B00000001,
  161. B10000000,B11100000,B00000000,B00000001,
  162. B10000000,B01000000,B00000000,B00000001,
  163. B10000000,B00000000,B00000000,B00000001,
  164. B10000000,B00000000,B00000000,B00000001,
  165. B10000000,B00000000,B00000000,B00000001,
  166. B10000000,B00000000,B00000000,B00000001,
  167. B01111111,B11111111,B11111111,B11111110,
  168. };
  169. static const uint8_t buttonB[] = {
  170. B01111111,B11111111,B11111111,B11111110,
  171. B10000000,B00000000,B00000000,B00000001,
  172. B10000000,B00000000,B00000000,B00000001,
  173. B10000000,B00000000,B00000000,B00000001,
  174. B10000000,B00000000,B00000000,B00000001,
  175. B10000000,B01100000,B00000010,B00000001,
  176. B10000000,B01100000,B00000111,B00000001,
  177. B10000000,B01100000,B00001111,B10000001,
  178. B10000000,B01100000,B00011111,B11000001,
  179. B10000111,B11111110,B00111111,B11100001,
  180. B10000111,B11111110,B00000111,B00000001,
  181. B10000000,B01100000,B00000111,B00000001,
  182. B10000000,B01100000,B00000111,B00000001,
  183. B10000000,B01100000,B00000111,B00000001,
  184. B10000000,B01100000,B00000111,B00000001,
  185. B10000000,B00000000,B00000000,B00000001,
  186. B10000000,B00000000,B00000000,B00000001,
  187. B10000000,B00000000,B00000000,B00000001,
  188. B10000000,B00000000,B00000000,B00000001,
  189. B01111111,B11111111,B11111111,B11111110,
  190. };
  191. #else
  192. static const uint8_t buttonA[] = {
  193. B01111111,B11111111,B11111111,B11111110,
  194. B10000000,B00000000,B00000000,B00000001,
  195. B10000000,B00000000,B00000000,B00000001,
  196. B10000000,B00000000,B00000000,B00000001,
  197. B10000000,B00000000,B00000000,B00000001,
  198. B10000000,B01000000,B00000000,B00000001,
  199. B10000000,B11100000,B00000000,B00000001,
  200. B10000001,B11110000,B00000000,B00000001,
  201. B10000011,B11111000,B00000000,B00000001,
  202. B10000111,B11111100,B00111111,B11100001,
  203. B10000000,B11100000,B00111111,B11100001,
  204. B10000000,B11100000,B00000000,B00000001,
  205. B10000000,B11100000,B00000000,B00000001,
  206. B10000000,B11100000,B00000000,B00000001,
  207. B10000000,B11100000,B00000000,B00000001,
  208. B10000000,B00000000,B00000000,B00000001,
  209. B10000000,B00000000,B00000000,B00000001,
  210. B10000000,B00000000,B00000000,B00000001,
  211. B10000000,B00000000,B00000000,B00000001,
  212. B01111111,B11111111,B11111111,B11111110,
  213. };
  214. static const uint8_t buttonB[] = {
  215. B01111111,B11111111,B11111111,B11111110,
  216. B10000000,B00000000,B00000000,B00000001,
  217. B10000000,B00000000,B00000000,B00000001,
  218. B10000000,B00000000,B00000000,B00000001,
  219. B10000000,B00000000,B00000000,B00000001,
  220. B10000000,B01100000,B00000111,B00000001,
  221. B10000000,B01100000,B00000111,B00000001,
  222. B10000000,B01100000,B00000111,B00000001,
  223. B10000000,B01100000,B00000111,B00000001,
  224. B10000111,B11111110,B00000111,B00000001,
  225. B10000111,B11111110,B00111111,B11100001,
  226. B10000000,B01100000,B00011111,B11000001,
  227. B10000000,B01100000,B00001111,B10000001,
  228. B10000000,B01100000,B00000111,B00000001,
  229. B10000000,B01100000,B00000010,B00000001,
  230. B10000000,B00000000,B00000000,B00000001,
  231. B10000000,B00000000,B00000000,B00000001,
  232. B10000000,B00000000,B00000000,B00000001,
  233. B10000000,B00000000,B00000000,B00000001,
  234. B01111111,B11111111,B11111111,B11111110,
  235. };
  236. #endif
  237. static const uint8_t buttonC[] = {
  238. B01111111,B11111111,B11111111,B11111110,
  239. B10000000,B00000000,B00000000,B00000001,
  240. B10000000,B00000000,B00000000,B00000001,
  241. B10000000,B00000000,B00000000,B00000001,
  242. B10000000,B00000000,B00000000,B00000001,
  243. B10000000,B00000000,B00000000,B00000001,
  244. B10000000,B00000000,B00011100,B00000001,
  245. B10000000,B00000100,B00011100,B00000001,
  246. B10000000,B00001100,B00011100,B00000001,
  247. B10000000,B00011111,B11111100,B00000001,
  248. B10000000,B00111111,B11111100,B00000001,
  249. B10000000,B00011111,B11111100,B00000001,
  250. B10000000,B00001100,B00000000,B00000001,
  251. B10000000,B00000100,B00000000,B00000001,
  252. B10000000,B00000000,B00000000,B00000001,
  253. B10000000,B00000000,B00000000,B00000001,
  254. B10000000,B00000000,B00000000,B00000001,
  255. B10000000,B00000000,B00000000,B00000001,
  256. B10000000,B00000000,B00000000,B00000001,
  257. B01111111,B11111111,B11111111,B11111110,
  258. };
  259. void drawImage(const uint8_t *data, u8g_t *u8g, u8g_dev_t *dev, uint16_t length, uint16_t height, uint16_t color) {
  260. uint16_t buffer[BUTTON_WIDTH * sq(GRAPHICAL_TFT_UPSCALE)];
  261. if (length > BUTTON_WIDTH) return;
  262. for (uint16_t i = 0; i < height; i++) {
  263. uint16_t k = 0;
  264. for (uint16_t j = 0; j < length; j++) {
  265. uint16_t v = TFT_MARLINBG_COLOR;
  266. if (*(data + (i * (length >> 3) + (j >> 3))) & (0x80 >> (j & 7)))
  267. v = color;
  268. else
  269. v = TFT_MARLINBG_COLOR;
  270. LOOP_L_N(n, GRAPHICAL_TFT_UPSCALE) buffer[k++] = v;
  271. }
  272. #if HAS_LCD_IO
  273. LOOP_S_L_N(n, 1, GRAPHICAL_TFT_UPSCALE)
  274. for (uint16_t l = 0; l < UPSCALE0(length); l++)
  275. buffer[l + n * UPSCALE0(length)] = buffer[l];
  276. tftio.WriteSequence(buffer, length * sq(GRAPHICAL_TFT_UPSCALE));
  277. #else
  278. for (uint8_t i = GRAPHICAL_TFT_UPSCALE; i--;)
  279. u8g_WriteSequence(u8g, dev, k << 1, (uint8_t*)buffer);
  280. #endif
  281. }
  282. }
  283. #endif // HAS_TOUCH_BUTTONS
  284. // Used to fill RGB565 (16bits) background
  285. inline void memset2(const void *ptr, uint16_t fill, size_t cnt) {
  286. uint16_t *wptr = (uint16_t*)ptr;
  287. for (size_t i = 0; i < cnt; i += 2) { *wptr = fill; wptr++; }
  288. }
  289. static bool preinit = true;
  290. static uint8_t page;
  291. #if HAS_TOUCH_BUTTONS
  292. static bool redrawTouchButtons = true;
  293. static void drawTouchButtons(u8g_t *u8g, u8g_dev_t *dev) {
  294. if (!redrawTouchButtons) return;
  295. redrawTouchButtons = false;
  296. // Bottom buttons
  297. setWindow(u8g, dev, BUTTOND_X_LO, BUTTON_Y_LO, BUTTOND_X_HI, BUTTON_Y_HI);
  298. drawImage(buttonD, u8g, dev, BUTTON_DRAW_WIDTH, BUTTON_DRAW_HEIGHT, TFT_BTCANCEL_COLOR);
  299. setWindow(u8g, dev, BUTTONA_X_LO, BUTTON_Y_LO, BUTTONA_X_HI, BUTTON_Y_HI);
  300. drawImage(buttonA, u8g, dev, BUTTON_DRAW_WIDTH, BUTTON_DRAW_HEIGHT, TFT_BTARROWS_COLOR);
  301. setWindow(u8g, dev, BUTTONB_X_LO, BUTTON_Y_LO, BUTTONB_X_HI, BUTTON_Y_HI);
  302. drawImage(buttonB, u8g, dev, BUTTON_DRAW_WIDTH, BUTTON_DRAW_HEIGHT, TFT_BTARROWS_COLOR);
  303. setWindow(u8g, dev, BUTTONC_X_LO, BUTTON_Y_LO, BUTTONC_X_HI, BUTTON_Y_HI);
  304. drawImage(buttonC, u8g, dev, BUTTON_DRAW_WIDTH, BUTTON_DRAW_HEIGHT, TFT_BTOKMENU_COLOR);
  305. }
  306. #endif // HAS_TOUCH_BUTTONS
  307. static void u8g_upscale_clear_lcd(u8g_t *u8g, u8g_dev_t *dev, uint16_t *buffer) {
  308. setWindow(u8g, dev, 0, 0, (TFT_WIDTH) - 1, (TFT_HEIGHT) - 1);
  309. #if HAS_LCD_IO
  310. UNUSED(buffer);
  311. tftio.WriteMultiple(TFT_MARLINBG_COLOR, (TFT_WIDTH) * (TFT_HEIGHT));
  312. #else
  313. memset2(buffer, TFT_MARLINBG_COLOR, (TFT_WIDTH) / 2);
  314. for (uint16_t i = 0; i < (TFT_HEIGHT) * sq(GRAPHICAL_TFT_UPSCALE); i++)
  315. u8g_WriteSequence(u8g, dev, (TFT_WIDTH) / 2, (uint8_t *)buffer);
  316. #endif
  317. }
  318. static uint8_t msgInitCount = 2; // Ignore all messages until 2nd U8G_COM_MSG_INIT
  319. uint8_t u8g_dev_tft_320x240_upscale_from_128x64_fn(u8g_t *u8g, u8g_dev_t *dev, uint8_t msg, void *arg) {
  320. u8g_pb_t *pb = (u8g_pb_t *)(dev->dev_mem);
  321. #if HAS_LCD_IO
  322. static uint16_t bufferA[WIDTH * sq(GRAPHICAL_TFT_UPSCALE)], bufferB[WIDTH * sq(GRAPHICAL_TFT_UPSCALE)];
  323. uint16_t *buffer = &bufferA[0];
  324. #else
  325. uint16_t buffer[WIDTH * GRAPHICAL_TFT_UPSCALE]; // 16-bit RGB 565 pixel line buffer
  326. #endif
  327. switch (msg) {
  328. case U8G_DEV_MSG_INIT:
  329. dev->com_fn(u8g, U8G_COM_MSG_INIT, U8G_SPI_CLK_CYCLE_NONE, nullptr);
  330. if (preinit) {
  331. preinit = false;
  332. return u8g_dev_pb8v1_base_fn(u8g, dev, msg, arg);
  333. }
  334. if (msgInitCount) return -1;
  335. tftio.Init();
  336. tftio.InitTFT();
  337. TERN_(TOUCH_SCREEN_CALIBRATION, touch_calibration.calibration_reset());
  338. u8g_upscale_clear_lcd(u8g, dev, buffer);
  339. return 0;
  340. case U8G_DEV_MSG_STOP: preinit = true; break;
  341. case U8G_DEV_MSG_PAGE_FIRST: {
  342. page = 0;
  343. #if HAS_TOUCH_BUTTONS_SLEEP
  344. static bool sleepCleared;
  345. if (touchBt.isSleeping()) {
  346. if (!sleepCleared) {
  347. sleepCleared = true;
  348. u8g_upscale_clear_lcd(u8g, dev, buffer);
  349. TERN_(HAS_TOUCH_BUTTONS, redrawTouchButtons = true);
  350. }
  351. break;
  352. }
  353. else
  354. sleepCleared = false;
  355. #endif
  356. TERN_(HAS_TOUCH_BUTTONS, drawTouchButtons(u8g, dev));
  357. setWindow(u8g, dev, TFT_PIXEL_OFFSET_X, TFT_PIXEL_OFFSET_Y, X_HI, Y_HI);
  358. } break;
  359. case U8G_DEV_MSG_PAGE_NEXT:
  360. if (TERN0(HAS_TOUCH_BUTTONS_SLEEP, touchBt.isSleeping())) break;
  361. if (++page > (HEIGHT / PAGE_HEIGHT)) return 1;
  362. LOOP_L_N(y, PAGE_HEIGHT) {
  363. uint32_t k = 0;
  364. TERN_(HAS_LCD_IO, buffer = (y & 1) ? bufferB : bufferA);
  365. for (uint16_t i = 0; i < (uint32_t)pb->width; i++) {
  366. const uint8_t b = *(((uint8_t *)pb->buf) + i);
  367. const uint16_t c = TEST(b, y) ? TFT_MARLINUI_COLOR : TFT_MARLINBG_COLOR;
  368. LOOP_L_N(n, GRAPHICAL_TFT_UPSCALE) buffer[k++] = c;
  369. }
  370. #if HAS_LCD_IO
  371. LOOP_S_L_N(n, 1, GRAPHICAL_TFT_UPSCALE)
  372. for (uint16_t l = 0; l < UPSCALE0(WIDTH); l++)
  373. buffer[l + n * UPSCALE0(WIDTH)] = buffer[l];
  374. tftio.WriteSequence(buffer, COUNT(bufferA));
  375. #else
  376. uint8_t *bufptr = (uint8_t*) buffer;
  377. for (uint8_t i = GRAPHICAL_TFT_UPSCALE; i--;) {
  378. LOOP_S_L_N(n, 0, GRAPHICAL_TFT_UPSCALE * 2) {
  379. u8g_WriteSequence(u8g, dev, WIDTH, &bufptr[WIDTH * n]);
  380. }
  381. }
  382. #endif
  383. }
  384. break;
  385. case U8G_DEV_MSG_SLEEP_ON:
  386. // Enter Sleep Mode (10h)
  387. return 1;
  388. case U8G_DEV_MSG_SLEEP_OFF:
  389. // Sleep Out (11h)
  390. return 1;
  391. }
  392. return u8g_dev_pb8v1_base_fn(u8g, dev, msg, arg);
  393. }
  394. uint8_t u8g_com_hal_tft_fn(u8g_t *u8g, uint8_t msg, uint8_t arg_val, void *arg_ptr) {
  395. if (msgInitCount) {
  396. if (msg == U8G_COM_MSG_INIT) msgInitCount--;
  397. if (msgInitCount) return -1;
  398. }
  399. static uint8_t isCommand;
  400. switch (msg) {
  401. case U8G_COM_MSG_STOP: break;
  402. case U8G_COM_MSG_INIT:
  403. isCommand = 0;
  404. break;
  405. case U8G_COM_MSG_ADDRESS: // define cmd (arg_val = 0) or data mode (arg_val = 1)
  406. isCommand = arg_val == 0 ? 1 : 0;
  407. break;
  408. case U8G_COM_MSG_RESET:
  409. break;
  410. case U8G_COM_MSG_WRITE_BYTE:
  411. tftio.DataTransferBegin(DATASIZE_8BIT);
  412. if (isCommand)
  413. tftio.WriteReg(arg_val);
  414. else
  415. tftio.WriteData((uint16_t)arg_val);
  416. tftio.DataTransferEnd();
  417. break;
  418. case U8G_COM_MSG_WRITE_SEQ:
  419. tftio.DataTransferBegin(DATASIZE_16BIT);
  420. for (uint8_t i = 0; i < arg_val; i += 2)
  421. tftio.WriteData(*(uint16_t *)(((uintptr_t)arg_ptr) + i));
  422. tftio.DataTransferEnd();
  423. break;
  424. }
  425. return 1;
  426. }
  427. 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);
  428. #if ENABLED(TOUCH_SCREEN_CALIBRATION)
  429. static void drawCross(uint16_t x, uint16_t y, uint16_t color) {
  430. tftio.set_window(x - 15, y, x + 15, y);
  431. tftio.WriteMultiple(color, 31);
  432. tftio.set_window(x, y - 15, x, y + 15);
  433. tftio.WriteMultiple(color, 31);
  434. }
  435. void MarlinUI::touch_calibration_screen() {
  436. uint16_t x, y;
  437. calibrationState calibration_stage = touch_calibration.get_calibration_state();
  438. if (calibration_stage == CALIBRATION_NONE) {
  439. // start and clear screen
  440. defer_status_screen(true);
  441. calibration_stage = touch_calibration.calibration_start();
  442. tftio.set_window(0, 0, (TFT_WIDTH) - 1, (TFT_HEIGHT) - 1);
  443. tftio.WriteMultiple(TFT_MARLINBG_COLOR, uint32_t(TFT_WIDTH) * (TFT_HEIGHT));
  444. }
  445. else {
  446. // clear last cross
  447. x = touch_calibration.calibration_points[_MIN(calibration_stage - 1, CALIBRATION_BOTTOM_RIGHT)].x;
  448. y = touch_calibration.calibration_points[_MIN(calibration_stage - 1, CALIBRATION_BOTTOM_RIGHT)].y;
  449. drawCross(x, y, TFT_MARLINBG_COLOR);
  450. }
  451. FSTR_P str = nullptr;
  452. if (calibration_stage < CALIBRATION_SUCCESS) {
  453. // handle current state
  454. switch (calibration_stage) {
  455. case CALIBRATION_TOP_LEFT: str = GET_TEXT_F(MSG_TOP_LEFT); break;
  456. case CALIBRATION_BOTTOM_LEFT: str = GET_TEXT_F(MSG_BOTTOM_LEFT); break;
  457. case CALIBRATION_TOP_RIGHT: str = GET_TEXT_F(MSG_TOP_RIGHT); break;
  458. case CALIBRATION_BOTTOM_RIGHT: str = GET_TEXT_F(MSG_BOTTOM_RIGHT); break;
  459. default: break;
  460. }
  461. x = touch_calibration.calibration_points[calibration_stage].x;
  462. y = touch_calibration.calibration_points[calibration_stage].y;
  463. drawCross(x, y, TFT_MARLINUI_COLOR);
  464. }
  465. else {
  466. // end calibration
  467. str = calibration_stage == CALIBRATION_SUCCESS ? GET_TEXT_F(MSG_CALIBRATION_COMPLETED) : GET_TEXT_F(MSG_CALIBRATION_FAILED);
  468. defer_status_screen(false);
  469. touch_calibration.calibration_end();
  470. TERN_(HAS_TOUCH_BUTTONS, redrawTouchButtons = true);
  471. }
  472. // draw current message
  473. tftio.set_window(TFT_PIXEL_OFFSET_X, TFT_PIXEL_OFFSET_Y, X_HI, Y_HI);
  474. do {
  475. set_font(FONT_MENU);
  476. lcd_put_u8str(0, LCD_PIXEL_HEIGHT / 2, str);
  477. } while (u8g.nextPage());
  478. drawing_screen = false;
  479. safe_delay(250);
  480. if (calibration_stage == CALIBRATION_SUCCESS) {
  481. safe_delay(500);
  482. ui.goto_previous_screen();
  483. }
  484. }
  485. #endif // TOUCH_SCREEN_CALIBRATION
  486. #endif // HAS_MARLINUI_U8GLIB && (FSMC_CS_PIN || HAS_SPI_GRAPHICAL_TFT)