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_string.cpp 5.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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. #include "../../../inc/MarlinConfig.h"
  23. #if IS_DWIN_MARLINUI
  24. #include "dwin_string.h"
  25. //#include "../../fontutils.h"
  26. char DWIN_String::data[];
  27. uint16_t DWIN_String::span;
  28. uint8_t DWIN_String::length;
  29. void DWIN_String::set() {
  30. //*data = 0x00;
  31. memset(data, 0x00, sizeof(data));
  32. span = 0;
  33. length = 0;
  34. }
  35. uint8_t read_byte(const uint8_t *byte) { return *byte; }
  36. /**
  37. * Add a string, applying substitutions for the following characters:
  38. *
  39. * $ displays the clipped string given by fstr or cstr
  40. * = displays '0'....'10' for indexes 0 - 10
  41. * ~ displays '1'....'11' for indexes 0 - 10
  42. * * displays 'E1'...'E11' for indexes 0 - 10 (By default. Uses LCD_FIRST_TOOL)
  43. * @ displays an axis name such as XYZUVW, or E for an extruder
  44. */
  45. void DWIN_String::add(const char *tpl, const int8_t index, const char *cstr/*=nullptr*/, FSTR_P const fstr/*=nullptr*/) {
  46. wchar_t wchar;
  47. while (*tpl) {
  48. tpl = get_utf8_value_cb(tpl, read_byte, &wchar);
  49. if (wchar > 255) wchar |= 0x0080;
  50. const uint8_t ch = uint8_t(wchar & 0x00FF);
  51. if (ch == '=' || ch == '~' || ch == '*') {
  52. if (index >= 0) {
  53. int8_t inum = index + ((ch == '=') ? 0 : LCD_FIRST_TOOL);
  54. if (ch == '*') add_character('E');
  55. if (inum >= 10) { add_character('0' + (inum / 10)); inum %= 10; }
  56. add_character('0' + inum);
  57. }
  58. else
  59. add(index == -2 ? GET_TEXT_F(MSG_CHAMBER) : GET_TEXT_F(MSG_BED));
  60. }
  61. else if (ch == '$' && fstr)
  62. add(fstr);
  63. else if (ch == '$' && cstr)
  64. add(cstr);
  65. else if (ch == '@')
  66. add_character(AXIS_CHAR(index));
  67. else
  68. add_character(ch);
  69. }
  70. eol();
  71. }
  72. void DWIN_String::add(const char *cstr, uint8_t max_len/*=MAX_STRING_LENGTH*/) {
  73. wchar_t wchar;
  74. while (*cstr && max_len) {
  75. cstr = get_utf8_value_cb(cstr, read_byte, &wchar);
  76. /*
  77. if (wchar > 255) wchar |= 0x0080;
  78. uint8_t ch = uint8_t(wchar & 0x00FF);
  79. add_character(ch);
  80. */
  81. add(wchar);
  82. max_len--;
  83. }
  84. eol();
  85. }
  86. void DWIN_String::add(const wchar_t character) {
  87. int ret;
  88. size_t idx = 0;
  89. dwin_charmap_t pinval;
  90. dwin_charmap_t *copy_address = nullptr;
  91. pinval.uchar = character;
  92. pinval.idx = -1;
  93. // For 8-bit ASCII just print the single character
  94. char str[] = { '?', 0 };
  95. if (character < 255) {
  96. str[0] = (char)character;
  97. }
  98. else {
  99. copy_address = nullptr;
  100. ret = pf_bsearch_r((void *)g_dwin_charmap_device, COUNT(g_dwin_charmap_device), pf_bsearch_cb_comp_dwinmap_pgm, (void *)&pinval, &idx);
  101. if (ret >= 0) {
  102. copy_address = (dwin_charmap_t*)(g_dwin_charmap_device + idx);
  103. }
  104. else {
  105. ret = pf_bsearch_r((void *)g_dwin_charmap_common, COUNT(g_dwin_charmap_common), pf_bsearch_cb_comp_dwinmap_pgm, (void *)&pinval, &idx);
  106. if (ret >= 0)
  107. copy_address = (dwin_charmap_t*)(g_dwin_charmap_common + idx);
  108. }
  109. if (ret >= 0) {
  110. dwin_charmap_t localval;
  111. memcpy_P(&localval, copy_address, sizeof(localval));
  112. str[0] = localval.idx;
  113. str[1] = localval.idx2;
  114. }
  115. }
  116. if (str[0]) add_character(str[0]);
  117. if (str[1]) add_character(str[1]);
  118. }
  119. void DWIN_String::add_character(const char character) {
  120. if (length < MAX_STRING_LENGTH) {
  121. data[length] = character;
  122. length++;
  123. //span += glyph(character)->DWidth;
  124. }
  125. }
  126. void DWIN_String::rtrim(const char character) {
  127. while (length) {
  128. if (data[length - 1] == 0x20 || data[length - 1] == character) {
  129. length--;
  130. //span -= glyph(data[length])->DWidth;
  131. eol();
  132. }
  133. else
  134. break;
  135. }
  136. }
  137. void DWIN_String::ltrim(const char character) {
  138. uint16_t i, j;
  139. for (i = 0; (i < length) && (data[i] == 0x20 || data[i] == character); i++) {
  140. //span -= glyph(data[i])->DWidth;
  141. }
  142. if (i == 0) return;
  143. for (j = 0; i < length; data[j++] = data[i++]);
  144. length = j;
  145. eol();
  146. }
  147. void DWIN_String::trim(const char character) {
  148. rtrim(character);
  149. ltrim(character);
  150. }
  151. /* return v1 - v2 */
  152. int dwin_charmap_compare(dwin_charmap_t *v1, dwin_charmap_t *v2) {
  153. return (v1->uchar < v2->uchar) ? -1 : (v1->uchar > v2->uchar) ? 1 : 0;
  154. }
  155. int pf_bsearch_cb_comp_dwinmap_pgm(void *userdata, size_t idx, void * data_pin) {
  156. dwin_charmap_t localval;
  157. dwin_charmap_t *p_dwin_charmap = (dwin_charmap_t *)userdata;
  158. memcpy_P(&localval, p_dwin_charmap + idx, sizeof(localval));
  159. return dwin_charmap_compare(&localval, (dwin_charmap_t *)data_pin);
  160. }
  161. DWIN_String dwin_string;
  162. #endif // IS_DWIN_MARLINUI