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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  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 inline 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 & other) : mask(other.mask) {} // Can't use = default here since not all framework support this
  42. static constexpr uint8_t All = 0xFF;
  43. };
  44. // 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
  45. template <class SerialT>
  46. struct BaseSerial : public SerialBase< BaseSerial<SerialT> >, public SerialT {
  47. typedef SerialBase< BaseSerial<SerialT> > BaseClassT;
  48. // It's required to implement a write method here to help compiler disambiguate what method to call
  49. using SerialT::write;
  50. using SerialT::flush;
  51. void msgDone() {}
  52. // We don't care about indices here, since if one can call us, it's the right index anyway
  53. int available(serial_index_t) { return (int)SerialT::available(); }
  54. int read(serial_index_t) { return (int)SerialT::read(); }
  55. bool connected() { return CALL_IF_EXISTS(bool, static_cast<SerialT*>(this), connected);; }
  56. void flushTX() { CALL_IF_EXISTS(void, static_cast<SerialT*>(this), flushTX); }
  57. SerialFeature features(serial_index_t index) const { return CALL_IF_EXISTS(SerialFeature, static_cast<const SerialT*>(this), features, index); }
  58. // Two implementations of the same method exist in both base classes so indicate the right one
  59. using SerialT::available;
  60. using SerialT::read;
  61. using SerialT::begin;
  62. using SerialT::end;
  63. using BaseClassT::print;
  64. using BaseClassT::println;
  65. BaseSerial(const bool e) : BaseClassT(e) {}
  66. // Forward constructor
  67. template <typename... Args>
  68. BaseSerial(const bool e, Args... args) : BaseClassT(e), SerialT(args...) {}
  69. };
  70. // A serial with a condition checked at runtime for its output
  71. // A bit less efficient than static dispatching but since it's only used for ethernet's serial output right now, it's ok.
  72. template <class SerialT>
  73. struct ConditionalSerial : public SerialBase< ConditionalSerial<SerialT> > {
  74. typedef SerialBase< ConditionalSerial<SerialT> > BaseClassT;
  75. bool & condition;
  76. SerialT & out;
  77. NO_INLINE size_t write(uint8_t c) { if (condition) return out.write(c); return 0; }
  78. void flush() { if (condition) out.flush(); }
  79. void begin(long br) { out.begin(br); }
  80. void end() { out.end(); }
  81. void msgDone() {}
  82. bool connected() { return CALL_IF_EXISTS(bool, &out, connected); }
  83. void flushTX() { CALL_IF_EXISTS(void, &out, flushTX); }
  84. int available(serial_index_t) { return (int)out.available(); }
  85. int read(serial_index_t) { return (int)out.read(); }
  86. int available() { return (int)out.available(); }
  87. int read() { return (int)out.read(); }
  88. SerialFeature features(serial_index_t index) const { return CALL_IF_EXISTS(SerialFeature, &out, features, index); }
  89. ConditionalSerial(bool & conditionVariable, SerialT & out, const bool e) : BaseClassT(e), condition(conditionVariable), out(out) {}
  90. };
  91. // A simple foward class that taking a reference to an existing serial instance (likely created in their respective framework)
  92. template <class SerialT>
  93. struct ForwardSerial : public SerialBase< ForwardSerial<SerialT> > {
  94. typedef SerialBase< ForwardSerial<SerialT> > BaseClassT;
  95. SerialT & out;
  96. NO_INLINE size_t write(uint8_t c) { return out.write(c); }
  97. void flush() { out.flush(); }
  98. void begin(long br) { out.begin(br); }
  99. void end() { out.end(); }
  100. void msgDone() {}
  101. // Existing instances implement Arduino's operator bool, so use that if it's available
  102. 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); }
  104. int available(serial_index_t) { return (int)out.available(); }
  105. int read(serial_index_t) { return (int)out.read(); }
  106. int available() { return (int)out.available(); }
  107. int read() { return (int)out.read(); }
  108. SerialFeature features(serial_index_t index) const { return CALL_IF_EXISTS(SerialFeature, &out, features, index); }
  109. ForwardSerial(const bool e, SerialT & out) : BaseClassT(e), out(out) {}
  110. };
  111. // A class that can be hooked and unhooked at runtime, useful to capture the output of the serial interface
  112. template <class SerialT>
  113. struct RuntimeSerial : public SerialBase< RuntimeSerial<SerialT> >, public SerialT {
  114. typedef SerialBase< RuntimeSerial<SerialT> > BaseClassT;
  115. typedef void (*WriteHook)(void * userPointer, uint8_t c);
  116. typedef void (*EndOfMessageHook)(void * userPointer);
  117. WriteHook writeHook;
  118. EndOfMessageHook eofHook;
  119. void * userPointer;
  120. NO_INLINE size_t write(uint8_t c) {
  121. if (writeHook) writeHook(userPointer, c);
  122. return SerialT::write(c);
  123. }
  124. NO_INLINE void msgDone() {
  125. if (eofHook) eofHook(userPointer);
  126. }
  127. int available(serial_index_t) { return (int)SerialT::available(); }
  128. int read(serial_index_t) { return (int)SerialT::read(); }
  129. using SerialT::available;
  130. using SerialT::read;
  131. using SerialT::flush;
  132. using SerialT::begin;
  133. using SerialT::end;
  134. using BaseClassT::print;
  135. using BaseClassT::println;
  136. // Underlying implementation might use Arduino's bool operator
  137. bool connected() {
  138. return Private::HasMember_connected<SerialT>::value
  139. ? CALL_IF_EXISTS(bool, static_cast<SerialT*>(this), connected)
  140. : static_cast<SerialT*>(this)->operator bool();
  141. }
  142. void flushTX() { CALL_IF_EXISTS(void, static_cast<SerialT*>(this), flushTX); }
  143. // Append Hookable for this class
  144. SerialFeature features(serial_index_t index) const { return SerialFeature::Hookable | CALL_IF_EXISTS(SerialFeature, static_cast<const SerialT*>(this), features, index); }
  145. void setHook(WriteHook writeHook = 0, EndOfMessageHook eofHook = 0, void * userPointer = 0) {
  146. // Order is important here as serial code can be called inside interrupts
  147. // 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
  148. if (userPointer) this->userPointer = userPointer;
  149. this->writeHook = writeHook;
  150. this->eofHook = eofHook;
  151. // Order is important here because of asynchronous access here
  152. // When unsetting a hook, the user pointer must be unset last so that any pending writeHook is still using the old pointer
  153. if (!userPointer) this->userPointer = 0;
  154. }
  155. RuntimeSerial(const bool e) : BaseClassT(e), writeHook(0), eofHook(0), userPointer(0) {}
  156. // Forward constructor
  157. template <typename... Args>
  158. RuntimeSerial(const bool e, Args... args) : BaseClassT(e), SerialT(args...), writeHook(0), eofHook(0), userPointer(0) {}
  159. };
  160. // A class that duplicates its output conditionally to 2 serial interfaces
  161. template <class Serial0T, class Serial1T, const uint8_t offset = 0, const uint8_t step = 1>
  162. struct MultiSerial : public SerialBase< MultiSerial<Serial0T, Serial1T, offset, step> > {
  163. typedef SerialBase< MultiSerial<Serial0T, Serial1T, offset, step> > BaseClassT;
  164. SerialMask portMask;
  165. Serial0T & serial0;
  166. Serial1T & serial1;
  167. static constexpr uint8_t Usage = ((1 << step) - 1); // A bit mask containing as many bits as step
  168. static constexpr uint8_t FirstOutput = (Usage << offset);
  169. static constexpr uint8_t SecondOutput = (Usage << (offset + step));
  170. static constexpr uint8_t Both = FirstOutput | SecondOutput;
  171. NO_INLINE void write(uint8_t c) {
  172. if (portMask.enabled(FirstOutput)) serial0.write(c);
  173. if (portMask.enabled(SecondOutput)) serial1.write(c);
  174. }
  175. NO_INLINE void msgDone() {
  176. if (portMask.enabled(FirstOutput)) serial0.msgDone();
  177. if (portMask.enabled(SecondOutput)) serial1.msgDone();
  178. }
  179. int available(serial_index_t index) {
  180. if (index.within(0 + offset, step + offset - 1))
  181. return serial0.available(index);
  182. else if (index.within(step + offset, 2 * step + offset - 1))
  183. return serial1.available(index);
  184. return false;
  185. }
  186. int read(serial_index_t index) {
  187. if (index.within(0 + offset, step + offset - 1))
  188. return serial0.read(index);
  189. else if (index.within(step + offset, 2 * step + offset - 1))
  190. return serial1.read(index);
  191. return -1;
  192. }
  193. void begin(const long br) {
  194. if (portMask.enabled(FirstOutput)) serial0.begin(br);
  195. if (portMask.enabled(SecondOutput)) serial1.begin(br);
  196. }
  197. void end() {
  198. if (portMask.enabled(FirstOutput)) serial0.end();
  199. if (portMask.enabled(SecondOutput)) serial1.end();
  200. }
  201. bool connected() {
  202. bool ret = true;
  203. if (portMask.enabled(FirstOutput)) ret = CALL_IF_EXISTS(bool, &serial0, connected);
  204. if (portMask.enabled(SecondOutput)) ret = ret && CALL_IF_EXISTS(bool, &serial1, connected);
  205. return ret;
  206. }
  207. using BaseClassT::available;
  208. using BaseClassT::read;
  209. // Redirect flush
  210. NO_INLINE void flush() {
  211. if (portMask.enabled(FirstOutput)) serial0.flush();
  212. if (portMask.enabled(SecondOutput)) serial1.flush();
  213. }
  214. NO_INLINE void flushTX() {
  215. if (portMask.enabled(FirstOutput)) CALL_IF_EXISTS(void, &serial0, flushTX);
  216. if (portMask.enabled(SecondOutput)) CALL_IF_EXISTS(void, &serial1, flushTX);
  217. }
  218. // Forward feature queries
  219. SerialFeature features(serial_index_t index) const {
  220. if (index.within(0 + offset, step + offset - 1))
  221. return serial0.features(index);
  222. else if (index.within(step + offset, 2 * step + offset - 1))
  223. return serial1.features(index);
  224. return SerialFeature::None;
  225. }
  226. MultiSerial(Serial0T & serial0, Serial1T & serial1, const SerialMask mask = Both, const bool e = false) :
  227. BaseClassT(e),
  228. portMask(mask), serial0(serial0), serial1(serial1) {}
  229. };
  230. // Build the actual serial object depending on current configuration
  231. #define Serial1Class TERN(SERIAL_RUNTIME_HOOK, RuntimeSerial, BaseSerial)
  232. #define ForwardSerial1Class TERN(SERIAL_RUNTIME_HOOK, RuntimeSerial, ForwardSerial)
  233. #ifdef HAS_MULTI_SERIAL
  234. #define Serial2Class ConditionalSerial
  235. #endif