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 9.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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 "macros.h"
  24. #include "serial_base.h"
  25. // 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
  26. template <class SerialT>
  27. struct BaseSerial : public SerialBase< BaseSerial<SerialT> >, public SerialT {
  28. typedef SerialBase< BaseSerial<SerialT> > BaseClassT;
  29. // It's required to implement a write method here to help compiler disambiguate what method to call
  30. using SerialT::write;
  31. using SerialT::flush;
  32. void msgDone() {}
  33. bool available(uint8_t index) { return index == 0 && SerialT::available(); }
  34. int read(uint8_t index) { return index == 0 ? SerialT::read() : -1; }
  35. bool connected() { return CALL_IF_EXISTS(bool, static_cast<SerialT*>(this), connected);; }
  36. void flushTX() { CALL_IF_EXISTS(void, static_cast<SerialT*>(this), flushTX); }
  37. // We have 2 implementation of the same method in both base class, let's say which one we want
  38. using SerialT::available;
  39. using SerialT::read;
  40. using SerialT::begin;
  41. using SerialT::end;
  42. using BaseClassT::print;
  43. using BaseClassT::println;
  44. BaseSerial(const bool e) : BaseClassT(e) {}
  45. // Forward constructor
  46. template <typename... Args>
  47. BaseSerial(const bool e, Args... args) : BaseClassT(e), SerialT(args...) {}
  48. };
  49. // A serial with a condition checked at runtime for its output
  50. // A bit less efficient than static dispatching but since it's only used for ethernet's serial output right now, it's ok.
  51. template <class SerialT>
  52. struct ConditionalSerial : public SerialBase< ConditionalSerial<SerialT> > {
  53. typedef SerialBase< ConditionalSerial<SerialT> > BaseClassT;
  54. bool & condition;
  55. SerialT & out;
  56. NO_INLINE size_t write(uint8_t c) { if (condition) return out.write(c); return 0; }
  57. void flush() { if (condition) out.flush(); }
  58. void begin(long br) { out.begin(br); }
  59. void end() { out.end(); }
  60. void msgDone() {}
  61. bool connected() { return CALL_IF_EXISTS(bool, &out, connected); }
  62. void flushTX() { CALL_IF_EXISTS(void, &out, flushTX); }
  63. bool available(uint8_t index) { return index == 0 && out.available(); }
  64. int read(uint8_t index) { return index == 0 ? out.read() : -1; }
  65. using BaseClassT::available;
  66. using BaseClassT::read;
  67. ConditionalSerial(bool & conditionVariable, SerialT & out, const bool e) : BaseClassT(e), condition(conditionVariable), out(out) {}
  68. };
  69. // A simple foward class that taking a reference to an existing serial instance (likely created in their respective framework)
  70. template <class SerialT>
  71. struct ForwardSerial : public SerialBase< ForwardSerial<SerialT> > {
  72. typedef SerialBase< ForwardSerial<SerialT> > BaseClassT;
  73. SerialT & out;
  74. NO_INLINE size_t write(uint8_t c) { return out.write(c); }
  75. void flush() { out.flush(); }
  76. void begin(long br) { out.begin(br); }
  77. void end() { out.end(); }
  78. void msgDone() {}
  79. // Existing instances implement Arduino's operator bool, so use that if it's available
  80. bool connected() { return Private::HasMember_connected<SerialT>::value ? CALL_IF_EXISTS(bool, &out, connected) : (bool)out; }
  81. void flushTX() { CALL_IF_EXISTS(void, &out, flushTX); }
  82. bool available(uint8_t index) { return index == 0 && out.available(); }
  83. int read(uint8_t index) { return index == 0 ? out.read() : -1; }
  84. bool available() { return out.available(); }
  85. int read() { return out.read(); }
  86. ForwardSerial(const bool e, SerialT & out) : BaseClassT(e), out(out) {}
  87. };
  88. // A class that's can be hooked and unhooked at runtime, useful to capturing the output of the serial interface
  89. template <class SerialT>
  90. struct RuntimeSerial : public SerialBase< RuntimeSerial<SerialT> >, public SerialT {
  91. typedef SerialBase< RuntimeSerial<SerialT> > BaseClassT;
  92. typedef void (*WriteHook)(void * userPointer, uint8_t c);
  93. typedef void (*EndOfMessageHook)(void * userPointer);
  94. WriteHook writeHook;
  95. EndOfMessageHook eofHook;
  96. void * userPointer;
  97. NO_INLINE size_t write(uint8_t c) {
  98. if (writeHook) writeHook(userPointer, c);
  99. return SerialT::write(c);
  100. }
  101. NO_INLINE void msgDone() {
  102. if (eofHook) eofHook(userPointer);
  103. }
  104. bool available(uint8_t index) { return index == 0 && SerialT::available(); }
  105. int read(uint8_t index) { return index == 0 ? SerialT::read() : -1; }
  106. using SerialT::available;
  107. using SerialT::read;
  108. using SerialT::flush;
  109. using SerialT::begin;
  110. using SerialT::end;
  111. using BaseClassT::print;
  112. using BaseClassT::println;
  113. // Underlying implementation might use Arduino's bool operator
  114. bool connected() {
  115. return Private::HasMember_connected<SerialT>::value ? CALL_IF_EXISTS(bool, static_cast<SerialT*>(this), connected) : static_cast<SerialT*>(this)->operator bool();
  116. }
  117. void flushTX() { CALL_IF_EXISTS(void, static_cast<SerialT*>(this), flushTX); }
  118. void setHook(WriteHook writeHook = 0, EndOfMessageHook eofHook = 0, void * userPointer = 0) {
  119. // Order is important here as serial code can be called inside interrupts
  120. // 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
  121. if (userPointer) this->userPointer = userPointer;
  122. this->writeHook = writeHook;
  123. this->eofHook = eofHook;
  124. // Order is important here because of asynchronous access here
  125. // When unsetting a hook, the user pointer must be unset last so that any pending writeHook is still using the old pointer
  126. if (!userPointer) this->userPointer = 0;
  127. }
  128. RuntimeSerial(const bool e) : BaseClassT(e), writeHook(0), eofHook(0), userPointer(0) {}
  129. // Forward constructor
  130. template <typename... Args>
  131. RuntimeSerial(const bool e, Args... args) : BaseClassT(e), SerialT(args...) {}
  132. };
  133. // A class that's duplicating its output conditionally to 2 serial interface
  134. template <class Serial0T, class Serial1T, const uint8_t offset = 0>
  135. struct MultiSerial : public SerialBase< MultiSerial<Serial0T, Serial1T, offset> > {
  136. typedef SerialBase< MultiSerial<Serial0T, Serial1T, offset> > BaseClassT;
  137. uint8_t portMask;
  138. Serial0T & serial0;
  139. Serial1T & serial1;
  140. enum Masks {
  141. FirstOutputMask = (1 << offset),
  142. SecondOutputMask = (1 << (offset + 1)),
  143. AllMask = FirstOutputMask | SecondOutputMask,
  144. };
  145. NO_INLINE size_t write(uint8_t c) {
  146. size_t ret = 0;
  147. if (portMask & FirstOutputMask) ret = serial0.write(c);
  148. if (portMask & SecondOutputMask) ret = serial1.write(c) | ret;
  149. return ret;
  150. }
  151. NO_INLINE void msgDone() {
  152. if (portMask & FirstOutputMask) serial0.msgDone();
  153. if (portMask & SecondOutputMask) serial1.msgDone();
  154. }
  155. bool available(uint8_t index) {
  156. switch(index) {
  157. case 0 + offset: return serial0.available();
  158. case 1 + offset: return serial1.available();
  159. default: return false;
  160. }
  161. }
  162. NO_INLINE int read(uint8_t index) {
  163. switch(index) {
  164. case 0 + offset: return serial0.read();
  165. case 1 + offset: return serial1.read();
  166. default: return -1;
  167. }
  168. }
  169. void begin(const long br) {
  170. if (portMask & FirstOutputMask) serial0.begin(br);
  171. if (portMask & SecondOutputMask) serial1.begin(br);
  172. }
  173. void end() {
  174. if (portMask & FirstOutputMask) serial0.end();
  175. if (portMask & SecondOutputMask) serial1.end();
  176. }
  177. bool connected() {
  178. bool ret = true;
  179. if (portMask & FirstOutputMask) ret = CALL_IF_EXISTS(bool, &serial0, connected);
  180. if (portMask & SecondOutputMask) ret = ret && CALL_IF_EXISTS(bool, &serial1, connected);
  181. return ret;
  182. }
  183. using BaseClassT::available;
  184. using BaseClassT::read;
  185. // Redirect flush
  186. NO_INLINE void flush() {
  187. if (portMask & FirstOutputMask) serial0.flush();
  188. if (portMask & SecondOutputMask) serial1.flush();
  189. }
  190. NO_INLINE void flushTX() {
  191. if (portMask & FirstOutputMask) CALL_IF_EXISTS(void, &serial0, flushTX);
  192. if (portMask & SecondOutputMask) CALL_IF_EXISTS(void, &serial1, flushTX);
  193. }
  194. MultiSerial(Serial0T & serial0, Serial1T & serial1, int8_t mask = AllMask, const bool e = false) :
  195. BaseClassT(e),
  196. portMask(mask), serial0(serial0), serial1(serial1) {}
  197. };
  198. // Build the actual serial object depending on current configuration
  199. #define Serial0Type TERN(SERIAL_RUNTIME_HOOK, RuntimeSerial, BaseSerial)
  200. #define ForwardSerial0Type TERN(SERIAL_RUNTIME_HOOK, RuntimeSerial, ForwardSerial)
  201. #ifdef HAS_MULTI_SERIAL
  202. #define Serial1Type ConditionalSerial
  203. #endif