Browse Source

Distinguish serial index from mask (#21287)

X-Ryl669 3 years ago
parent
commit
55c31fbe9a
No account linked to committer's email address

+ 15
- 16
Marlin/src/core/serial.h View File

62
 //
62
 //
63
 // Serial redirection
63
 // Serial redirection
64
 //
64
 //
65
-#define SERIAL_ALL 0xFF
66
 #if HAS_MULTI_SERIAL
65
 #if HAS_MULTI_SERIAL
67
-  #define _PORT_REDIRECT(n,p)   REMEMBER(n,multiSerial.portMask,p)
68
-  #define _PORT_RESTORE(n,p)    RESTORE(n)
69
-  #define SERIAL_ASSERT(P)      if(multiSerial.portMask!=(P)){ debugger(); }
66
+  #define _PORT_REDIRECT(n,p) REMEMBER(n,multiSerial.portMask,p)
67
+  #define _PORT_RESTORE(n,p)  RESTORE(n)
68
+  #define SERIAL_ASSERT(P)    if(multiSerial.portMask!=(P)){ debugger(); }
70
   #ifdef SERIAL_CATCHALL
69
   #ifdef SERIAL_CATCHALL
71
     typedef MultiSerial<decltype(MYSERIAL), decltype(SERIAL_CATCHALL), 0> SerialOutputT;
70
     typedef MultiSerial<decltype(MYSERIAL), decltype(SERIAL_CATCHALL), 0> SerialOutputT;
72
   #else
71
   #else
73
-    typedef MultiSerial<decltype(MYSERIAL0), TERN(HAS_ETHERNET, ConditionalSerial<decltype(MYSERIAL1)>, decltype(MYSERIAL1)), 0>      SerialOutputT;
72
+    typedef MultiSerial<decltype(MYSERIAL0), TERN(HAS_ETHERNET, ConditionalSerial<decltype(MYSERIAL1)>, decltype(MYSERIAL1)), 0> SerialOutputT;
74
   #endif
73
   #endif
75
-  extern SerialOutputT          multiSerial;
76
-  #define _SERIAL_IMPL          multiSerial
74
+  extern SerialOutputT        multiSerial;
75
+  #define _SERIAL_IMPL        multiSerial
77
 #else
76
 #else
78
-  #define _PORT_REDIRECT(n,p)   NOOP
79
-  #define _PORT_RESTORE(n)      NOOP
80
-  #define SERIAL_ASSERT(P)      NOOP
81
-  #define _SERIAL_IMPL          MYSERIAL0
77
+  #define _PORT_REDIRECT(n,p) NOOP
78
+  #define _PORT_RESTORE(n)    NOOP
79
+  #define SERIAL_ASSERT(P)    NOOP
80
+  #define _SERIAL_IMPL        MYSERIAL0
82
 #endif
81
 #endif
83
 
82
 
84
 #if ENABLED(MEATPACK)
83
 #if ENABLED(MEATPACK)
85
   extern MeatpackSerial<decltype(_SERIAL_IMPL)> mpSerial;
84
   extern MeatpackSerial<decltype(_SERIAL_IMPL)> mpSerial;
86
-  #define SERIAL_IMPL          mpSerial
85
+  #define SERIAL_IMPL mpSerial
87
 #else
86
 #else
88
-  #define SERIAL_IMPL          _SERIAL_IMPL
87
+  #define SERIAL_IMPL _SERIAL_IMPL
89
 #endif
88
 #endif
90
 
89
 
91
 #define SERIAL_OUT(WHAT, V...)  (void)SERIAL_IMPL.WHAT(V)
90
 #define SERIAL_OUT(WHAT, V...)  (void)SERIAL_IMPL.WHAT(V)
92
 
91
 
93
-#define PORT_REDIRECT(p)        _PORT_REDIRECT(1,p)
94
-#define PORT_RESTORE()          _PORT_RESTORE(1)
95
-#define SERIAL_PORTMASK(P)      _BV(P)
92
+#define PORT_REDIRECT(p)   _PORT_REDIRECT(1,p)
93
+#define PORT_RESTORE()     _PORT_RESTORE(1)
94
+#define SERIAL_PORTMASK(P) SerialMask::from(P)
96
 
95
 
97
 //
96
 //
98
 // SERIAL_CHAR - Print one or more individual chars
97
 // SERIAL_CHAR - Print one or more individual chars

+ 20
- 3
Marlin/src/core/serial_base.h View File

22
 #pragma once
22
 #pragma once
23
 
23
 
24
 #include "../inc/MarlinConfigPre.h"
24
 #include "../inc/MarlinConfigPre.h"
25
-#include "macros.h"
26
 
25
 
27
 #if ENABLED(EMERGENCY_PARSER)
26
 #if ENABLED(EMERGENCY_PARSER)
28
   #include "../feature/e_parser.h"
27
   #include "../feature/e_parser.h"
29
 #endif
28
 #endif
30
 
29
 
30
+// Used in multiple places
31
+// You can build it but not manipulate it.
32
+// There are only few places where it's required to access the underlying member: GCodeQueue, SerialMask and MultiSerial
33
+struct serial_index_t {
34
+  // A signed index, where -1 is a special case meaning no action (neither output or input)
35
+  int8_t  index;
36
+
37
+  // Check if the index is within the range [a ... b]
38
+  constexpr inline bool within(const int8_t a, const int8_t b) const { return WITHIN(index, a, b); }
39
+  constexpr inline bool valid() const { return WITHIN(index, 0, 7); } // At most, 8 bits
40
+
41
+  // Construction is either from an index
42
+  constexpr serial_index_t(const int8_t index) : index(index) {}
43
+
44
+  // Default to "no index"
45
+  constexpr serial_index_t() : index(-1) {}
46
+};
47
+
31
 // flushTX is not implemented in all HAL, so use SFINAE to call the method where it is.
48
 // flushTX is not implemented in all HAL, so use SFINAE to call the method where it is.
32
 CALL_IF_EXISTS_IMPL(void, flushTX);
49
 CALL_IF_EXISTS_IMPL(void, flushTX);
33
 CALL_IF_EXISTS_IMPL(bool, connected, true);
50
 CALL_IF_EXISTS_IMPL(bool, connected, true);
79
   void end()                        { static_cast<Child*>(this)->end(); }
96
   void end()                        { static_cast<Child*>(this)->end(); }
80
   /** Check for available data from the port
97
   /** Check for available data from the port
81
       @param index  The port index, usually 0 */
98
       @param index  The port index, usually 0 */
82
-  int available(uint8_t index = 0)  { return static_cast<Child*>(this)->available(index); }
99
+  int available(serial_index_t index = 0)  { return static_cast<Child*>(this)->available(index); }
83
   /** Read a value from the port
100
   /** Read a value from the port
84
       @param index  The port index, usually 0 */
101
       @param index  The port index, usually 0 */
85
-  int  read(uint8_t index = 0)      { return static_cast<Child*>(this)->read(index); }
102
+  int  read(serial_index_t index = 0)      { return static_cast<Child*>(this)->read(index); }
86
   // Check if the serial port is connected (usually bypassed)
103
   // Check if the serial port is connected (usually bypassed)
87
   bool connected()                  { return static_cast<Child*>(this)->connected(); }
104
   bool connected()                  { return static_cast<Child*>(this)->connected(); }
88
   // Redirect flush
105
   // Redirect flush

+ 62
- 44
Marlin/src/core/serial_hook.h View File

21
  */
21
  */
22
 #pragma once
22
 #pragma once
23
 
23
 
24
-#include "macros.h"
25
 #include "serial_base.h"
24
 #include "serial_base.h"
26
 
25
 
27
-// Used in multiple places
28
-typedef int8_t serial_index_t;
26
+// A mask containing a bitmap of the serial port to act upon
27
+// This is written to ensure a serial index is never used as a serial mask
28
+class SerialMask {
29
+  uint8_t mask;
30
+
31
+  // This constructor is private to ensure you can't convert an index to a mask
32
+  // The compiler will stop here if you are mixing index and mask in your code.
33
+  // If you need to, you'll have to use the explicit static "from" method here
34
+  SerialMask(const serial_index_t);
35
+
36
+public:
37
+  inline constexpr bool enabled(const SerialMask PortMask) const    { return mask & PortMask.mask; }
38
+  inline constexpr SerialMask combine(const SerialMask other) const { return SerialMask(mask | other.mask); }
39
+  inline constexpr SerialMask operator<< (const int offset) const   { return SerialMask(mask << offset); }
40
+  static inline SerialMask from(const serial_index_t index) {
41
+    if (index.valid()) return SerialMask(_BV(index.index));
42
+    return SerialMask(0); // A invalid index mean no output
43
+  }
44
+
45
+  constexpr SerialMask(const uint8_t mask) : mask(mask) {}
46
+  constexpr SerialMask(const SerialMask & other) : mask(other.mask) {} // Can't use = default here since not all framework support this
47
+
48
+  static constexpr uint8_t All = 0xFF;
49
+};
29
 
50
 
30
 // 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
51
 // 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
31
 template <class SerialT>
52
 template <class SerialT>
39
   void msgDone() {}
60
   void msgDone() {}
40
 
61
 
41
   // We don't care about indices here, since if one can call us, it's the right index anyway
62
   // We don't care about indices here, since if one can call us, it's the right index anyway
42
-  int available(uint8_t)  { return (int)SerialT::available(); }
43
-  int read(uint8_t)       { return (int)SerialT::read(); }
44
-  bool connected()        { return CALL_IF_EXISTS(bool, static_cast<SerialT*>(this), connected);; }
45
-  void flushTX()          { CALL_IF_EXISTS(void, static_cast<SerialT*>(this), flushTX); }
63
+  int available(serial_index_t) { return (int)SerialT::available(); }
64
+  int read(serial_index_t)      { return (int)SerialT::read(); }
65
+  bool connected()              { return CALL_IF_EXISTS(bool, static_cast<SerialT*>(this), connected);; }
66
+  void flushTX()                { CALL_IF_EXISTS(void, static_cast<SerialT*>(this), flushTX); }
46
 
67
 
47
   // We have 2 implementation of the same method in both base class, let's say which one we want
68
   // We have 2 implementation of the same method in both base class, let's say which one we want
48
   using SerialT::available;
69
   using SerialT::available;
77
   bool connected()          { return CALL_IF_EXISTS(bool, &out, connected); }
98
   bool connected()          { return CALL_IF_EXISTS(bool, &out, connected); }
78
   void flushTX()            { CALL_IF_EXISTS(void, &out, flushTX); }
99
   void flushTX()            { CALL_IF_EXISTS(void, &out, flushTX); }
79
 
100
 
80
-  int available(uint8_t )   { return (int)out.available(); }
81
-  int read(uint8_t )        { return (int)out.read(); }
82
-  int available()           { return (int)out.available(); }
83
-  int read()                { return (int)out.read(); }
84
-
101
+  int available(serial_index_t )  { return (int)out.available(); }
102
+  int read(serial_index_t )       { return (int)out.read(); }
103
+  int available()                 { return (int)out.available(); }
104
+  int read()                      { return (int)out.read(); }
85
 
105
 
86
   ConditionalSerial(bool & conditionVariable, SerialT & out, const bool e) : BaseClassT(e), condition(conditionVariable), out(out) {}
106
   ConditionalSerial(bool & conditionVariable, SerialT & out, const bool e) : BaseClassT(e), condition(conditionVariable), out(out) {}
87
 };
