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.

Print.cpp 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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. */
  17. #include <stdlib.h>
  18. #include <stdio.h>
  19. #include <string.h>
  20. #include <math.h>
  21. #include "wiring.h"
  22. #include "Print.h"
  23. // Public Methods //////////////////////////////////////////////////////////////
  24. /* default implementation: may be overridden */
  25. void Print::write(const char *str)
  26. {
  27. while (*str)
  28. write(*str++);
  29. }
  30. /* default implementation: may be overridden */
  31. void Print::write(const uint8_t *buffer, size_t size)
  32. {
  33. while (size--)
  34. write(*buffer++);
  35. }
  36. void Print::print(const String &s)
  37. {
  38. for (int i = 0; i < s.length(); i++) {
  39. write(s[i]);
  40. }
  41. }
  42. void Print::print(const char str[])
  43. {
  44. write(str);
  45. }
  46. void Print::print(char c, int base)
  47. {
  48. print((long) c, base);
  49. }
  50. void Print::print(unsigned char b, int base)
  51. {
  52. print((unsigned long) b, base);
  53. }
  54. void Print::print(int n, int base)
  55. {
  56. print((long) n, base);
  57. }
  58. void Print::print(unsigned int n, int base)
  59. {
  60. print((unsigned long) n, base);
  61. }
  62. void Print::print(long n, int base)
  63. {
  64. if (base == 0) {
  65. write(n);
  66. } else if (base == 10) {
  67. if (n < 0) {
  68. print('-');
  69. n = -n;
  70. }
  71. printNumber(n, 10);
  72. } else {
  73. printNumber(n, base);
  74. }
  75. }
  76. void Print::print(unsigned long n, int base)
  77. {
  78. if (base == 0) write(n);
  79. else printNumber(n, base);
  80. }
  81. void Print::print(double n, int digits)
  82. {
  83. printFloat(n, digits);
  84. }
  85. void Print::println(void)
  86. {
  87. print('\r');
  88. print('\n');
  89. }
  90. void Print::println(const String &s)
  91. {
  92. print(s);
  93. println();
  94. }
  95. void Print::println(const char c[])
  96. {
  97. print(c);
  98. println();
  99. }
  100. void Print::println(char c, int base)
  101. {
  102. print(c, base);
  103. println();
  104. }
  105. void Print::println(unsigned char b, int base)
  106. {
  107. print(b, base);
  108. println();
  109. }
  110. void Print::println(int n, int base)
  111. {
  112. print(n, base);
  113. println();
  114. }
  115. void Print::println(unsigned int n, int base)
  116. {
  117. print(n, base);
  118. println();
  119. }
  120. void Print::println(long n, int base)
  121. {
  122. print(n, base);
  123. println();
  124. }
  125. void Print::println(unsigned long n, int base)
  126. {
  127. print(n, base);
  128. println();
  129. }
  130. void Print::println(double n, int digits)
  131. {
  132. print(n, digits);
  133. println();
  134. }
  135. // Private Methods /////////////////////////////////////////////////////////////
  136. void Print::printNumber(unsigned long n, uint8_t base)
  137. {
  138. unsigned char buf[8 * sizeof(long)]; // Assumes 8-bit chars.
  139. unsigned long i = 0;
  140. if (n == 0) {
  141. print('0');
  142. return;
  143. }
  144. while (n > 0) {
  145. buf[i++] = n % base;
  146. n /= base;
  147. }
  148. for (; i > 0; i--)
  149. print((char) (buf[i - 1] < 10 ?
  150. '0' + buf[i - 1] :
  151. 'A' + buf[i - 1] - 10));
  152. }
  153. void Print::printFloat(double number, uint8_t digits)
  154. {
  155. // Handle negative numbers
  156. if (number < 0.0)
  157. {
  158. print('-');
  159. number = -number;
  160. }
  161. // Round correctly so that print(1.999, 2) prints as "2.00"
  162. double rounding = 0.5;
  163. for (uint8_t i=0; i<digits; ++i)
  164. rounding /= 10.0;
  165. number += rounding;
  166. // Extract the integer part of the number and print it
  167. unsigned long int_part = (unsigned long)number;
  168. double remainder = number - (double)int_part;
  169. print(int_part);
  170. // Print the decimal point, but only if there are digits beyond
  171. if (digits > 0)
  172. print(".");
  173. // Extract digits from the remainder one at a time
  174. while (digits-- > 0)
  175. {
  176. remainder *= 10.0;
  177. int toPrint = int(remainder);
  178. print(toPrint);
  179. remainder -= toPrint;
  180. }
  181. }