My Marlin configs for Fabrikator Mini and CTC i3 Pro B
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

serial_hook.h 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. /**
  2. * Marlin 3D Printer Firmware
  3. * Copyright (c) 2020 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
  4. *
  5. * Based on Sprinter and grbl.
  6. * Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm
  7. *
  8. * This program is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation, either version 3 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  20. *
  21. */
  22. #pragma once
  23. #include "serial_base.h"
  24. // A mask containing a bitmap of the serial port to act upon
  25. // This is written to ensure a serial index is never used as a serial mask
  26. class SerialMask {
  27. uint8_t mask;
  28. // This constructor is private to ensure you can't convert an index to a mask
  29. // The compiler will stop here if you are mixing index and mask in your code.
  30. // If you need to, you'll have to use the explicit static "from" method here
  31. SerialMask(const serial_index_t);
  32. public:
  33. inline constexpr bool enabled(const SerialMask PortMask) const { return mask & PortMask.mask; }
  34. inline constexpr SerialMask combine(const SerialMask other) const { return SerialMask(mask | other.mask); }
  35. inline constexpr SerialMask operator<< (const int offset) const { return SerialMask(mask << offset); }
  36. static SerialMask from(const serial_index_t index) {
  37. if (index.valid()) return SerialMask(_BV(index.index));
  38. return SerialMask(0); // A invalid index mean no output
  39. }
  40. constexpr SerialMask(const uint8_t mask) : mask(mask) {}
  41. constexpr SerialMask(const SerialMask &rs) : mask(rs.mask) {} // Can't use = default here since not all frameworks support this
  42. SerialMask& operator=(const SerialMask &rs) { mask = rs.mask; return *this; }
  43. static constexpr uint8_t All = 0xFF;
  44. };
  45. // 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
  46. template <class SerialT>
  47. struct BaseSerial : public SerialBase< BaseSerial<SerialT> >, public SerialT {
  48. typedef SerialBase< BaseSerial<SerialT> > BaseClassT;
  49. // It's required to implement a write method here to help compiler disambiguate what method to call
  50. using SerialT::write;
  51. using SerialT::flush;
  52. void msgDone() {}
  53. // We don't care about indices here, since if one can call us, it's the right index anyway
  54. int available(serial_index_t) { return (int)SerialT::available(); }
  55. int read(serial_index_t) { return (int)SerialT::read(); }
  56. bool connected() { return CALL_IF_EXISTS(bool, static_cast<SerialT*>(this), connected);; }
  57. void flushTX() { CALL_IF_EXISTS(void, static_cast<SerialT*>(this), flushTX); }
  58. SerialFeature features(serial_index_t index) const { return CALL_IF_EXISTS(SerialFeature, static_cast<const SerialT*>(this), features, index); }
  59. // Two implementations of the same method exist in both base classes so indicate the right one
  60. using SerialT::available;
  61. using SerialT::read;
  62. using SerialT::begin;
  63. using SerialT::end;
  64. using BaseClassT::print;
  65. using BaseClassT::println;
  66. BaseSerial(const bool e) : BaseClassT(e) {}
  67. // Forward constructor
  68. template <typename... Args>
  69. BaseSerial(const bool e, Args... args) : BaseClassT(e), SerialT(args...) {}
  70. };
  71. // A serial with a condition checked at runtime for its output
  72. // A bit less efficient than static dispatching but since it's only used for ethernet's serial output right now, it's ok.
  73. template <class SerialT>
  74. struct ConditionalSerial : public SerialBase< ConditionalSerial<SerialT> > {
  75. typedef SerialBase< ConditionalSerial<SerialT> > BaseClassT;
  76. bool & condition;
  77. SerialT & out;
  78. NO_INLINE size_t write(uint8_t c) { if (condition) return out.write(c); return 0; }
  79. void flush() { if (condition) out.flush(); }
  80. void begin(long br) { out.begin(br); }
  81. void end() { out.end(); }
  82. void msgDone() {}
  83. bool connected() { return CALL_IF_EXISTS(bool, &out, connected); }
  84. void flushTX() { CALL_IF_EXISTS(void, &out, flushTX); }
  85. int available(serial_index_t) { return (int)out.available(); }
  86. int read(serial_index_t) { return (int)out.read(); }
  87. int available() { return (int)out.available(); }
  88. int read() { return (int)out.read(); }
  89. SerialFeature features(serial_index_t index) const { return CALL_IF_EXISTS(SerialFeature, &out, features, index); }
  90. ConditionalSerial(bool & conditionVariable, SerialT & out, const bool e) : BaseClassT(e), condition(conditionVariable), out(out) {}
  91. };
  92. // A simple forward class that taking a reference to an existing serial instance (likely created in their respective framework)
  93. template <class SerialT>
  94. struct ForwardSerial : public SerialBase< ForwardSerial<SerialT> > {
  95. typedef SerialBase< ForwardSerial<SerialT> > BaseClassT;
  96. SerialT & out;
  97. NO_INLINE size_t write(uint8_t c) { return out.write(c); }
  98. void flush() { out.flush(); }
  99. void begin(long br) { out.begin(br); }
  100. void end() { out.end(); }
  101. void msgDone() {}
  102. // Existing instances implement Arduino's operator bool, so use that if it's available
  103. bool connected() { return Private::HasMember_connected<SerialT>::value ? CALL_IF_EXISTS(bool, &out, connected) : (bool)out; }
  104. void flushTX() { CALL_IF_EXISTS(void, &out, flushTX); }
  105. int available(serial_index_t) { return (int)out.available(); }
  106. int read(serial_index_t) { return (int)out.read(); }
  107. int available() { return (int)out.available(); }
  108. int read() { return (int)out.read(); }
  109. SerialFeature features(serial_index_t index) const { return CALL_IF_EXISTS(SerialFeature, &out, features, index); }
  110. ForwardSerial(const bool e, SerialT & out) : BaseClassT(e), out(out) {}
  111. };
  112. // A class that can be hooked and unhooked at runtime, useful to capture the output of the serial interface
  113. template <class SerialT>
  114. struct RuntimeSerial : public SerialBase< RuntimeSerial<SerialT> >, public SerialT {
  115. typedef SerialBase< RuntimeSerial<SerialT> > BaseClassT;
  116. typedef void (*WriteHook)(void * userPointer, uint8_t c);
  117. typedef void (*EndOfMessageHook)(void * userPointer);
  118. WriteHook writeHook;
  119. EndOfMessageHook eofHook;
  120. void * userPointer;
  121. NO_INLINE size_t write(uint8_t c) {
  122. if (writeHook) writeHook(userPointer, c);
  123. return SerialT::write(c);
  124. }
  125. NO_INLINE void msgDone() {
  126. if (eofHook) eofHook(userPointer);
  127. }
  128. int available(serial_index_t) { return (int)SerialT::available(); }
  129. int read(serial_index_t) { return (int)SerialT::read(); }
  130. using SerialT::available;
  131. using SerialT::read;
  132. using SerialT::flush;
  133. using SerialT::begin;
  134. using SerialT::end;
  135. using BaseClassT::print;
  136. using BaseClassT::println;
  137. // Underlying implementation might use Arduino's bool operator
  138. bool connected() {
  139. return Private::HasMember_connected<SerialT>::value
  140. ? CALL_IF_EXISTS(bool, static_cast<SerialT*>(this), connected)
  141. : static_cast<SerialT*>(this)->operator bool();
  142. }
  143. void flushTX() { CALL_IF_EXISTS(void, static_cast<SerialT*>(this), flushTX); }
  144. // Append Hookable for this class
  145. SerialFeature features(serial_index_t index) const { return SerialFeature::Hookable | CALL_IF_EXISTS(SerialFeature, static_cast<const SerialT*>(this), features, index); }
  146. void setHook(WriteHook writeHook = 0, EndOfMessageHook eofHook = 0, void * userPointer = 0) {
  147. // Order is important here as serial code can be called inside interrupts
  148. // When setting a hook, the user pointer must be set first so if writeHook is called as soon as it's set, it'll be valid
  149. if (userPointer) this->userPointer = userPointer;
  150. this->writeHook = writeHook;
  151. this->eofHook = eofHook;
  152. // Order is important here because of asynchronous access here
  153. // When unsetting a hook, the user pointer must be unset last so that any pending writeHook is still using the old pointer
  154. if (!userPointer) this->userPointer = 0;
  155. }
  156. RuntimeSerial(const bool e) : BaseClassT(e), writeHook(0), eofHook(0), userPointer(0) {}
  157. // Forward constructor
  158. template <typename... Args>
  159. RuntimeSerial(const bool e, Args... args) : BaseClassT(e), SerialT(args...), writeHook(0), eofHook(0), userPointer(0) {}
  160. };
  161. #define _S_CLASS(N) class Serial##N##T,
  162. #define _S_NAME(N) Serial##N##T,
  163. template < REPEAT(NUM_SERIAL, _S_CLASS) const uint8_t offset=0, const uint8_t step=1 >
  164. struct MultiSerial : public SerialBase< MultiSerial< REPEAT(NUM_SERIAL, _S_NAME) offset, step > > {
  165. typedef SerialBase< MultiSerial< REPEAT(NUM_SERIAL, _S_NAME) offset, step > > BaseClassT;
  166. #undef _S_CLASS
  167. #undef _S_NAME
  168. SerialMask portMask;
  169. #define _S_DECLARE(N) Serial##N##T & serial##N;
  170. REPEAT(NUM_SERIAL, _S_DECLARE);
  171. #undef _S_DECLARE
  172. static constexpr uint8_t Usage = _BV(step) - 1; // A bit mask containing 'step' bits
  173. #define _OUT_PORT(N) (Usage << (offset + (step * N))),
  174. static constexpr uint8_t output[] = { REPEAT(NUM_SERIAL, _OUT_PORT) };
  175. #undef _OUT_PORT
  176. #define _OUT_MASK(N) | output[N]
  177. static constexpr uint8_t ALL = 0 REPEAT(NUM_SERIAL, _OUT_MASK);
  178. #undef _OUT_MASK
  179. NO_INLINE void write(uint8_t c) {
  180. #define _S_WRITE(N) if (portMask.enabled(output[N])) serial##N.write(c);
  181. REPEAT(NUM_SERIAL, _S_WRITE);
  182. #undef _S_WRITE
  183. }
  184. NO_INLINE void msgDone() {
  185. #define _S_DONE(N) if (portMask.enabled(output[N])) serial##N.msgDone();
  186. REPEAT(NUM_SERIAL, _S_DONE);
  187. #undef _S_DONE
  188. }
  189. int available(serial_index_t index) {
  190. uint8_t pos = offset;
  191. #define _S_AVAILABLE(N) if (index.within(pos, pos + step - 1)) return serial##N.available(index); else pos += step;
  192. REPEAT(NUM_SERIAL, _S_AVAILABLE);
  193. #undef _S_AVAILABLE
  194. return false;
  195. }
  196. int read(serial_index_t index) {
  197. uint8_t pos = offset;
  198. #define _S_READ(N) if (index.within(pos, pos + step - 1)) return serial##N.read(index); else pos += step;
  199. REPEAT(NUM_SERIAL, _S_READ);
  200. #undef _S_READ
  201. return -1;
  202. }
  203. void begin(const long br) {
  204. #define _S_BEGIN(N) if (portMask.enabled(output[N])) serial##N.begin(br);
  205. REPEAT(NUM_SERIAL, _S_BEGIN);
  206. #undef _S_BEGIN
  207. }
  208. void end() {
  209. #define _S_END(N) if (portMask.enabled(output[N])) serial##N.end();
  210. REPEAT(NUM_SERIAL, _S_END);
  211. #undef _S_END
  212. }
  213. bool connected() {
  214. bool ret = true;
  215. #define _S_CONNECTED(N) if (portMask.enabled(output[N]) && !CALL_IF_EXISTS(bool, &serial##N, connected)) ret = false;
  216. REPEAT(NUM_SERIAL, _S_CONNECTED);
  217. #undef _S_CONNECTED
  218. return ret;
  219. }
  220. using BaseClassT::available;
  221. using BaseClassT::read;
  222. // Redirect flush
  223. NO_INLINE void flush() {
  224. #define _S_FLUSH(N) if (portMask.enabled(output[N])) serial##N.flush();
  225. REPEAT(NUM_SERIAL, _S_FLUSH);
  226. #undef _S_FLUSH
  227. }
  228. NO_INLINE void flushTX() {
  229. #define _S_FLUSHTX(N) if (portMask.enabled(output[N])) CALL_IF_EXISTS(void, &serial0, flushTX);
  230. REPEAT(NUM_SERIAL, _S_FLUSHTX);
  231. #undef _S_FLUSHTX
  232. }
  233. // Forward feature queries
  234. SerialFeature features(serial_index_t index) const {
  235. uint8_t pos = offset;
  236. #define _S_FEATURES(N) if (index.within(pos, pos + step - 1)) return serial##N.features(index); else pos += step;
  237. REPEAT(NUM_SERIAL, _S_FEATURES);
  238. #undef _S_FEATURES
  239. return SerialFeature::None;
  240. }
  241. #define _S_REFS(N) Serial##N##T & serial##N,
  242. #define _S_INIT(N) ,serial##N (serial##N)
  243. MultiSerial(REPEAT(NUM_SERIAL, _S_REFS) const SerialMask mask = ALL, const bool e = false)
  244. : BaseClassT(e), portMask(mask) REPEAT(NUM_SERIAL, _S_INIT) {}
  245. };
  246. // Build the actual serial object depending on current configuration
  247. #define Serial1Class TERN(SERIAL_RUNTIME_HOOK, RuntimeSerial, BaseSerial)
  248. #define ForwardSerial1Class TERN(SERIAL_RUNTIME_HOOK, RuntimeSerial, ForwardSerial)
  249. #if HAS_MULTI_SERIAL
  250. #define Serial2Class ConditionalSerial
  251. #if NUM_SERIAL >= 3
  252. #define Serial3Class ConditionalSerial
  253. #endif
  254. #endif