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.

bed_mesh_screen.cpp 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  1. /***********************
  2. * bed_mesh_screen.cpp *
  3. ***********************/
  4. /****************************************************************************
  5. * Written By Marcio Teixeira 2020 *
  6. * *
  7. * This program is free software: you can redistribute it and/or modify *
  8. * it under the terms of the GNU General Public License as published by *
  9. * the Free Software Foundation, either version 3 of the License, or *
  10. * (at your option) any later version. *
  11. * *
  12. * This program is distributed in the hope that it will be useful, *
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of *
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
  15. * GNU General Public License for more details. *
  16. * *
  17. * To view a copy of the GNU General Public License, go to the following *
  18. * location: <https://www.gnu.org/licenses/>. *
  19. ****************************************************************************/
  20. #include "../config.h"
  21. #if BOTH(TOUCH_UI_FTDI_EVE, HAS_MESH)
  22. #include "screens.h"
  23. #include "screen_data.h"
  24. using namespace FTDI;
  25. using namespace Theme;
  26. using namespace ExtUI;
  27. #ifdef TOUCH_UI_PORTRAIT
  28. #define GRID_COLS 2
  29. #define GRID_ROWS 10
  30. #define MESH_POS BTN_POS(1, 2), BTN_SIZE(2,5)
  31. #define MESSAGE_POS BTN_POS(1, 7), BTN_SIZE(2,1)
  32. #define Z_LABEL_POS BTN_POS(1, 8), BTN_SIZE(1,1)
  33. #define Z_VALUE_POS BTN_POS(2, 8), BTN_SIZE(1,1)
  34. #define OKAY_POS BTN_POS(1,10), BTN_SIZE(2,1)
  35. #else
  36. #define GRID_COLS 5
  37. #define GRID_ROWS 5
  38. #define MESH_POS BTN_POS(1,1), BTN_SIZE(3,5)
  39. #define MESSAGE_POS BTN_POS(4,1), BTN_SIZE(2,1)
  40. #define Z_LABEL_POS BTN_POS(4,2), BTN_SIZE(2,1)
  41. #define Z_VALUE_POS BTN_POS(4,3), BTN_SIZE(2,1)
  42. #define OKAY_POS BTN_POS(4,5), BTN_SIZE(2,1)
  43. #endif
  44. void BedMeshScreen::drawMesh(int16_t x, int16_t y, int16_t w, int16_t h, ExtUI::bed_mesh_t data, uint8_t opts, float autoscale_max) {
  45. constexpr uint8_t rows = GRID_MAX_POINTS_Y;
  46. constexpr uint8_t cols = GRID_MAX_POINTS_X;
  47. #define VALUE(X,Y) (data ? data[X][Y] : 0)
  48. #define ISVAL(X,Y) (data ? !isnan(VALUE(X,Y)) : true)
  49. #define HEIGHT(X,Y) (ISVAL(X,Y) ? (VALUE(X,Y) - val_min) * scale_z : 0)
  50. // Compute the mean, min and max for the points
  51. float val_mean = 0;
  52. float val_max = -INFINITY;
  53. float val_min = INFINITY;
  54. uint8_t val_cnt = 0;
  55. if (data && (opts & USE_AUTOSCALE)) {
  56. for (uint8_t y = 0; y < rows; y++) {
  57. for (uint8_t x = 0; x < cols; x++) {
  58. if (ISVAL(x,y)) {
  59. const float val = VALUE(x,y);
  60. val_mean += val;
  61. val_max = max(val_max, val);
  62. val_min = min(val_min, val);
  63. val_cnt++;
  64. }
  65. }
  66. }
  67. }
  68. if (val_cnt) {
  69. val_mean /= val_cnt;
  70. } else {
  71. val_mean = 0;
  72. val_min = 0;
  73. val_max = 0;
  74. }
  75. const float scale_z = ((val_max == val_min) ? 1 : 1/(val_max - val_min)) * autoscale_max;
  76. /**
  77. * The 3D points go through a 3D graphics pipeline to determine the final 2D point on the screen.
  78. * This is written out as a stack of macros that each apply an affine transformation to the point.
  79. * At compile time, the compiler should be able to reduce these expressions.
  80. *
  81. * The last transformation in the chain (TRANSFORM_5) is initially set to a no-op so we can measure
  82. * the dimensions of the grid, but is later replaced with a scaling transform that scales the grid
  83. * to fit.
  84. */
  85. #define TRANSFORM_5(X,Y,Z) (X), (Y) // No transform
  86. #define TRANSFORM_4(X,Y,Z) TRANSFORM_5((X)/(Z),(Y)/-(Z), 0) // Perspective
  87. #define TRANSFORM_3(X,Y,Z) TRANSFORM_4((X), (Z), (Y)) // Swap Z and Y
  88. #define TRANSFORM_2(X,Y,Z) TRANSFORM_3((X), (Y) + 2.5, (Z) - 1) // Translate
  89. #define TRANSFORM(X,Y,Z) TRANSFORM_2(float(X)/(cols-1) - 0.5, float(Y)/(rows-1) - 0.5, (Z)) // Normalize
  90. // Compute the bounding box for the grid prior to scaling. Do this at compile-time by
  91. // transforming the four corner points via the transformation equations and finding
  92. // the min and max for each axis.
  93. constexpr float bounds[][3] = {{TRANSFORM(0 , 0 , 0)},
  94. {TRANSFORM(cols-1, 0 , 0)},
  95. {TRANSFORM(0 , rows-1, 0)},
  96. {TRANSFORM(cols-1, rows-1, 0)}};
  97. #define APPLY(FUNC, AXIS) FUNC(FUNC(bounds[0][AXIS], bounds[1][AXIS]), FUNC(bounds[2][AXIS], bounds[3][AXIS]))
  98. constexpr float grid_x = APPLY(min,0);
  99. constexpr float grid_y = APPLY(min,1);
  100. constexpr float grid_w = APPLY(max,0) - grid_x;
  101. constexpr float grid_h = APPLY(max,1) - grid_y;
  102. constexpr float grid_cx = grid_x + grid_w/2;
  103. constexpr float grid_cy = grid_y + grid_h/2;
  104. // Figure out scale and offset such that the grid fits within the rectangle given by (x,y,w,h)
  105. const float scale_x = float(w)/grid_w;
  106. const float scale_y = float(h)/grid_h;
  107. const float center_x = x + w/2;
  108. const float center_y = y + h/2;
  109. // Now replace the last transformation in the chain with a scaling operation.
  110. #undef TRANSFORM_5
  111. #define TRANSFORM_6(X,Y,Z) (X)*16, (Y)*16 // Scale to 1/16 pixel units
  112. #define TRANSFORM_5(X,Y,Z) TRANSFORM_6( center_x + ((X) - grid_cx) * scale_x, \
  113. center_y + ((Y) - grid_cy) * scale_y, 0) // Scale to bounds
  114. // Draw the grid
  115. const uint16_t basePointSize = min(w,h) / max(cols,rows);
  116. CommandProcessor cmd;
  117. cmd.cmd(SAVE_CONTEXT())
  118. .cmd(TAG_MASK(false))
  119. .cmd(SAVE_CONTEXT());
  120. for (uint8_t y = 0; y < rows; y++) {
  121. for (uint8_t x = 0; x < cols; x++) {
  122. if (ISVAL(x,y)) {
  123. const bool hasLeftSegment = x < cols - 1 && ISVAL(x+1,y);
  124. const bool hasRightSegment = y < rows - 1 && ISVAL(x,y+1);
  125. if (hasLeftSegment || hasRightSegment) {
  126. cmd.cmd(BEGIN(LINE_STRIP));
  127. if (hasLeftSegment) cmd.cmd(VERTEX2F(TRANSFORM(x + 1, y , HEIGHT(x + 1, y ))));
  128. cmd.cmd( VERTEX2F(TRANSFORM(x , y , HEIGHT(x , y ))));
  129. if (hasRightSegment) cmd.cmd(VERTEX2F(TRANSFORM(x , y + 1, HEIGHT(x , y + 1))));
  130. }
  131. }
  132. }
  133. if (opts & USE_POINTS) {
  134. const float sq_min = sq(val_min - val_mean);
  135. const float sq_max = sq(val_max - val_mean);
  136. cmd.cmd(POINT_SIZE(basePointSize * 2));
  137. cmd.cmd(BEGIN(POINTS));
  138. for (uint8_t x = 0; x < cols; x++) {
  139. if (ISVAL(x,y)) {
  140. if (opts & USE_COLORS) {
  141. const float val_dev = VALUE(x, y) - val_mean;
  142. const uint8_t neg_byte = sq(val_dev) / (val_dev < 0 ? sq_min : sq_max) * 0xFF;
  143. const uint8_t pos_byte = 255 - neg_byte;
  144. cmd.cmd(COLOR_RGB(pos_byte, pos_byte, 0xFF));
  145. }
  146. cmd.cmd(VERTEX2F(TRANSFORM(x, y, HEIGHT(x, y))));
  147. }
  148. }
  149. if (opts & USE_COLORS) {
  150. cmd.cmd(RESTORE_CONTEXT())
  151. .cmd(SAVE_CONTEXT());
  152. }
  153. }
  154. }
  155. cmd.cmd(RESTORE_CONTEXT())
  156. .cmd(TAG_MASK(true));
  157. if (opts & USE_TAGS) {
  158. cmd.cmd(COLOR_MASK(false, false, false, false))
  159. .cmd(POINT_SIZE(basePointSize * 10))
  160. .cmd(BEGIN(POINTS));
  161. for (uint8_t y = 0; y < rows; y++) {
  162. for (uint8_t x = 0; x < cols; x++) {
  163. const uint8_t tag = pointToTag(x, y);
  164. cmd.tag(tag).cmd(VERTEX2F(TRANSFORM(x, y, HEIGHT(x, y))));
  165. }
  166. }
  167. cmd.cmd(COLOR_MASK(true, true, true, true));
  168. }
  169. if (opts & USE_HIGHLIGHT) {
  170. const uint8_t tag = screen_data.BedMeshScreen.highlightedTag;
  171. uint8_t x, y;
  172. if (tagToPoint(tag, x, y)) {
  173. cmd.cmd(COLOR_A(128))
  174. .cmd(POINT_SIZE(basePointSize * 6))
  175. .cmd(BEGIN(POINTS))
  176. .tag(tag).cmd(VERTEX2F(TRANSFORM(x, y, HEIGHT(x, y))));
  177. }
  178. }
  179. cmd.cmd(END());
  180. cmd.cmd(RESTORE_CONTEXT());
  181. }
  182. uint8_t BedMeshScreen::pointToTag(uint8_t x, uint8_t y) {
  183. return y * (GRID_MAX_POINTS_X) + x + 10;
  184. }
  185. bool BedMeshScreen::tagToPoint(uint8_t tag, uint8_t &x, uint8_t &y) {
  186. if (tag < 10) return false;
  187. x = (tag - 10) % (GRID_MAX_POINTS_X);
  188. y = (tag - 10) / (GRID_MAX_POINTS_X);
  189. return true;
  190. }
  191. void BedMeshScreen::onEntry() {
  192. screen_data.BedMeshScreen.highlightedTag = 0;
  193. screen_data.BedMeshScreen.count = GRID_MAX_POINTS;
  194. screen_data.BedMeshScreen.showMappingDone = false;
  195. BaseScreen::onEntry();
  196. }
  197. float BedMeshScreen::getHightlightedValue() {
  198. if (screen_data.BedMeshScreen.highlightedTag) {
  199. xy_uint8_t pt;
  200. tagToPoint(screen_data.BedMeshScreen.highlightedTag, pt.x, pt.y);
  201. return ExtUI::getMeshPoint(pt);
  202. }
  203. return NAN;
  204. }
  205. void BedMeshScreen::drawHighlightedPointValue() {
  206. char str[16];
  207. const float val = getHightlightedValue();
  208. const bool isGood = !isnan(val);
  209. if (isGood)
  210. dtostrf(val, 5, 3, str);
  211. else
  212. strcpy_P(str, PSTR("-"));
  213. CommandProcessor cmd;
  214. cmd.font(Theme::font_medium)
  215. .text(Z_LABEL_POS, GET_TEXT_F(MSG_MESH_EDIT_Z))
  216. .text(Z_VALUE_POS, str)
  217. .colors(action_btn)
  218. .tag(1).button( OKAY_POS, GET_TEXT_F(MSG_BUTTON_OKAY))
  219. .tag(0);
  220. if (screen_data.BedMeshScreen.showMappingDone) {
  221. cmd.text(MESSAGE_POS, GET_TEXT_F(MSG_BED_MAPPING_DONE));
  222. }
  223. }
  224. void BedMeshScreen::onRedraw(draw_mode_t what) {
  225. #define _INSET_POS(x,y,w,h) x + min(w,h)/10, y + min(w,h)/10, w - min(w,h)/5, h - min(w,h)/5
  226. #define INSET_POS(pos) _INSET_POS(pos)
  227. if (what & BACKGROUND) {
  228. CommandProcessor cmd;
  229. cmd.cmd(CLEAR_COLOR_RGB(bg_color))
  230. .cmd(CLEAR(true,true,true));
  231. // Draw the shadow and tags
  232. cmd.cmd(COLOR_RGB(0x444444));
  233. BedMeshScreen::drawMesh(INSET_POS(MESH_POS), nullptr, USE_POINTS | USE_TAGS);
  234. cmd.cmd(COLOR_RGB(bg_text_enabled));
  235. }
  236. if (what & FOREGROUND) {
  237. constexpr float autoscale_max_amplitude = 0.03;
  238. const bool gotAllPoints = screen_data.BedMeshScreen.count >= GRID_MAX_POINTS;
  239. if (gotAllPoints) {
  240. drawHighlightedPointValue();
  241. }
  242. const float levelingProgress = sq(float(screen_data.BedMeshScreen.count) / GRID_MAX_POINTS);
  243. BedMeshScreen::drawMesh(INSET_POS(MESH_POS), ExtUI::getMeshArray(),
  244. USE_POINTS | USE_HIGHLIGHT | USE_AUTOSCALE | (gotAllPoints ? USE_COLORS : 0),
  245. autoscale_max_amplitude * levelingProgress
  246. );
  247. }
  248. }
  249. bool BedMeshScreen::onTouchStart(uint8_t tag) {
  250. screen_data.BedMeshScreen.highlightedTag = tag;
  251. return true;
  252. }
  253. bool BedMeshScreen::onTouchEnd(uint8_t tag) {
  254. switch (tag) {
  255. case 1:
  256. GOTO_PREVIOUS();
  257. return true;
  258. default:
  259. return false;
  260. }
  261. }
  262. void BedMeshScreen::onMeshUpdate(const int8_t, const int8_t, const float) {
  263. if (AT_SCREEN(BedMeshScreen))
  264. onRefresh();
  265. }
  266. void BedMeshScreen::onMeshUpdate(const int8_t x, const int8_t y, const ExtUI::probe_state_t state) {
  267. switch(state) {
  268. case ExtUI::MESH_START:
  269. screen_data.BedMeshScreen.count = 0;
  270. screen_data.BedMeshScreen.showMappingDone = false;
  271. break;
  272. case ExtUI::MESH_FINISH:
  273. screen_data.BedMeshScreen.count = GRID_MAX_POINTS;
  274. screen_data.BedMeshScreen.showMappingDone = true;
  275. break;
  276. case ExtUI::PROBE_START:
  277. screen_data.BedMeshScreen.highlightedTag = pointToTag(x, y);
  278. break;
  279. case ExtUI::PROBE_FINISH:
  280. screen_data.BedMeshScreen.count++;
  281. break;
  282. }
  283. BedMeshScreen::onMeshUpdate(x, y, 0);
  284. }
  285. void BedMeshScreen::startMeshProbe() {
  286. GOTO_SCREEN(BedMeshScreen);
  287. screen_data.BedMeshScreen.count = 0;
  288. injectCommands_P(PSTR(BED_LEVELING_COMMANDS));
  289. }
  290. #endif // TOUCH_UI_FTDI_EVE && HAS_MESH