Distinguish serial index from mask (#21287)

This commit is contained in:
X-Ryl669 2021-03-09 10:20:37 +01:00 committed by GitHub
parent 2f1fd4bbaa
commit 55c31fbe9a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
14 changed files with 135 additions and 101 deletions

View file

@ -62,7 +62,6 @@ extern uint8_t marlin_debug_flags;
//
// Serial redirection
//
#define SERIAL_ALL 0xFF
#if HAS_MULTI_SERIAL
#define _PORT_REDIRECT(n,p) REMEMBER(n,multiSerial.portMask,p)
#define _PORT_RESTORE(n,p) RESTORE(n)
@ -92,7 +91,7 @@ extern uint8_t marlin_debug_flags;
#define PORT_REDIRECT(p) _PORT_REDIRECT(1,p)
#define PORT_RESTORE() _PORT_RESTORE(1)
#define SERIAL_PORTMASK(P) _BV(P)
#define SERIAL_PORTMASK(P) SerialMask::from(P)
//
// SERIAL_CHAR - Print one or more individual chars

View file

@ -22,12 +22,29 @@
#pragma once
#include "../inc/MarlinConfigPre.h"
#include "macros.h"
#if ENABLED(EMERGENCY_PARSER)
#include "../feature/e_parser.h"
#endif
// Used in multiple places
// You can build it but not manipulate it.
// There are only few places where it's required to access the underlying member: GCodeQueue, SerialMask and MultiSerial
struct serial_index_t {
// A signed index, where -1 is a special case meaning no action (neither output or input)
int8_t index;
// Check if the index is within the range [a ... b]
constexpr inline bool within(const int8_t a, const int8_t b) const { return WITHIN(index, a, b); }
constexpr inline bool valid() const { return WITHIN(index, 0, 7); } // At most, 8 bits
// Construction is either from an index
constexpr serial_index_t(const int8_t index) : index(index) {}
// Default to "no index"
constexpr serial_index_t() : index(-1) {}
};
// flushTX is not implemented in all HAL, so use SFINAE to call the method where it is.
CALL_IF_EXISTS_IMPL(void, flushTX);
CALL_IF_EXISTS_IMPL(bool, connected, true);
@ -79,10 +96,10 @@ struct SerialBase {
void end() { static_cast<Child*>(this)->end(); }
/** Check for available data from the port
@param index The port index, usually 0 */
int available(uint8_t index = 0) { return static_cast<Child*>(this)->available(index); }
int available(serial_index_t index = 0) { return static_cast<Child*>(this)->available(index); }
/** Read a value from the port
@param index The port index, usually 0 */
int read(uint8_t index = 0) { return static_cast<Child*>(this)->read(index); }
int read(serial_index_t index = 0) { return static_cast<Child*>(this)->read(index); }
// Check if the serial port is connected (usually bypassed)
bool connected() { return static_cast<Child*>(this)->connected(); }
// Redirect flush

View file

@ -21,11 +21,32 @@
*/
#pragma once
#include "macros.h"
#include "serial_base.h"
// Used in multiple places
typedef int8_t serial_index_t;
// A mask containing a bitmap of the serial port to act upon
// This is written to ensure a serial index is never used as a serial mask
class SerialMask {
uint8_t mask;
// This constructor is private to ensure you can't convert an index to a mask
// The compiler will stop here if you are mixing index and mask in your code.
// If you need to, you'll have to use the explicit static "from" method here
SerialMask(const serial_index_t);
public:
inline constexpr bool enabled(const SerialMask PortMask) const { return mask & PortMask.mask; }
inline constexpr SerialMask combine(const SerialMask other) const { return SerialMask(mask | other.mask); }
inline constexpr SerialMask operator<< (const int offset) const { return SerialMask(mask << offset); }
static inline SerialMask from(const serial_index_t index) {
if (index.valid()) return SerialMask(_BV(index.index));
return SerialMask(0); // A invalid index mean no output
}
constexpr SerialMask(const uint8_t mask) : mask(mask) {}
constexpr SerialMask(const SerialMask & other) : mask(other.mask) {} // Can't use = default here since not all framework support this
static constexpr uint8_t All = 0xFF;
};
// The most basic serial class: it dispatch to the base serial class with no hook whatsoever. This will compile to nothing but the base serial class
template <class SerialT>
@ -39,8 +60,8 @@ struct BaseSerial : public SerialBase< BaseSerial<SerialT> >, public SerialT {
void msgDone() {}
// We don't care about indices here, since if one can call us, it's the right index anyway
int available(uint8_t) { return (int)SerialT::available(); }
int read(uint8_t) { return (int)SerialT::read(); }
int available(serial_index_t) { return (int)SerialT::available(); }
int read(serial_index_t) { return (int)SerialT::read(); }
bool connected() { return CALL_IF_EXISTS(bool, static_cast<SerialT*>(this), connected);; }
void flushTX() { CALL_IF_EXISTS(void, static_cast<SerialT*>(this), flushTX); }
@ -77,12 +98,11 @@ struct ConditionalSerial : public SerialBase< ConditionalSerial<SerialT> > {
bool connected() { return CALL_IF_EXISTS(bool, &out, connected); }
void flushTX() { CALL_IF_EXISTS(void, &out, flushTX); }
int available(uint8_t ) { return (int)out.available(); }
int read(uint8_t ) { return (int)out.read(); }
int available(serial_index_t ) { return (int)out.available(); }
int read(serial_index_t ) { return (int)out.read(); }
int available() { return (int)out.available(); }
int read() { return (int)out.read(); }
ConditionalSerial(bool & conditionVariable, SerialT & out, const bool e) : BaseClassT(e), condition(conditionVariable), out(out) {}
};
@ -102,8 +122,8 @@ struct ForwardSerial : public SerialBase< ForwardSerial<SerialT> > {
bool connected() { return Private::HasMember_connected<SerialT>::value ? CALL_IF_EXISTS(bool, &out, connected) : (bool)out; }
void flushTX() { CALL_IF_EXISTS(void, &out, flushTX); }
int available(uint8_t) { return (int)out.available(); }
int read(uint8_t) { return (int)out.read(); }
int available(serial_index_t) { return (int)out.available(); }
int read(serial_index_t) { return (int)out.read(); }
int available() { return (int)out.available(); }
int read() { return (int)out.read(); }
@ -130,8 +150,8 @@ struct RuntimeSerial : public SerialBase< RuntimeSerial<SerialT> >, public Seria
if (eofHook) eofHook(userPointer);
}
int available(uint8_t) { return (int)SerialT::available(); }
int read(uint8_t) { return (int)SerialT::read(); }
int available(serial_index_t) { return (int)SerialT::available(); }
int read(serial_index_t) { return (int)SerialT::read(); }
using SerialT::available;
using SerialT::read;
using SerialT::flush;
@ -170,53 +190,51 @@ template <class Serial0T, class Serial1T, const uint8_t offset = 0, const uint8_
struct MultiSerial : public SerialBase< MultiSerial<Serial0T, Serial1T, offset, step> > {
typedef SerialBase< MultiSerial<Serial0T, Serial1T, offset, step> > BaseClassT;
uint8_t portMask;
SerialMask portMask;
Serial0T & serial0;
Serial1T & serial1;
enum Masks {
UsageMask = ((1 << step) - 1), // A bit mask containing as many bits as step
FirstOutputMask = (UsageMask << offset),
SecondOutputMask = (UsageMask << (offset + step)),
AllMask = FirstOutputMask | SecondOutputMask,
};
static constexpr uint8_t Usage = ((1 << step) - 1); // A bit mask containing as many bits as step
static constexpr uint8_t FirstOutput = (Usage << offset);
static constexpr uint8_t SecondOutput = (Usage << (offset + step));
static constexpr uint8_t Both = FirstOutput | SecondOutput;
NO_INLINE size_t write(uint8_t c) {
size_t ret = 0;
if (portMask & FirstOutputMask) ret = serial0.write(c);
if (portMask & SecondOutputMask) ret = serial1.write(c) | ret;
if (portMask.enabled(FirstOutput)) ret = serial0.write(c);
if (portMask.enabled(SecondOutput)) ret = serial1.write(c) | ret;
return ret;
}
NO_INLINE void msgDone() {
if (portMask & FirstOutputMask) serial0.msgDone();
if (portMask & SecondOutputMask) serial1.msgDone();
if (portMask.enabled(FirstOutput)) serial0.msgDone();
if (portMask.enabled(SecondOutput)) serial1.msgDone();
}
int available(uint8_t index) {
if (index >= 0 + offset && index < step + offset)
int available(serial_index_t index) {
if (index.within(0 + offset, step + offset - 1))
return serial0.available(index);
else if (index >= step + offset && index < 2 * step + offset)
else if (index.within(step + offset, 2 * step + offset - 1))
return serial1.available(index);
return false;
}
int read(uint8_t index) {
if (index >= 0 + offset && index < step + offset)
int read(serial_index_t index) {
if (index.within(0 + offset, step + offset - 1))
return serial0.read(index);
else if (index >= step + offset && index < 2 * step + offset)
else if (index.within(step + offset, 2 * step + offset - 1))
return serial1.read(index);
return -1;
}
void begin(const long br) {
if (portMask & FirstOutputMask) serial0.begin(br);
if (portMask & SecondOutputMask) serial1.begin(br);
if (portMask.enabled(FirstOutput)) serial0.begin(br);
if (portMask.enabled(SecondOutput)) serial1.begin(br);
}
void end() {
if (portMask & FirstOutputMask) serial0.end();
if (portMask & SecondOutputMask) serial1.end();
if (portMask.enabled(FirstOutput)) serial0.end();
if (portMask.enabled(SecondOutput)) serial1.end();
}
bool connected() {
bool ret = true;
if (portMask & FirstOutputMask) ret = CALL_IF_EXISTS(bool, &serial0, connected);
if (portMask & SecondOutputMask) ret = ret && CALL_IF_EXISTS(bool, &serial1, connected);
if (portMask.enabled(FirstOutput)) ret = CALL_IF_EXISTS(bool, &serial0, connected);
if (portMask.enabled(SecondOutput)) ret = ret && CALL_IF_EXISTS(bool, &serial1, connected);
return ret;
}
@ -225,15 +243,15 @@ struct MultiSerial : public SerialBase< MultiSerial<Serial0T, Serial1T, offset,
// Redirect flush
NO_INLINE void flush() {
if (portMask & FirstOutputMask) serial0.flush();
if (portMask & SecondOutputMask) serial1.flush();
if (portMask.enabled(FirstOutput)) serial0.flush();
if (portMask.enabled(SecondOutput)) serial1.flush();
}
NO_INLINE void flushTX() {
if (portMask & FirstOutputMask) CALL_IF_EXISTS(void, &serial0, flushTX);
if (portMask & SecondOutputMask) CALL_IF_EXISTS(void, &serial1, flushTX);
if (portMask.enabled(FirstOutput)) CALL_IF_EXISTS(void, &serial0, flushTX);
if (portMask.enabled(SecondOutput)) CALL_IF_EXISTS(void, &serial1, flushTX);
}
MultiSerial(Serial0T & serial0, Serial1T & serial1, int8_t mask = AllMask, const bool e = false) :
MultiSerial(Serial0T & serial0, Serial1T & serial1, const SerialMask mask = Both, const bool e = false) :
BaseClassT(e),
portMask(mask), serial0(serial0), serial1(serial1) {}
};

View file

@ -38,7 +38,7 @@
#endif
void host_action(PGM_P const pstr, const bool eol) {
PORT_REDIRECT(SERIAL_ALL);
PORT_REDIRECT(SerialMask::All);
SERIAL_ECHOPGM("//action:");
SERIAL_ECHOPGM_P(pstr);
if (eol) SERIAL_EOL();
@ -78,19 +78,19 @@ void host_action(PGM_P const pstr, const bool eol) {
PromptReason host_prompt_reason = PROMPT_NOT_DEFINED;
void host_action_notify(const char * const message) {
PORT_REDIRECT(SERIAL_ALL);
PORT_REDIRECT(SerialMask::All);
host_action(PSTR("notification "), false);
SERIAL_ECHOLN(message);
}
void host_action_notify_P(PGM_P const message) {
PORT_REDIRECT(SERIAL_ALL);
PORT_REDIRECT(SerialMask::All);
host_action(PSTR("notification "), false);
SERIAL_ECHOLNPGM_P(message);
}
void host_action_prompt(PGM_P const ptype, const bool eol=true) {
PORT_REDIRECT(SERIAL_ALL);
PORT_REDIRECT(SerialMask::All);
host_action(PSTR("prompt_"), false);
SERIAL_ECHOPGM_P(ptype);
if (eol) SERIAL_EOL();
@ -98,7 +98,7 @@ void host_action(PGM_P const pstr, const bool eol) {
void host_action_prompt_plus(PGM_P const ptype, PGM_P const pstr, const char extra_char='\0') {
host_action_prompt(ptype, false);
PORT_REDIRECT(SERIAL_ALL);
PORT_REDIRECT(SerialMask::All);
SERIAL_CHAR(' ');
SERIAL_ECHOPGM_P(pstr);
if (extra_char != '\0') SERIAL_CHAR(extra_char);

View file

@ -142,7 +142,7 @@ struct MeatpackSerial : public SerialBase <MeatpackSerial < SerialT >> {
bool connected() { return Private::HasMember_connected<SerialT>::value ? CALL_IF_EXISTS(bool, &out, connected) : (bool)out; }
void flushTX() { CALL_IF_EXISTS(void, &out, flushTX); }
int available(uint8_t index) {
int available(serial_index_t index) {
// There is a potential issue here with multiserial, since it'll return its decoded buffer whatever the serial index here.
// So, instead of doing MeatpackSerial<MultiSerial<...>> we should do MultiSerial<MeatpackSerial<...>, MeatpackSerial<...>>
// TODO, let's fix this later on
@ -160,7 +160,7 @@ struct MeatpackSerial : public SerialBase <MeatpackSerial < SerialT >> {
return charCount;
}
int readImpl(const uint8_t index) {
int readImpl(const serial_index_t index) {
// Not enough char to make progress?
if (charCount == 0 && available(index) == 0) return -1;
@ -168,7 +168,7 @@ struct MeatpackSerial : public SerialBase <MeatpackSerial < SerialT >> {
return serialBuffer[readIndex++];
}
int read(uint8_t index) { return readImpl(index); }
int read(serial_index_t index) { return readImpl(index); }
int available() { return available(0); }
int read() { return readImpl(0); }

View file

@ -1067,7 +1067,7 @@ void GcodeSuite::process_subcommands_now(char * gcode) {
static millis_t next_busy_signal_ms = 0;
if (!autoreport_paused && host_keepalive_interval && busy_state != NOT_BUSY) {
if (PENDING(ms, next_busy_signal_ms)) return;
PORT_REDIRECT(SERIAL_ALL);
PORT_REDIRECT(SerialMask::All);
switch (busy_state) {
case IN_HANDLER:
case IN_PROCESS:

View file

@ -52,7 +52,7 @@ void GcodeSuite::M118() {
while (*p == ' ') ++p;
}
PORT_REDIRECT(WITHIN(port, 0, NUM_SERIAL) ? (port ? SERIAL_PORTMASK(port - 1) : SERIAL_ALL) : multiSerial.portMask);
PORT_REDIRECT(WITHIN(port, 0, NUM_SERIAL) ? (port ? SERIAL_PORTMASK(port - 1) : SerialMask::All) : multiSerial.portMask);
if (hasE) SERIAL_ECHO_START();
if (hasA) SERIAL_ECHOPGM("//");

View file

@ -240,7 +240,7 @@ void GCodeQueue::RingBuffer::ok_to_send() {
CommandLine &command = commands[index_r];
#if HAS_MULTI_SERIAL
const serial_index_t serial_ind = command.port;
if (serial_ind < 0) return;
if (!serial_ind.valid()) return; // Optimization here, skip processing if it's not going anywhere
PORT_REDIRECT(SERIAL_PORTMASK(serial_ind)); // Reply to the serial port that sent the command
#endif
if (command.skip_ok) return;
@ -264,15 +264,15 @@ void GCodeQueue::RingBuffer::ok_to_send() {
*/
void GCodeQueue::flush_and_request_resend(const serial_index_t serial_ind) {
#if HAS_MULTI_SERIAL
if (serial_ind < 0) return; // Never mind. Command came from SD or Flash Drive
if (!serial_ind.valid()) return; // Optimization here, skip if the command came from SD or Flash Drive
PORT_REDIRECT(SERIAL_PORTMASK(serial_ind)); // Reply to the serial port that sent the command
#endif
SERIAL_FLUSH();
SERIAL_ECHOPGM(STR_RESEND);
SERIAL_ECHOLN(serial_state[serial_ind].last_N + 1);
SERIAL_ECHOLN(serial_state[serial_ind.index].last_N + 1);
}
inline bool serial_data_available(uint8_t index) {
inline bool serial_data_available(serial_index_t index) {
const int a = SERIAL_IMPL.available(index);
#if BOTH(RX_BUFFER_MONITOR, RX_BUFFER_SIZE)
if (a > RX_BUFFER_SIZE - 2) {
@ -290,15 +290,15 @@ inline bool any_serial_data_available() {
return true;
}
inline int read_serial(const uint8_t index) { return SERIAL_IMPL.read(index); }
inline int read_serial(const serial_index_t index) { return SERIAL_IMPL.read(index); }
void GCodeQueue::gcode_line_error(PGM_P const err, const serial_index_t serial_ind) {
PORT_REDIRECT(SERIAL_PORTMASK(serial_ind)); // Reply to the serial port that sent the command
SERIAL_ERROR_START();
SERIAL_ECHOLNPAIR_P(err, serial_state[serial_ind].last_N);
SERIAL_ECHOLNPAIR_P(err, serial_state[serial_ind.index].last_N);
while (read_serial(serial_ind) != -1) { /* nada */ } // Clear out the RX buffer. Why don't use flush here ?
flush_and_request_resend(serial_ind);
serial_state[serial_ind].count = 0;
serial_state[serial_ind.index].count = 0;
}
FORCE_INLINE bool is_M29(const char * const cmd) { // matches "M29" & "M29 ", but not "M290", etc

View file

@ -79,13 +79,13 @@ public:
void commit_command(bool skip_ok
#if HAS_MULTI_SERIAL
, serial_index_t serial_ind=-1
, serial_index_t serial_ind = serial_index_t()
#endif
);
bool enqueue(const char* cmd, bool skip_ok = true
#if HAS_MULTI_SERIAL
, serial_index_t serial_ind=-1
, serial_index_t serial_ind = serial_index_t()
#endif
);
@ -197,7 +197,7 @@ public:
/**
* (Re)Set the current line number for the last received command
*/
static inline void set_current_line_number(long n) { serial_state[ring_buffer.command_port()].last_N = n; }
static inline void set_current_line_number(long n) { serial_state[ring_buffer.command_port().index].last_N = n; }
private:

View file

@ -82,7 +82,7 @@ void GcodeSuite::M1001() {
// Announce SD file completion
{
PORT_REDIRECT(SERIAL_ALL);
PORT_REDIRECT(SerialMask::All);
SERIAL_ECHOLNPGM(STR_FILE_PRINTED);
}

View file

@ -395,21 +395,21 @@ void DGUSScreenHandler::HandleTemperatureChanged(DGUS_VP_Variable &var, void *va
default: return;
#if HOTENDS >= 1
case VP_T_E0_Set:
NOMORE(newvalue, HEATER_0_MAXTEMP);
NOMORE(newvalue, (uint16_t)HEATER_0_MAXTEMP);
thermalManager.setTargetHotend(newvalue, 0);
acceptedvalue = thermalManager.degTargetHotend(0);
break;
#endif
#if HOTENDS >= 2
case VP_T_E1_Set:
NOMORE(newvalue, HEATER_1_MAXTEMP);
NOMORE(newvalue, (uint16_t)HEATER_1_MAXTEMP);
thermalManager.setTargetHotend(newvalue, 1);
acceptedvalue = thermalManager.degTargetHotend(1);
break;
#endif
#if HAS_HEATED_BED
case VP_T_Bed_Set:
NOMORE(newvalue, BED_MAXTEMP);
NOMORE(newvalue, (uint16_t)BED_MAXTEMP);
thermalManager.setTargetBed(newvalue);
acceptedvalue = thermalManager.degTargetBed();
break;

View file

@ -28,8 +28,8 @@ struct AutoReporter {
millis_t next_report_ms;
uint8_t report_interval;
#if HAS_MULTI_SERIAL
serial_index_t report_port_mask;
AutoReporter() : report_port_mask(SERIAL_ALL) {}
SerialMask report_port_mask;
AutoReporter() : report_port_mask(SerialMask::All) {}
#endif
inline void set_interval(uint8_t seconds, const uint8_t limit=60) {

View file

@ -329,7 +329,7 @@ const char str_t_thermal_runaway[] PROGMEM = STR_T_THERMAL_RUNAWAY,
*/
void Temperature::report_fan_speed(const uint8_t target) {
if (target >= FAN_COUNT) return;
PORT_REDIRECT(SERIAL_ALL);
PORT_REDIRECT(SerialMask::All);
SERIAL_ECHOLNPAIR("M106 P", target, " S", fan_speed[target]);
}
#endif

View file

@ -549,7 +549,7 @@ void openFailed(const char * const fname) {
void announceOpen(const uint8_t doing, const char * const path) {
if (doing) {
PORT_REDIRECT(SERIAL_ALL);
PORT_REDIRECT(SerialMask::All);
SERIAL_ECHO_START();
SERIAL_ECHOPGM("Now ");
SERIAL_ECHOPGM_P(doing == 1 ? PSTR("doing") : PSTR("fresh"));
@ -615,7 +615,7 @@ void CardReader::openFileRead(char * const path, const uint8_t subcall_type/*=0*
sdpos = 0;
{ // Don't remove this block, as the PORT_REDIRECT is a RAII
PORT_REDIRECT(SERIAL_ALL);
PORT_REDIRECT(SerialMask::All);
SERIAL_ECHOLNPAIR(STR_SD_FILE_OPENED, fname, STR_SD_SIZE, filesize);
SERIAL_ECHOLNPGM(STR_SD_FILE_SELECTED);
}