Skip to content

Commit

Permalink
Refactor Host Actions as HostUI singleton
Browse files Browse the repository at this point in the history
  • Loading branch information
thinkyhead committed Sep 29, 2021
1 parent fa8f232 commit da9d863
Show file tree
Hide file tree
Showing 32 changed files with 206 additions and 162 deletions.
7 changes: 7 additions & 0 deletions Marlin/src/HAL/shared/Marduino.h
Original file line number Diff line number Diff line change
Expand Up @@ -87,3 +87,10 @@
#endif

#include "progmem.h"

class __FlashStringHelper;
typedef const __FlashStringHelper* FSTR_P;
#ifndef FPSTR
#define FPSTR(S) (reinterpret_cast<FSTR_P>(S))
#endif
#define FTOP(S) (reinterpret_cast<const char*>(S))
5 changes: 3 additions & 2 deletions Marlin/src/HAL/shared/progmem.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@
#define PSTR(str) (str)
#endif
#ifndef F
#define F(str) (str)
class __FlashStringHelper;
#define F(str) (reinterpret_cast<const __FlashStringHelper *>(PSTR(str)))
#endif
#ifndef _SFR_BYTE
#define _SFR_BYTE(n) (n)
Expand Down Expand Up @@ -110,7 +111,7 @@
#define strrchr_P(str, c) strrchr((str), (c))
#endif
#ifndef strsep_P
#define strsep_P(strp, delim) strsep((strp), (delim))
#define strsep_P(pstr, delim) strsep((pstr), (delim))
#endif
#ifndef strspn_P
#define strspn_P(str, chrs) strspn((str), (chrs))
Expand Down
4 changes: 2 additions & 2 deletions Marlin/src/MarlinCore.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -918,7 +918,7 @@ void kill(PGM_P const lcd_error/*=nullptr*/, PGM_P const lcd_component/*=nullptr
SERIAL_ERROR_MSG(STR_ERR_KILLED);

#ifdef ACTION_ON_KILL
host_action_kill();
hostui.kill();
#endif

minkill(steppers_off);
Expand Down Expand Up @@ -1571,7 +1571,7 @@ void setup() {
#endif

#if ENABLED(HOST_PROMPT_SUPPORT)
SETUP_RUN(host_action_prompt_end());
SETUP_RUN(hostui.prompt_end());
#endif

#if HAS_TRINAMIC_CONFIG && DISABLED(PSU_DEFAULT_OFF)
Expand Down
2 changes: 1 addition & 1 deletion Marlin/src/core/multi_language.h
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ typedef const char Language_Str[];
#define GET_TEXT(MSG) GET_LANG(LCD_LANGUAGE)::MSG
#define MAX_LANG_CHARSIZE LANG_CHARSIZE
#endif
#define GET_TEXT_F(MSG) (const __FlashStringHelper*)GET_TEXT(MSG)
#define GET_TEXT_F(MSG) FPSTR(GET_TEXT(MSG))

#define GET_LANGUAGE_NAME(INDEX) GET_LANG(LCD_LANGUAGE_##INDEX)::LANGUAGE
#define LANG_CHARSIZE GET_TEXT(CHARSIZE)
Expand Down
3 changes: 0 additions & 3 deletions Marlin/src/core/types.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,6 @@

#include "../inc/MarlinConfigPre.h"

class __FlashStringHelper;
typedef const __FlashStringHelper *progmem_str;

//
// Conditional type assignment magic. For example...
//
Expand Down
2 changes: 1 addition & 1 deletion Marlin/src/feature/e_parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ class EmergencyParser {
case EP_M112: killed_by_M112 = true; break;
case EP_M410: quickstop_by_M410 = true; break;
#if ENABLED(HOST_PROMPT_SUPPORT)
case EP_M876SN: host_response_handler(M876_reason); break;
case EP_M876SN: hostui.handle_response(M876_reason); break;
#endif
#if ENABLED(REALTIME_REPORTING_COMMANDS)
case EP_GRBL_STATUS: report_current_position_moving(); break;
Expand Down
88 changes: 46 additions & 42 deletions Marlin/src/feature/host_actions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@

#if ENABLED(HOST_ACTION_COMMANDS)

#include "host_actions.h"

//#define DEBUG_HOST_ACTIONS

#include "host_actions.h"

#if ENABLED(ADVANCED_PAUSE_FEATURE)
#include "pause.h"
#include "../gcode/queue.h"
Expand All @@ -37,100 +37,104 @@
#include "runout.h"
#endif

void host_action(PGM_P const pstr, const bool eol) {
HostUI hostui;

host_enable_t HostUI::host_enable; // {0}

void HostUI::action(FSTR_P const fstr, const bool eol) {
PORT_REDIRECT(SerialMask::All);
SERIAL_ECHOPGM("//action:");
SERIAL_ECHOPGM_P(pstr);
SERIAL_ECHOPGM_P(FTOP(fstr));
if (eol) SERIAL_EOL();
}

#ifdef ACTION_ON_KILL
void host_action_kill() { host_action(PSTR(ACTION_ON_KILL)); }
void HostUI::kill() { action(F(ACTION_ON_KILL)); }
#endif
#ifdef ACTION_ON_PAUSE
void host_action_pause(const bool eol/*=true*/) { host_action(PSTR(ACTION_ON_PAUSE), eol); }
void HostUI::pause(const bool eol/*=true*/) { action(F(ACTION_ON_PAUSE), eol); }
#endif
#ifdef ACTION_ON_PAUSED
void host_action_paused(const bool eol/*=true*/) { host_action(PSTR(ACTION_ON_PAUSED), eol); }
void HostUI::paused(const bool eol/*=true*/) { action(F(ACTION_ON_PAUSED), eol); }
#endif
#ifdef ACTION_ON_RESUME
void host_action_resume() { host_action(PSTR(ACTION_ON_RESUME)); }
void HostUI::resume() { action(F(ACTION_ON_RESUME)); }
#endif
#ifdef ACTION_ON_RESUMED
void host_action_resumed() { host_action(PSTR(ACTION_ON_RESUMED)); }
void HostUI::resumed() { action(F(ACTION_ON_RESUMED)); }
#endif
#ifdef ACTION_ON_CANCEL
void host_action_cancel() { host_action(PSTR(ACTION_ON_CANCEL)); }
void HostUI::cancel() { action(F(ACTION_ON_CANCEL)); }
#endif
#ifdef ACTION_ON_START
void host_action_start() { host_action(PSTR(ACTION_ON_START)); }
void HostUI::start() { action(F(ACTION_ON_START)); }
#endif

