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.

dwinui.cpp 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  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. * DWIN Enhanced implementation for PRO UI
  24. * Author: Miguel A. Risco-Castillo (MRISCOC)
  25. * Version: 3.17.1
  26. * Date: 2022/04/12
  27. */
  28. #include "../../../inc/MarlinConfigPre.h"
  29. #if ENABLED(DWIN_LCD_PROUI)
  30. #include "../../../inc/MarlinConfig.h"
  31. #include "dwin_lcd.h"
  32. #include "dwinui.h"
  33. #include "dwin_defines.h"
  34. //#define DEBUG_OUT 1
  35. #include "../../../core/debug_out.h"
  36. xy_int_t DWINUI::cursor = { 0 };
  37. uint16_t DWINUI::pencolor = Color_White;
  38. uint16_t DWINUI::textcolor = Def_Text_Color;
  39. uint16_t DWINUI::backcolor = Def_Background_Color;
  40. uint16_t DWINUI::buttoncolor = Def_Button_Color;
  41. uint8_t DWINUI::font = font8x16;
  42. FSTR_P const DWINUI::Author = F(STRING_CONFIG_H_AUTHOR);
  43. void (*DWINUI::onTitleDraw)(TitleClass* title) = nullptr;
  44. void DWINUI::init() {
  45. delay(750); // Delay for wait to wakeup screen
  46. const bool hs = DWIN_Handshake(); UNUSED(hs);
  47. #if ENABLED(DEBUG_DWIN)
  48. SERIAL_ECHOPGM("DWIN_Handshake ");
  49. SERIAL_ECHOLNF(hs ? F("ok.") : F("error."));
  50. #endif
  51. DWIN_Frame_SetDir(1);
  52. cursor.reset();
  53. pencolor = Color_White;
  54. textcolor = Def_Text_Color;
  55. backcolor = Def_Background_Color;
  56. buttoncolor = Def_Button_Color;
  57. font = font8x16;
  58. }
  59. // Set text/number font
  60. void DWINUI::setFont(uint8_t cfont) {
  61. font = cfont;
  62. }
  63. // Get font character width
  64. uint8_t DWINUI::fontWidth(uint8_t cfont) {
  65. switch (cfont) {
  66. case font6x12 : return 6;
  67. case font8x16 : return 8;
  68. case font10x20: return 10;
  69. case font12x24: return 12;
  70. case font14x28: return 14;
  71. case font16x32: return 16;
  72. case font20x40: return 20;
  73. case font24x48: return 24;
  74. case font28x56: return 28;
  75. case font32x64: return 32;
  76. default: return 0;
  77. }
  78. }
  79. // Get font character height
  80. uint8_t DWINUI::fontHeight(uint8_t cfont) {
  81. switch (cfont) {
  82. case font6x12 : return 12;
  83. case font8x16 : return 16;
  84. case font10x20: return 20;
  85. case font12x24: return 24;
  86. case font14x28: return 28;
  87. case font16x32: return 32;
  88. case font20x40: return 40;
  89. case font24x48: return 48;
  90. case font28x56: return 56;
  91. case font32x64: return 64;
  92. default: return 0;
  93. }
  94. }
  95. // Get screen x coordinates from text column
  96. uint16_t DWINUI::ColToX(uint8_t col) {
  97. return col * fontWidth(font);
  98. }
  99. // Get screen y coordinates from text row
  100. uint16_t DWINUI::RowToY(uint8_t row) {
  101. return row * fontHeight(font);
  102. }
  103. // Set text/number color
  104. void DWINUI::SetColors(uint16_t fgcolor, uint16_t bgcolor, uint16_t alcolor) {
  105. textcolor = fgcolor;
  106. backcolor = bgcolor;
  107. buttoncolor = alcolor;
  108. }
  109. void DWINUI::SetTextColor(uint16_t fgcolor) {
  110. textcolor = fgcolor;
  111. }
  112. void DWINUI::SetBackgroundColor(uint16_t bgcolor) {
  113. backcolor = bgcolor;
  114. }
  115. // Moves cursor to point
  116. // x: abscissa of the display
  117. // y: ordinate of the display
  118. // point: xy coordinate
  119. void DWINUI::MoveTo(int16_t x, int16_t y) {
  120. cursor.x = x;
  121. cursor.y = y;
  122. }
  123. void DWINUI::MoveTo(xy_int_t point) {
  124. cursor = point;
  125. }
  126. // Moves cursor relative to the actual position
  127. // x: abscissa of the display
  128. // y: ordinate of the display
  129. // point: xy coordinate
  130. void DWINUI::MoveBy(int16_t x, int16_t y) {
  131. cursor.x += x;
  132. cursor.y += y;
  133. }
  134. void DWINUI::MoveBy(xy_int_t point) {
  135. cursor += point;
  136. }
  137. // Draw a Centered string using arbitrary x1 and x2 margins
  138. void DWINUI::Draw_CenteredString(bool bShow, uint8_t size, uint16_t color, uint16_t bColor, uint16_t x1, uint16_t x2, uint16_t y, const char * const string) {
  139. const uint16_t x = _MAX(0U, x2 + x1 - strlen_P(string) * fontWidth(size)) / 2 - 1;
  140. DWIN_Draw_String(bShow, size, color, bColor, x, y, string);
  141. }
  142. // Draw a char
  143. // color: Character color
  144. // x: abscissa of the display
  145. // y: ordinate of the display
  146. // c: ASCII code of char
  147. void DWINUI::Draw_Char(uint16_t color, uint16_t x, uint16_t y, const char c) {
  148. const char string[2] = { c, 0};
  149. DWIN_Draw_String(false, font, color, backcolor, x, y, string, 1);
  150. }
  151. // Draw a char at cursor position and increment cursor
  152. void DWINUI::Draw_Char(uint16_t color, const char c) {
  153. Draw_Char(color, cursor.x, cursor.y, c);
  154. MoveBy(fontWidth(font), 0);
  155. }
  156. // Draw a string at cursor position
  157. // color: Character color
  158. // *string: The string
  159. // rlimit: For draw less chars than string length use rlimit
  160. void DWINUI::Draw_String(const char * const string, uint16_t rlimit) {
  161. DWIN_Draw_String(false, font, textcolor, backcolor, cursor.x, cursor.y, string, rlimit);
  162. MoveBy(strlen(string) * fontWidth(font), 0);
  163. }
  164. void DWINUI::Draw_String(uint16_t color, const char * const string, uint16_t rlimit) {
  165. DWIN_Draw_String(false, font, color, backcolor, cursor.x, cursor.y, string, rlimit);
  166. MoveBy(strlen(string) * fontWidth(font), 0);
  167. }
  168. // Draw a numeric integer value
  169. // bShow: true=display background color; false=don't display background color
  170. // signedMode: 1=signed; 0=unsigned
  171. // size: Font size
  172. // color: Character color
  173. // bColor: Background color
  174. // iNum: Number of digits
  175. // x/y: Upper-left coordinate
  176. // value: Integer value
  177. void DWINUI::Draw_Int(uint8_t bShow, bool signedMode, uint8_t size, uint16_t color, uint16_t bColor, uint8_t iNum, uint16_t x, uint16_t y, int32_t value) {
  178. char nstr[10];
  179. sprintf_P(nstr, PSTR("%*li"), (signedMode ? iNum + 1 : iNum), value);
  180. DWIN_Draw_String(bShow, size, color, bColor, x, y, nstr);
  181. }
  182. // Draw a numeric float value
  183. // bShow: true=display background color; false=don't display background color
  184. // signedMode: 1=signed; 0=unsigned
  185. // size: Font size
  186. // color: Character color
  187. // bColor: Background color
  188. // iNum: Number of digits
  189. // fNum: Number of decimal digits
  190. // x/y: Upper-left coordinate
  191. // value: float value
  192. void DWINUI::Draw_Float(uint8_t bShow, bool signedMode, uint8_t size, uint16_t color, uint16_t bColor, uint8_t iNum, uint8_t fNum, uint16_t x, uint16_t y, float value) {
  193. char nstr[10];
  194. DWIN_Draw_String(bShow, size, color, bColor, x, y, dtostrf(value, iNum + (signedMode ? 2:1) + fNum, fNum, nstr));
  195. }
  196. // ------------------------- Buttons ------------------------------//
  197. void DWINUI::Draw_Button(uint16_t color, uint16_t bcolor, uint16_t x1, uint16_t y1, uint16_t x2, uint16_t y2, const char * const caption) {
  198. DWIN_Draw_Rectangle(1, bcolor, x1, y1, x2, y2);
  199. Draw_CenteredString(0, font, color, bcolor, x1, x2, (y2 + y1 - fontHeight())/2, caption);
  200. }
  201. void DWINUI::Draw_Button(uint8_t id, uint16_t x, uint16_t y) {
  202. switch (id) {
  203. case BTN_Cancel : Draw_Button(GET_TEXT_F(MSG_BUTTON_CANCEL), x, y); break;
  204. case BTN_Confirm : Draw_Button(GET_TEXT_F(MSG_BUTTON_CONFIRM), x, y); break;
  205. case BTN_Continue: Draw_Button(GET_TEXT_F(MSG_BUTTON_CONTINUE), x, y); break;
  206. case BTN_Print : Draw_Button(GET_TEXT_F(MSG_BUTTON_PRINT), x, y); break;
  207. case BTN_Save : Draw_Button(GET_TEXT_F(MSG_BUTTON_SAVE), x, y); break;
  208. case BTN_Purge : Draw_Button(GET_TEXT_F(MSG_BUTTON_PURGE), x, y); break;
  209. default: break;
  210. }
  211. }
  212. // -------------------------- Extra -------------------------------//
  213. // Draw a circle
  214. // color: circle color
  215. // x: the abscissa of the center of the circle
  216. // y: ordinate of the center of the circle
  217. // r: circle radius
  218. void DWINUI::Draw_Circle(uint16_t color, uint16_t x, uint16_t y, uint8_t r) {
  219. int a = 0, b = 0;
  220. while (a <= b) {
  221. b = SQRT(sq(r) - sq(a));
  222. if (a == 0) b--;
  223. DWIN_Draw_Point(color, 1, 1, x + a, y + b); // Draw some sector 1
  224. DWIN_Draw_Point(color, 1, 1, x + b, y + a); // Draw some sector 2
  225. DWIN_Draw_Point(color, 1, 1, x + b, y - a); // Draw some sector 3
  226. DWIN_Draw_Point(color, 1, 1, x + a, y - b); // Draw some sector 4
  227. DWIN_Draw_Point(color, 1, 1, x - a, y - b); // Draw some sector 5
  228. DWIN_Draw_Point(color, 1, 1, x - b, y - a); // Draw some sector 6
  229. DWIN_Draw_Point(color, 1, 1, x - b, y + a); // Draw some sector 7
  230. DWIN_Draw_Point(color, 1, 1, x - a, y + b); // Draw some sector 8
  231. a++;
  232. }
  233. }
  234. // Draw a circle filled with color
  235. // bcolor: fill color
  236. // x: the abscissa of the center of the circle
  237. // y: ordinate of the center of the circle
  238. // r: circle radius
  239. void DWINUI::Draw_FillCircle(uint16_t bcolor, uint16_t x,uint16_t y,uint8_t r) {
  240. int a = 0, b = 0;
  241. while (a <= b) {
  242. b = SQRT(sq(r) - sq(a)); // b=sqrt(r*r-a*a);
  243. if (a == 0) b--;
  244. DWIN_Draw_Line(bcolor, x-b,y-a,x+b,y-a);
  245. DWIN_Draw_Line(bcolor, x-a,y-b,x+a,y-b);
  246. DWIN_Draw_Line(bcolor, x-b,y+a,x+b,y+a);
  247. DWIN_Draw_Line(bcolor, x-a,y+b,x+a,y+b);
  248. a++;
  249. }
  250. }
  251. // Color Interpolator
  252. // val : Interpolator minv..maxv
  253. // minv : Minimum value
  254. // maxv : Maximum value
  255. // color1 : Start color
  256. // color2 : End color
  257. uint16_t DWINUI::ColorInt(int16_t val, int16_t minv, int16_t maxv, uint16_t color1, uint16_t color2) {
  258. uint8_t B, G, R;
  259. const float n = (float)(val - minv) / (maxv - minv);
  260. R = (1 - n) * GetRColor(color1) + n * GetRColor(color2);
  261. G = (1 - n) * GetGColor(color1) + n * GetGColor(color2);
  262. B = (1 - n) * GetBColor(color1) + n * GetBColor(color2);
  263. return RGB(R, G, B);
  264. }
  265. // Color Interpolator through Red->Yellow->Green->Blue (Pro UI)
  266. // val : Interpolator minv..maxv
  267. // minv : Minimum value
  268. // maxv : Maximum value
  269. uint16_t DWINUI::RainbowInt(int16_t val, int16_t minv, int16_t maxv) {
  270. uint8_t B, G, R;
  271. const uint8_t maxB = 28, maxR = 28, maxG = 38;
  272. const int16_t limv = _MAX(abs(minv), abs(maxv));
  273. float n = minv >= 0 ? (float)(val - minv) / (maxv - minv) : (float)val / limv;
  274. LIMIT(n, -1, 1);
  275. if (n < 0) {
  276. R = 0;
  277. G = (1 + n) * maxG;
  278. B = (-n) * maxB;
  279. }
  280. else if (n < 0.5) {
  281. R = maxR * n * 2;
  282. G = maxG;
  283. B = 0;
  284. }
  285. else {
  286. R = maxR;
  287. G = maxG * (1 - n);
  288. B = 0;
  289. }
  290. return RGB(R, G, B);
  291. }
  292. // Draw a checkbox
  293. // Color: frame color
  294. // bColor: Background color
  295. // x/y: Upper-left point
  296. // mode : 0 : unchecked, 1 : checked
  297. void DWINUI::Draw_Checkbox(uint16_t color, uint16_t bcolor, uint16_t x, uint16_t y, bool checked=false) {
  298. DWIN_Draw_String(true, font8x16, color, bcolor, x + 4, y, checked ? F("x") : F(" "));
  299. DWIN_Draw_Rectangle(0, color, x + 2, y + 2, x + 17, y + 17);
  300. }
  301. // Clear Menu by filling the menu area with background color
  302. void DWINUI::ClearMainArea() {
  303. DWIN_Draw_Rectangle(1, backcolor, 0, TITLE_HEIGHT, DWIN_WIDTH - 1, STATUS_Y - 1);
  304. }
  305. /* Title Class ==============================================================*/
  306. TitleClass Title;
  307. void TitleClass::draw() {
  308. if (DWINUI::onTitleDraw != nullptr) (*DWINUI::onTitleDraw)(this);
  309. }
  310. void TitleClass::SetCaption(const char * const title) {
  311. frameid = 0;
  312. if ( caption == title ) return;
  313. const uint8_t len = _MIN(sizeof(caption) - 1, strlen(title));
  314. memcpy(&caption[0], title, len);
  315. caption[len] = '\0';
  316. }
  317. void TitleClass::ShowCaption(const char * const title) {
  318. SetCaption(title);
  319. draw();
  320. }
  321. void TitleClass::SetFrame(uint8_t id, uint16_t x1, uint16_t y1, uint16_t x2, uint16_t y2) {
  322. caption[0] = '\0';
  323. frameid = id;
  324. frame = { x1, y1, x2, y2 };
  325. }
  326. void TitleClass::SetFrame(uint16_t x, uint16_t y, uint16_t w, uint16_t h) {
  327. SetFrame(1, x, y, x + w - 1, y + h - 1);
  328. }
  329. void TitleClass::FrameCopy(uint8_t id, uint16_t x1, uint16_t y1, uint16_t x2, uint16_t y2) {
  330. SetFrame(id, x1, y1, x2, y2);
  331. draw();
  332. }
  333. void TitleClass::FrameCopy(uint16_t x, uint16_t y, uint16_t w, uint16_t h) {
  334. FrameCopy(1, x, y, x + w - 1, y + h - 1);
  335. }
  336. #endif // DWIN_LCD_PROUI