My Marlin configs for Fabrikator Mini and CTC i3 Pro B
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

dwin_lcd.cpp 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470
  1. /**
  2. * Marlin 3D Printer Firmware
  3. * Copyright (c) 2021 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 lcd/e3v2/marlinui/dwin_lcd.cpp
  24. * @brief DWIN screen control functions
  25. ********************************************************************************/
  26. #include "../../../inc/MarlinConfigPre.h"
  27. #if IS_DWIN_MARLINUI
  28. #include "../../../inc/MarlinConfig.h"
  29. #include "dwin_lcd.h"
  30. #include <string.h> // for memset
  31. //#define DEBUG_OUT 1
  32. #include "../../../core/debug_out.h"
  33. // Make sure DWIN_SendBuf is large enough to hold the largest string plus draw command and tail.
  34. // Assume the narrowest (6 pixel) font and 2-byte gb2312-encoded characters.
  35. uint8_t DWIN_SendBuf[11 + DWIN_WIDTH / 6 * 2] = { 0xAA };
  36. uint8_t DWIN_BufTail[4] = { 0xCC, 0x33, 0xC3, 0x3C };
  37. uint8_t databuf[26] = { 0 };
  38. uint8_t receivedType;
  39. int recnum = 0;
  40. inline void DWIN_Byte(size_t &i, const uint16_t bval) {
  41. DWIN_SendBuf[++i] = bval;
  42. }
  43. inline void DWIN_Word(size_t &i, const uint16_t wval) {
  44. DWIN_SendBuf[++i] = wval >> 8;
  45. DWIN_SendBuf[++i] = wval & 0xFF;
  46. }
  47. inline void DWIN_Long(size_t &i, const uint32_t lval) {
  48. DWIN_SendBuf[++i] = (lval >> 24) & 0xFF;
  49. DWIN_SendBuf[++i] = (lval >> 16) & 0xFF;
  50. DWIN_SendBuf[++i] = (lval >> 8) & 0xFF;
  51. DWIN_SendBuf[++i] = lval & 0xFF;
  52. }
  53. inline void DWIN_String(size_t &i, char * const string) {
  54. const size_t len = _MIN(sizeof(DWIN_SendBuf) - i, strlen(string));
  55. memcpy(&DWIN_SendBuf[i+1], string, len);
  56. i += len;
  57. }
  58. inline void DWIN_String(size_t &i, const __FlashStringHelper * string) {
  59. if (!string) return;
  60. const size_t len = strlen_P((PGM_P)string); // cast it to PGM_P, which is basically const char *, and measure it using the _P version of strlen.
  61. if (len == 0) return;
  62. memcpy(&DWIN_SendBuf[i+1], string, len);
  63. i += len;
  64. }
  65. // Send the data in the buffer and the packet end
  66. inline void DWIN_Send(size_t &i) {
  67. ++i;
  68. LOOP_L_N(n, i) { LCD_SERIAL.write(DWIN_SendBuf[n]); delayMicroseconds(1); }
  69. LOOP_L_N(n, 4) { LCD_SERIAL.write(DWIN_BufTail[n]); delayMicroseconds(1); }
  70. }
  71. /*-------------------------------------- System variable function --------------------------------------*/
  72. // Handshake (1: Success, 0: Fail)
  73. bool DWIN_Handshake(void) {
  74. #ifndef LCD_BAUDRATE
  75. #define LCD_BAUDRATE 115200
  76. #endif
  77. LCD_SERIAL.begin(LCD_BAUDRATE);
  78. const millis_t serial_connect_timeout = millis() + 1000UL;
  79. while (!LCD_SERIAL.connected() && PENDING(millis(), serial_connect_timeout)) { /*nada*/ }
  80. size_t i = 0;
  81. DWIN_Byte(i, 0x00);
  82. DWIN_Send(i);
  83. while (LCD_SERIAL.available() > 0 && recnum < (signed)sizeof(databuf)) {
  84. databuf[recnum] = LCD_SERIAL.read();
  85. // ignore the invalid data
  86. if (databuf[0] != FHONE) { // prevent the program from running.
  87. if (recnum > 0) {
  88. recnum = 0;
  89. ZERO(databuf);
  90. }
  91. continue;
  92. }
  93. delay(10);
  94. recnum++;
  95. }
  96. return ( recnum >= 3
  97. && databuf[0] == FHONE
  98. && databuf[1] == '\0'
  99. && databuf[2] == 'O'
  100. && databuf[3] == 'K' );
  101. }
  102. void DWIN_Startup(void) {
  103. DEBUG_ECHOPGM("\r\nDWIN handshake ");
  104. delay(750); // Delay here or init later in the boot process
  105. const bool success = DWIN_Handshake();
  106. if (success) DEBUG_ECHOLNPGM("ok."); else DEBUG_ECHOLNPGM("error.");
  107. DWIN_Frame_SetDir(TERN(DWIN_MARLINUI_LANDSCAPE, 0, 1));
  108. DWIN_Frame_Clear(Color_Bg_Black); // MarlinUI handles the bootscreen so just clear here
  109. DWIN_UpdateLCD();
  110. }
  111. // Set the backlight luminance
  112. // luminance: (0x00-0xFF)
  113. void DWIN_Backlight_SetLuminance(const uint8_t luminance) {
  114. size_t i = 0;
  115. DWIN_Byte(i, 0x30);
  116. DWIN_Byte(i, _MAX(luminance, 0x1F));
  117. DWIN_Send(i);
  118. }
  119. // Set screen display direction
  120. // dir: 0=0°, 1=90°, 2=180°, 3=270°
  121. void DWIN_Frame_SetDir(uint8_t dir) {
  122. size_t i = 0;
  123. DWIN_Byte(i, 0x34);
  124. DWIN_Byte(i, 0x5A);
  125. DWIN_Byte(i, 0xA5);
  126. DWIN_Byte(i, dir);
  127. DWIN_Send(i);
  128. }
  129. // Update display
  130. void DWIN_UpdateLCD(void) {
  131. size_t i = 0;
  132. DWIN_Byte(i, 0x3D);
  133. DWIN_Send(i);
  134. }
  135. /*---------------------------------------- Drawing functions ----------------------------------------*/
  136. // Clear screen
  137. // color: Clear screen color
  138. void DWIN_Frame_Clear(const uint16_t color) {
  139. size_t i = 0;
  140. DWIN_Byte(i, 0x01);
  141. DWIN_Word(i, color);
  142. DWIN_Send(i);
  143. }
  144. // Draw a point
  145. // width: point width 0x01-0x0F
  146. // height: point height 0x01-0x0F
  147. // x,y: upper left point
  148. void DWIN_Draw_Point(uint16_t color, uint8_t width, uint8_t height, uint16_t x, uint16_t y) {
  149. size_t i = 0;
  150. DWIN_Byte(i, 0x02);
  151. DWIN_Word(i, color);
  152. DWIN_Byte(i, width);
  153. DWIN_Byte(i, height);
  154. DWIN_Word(i, x);
  155. DWIN_Word(i, y);
  156. DWIN_Send(i);
  157. }
  158. // Draw a line
  159. // color: Line segment color
  160. // xStart/yStart: Start point
  161. // xEnd/yEnd: End point
  162. void DWIN_Draw_Line(uint16_t color, uint16_t xStart, uint16_t yStart, uint16_t xEnd, uint16_t yEnd) {
  163. size_t i = 0;
  164. DWIN_Byte(i, 0x03);
  165. DWIN_Word(i, color);
  166. DWIN_Word(i, xStart);
  167. DWIN_Word(i, yStart);
  168. DWIN_Word(i, xEnd);
  169. DWIN_Word(i, yEnd);
  170. DWIN_Send(i);
  171. }
  172. // Draw a rectangle
  173. // mode: 0=frame, 1=fill, 2=XOR fill
  174. // color: Rectangle color
  175. // xStart/yStart: upper left point
  176. // xEnd/yEnd: lower right point
  177. void DWIN_Draw_Rectangle(uint8_t mode, uint16_t color,
  178. uint16_t xStart, uint16_t yStart, uint16_t xEnd, uint16_t yEnd) {
  179. size_t i = 0;
  180. DWIN_Byte(i, 0x05);
  181. DWIN_Byte(i, mode);
  182. DWIN_Word(i, color);
  183. DWIN_Word(i, xStart);
  184. DWIN_Word(i, yStart);
  185. DWIN_Word(i, xEnd);
  186. DWIN_Word(i, yEnd);
  187. DWIN_Send(i);
  188. }
  189. // Move a screen area
  190. // mode: 0, circle shift; 1, translation
  191. // dir: 0=left, 1=right, 2=up, 3=down
  192. // dis: Distance
  193. // color: Fill color
  194. // xStart/yStart: upper left point
  195. // xEnd/yEnd: bottom right point
  196. void DWIN_Frame_AreaMove(uint8_t mode, uint8_t dir, uint16_t dis,
  197. uint16_t color, uint16_t xStart, uint16_t yStart, uint16_t xEnd, uint16_t yEnd) {
  198. size_t i = 0;
  199. DWIN_Byte(i, 0x09);
  200. DWIN_Byte(i, (mode << 7) | dir);
  201. DWIN_Word(i, dis);
  202. DWIN_Word(i, color);
  203. DWIN_Word(i, xStart);
  204. DWIN_Word(i, yStart);
  205. DWIN_Word(i, xEnd);
  206. DWIN_Word(i, yEnd);
  207. DWIN_Send(i);
  208. }
  209. /*---------------------------------------- Text related functions ----------------------------------------*/
  210. // Draw a string
  211. // bShow: true=display background color; false=don't display background color
  212. // size: Font size
  213. // color: Character color
  214. // bColor: Background color
  215. // x/y: Upper-left coordinate of the string
  216. // *string: The string
  217. void DWIN_Draw_String(bool bShow, uint8_t size, uint16_t color, uint16_t bColor, uint16_t x, uint16_t y, char *string) {
  218. uint8_t widthAdjust = 0;
  219. size_t i = 0;
  220. DWIN_Byte(i, 0x11);
  221. // Bit 7: widthAdjust
  222. // Bit 6: bShow
  223. // Bit 5-4: Unused (0)
  224. // Bit 3-0: size
  225. DWIN_Byte(i, (widthAdjust * 0x80) | (bShow * 0x40) | size);
  226. DWIN_Word(i, color);
  227. DWIN_Word(i, bColor);
  228. DWIN_Word(i, x);
  229. DWIN_Word(i, y);
  230. DWIN_String(i, string);
  231. DWIN_Send(i);
  232. }
  233. // Draw a positive integer
  234. // bShow: true=display background color; false=don't display background color
  235. // zeroFill: true=zero fill; false=no zero fill
  236. // zeroMode: 1=leading 0 displayed as 0; 0=leading 0 displayed as a space
  237. // size: Font size
  238. // color: Character color
  239. // bColor: Background color
  240. // iNum: Number of digits
  241. // x/y: Upper-left coordinate
  242. // value: Integer value
  243. void DWIN_Draw_IntValue(uint8_t bShow, bool zeroFill, uint8_t zeroMode, uint8_t size, uint16_t color,
  244. uint16_t bColor, uint8_t iNum, uint16_t x, uint16_t y, uint16_t value) {
  245. size_t i = 0;
  246. DWIN_Byte(i, 0x14);
  247. // Bit 7: bshow
  248. // Bit 6: 1 = signed; 0 = unsigned number;
  249. // Bit 5: zeroFill
  250. // Bit 4: zeroMode
  251. // Bit 3-0: size
  252. DWIN_Byte(i, (bShow * 0x80) | (zeroFill * 0x20) | (zeroMode * 0x10) | size);
  253. DWIN_Word(i, color);
  254. DWIN_Word(i, bColor);
  255. DWIN_Byte(i, iNum);
  256. DWIN_Byte(i, 0); // fNum
  257. DWIN_Word(i, x);
  258. DWIN_Word(i, y);
  259. #if 0
  260. for (char count = 0; count < 8; count++) {
  261. DWIN_Byte(i, value);
  262. value >>= 8;
  263. if (!(value & 0xFF)) break;
  264. }
  265. #else
  266. // Write a big-endian 64 bit integer
  267. const size_t p = i + 1;
  268. for (char count = 8; count--;) { // 7..0
  269. ++i;
  270. DWIN_SendBuf[p + count] = value;
  271. value >>= 8;
  272. }
  273. #endif
  274. DWIN_Send(i);
  275. }
  276. // Draw a floating point number
  277. // bShow: true=display background color; false=don't display background color
  278. // zeroFill: true=zero fill; false=no zero fill
  279. // zeroMode: 1=leading 0 displayed as 0; 0=leading 0 displayed as a space
  280. // size: Font size
  281. // color: Character color
  282. // bColor: Background color
  283. // iNum: Number of whole digits
  284. // fNum: Number of decimal digits
  285. // x/y: Upper-left point
  286. // value: Float value
  287. void DWIN_Draw_FloatValue(uint8_t bShow, bool zeroFill, uint8_t zeroMode, uint8_t size, uint16_t color,
  288. uint16_t bColor, uint8_t iNum, uint8_t fNum, uint16_t x, uint16_t y, long value) {
  289. //uint8_t *fvalue = (uint8_t*)&value;
  290. size_t i = 0;
  291. DWIN_Byte(i, 0x14);
  292. DWIN_Byte(i, (bShow * 0x80) | (zeroFill * 0x20) | (zeroMode * 0x10) | size);
  293. DWIN_Word(i, color);
  294. DWIN_Word(i, bColor);
  295. DWIN_Byte(i, iNum);
  296. DWIN_Byte(i, fNum);
  297. DWIN_Word(i, x);
  298. DWIN_Word(i, y);
  299. DWIN_Long(i, value);
  300. /*
  301. DWIN_Byte(i, fvalue[3]);
  302. DWIN_Byte(i, fvalue[2]);
  303. DWIN_Byte(i, fvalue[1]);
  304. DWIN_Byte(i, fvalue[0]);
  305. */
  306. DWIN_Send(i);
  307. }
  308. /*---------------------------------------- Picture related functions ----------------------------------------*/
  309. // Draw JPG and cached in #0 virtual display area
  310. // id: Picture ID
  311. void DWIN_JPG_ShowAndCache(const uint8_t id) {
  312. size_t i = 0;
  313. DWIN_Word(i, 0x2200);
  314. DWIN_Byte(i, id);
  315. DWIN_Send(i); // AA 23 00 00 00 00 08 00 01 02 03 CC 33 C3 3C
  316. }
  317. // Draw an Icon
  318. // libID: Icon library ID
  319. // picID: Icon ID
  320. // x/y: Upper-left point
  321. void DWIN_ICON_Show(uint8_t libID, uint8_t picID, uint16_t x, uint16_t y) {
  322. NOMORE(x, DWIN_WIDTH - 1);
  323. NOMORE(y, DWIN_HEIGHT - 1); // -- ozy -- srl
  324. size_t i = 0;
  325. DWIN_Byte(i, 0x23);
  326. DWIN_Word(i, x);
  327. DWIN_Word(i, y);
  328. DWIN_Byte(i, 0x80 | libID);
  329. //DWIN_Byte(i, libID);
  330. DWIN_Byte(i, picID);
  331. DWIN_Send(i);
  332. }
  333. // Unzip the JPG picture to a virtual display area
  334. // n: Cache index
  335. // id: Picture ID
  336. void DWIN_JPG_CacheToN(uint8_t n, uint8_t id) {
  337. size_t i = 0;
  338. DWIN_Byte(i, 0x25);
  339. DWIN_Byte(i, n);
  340. DWIN_Byte(i, id);
  341. DWIN_Send(i);
  342. }
  343. // Copy area from virtual display area to current screen
  344. // cacheID: virtual area number
  345. // xStart/yStart: Upper-left of virtual area
  346. // xEnd/yEnd: Lower-right of virtual area
  347. // x/y: Screen paste point
  348. void DWIN_Frame_AreaCopy(uint8_t cacheID, uint16_t xStart, uint16_t yStart,
  349. uint16_t xEnd, uint16_t yEnd, uint16_t x, uint16_t y) {
  350. size_t i = 0;
  351. DWIN_Byte(i, 0x27);
  352. DWIN_Byte(i, 0x80 | cacheID);
  353. DWIN_Word(i, xStart);
  354. DWIN_Word(i, yStart);
  355. DWIN_Word(i, xEnd);
  356. DWIN_Word(i, yEnd);
  357. DWIN_Word(i, x);
  358. DWIN_Word(i, y);
  359. DWIN_Send(i);
  360. }
  361. // Animate a series of icons
  362. // animID: Animation ID; 0x00-0x0F
  363. // animate: true on; false off;
  364. // libID: Icon library ID
  365. // picIDs: Icon starting ID
  366. // picIDe: Icon ending ID
  367. // x/y: Upper-left point
  368. // interval: Display time interval, unit 10mS
  369. void DWIN_ICON_Animation(uint8_t animID, bool animate, uint8_t libID, uint8_t picIDs, uint8_t picIDe, uint16_t x, uint16_t y, uint16_t interval) {
  370. NOMORE(x, DWIN_WIDTH - 1);
  371. NOMORE(y, DWIN_HEIGHT - 1); // -- ozy -- srl
  372. size_t i = 0;
  373. DWIN_Byte(i, 0x28);
  374. DWIN_Word(i, x);
  375. DWIN_Word(i, y);
  376. // Bit 7: animation on or off
  377. // Bit 6: start from begin or end
  378. // Bit 5-4: unused (0)
  379. // Bit 3-0: animID
  380. DWIN_Byte(i, (animate * 0x80) | 0x40 | animID);
  381. DWIN_Byte(i, libID);
  382. DWIN_Byte(i, picIDs);
  383. DWIN_Byte(i, picIDe);
  384. DWIN_Byte(i, interval);
  385. DWIN_Send(i);
  386. }
  387. // Animation Control
  388. // state: 16 bits, each bit is the state of an animation id
  389. void DWIN_ICON_AnimationControl(uint16_t state) {
  390. size_t i = 0;
  391. DWIN_Byte(i, 0x29);
  392. DWIN_Word(i, state);
  393. DWIN_Send(i);
  394. }
  395. /*---------------------------------------- Memory functions ----------------------------------------*/
  396. // The LCD has an additional 32KB SRAM and 16KB Flash
  397. // Data can be written to the sram and save to one of the jpeg page files
  398. // Write Data Memory
  399. // command 0x31
  400. // Type: Write memory selection; 0x5A=SRAM; 0xA5=Flash
  401. // Address: Write data memory address; 0x000-0x7FFF for SRAM; 0x000-0x3FFF for Flash
  402. // Data: data
  403. //
  404. // Flash writing returns 0xA5 0x4F 0x4B
  405. // Read Data Memory
  406. // command 0x32
  407. // Type: Read memory selection; 0x5A=SRAM; 0xA5=Flash
  408. // Address: Read data memory address; 0x000-0x7FFF for SRAM; 0x000-0x3FFF for Flash
  409. // Length: leangth of data to read; 0x01-0xF0
  410. //
  411. // Response:
  412. // Type, Address, Length, Data
  413. // Write Picture Memory
  414. // Write the contents of the 32KB SRAM data memory into the designated image memory space
  415. // Issued: 0x5A, 0xA5, PIC_ID
  416. // Response: 0xA5 0x4F 0x4B
  417. //
  418. // command 0x33
  419. // 0x5A, 0xA5
  420. // PicId: Picture Memory location, 0x00-0x0F
  421. //
  422. // Flash writing returns 0xA5 0x4F 0x4B
  423. #endif // IS_DWIN_MARLINUI