Skip to content

Commit

Permalink
common code
Browse files Browse the repository at this point in the history
  • Loading branch information
thinkyhead committed Jul 16, 2023
1 parent 3397e46 commit c2e5d5f
Show file tree
Hide file tree
Showing 12 changed files with 349 additions and 708 deletions.
7 changes: 5 additions & 2 deletions Marlin/src/lcd/tft/touch.h
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ class Touch {

static bool get_point(int16_t *x, int16_t *y);
static void touch(touch_control_t *control);
static void hold(touch_control_t *control, millis_t delay = 0);
static void hold(touch_control_t *control, millis_t delay=0);

public:
static void init();
Expand All @@ -121,7 +121,10 @@ class Touch {
static void sleepTimeout();
static void wakeUp();
#endif
static void add_control(TouchControlType type, uint16_t x, uint16_t y, uint16_t width, uint16_t height, intptr_t data = 0);
static void add_control(TouchControlType type, uint16_t x, uint16_t y, uint16_t width, uint16_t height, intptr_t data=0);
static void add_control(TouchControlType type, uint16_t x, uint16_t y, uint16_t width, uint16_t height, void (*handler)()) {
add_control(type, x, y, width, height, intptr_t(handler));
}
};

extern Touch touch;
1 change: 0 additions & 1 deletion Marlin/src/lcd/tft/ui_1024x600.h
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,6 @@
#define BUTTON_CONFIRM_Y TFT_HEIGHT - 80
#endif


// MarlinUI::draw_kill_screen()
#if !defined(KILL_SCREEN_STATUS_Y) && !defined(KILL_SCREEN_HALTED_Y) && !defined(KILL_SCREEN_RESET_Y)
#define KILL_SCREEN_STATUS_Y 94
Expand Down
2 changes: 0 additions & 2 deletions Marlin/src/lcd/tft/ui_240x320.h
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,6 @@
#define BUTTON_CONFIRM_Y TFT_HEIGHT - 64
#endif


// MarlinUI::draw_kill_screen()
#if !defined(KILL_SCREEN_STATUS_Y) && !defined(KILL_SCREEN_HALTED_Y) && !defined(KILL_SCREEN_RESET_Y)
#define KILL_SCREEN_STATUS_Y 104
Expand Down Expand Up @@ -140,7 +139,6 @@
#define COORDINATES_Y 104
#endif


#if !defined(E_MARK_X) && !defined(E_MARK_Y) && !defined(E_VALUE_X) && !defined(E_VALUE_Y)
#define E_MARK_X 10
#define E_MARK_Y VCENTER + FONT_LINE_HEIGHT
Expand Down
1 change: 0 additions & 1 deletion Marlin/src/lcd/tft/ui_320x240.h
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,6 @@
#define BUTTON_CONFIRM_Y TFT_HEIGHT - 64
#endif


// MarlinUI::draw_kill_screen()
#if !defined(KILL_SCREEN_STATUS_Y) && !defined(KILL_SCREEN_HALTED_Y) && !defined(KILL_SCREEN_RESET_Y)
#define KILL_SCREEN_STATUS_Y 60
Expand Down
1 change: 0 additions & 1 deletion Marlin/src/lcd/tft/ui_320x480.h
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,6 @@
#define BUTTON_CONFIRM_Y TFT_HEIGHT - 64
#endif


// MarlinUI::draw_kill_screen()
#if !defined(KILL_SCREEN_STATUS_Y) && !defined(KILL_SCREEN_HALTED_Y) && !defined(KILL_SCREEN_RESET_Y)
#define KILL_SCREEN_STATUS_Y 139
Expand Down
1 change: 0 additions & 1 deletion Marlin/src/lcd/tft/ui_480x272.h
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,6 @@
#define BUTTON_CONFIRM_Y TFT_HEIGHT - 64
#endif


// MarlinUI::draw_kill_screen()
#if !defined(KILL_SCREEN_STATUS_Y) && !defined(KILL_SCREEN_HALTED_Y) && !defined(KILL_SCREEN_RESET_Y)
#define KILL_SCREEN_STATUS_Y 62
Expand Down
1 change: 0 additions & 1 deletion Marlin/src/lcd/tft/ui_480x320.h
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,6 @@
#define BUTTON_CONFIRM_Y TFT_HEIGHT - 64
#endif


// MarlinUI::draw_kill_screen()
#if !defined(KILL_SCREEN_STATUS_Y) && !defined(KILL_SCREEN_HALTED_Y) && !defined(KILL_SCREEN_RESET_Y)
#define KILL_SCREEN_STATUS_Y 94
Expand Down
180 changes: 177 additions & 3 deletions Marlin/src/lcd/tft/ui_common.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,166 @@
#include "../lcdprint.h"
#include "../../libs/numtostr.h"
#include "../menu/menu.h"
#include "../../gcode/queue.h"
#include "../../module/temperature.h"
#include "../../module/planner.h"
#include "../../module/probe.h"

void menu_pause_option();
#if ENABLED(BABYSTEP_ZPROBE_OFFSET)
#include "../../feature/babystep.h"
#endif

motionAxisState_t motionAxisState;
static xy_uint_t cursor;

#if ENABLED(TOUCH_SCREEN)
bool draw_menu_navigation = false;
#endif

void menu_pause_option();

void quick_feedback() {
#if HAS_CHIRP
ui.chirp(); // Buzz and wait. Is the delay needed for buttons to settle?
#if ALL(HAS_MARLINUI_MENU, HAS_BEEPER)
for (int8_t i = 5; i--;) { buzzer.tick(); delay(2); }
#elif HAS_MARLINUI_MENU
delay(10);
#endif
#endif
}

void disable_steppers() {
quick_feedback();
queue.inject(F("M84"));
}

void moveAxis(const AxisEnum axis, const int8_t direction) {
quick_feedback();

#if HAS_EXTRUDERS
if (axis == E_AXIS && thermalManager.tooColdToExtrude(motionAxisState.e_selection)) {
drawMessage(F("Too cold"));
return;
}
#endif

const float diff = motionAxisState.currentStepSize * direction;

#if HAS_BED_PROBE

if (axis == Z_AXIS && motionAxisState.z_selection == Z_SELECTION_Z_PROBE) {
#if ENABLED(BABYSTEP_ZPROBE_OFFSET)
const int16_t babystep_increment = direction * BABYSTEP_SIZE_Z;
const bool do_probe = DISABLED(BABYSTEP_HOTEND_Z_OFFSET) || active_extruder == 0;
const float bsDiff = planner.mm_per_step[Z_AXIS] * babystep_increment,
new_probe_offset = probe.offset.z + bsDiff,
new_offs = TERN(BABYSTEP_HOTEND_Z_OFFSET
, do_probe ? new_probe_offset : hotend_offset[active_extruder].z - bsDiff
, new_probe_offset
);
if (WITHIN(new_offs, Z_PROBE_OFFSET_RANGE_MIN, Z_PROBE_OFFSET_RANGE_MAX)) {
babystep.add_steps(Z_AXIS, babystep_increment);
if (do_probe)
probe.offset.z = new_offs;
else
TERN(BABYSTEP_HOTEND_Z_OFFSET, hotend_offset[active_extruder].z = new_offs, NOOP);
drawMessage_P(NUL_STR); // Clear the error
drawAxisValue(axis);
}
else
drawMessage(GET_TEXT_F(MSG_LCD_SOFT_ENDSTOPS));

#else
// Only change probe.offset.z
probe.offset.z += diff;
if (direction < 0 && current_position.z < Z_PROBE_OFFSET_RANGE_MIN) {
current_position.z = Z_PROBE_OFFSET_RANGE_MIN;
drawMessage(GET_TEXT_F(MSG_LCD_SOFT_ENDSTOPS));
}
else if (direction > 0 && current_position.z > Z_PROBE_OFFSET_RANGE_MAX) {
current_position.z = Z_PROBE_OFFSET_RANGE_MAX;
drawMessage(GET_TEXT_F(MSG_LCD_SOFT_ENDSTOPS));
}
else
drawMessage_P(NUL_STR); // Clear the error

drawAxisValue(axis);
#endif
}

#endif // HAS_BED_PROBE

if (!ui.manual_move.processing) {
// Get motion limit from software endstops, if any
float min, max;
soft_endstop.get_manual_axis_limits(axis, min, max);

// Delta limits XY based on the current offset from center
// This assumes the center is 0,0
#if ENABLED(DELTA)
if (axis != Z_AXIS && TERN1(HAS_EXTRUDERS, axis != E_AXIS)) {
max = SQRT(sq(float(PRINTABLE_RADIUS)) - sq(current_position[Y_AXIS - axis])); // (Y_AXIS - axis) == the other axis
min = -max;
}
#endif

// Get the new position
const bool limited = ui.manual_move.apply_diff(axis, diff, min, max);
#if IS_KINEMATIC
UNUSED(limited);
#else
FSTR_P const msg = limited ? GET_TEXT_F(MSG_LCD_SOFT_ENDSTOPS) : FPSTR(NUL_STR);
drawMessage(msg);
#endif

ui.manual_move.soon(axis OPTARG(MULTI_E_MANUAL, motionAxisState.e_selection));
}

drawAxisValue(axis);
}

#if ENABLED(TOUCH_SCREEN)

void do_home() {
quick_feedback();
drawMessage(GET_TEXT_F(MSG_LEVEL_BED_HOMING));
queue.inject_P(G28_STR);
// Disable touch until home is done
touch.disable();
TERN_(HAS_EXTRUDERS, drawAxisValue(E_AXIS));
TERN_(HAS_X_AXIS, drawAxisValue(X_AXIS));
TERN_(HAS_Y_AXIS, drawAxisValue(Y_AXIS));
TERN_(HAS_Z_AXIS, drawAxisValue(Z_AXIS));
}

void step_size() {
motionAxisState.currentStepSize = motionAxisState.currentStepSize / 10.0;
if (motionAxisState.currentStepSize < 0.0015) motionAxisState.currentStepSize = 10.0;
quick_feedback();
drawCurStepValue();
}

#if HAS_EXTRUDERS
void e_select() {
if (++motionAxisState.e_selection >= EXTRUDERS)
motionAxisState.e_selection = 0;
quick_feedback();
drawCurESelection();
drawAxisValue(E_AXIS);
}
#endif

#if HAS_BED_PROBE
void z_select() {
motionAxisState.z_selection *= -1;
quick_feedback();
drawCurZSelection();
drawAxisValue(Z_AXIS);
}
#endif

#endif

#if HAS_TOUCH_SLEEP

bool lcd_sleep_task() {
Expand All @@ -56,7 +207,7 @@ static xy_uint_t cursor;
return false;
}

#endif
#endif // HAS_TOUCH_SLEEP

void text_line(const uint16_t y, uint16_t color) {
tft.canvas(0, y, TFT_WIDTH, MENU_ITEM_HEIGHT);
Expand Down Expand Up @@ -92,6 +243,29 @@ void add_control(uint16_t x, uint16_t y, TouchControlType control_type, intptr_t
#endif
}

void drawBtn(const int x, const int y, const char *label, intptr_t data, const MarlinImage btnimg, const MarlinImage img, uint16_t bgColor, const bool enabled) {
const uint16_t width = images[btnimg].width,
height = images[btnimg].height;

if (!enabled) bgColor = COLOR_CONTROL_DISABLED;

tft.canvas(x, y, width, height);
tft.set_background(COLOR_BACKGROUND);
tft.add_image(0, 0, btnimg, bgColor, COLOR_BACKGROUND, COLOR_DARKGREY);

// TODO: Make an add_text() taking a font arg
if (label) {
tft_string.set(label);
tft_string.trim();
tft.add_text(tft_string.center(width), height / 2 - tft_string.font_height() / 2, bgColor, tft_string);
}
else
tft.add_image(0, 0, img, bgColor, COLOR_BACKGROUND, COLOR_DARKGREY);

TERN_(TOUCH_SCREEN, if (enabled) touch.add_control(BUTTON, x, y, width, height, data));
}


//
// lcdprint.h functions
//
Expand Down
84 changes: 81 additions & 3 deletions Marlin/src/lcd/tft/ui_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,67 @@
#include "tft_font.h"
#include "tft_color.h"

// Common Implementation
#define Z_SELECTION_Z 1
#define Z_SELECTION_Z_PROBE -1

typedef struct {
#if HAS_X_AXIS
xy_int_t xValuePos;
#endif
#if HAS_Y_AXIS
xy_int_t yValuePos;
#endif
#if HAS_Z_AXIS
xy_int_t zValuePos, zTypePos;
int z_selection = Z_SELECTION_Z;
#endif
#if HAS_EXTRUDERS
xy_int_t eValuePos, eNamePos;
uint8_t e_selection = 0;
#endif
xy_int_t stepValuePos;
float currentStepSize = 10.0;
bool blocked = false;
char message[32];
} motionAxisState_t;

extern motionAxisState_t motionAxisState;

void moveAxis(const AxisEnum axis, const int8_t direction);

#if HAS_EXTRUDERS
inline void e_plus() { moveAxis(E_AXIS, +1); }
inline void e_minus() { moveAxis(E_AXIS, -1); }
#endif
#if HAS_X_AXIS
inline void x_minus() { moveAxis(X_AXIS, -1); }
inline void x_plus() { moveAxis(X_AXIS, +1); }
#endif
#if HAS_Y_AXIS
inline void y_plus() { moveAxis(Y_AXIS, +1); }
inline void y_minus() { moveAxis(Y_AXIS, -1); }
#endif
#if HAS_Z_AXIS
inline void z_plus() { moveAxis(Z_AXIS, +1); }
inline void z_minus() { moveAxis(Z_AXIS, -1); }
#endif
void quick_feedback();
void disable_steppers();
#if ENABLED(TOUCH_SCREEN)
void do_home();
void step_size();
#if HAS_BED_PROBE
void z_select();
#endif
#if HAS_EXTRUDERS
void e_select();
#endif
#endif
#if HAS_TOUCH_SLEEP
bool lcd_sleep_task();
#endif

void draw_heater_status(uint16_t x, uint16_t y, const int8_t Heater);
void draw_fan_status(uint16_t x, uint16_t y, const bool blink);

Expand All @@ -67,6 +128,12 @@ inline void add_control(
add_control(x, y, control_type, 0, image, is_enabled, color_enabled, color_disabled);
}
#if ENABLED(TOUCH_SCREEN)
inline void add_control(
uint16_t x, uint16_t y, TouchControlType control_type, screenFunc_t action, MarlinImage image, bool is_enabled=true,
uint16_t color_enabled=COLOR_CONTROL_ENABLED, uint16_t color_disabled=COLOR_CONTROL_DISABLED
) {
add_control(x, y, control_type, (intptr_t)action, image, is_enabled, color_enabled, color_disabled);
}
inline void add_control(
uint16_t x, uint16_t y, screenFunc_t screen, MarlinImage image, bool is_enabled=true,
uint16_t color_enabled=COLOR_CONTROL_ENABLED, uint16_t color_disabled=COLOR_CONTROL_DISABLED
Expand All @@ -75,9 +142,20 @@ inline void add_control(
}
#endif

#if HAS_TOUCH_SLEEP
bool lcd_sleep_task();
#endif
void drawBtn(const int x, const int y, const char *label, intptr_t data, const MarlinImage btnimg, const MarlinImage img, uint16_t bgColor, const bool enabled=true);
void drawBtn(const int x, const int y, const char *label, intptr_t data, const MarlinImage img, uint16_t bgColor, const bool enabled=true);
inline void drawBtn(const int x, const int y, const char *label, void (*handler)(), const MarlinImage img, uint16_t bgColor, const bool enabled=true) {
drawBtn(x, y, label, intptr_t(handler), img, bgColor, enabled);
}

// Custom Implementation
void drawMessage_P(PGM_P const msg);
inline void drawMessage(FSTR_P const fmsg) { drawMessage_P(FTOP(fmsg)); }

void drawAxisValue(const AxisEnum axis);
void drawCurZSelection();
void drawCurESelection();
void drawCurStepValue();

#define ABSOLUTE_ZERO -273.15

Expand Down
Loading

0 comments on commit c2e5d5f

Please sign in to comment.