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_lcd.cpp 9.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  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 <http://www.gnu.org/licenses/>.
  20. *
  21. */
  22. /********************************************************************************
  23. * @file dwin_lcd.c
  24. * @author LEO / Creality3D
  25. * @date 2019/07/18
  26. * @version 2.0.1
  27. * @brief 迪文屏控制操作函数
  28. ********************************************************************************/
  29. #include "../../inc/MarlinConfigPre.h"
  30. #if ENABLED(DWIN_CREALITY_LCD)
  31. #include "../../inc/MarlinConfig.h"
  32. #include "dwin_lcd.h"
  33. #include <string.h> // for memset
  34. // Make sure DWIN_SendBuf is large enough to hold the largest
  35. // printed string plus the draw command and tail.
  36. uint8_t DWIN_SendBuf[11 + 24] = { 0xAA };
  37. uint8_t DWIN_BufTail[4] = { 0xCC, 0x33, 0xC3, 0x3C };
  38. uint8_t databuf[26] = { 0 };
  39. uint8_t receivedType;
  40. int recnum = 0;
  41. inline void DWIN_Byte(size_t &i, const uint16_t bval) {
  42. DWIN_SendBuf[++i] = bval;
  43. }
  44. inline void DWIN_Word(size_t &i, const uint16_t wval) {
  45. DWIN_SendBuf[++i] = wval >> 8;
  46. DWIN_SendBuf[++i] = wval & 0xFF;
  47. }
  48. inline void DWIN_Long(size_t &i, const uint32_t lval) {
  49. DWIN_SendBuf[++i] = (lval >> 24) & 0xFF;
  50. DWIN_SendBuf[++i] = (lval >> 16) & 0xFF;
  51. DWIN_SendBuf[++i] = (lval >> 8) & 0xFF;
  52. DWIN_SendBuf[++i] = lval & 0xFF;
  53. }
  54. inline void DWIN_String(size_t &i, char * const string) {
  55. const size_t len = strlen(string);
  56. memcpy(&DWIN_SendBuf[i+1], string, len);
  57. i += len;
  58. }
  59. /*发送当前BUF中的数据以及包尾数据 len:整包数据长度*/
  60. inline void DWIN_Send(size_t &i) {
  61. ++i;
  62. LOOP_L_N(n, i) { MYSERIAL1.write(DWIN_SendBuf[n]);
  63. delayMicroseconds(1); }
  64. LOOP_L_N(n, 4) { MYSERIAL1.write(DWIN_BufTail[n]);
  65. delayMicroseconds(1); }
  66. }
  67. /*----------------------------------------------系统变量函数----------------------------------------------*/
  68. /*握手 1: 握手成功 2: 握手失败*/
  69. bool DWIN_Handshake(void) {
  70. size_t i = 0;
  71. DWIN_Byte(i, 0x00);
  72. DWIN_Send(i);
  73. while (MYSERIAL1.available() > 0 && recnum < (signed)sizeof(databuf)) {
  74. databuf[recnum] = MYSERIAL1.read();
  75. // ignore the invalid data
  76. if (databuf[0] != FHONE) { // prevent the program from running.
  77. if (recnum > 0) {
  78. recnum = 0;
  79. ZERO(databuf);
  80. }
  81. continue;
  82. }
  83. delay(10);
  84. recnum++;
  85. }
  86. return ( recnum >= 3
  87. && databuf[0] == FHONE
  88. && databuf[1] == '\0'
  89. && databuf[2] == 'O'
  90. && databuf[3] == 'K' );
  91. }
  92. /*设定背光亮度 luminance:亮度(0x00~0xFF)*/
  93. void DWIN_Backlight_SetLuminance(const uint8_t luminance) {
  94. size_t i = 0;
  95. DWIN_Byte(i, 0x30);
  96. DWIN_Byte(i, _MAX(luminance, 0x1F));
  97. DWIN_Send(i);
  98. }
  99. /*设定画面显示方向 dir:0,0°; 1,90°; 2,180°; 3,270°*/
  100. void DWIN_Frame_SetDir(uint8_t dir) {
  101. size_t i = 0;
  102. DWIN_Byte(i, 0x34);
  103. DWIN_Byte(i, 0x5A);
  104. DWIN_Byte(i, 0xA5);
  105. DWIN_Byte(i, dir);
  106. DWIN_Send(i);
  107. }
  108. /*更新显示*/
  109. void DWIN_UpdateLCD(void) {
  110. size_t i = 0;
  111. DWIN_Byte(i, 0x3D);
  112. DWIN_Send(i);
  113. }
  114. /*----------------------------------------------绘图相关函数----------------------------------------------*/
  115. /*画面清屏 color:清屏颜色*/
  116. void DWIN_Frame_Clear(const uint16_t color) {
  117. size_t i = 0;
  118. DWIN_Byte(i, 0x01);
  119. DWIN_Word(i, color);
  120. DWIN_Send(i);
  121. }
  122. /*画面画线 color:线段颜色 xStart:X起始坐标 yStart:Y起始坐标 xEnd:X终止坐标 yEnd:Y终止坐标*/
  123. void DWIN_Draw_Line(uint16_t color, uint16_t xStart, uint16_t yStart, uint16_t xEnd, uint16_t yEnd) {
  124. size_t i = 0;
  125. DWIN_Byte(i, 0x03);
  126. DWIN_Word(i, color);
  127. DWIN_Word(i, xStart);
  128. DWIN_Word(i, yStart);
  129. DWIN_Word(i, xEnd);
  130. DWIN_Word(i, yEnd);
  131. DWIN_Send(i);
  132. }
  133. /*画面画矩形 mode:0,外框;1,填充;2,异或填充 color:颜色 xStart/yStart:矩形左上坐标 xEnd/yEnd:矩形右下坐标*/
  134. void DWIN_Draw_Rectangle(uint8_t mode, uint16_t color,
  135. uint16_t xStart, uint16_t yStart, uint16_t xEnd, uint16_t yEnd) {
  136. size_t i = 0;
  137. DWIN_Byte(i, 0x05);
  138. DWIN_Byte(i, mode);
  139. DWIN_Word(i, color);
  140. DWIN_Word(i, xStart);
  141. DWIN_Word(i, yStart);
  142. DWIN_Word(i, xEnd);
  143. DWIN_Word(i, yEnd);
  144. DWIN_Send(i);
  145. }
  146. /*画面区域移动 mode:0,环移;1,平移 dir:0,向左移动;1,向右移动;2,向上移动;3,向下移动 dis:移动距离
  147. color:填充颜色 xStart/yStart:选定区域左上坐标 xEnd/yEnd:选定区域右下坐标*/
  148. void DWIN_Frame_AreaMove(uint8_t mode, uint8_t dir, uint16_t dis,
  149. uint16_t color, uint16_t xStart, uint16_t yStart, uint16_t xEnd, uint16_t yEnd) {
  150. size_t i = 0;
  151. DWIN_Byte(i, 0x09);
  152. DWIN_Byte(i, (mode << 7) | dir);
  153. DWIN_Word(i, dis);
  154. DWIN_Word(i, color);
  155. DWIN_Word(i, xStart);
  156. DWIN_Word(i, yStart);
  157. DWIN_Word(i, xEnd);
  158. DWIN_Word(i, yEnd);
  159. DWIN_Send(i);
  160. }
  161. /*----------------------------------------------文本相关函数----------------------------------------------*/
  162. /*画面显示字符串 widthAdjust:true,自调整字符宽度;false,不调整字符宽度 bShow:true,显示背景色;false,不显示背景色 size:字号大小
  163. color:字符颜色 bColor:背景颜色 x/y:字符串左上坐标 *string:字符串*/
  164. void DWIN_Draw_String(bool widthAdjust, bool bShow, uint8_t size,
  165. uint16_t color, uint16_t bColor, uint16_t x, uint16_t y, char *string) {
  166. size_t i = 0;
  167. DWIN_Byte(i, 0x11);
  168. DWIN_Byte(i, (widthAdjust? 0x80:0x00) | (bShow? 0x40:0x00) | size);
  169. DWIN_Word(i, color);
  170. DWIN_Word(i, bColor);
  171. DWIN_Word(i, x);
  172. DWIN_Word(i, y);
  173. DWIN_String(i, string);
  174. DWIN_Send(i);
  175. }
  176. /*画面显示正整数 bShow:true,显示背景色;false,不显示背景色 zeroFill:true,补零;false,不补零 zeroMode:1,无效0显示为0; 0,无效0显示为空格 size:字号大小
  177. color:字符颜色 bColor:背景颜色 iNum:位数 x/y:变量左上坐标 value:整型变量*/
  178. void DWIN_Draw_IntValue(uint8_t bShow, bool zeroFill, uint8_t zeroMode, uint8_t size, uint16_t color,
  179. uint16_t bColor, uint8_t iNum, uint16_t x, uint16_t y, uint16_t value) {
  180. size_t i = 0;
  181. DWIN_Byte(i, 0x14);
  182. DWIN_Byte(i, (bShow? 0x80:0x00) | (zeroFill? 0x20:0x00) | (zeroMode? 0x10:0x00) | size);
  183. DWIN_Word(i, color);
  184. DWIN_Word(i, bColor);
  185. DWIN_Byte(i, iNum);
  186. DWIN_Byte(i, 0); // fNum
  187. DWIN_Word(i, x);
  188. DWIN_Word(i, y);
  189. #if 0
  190. for (char count = 0; count < 8; count++) {
  191. DWIN_Byte(i, value);
  192. value >>= 8;
  193. if ((value&0xFF) == 0x00) break;
  194. }
  195. #else
  196. // Write a big-endian 64 bit integer
  197. const size_t p = i + 1;
  198. for (char count = 8; count--;) { // 7..0
  199. ++i;
  200. DWIN_SendBuf[p + count] = value;
  201. value >>= 8;
  202. }
  203. #endif
  204. DWIN_Send(i);
  205. }
  206. /*画面显示浮点数 bShow:true,显示背景色;false,不显示背景色 zeroFill:true,补零;false,不补零 zeroMode:1,无效0显示为0; 0,无效0显示为空格 size:字号大小
  207. color:字符颜色 bColor:背景颜色 iNum:整数位数 fNum:小数位数 x/y:变量左上坐标 value:浮点数变量*/
  208. void DWIN_Draw_FloatValue(uint8_t bShow, bool zeroFill, uint8_t zeroMode, uint8_t size, uint16_t color,
  209. uint16_t bColor, uint8_t iNum, uint8_t fNum, uint16_t x, uint16_t y, long value) {
  210. //uint8_t *fvalue = (uint8_t*)&value;
  211. size_t i = 0;
  212. DWIN_Byte(i, 0x14);
  213. DWIN_Byte(i, (bShow? 0x80:0x00) | (zeroFill? 0x20:0x00) | (zeroMode? 0x10:0x00) | size);
  214. DWIN_Word(i, color);
  215. DWIN_Word(i, bColor);
  216. DWIN_Byte(i, iNum);
  217. DWIN_Byte(i, fNum);
  218. DWIN_Word(i, x);
  219. DWIN_Word(i, y);
  220. DWIN_Long(i, value);
  221. /*
  222. DWIN_Byte(i, fvalue[3]);
  223. DWIN_Byte(i, fvalue[2]);
  224. DWIN_Byte(i, fvalue[1]);
  225. DWIN_Byte(i, fvalue[0]);
  226. */
  227. DWIN_Send(i);
  228. }
  229. /*----------------------------------------------图片相关函数----------------------------------------------*/
  230. /*jpg图片显示并缓存在#0虚拟显示区 id:图片ID*/
  231. void DWIN_JPG_ShowAndCache(const uint8_t id) {
  232. size_t i = 0;
  233. DWIN_Word(i, 0x2200);
  234. DWIN_Byte(i, id);
  235. DWIN_Send(i); //AA 23 00 00 00 00 08 00 01 02 03 CC 33 C3 3C
  236. }
  237. /*图标显示 libID:图标库ID picID:图标ID x/y:图标左上坐标*/
  238. void DWIN_ICON_Show(uint8_t libID, uint8_t picID, uint16_t x, uint16_t y) {
  239. NOMORE(x, DWIN_WIDTH - 1);
  240. NOMORE(y, DWIN_HEIGHT - 1); // -- ozy -- srl
  241. size_t i = 0;
  242. DWIN_Byte(i, 0x23);
  243. DWIN_Word(i, x);
  244. DWIN_Word(i, y);
  245. DWIN_Byte(i, 0x80 | libID);
  246. DWIN_Byte(i, picID);
  247. DWIN_Send(i);
  248. }
  249. /*jpg图片解压到#1虚拟显示区 id:图片ID*/
  250. void DWIN_JPG_CacheToN(uint8_t n, uint8_t id) {
  251. size_t i = 0;
  252. DWIN_Byte(i, 0x25);
  253. DWIN_Byte(i, n);
  254. DWIN_Byte(i, id);
  255. DWIN_Send(i);
  256. }
  257. /*从虚拟显示区复制区域至当前画面 cacheID:虚拟区号 xStart/yStart:虚拟区左上坐标 xEnd/yEnd:虚拟区右下坐标 x/y:当前画面粘贴坐标*/
  258. void DWIN_Frame_AreaCopy(uint8_t cacheID, uint16_t xStart, uint16_t yStart,
  259. uint16_t xEnd, uint16_t yEnd, uint16_t x, uint16_t y) {
  260. size_t i = 0;
  261. DWIN_Byte(i, 0x27);
  262. DWIN_Byte(i, 0x80 | cacheID);
  263. DWIN_Word(i, xStart);
  264. DWIN_Word(i, yStart);
  265. DWIN_Word(i, xEnd);
  266. DWIN_Word(i, yEnd);
  267. DWIN_Word(i, x);
  268. DWIN_Word(i, y);
  269. DWIN_Send(i);
  270. }
  271. #endif // DWIN_CREALITY_LCD