107
 };
102
   bool connected()              { return Private::HasMember_connected<SerialT>::value ? CALL_IF_EXISTS(bool, &out, connected) : (bool)out; }
122
   bool connected()              { return Private::HasMember_connected<SerialT>::value ? CALL_IF_EXISTS(bool, &out, connected) : (bool)out; }
103
   void flushTX()                { CALL_IF_EXISTS(void, &out, flushTX); }
123
   void flushTX()                { CALL_IF_EXISTS(void, &out, flushTX); }
104
 
124
 
105
-  int available(uint8_t)        { return (int)out.available(); }
106
-  int read(uint8_t)             { return (int)out.read(); }
125
+  int available(serial_index_t) { return (int)out.available(); }
126
+  int read(serial_index_t)      { return (int)out.read(); }
107
   int available()               { return (int)out.available(); }
127
   int available()               { return (int)out.available(); }
108
   int read()                    { return (int)out.read(); }
128
   int read()                    { return (int)out.read(); }
109
 
129
 
130
     if (eofHook) eofHook(userPointer);
150
     if (eofHook) eofHook(userPointer);
131
   }
151
   }
132
 
152
 
133
-  int available(uint8_t)  { return (int)SerialT::available(); }
134
-  int read(uint8_t)       { return (int)SerialT::read(); }
153
+  int available(serial_index_t)  { return (int)SerialT::available(); }
154
+  int read(serial_index_t)       { return (int)SerialT::read(); }
135
   using SerialT::available;
