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

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