Skip to content

Commit

Permalink
♻️ Consolidate DGUSScreenHandler class (MarlinFirmware#24067)
Browse files Browse the repository at this point in the history
  • Loading branch information
thinkyhead authored and LCh-77 committed May 11, 2022
1 parent 50b6bde commit 5a008ad
Show file tree
Hide file tree
Showing 14 changed files with 420 additions and 1,040 deletions.
6 changes: 0 additions & 6 deletions Marlin/src/lcd/extui/dgus/DGUSDisplay.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -109,12 +109,6 @@ void DGUSDisplay::WriteVariable(uint16_t adr, int8_t value) {
WriteVariable(adr, static_cast<const void*>(&value), sizeof(int8_t));
}

#if ENABLED(DGUS_LCD_UI_MKS)
void DGUSDisplay::MKS_WriteVariable(uint16_t adr, uint8_t value) {
WriteVariable(adr, static_cast<const void *>(&value), sizeof(uint8_t));
}
#endif

void DGUSDisplay::WriteVariable(uint16_t adr, long value) {
union { long l; char lb[4]; } endian;
char tmp[4];
Expand Down
5 changes: 2 additions & 3 deletions Marlin/src/lcd/extui/dgus/DGUSDisplay.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ typedef enum : uint8_t {
DGUS_WAIT_TELEGRAM, //< LEN received, Waiting for to receive all bytes.
} rx_datagram_state_t;

constexpr uint16_t swap16(const uint16_t value) { return (value & 0xFFU) << 8U | (value >> 8U); }

// Low-Level access to the display.
class DGUSDisplay {
public:
Expand All @@ -66,8 +68,6 @@ class DGUSDisplay {
static void WriteVariable(uint16_t adr, uint8_t value);
static void WriteVariable(uint16_t adr, int8_t value);
static void WriteVariable(uint16_t adr, long value);
static void MKS_WriteVariable(uint16_t adr, uint8_t value);


// Utility functions for bridging ui_api and dbus
template<typename T, float(*Getter)(const T), T selector, typename WireType=uint16_t>
Expand Down Expand Up @@ -105,7 +105,6 @@ class DGUSDisplay {
static void WritePGM(const char str[], uint8_t len);
static void ProcessRx();

static uint16_t swap16(const uint16_t value) { return (value & 0xFFU) << 8U | (value >> 8U); }
static rx_datagram_state_t rx_datagram_state;
static uint8_t rx_datagram_len;
static bool Initialized, no_reentrance;
Expand Down
2 changes: 1 addition & 1 deletion Marlin/src/lcd/extui/dgus/DGUSScreenHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
#include "../../../feature/powerloss.h"
#endif

DGUSScreenHandler ScreenHandler;
DGUSScreenHandlerClass ScreenHandler;

uint16_t DGUSScreenHandler::ConfirmVP;

Expand Down
5 changes: 1 addition & 4 deletions Marlin/src/lcd/extui/dgus/DGUSScreenHandler.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,6 @@

#endif

// endianness swap
inline uint16_t swap16(const uint16_t value) { return (value & 0xFFU) << 8U | (value >> 8U); }

#if ENABLED(DGUS_LCD_UI_ORIGIN)
#include "origin/DGUSScreenHandler.h"
#elif ENABLED(DGUS_LCD_UI_MKS)
Expand All @@ -55,7 +52,7 @@ inline uint16_t swap16(const uint16_t value) { return (value & 0xFFU) << 8U | (v
#include "hiprecy/DGUSScreenHandler.h"
#endif

extern DGUSScreenHandler ScreenHandler;
extern DGUSScreenHandlerClass ScreenHandler;

// Helper to define a DGUS_VP_Variable for common use-cases.
#define VPHELPER(VPADR, VPADRVAR, RXFPTR, TXFPTR) { \
Expand Down
241 changes: 241 additions & 0 deletions Marlin/src/lcd/extui/dgus/DGUSScreenHandlerBase.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,241 @@
/**
* Marlin 3D Printer Firmware
* Copyright (c) 2022 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
*
* Based on Sprinter and grbl.
* Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm
*
* 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.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
*/
#pragma once

#include "DGUSDisplay.h"
#include "DGUSVPVariable.h"
#include "DGUSDisplayDef.h"

#include "../../../inc/MarlinConfig.h"

enum DGUSLCD_Screens : uint8_t;

class DGUSScreenHandler {
public:
DGUSScreenHandler() = default;

static bool loop();

// Send all 4 strings that are displayed on the infoscreen, confirmation screen and kill screen
// The bools specifying whether the strings are in RAM or FLASH.
static void sendinfoscreen(PGM_P const line1, PGM_P const line2, PGM_P const line3, PGM_P const line4, bool l1inflash, bool l2inflash, bool l3inflash, bool liinflash);
static void sendinfoscreen(FSTR_P const line1, FSTR_P const line2, PGM_P const line3, PGM_P const line4, bool l1inflash, bool l2inflash, bool l3inflash, bool liinflash) {
sendinfoscreen(FTOP(line1), FTOP(line2), line3, line4, l1inflash, l2inflash, l3inflash, liinflash);
}
static void sendinfoscreen(FSTR_P const line1, FSTR_P const line2, FSTR_P const line3, FSTR_P const line4, bool l1inflash, bool l2inflash, bool l3inflash, bool liinflash) {
sendinfoscreen(FTOP(line1), FTOP(line2), FTOP(line3), FTOP(line4), l1inflash, l2inflash, l3inflash, liinflash);
}

static void HandleUserConfirmationPopUp(uint16_t ConfirmVP, PGM_P const line1, PGM_P const line2, PGM_P const line3, PGM_P const line4, bool l1inflash, bool l2inflash, bool l3inflash, bool liinflash);

// "M117" Message -- msg is a RAM ptr.
static void setstatusmessage(const char *msg);
// The same for messages from Flash
static void setstatusmessagePGM(PGM_P const msg);
// Callback for VP "Display wants to change screen on idle printer"
static void ScreenChangeHookIfIdle(DGUS_VP_Variable &var, void *val_ptr);
// Callback for VP "Screen has been changed"
static void ScreenChangeHook(DGUS_VP_Variable &var, void *val_ptr);

// Callback for VP "All Heaters Off"
static void HandleAllHeatersOff(DGUS_VP_Variable &var, void *val_ptr);
// Hook for "Change this temperature"
static void HandleTemperatureChanged(DGUS_VP_Variable &var, void *val_ptr);
// Hook for "Change Flowrate"
static void HandleFlowRateChanged(DGUS_VP_Variable &var, void *val_ptr);
#if ENABLED(DGUS_UI_MOVE_DIS_OPTION)
// Hook for manual move option
static void HandleManualMoveOption(DGUS_VP_Variable &var, void *val_ptr);
#endif

// Hook for manual move.
static void HandleManualMove(DGUS_VP_Variable &var, void *val_ptr);
// Hook for manual extrude.
static void HandleManualExtrude(DGUS_VP_Variable &var, void *val_ptr);
// Hook for motor lock and unlook
static void HandleMotorLockUnlock(DGUS_VP_Variable &var, void *val_ptr);
#if ENABLED(POWER_LOSS_RECOVERY)
// Hook for power loss recovery.
static void HandlePowerLossRecovery(DGUS_VP_Variable &var, void *val_ptr);
#endif
// Hook for settings
static void HandleSettings(DGUS_VP_Variable &var, void *val_ptr);
static void HandleStepPerMMChanged(DGUS_VP_Variable &var, void *val_ptr);
static void HandleStepPerMMExtruderChanged(DGUS_VP_Variable &var, void *val_ptr);

#if HAS_PID_HEATING
// Hook for "Change this temperature PID para"
static void HandleTemperaturePIDChanged(DGUS_VP_Variable &var, void *val_ptr);
// Hook for PID autotune
static void HandlePIDAutotune(DGUS_VP_Variable &var, void *val_ptr);
#endif
#if HAS_BED_PROBE
// Hook for "Change probe offset z"
static void HandleProbeOffsetZChanged(DGUS_VP_Variable &var, void *val_ptr);
#endif
#if ENABLED(BABYSTEPPING)
// Hook for live z adjust action
static void HandleLiveAdjustZ(DGUS_VP_Variable &var, void *val_ptr);
#endif
#if HAS_FAN
// Hook for fan control
static void HandleFanControl(DGUS_VP_Variable &var, void *val_ptr);
#endif
// Hook for heater control
static void HandleHeaterControl(DGUS_VP_Variable &var, void *val_ptr);
#if ENABLED(DGUS_PREHEAT_UI)
// Hook for preheat
static void HandlePreheat(DGUS_VP_Variable &var, void *val_ptr);
#endif
#if ENABLED(DGUS_FILAMENT_LOADUNLOAD)
// Hook for filament load and unload filament option
static void HandleFilamentOption(DGUS_VP_Variable &var, void *val_ptr);
// Hook for filament load and unload
static void HandleFilamentLoadUnload(DGUS_VP_Variable &var);
#endif

#if ENABLED(SDSUPPORT)
// Callback for VP "Display wants to change screen when there is a SD card"
static void ScreenChangeHookIfSD(DGUS_VP_Variable &var, void *val_ptr);
// Scroll buttons on the file listing screen.
static void DGUSLCD_SD_ScrollFilelist(DGUS_VP_Variable &var, void *val_ptr);
// File touched.
static void DGUSLCD_SD_FileSelected(DGUS_VP_Variable &var, void *val_ptr);
// start print after confirmation received.
static void DGUSLCD_SD_StartPrint(DGUS_VP_Variable &var, void *val_ptr);
// User hit the pause, resume or abort button.
static void DGUSLCD_SD_ResumePauseAbort(DGUS_VP_Variable &var, void *val_ptr);
// User confirmed the abort action
static void DGUSLCD_SD_ReallyAbort(DGUS_VP_Variable &var, void *val_ptr);
// User hit the tune button
static void DGUSLCD_SD_PrintTune(DGUS_VP_Variable &var, void *val_ptr);
// Send a single filename to the display.
static void DGUSLCD_SD_SendFilename(DGUS_VP_Variable &var);
// Marlin informed us that a new SD has been inserted.
static void SDCardInserted();
// Marlin informed us that the SD Card has been removed().
static void SDCardRemoved();
// Marlin informed us about a bad SD Card.
static void SDCardError();
#endif

// OK Button on the Confirm screen.
static void ScreenConfirmedOK(DGUS_VP_Variable &var, void *val_ptr);

// Update data after going to a new screen (by display or by GotoScreen)
// remember to store the last-displayed screen so it can be restored.
// (e.g., for popup messages)
static void UpdateNewScreen(DGUSLCD_Screens newscreen, bool popup=false);

// Recall the remembered screen.
static void PopToOldScreen();

// Make the display show the screen and update all VPs in it.
static void GotoScreen(DGUSLCD_Screens screen, bool ispopup = false);

static void UpdateScreenVPData();

// Helpers to convert and transfer data to the display.
static void DGUSLCD_SendWordValueToDisplay(DGUS_VP_Variable &var);
static void DGUSLCD_SendStringToDisplay(DGUS_VP_Variable &var);
static void DGUSLCD_SendStringToDisplayPGM(DGUS_VP_Variable &var);
static void DGUSLCD_SendTemperaturePID(DGUS_VP_Variable &var);
static void DGUSLCD_SendPercentageToDisplay(DGUS_VP_Variable &var);
static void DGUSLCD_SendPrintProgressToDisplay(DGUS_VP_Variable &var);
static void DGUSLCD_SendPrintTimeToDisplay(DGUS_VP_Variable &var);

#if ENABLED(PRINTCOUNTER)
static void DGUSLCD_SendPrintAccTimeToDisplay(DGUS_VP_Variable &var);
static void DGUSLCD_SendPrintsTotalToDisplay(DGUS_VP_Variable &var);
#endif
#if HAS_FAN
static void DGUSLCD_SendFanStatusToDisplay(DGUS_VP_Variable &var);
#endif
static void DGUSLCD_SendHeaterStatusToDisplay(DGUS_VP_Variable &var);
#if ENABLED(DGUS_UI_WAITING)
static void DGUSLCD_SendWaitingStatusToDisplay(DGUS_VP_Variable &var);
#endif

// Send a value from 0..100 to a variable with a range from 0..255
static void DGUSLCD_PercentageToUint8(DGUS_VP_Variable &var, void *val_ptr);

template<typename T>
static void DGUSLCD_SetValueDirectly(DGUS_VP_Variable &var, void *val_ptr) {
if (!var.memadr) return;
union { unsigned char tmp[sizeof(T)]; T t; } x;
unsigned char *ptr = (unsigned char*)val_ptr;
LOOP_L_N(i, sizeof(T)) x.tmp[i] = ptr[sizeof(T) - i - 1];
*(T*)var.memadr = x.t;
}

// Send a float value to the display.
// Display will get a 4-byte integer scaled to the number of digits:
// Tell the display the number of digits and it cheats by displaying a dot between...
template<unsigned int decimals>
static void DGUSLCD_SendFloatAsLongValueToDisplay(DGUS_VP_Variable &var) {
if (var.memadr) {
float f = *(float *)var.memadr;
f *= cpow(10, decimals);
dgusdisplay.WriteVariable(var.VP, (long)f);
}
}

// Send a float value to the display.
// Display will get a 2-byte integer scaled to the number of digits:
// Tell the display the number of digits and it cheats by displaying a dot between...
template<unsigned int decimals>
static void DGUSLCD_SendFloatAsIntValueToDisplay(DGUS_VP_Variable &var) {
if (var.memadr) {
float f = *(float *)var.memadr;
DEBUG_ECHOLNPAIR_F(" >> ", f, 6);
f *= cpow(10, decimals);
dgusdisplay.WriteVariable(var.VP, (int16_t)f);
}
}

// Force an update of all VP on the current screen.
static void ForceCompleteUpdate() { update_ptr = 0; ScreenComplete = false; }
// Has all VPs sent to the screen
static bool IsScreenComplete() { return ScreenComplete; }

static DGUSLCD_Screens getCurrentScreen() { return current_screen; }

static void SetupConfirmAction( void (*f)()) { confirm_action_cb = f; }

protected:
static DGUSLCD_Screens current_screen; //< currently on screen
static constexpr uint8_t NUM_PAST_SCREENS = 4;
static DGUSLCD_Screens past_screens[NUM_PAST_SCREENS]; //< LIFO with past screens for the "back" button.

static uint8_t update_ptr; //< Last sent entry in the VPList for the actual screen.
static uint16_t skipVP; //< When updating the screen data, skip this one, because the user is interacting with it.
static bool ScreenComplete; //< All VPs sent to screen?

static uint16_t ConfirmVP; //< context for confirm screen (VP that will be emulated-sent on "OK").

#if ENABLED(SDSUPPORT)
static int16_t top_file; //< file on top of file chooser
static int16_t file_to_print; //< touched file to be confirmed
#endif

static void (*confirm_action_cb)();
};
16 changes: 6 additions & 10 deletions Marlin/src/lcd/extui/dgus/fysetc/DGUSScreenHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -333,9 +333,9 @@ void DGUSScreenHandler::HandleManualMove(DGUS_VP_Variable &var, void *val_ptr) {
if (filament_data.action == 0) { // Go back to utility screen
#if HAS_HOTEND
thermalManager.setTargetHotend(e_temp, ExtUI::extruder_t::E0);
#endif
#if HAS_MULTI_HOTEND
thermalManager.setTargetHotend(e_temp, ExtUI::extruder_t::E1);
#if HAS_MULTI_HOTEND
thermalManager.setTargetHotend(e_temp, ExtUI::extruder_t::E1);
#endif
#endif
GotoScreen(DGUSLCD_SCREEN_UTILITY);
}
Expand All @@ -348,7 +348,7 @@ void DGUSScreenHandler::HandleManualMove(DGUS_VP_Variable &var, void *val_ptr) {
thermalManager.setTargetHotend(e_temp, filament_data.extruder);
break;
#endif
#if HAS_MULTI_EXTRUDER
#if HAS_MULTI_HOTEND
case VP_E1_FILAMENT_LOAD_UNLOAD:
filament_data.extruder = ExtUI::extruder_t::E1;
thermalManager.setTargetHotend(e_temp, filament_data.extruder);
Expand Down Expand Up @@ -413,14 +413,10 @@ bool DGUSScreenHandler::loop() {

if (!booted && ELAPSED(ms, BOOTSCREEN_TIMEOUT)) {
booted = true;

if (TERN0(POWER_LOSS_RECOVERY, recovery.valid()))
GotoScreen(DGUSLCD_SCREEN_POWER_LOSS);
else
GotoScreen(DGUSLCD_SCREEN_MAIN);
GotoScreen(TERN0(POWER_LOSS_RECOVERY, recovery.valid()) ? DGUSLCD_SCREEN_POWER_LOSS : DGUSLCD_SCREEN_MAIN);
}

#endif

return IsScreenComplete();
}

Expand Down
Loading

0 comments on commit 5a008ad

Please sign in to comment.