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.

tft_string.cpp 5.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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 <https://www.gnu.org/licenses/>.
  20. *
  21. */
  22. #include "../../inc/MarlinConfig.h"
  23. #if HAS_GRAPHICAL_TFT
  24. #include "tft_string.h"
  25. #include "../fontutils.h"
  26. #include "../marlinui.h"
  27. //#define DEBUG_TFT_FONT
  28. #define DEBUG_OUT ENABLED(DEBUG_TFT_FONT)
  29. #include "../../core/debug_out.h"
  30. glyph_t *TFT_String::glyphs[256];
  31. font_t *TFT_String::font_header;
  32. char TFT_String::data[];
  33. uint16_t TFT_String::span;
  34. uint8_t TFT_String::length;
  35. void TFT_String::set_font(const uint8_t *font) {
  36. font_header = (font_t *)font;
  37. uint32_t glyph;
  38. for (glyph = 0; glyph < 256; glyph++) glyphs[glyph] = nullptr;
  39. DEBUG_ECHOLNPGM("Format: ", font_header->Format);
  40. DEBUG_ECHOLNPGM("BBXWidth: ", font_header->BBXWidth);
  41. DEBUG_ECHOLNPGM("BBXHeight: ", font_header->BBXHeight);
  42. DEBUG_ECHOLNPGM("BBXOffsetX: ", font_header->BBXOffsetX);
  43. DEBUG_ECHOLNPGM("BBXOffsetY: ", font_header->BBXOffsetY);
  44. DEBUG_ECHOLNPGM("CapitalAHeight: ", font_header->CapitalAHeight);
  45. DEBUG_ECHOLNPGM("Encoding65Pos: ", font_header->Encoding65Pos);
  46. DEBUG_ECHOLNPGM("Encoding97Pos: ", font_header->Encoding97Pos);
  47. DEBUG_ECHOLNPGM("FontStartEncoding: ", font_header->FontStartEncoding);
  48. DEBUG_ECHOLNPGM("FontEndEncoding: ", font_header->FontEndEncoding);
  49. DEBUG_ECHOLNPGM("LowerGDescent: ", font_header->LowerGDescent);
  50. DEBUG_ECHOLNPGM("FontAscent: ", font_header->FontAscent);
  51. DEBUG_ECHOLNPGM("FontDescent: ", font_header->FontDescent);
  52. DEBUG_ECHOLNPGM("FontXAscent: ", font_header->FontXAscent);
  53. DEBUG_ECHOLNPGM("FontXDescent: ", font_header->FontXDescent);
  54. add_glyphs(font);
  55. }
  56. void TFT_String::add_glyphs(const uint8_t *font) {
  57. uint32_t glyph;
  58. uint8_t *pointer = (uint8_t *)font + sizeof(font_t);
  59. for (glyph = ((font_t *)font)->FontStartEncoding; glyph <= ((font_t *)font)->FontEndEncoding; glyph++) {
  60. if (*pointer != NO_GLYPH) {
  61. glyphs[glyph] = (glyph_t *)pointer;
  62. pointer += sizeof(glyph_t) + ((glyph_t *)pointer)->DataSize;
  63. }
  64. else
  65. pointer++;
  66. }
  67. }
  68. void TFT_String::set() {
  69. *data = 0x00;
  70. span = 0;
  71. length = 0;
  72. }
  73. /**
  74. * Add a string, applying substitutions for the following characters:
  75. *
  76. * $ displays the string given by fstr or cstr
  77. * = displays '0'....'10' for indexes 0 - 10
  78. * ~ displays '1'....'11' for indexes 0 - 10
  79. * * displays 'E1'...'E11' for indexes 0 - 10 (By default. Uses LCD_FIRST_TOOL)
  80. * @ displays an axis name such as XYZUVW, or E for an extruder
  81. */
  82. void TFT_String::add(const char *tpl, const int8_t index, const char *cstr/*=nullptr*/, FSTR_P const fstr/*=nullptr*/) {
  83. wchar_t wchar;
  84. while (*tpl) {
  85. tpl = get_utf8_value_cb(tpl, read_byte_ram, &wchar);
  86. if (wchar > 255) wchar |= 0x0080;
  87. const uint8_t ch = uint8_t(wchar & 0x00FF);
  88. if (ch == '=' || ch == '~' || ch == '*') {
  89. if (index >= 0) {
  90. int8_t inum = index + ((ch == '=') ? 0 : LCD_FIRST_TOOL);
  91. if (ch == '*') add_character('E');
  92. if (inum >= 10) { add_character('0' + (inum / 10)); inum %= 10; }
  93. add_character('0' + inum);
  94. }
  95. else
  96. add(index == -2 ? GET_TEXT_F(MSG_CHAMBER) : GET_TEXT_F(MSG_BED));
  97. }
  98. else if (ch == '$' && fstr)
  99. add(fstr);
  100. else if (ch == '$' && cstr)
  101. add(cstr);
  102. else if (ch == '@')
  103. add_character(AXIS_CHAR(index));
  104. else
  105. add_character(ch);
  106. }
  107. eol();
  108. }
  109. void TFT_String::add(const char *cstr, uint8_t max_len/*=MAX_STRING_LENGTH*/) {
  110. wchar_t wchar;
  111. while (*cstr && max_len) {
  112. cstr = get_utf8_value_cb(cstr, read_byte_ram, &wchar);
  113. if (wchar > 255) wchar |= 0x0080;
  114. const uint8_t ch = uint8_t(wchar & 0x00FF);
  115. add_character(ch);
  116. max_len--;
  117. }
  118. eol();
  119. }
  120. void TFT_String::add_character(const char character) {
  121. if (length < MAX_STRING_LENGTH) {
  122. data[length] = character;
  123. length++;
  124. span += glyph(character)->DWidth;
  125. }
  126. }
  127. void TFT_String::rtrim(const char character) {
  128. while (length) {
  129. if (data[length - 1] == 0x20 || data[length - 1] == character) {
  130. length--;
  131. span -= glyph(data[length])->DWidth;
  132. eol();
  133. }
  134. else
  135. break;
  136. }
  137. }
  138. void TFT_String::ltrim(const char character) {
  139. uint16_t i, j;
  140. for (i = 0; (i < length) && (data[i] == 0x20 || data[i] == character); i++) {
  141. span -= glyph(data[i])->DWidth;
  142. }
  143. if (i == 0) return;
  144. for (j = 0; i < length; data[j++] = data[i++]);
  145. length = j;
  146. eol();
  147. }
  148. void TFT_String::trim(const char character) {
  149. rtrim(character);
  150. ltrim(character);
  151. }
  152. TFT_String tft_string;
  153. #endif // HAS_GRAPHICAL_TFT