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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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. #if ENABLED(EMERGENCY_PARSER)
  25. #include "../feature/e_parser.h"
  26. #endif
  27. #ifndef DEC
  28. #define DEC 10
  29. #define HEX 16
  30. #define OCT 8
  31. #define BIN 2
  32. #endif
  33. // flushTX is not implemented in all HAL, so use SFINAE to call the method where it is.
  34. CALL_IF_EXISTS_IMPL(void, flushTX );
  35. CALL_IF_EXISTS_IMPL(bool, connected, true);
  36. // Using Curiously Recurring Template Pattern here to avoid virtual table cost when compiling.
  37. // Since the real serial class is known at compile time, this results in compiler writing a completely
  38. // efficient code
  39. template <class Child>
  40. struct SerialBase {
  41. #if ENABLED(EMERGENCY_PARSER)
  42. const bool ep_enabled;
  43. EmergencyParser::State emergency_state;
  44. inline bool emergency_parser_enabled() { return ep_enabled; }
  45. SerialBase(bool ep_capable) : ep_enabled(ep_capable), emergency_state(EmergencyParser::State::EP_RESET) {}
  46. #else
  47. SerialBase(const bool) {}
  48. #endif
  49. // Static dispatch methods below:
  50. // The most important method here is where it all ends to:
  51. size_t write(uint8_t c) { return static_cast<Child*>(this)->write(c); }
  52. // Called when the parser finished processing an instruction, usually build to nothing
  53. void msgDone() { static_cast<Child*>(this)->msgDone(); }
  54. // Called upon initialization
  55. void begin(const long baudRate) { static_cast<Child*>(this)->begin(baudRate); }
  56. // Called upon destruction
  57. void end() { static_cast<Child*>(this)->end(); }
  58. /** Check for available data from the port
  59. @param index The port index, usually 0 */
  60. bool available(uint8_t index = 0) { return static_cast<Child*>(this)->available(index); }
  61. /** Read a value from the port
  62. @param index The port index, usually 0 */
  63. int read(uint8_t index = 0) { return static_cast<Child*>(this)->read(index); }
  64. // Check if the serial port is connected (usually bypassed)
  65. bool connected() { return static_cast<Child*>(this)->connected(); }
  66. // Redirect flush
  67. void flush() { static_cast<Child*>(this)->flush(); }
  68. // Not all implementation have a flushTX, so let's call them only if the child has the implementation
  69. void flushTX() { CALL_IF_EXISTS(void, static_cast<Child*>(this), flushTX); }
  70. // Glue code here
  71. FORCE_INLINE void write(const char* str) { while (*str) write(*str++); }
  72. FORCE_INLINE void write(const uint8_t* buffer, size_t size) { while (size--) write(*buffer++); }
  73. FORCE_INLINE void print(const char* str) { write(str); }
  74. FORCE_INLINE void print(char c, int base = 0) { print((long)c, base); }
  75. FORCE_INLINE void print(unsigned char c, int base = 0) { print((unsigned long)c, base); }
  76. FORCE_INLINE void print(int c, int base = DEC) { print((long)c, base); }
  77. FORCE_INLINE void print(unsigned int c, int base = DEC) { print((unsigned long)c, base); }
  78. void print(long c, int base = DEC) { if (!base) write(c); write((const uint8_t*)"-", c < 0); printNumber(c < 0 ? -c : c, base); }
  79. void print(unsigned long c, int base = DEC) { printNumber(c, base); }
  80. void print(double c, int digits = 2) { printFloat(c, digits); }
  81. FORCE_INLINE void println(const char s[]) { print(s); println(); }
  82. FORCE_INLINE void println(char c, int base = 0) { print(c, base); println(); }
  83. FORCE_INLINE void println(unsigned char c, int base = 0) { print(c, base); println(); }
  84. FORCE_INLINE void println(int c, int base = DEC) { print(c, base); println(); }
  85. FORCE_INLINE void println(unsigned int c, int base = DEC) { print(c, base); println(); }
  86. FORCE_INLINE void println(long c, int base = DEC) { print(c, base); println(); }
  87. FORCE_INLINE void println(unsigned long c, int base = DEC) { print(c, base); println(); }
  88. FORCE_INLINE void println(double c, int digits = 2) { print(c, digits); println(); }
  89. void println() { write("\r\n"); }
  90. // Print a number with the given base
  91. void printNumber(unsigned long n, const uint8_t base) {
  92. if (n) {
  93. unsigned char buf[8 * sizeof(long)]; // Enough space for base 2
  94. int8_t i = 0;
  95. while (n) {
  96. buf[i++] = n % base;
  97. n /= base;
  98. }
  99. while (i--) write((char)(buf[i] + (buf[i] < 10 ? '0' : 'A' - 10)));
  100. }
  101. else write('0');
  102. }
  103. // Print a decimal number
  104. void printFloat(double number, uint8_t digits) {
  105. // Handle negative numbers
  106. if (number < 0.0) {
  107. write('-');
  108. number = -number;
  109. }
  110. // Round correctly so that print(1.999, 2) prints as "2.00"
  111. double rounding = 0.5;
  112. LOOP_L_N(i, digits) rounding *= 0.1;
  113. number += rounding;
  114. // Extract the integer part of the number and print it
  115. unsigned long int_part = (unsigned long)number;
  116. double remainder = number - (double)int_part;
  117. printNumber(int_part, 10);
  118. // Print the decimal point, but only if there are digits beyond
  119. if (digits) {
  120. write('.');
  121. // Extract digits from the remainder one at a time
  122. while (digits--) {
  123. remainder *= 10.0;
  124. int toPrint = int(remainder);
  125. printNumber(toPrint, 10);
  126. remainder -= toPrint;
  127. }
  128. }
  129. }
  130. };
  131. // All serial instances will be built by chaining the features required for the function in a form of a template
  132. // type definition