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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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 <http://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, (char*)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(wchar_t c, 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, (char*)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, uint8_t (*cb_read_byte)(uint8_t * str), pixel_len_t max_length) {
  72. 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, (char*)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, 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, 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_ind_P(PGM_P const pstr, const int8_t ind, PGM_P const inStr/*=nullptr*/, const lcd_uint_t maxlen/*=LCD_WIDTH*/) {
  91. dwin_string.set();
  92. dwin_string.add((uint8_t*)pstr, ind, (uint8_t*)inStr);
  93. dwin_string.truncate(maxlen);
  94. DWIN_Draw_String(dwin_font.solid, dwin_font.index, dwin_font.fg, dwin_font.bg, cursor.x, cursor.y, (char*)dwin_string.string());
  95. lcd_advance_cursor(dwin_string.length());
  96. return dwin_string.length();
  97. }
  98. #if ENABLED(DEBUG_LCDPRINT)
  99. int test_dwin_charmap(dwin_charmap_t *data, size_t size, char *name, char flg_show_contents) {
  100. int ret;
  101. size_t idx = 0;
  102. dwin_charmap_t preval = { 0, 0, 0 };
  103. dwin_charmap_t pinval = { 0, 0, 0 };
  104. char flg_error = 0;
  105. int i;
  106. TRACE("Test %s\n", name);
  107. for (i = 0; i < size; i ++) {
  108. memcpy_P(&pinval, &(data[i]), sizeof(pinval));
  109. if (flg_show_contents) {
  110. #if 1
  111. 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"));
  112. #else
  113. TRACE("[% 4d]", i);
  114. TRACE("% 6" PRIu32 "(0x%04" PRIX32 "),", pinval.uchar, pinval.uchar);
  115. TRACE("0x%02X,", (unsigned int)(pinval.idx));
  116. TRACE("0x%02X,", (unsigned int)(pinval.idx2));
  117. TRACE("%s", (preval.uchar < pinval.uchar?"":" <--- ERROR"));
  118. #endif
  119. }
  120. if (preval.uchar >= pinval.uchar) {
  121. flg_error = 1;
  122. //TRACE("Error: out of order in array %s: idx=%d, val=%d(0x%x)\n", name, i, pinval.uchar, pinval.uchar);
  123. //return -1;
  124. }
  125. memcpy(&preval, &pinval, sizeof(pinval));
  126. ret = pf_bsearch_r((void *)data, size, pf_bsearch_cb_comp_dwinmap_pgm, (void *)&pinval, &idx);
  127. if (ret < 0) {
  128. flg_error = 1;
  129. TRACE("Error: not found item in array %s: idx=%d, val=%d(0x%x)\n", name, i, pinval.uchar, pinval.uchar);
  130. //return -1;
  131. }
  132. if (idx != i) {
  133. flg_error = 1;
  134. TRACE("Error: wrong index found item in array %s: idx=%d, val=%d(0x%x)\n", name, i, pinval.uchar, pinval.uchar);
  135. //return -1;
  136. }
  137. }
  138. if (flg_error) {
  139. TRACE("\nError: in array %s\n\n", name);
  140. return -1;
  141. }
  142. TRACE("\nPASS array %s\n\n", name);
  143. return 0;
  144. }
  145. int test_dwin_charmap_all() {
  146. int flg_error = 0;
  147. if (test_dwin_charmap(g_dwin_charmap_device, COUNT(g_dwin_charmap_device), "g_dwin_charmap_device", 0) < 0) {
  148. flg_error = 1;
  149. test_dwin_charmap(g_dwin_charmap_device, COUNT(g_dwin_charmap_device), "g_dwin_charmap_device", 1);
  150. }
  151. if (test_dwin_charmap(g_dwin_charmap_common, COUNT(g_dwin_charmap_common), "g_dwin_charmap_common", 0) < 0) {
  152. flg_error = 1;
  153. test_dwin_charmap(g_dwin_charmap_common, COUNT(g_dwin_charmap_common), "g_dwin_charmap_common", 1);
  154. }
  155. if (flg_error) {
  156. TRACE("\nFAILED in dwin tests!\n");
  157. return -1;
  158. }
  159. TRACE("\nPASS in dwin tests.\n");
  160. return 0;
  161. }
  162. #endif // DEBUG_LCDPRINT
  163. #endif // IS_DWIN_MARLINUI