Distinguish serial index from mask (#21287)
This commit is contained in:
@@ -62,37 +62,36 @@ 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)
|
||||
#define SERIAL_ASSERT(P) if(multiSerial.portMask!=(P)){ debugger(); }
|
||||
#define _PORT_REDIRECT(n,p) REMEMBER(n,multiSerial.portMask,p)
|
||||
#define _PORT_RESTORE(n,p) RESTORE(n)
|
||||
#define SERIAL_ASSERT(P) if(multiSerial.portMask!=(P)){ debugger(); }
|
||||
#ifdef SERIAL_CATCHALL
|
||||
typedef MultiSerial<decltype(MYSERIAL), decltype(SERIAL_CATCHALL), 0> SerialOutputT;
|
||||
#else
|
||||
typedef MultiSerial<decltype(MYSERIAL0), TERN(HAS_ETHERNET, ConditionalSerial<decltype(MYSERIAL1)>, decltype(MYSERIAL1)), 0> SerialOutputT;
|
||||
typedef MultiSerial<decltype(MYSERIAL0), TERN(HAS_ETHERNET, ConditionalSerial<decltype(MYSERIAL1)>, decltype(MYSERIAL1)), 0> SerialOutputT;
|
||||
#endif
|
||||
extern SerialOutputT multiSerial;
|
||||
#define _SERIAL_IMPL multiSerial
|
||||
extern SerialOutputT multiSerial;
|
||||
#define _SERIAL_IMPL multiSerial
|
||||
#else
|
||||
#define _PORT_REDIRECT(n,p) NOOP
|
||||
#define _PORT_RESTORE(n) NOOP
|
||||
#define SERIAL_ASSERT(P) NOOP
|
||||
#define _SERIAL_IMPL MYSERIAL0
|
||||
#define _PORT_REDIRECT(n,p) NOOP
|
||||
#define _PORT_RESTORE(n) NOOP
|
||||
#define SERIAL_ASSERT(P) NOOP
|
||||
#define _SERIAL_IMPL MYSERIAL0
|
||||
#endif
|
||||
|
||||
#if ENABLED(MEATPACK)
|
||||
extern MeatpackSerial<decltype(_SERIAL_IMPL)> mpSerial;
|
||||
#define SERIAL_IMPL mpSerial
|
||||
#define SERIAL_IMPL mpSerial
|
||||
#else
|
||||
#define SERIAL_IMPL _SERIAL_IMPL
|
||||
#define SERIAL_IMPL _SERIAL_IMPL
|
||||
#endif
|
||||
|
||||
#define SERIAL_OUT(WHAT, V...) (void)SERIAL_IMPL.WHAT(V)
|
||||
|
||||
#define PORT_REDIRECT(p) _PORT_REDIRECT(1,p)
|
||||
#define PORT_RESTORE() _PORT_RESTORE(1)
|
||||
#define SERIAL_PORTMASK(P) _BV(P)
|
||||
#define PORT_REDIRECT(p) _PORT_REDIRECT(1,p)
|
||||
#define PORT_RESTORE() _PORT_RESTORE(1)
|
||||
#define SERIAL_PORTMASK(P) SerialMask::from(P)
|
||||
|
||||
//
|
||||
// SERIAL_CHAR - Print one or more individual chars
|
||||
|
@@ -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
|
||||
|
@@ -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,10 +60,10 @@ 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(); }
|
||||
bool connected() { return CALL_IF_EXISTS(bool, static_cast<SerialT*>(this), connected);; }
|
||||
void flushTX() { CALL_IF_EXISTS(void, static_cast<SerialT*>(this), flushTX); }
|
||||
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); }
|
||||
|
||||
// We have 2 implementation of the same method in both base class, let's say which one we want
|
||||
using SerialT::available;
|
||||
@@ -77,11 +98,10 @@ 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() { return (int)out.available(); }
|
||||
int read() { 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) {}
|
||||
};
|
||||
|
Reference in New Issue
Block a user