My Marlin configs for Fabrikator Mini and CTC i3 Pro B
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

Print.cpp 7.8KB

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