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.

dwin_api.cpp 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471
  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. #include "../../../inc/MarlinConfigPre.h"
  23. #if HAS_DWIN_E3V2 || IS_DWIN_MARLINUI
  24. #include "dwin_api.h"
  25. #include "dwin_set.h"
  26. #include "dwin_font.h"
  27. #include "../../../inc/MarlinConfig.h"
  28. #include <string.h> // for memset
  29. uint8_t DWIN_SendBuf[11 + DWIN_WIDTH / 6 * 2] = { 0xAA };
  30. uint8_t DWIN_BufTail[4] = { 0xCC, 0x33, 0xC3, 0x3C };
  31. uint8_t databuf[26] = { 0 };
  32. // Send the data in the buffer plus the packet tail
  33. void DWIN_Send(size_t &i) {
  34. ++i;
  35. LOOP_L_N(n, i) { LCD_SERIAL.write(DWIN_SendBuf[n]); delayMicroseconds(1); }
  36. LOOP_L_N(n, 4) { LCD_SERIAL.write(DWIN_BufTail[n]); delayMicroseconds(1); }
  37. }
  38. /*-------------------------------------- System variable function --------------------------------------*/
  39. // Handshake (1: Success, 0: Fail)
  40. bool DWIN_Handshake() {
  41. static int recnum = 0;
  42. #ifndef LCD_BAUDRATE
  43. #define LCD_BAUDRATE 115200
  44. #endif
  45. LCD_SERIAL.begin(LCD_BAUDRATE);
  46. const millis_t serial_connect_timeout = millis() + 1000UL;
  47. while (!LCD_SERIAL.connected() && PENDING(millis(), serial_connect_timeout)) { /*nada*/ }
  48. size_t i = 0;
  49. DWIN_Byte(i, 0x00);
  50. DWIN_Send(i);
  51. while (LCD_SERIAL.available() > 0 && recnum < (signed)sizeof(databuf)) {
  52. databuf[recnum] = LCD_SERIAL.read();
  53. // ignore the invalid data
  54. if (databuf[0] != FHONE) { // prevent the program from running.
  55. if (recnum > 0) {
  56. recnum = 0;
  57. ZERO(databuf);
  58. }
  59. continue;
  60. }
  61. delay(10);
  62. recnum++;
  63. }
  64. return ( recnum >= 3
  65. && databuf[0] == FHONE
  66. && databuf[1] == '\0'
  67. && databuf[2] == 'O'
  68. && databuf[3] == 'K' );
  69. }
  70. #if HAS_LCD_BRIGHTNESS
  71. // Set LCD backlight (from DWIN Enhanced)
  72. // brightness: 0x00-0xFF
  73. void DWIN_LCD_Brightness(const uint8_t brightness) {
  74. size_t i = 0;
  75. DWIN_Byte(i, 0x30);
  76. DWIN_Byte(i, brightness);
  77. DWIN_Send(i);
  78. }
  79. #endif
  80. // Get font character width
  81. uint8_t fontWidth(uint8_t cfont) {
  82. switch (cfont) {
  83. case font6x12 : return 6;
  84. case font8x16 : return 8;
  85. case font10x20: return 10;
  86. case font12x24: return 12;
  87. case font14x28: return 14;
  88. case font16x32: return 16;
  89. case font20x40: return 20;
  90. case font24x48: return 24;
  91. case font28x56: return 28;
  92. case font32x64: return 32;
  93. default: return 0;
  94. }
  95. }
  96. // Get font character height
  97. uint8_t fontHeight(uint8_t cfont) {
  98. switch (cfont) {
  99. case font6x12 : return 12;
  100. case font8x16 : return 16;
  101. case font10x20: return 20;
  102. case font12x24: return 24;
  103. case font14x28: return 28;
  104. case font16x32: return 32;
  105. case font20x40: return 40;
  106. case font24x48: return 48;
  107. case font28x56: return 56;
  108. case font32x64: return 64;
  109. default: return 0;
  110. }
  111. }
  112. // Set screen display direction
  113. // dir: 0=0°, 1=90°, 2=180°, 3=270°
  114. void DWIN_Frame_SetDir(uint8_t dir) {
  115. size_t i = 0;
  116. DWIN_Byte(i, 0x34);
  117. DWIN_Byte(i, 0x5A);
  118. DWIN_Byte(i, 0xA5);
  119. DWIN_Byte(i, dir);
  120. DWIN_Send(i);
  121. }
  122. // Update display
  123. void DWIN_UpdateLCD() {
  124. size_t i = 0;
  125. DWIN_Byte(i, 0x3D);
  126. DWIN_Send(i);
  127. }
  128. /*---------------------------------------- Drawing functions ----------------------------------------*/
  129. // Clear screen
  130. // color: Clear screen color
  131. void DWIN_Frame_Clear(const uint16_t color) {
  132. size_t i = 0;
  133. DWIN_Byte(i, 0x01);
  134. DWIN_Word(i, color);
  135. DWIN_Send(i);
  136. }
  137. // Draw a point
  138. // color: point color
  139. // width: point width 0x01-0x0F
  140. // height: point height 0x01-0x0F
  141. // x,y: upper left point
  142. void DWIN_Draw_Point(uint16_t color, uint8_t width, uint8_t height, uint16_t x, uint16_t y) {
  143. size_t i = 0;
  144. DWIN_Byte(i, 0x02);
  145. DWIN_Word(i, color);
  146. DWIN_Byte(i, width);
  147. DWIN_Byte(i, height);
  148. DWIN_Word(i, x);
  149. DWIN_Word(i, y);
  150. DWIN_Send(i);
  151. }
  152. // Draw a line
  153. // color: Line segment color
  154. // xStart/yStart: Start point
  155. // xEnd/yEnd: End point
  156. void DWIN_Draw_Line(uint16_t color, uint16_t xStart, uint16_t yStart, uint16_t xEnd, uint16_t yEnd) {
  157. size_t i = 0;
  158. DWIN_Byte(i, 0x03);
  159. DWIN_Word(i, color);
  160. DWIN_Word(i, xStart);
  161. DWIN_Word(i, yStart);
  162. DWIN_Word(i, xEnd);
  163. DWIN_Word(i, yEnd);
  164. DWIN_Send(i);
  165. }
  166. // Draw a rectangle
  167. // mode: 0=frame, 1=fill, 2=XOR fill
  168. // color: Rectangle color
  169. // xStart/yStart: upper left point
  170. // xEnd/yEnd: lower right point
  171. void DWIN_Draw_Rectangle(uint8_t mode, uint16_t color, uint16_t xStart, uint16_t yStart, uint16_t xEnd, uint16_t yEnd) {
  172. size_t i = 0;
  173. DWIN_Byte(i, 0x05);
  174. DWIN_Byte(i, mode);
  175. DWIN_Word(i, color);
  176. DWIN_Word(i, xStart);
  177. DWIN_Word(i, yStart);
  178. DWIN_Word(i, xEnd);
  179. DWIN_Word(i, yEnd);
  180. DWIN_Send(i);
  181. }
  182. // Move a screen area
  183. // mode: 0, circle shift; 1, translation
  184. // dir: 0=left, 1=right, 2=up, 3=down
  185. // dis: Distance
  186. // color: Fill color
  187. // xStart/yStart: upper left point
  188. // xEnd/yEnd: bottom right point
  189. void DWIN_Frame_AreaMove(uint8_t mode, uint8_t dir, uint16_t dis,
  190. uint16_t color, uint16_t xStart, uint16_t yStart, uint16_t xEnd, uint16_t yEnd) {
  191. size_t i = 0;
  192. DWIN_Byte(i, 0x09);
  193. DWIN_Byte(i, (mode << 7) | dir);
  194. DWIN_Word(i, dis);
  195. DWIN_Word(i, color);
  196. DWIN_Word(i, xStart);
  197. DWIN_Word(i, yStart);
  198. DWIN_Word(i, xEnd);
  199. DWIN_Word(i, yEnd);
  200. DWIN_Send(i);
  201. }
  202. /*---------------------------------------- Text related functions ----------------------------------------*/
  203. // Draw a string
  204. // widthAdjust: true=self-adjust character width; false=no adjustment
  205. // bShow: true=display background color; false=don't display background color
  206. // size: Font size
  207. // color: Character color
  208. // bColor: Background color
  209. // x/y: Upper-left coordinate of the string
  210. // *string: The string
  211. // rlimit: To limit the drawn string length
  212. void DWIN_Draw_String(bool bShow, uint8_t size, uint16_t color, uint16_t bColor, uint16_t x, uint16_t y, const char * const string, uint16_t rlimit/*=0xFFFF*/) {
  213. #if DISABLED(DWIN_LCD_PROUI)
  214. DWIN_Draw_Rectangle(1, bColor, x, y, x + (fontWidth(size) * strlen_P(string)), y + fontHeight(size));
  215. #endif
  216. constexpr uint8_t widthAdjust = 0;
  217. size_t i = 0;
  218. DWIN_Byte(i, 0x11);
  219. // Bit 7: widthAdjust
  220. // Bit 6: bShow
  221. // Bit 5-4: Unused (0)
  222. // Bit 3-0: size
  223. DWIN_Byte(i, (widthAdjust * 0x80) | (bShow * 0x40) | size);
  224. DWIN_Word(i, color);
  225. DWIN_Word(i, bColor);
  226. DWIN_Word(i, x);
  227. DWIN_Word(i, y);
  228. DWIN_Text(i, string, rlimit);
  229. DWIN_Send(i);
  230. }
  231. // Draw a positive integer
  232. // bShow: true=display background color; false=don't display background color
  233. // zeroFill: true=zero fill; false=no zero fill
  234. // zeroMode: 1=leading 0 displayed as 0; 0=leading 0 displayed as a space
  235. // size: Font size
  236. // color: Character color
  237. // bColor: Background color
  238. // iNum: Number of digits
  239. // x/y: Upper-left coordinate
  240. // value: Integer value
  241. void DWIN_Draw_IntValue(uint8_t bShow, bool zeroFill, uint8_t zeroMode, uint8_t size, uint16_t color,
  242. uint16_t bColor, uint8_t iNum, uint16_t x, uint16_t y, uint32_t value) {
  243. size_t i = 0;
  244. DWIN_Draw_Rectangle(1, bColor, x, y, x + fontWidth(size) * iNum + 1, y + fontHeight(size));
  245. DWIN_Byte(i, 0x14);
  246. // Bit 7: bshow
  247. // Bit 6: 1 = signed; 0 = unsigned number;
  248. // Bit 5: zeroFill
  249. // Bit 4: zeroMode
  250. // Bit 3-0: size
  251. DWIN_Byte(i, (bShow * 0x80) | (zeroFill * 0x20) | (zeroMode * 0x10) | size);
  252. DWIN_Word(i, color);
  253. DWIN_Word(i, bColor);
  254. DWIN_Byte(i, iNum);
  255. DWIN_Byte(i, 0); // fNum
  256. DWIN_Word(i, x);
  257. DWIN_Word(i, y);
  258. #if 0
  259. for (char count = 0; count < 8; count++) {
  260. DWIN_Byte(i, value);
  261. value >>= 8;
  262. if (!(value & 0xFF)) break;
  263. }
  264. #else
  265. // Write a big-endian 64 bit integer
  266. const size_t p = i + 1;
  267. for (char count = 8; count--;) { // 7..0
  268. ++i;
  269. DWIN_SendBuf[p + count] = value;
  270. value >>= 8;
  271. }
  272. #endif
  273. DWIN_Send(i);
  274. }
  275. // Draw a floating point number
  276. // bShow: true=display background color; false=don't display background color
  277. // zeroFill: true=zero fill; false=no zero fill
  278. // zeroMode: 1=leading 0 displayed as 0; 0=leading 0 displayed as a space
  279. // size: Font size
  280. // color: Character color
  281. // bColor: Background color
  282. // iNum: Number of whole digits
  283. // fNum: Number of decimal digits
  284. // x/y: Upper-left point
  285. // value: Float value
  286. void DWIN_Draw_FloatValue(uint8_t bShow, bool zeroFill, uint8_t zeroMode, uint8_t size, uint16_t color,
  287. uint16_t bColor, uint8_t iNum, uint8_t fNum, uint16_t x, uint16_t y, int32_t value) {
  288. //uint8_t *fvalue = (uint8_t*)&value;
  289. size_t i = 0;
  290. DWIN_Draw_Rectangle(1, bColor, x, y, x + fontWidth(size) * (iNum+fNum+1), y + fontHeight(size));
  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. // Draw a floating point number
  309. // value: positive unscaled float value
  310. void DWIN_Draw_FloatValue(uint8_t bShow, bool zeroFill, uint8_t zeroMode, uint8_t size, uint16_t color,
  311. uint16_t bColor, uint8_t iNum, uint8_t fNum, uint16_t x, uint16_t y, float value) {
  312. const int32_t val = round(value * POW(10, fNum));
  313. DWIN_Draw_FloatValue(bShow, zeroFill, zeroMode, size, color, bColor, iNum, fNum, x, y, val);
  314. }
  315. /*---------------------------------------- Picture related functions ----------------------------------------*/
  316. // Draw JPG and cached in #0 virtual display area
  317. // id: Picture ID
  318. void DWIN_JPG_ShowAndCache(const uint8_t id) {
  319. size_t i = 0;
  320. DWIN_Word(i, 0x2200);
  321. DWIN_Byte(i, id);
  322. DWIN_Send(i); // AA 23 00 00 00 00 08 00 01 02 03 CC 33 C3 3C
  323. }
  324. // Draw an Icon
  325. // IBD: The icon background display: 0=Background filtering is not displayed, 1=Background display \\When setting the background filtering not to display, the background must be pure black
  326. // BIR: Background image restoration: 0=Background image is not restored, 1=Automatically use virtual display area image for background restoration
  327. // BFI: Background filtering strength: 0=normal, 1=enhanced, (only valid when the icon background display=0)
  328. // libID: Icon library ID
  329. // picID: Icon ID
  330. // x/y: Upper-left point
  331. void DWIN_ICON_Show(bool IBD, bool BIR, bool BFI, uint8_t libID, uint8_t picID, uint16_t x, uint16_t y) {
  332. NOMORE(x, DWIN_WIDTH - 1);
  333. NOMORE(y, DWIN_HEIGHT - 1); // -- ozy -- srl
  334. size_t i = 0;
  335. DWIN_Byte(i, 0x23);
  336. DWIN_Word(i, x);
  337. DWIN_Word(i, y);
  338. DWIN_Byte(i, (IBD << 7) | (BIR << 6) | (BFI << 5) | libID);
  339. DWIN_Byte(i, picID);
  340. DWIN_Send(i);
  341. }
  342. // Draw an Icon from SRAM
  343. // IBD: The icon background display: 0=Background filtering is not displayed, 1=Background display \\When setting the background filtering not to display, the background must be pure black
  344. // BIR: Background image restoration: 0=Background image is not restored, 1=Automatically use virtual display area image for background restoration
  345. // BFI: Background filtering strength: 0=normal, 1=enhanced, (only valid when the icon background display=0)
  346. // x/y: Upper-left point
  347. // addr: SRAM address
  348. void DWIN_ICON_Show(bool IBD, bool BIR, bool BFI, uint16_t x, uint16_t y, uint16_t addr) {
  349. NOMORE(x, DWIN_WIDTH - 1);
  350. NOMORE(y, DWIN_HEIGHT - 1); // -- ozy -- srl
  351. size_t i = 0;
  352. DWIN_Byte(i, 0x24);
  353. DWIN_Word(i, x);
  354. DWIN_Word(i, y);
  355. DWIN_Byte(i, (IBD << 7) | (BIR << 6) | (BFI << 5) | 0x00);
  356. DWIN_Word(i, addr);
  357. DWIN_Send(i);
  358. }
  359. // Unzip the JPG picture to a virtual display area
  360. // n: Cache index
  361. // id: Picture ID
  362. void DWIN_JPG_CacheToN(uint8_t n, uint8_t id) {
  363. size_t i = 0;
  364. DWIN_Byte(i, 0x25);
  365. DWIN_Byte(i, n);
  366. DWIN_Byte(i, id);
  367. DWIN_Send(i);
  368. }
  369. // Animate a series of icons
  370. // animID: Animation ID; 0x00-0x0F
  371. // animate: true on; false off;
  372. // libID: Icon library ID
  373. // picIDs: Icon starting ID
  374. // picIDe: Icon ending ID
  375. // x/y: Upper-left point
  376. // interval: Display time interval, unit 10mS
  377. 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) {
  378. NOMORE(x, DWIN_WIDTH - 1);
  379. NOMORE(y, DWIN_HEIGHT - 1); // -- ozy -- srl
  380. size_t i = 0;
  381. DWIN_Byte(i, 0x28);
  382. DWIN_Word(i, x);
  383. DWIN_Word(i, y);
  384. // Bit 7: animation on or off
  385. // Bit 6: start from begin or end
  386. // Bit 5-4: unused (0)
  387. // Bit 3-0: animID
  388. DWIN_Byte(i, (animate * 0x80) | 0x40 | animID);
  389. DWIN_Byte(i, libID);
  390. DWIN_Byte(i, picIDs);
  391. DWIN_Byte(i, picIDe);
  392. DWIN_Byte(i, interval);
  393. DWIN_Send(i);
  394. }
  395. // Animation Control
  396. // state: 16 bits, each bit is the state of an animation id
  397. void DWIN_ICON_AnimationControl(uint16_t state) {
  398. size_t i = 0;
  399. DWIN_Byte(i, 0x29);
  400. DWIN_Word(i, state);
  401. DWIN_Send(i);
  402. }
  403. /*---------------------------------------- Memory functions ----------------------------------------*/
  404. // The LCD has an additional 32KB SRAM and 16KB Flash
  405. // Data can be written to the SRAM and saved to one of the jpeg page files
  406. // Write Data Memory
  407. // command 0x31
  408. // Type: Write memory selection; 0x5A=SRAM; 0xA5=Flash
  409. // Address: Write data memory address; 0x000-0x7FFF for SRAM; 0x000-0x3FFF for Flash
  410. // Data: data
  411. //
  412. // Flash writing returns 0xA5 0x4F 0x4B
  413. // Read Data Memory
  414. // command 0x32
  415. // Type: Read memory selection; 0x5A=SRAM; 0xA5=Flash
  416. // Address: Read data memory address; 0x000-0x7FFF for SRAM; 0x000-0x3FFF for Flash
  417. // Length: leangth of data to read; 0x01-0xF0
  418. //
  419. // Response:
  420. // Type, Address, Length, Data
  421. // Write Picture Memory
  422. // Write the contents of the 32KB SRAM data memory into the designated image memory space
  423. // Issued: 0x5A, 0xA5, PIC_ID
  424. // Response: 0xA5 0x4F 0x4B
  425. //
  426. // command 0x33
  427. // 0x5A, 0xA5
  428. // PicId: Picture Memory location, 0x00-0x0F
  429. //
  430. // Flash writing returns 0xA5 0x4F 0x4B
  431. #endif // HAS_DWIN_E3V2 || IS_DWIN_MARLINUI