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.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. /* Teensyduino Core Library
  2. * http://www.pjrc.com/teensy/
  3. * Copyright (c) 2017 PJRC.COM, LLC.
  4. *
  5. * Permission is hereby granted, free of charge, to any person obtaining
  6. * a copy of this software and associated documentation files (the
  7. * "Software"), to deal in the Software without restriction, including
  8. * without limitation the rights to use, copy, modify, merge, publish,
  9. * distribute, sublicense, and/or sell copies of the Software, and to
  10. * permit persons to whom the Software is furnished to do so, subject to
  11. * the following conditions:
  12. *
  13. * 1. The above copyright notice and this permission notice shall be
  14. * included in all copies or substantial portions of the Software.
  15. *
  16. * 2. If the Software is incorporated into a build system that allows
  17. * selection among a list of target devices, then similar target
  18. * devices manufactured by PJRC.COM must be included in the list of
  19. * target devices and selectable in the same manner.
  20. *
  21. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  22. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  23. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  24. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  25. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  26. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  27. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  28. * SOFTWARE.
  29. */
  30. // Long ago this file contained code from Arduino.cc, which was
  31. // Copyright (c) 2008 David A. Mellis. No substantial portion of
  32. // Arduino's original code remains. In fact, several improvements
  33. // developed for Teensyduino have made their way back into
  34. // Arduino's code base. :-)
  35. #include <Arduino.h>
  36. #include "debug/printf.h"
  37. #undef printf
  38. size_t Print::write(const uint8_t *buffer, size_t size)
  39. {
  40. size_t count = 0;
  41. while (size--) count += write(*buffer++);
  42. return count;
  43. }
  44. size_t Print::print(const String &s)
  45. {
  46. uint8_t buffer[33];
  47. size_t count = 0;
  48. unsigned int index = 0;
  49. unsigned int len = s.length();
  50. while (len > 0) {
  51. s.getBytes(buffer, sizeof(buffer), index);
  52. unsigned int nbytes = len;
  53. if (nbytes > sizeof(buffer)-1) nbytes = sizeof(buffer)-1;
  54. index += nbytes;
  55. len -= nbytes;
  56. count += write(buffer, nbytes);
  57. }
  58. return count;
  59. }
  60. size_t Print::print(long n)
  61. {
  62. uint8_t sign=0;
  63. if (n < 0) {
  64. sign = '-';
  65. n = -n;
  66. }
  67. return printNumber(n, 10, sign);
  68. }
  69. size_t Print::println(void)
  70. {
  71. uint8_t buf[2]={'\r', '\n'};
  72. return write(buf, 2);
  73. }
  74. extern "C" {
  75. __attribute__((weak))
  76. int _write(int file, char *ptr, int len)
  77. {
  78. ((class Print *)file)->write((uint8_t *)ptr, len);
  79. return len;
  80. }
  81. }
  82. int Print::printf(const char *format, ...)
  83. {
  84. va_list ap;
  85. va_start(ap, format);
  86. #ifdef __STRICT_ANSI__
  87. return 0; // TODO: make this work with -std=c++0x
  88. #else
  89. return vdprintf((int)this, format, ap);
  90. #endif
  91. }
  92. int Print::printf(const __FlashStringHelper *format, ...)
  93. {
  94. va_list ap;
  95. va_start(ap, format);
  96. #ifdef __STRICT_ANSI__
  97. return 0;
  98. #else
  99. return vdprintf((int)this, (const char *)format, ap);
  100. #endif
  101. }
  102. size_t Print::printNumber(unsigned long n, uint8_t base, uint8_t sign)
  103. {
  104. uint8_t buf[34];
  105. uint8_t digit, i;
  106. // TODO: make these checks as inline, since base is
  107. // almost always a constant. base = 0 (BYTE) should
  108. // inline as a call directly to write()
  109. if (base == 0) {
  110. return write((uint8_t)n);
  111. } else if (base == 1) {
  112. base = 10;
  113. }
  114. if (n == 0) {
  115. buf[sizeof(buf) - 1] = '0';
  116. i = sizeof(buf) - 1;
  117. } else {
  118. i = sizeof(buf) - 1;
  119. while (1) {
  120. digit = n % base;
  121. buf[i] = ((digit < 10) ? '0' + digit : 'A' + digit - 10);
  122. n /= base;
  123. if (n == 0) break;
  124. i--;
  125. }
  126. }
  127. if (sign) {
  128. i--;
  129. buf[i] = '-';
  130. }
  131. return write(buf + i, sizeof(buf) - i);
  132. }
  133. size_t Print::printFloat(double number, uint8_t digits)
  134. {
  135. uint8_t sign=0;
  136. size_t count=0;
  137. if (isnan(number)) return print("nan");
  138. if (isinf(number)) return print("inf");
  139. if (number > 4294967040.0f) return print("ovf"); // constant determined empirically
  140. if (number <-4294967040.0f) return print("ovf"); // constant determined empirically
  141. // Handle negative numbers
  142. if (number < 0.0) {
  143. sign = 1;
  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 *= 0.1;
  150. }
  151. number += rounding;
  152. // Extract the integer part of the number and print it
  153. unsigned long int_part = (unsigned long)number;
  154. double remainder = number - (double)int_part;
  155. count += printNumber(int_part, 10, sign);
  156. // Print the decimal point, but only if there are digits beyond
  157. if (digits > 0) {
  158. uint8_t n, buf[16], count=1;
  159. buf[0] = '.';
  160. // Extract digits from the remainder one at a time
  161. if (digits > sizeof(buf) - 1) digits = sizeof(buf) - 1;
  162. while (digits-- > 0) {
  163. remainder *= 10.0;
  164. n = (uint8_t)(remainder);
  165. buf[count++] = '0' + n;
  166. remainder -= n;
  167. }
  168. count += write(buf, count);
  169. }
  170. return count;
  171. }