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.

lcdprint_dwin.cpp 6.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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. * lcd/e3v2/marlinui/lcdprint_dwin.cpp
  24. *
  25. * Due to DWIN hardware limitations simplified characters are used
  26. */
  27. #include "../../../inc/MarlinConfigPre.h"
  28. #if IS_DWIN_MARLINUI
  29. #include "lcdprint_dwin.h"
  30. #include "dwin_lcd.h"
  31. #include "dwin_string.h"
  32. #include "../../marlinui.h"
  33. #include "../../../MarlinCore.h"
  34. #include <string.h>
  35. cursor_t cursor;
  36. extern dwin_font_t dwin_font;
  37. void lcd_moveto_xy(const lcd_uint_t x, const lcd_uint_t y) { cursor.x = x; cursor.y = y; }
  38. void lcd_moveto(const lcd_uint_t col, const lcd_uint_t row) {
  39. lcd_moveto_xy(col * dwin_font.width, row * (dwin_font.height + EXTRA_ROW_HEIGHT) + EXTRA_ROW_HEIGHT / 2);
  40. }
  41. inline void lcd_advance_cursor(const uint8_t len=1) { cursor.x += len * dwin_font.width; }
  42. void lcd_put_int(const int i) {
  43. // TODO: Draw an int at the cursor position, advance the cursor
  44. }
  45. int lcd_put_dwin_string() {
  46. DWIN_Draw_String(dwin_font.solid, dwin_font.index, dwin_font.fg, dwin_font.bg, cursor.x, cursor.y, dwin_string.string());
  47. lcd_advance_cursor(dwin_string.length);
  48. return dwin_string.length;
  49. }
  50. // return < 0 on error
  51. // return the advanced cols
  52. int lcd_put_wchar_max(const wchar_t c, const pixel_len_t max_length) {
  53. dwin_string.set(c);
  54. dwin_string.truncate(max_length);
  55. // Draw the char(s) at the cursor and advance the cursor
  56. DWIN_Draw_String(dwin_font.solid, dwin_font.index, dwin_font.fg, dwin_font.bg, cursor.x, cursor.y, dwin_string.string());
  57. lcd_advance_cursor(dwin_string.length);
  58. return dwin_string.length;
  59. }
  60. /**
  61. * @brief Draw a UTF-8 string
  62. *
  63. * @param utf8_str : the UTF-8 string
  64. * @param cb_read_byte : the callback function to read one byte from the utf8_str (from RAM or ROM)
  65. * @param max_length : the pixel length of the string allowed (or number of slots in HD44780)
  66. *
  67. * @return the number of pixels advanced
  68. *
  69. * Draw a UTF-8 string
  70. */
  71. static int lcd_put_u8str_max_cb(const char * utf8_str, read_byte_cb_t cb_read_byte, const pixel_len_t max_length) {
  72. const uint8_t *p = (uint8_t *)utf8_str;
  73. dwin_string.set();
  74. while (dwin_string.length < max_length) {
  75. wchar_t ch = 0;
  76. p = get_utf8_value_cb(p, cb_read_byte, &ch);
  77. if (!ch) break;
  78. dwin_string.add(ch);
  79. }
  80. DWIN_Draw_String(dwin_font.solid, dwin_font.index, dwin_font.fg, dwin_font.bg, cursor.x, cursor.y, dwin_string.string());
  81. lcd_advance_cursor(dwin_string.length);
  82. return dwin_string.length;
  83. }
  84. int lcd_put_u8str_max(const char * utf8_str, const pixel_len_t max_length) {
  85. return lcd_put_u8str_max_cb(utf8_str, read_byte_ram, max_length);
  86. }
  87. int lcd_put_u8str_max_P(PGM_P utf8_pstr, const pixel_len_t max_length) {
  88. return lcd_put_u8str_max_cb(utf8_pstr, read_byte_rom, max_length);
  89. }
  90. lcd_uint_t lcd_put_u8str_P(PGM_P const ptpl, const int8_t ind, const char * const cstr/*=nullptr*/, FSTR_P const fstr/*=nullptr*/, const lcd_uint_t maxlen/*=LCD_WIDTH*/) {
  91. dwin_string.set(ptpl, ind, cstr, fstr);
  92. dwin_string.truncate(maxlen);
  93. DWIN_Draw_String(dwin_font.solid, dwin_font.index, dwin_font.fg, dwin_font.bg, cursor.x, cursor.y, dwin_string.string());
  94. lcd_advance_cursor(dwin_string.length);
  95. return dwin_string.length;
  96. }
  97. #if ENABLED(DEBUG_LCDPRINT)
  98. int test_dwin_charmap(dwin_charmap_t *data, size_t size, char *name, char flg_show_contents) {
  99. int ret;
  100. size_t idx = 0;
  101. dwin_charmap_t preval = { 0, 0, 0 };
  102. dwin_charmap_t pinval = { 0, 0, 0 };
  103. char flg_error = 0;
  104. int i;
  105. TRACE("Test %s\n", name);
  106. for (i = 0; i < size; i ++) {
  107. memcpy_P(&pinval, &(data[i]), sizeof(pinval));
  108. if (flg_show_contents) {
  109. #if 1
  110. TRACE("[% 4d] % 6" PRIu32 "(0x%04" PRIX32 ") --> 0x%02X,0x%02X%s\n", i, pinval.uchar, pinval.uchar, (unsigned int)(pinval.idx), (unsigned int)(pinval.idx2), (preval.uchar < pinval.uchar?"":" <--- ERROR"));
  111. #else
  112. TRACE("[% 4d]", i);
  113. TRACE("% 6" PRIu32 "(0x%04" PRIX32 "),", pinval.uchar, pinval.uchar);
  114. TRACE("0x%02X,", (unsigned int)(pinval.idx));
  115. TRACE("0x%02X,", (unsigned int)(pinval.idx2));
  116. TRACE("%s", (preval.uchar < pinval.uchar?"":" <--- ERROR"));
  117. #endif
  118. }
  119. if (preval.uchar >= pinval.uchar) {
  120. flg_error = 1;
  121. //TRACE("Error: out of order in array %s: idx=%d, val=%d(0x%x)\n", name, i, pinval.uchar, pinval.uchar);
  122. //return -1;
  123. }
  124. memcpy(&preval, &pinval, sizeof(pinval));
  125. ret = pf_bsearch_r((void *)data, size, pf_bsearch_cb_comp_dwinmap_pgm, (void *)&pinval, &idx);
  126. if (ret < 0) {
  127. flg_error = 1;
  128. TRACE("Error: not found item in array %s: idx=%d, val=%d(0x%x)\n", name, i, pinval.uchar, pinval.uchar);
  129. //return -1;
  130. }
  131. if (idx != i) {
  132. flg_error = 1;
  133. TRACE("Error: wrong index found item in array %s: idx=%d, val=%d(0x%x)\n", name, i, pinval.uchar, pinval.uchar);
  134. //return -1;
  135. }
  136. }
  137. if (flg_error) {
  138. TRACE("\nError: in array %s\n\n", name);
  139. return -1;
  140. }
  141. TRACE("\nPASS array %s\n\n", name);
  142. return 0;
  143. }
  144. int test_dwin_charmap_all() {
  145. int flg_error = 0;
  146. if (test_dwin_charmap(g_dwin_charmap_device, COUNT(g_dwin_charmap_device), "g_dwin_charmap_device", 0) < 0) {
  147. flg_error = 1;
  148. test_dwin_charmap(g_dwin_charmap_device, COUNT(g_dwin_charmap_device), "g_dwin_charmap_device", 1);
  149. }
  150. if (test_dwin_charmap(g_dwin_charmap_common, COUNT(g_dwin_charmap_common), "g_dwin_charmap_common", 0) < 0) {
  151. flg_error = 1;
  152. test_dwin_charmap(g_dwin_charmap_common, COUNT(g_dwin_charmap_common), "g_dwin_charmap_common", 1);
  153. }
  154. if (flg_error) {
  155. TRACE("\nFAILED in dwin tests!\n");
  156. return -1;
  157. }
  158. TRACE("\nPASS in dwin tests.\n");
  159. return 0;
  160. }
  161. #endif // DEBUG_LCDPRINT
  162. #endif // IS_DWIN_MARLINUI