155
   using SerialT::available;
136
   using SerialT::read;
156
   using SerialT::read;
137
   using SerialT::flush;
157
   using SerialT::flush;
170
 struct MultiSerial : public SerialBase< MultiSerial<Serial0T, Serial1T, offset, step> > {
190
 struct MultiSerial : public SerialBase< MultiSerial<Serial0T, Serial1T, offset, step> > {
171
   typedef SerialBase< MultiSerial<Serial0T, Serial1T, offset, step> > BaseClassT;
191
   typedef SerialBase< MultiSerial<Serial0T, Serial1T, offset, step> > BaseClassT;
172
 
192
 
173
-  uint8_t    portMask;
193
+  SerialMask portMask;
174
   Serial0T & serial0;
194
   Serial0T & serial0;
175
   Serial1T & serial1;
195
   Serial1T & serial1;
176
 
196
 
177
-  enum Masks {
178
-    UsageMask         =  ((1 << step) - 1), // A bit mask containing as many bits as step
179
-    FirstOutputMask   =  (UsageMask << offset),
180
-    SecondOutputMask  =  (UsageMask << (offset + step)),
181
-    AllMask           = FirstOutputMask | SecondOutputMask,
182
-  };
197
+  static constexpr uint8_t Usage         =  ((1 << step) - 1); // A bit mask containing as many bits as step
198
+  static constexpr uint8_t FirstOutput   = (Usage << offset);
199
+  static constexpr uint8_t SecondOutput  = (Usage << (offset + step));
200
+  static constexpr uint8_t Both          = FirstOutput | SecondOutput;
183
 
201
 
184
   NO_INLINE size_t write(uint8_t c) {
202
   NO_INLINE size_t write(uint8_t c) {
185
     size_t ret = 0;
203
     size_t ret = 0;
186
-    if (portMask & FirstOutputMask)   ret = serial0.write(c);
187
-    if (portMask & SecondOutputMask)  ret = serial1.write(c) | ret;
204
+    if (portMask.enabled(FirstOutput))   ret = serial0.write(c);
205
+    if (portMask.enabled(SecondOutput))  ret = serial1.write(c) | ret;
188
     return ret;
206
     return ret;
189
   }
207
   }
190
   NO_INLINE void msgDone() {
208
   NO_INLINE void msgDone() {
191
-    if (portMask & FirstOutputMask)   serial0.msgDone();
192
-    if (portMask & SecondOutputMask)  serial1.msgDone();
209
+    if (portMask.enabled(FirstOutput))   serial0.msgDone();
210
+    if (portMask.enabled(SecondOutput))  serial1.msgDone();
193
   }
211
   }
194
-  int available(uint8_t index) {
195
-    if (index >= 0 + offset && index < step + offset)
212
+  int available(serial_index_t index) {
213
+    if (index.within(0 + offset, step + offset - 1))
196
       return serial0.available(index);
214
       return serial0.available(index);
197
-    else if (index >= step + offset && index < 2 * step + offset)
215
+    else if (index.within(step + offset, 2 * step + offset - 1))
198
       return serial1.available(index);
216
       return serial1.available(index);
199
     return false;
217
     return false;
200
   }
218
   }
201
-  int read(uint8_t index) {
202
-    if (index >= 0 + offset && index < step + offset)
219
+  int read(serial_index_t index) {
220
+    if (index.within(0 + offset, step + offset - 1))
203
       return serial0.read(index);
221
       return serial0.read(index);
204
-    else if (index >= step + offset && index < 2 * step + offset)
222
+    else if (index.within(step + offset, 2 * step + offset - 1))
205
       return serial1.read(index);
223
       return serial1.read(index);
206
     return -1;
224
     return -1;
207
   }
225
   }
208
   void begin(const long br) {
226
   void begin(const long br) {
209
-    if (portMask & FirstOutputMask)   serial0.begin(br);
210
-    if (portMask & SecondOutputMask)  serial1.begin(br);
227
+    if (portMask.enabled(FirstOutput))   serial0.begin(br);
228
+    if (portMask.enabled(SecondOutput))  serial1.begin(br);
211
   }
229
   }
212
   void end() {
230
   void end() {
213
-    if (portMask & FirstOutputMask)   serial0.end();
214
-    if (portMask & SecondOutputMask)  serial1.end();
231
+    if (portMask.enabled(FirstOutput))   serial0.end();
232
+    if (portMask.enabled(SecondOutput))  serial1.end();
215
   }
233
   }
216
   bool connected() {
234
   bool connected() {
217
     bool ret = true;
235
     bool ret = true;
218
-    if (portMask & FirstOutputMask)   ret = CALL_IF_EXISTS(bool, &serial0, connected);
219
-    if (portMask & SecondOutputMask)  ret = ret && CALL_IF_EXISTS(bool, &serial1, connected);
236
+    if (portMask.enabled(FirstOutput))   ret = CALL_IF_EXISTS(bool, &serial0, connected);
237
+    if (portMask.enabled(SecondOutput))  ret = ret && CALL_IF_EXISTS(bool, &serial1, connected);
220
     return ret;
238
     return ret;
221
   }
239
   }
222
 
240
 
225
 
243
 
226
   // Redirect flush
244
   // Redirect flush
227
   NO_INLINE void flush()      {
245
   NO_INLINE void flush()      {
228
-    if (portMask & FirstOutputMask)   serial0.flush();
229
-    if (portMask & SecondOutputMask)  serial1.flush();
246
+    if (portMask.enabled(FirstOutput))   serial0.flush();
247
+    if (portMask.enabled(SecondOutput))  serial1.flush();
230
   }
248
   }
231
   NO_INLINE void flushTX()    {
249
   NO_INLINE void flushTX()    {
232
-    if (portMask & FirstOutputMask)   CALL_IF_EXISTS(void, &serial0, flushTX);
233
-    if (portMask & SecondOutputMask)  CALL_IF_EXISTS(void, &serial1, flushTX);
250
+    if (portMask.enabled(FirstOutput))   CALL_IF_EXISTS(void, &serial0, flushTX);
251
+    if (portMask.enabled(SecondOutput))  CALL_IF_EXISTS(void, &serial1, flushTX);
234
   }
252
   }
235
 
253
 
236
-  MultiSerial(Serial0T & serial0, Serial1T & serial1, int8_t mask = AllMask, const bool e = false) :
254
+  MultiSerial(Serial0T & serial0, Serial1T & serial1, const SerialMask mask = Both, const bool e = false) :
237
     BaseClassT(e),
255
     BaseClassT(e),
238
     portMask(mask), serial0(serial0), serial1(serial1) {}
256
     portMask(mask), serial0(serial0), serial1(serial1) {}
239
 };
257
 };

