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

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