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_base.h 9.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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 "../inc/MarlinConfigPre.h"
  24. #include "macros.h"
  25. #if ENABLED(EMERGENCY_PARSER)
  26. #include "../feature/e_parser.h"
  27. #endif
  28. // flushTX is not implemented in all HAL, so use SFINAE to call the method where it is.
  29. CALL_IF_EXISTS_IMPL(void, flushTX );
  30. CALL_IF_EXISTS_IMPL(bool, connected, true);
  31. // In order to catch usage errors in code, we make the base to encode number explicit
  32. // If given a number (and not this enum), the compiler will reject the overload, falling back to the (double, digit) version
  33. // We don't want hidden conversion of the first parameter to double, so it has to be as hard to do for the compiler as creating this enum
  34. enum class PrintBase {
  35. Dec = 10,
  36. Hex = 16,
  37. Oct = 8,
  38. Bin = 2
  39. };
  40. // A simple forward struct that prevent the compiler to select print(double, int) as a default overload for any type different than
  41. // double or float. For double or float, a conversion exists so the call will be transparent
  42. struct EnsureDouble {
  43. double a;
  44. FORCE_INLINE operator double() { return a; }
  45. // If the compiler breaks on ambiguity here, it's likely because you're calling print(X, base) with X not a double or a float, and a
  46. // base that's not one of PrintBase's value. This exact code is made to detect such error, you NEED to set a base explicitely like this:
  47. // SERIAL_PRINT(v, PrintBase::Hex)
  48. FORCE_INLINE EnsureDouble(double a) : a(a) {}
  49. FORCE_INLINE EnsureDouble(float a) : a(a) {}
  50. };
  51. // Using Curiously Recurring Template Pattern here to avoid virtual table cost when compiling.
  52. // Since the real serial class is known at compile time, this results in the compiler writing
  53. // a completely efficient code.
  54. template <class Child>
  55. struct SerialBase {
  56. #if ENABLED(EMERGENCY_PARSER)
  57. const bool ep_enabled;
  58. EmergencyParser::State emergency_state;
  59. inline bool emergency_parser_enabled() { return ep_enabled; }
  60. SerialBase(bool ep_capable) : ep_enabled(ep_capable), emergency_state(EmergencyParser::State::EP_RESET) {}
  61. #else
  62. SerialBase(const bool) {}
  63. #endif
  64. // Static dispatch methods below:
  65. // The most important method here is where it all ends to:
  66. size_t write(uint8_t c) { return static_cast<Child*>(this)->write(c); }
  67. // Called when the parser finished processing an instruction, usually build to nothing
  68. void msgDone() { static_cast<Child*>(this)->msgDone(); }
  69. // Called upon initialization
  70. void begin(const long baudRate) { static_cast<Child*>(this)->begin(baudRate); }
  71. // Called upon destruction
  72. void end() { static_cast<Child*>(this)->end(); }
  73. /** Check for available data from the port
  74. @param index The port index, usually 0 */
  75. bool available(uint8_t index = 0) { return static_cast<Child*>(this)->available(index); }
  76. /** Read a value from the port
  77. @param index The port index, usually 0 */
  78. int read(uint8_t index = 0) { return static_cast<Child*>(this)->read(index); }
  79. // Check if the serial port is connected (usually bypassed)
  80. bool connected() { return static_cast<Child*>(this)->connected(); }
  81. // Redirect flush
  82. void flush() { static_cast<Child*>(this)->flush(); }
  83. // Not all implementation have a flushTX, so let's call them only if the child has the implementation
  84. void flushTX() { CALL_IF_EXISTS(void, static_cast<Child*>(this), flushTX); }
  85. // Glue code here
  86. FORCE_INLINE void write(const char* str) { while (*str) write(*str++); }
  87. FORCE_INLINE void write(const uint8_t* buffer, size_t size) { while (size--) write(*buffer++); }
  88. FORCE_INLINE void print(const char* str) { write(str); }
  89. // No default argument to avoid ambiguity
  90. NO_INLINE void print(char c, PrintBase base) { printNumber((signed long)c, (uint8_t)base); }
  91. NO_INLINE void print(unsigned char c, PrintBase base) { printNumber((unsigned long)c, (uint8_t)base); }
  92. NO_INLINE void print(int c, PrintBase base) { printNumber((signed long)c, (uint8_t)base); }
  93. NO_INLINE void print(unsigned int c, PrintBase base) { printNumber((unsigned long)c, (uint8_t)base); }
  94. void print(unsigned long c, PrintBase base) { printNumber((unsigned long)c, (uint8_t)base); }
  95. void print(long c, PrintBase base) { printNumber((signed long)c, (uint8_t)base); }
  96. void print(EnsureDouble c, int digits) { printFloat(c, digits); }
  97. // Forward the call to the former's method
  98. FORCE_INLINE void print(char c) { print(c, PrintBase::Dec); }
  99. FORCE_INLINE void print(unsigned char c) { print(c, PrintBase::Dec); }
  100. FORCE_INLINE void print(int c) { print(c, PrintBase::Dec); }
  101. FORCE_INLINE void print(unsigned int c) { print(c, PrintBase::Dec); }
  102. FORCE_INLINE void print(unsigned long c) { print(c, PrintBase::Dec); }
  103. FORCE_INLINE void print(long c) { print(c, PrintBase::Dec); }
  104. FORCE_INLINE void print(double c) { print(c, 2); }
  105. FORCE_INLINE void println(const char s[]) { print(s); println(); }
  106. FORCE_INLINE void println(char c, PrintBase base) { print(c, base); println(); }
  107. FORCE_INLINE void println(unsigned char c, PrintBase base) { print(c, base); println(); }
  108. FORCE_INLINE void println(int c, PrintBase base) { print(c, base); println(); }
  109. FORCE_INLINE void println(unsigned int c, PrintBase base) { print(c, base); println(); }
  110. FORCE_INLINE void println(long c, PrintBase base) { print(c, base); println(); }
  111. FORCE_INLINE void println(unsigned long c, PrintBase base) { print(c, base); println(); }
  112. FORCE_INLINE void println(double c, int digits) { print(c, digits); println(); }
  113. FORCE_INLINE void println() { write('\r'); write('\n'); }
  114. // Forward the call to the former's method
  115. FORCE_INLINE void println(char c) { println(c, PrintBase::Dec); }
  116. FORCE_INLINE void println(unsigned char c) { println(c, PrintBase::Dec); }
  117. FORCE_INLINE void println(int c) { println(c, PrintBase::Dec); }
  118. FORCE_INLINE void println(unsigned int c) { println(c, PrintBase::Dec); }
  119. FORCE_INLINE void println(unsigned long c) { println(c, PrintBase::Dec); }
  120. FORCE_INLINE void println(long c) { println(c, PrintBase::Dec); }
  121. FORCE_INLINE void println(double c) { println(c, 2); }
  122. // Print a number with the given base
  123. NO_INLINE void printNumber(unsigned long n, const uint8_t base) {
  124. if (!base) return; // Hopefully, this should raise visible bug immediately
  125. if (n) {
  126. unsigned char buf[8 * sizeof(long)]; // Enough space for base 2
  127. int8_t i = 0;
  128. while (n) {
  129. buf[i++] = n % base;
  130. n /= base;
  131. }
  132. while (i--) write((char)(buf[i] + (buf[i] < 10 ? '0' : 'A' - 10)));
  133. }
  134. else write('0');
  135. }
  136. void printNumber(signed long n, const uint8_t base) {
  137. if (base == 10 && n < 0) {
  138. n = -n; // This works because all platforms Marlin's builds on are using 2-complement encoding for negative number
  139. // On such CPU, changing the sign of a number is done by inverting the bits and adding one, so if n = 0x80000000 = -2147483648 then
  140. // -n = 0x7FFFFFFF + 1 => 0x80000000 = 2147483648 (if interpreted as unsigned) or -2147483648 if interpreted as signed.
  141. // On non 2-complement CPU, there would be no possible representation for 2147483648.
  142. write('-');
  143. }
  144. printNumber((unsigned long)n , base);
  145. }
  146. // Print a decimal number
  147. NO_INLINE void printFloat(double number, uint8_t digits) {
  148. // Handle negative numbers
  149. if (number < 0.0) {
  150. write('-');
  151. number = -number;
  152. }
  153. // Round correctly so that print(1.999, 2) prints as "2.00"
  154. double rounding = 0.5;
  155. LOOP_L_N(i, digits) rounding *= 0.1;
  156. number += rounding;
  157. // Extract the integer part of the number and print it
  158. unsigned long int_part = (unsigned long)number;
  159. double remainder = number - (double)int_part;
  160. printNumber(int_part, 10);
  161. // Print the decimal point, but only if there are digits beyond
  162. if (digits) {
  163. write('.');
  164. // Extract digits from the remainder one at a time
  165. while (digits--) {
  166. remainder *= 10.0;
  167. unsigned long toPrint = (unsigned long)remainder;
  168. printNumber(toPrint, 10);
  169. remainder -= toPrint;
  170. }
  171. }
  172. }
  173. };
  174. // All serial instances will be built by chaining the features required
  175. // for the function in the form of a template type definition.