My Marlin configs for Fabrikator Mini and CTC i3 Pro B
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

canvas.cpp 5.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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 "canvas.h"
  25. uint16_t CANVAS::width, CANVAS::height;
  26. uint16_t CANVAS::startLine, CANVAS::endLine;
  27. uint16_t *CANVAS::buffer = TFT::buffer;
  28. void CANVAS::New(uint16_t x, uint16_t y, uint16_t width, uint16_t height) {
  29. CANVAS::width = width;
  30. CANVAS::height = height;
  31. startLine = 0;
  32. endLine = 0;
  33. tft.set_window(x, y, x + width - 1, y + height - 1);
  34. }
  35. void CANVAS::Continue() {
  36. startLine = endLine;
  37. endLine = TFT_BUFFER_SIZE < width * (height - startLine) ? startLine + TFT_BUFFER_SIZE / width : height;
  38. }
  39. bool CANVAS::ToScreen() {
  40. tft.write_sequence(buffer, width * (endLine - startLine));
  41. return endLine == height;
  42. }
  43. void CANVAS::SetBackground(uint16_t color) {
  44. /* TODO: test and optimize perfomance */
  45. /*
  46. uint32_t count = (endLine - startLine) * width;
  47. uint16_t *pixel = buffer;
  48. while (count--)
  49. *pixel++ = color;
  50. */
  51. const uint32_t two_pixels = (((uint32_t )color) << 16) | color;
  52. uint32_t count = ((endLine - startLine) * width + 1) >> 1;
  53. uint32_t *pointer = (uint32_t *)buffer;
  54. while (count--) *pointer++ = two_pixels;
  55. }
  56. void CANVAS::AddText(uint16_t x, uint16_t y, uint16_t color, uint8_t *string, uint16_t maxWidth) {
  57. if (endLine < y || startLine > y + GetFontHeight()) return;
  58. if (maxWidth == 0) maxWidth = width - x;
  59. uint16_t stringWidth = 0;
  60. for (uint16_t i = 0 ; *(string + i) ; i++) {
  61. glyph_t *glyph = Glyph(string + i);
  62. if (stringWidth + glyph->BBXWidth > maxWidth) break;
  63. AddImage(x + stringWidth + glyph->BBXOffsetX, y + Font()->FontAscent - glyph->BBXHeight - glyph->BBXOffsetY, glyph->BBXWidth, glyph->BBXHeight, GREYSCALE1, ((uint8_t *)glyph) + sizeof(glyph_t), &color);
  64. stringWidth += glyph->DWidth;
  65. }
  66. }
  67. void CANVAS::AddImage(int16_t x, int16_t y, MarlinImage image, uint16_t *colors) {
  68. uint16_t *data = (uint16_t *)Images[image].data;
  69. if (!data) return;
  70. uint16_t image_width = Images[image].width,
  71. image_height = Images[image].height;
  72. colorMode_t color_mode = Images[image].colorMode;
  73. if (color_mode != HIGHCOLOR)
  74. return AddImage(x, y, image_width, image_height, color_mode, (uint8_t *)data, colors);
  75. // HIGHCOLOR - 16 bits per pixel
  76. for (int16_t i = 0; i < image_height; i++) {
  77. int16_t line = y + i;
  78. if (line >= startLine && line < endLine) {
  79. uint16_t *pixel = buffer + x + (line - startLine) * width;
  80. for (int16_t j = 0; j < image_width; j++) {
  81. if ((x + j >= 0) && (x + j < width)) *pixel = *data;
  82. pixel++;
  83. data++;
  84. }
  85. }
  86. else
  87. data += image_width;
  88. }
  89. }
  90. void CANVAS::AddImage(int16_t x, int16_t y, uint8_t image_width, uint8_t image_height, colorMode_t color_mode, uint8_t *data, uint16_t *colors) {
  91. uint8_t bitsPerPixel;
  92. switch (color_mode) {
  93. case GREYSCALE1: bitsPerPixel = 1; break;
  94. case GREYSCALE2: bitsPerPixel = 2; break;
  95. case GREYSCALE4: bitsPerPixel = 4; break;
  96. default: return;
  97. }
  98. uint8_t mask = 0xFF >> (8 - bitsPerPixel),
  99. pixelsPerByte = 8 / bitsPerPixel;
  100. colors--;
  101. for (int16_t i = 0; i < image_height; i++) {
  102. int16_t line = y + i;
  103. if (line >= startLine && line < endLine) {
  104. uint16_t *pixel = buffer + x + (line - startLine) * width;
  105. uint8_t offset = 8 - bitsPerPixel;
  106. for (int16_t j = 0; j < image_width; j++) {
  107. if (offset > 8) {
  108. data++;
  109. offset = 8 - bitsPerPixel;
  110. }
  111. if ((x + j >= 0) && (x + j < width)) {
  112. const uint8_t color = ((*data) >> offset) & mask;
  113. if (color) *pixel = *(colors + color);
  114. }
  115. pixel++;
  116. offset -= bitsPerPixel;
  117. }
  118. data++;
  119. }
  120. else
  121. data += (image_width + pixelsPerByte - 1) / pixelsPerByte;
  122. }
  123. }
  124. void CANVAS::AddRectangle(uint16_t x, uint16_t y, uint16_t rectangleWidth, uint16_t rectangleHeight, uint16_t color) {
  125. if (endLine < y || startLine > y + rectangleHeight) return;
  126. for (uint16_t i = 0; i < rectangleHeight; i++) {
  127. uint16_t line = y + i;
  128. if (line >= startLine && line < endLine) {
  129. uint16_t *pixel = buffer + x + (line - startLine) * width;
  130. if (i == 0 || i == rectangleHeight - 1) {
  131. for (uint16_t j = 0; j < rectangleWidth; j++) *pixel++ = color;
  132. }
  133. else {
  134. *pixel = color;
  135. pixel += rectangleWidth - 1;
  136. *pixel = color;
  137. }
  138. }
  139. }
  140. }
  141. void CANVAS::AddBar(uint16_t x, uint16_t y, uint16_t barWidth, uint16_t barHeight, uint16_t color) {
  142. if (endLine < y || startLine > y + barHeight) return;
  143. for (uint16_t i = 0; i < barHeight; i++) {
  144. uint16_t line = y + i;
  145. if (line >= startLine && line < endLine) {
  146. uint16_t *pixel = buffer + x + (line - startLine) * width;
  147. for (uint16_t j = 0; j < barWidth; j++) *pixel++ = color;
  148. }
  149. }
  150. }
  151. CANVAS Canvas;
  152. #endif // HAS_GRAPHICAL_TFT