My Marlin configs for Fabrikator Mini and CTC i3 Pro B
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

Print.cpp 7.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. /**
  2. * Print.cpp - Base class that provides print() and println()
  3. * Copyright (c) 2008 David A. Mellis. All right reserved.
  4. *
  5. * This library is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU Lesser General Public
  7. * License as published by the Free Software Foundation; either
  8. * version 2.1 of the License, or (at your option) any later version.
  9. *
  10. * This library is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * Lesser General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Lesser General Public
  16. * License along with this library; if not, write to the Free Software
  17. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  18. *
  19. * Modified 23 November 2006 by David A. Mellis
  20. * Modified 03 August 2015 by Chuck Todd
  21. * Modified 03 February 2018 by Scott Lahteine
  22. */
  23. #include <stdlib.h>
  24. #include <stdio.h>
  25. #include <string.h>
  26. #include <math.h>
  27. #include <inttypes.h>
  28. #include "Print.h"
  29. #include <stdarg.h>
  30. #define ENABLE_PRINTF
  31. // Public Methods //////////////////////////////////////////////////////////////
  32. /* default implementation: may be overridden */
  33. size_t Print::write(const uint8_t *buffer, size_t size) {
  34. size_t n = 0;
  35. while (size--) {
  36. if (write(*buffer++)) n++;
  37. else break;
  38. }
  39. return n;
  40. }
  41. size_t Print::print(const char str[]) { return write(str); }
  42. size_t Print::print(char c) { return write(c); }
  43. size_t Print::print(unsigned char b, int base) { return print((unsigned long) b, base); }
  44. size_t Print::print(int n, int base) { return print((long) n, base); }
  45. size_t Print::print(unsigned int n, int base) { return print((unsigned long) n, base); }
  46. size_t Print::print(double n, int digits) { return printFloat(n, digits); }
  47. size_t Print::print(const Printable& x) { return x.printTo(*this); }
  48. size_t Print::println(void) { return write("\r\n"); }
  49. size_t Print::print(long n, int base) {
  50. if (base == 0) return write(n);
  51. if (base == 10) {
  52. if (n < 0) {
  53. const int t = print('-');
  54. return printNumber(-n, 10) + t;
  55. }
  56. }
  57. return printNumber(n, base);
  58. }
  59. size_t Print::print(unsigned long n, int base) {
  60. if (base == 0) return write(n);
  61. return printNumber(n, base);
  62. }
  63. #define PRINTLN(...) do{ \
  64. size_t n = print(__VA_ARGS__); \
  65. n += println(); \
  66. return n; \
  67. }while(0)
  68. size_t Print::println(const char c[]) { PRINTLN(c); }
  69. size_t Print::println(char c) { PRINTLN(c); }
  70. size_t Print::println(unsigned char b, int base) { PRINTLN(b, base); }
  71. size_t Print::println(int num, int base) { PRINTLN(num, base); }
  72. size_t Print::println(unsigned int num, int base) { PRINTLN(num, base); }
  73. size_t Print::println(long num, int base) { PRINTLN(num, base); }
  74. size_t Print::println(unsigned long num, int base) { PRINTLN(num, base); }
  75. size_t Print::println(double num, int digits) { PRINTLN(num, digits); }
  76. size_t Print::println(const Printable& x) { PRINTLN(x); }
  77. // Private Methods /////////////////////////////////////////////////////////////
  78. size_t Print::printNumber(unsigned long n, uint8_t base) {
  79. char buf[8 * sizeof(long) + 1]; // Assumes 8-bit chars plus zero byte.
  80. char *str = &buf[sizeof(buf) - 1];
  81. *str = '\0';
  82. // prevent crash if called with base == 1
  83. if (base < 2) base = 10;
  84. do {
  85. unsigned long m = n;
  86. n /= base;
  87. char c = m - base * n;
  88. *--str = c < 10 ? c + '0' : c + 'A' - 10;
  89. } while(n);
  90. return write(str);
  91. }
  92. size_t Print::printFloat(double number, uint8_t digits) {
  93. size_t n = 0;
  94. if (isnan(number)) return print("nan");
  95. if (isinf(number)) return print("inf");
  96. if (number > 4294967040.0) return print ("ovf"); // constant determined empirically
  97. if (number <-4294967040.0) return print ("ovf"); // constant determined empirically
  98. // Handle negative numbers
  99. if (number < 0.0) {
  100. n += print('-');
  101. number = -number;
  102. }
  103. // Round correctly so that print(1.999, 2) prints as "2.00"
  104. double rounding = 0.5;
  105. for (uint8_t i = 0; i < digits; ++i) rounding /= 10.0;
  106. number += rounding;
  107. // Extract the integer part of the number and print it
  108. uint32_t int_part = (uint32_t)number;
  109. double remainder = number - (double)int_part;
  110. n += print(int_part);
  111. // Print the decimal point, but only if there are digits beyond
  112. if (digits > 0) n += print('.');
  113. // Extract digits from the remainder one at a time
  114. while (digits-- > 0) {
  115. remainder *= 10.0;
  116. const int toPrint = int(remainder);
  117. n += print(toPrint);
  118. remainder -= toPrint;
  119. }
  120. return n;
  121. }
  122. #ifdef ENABLE_PRINTF
  123. size_t Print::printf(const char *argList, ...) {
  124. const char *ptr;
  125. double floatNum_f32;
  126. va_list argp;
  127. int16_t num_s16;
  128. int32_t num_s32;
  129. uint16_t num_u16;
  130. uint32_t num_u32;
  131. char *str;
  132. char ch;
  133. uint8_t numOfDigits;
  134. va_start(argp, argList);
  135. // Loop through the list to extract all the input arguments
  136. for (ptr = argList; (ch = *ptr); ptr++) {
  137. if (ch == '%') { //Check for '%' as there will be format specifier after it
  138. ptr++;
  139. ch = *ptr;
  140. if (ch >= '0' && ch <= '9') {
  141. numOfDigits = 0;
  142. while (ch >= '0' && ch <= '9') {
  143. numOfDigits = numOfDigits * 10 + ch - '0';
  144. ptr++;
  145. ch = *ptr;
  146. }
  147. }
  148. else
  149. numOfDigits = 0xFF;
  150. switch(ch) { // Decode the type of the argument
  151. case 'C':
  152. case 'c': // Argument type is of char, hence read char data from the argp
  153. ch = va_arg(argp, int);
  154. print(ch);
  155. break;
  156. case 'd': // Argument type is of signed integer, hence read 16bit data from the argp
  157. case 'D':
  158. num_s32 = va_arg(argp, int);
  159. print(num_s32, 10);
  160. break;
  161. case 'u':
  162. case 'U': // Argument type is of integer, hence read 32bit unsigend data
  163. num_u32 = va_arg(argp, uint32_t);
  164. print(num_u32, 10);
  165. break;
  166. case 'x':
  167. case 'X': // Argument type is of hex, hence hexadecimal data from the argp
  168. num_u32 = va_arg(argp, uint32_t);
  169. print(num_u32, 16);
  170. break;
  171. case 'b':
  172. case 'B': // Argument type is of binary,Read int and convert to binary
  173. num_u32 = va_arg(argp, uint32_t);
  174. print(num_u32, 2);
  175. break;
  176. case 'F':
  177. case 'f': // Argument type is of float, hence read double data from the argp
  178. floatNum_f32 = va_arg(argp, double);
  179. printFloat(floatNum_f32, 10);
  180. break;
  181. case 'S':
  182. case 's': // Argument type is of string, hence get the pointer to sting passed
  183. str = va_arg(argp, char *);
  184. print(str);
  185. break;
  186. case '%':
  187. print('%');
  188. break;
  189. }
  190. }
  191. else
  192. print(ch); // As '%' is not detected transmit the char passed
  193. }
  194. va_end(argp);
  195. }
  196. #endif // ENABLE_PRINTF