+ 5
- 5
Marlin/src/feature/host_actions.cpp View File

38
 #endif
38
 #endif
39
 
39
 
40
 void host_action(PGM_P const pstr, const bool eol) {
40
 void host_action(PGM_P const pstr, const bool eol) {
41
-  PORT_REDIRECT(SERIAL_ALL);
41
+  PORT_REDIRECT(SerialMask::All);
42
   SERIAL_ECHOPGM("//action:");
42
   SERIAL_ECHOPGM("//action:");
43
   SERIAL_ECHOPGM_P(pstr);
43
   SERIAL_ECHOPGM_P(pstr);
44
   if (eol) SERIAL_EOL();
44
   if (eol) SERIAL_EOL();
78
   PromptReason host_prompt_reason = PROMPT_NOT_DEFINED;
78
   PromptReason host_prompt_reason = PROMPT_NOT_DEFINED;
79
 
79
 
80
   void host_action_notify(const char * const message) {
80
   void host_action_notify(const char * const message) {
81
-    PORT_REDIRECT(SERIAL_ALL);
81
+    PORT_REDIRECT(SerialMask::All);
82
     host_action(PSTR("notification "), false);
82
     host_action(PSTR("notification "), false);
83
     SERIAL_ECHOLN(message);
83
     SERIAL_ECHOLN(message);
84
   }
84
   }
85
 
85
 
86
   void host_action_notify_P(PGM_P const message) {
86
   void host_action_notify_P(PGM_P const message) {
87
-    PORT_REDIRECT(SERIAL_ALL);
87
+    PORT_REDIRECT(SerialMask::All);
88
     host_action(PSTR("notification "), false);
88
     host_action(PSTR("notification "), false);
89
     SERIAL_ECHOLNPGM_P(message);
89
     SERIAL_ECHOLNPGM_P(message);
90
   }
90
   }
91
 
91
 
92
   void host_action_prompt(PGM_P const ptype, const bool eol=true) {
92
   void host_action_prompt(PGM_P const ptype, const bool eol=true) {
93
-    PORT_REDIRECT(SERIAL_ALL);
93
+    PORT_REDIRECT(SerialMask::All);
94
     host_action(PSTR("prompt_"), false);
94
     host_action(PSTR("prompt_"), false);
95
     SERIAL_ECHOPGM_P(ptype);
95
     SERIAL_ECHOPGM_P(ptype);
96
     if (eol) SERIAL_EOL();
96
     if (eol) SERIAL_EOL();
98
 
98
 
99
   void host_action_prompt_plus(PGM_P const ptype, PGM_P const pstr, const char extra_char='\0') {
99
   void host_action_prompt_plus(PGM_P const ptype, PGM_P const pstr, const char extra_char='\0') {
100
     host_action_prompt(ptype, false);
100
     host_action_prompt(ptype, false);
101
-    PORT_REDIRECT(SERIAL_ALL);
101
+    PORT_REDIRECT(SerialMask::All);
102
     SERIAL_CHAR(' ');
102
     SERIAL_CHAR(' ');
103
     SERIAL_ECHOPGM_P(pstr);
103
     SERIAL_ECHOPGM_P(pstr);
104
     if (extra_char != '\0') SERIAL_CHAR(extra_char);
104
     if (extra_char != '\0') SERIAL_CHAR(extra_char);

+ 12
- 12
Marlin/src/feature/meatpack.h View File

132
   uint8_t charCount;
132
   uint8_t charCount;
133
   uint8_t readIndex;
133
   uint8_t readIndex;
134
 
134
 
135
-  NO_INLINE size_t write(uint8_t c) { return out.write(c); }
136
-  void flush()                      { out.flush();  }
137
-  void begin(long br)               { out.begin(br); readIndex = 0; }
138
-  void end()                        { out.end(); }
135
+  NO_INLINE size_t write(uint8_t c)   { return out.write(c); }
136
+  void flush()                        { out.flush();  }
137
+  void begin(long br)                 { out.begin(br); readIndex = 0; }
138
+  void end()                          { out.end(); }
139
 
139
 
140
-  void msgDone()                    { out.msgDone(); }
140
+  void msgDone()                      { out.msgDone(); }
141
   // Existing instances implement Arduino's operator bool, so use that if it's available
141
   // Existing instances implement Arduino's operator bool, so use that if it's available
142
-  bool connected()                  { return Private::HasMember_connected<SerialT>::value ? CALL_IF_EXISTS(bool, &out, connected) : (bool)out; }
143
-  void flushTX()                    { CALL_IF_EXISTS(void, &out, flushTX); }
142
+  bool connected()                    { return Private::HasMember_connected<SerialT>::value ? CALL_IF_EXISTS(bool, &out, connected) : (bool)out; }
143
+  void flushTX()                      { CALL_IF_EXISTS(void, &out, flushTX); }
144
 
144
 
145
-  int available(uint8_t index) {
145
+  int available(serial_index_t index) {
146
     // There is a potential issue here with multiserial, since it'll return its decoded buffer whatever the serial index here.
146
     // There is a potential issue here with multiserial, since it'll return its decoded buffer whatever the serial index here.
147
     // So, instead of doing MeatpackSerial<MultiSerial<...>> we should do MultiSerial<MeatpackSerial<...>, MeatpackSerial<...>>
147
     // So, instead of doing MeatpackSerial<MultiSerial<...>> we should do MultiSerial<MeatpackSerial<...>, MeatpackSerial<...>>
148
     // TODO, let's fix this later on
148
     // TODO, let's fix this later on
160
     return charCount;
160
     return charCount;
161
   }
161
   }
162
 
162
 
163
-  int readImpl(const uint8_t index) {
163
+  int readImpl(const serial_index_t index) {
164
     // Not enough char to make progress?
164
     // Not enough char to make progress?
165
     if (charCount == 0 && available(index) == 0) return -1;
165
     if (charCount == 0 && available(index) == 0) return -1;
166
 
166
 
168
     return serialBuffer[readIndex++];
168
     return serialBuffer[readIndex++];
169
   }
169
   }
170
 
170
 
171
-  int read(uint8_t index) { return readImpl(index); }
172
-  int available()         { return available(0); }
173
-  int read()              { return readImpl(0); }
171
+  int read(serial_index_t index)  { return readImpl(index); }
172
+  int available()                 { return available(0); }
173
+  int read()                      { return readImpl(0); }
174
 
174
 
175
   MeatpackSerial(const bool e, SerialT & out) : BaseClassT(e), out(out) {}
175
   MeatpackSerial(const bool e, SerialT & out) : BaseClassT(e), out(out) {}
176
 };
176
 };

+ 1
- 1
Marlin/src/gcode/gcode.cpp View File

1067
     static millis_t next_busy_signal_ms = 0;
1067
     static millis_t next_busy_signal_ms = 0;
1068
     if (!autoreport_paused && host_keepalive_interval && busy_state != NOT_BUSY) {
1068
     if (!autoreport_paused && host_keepalive_interval && busy_state != NOT_BUSY) {
1069
       if (PENDING(ms, next_busy_signal_ms)) return;
1069
       if (PENDING(ms, next_busy_signal_ms)) return;
1070
-      PORT_REDIRECT(SERIAL_ALL);
1070
+      PORT_REDIRECT(SerialMask::All);
1071
       switch (busy_state) {
1071
       switch (busy_state) {
1072
         case IN_HANDLER:
1072
         case IN_HANDLER:
1073
         case IN_PROCESS:
1073
         case IN_PROCESS:

+ 1
- 1
Marlin/src/gcode/host/M118.cpp View File

52
     while (*p == ' ') ++p;
52
     while (*p == ' ') ++p;
53
   }
53
   }
54
 
54
 
55
-  PORT_REDIRECT(WITHIN(port, 0, NUM_SERIAL) ? (port ? SERIAL_PORTMASK(port - 1) : SERIAL_ALL) : multiSerial.portMask);
55
+  PORT_REDIRECT(WITHIN(port, 0, NUM_SERIAL) ? (port ? SERIAL_PORTMASK(port - 1) : SerialMask::All) : multiSerial.portMask);
56
 
56
 
57
   if (hasE) SERIAL_ECHO_START();
57
   if (hasE) SERIAL_ECHO_START();
58
   if (hasA) SERIAL_ECHOPGM("//");
58
   if (hasA) SERIAL_ECHOPGM("//");

+ 7
- 7
Marlin/src/gcode/queue.cpp View File

240
   CommandLine &command = commands[index_r];
240
   CommandLine &command = commands[index_r];
241
   #if HAS_MULTI_SERIAL
241
   #if HAS_MULTI_SERIAL
242
     const serial_index_t serial_ind = command.port;
242
     const serial_index_t serial_ind = command.port;
243
-    if (serial_ind < 0) return;
243
+    if (!serial_ind.valid()) return;              // Optimization here, skip processing if it's not going anywhere
244
     PORT_REDIRECT(SERIAL_PORTMASK(serial_ind));   // Reply to the serial port that sent the command
244
     PORT_REDIRECT(SERIAL_PORTMASK(serial_ind));   // Reply to the serial port that sent the command
245
   #endif
245
   #endif
246
   if (command.skip_ok) return;
246
   if (command.skip_ok) return;
264
  */
264
  */
265
 void GCodeQueue::flush_and_request_resend(const serial_index_t serial_ind) {
265
 void GCodeQueue::flush_and_request_resend(const serial_index_t serial_ind) {
266
   #if HAS_MULTI_SERIAL
266
   #if HAS_MULTI_SERIAL
267
-    if (serial_ind < 0) return;                   // Never mind. Command came from SD or Flash Drive
267
+    if (!serial_ind.valid()) return;              // Optimization here, skip if the command came from SD or Flash Drive
268
     PORT_REDIRECT(SERIAL_PORTMASK(serial_ind));   // Reply to the serial port that sent the command
268
     PORT_REDIRECT(SERIAL_PORTMASK(serial_ind));   // Reply to the serial port that sent the command
269
   #endif
269
   #endif
270
   SERIAL_FLUSH();
270
   SERIAL_FLUSH();
271
   SERIAL_ECHOPGM(STR_RESEND);
271
   SERIAL_ECHOPGM(STR_RESEND);
272
-  SERIAL_ECHOLN(serial_state[serial_ind].last_N + 1);
272
+  SERIAL_ECHOLN(serial_state[serial_ind.index].last_N + 1);
273
 }
273
 }
274
 
274
 
275
-inline bool serial_data_available(uint8_t index) {
275
+inline bool serial_data_available(serial_index_t index) {
276
   const int a = SERIAL_IMPL.available(index);
276
   const int a = SERIAL_IMPL.available(index);
277
   #if BOTH(RX_BUFFER_MONITOR, RX_BUFFER_SIZE)
277
   #if BOTH(RX_BUFFER_MONITOR, RX_BUFFER_SIZE)
278
     if (a > RX_BUFFER_SIZE - 2) {
278
     if (a > RX_BUFFER_SIZE - 2) {
290
       return true;
290
       return true;
291
 }
291
 }
292
 
292
 
293
-inline int read_serial(const uint8_t index) { return SERIAL_IMPL.read(index); }
293
+inline int read_serial(const serial_index_t index) { return SERIAL_IMPL.read(index); }
294
 
294
 
295
 void GCodeQueue::gcode_line_error(PGM_P const err, const serial_index_t serial_ind) {
295
 void GCodeQueue::gcode_line_error(PGM_P const err, const serial_index_t serial_ind) {
296
   PORT_REDIRECT(SERIAL_PORTMASK(serial_ind)); // Reply to the serial port that sent the command
296
   PORT_REDIRECT(SERIAL_PORTMASK(serial_ind)); // Reply to the serial port that sent the command
297
   SERIAL_ERROR_START();
297
   SERIAL_ERROR_START();
298
-  SERIAL_ECHOLNPAIR_P(err, serial_state[serial_ind].last_N);
298
+  SERIAL_ECHOLNPAIR_P(err, serial_state[serial_ind.index].last_N);
299
   while (read_serial(serial_ind) != -1) { /* nada */ } // Clear out the RX buffer. Why don't use flush here ?
299
   while (read_serial(serial_ind) != -1) { /* nada */ } // Clear out the RX buffer. Why don't use flush here ?
300
   flush_and_request_resend(serial_ind);
300
   flush_and_request_resend(serial_ind);
301
-  serial_state[serial_ind].count = 0;
301
+  serial_state[serial_ind.index].count = 0;
302
 }
302
 }
303
 
303
 
304
 FORCE_INLINE bool is_M29(const char * const cmd) {  // matches "M29" & "M29 ", but not "M290", etc
304
 FORCE_INLINE bool is_M29(const char * const cmd) {  // matches "M29" & "M29 ", but not "M290", etc

+ 3
- 3
Marlin/src/gcode/queue.h View File

79
 
79
 
80
     void commit_command(bool skip_ok
80
     void commit_command(bool skip_ok
81
       #if HAS_MULTI_SERIAL
81
       #if HAS_MULTI_SERIAL
82
-        , serial_index_t serial_ind=-1
82
+        , serial_index_t serial_ind = serial_index_t()
83
       #endif
83
       #endif
84
     );
84
     );
85
 
85
 
86
     bool enqueue(const char* cmd, bool skip_ok = true
86
     bool enqueue(const char* cmd, bool skip_ok = true
87
       #if HAS_MULTI_SERIAL
87
       #if HAS_MULTI_SERIAL
88
-        , serial_index_t serial_ind=-1
88
+        , serial_index_t serial_ind = serial_index_t()
89
       #endif
89
       #endif
90
     );
90
     );
91
 
91
 
197
   /**
197
   /**
198
    * (Re)Set the current line number for the last received command
198
    * (Re)Set the current line number for the last received command
199
    */
199
    */
200
-  static inline void set_current_line_number(long n) { serial_state[ring_buffer.command_port()].last_N = n; }
200
+  static inline void set_current_line_number(long n) { serial_state[ring_buffer.command_port().index].last_N = n; }
201
 
201
 
202
 private:
202
 private:
203
 
203
 

+ 1
- 1
Marlin/src/gcode/sd/M1001.cpp View File

82
 
82
 
83
   // Announce SD file completion
83
   // Announce SD file completion
84
   {
84
   {
85
-    PORT_REDIRECT(SERIAL_ALL);
85
+    PORT_REDIRECT(SerialMask::All);
86
     SERIAL_ECHOLNPGM(STR_FILE_PRINTED);
86
     SERIAL_ECHOLNPGM(STR_FILE_PRINTED);
87
   }
87
   }
88
 
88
 

+ 3
- 3
Marlin/src/lcd/extui/lib/dgus/DGUSScreenHandler.cpp View File

395
     default: return;
395
     default: return;
396
     #if HOTENDS >= 1
396
     #if HOTENDS >= 1
397
       case VP_T_E0_Set:
397
       case VP_T_E0_Set:
398
-        NOMORE(newvalue, HEATER_0_MAXTEMP);
398
+        NOMORE(newvalue, (uint16_t)HEATER_0_MAXTEMP);
399
         thermalManager.setTargetHotend(newvalue, 0);
399
         thermalManager.setTargetHotend(newvalue, 0);
400
         acceptedvalue = thermalManager.degTargetHotend(0);
400
         acceptedvalue = thermalManager.degTargetHotend(0);
401
         break;
401
         break;
402
     #endif
402
     #endif
403
     #if HOTENDS >= 2
403
     #if HOTENDS >= 2
404
       case VP_T_E1_Set:
404
       case VP_T_E1_Set:
405
-        NOMORE(newvalue, HEATER_1_MAXTEMP);
405
+        NOMORE(newvalue, (uint16_t)HEATER_1_MAXTEMP);
406
         thermalManager.setTargetHotend(newvalue, 1);
406
         thermalManager.setTargetHotend(newvalue, 1);
407
         acceptedvalue = thermalManager.degTargetHotend(1);
407
         acceptedvalue = thermalManager.degTargetHotend(1);
408
         break;
408
         break;
409
     #endif
409
     #endif
410
     #if HAS_HEATED_BED
410
     #if HAS_HEATED_BED
411
       case VP_T_Bed_Set:
411
       case VP_T_Bed_Set:
412
-        NOMORE(newvalue, BED_MAXTEMP);
412
+        NOMORE(newvalue, (uint16_t)BED_MAXTEMP);
413
         thermalManager.setTargetBed(newvalue);
413
         thermalManager.setTargetBed(newvalue);
414
         acceptedvalue = thermalManager.degTargetBed();
414
         acceptedvalue = thermalManager.degTargetBed();
415
         break;
415
         break;

+ 2
- 2
Marlin/src/libs/autoreport.h View File

28
   millis_t next_report_ms;
28
   millis_t next_report_ms;
29
   uint8_t report_interval;
29
   uint8_t report_interval;
30
   #if HAS_MULTI_SERIAL
30
   #if HAS_MULTI_SERIAL
31
-    serial_index_t report_port_mask;
32
-    AutoReporter() : report_port_mask(SERIAL_ALL) {}
31
+    SerialMask report_port_mask;
32
+    AutoReporter() : report_port_mask(SerialMask::All) {}
33
   #endif
33
   #endif
34
 
34
 
35
   inline void set_interval(uint8_t seconds, const uint8_t limit=60) {
35
   inline void set_interval(uint8_t seconds, const uint8_t limit=60) {

+ 1
- 1
Marlin/src/module/temperature.cpp View File

329
      */
329
      */
330
     void Temperature::report_fan_speed(const uint8_t target) {
330
     void Temperature::report_fan_speed(const uint8_t target) {
331
       if (target >= FAN_COUNT) return;
331
       if (target >= FAN_COUNT) return;
332
-      PORT_REDIRECT(SERIAL_ALL);
332
+      PORT_REDIRECT(SerialMask::All);
333
       SERIAL_ECHOLNPAIR("M106 P", target, " S", fan_speed[target]);
333
       SERIAL_ECHOLNPAIR("M106 P", target, " S", fan_speed[target]);
334
     }
334
     }
335
   #endif
335
   #endif

+ 2
- 2
Marlin/src/sd/cardreader.cpp View File

549
 
549
 
550
 void announceOpen(const uint8_t doing, const char * const path) {
550
 void announceOpen(const uint8_t doing, const char * const path) {
551
   if (doing) {
551
   if (doing) {
552
-    PORT_REDIRECT(SERIAL_ALL);
552
+    PORT_REDIRECT(SerialMask::All);
553
     SERIAL_ECHO_START();
553
     SERIAL_ECHO_START();
554
     SERIAL_ECHOPGM("Now ");
554
     SERIAL_ECHOPGM("Now ");
555
     SERIAL_ECHOPGM_P(doing == 1 ? PSTR("doing") : PSTR("fresh"));
555
     SERIAL_ECHOPGM_P(doing == 1 ? PSTR("doing") : PSTR("fresh"));
615
     sdpos = 0;
615
     sdpos = 0;
616
 
616
 
617
     { // Don't remove this block, as the PORT_REDIRECT is a RAII
617
     { // Don't remove this block, as the PORT_REDIRECT is a RAII
618
-      PORT_REDIRECT(SERIAL_ALL);
618
+      PORT_REDIRECT(SerialMask::All);
619
       SERIAL_ECHOLNPAIR(STR_SD_FILE_OPENED, fname, STR_SD_SIZE, filesize);
619
       SERIAL_ECHOLNPAIR(STR_SD_FILE_OPENED, fname, STR_SD_SIZE, filesize);
620
       SERIAL_ECHOLNPGM(STR_SD_FILE_SELECTED);
620
       SERIAL_ECHOLNPGM(STR_SD_FILE_SELECTED);
621
     }
621
     }

Loading…
Cancel
Save