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.

преди 11 години
преди 11 години
преди 11 години
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. /*
  2. Print.cpp - Base class that provides print() and println()
  3. Copyright (c) 2008 David A. Mellis. All right reserved.
  4. many modifications, by Paul Stoffregen <paul@pjrc.com>
  5. This library is free software; you can redistribute it and/or
  6. modify it under the terms of the GNU Lesser General Public
  7. License as published by the Free Software Foundation; either
  8. version 2.1 of the License, or (at your option) any later version.
  9. This library is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. Lesser General Public License for more details.
  13. You should have received a copy of the GNU Lesser General Public
  14. License along with this library; if not, write to the Free Software
  15. Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  16. Modified 23 November 2006 by David A. Mellis
  17. */
  18. //#include <stdio.h>
  19. //#include <string.h>
  20. #include <inttypes.h>
  21. //#include <math.h>
  22. //#include <avr/pgmspace.h>
  23. //#include "wiring.h"
  24. #include "Print.h"
  25. size_t Print::write(const uint8_t *buffer, size_t size)
  26. {
  27. size_t count = 0;
  28. while (size--) count += write(*buffer++);
  29. return count;
  30. }
  31. size_t Print::print(const String &s)
  32. {
  33. uint8_t buffer[33];
  34. size_t count = 0;
  35. unsigned int index = 0;
  36. unsigned int len = s.length();
  37. while (len > 0) {
  38. s.getBytes(buffer, sizeof(buffer), index);
  39. unsigned int nbytes = len;
  40. if (nbytes > sizeof(buffer)-1) nbytes = sizeof(buffer)-1;
  41. index += nbytes;
  42. len -= nbytes;
  43. count += write(buffer, nbytes);
  44. }
  45. return count;
  46. }
  47. size_t Print::print(long n)
  48. {
  49. uint8_t sign=0;
  50. if (n < 0) {
  51. sign = '-';
  52. n = -n;
  53. }
  54. return printNumber(n, 10, sign);
  55. }
  56. size_t Print::println(void)
  57. {
  58. uint8_t buf[2]={'\r', '\n'};
  59. return write(buf, 2);
  60. }
  61. extern "C" {
  62. __attribute__((weak))
  63. int _write(int file, char *ptr, int len)
  64. {
  65. ((class Print *)file)->write((uint8_t *)ptr, len);
  66. return 0;
  67. }
  68. }
  69. int Print::printf(const char *format, ...)
  70. {
  71. va_list ap;
  72. va_start(ap, format);
  73. return vdprintf((int)this, format, ap);
  74. }
  75. int Print::printf(const __FlashStringHelper *format, ...)
  76. {
  77. va_list ap;
  78. va_start(ap, format);
  79. return vdprintf((int)this, (const char *)format, ap);
  80. }
  81. size_t Print::printNumber(unsigned long n, uint8_t base, uint8_t sign)
  82. {
  83. uint8_t buf[34];
  84. uint8_t digit, i;
  85. // TODO: make these checks as inline, since base is
  86. // almost always a constant. base = 0 (BYTE) should
  87. // inline as a call directly to write()
  88. if (base == 0) {
  89. return write((uint8_t)n);
  90. } else if (base == 1) {
  91. base = 10;
  92. }
  93. if (n == 0) {
  94. buf[sizeof(buf) - 1] = '0';
  95. i = sizeof(buf) - 1;
  96. } else {
  97. i = sizeof(buf) - 1;
  98. while (1) {
  99. digit = n % base;
  100. buf[i] = ((digit < 10) ? '0' + digit : 'A' + digit - 10);
  101. n /= base;
  102. if (n == 0) break;
  103. i--;
  104. }
  105. }
  106. if (sign) {
  107. i--;
  108. buf[i] = '-';
  109. }
  110. return write(buf + i, sizeof(buf) - i);
  111. }
  112. size_t Print::printFloat(double number, uint8_t digits)
  113. {
  114. uint8_t sign=0;
  115. size_t count=0;
  116. // Handle negative numbers
  117. if (number < 0.0) {
  118. sign = 1;
  119. number = -number;
  120. }
  121. // Round correctly so that print(1.999, 2) prints as "2.00"
  122. double rounding = 0.5;
  123. for (uint8_t i=0; i<digits; ++i) {
  124. rounding *= 0.1;
  125. }
  126. number += rounding;
  127. // Extract the integer part of the number and print it
  128. unsigned long int_part = (unsigned long)number;
  129. double remainder = number - (double)int_part;
  130. count += printNumber(int_part, 10, sign);
  131. // Print the decimal point, but only if there are digits beyond
  132. if (digits > 0) {
  133. uint8_t n, buf[8], count=1;
  134. buf[0] = '.';
  135. // Extract digits from the remainder one at a time
  136. if (digits > sizeof(buf) - 1) digits = sizeof(buf) - 1;
  137. while (digits-- > 0) {
  138. remainder *= 10.0;
  139. n = (uint8_t)(remainder);
  140. buf[count++] = '0' + n;
  141. remainder -= n;
  142. }
  143. count += write(buf, count);
  144. }
  145. return count;
  146. }