#if ENABLED(HOST_PROMPT_SUPPORT)

PromptReason HostUI::host_prompt_reason = PROMPT_NOT_DEFINED;

PGMSTR(CONTINUE_STR, "Continue");
PGMSTR(DISMISS_STR, "Dismiss");

#if HAS_RESUME_CONTINUE
extern bool wait_for_user;
#endif

PromptReason host_prompt_reason = PROMPT_NOT_DEFINED;

void host_action_notify(const char * const message) {
void HostUI::notify(const char * const message) {
PORT_REDIRECT(SerialMask::All);
host_action(PSTR("notification "), false);
action(F("notification "), false);
SERIAL_ECHOLN(message);
}

void host_action_notify_P(PGM_P const message) {
void HostUI::notify_P(PGM_P const message) {
PORT_REDIRECT(SerialMask::All);
host_action(PSTR("notification "), false);
action(F("notification "), false);
SERIAL_ECHOLNPGM_P(message);
}

void host_action_prompt(PGM_P const ptype, const bool eol=true) {
void HostUI::prompt(FSTR_P const ptype, const bool eol=true) {
PORT_REDIRECT(SerialMask::All);
host_action(PSTR("prompt_"), false);
SERIAL_ECHOPGM_P(ptype);
action(F("prompt_"), false);
SERIAL_ECHOPGM_P(FTOP(ptype));
if (eol) SERIAL_EOL();
}

void host_action_prompt_plus(PGM_P const ptype, PGM_P const pstr, const char extra_char='\0') {
host_action_prompt(ptype, false);
void HostUI::prompt_plus(FSTR_P const ptype, FSTR_P const fstr, const char extra_char='\0') {
prompt(ptype, false);
PORT_REDIRECT(SerialMask::All);
SERIAL_CHAR(' ');
SERIAL_ECHOPGM_P(pstr);
SERIAL_ECHOPGM_P(FTOP(fstr));
if (extra_char != '\0') SERIAL_CHAR(extra_char);
SERIAL_EOL();
}
void host_action_prompt_begin(const PromptReason reason, PGM_P const pstr, const char extra_char/*='\0'*/) {
host_action_prompt_end();
void HostUI::prompt_begin(const PromptReason reason, FSTR_P const fstr, const char extra_char/*='\0'*/) {
prompt_end();
host_prompt_reason = reason;
host_action_prompt_plus(PSTR("begin"), pstr, extra_char);
prompt_plus(F("begin"), fstr, extra_char);
}
void host_action_prompt_button(PGM_P const pstr) { host_action_prompt_plus(PSTR("button"), pstr); }
void host_action_prompt_end() { host_action_prompt(PSTR("end")); }
void host_action_prompt_show() { host_action_prompt(PSTR("show")); }

void _host_prompt_show(PGM_P const btn1/*=nullptr*/, PGM_P const btn2/*=nullptr*/) {
if (btn1) host_action_prompt_button(btn1);
if (btn2) host_action_prompt_button(btn2);
host_action_prompt_show();
void HostUI::prompt_button(FSTR_P const fstr) { prompt_plus(F("button"), fstr); }
void HostUI::prompt_end() { prompt(F("end")); }
void HostUI::prompt_show() { prompt(F("show")); }

void HostUI::_host_prompt_show(FSTR_P const btn1, FSTR_P const btn2) {
if (btn1) prompt_button(btn1);
if (btn2) prompt_button(btn2);
prompt_show();
}
void host_prompt_do(const PromptReason reason, PGM_P const pstr, PGM_P const btn1/*=nullptr*/, PGM_P const btn2/*=nullptr*/) {
host_action_prompt_begin(reason, pstr);
void HostUI::prompt_do(const PromptReason reason, FSTR_P const fstr, FSTR_P const btn1/*=nullptr*/, FSTR_P const btn2/*=nullptr*/) {
prompt_begin(reason, fstr);
_host_prompt_show(btn1, btn2);
}
void host_prompt_do(const PromptReason reason, PGM_P const pstr, const char extra_char, PGM_P const btn1/*=nullptr*/, PGM_P const btn2/*=nullptr*/) {
host_action_prompt_begin(reason, pstr, extra_char);
void HostUI::prompt_do(const PromptReason reason, FSTR_P const fstr, const char extra_char, FSTR_P const btn1/*=nullptr*/, FSTR_P const btn2/*=nullptr*/) {
prompt_begin(reason, fstr, extra_char);
_host_prompt_show(btn1, btn2);
}

void filament_load_host_prompt() {
void HostUI::filament_load_prompt() {
const bool disable_to_continue = TERN0(HAS_FILAMENT_SENSOR, runout.filament_ran_out);
host_prompt_do(PROMPT_FILAMENT_RUNOUT, PSTR("Paused"), PSTR("PurgeMore"),
disable_to_continue ? PSTR("DisableRunout") : CONTINUE_STR
prompt_do(PROMPT_FILAMENT_RUNOUT, F("Paused"), F("PurgeMore"),
disable_to_continue ? F("DisableRunout") : FPSTR(CONTINUE_STR)
);
}

Expand All @@ -141,7 +145,7 @@ void host_action(PGM_P const pstr, const bool eol) {
// - Resume Print response
// - Dismissal of info
//
void host_response_handler(const uint8_t response) {
void HostUI::handle_response(const uint8_t response) {
const PromptReason hpr = host_prompt_reason;
host_prompt_reason = PROMPT_NOT_DEFINED; // Reset now ahead of logic
switch (hpr) {
Expand Down
107 changes: 67 additions & 40 deletions Marlin/src/feature/host_actions.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,34 +24,16 @@
#include "../inc/MarlinConfigPre.h"
#include "../HAL/shared/Marduino.h"

void host_action(PGM_P const pstr, const bool eol=true);

#ifdef ACTION_ON_KILL
void host_action_kill();
#endif
#ifdef ACTION_ON_PAUSE
void host_action_pause(const bool eol=true);
#endif
#ifdef ACTION_ON_PAUSED
void host_action_paused(const bool eol=true);
#endif
#ifdef ACTION_ON_RESUME
void host_action_resume();
#endif
#ifdef ACTION_ON_RESUMED
void host_action_resumed();
#endif
#ifdef ACTION_ON_CANCEL
void host_action_cancel();
#endif
#ifdef ACTION_ON_START
void host_action_start();
#endif
typedef union {
uint8_t bits;
bool info:1,
errors:1,
debug:1,
muted:1;
} host_enable_t;

#if ENABLED(HOST_PROMPT_SUPPORT)

extern const char CONTINUE_STR[], DISMISS_STR[];

enum PromptReason : uint8_t {
PROMPT_NOT_DEFINED,
PROMPT_FILAMENT_RUNOUT,
Expand All @@ -61,21 +43,66 @@ void host_action(PGM_P const pstr, const bool eol=true);
PROMPT_INFO
};

extern PromptReason host_prompt_reason;
#endif

void host_response_handler(const uint8_t response);
void host_action_notify(const char * const message);
void host_action_notify_P(PGM_P const message);
void host_action_prompt_begin(const PromptReason reason, PGM_P const pstr, const char extra_char='\0');
void host_action_prompt_button(PGM_P const pstr);
void host_action_prompt_end();
void host_action_prompt_show();
void host_prompt_do(const PromptReason reason, PGM_P const pstr, PGM_P const btn1=nullptr, PGM_P const btn2=nullptr);
void host_prompt_do(const PromptReason reason, PGM_P const pstr, const char extra_char, PGM_P const btn1=nullptr, PGM_P const btn2=nullptr);
inline void host_prompt_open(const PromptReason reason, PGM_P const pstr, PGM_P const btn1=nullptr, PGM_P const btn2=nullptr) {
if (host_prompt_reason == PROMPT_NOT_DEFINED) host_prompt_do(reason, pstr, btn1, btn2);
}
class HostUI {
private:
static void action(FSTR_P const fstr, const bool eol=true);

void filament_load_host_prompt();
public:
static host_enable_t host_enable;

#endif
#ifdef ACTION_ON_KILL
static void kill();
#endif
#ifdef ACTION_ON_PAUSE
static void pause(const bool eol=true);
#endif
#ifdef ACTION_ON_PAUSED
static void paused(const bool eol=true);
#endif
#ifdef ACTION_ON_RESUME
static void resume();
#endif
#ifdef ACTION_ON_RESUMED
static void resumed();
#endif
#ifdef ACTION_ON_CANCEL
static void cancel();
#endif
#ifdef ACTION_ON_START
static void start();
#endif

#if ENABLED(HOST_PROMPT_SUPPORT)
private:
static void _host_prompt_show(FSTR_P const btn1, FSTR_P const btn2);

public:
static PromptReason host_prompt_reason;

static void handle_response(const uint8_t response);

static void notify_P(PGM_P const message);
static inline void notify(FSTR_P const fmsg) { notify_P(FTOP(fmsg)); }
static void notify(const char * const message);

static void prompt_begin(const PromptReason reason, FSTR_P const fstr, const char extra_char='\0');
static void prompt_button(FSTR_P const fstr);
static void prompt_end();
static void prompt_show();
static void prompt_do(const PromptReason reason, FSTR_P const pstr, FSTR_P const btn1=nullptr, FSTR_P const btn2=nullptr);
static void prompt_do(const PromptReason reason, FSTR_P const pstr, const char extra_char, FSTR_P const btn1=nullptr, FSTR_P const btn2=nullptr);
static inline void prompt_open(const PromptReason reason, FSTR_P const pstr, FSTR_P const btn1=nullptr, FSTR_P const btn2=nullptr) {
if (host_prompt_reason == PROMPT_NOT_DEFINED) prompt_do(reason, pstr, btn1, btn2);
}

static void filament_load_prompt();

#endif

};

extern HostUI hostui;

extern const char CONTINUE_STR[], DISMISS_STR[];
2 changes: 1 addition & 1 deletion Marlin/src/feature/mmu/mmu2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -960,7 +960,7 @@ bool MMU2::eject_filament(const uint8_t index, const bool recover) {
if (recover) {
LCD_MESSAGEPGM(MSG_MMU2_EJECT_RECOVER);
BUZZ(200, 404);
TERN_(HOST_PROMPT_SUPPORT, host_prompt_do(PROMPT_USER_CONTINUE, PSTR("MMU2 Eject Recover"), CONTINUE_STR));
TERN_(HOST_PROMPT_SUPPORT, hostui.prompt_do(PROMPT_USER_CONTINUE, F("MMU2 Eject Recover"), FPSTR(CONTINUE_STR)));
TERN_(EXTENSIBLE_UI, ExtUI::onUserConfirmRequired_P(PSTR("MMU2 Eject Recover")));
TERN_(HAS_RESUME_CONTINUE, wait_for_user_response());
BUZZ(200, 404);
Expand Down
Loading

0 comments on commit da9d863

Please sign in to comment.