From 5b0dc4d325b99ff8e030bdde7af2ceeefa822f04 Mon Sep 17 00:00:00 2001 From: Marcio T Date: Mon, 22 Mar 2021 23:51:03 -0600 Subject: [PATCH] Mesh Editor for FTDI Eve Touch UI (#21381) --- .../ftdi_eve_lib/extended/grid_layout.h | 13 +++ .../ftdi_eve_lib/extras/adjuster_widget.cpp | 54 +++++++++ .../ftdi_eve_lib/extras/adjuster_widget.h | 32 ++++++ .../ftdi_eve_touch_ui/language/language_en.h | 1 - .../screens/bed_mesh_screen.cpp | 103 ++++++++++++------ .../screens/bed_mesh_screen.h | 15 ++- .../screens/leveling_menu.cpp | 8 +- Marlin/src/lcd/extui/ui_api.cpp | 20 ++++ Marlin/src/lcd/extui/ui_api.h | 1 + 9 files changed, 205 insertions(+), 42 deletions(-) create mode 100644 Marlin/src/lcd/extui/lib/ftdi_eve_touch_ui/ftdi_eve_lib/extras/adjuster_widget.cpp create mode 100644 Marlin/src/lcd/extui/lib/ftdi_eve_touch_ui/ftdi_eve_lib/extras/adjuster_widget.h diff --git a/Marlin/src/lcd/extui/lib/ftdi_eve_touch_ui/ftdi_eve_lib/extended/grid_layout.h b/Marlin/src/lcd/extui/lib/ftdi_eve_touch_ui/ftdi_eve_lib/extended/grid_layout.h index 82bb8abf7..47aec24d8 100644 --- a/Marlin/src/lcd/extui/lib/ftdi_eve_touch_ui/ftdi_eve_lib/extended/grid_layout.h +++ b/Marlin/src/lcd/extui/lib/ftdi_eve_touch_ui/ftdi_eve_lib/extended/grid_layout.h @@ -87,6 +87,19 @@ cmd.cmd(LINE_WIDTH(16)); \ } +// Routines for subdividing a grid within a box (x,y,w,h) + +#define SUB_GRID_W(W) ((W)*w/SUB_COLS) +#define SUB_GRID_H(H) ((H)*h/SUB_ROWS) +#define SUB_GRID_X(X) (SUB_GRID_W((X)-1) + x) +#define SUB_GRID_Y(Y) (SUB_GRID_H((Y)-1) + y) +#define SUB_X(X) (SUB_GRID_X(X) + MARGIN_L) +#define SUB_Y(Y) (SUB_GRID_Y(Y) + MARGIN_T) +#define SUB_W(W) (SUB_GRID_W(W) - MARGIN_L - MARGIN_R) +#define SUB_H(H) (SUB_GRID_H(H) - MARGIN_T - MARGIN_B) +#define SUB_POS(X,Y) SUB_X(X), SUB_Y(Y) +#define SUB_SIZE(W,H) SUB_W(W), SUB_H(H) + namespace FTDI { #if ENABLED(TOUCH_UI_PORTRAIT) constexpr uint16_t display_width = Vsize; diff --git a/Marlin/src/lcd/extui/lib/ftdi_eve_touch_ui/ftdi_eve_lib/extras/adjuster_widget.cpp b/Marlin/src/lcd/extui/lib/ftdi_eve_touch_ui/ftdi_eve_lib/extras/adjuster_widget.cpp new file mode 100644 index 000000000..084c9c014 --- /dev/null +++ b/Marlin/src/lcd/extui/lib/ftdi_eve_touch_ui/ftdi_eve_lib/extras/adjuster_widget.cpp @@ -0,0 +1,54 @@ +/*********************** + * adjuster_widget.cpp * + ***********************/ + +/**************************************************************************** + * Written By Marcio Teixeira 2021 - Cocoa Press * + * * + * This program is free software: you can redistribute it and/or modify * + * it under the terms of the GNU General Public License as published by * + * the Free Software Foundation, either version 3 of the License, or * + * (at your option) any later version. * + * * + * This program is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * + * GNU General Public License for more details. * + * * + * To view a copy of the GNU General Public License, go to the following * + * location: . * + ****************************************************************************/ + +#include "../ftdi_eve_lib.h" +#include "../extended/grid_layout.h" + +#include "adjuster_widget.h" + +#define SUB_COLS 9 +#define SUB_ROWS 1 +#define VAL_POS SUB_POS(1,1), SUB_SIZE(5,1) +#define INC_POS SUB_POS(6,1), SUB_SIZE(2,1) +#define DEC_POS SUB_POS(8,1), SUB_SIZE(2,1) + +void draw_adjuster(CommandProcessor& cmd, int16_t x, int16_t y, int16_t w, int16_t h, uint8_t tag, float value, progmem_str units, int8_t width, uint8_t precision, draw_mode_t what) { + if (what & BACKGROUND) + cmd.tag(0).button(VAL_POS, F(""), FTDI::OPT_FLAT); + + if (what & FOREGROUND) { + char str[width + precision + 10 + (units ? strlen_P((const char*) units) : 0)]; + if (isnan(value)) + strcpy_P(str, PSTR("-")); + else + dtostrf(value, width, precision, str); + + if (units) { + strcat_P(str, PSTR(" ")); + strcat_P(str, (const char*) units); + } + + cmd.tag(0) + .text(VAL_POS, str) + .tag(tag ).button(INC_POS, F("-")) + .tag(tag+1).button(DEC_POS, F("+")); + } +} diff --git a/Marlin/src/lcd/extui/lib/ftdi_eve_touch_ui/ftdi_eve_lib/extras/adjuster_widget.h b/Marlin/src/lcd/extui/lib/ftdi_eve_touch_ui/ftdi_eve_lib/extras/adjuster_widget.h new file mode 100644 index 000000000..c48e37f62 --- /dev/null +++ b/Marlin/src/lcd/extui/lib/ftdi_eve_touch_ui/ftdi_eve_lib/extras/adjuster_widget.h @@ -0,0 +1,32 @@ +/********************* + * adjuster_widget.h * + *********************/ + +/**************************************************************************** + * Written By Marcio Teixeira 2021 - Cocoa Press * + * * + * This program is free software: you can redistribute it and/or modify * + * it under the terms of the GNU General Public License as published by * + * the Free Software Foundation, either version 3 of the License, or * + * (at your option) any later version. * + * * + * This program is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * + * GNU General Public License for more details. * + * * + * To view a copy of the GNU General Public License, go to the following * + * location: . * + ****************************************************************************/ + +#pragma once +#include "../extended/screen_types.h" + +void draw_adjuster( + CommandProcessor& cmd, + int16_t x, int16_t y, int16_t w, int16_t h, + uint8_t tag, + float value, progmem_str units = nullptr, + int8_t width = 5, uint8_t precision = 1, + draw_mode_t what = BOTH +); diff --git a/Marlin/src/lcd/extui/lib/ftdi_eve_touch_ui/language/language_en.h b/Marlin/src/lcd/extui/lib/ftdi_eve_touch_ui/language/language_en.h index e57858aa3..f6603e514 100644 --- a/Marlin/src/lcd/extui/lib/ftdi_eve_touch_ui/language/language_en.h +++ b/Marlin/src/lcd/extui/lib/ftdi_eve_touch_ui/language/language_en.h @@ -144,7 +144,6 @@ namespace Language_en { PROGMEM Language_Str MSG_BED_MAPPING_DONE = u8"Bed mapping finished"; PROGMEM Language_Str MSG_BED_MAPPING_INCOMPLETE = u8"Not all points probed"; PROGMEM Language_Str MSG_LEVELING = u8"Leveling"; - PROGMEM Language_Str MSG_SHOW_MESH = u8"Show Bed Mesh"; #if ENABLED(TOUCH_UI_LULZBOT_BIO) PROGMEM Language_Str MSG_MOVE_TO_HOME = u8"Move to Home"; diff --git a/Marlin/src/lcd/extui/lib/ftdi_eve_touch_ui/screens/bed_mesh_screen.cpp b/Marlin/src/lcd/extui/lib/ftdi_eve_touch_ui/screens/bed_mesh_screen.cpp index 6de573da8..c87329ebe 100644 --- a/Marlin/src/lcd/extui/lib/ftdi_eve_touch_ui/screens/bed_mesh_screen.cpp +++ b/Marlin/src/lcd/extui/lib/ftdi_eve_touch_ui/screens/bed_mesh_screen.cpp @@ -25,21 +25,24 @@ #ifdef FTDI_BED_MESH_SCREEN +#include "../ftdi_eve_lib/extras/adjuster_widget.h" + using namespace FTDI; using namespace Theme; using namespace ExtUI; constexpr static BedMeshScreenData &mydata = screen_data.BedMeshScreen; +constexpr static float gaugeThickness = 0.25; #if ENABLED(TOUCH_UI_PORTRAIT) - #define GRID_COLS 2 + #define GRID_COLS 3 #define GRID_ROWS 10 - #define MESH_POS BTN_POS(1, 2), BTN_SIZE(2,5) - #define MESSAGE_POS BTN_POS(1, 7), BTN_SIZE(2,1) + #define MESH_POS BTN_POS(1, 2), BTN_SIZE(3,5) + #define MESSAGE_POS BTN_POS(1, 7), BTN_SIZE(3,1) #define Z_LABEL_POS BTN_POS(1, 8), BTN_SIZE(1,1) - #define Z_VALUE_POS BTN_POS(2, 8), BTN_SIZE(1,1) - #define OKAY_POS BTN_POS(1,10), BTN_SIZE(2,1) + #define Z_VALUE_POS BTN_POS(2, 8), BTN_SIZE(2,1) + #define OKAY_POS BTN_POS(1,10), BTN_SIZE(3,1) #else #define GRID_COLS 5 #define GRID_ROWS 5 @@ -198,12 +201,12 @@ void BedMeshScreen::drawMesh(int16_t x, int16_t y, int16_t w, int16_t h, ExtUI:: if (opts & USE_HIGHLIGHT) { const uint8_t tag = mydata.highlightedTag; - uint8_t x, y; - if (tagToPoint(tag, x, y)) { + xy_uint8_t pt; + if (tagToPoint(tag, pt)) { cmd.cmd(COLOR_A(128)) .cmd(POINT_SIZE(basePointSize * 6)) .cmd(BEGIN(POINTS)) - .tag(tag).cmd(VERTEX2F(TRANSFORM(x, y, HEIGHT(x, y)))); + .tag(tag).cmd(VERTEX2F(TRANSFORM(pt.x, pt.y, HEIGHT(pt.x, pt.y)))); } } cmd.cmd(END()); @@ -214,43 +217,68 @@ uint8_t BedMeshScreen::pointToTag(uint8_t x, uint8_t y) { return y * (GRID_MAX_POINTS_X) + x + 10; } -bool BedMeshScreen::tagToPoint(uint8_t tag, uint8_t &x, uint8_t &y) { +bool BedMeshScreen::tagToPoint(uint8_t tag, xy_uint8_t &pt) { if (tag < 10) return false; - x = (tag - 10) % (GRID_MAX_POINTS_X); - y = (tag - 10) / (GRID_MAX_POINTS_X); + pt.x = (tag - 10) % (GRID_MAX_POINTS_X); + pt.y = (tag - 10) / (GRID_MAX_POINTS_X); return true; } void BedMeshScreen::onEntry() { mydata.highlightedTag = 0; + mydata.zAdjustment = 0; mydata.count = GRID_MAX_POINTS; mydata.message = mydata.MSG_NONE; BaseScreen::onEntry(); } -float BedMeshScreen::getHightlightedValue() { - if (mydata.highlightedTag) { - xy_uint8_t pt; - tagToPoint(mydata.highlightedTag, pt.x, pt.y); - return ExtUI::getMeshPoint(pt); +float BedMeshScreen::getHighlightedValue(bool nanAsZero) { + xy_uint8_t pt; + if (tagToPoint(mydata.highlightedTag, pt)) { + const float val = ExtUI::getMeshPoint(pt); + return (isnan(val) && nanAsZero) ? 0 : val; } return NAN; } -void BedMeshScreen::drawHighlightedPointValue() { - char str[16]; - const float val = getHightlightedValue(); - const bool isGood = !isnan(val); - if (isGood) - dtostrf(val, 5, 3, str); - else - strcpy_P(str, PSTR("-")); +void BedMeshScreen::setHighlightedValue(float value) { + xy_uint8_t pt; + if (tagToPoint(mydata.highlightedTag, pt)) + ExtUI::setMeshPoint(pt, value); +} +void BedMeshScreen::moveToHighlightedValue() { + xy_uint8_t pt; + if (tagToPoint(mydata.highlightedTag, pt)) + ExtUI::moveToMeshPoint(pt, gaugeThickness + mydata.zAdjustment); +} + +void BedMeshScreen::adjustHighlightedValue(float increment) { + mydata.zAdjustment += increment; + moveToHighlightedValue(); +} + +void BedMeshScreen::saveAdjustedHighlightedValue() { + if(mydata.zAdjustment) { + BedMeshScreen::setHighlightedValue(BedMeshScreen::getHighlightedValue(true) + mydata.zAdjustment); + mydata.zAdjustment = 0; + } +} + +void BedMeshScreen::changeHighlightedValue(uint8_t tag) { + saveAdjustedHighlightedValue(); + mydata.highlightedTag = tag; + moveToHighlightedValue(); +} + +void BedMeshScreen::drawHighlightedPointValue() { CommandProcessor cmd; cmd.font(Theme::font_medium) + .colors(normal_btn) .text(Z_LABEL_POS, GET_TEXT_F(MSG_MESH_EDIT_Z)) - .text(Z_VALUE_POS, str) - .colors(action_btn) + .font(font_small); + draw_adjuster(cmd, Z_VALUE_POS, 2, getHighlightedValue(true) + mydata.zAdjustment, GET_TEXT_F(MSG_UNITS_MM), 4, 3); + cmd.colors(action_btn) .tag(1).button(OKAY_POS, GET_TEXT_F(MSG_BUTTON_OKAY)) .tag(0); @@ -292,19 +320,23 @@ void BedMeshScreen::onRedraw(draw_mode_t what) { } } -bool BedMeshScreen::onTouchStart(uint8_t tag) { - mydata.highlightedTag = tag; - return true; -} - bool BedMeshScreen::onTouchEnd(uint8_t tag) { + constexpr float increment = 0.01; switch (tag) { case 1: + saveAdjustedHighlightedValue(); + injectCommands_P(PSTR("G29 S1")); GOTO_PREVIOUS(); return true; + case 2: adjustHighlightedValue(-increment); break; + case 3: adjustHighlightedValue( increment); break; default: - return false; + if (tag >= 10) + changeHighlightedValue(tag); + else + return false; } + return true; } void BedMeshScreen::onMeshUpdate(const int8_t, const int8_t, const float) { @@ -341,4 +373,11 @@ void BedMeshScreen::startMeshProbe() { injectCommands_P(PSTR(BED_LEVELING_COMMANDS)); } +void BedMeshScreen::showMeshEditor() { + SpinnerDialogBox::enqueueAndWait_P(ExtUI::isMachineHomed() ? F("M420 S1") : F("G28\nM420 S1")); + // After the spinner, go to this screen. + current_screen.forget(); + PUSH_SCREEN(BedMeshScreen); +} + #endif // FTDI_BED_MESH_SCREEN diff --git a/Marlin/src/lcd/extui/lib/ftdi_eve_touch_ui/screens/bed_mesh_screen.h b/Marlin/src/lcd/extui/lib/ftdi_eve_touch_ui/screens/bed_mesh_screen.h index de72d863a..804bb57ad 100644 --- a/Marlin/src/lcd/extui/lib/ftdi_eve_touch_ui/screens/bed_mesh_screen.h +++ b/Marlin/src/lcd/extui/lib/ftdi_eve_touch_ui/screens/bed_mesh_screen.h @@ -32,6 +32,7 @@ struct BedMeshScreenData { } message; uint8_t count; uint8_t highlightedTag; + float zAdjustment; }; class BedMeshScreen : public BaseScreen, public CachedScreen { @@ -45,18 +46,22 @@ class BedMeshScreen : public BaseScreen, public CachedScreen