My Marlin configs for Fabrikator Mini and CTC i3 Pro B
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

Print.cpp 7.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  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 0
  26. // Public Methods //////////////////////////////////////////////////////////////
  27. /* default implementation: may be overridden */
  28. size_t Print::write(const uint8_t *buffer, size_t size)
  29. {
  30. size_t n = 0;
  31. while (size--) {
  32. if (write(*buffer++)) n++;
  33. else break;
  34. }
  35. return n;
  36. }
  37. size_t Print::print(const char str[])
  38. {
  39. //while(1);
  40. return write(str);
  41. }
  42. size_t Print::print(char c)
  43. {
  44. return write(c);
  45. }
  46. size_t Print::print(unsigned char b, int base)
  47. {
  48. return print((unsigned long) b, base);
  49. }
  50. size_t Print::print(int n, int base)
  51. {
  52. return print((long) n, base);
  53. }
  54. size_t Print::print(unsigned int n, int base)
  55. {
  56. return print((unsigned long) n, base);
  57. }
  58. size_t Print::print(long n, int base)
  59. {
  60. if (base == 0) {
  61. return write(n);
  62. } else if (base == 10) {
  63. if (n < 0) {
  64. int t = print('-');
  65. n = -n;
  66. return printNumber(n, 10) + t;
  67. }
  68. return printNumber(n, 10);
  69. } else {
  70. return printNumber(n, base);
  71. }
  72. }
  73. size_t Print::print(unsigned long n, int base)
  74. {
  75. if (base == 0) return write(n);
  76. else return printNumber(n, base);
  77. }
  78. size_t Print::print(double n, int digits)
  79. {
  80. return printFloat(n, digits);
  81. }
  82. size_t Print::print(const Printable& x)
  83. {
  84. return x.printTo(*this);
  85. }
  86. size_t Print::println(void)
  87. {
  88. return write("\r\n");
  89. }
  90. size_t Print::println(const char c[])
  91. {
  92. size_t n = print(c);
  93. n += println();
  94. return n;
  95. }
  96. size_t Print::println(char c)
  97. {
  98. size_t n = print(c);
  99. n += println();
  100. return n;
  101. }
  102. size_t Print::println(unsigned char b, int base)
  103. {
  104. size_t n = print(b, base);
  105. n += println();
  106. return n;
  107. }
  108. size_t Print::println(int num, int base)
  109. {
  110. size_t n = print(num, base);
  111. n += println();
  112. return n;
  113. }
  114. size_t Print::println(unsigned int num, int base)
  115. {
  116. size_t n = print(num, base);
  117. n += println();
  118. return n;
  119. }
  120. size_t Print::println(long num, int base)
  121. {
  122. size_t n = print(num, base);
  123. n += println();
  124. return n;
  125. }
  126. size_t Print::println(unsigned long num, int base)
  127. {
  128. size_t n = print(num, base);
  129. n += println();
  130. return n;
  131. }
  132. size_t Print::println(double num, int digits)
  133. {
  134. size_t n = print(num, digits);
  135. n += println();
  136. return n;
  137. }
  138. size_t Print::println(const Printable& x)
  139. {
  140. size_t n = print(x);
  141. n += println();
  142. return n;
  143. }
  144. // Private Methods /////////////////////////////////////////////////////////////
  145. size_t Print::printNumber(unsigned long n, uint8_t base) {
  146. char buf[8 * sizeof(long) + 1]; // Assumes 8-bit chars plus zero byte.
  147. char *str = &buf[sizeof(buf) - 1];
  148. *str = '\0';
  149. // prevent crash if called with base == 1
  150. if (base < 2) base = 10;
  151. do {
  152. unsigned long m = n;
  153. n /= base;
  154. char c = m - base * n;
  155. *--str = c < 10 ? c + '0' : c + 'A' - 10;
  156. } while(n);
  157. return write(str);
  158. }
  159. size_t Print::printFloat(double number, uint8_t digits)
  160. {
  161. size_t n = 0;
  162. if (isnan(number)) return print("nan");
  163. if (isinf(number)) return print("inf");
  164. if (number > 4294967040.0) return print ("ovf"); // constant determined empirically
  165. if (number <-4294967040.0) return print ("ovf"); // constant determined empirically
  166. // Handle negative numbers
  167. if (number < 0.0)
  168. {
  169. n += print('-');
  170. number = -number;
  171. }
  172. // Round correctly so that print(1.999, 2) prints as "2.00"
  173. double rounding = 0.5;
  174. for (uint8_t i=0; i<digits; ++i)
  175. rounding /= 10.0;
  176. number += rounding;
  177. // Extract the integer part of the number and print it
  178. unsigned long int_part = (unsigned long)number;
  179. double remainder = number - (double)int_part;
  180. n += print(int_part);
  181. // Print the decimal point, but only if there are digits beyond
  182. if (digits > 0) {
  183. n += print(".");
  184. }
  185. // Extract digits from the remainder one at a time
  186. while (digits-- > 0)
  187. {
  188. remainder *= 10.0;
  189. int toPrint = int(remainder);
  190. n += print(toPrint);
  191. remainder -= toPrint;
  192. }
  193. return n;
  194. }
  195. #if (PrintfEnable == 1)
  196. size_t Print::printf(const char *argList, ...)
  197. {
  198. const char *ptr;
  199. double floatNum_f32;
  200. va_list argp;
  201. sint16_t num_s16;
  202. sint32_t num_s32;
  203. uint16_t num_u16;
  204. uint32_t num_u32;
  205. char *str;
  206. char ch;
  207. uint8_t numOfDigits;
  208. va_start(argp, argList);
  209. /* Loop through the list to extract all the input arguments */
  210. for(ptr = argList; *ptr != '\0'; ptr++)
  211. {
  212. ch= *ptr;
  213. if(ch == '%') /*Check for '%' as there will be format specifier after it */
  214. {
  215. ptr++;
  216. ch = *ptr;
  217. if((ch>=0x30) && (ch<=0x39))
  218. {
  219. numOfDigits = 0;
  220. while((ch>=0x30) && (ch<=0x39))
  221. {
  222. numOfDigits = (numOfDigits * 10) + (ch-0x30);
  223. ptr++;
  224. ch = *ptr;
  225. }
  226. }
  227. else
  228. {
  229. numOfDigits = 0xff;
  230. }
  231. switch(ch) /* Decode the type of the argument */
  232. {
  233. case 'C':
  234. case 'c': /* Argument type is of char, hence read char data from the argp */
  235. ch = va_arg(argp, int);
  236. print(ch);
  237. break;
  238. case 'd': /* Argument type is of signed integer, hence read 16bit data from the argp */
  239. case 'D':
  240. num_s32 = va_arg(argp, int);
  241. print(num_s32, 10);
  242. break;
  243. case 'u':
  244. case 'U': /* Argument type is of integer, hence read 32bit unsigend data */
  245. num_u32 = va_arg(argp, uint32_t);
  246. print(num_u32, 10);
  247. break;
  248. case 'x':
  249. case 'X': /* Argument type is of hex, hence hexadecimal data from the argp */
  250. num_u32 = va_arg(argp, uint32_t);
  251. print(num_u32, 16);
  252. break;
  253. case 'b':
  254. case 'B': /* Argument type is of binary,Read int and convert to binary */
  255. num_u32 = va_arg(argp, uint32_t);
  256. print(num_u32, 2);
  257. break;
  258. case 'F':
  259. case 'f': /* Argument type is of float, hence read double data from the argp */
  260. floatNum_f32 = va_arg(argp, double);
  261. printFloat(floatNum_f32,10);
  262. break;
  263. case 'S':
  264. case 's': /* Argument type is of string, hence get the pointer to sting passed */
  265. str = va_arg(argp, char *);
  266. print(str);
  267. break;
  268. case '%':
  269. print('%');
  270. break;
  271. }
  272. }
  273. else
  274. {
  275. /* As '%' is not detected transmit the char passed */
  276. print(ch);
  277. }
  278. }
  279. va_end(argp);
  280. }
  281. #endif