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

dwin_api.cpp 13KB

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