Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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. size_t Print::printNumber(unsigned long n, uint8_t base, uint8_t sign)
  62. {
  63. uint8_t buf[34];
  64. uint8_t digit, i;
  65. // TODO: make these checks as inline, since base is
  66. // almost always a constant. base = 0 (BYTE) should
  67. // inline as a call directly to write()
  68. if (base == 0) {
  69. return write((uint8_t)n);
  70. } else if (base == 1) {
  71. base = 10;
  72. }
  73. if (n == 0) {
  74. buf[sizeof(buf) - 1] = '0';
  75. i = sizeof(buf) - 1;
  76. } else {
  77. i = sizeof(buf) - 1;
  78. while (1) {
  79. digit = n % base;
  80. buf[i] = ((digit < 10) ? '0' + digit : 'A' + digit - 10);
  81. n /= base;
  82. if (n == 0) break;
  83. i--;
  84. }
  85. }
  86. if (sign) {
  87. i--;
  88. buf[i] = '-';
  89. }
  90. return write(buf + i, sizeof(buf) - i);
  91. }
  92. size_t Print::printFloat(double number, uint8_t digits)
  93. {
  94. uint8_t sign=0;
  95. size_t count=0;
  96. // Handle negative numbers
  97. if (number < 0.0) {
  98. sign = 1;
  99. number = -number;
  100. }
  101. // Round correctly so that print(1.999, 2) prints as "2.00"
  102. double rounding = 0.5;
  103. for (uint8_t i=0; i<digits; ++i) {
  104. rounding *= 0.1;
  105. }
  106. number += rounding;
  107. // Extract the integer part of the number and print it
  108. unsigned long int_part = (unsigned long)number;
  109. double remainder = number - (double)int_part;
  110. count += printNumber(int_part, 10, sign);
  111. // Print the decimal point, but only if there are digits beyond
  112. if (digits > 0) {
  113. uint8_t n, buf[8], count=1;
  114. buf[0] = '.';
  115. // Extract digits from the remainder one at a time
  116. if (digits > sizeof(buf) - 1) digits = sizeof(buf) - 1;
  117. while (digits-- > 0) {
  118. remainder *= 10.0;
  119. n = (uint8_t)(remainder);
  120. buf[count++] = '0' + n;
  121. remainder -= n;
  122. }
  123. count += write(buf, count);
  124. }
  125. return count;
  126. }