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

5 jaren geleden
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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. if (buffer == nullptr) return 0;
  41. size_t count = 0;
  42. while (size--) count += write(*buffer++);
  43. return count;
  44. }
  45. size_t Print::print(const String &s)
  46. {
  47. uint8_t buffer[33];
  48. size_t count = 0;
  49. unsigned int index = 0;
  50. unsigned int len = s.length();
  51. while (len > 0) {
  52. s.getBytes(buffer, sizeof(buffer), index);
  53. unsigned int nbytes = len;
  54. if (nbytes > sizeof(buffer)-1) nbytes = sizeof(buffer)-1;
  55. index += nbytes;
  56. len -= nbytes;
  57. count += write(buffer, nbytes);
  58. }
  59. return count;
  60. }
  61. size_t Print::print(long n)
  62. {
  63. uint8_t sign=0;
  64. if (n < 0) {
  65. sign = '-';
  66. n = -n;
  67. }
  68. return printNumber(n, 10, sign);
  69. }
  70. size_t Print::print(int64_t n)
  71. {
  72. if (n < 0) return printNumber64(-n, 10, 1);
  73. return printNumber64(n, 10, 0);
  74. }
  75. size_t Print::println(void)
  76. {
  77. uint8_t buf[2]={'\r', '\n'};
  78. return write(buf, 2);
  79. }
  80. extern "C" {
  81. __attribute__((weak))
  82. int _write(int file, char *ptr, int len)
  83. {
  84. ((class Print *)file)->write((uint8_t *)ptr, len);
  85. return len;
  86. }
  87. }
  88. int Print::printf(const char *format, ...)
  89. {
  90. va_list ap;
  91. va_start(ap, format);
  92. #ifdef __STRICT_ANSI__
  93. return 0; // TODO: make this work with -std=c++0x
  94. #else
  95. return vdprintf((int)this, format, ap);
  96. #endif
  97. }
  98. int Print::printf(const __FlashStringHelper *format, ...)
  99. {
  100. va_list ap;
  101. va_start(ap, format);
  102. #ifdef __STRICT_ANSI__
  103. return 0;
  104. #else
  105. return vdprintf((int)this, (const char *)format, ap);
  106. #endif
  107. }
  108. size_t Print::printNumber(unsigned long n, uint8_t base, uint8_t sign)
  109. {
  110. uint8_t buf[34];
  111. uint8_t digit, i;
  112. // TODO: make these checks as inline, since base is
  113. // almost always a constant. base = 0 (BYTE) should
  114. // inline as a call directly to write()
  115. if (base == 0) {
  116. return write((uint8_t)n);
  117. } else if (base == 1) {
  118. base = 10;
  119. }
  120. if (n == 0) {
  121. buf[sizeof(buf) - 1] = '0';
  122. i = sizeof(buf) - 1;
  123. } else {
  124. i = sizeof(buf) - 1;
  125. while (1) {
  126. digit = n % base;
  127. buf[i] = ((digit < 10) ? '0' + digit : 'A' + digit - 10);
  128. n /= base;
  129. if (n == 0) break;
  130. i--;
  131. }
  132. }
  133. if (sign) {
  134. i--;
  135. buf[i] = '-';
  136. }
  137. return write(buf + i, sizeof(buf) - i);
  138. }
  139. size_t Print::printNumber64(uint64_t n, uint8_t base, uint8_t sign)
  140. {
  141. uint8_t buf[66];
  142. uint8_t digit, i;
  143. if (base < 2) return 0;
  144. if (n == 0) {
  145. buf[sizeof(buf) - 1] = '0';
  146. i = sizeof(buf) - 1;
  147. } else {
  148. i = sizeof(buf) - 1;
  149. while (1) {
  150. digit = n % base;
  151. buf[i] = ((digit < 10) ? '0' + digit : 'A' + digit - 10);
  152. n /= base;
  153. if (n == 0) break;
  154. i--;
  155. }
  156. }
  157. if (sign) {
  158. i--;
  159. buf[i] = '-';
  160. }
  161. return write(buf + i, sizeof(buf) - i);
  162. }
  163. size_t Print::printFloat(double number, uint8_t digits)
  164. {
  165. uint8_t sign=0;
  166. size_t count=0;
  167. if (isnan(number)) return print("nan");
  168. if (isinf(number)) return print("inf");
  169. if (number > 4294967040.0f) return print("ovf"); // constant determined empirically
  170. if (number <-4294967040.0f) return print("ovf"); // constant determined empirically
  171. // Handle negative numbers
  172. if (number < 0.0) {
  173. sign = 1;
  174. number = -number;
  175. }
  176. // Round correctly so that print(1.999, 2) prints as "2.00"
  177. double rounding = 0.5;
  178. for (uint8_t i=0; i<digits; ++i) {
  179. rounding *= 0.1;
  180. }
  181. number += rounding;
  182. // Extract the integer part of the number and print it
  183. unsigned long int_part = (unsigned long)number;
  184. double remainder = number - (double)int_part;
  185. count += printNumber(int_part, 10, sign);
  186. // Print the decimal point, but only if there are digits beyond
  187. if (digits > 0) {
  188. uint8_t n, buf[16], count=1;
  189. buf[0] = '.';
  190. // Extract digits from the remainder one at a time
  191. if (digits > sizeof(buf) - 1) digits = sizeof(buf) - 1;
  192. while (digits-- > 0) {
  193. remainder *= 10.0;
  194. n = (uint8_t)(remainder);
  195. buf[count++] = '0' + n;
  196. remainder -= n;
  197. }
  198. count += write(buf, count);
  199. }
  200. return count;
  201. }