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.

291 satır
7.0KB

  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. #ifdef __MKL26Z64__
  82. // optimized code inspired by Stimmer's optimization
  83. // obviously a dit different, adapted to 32 bit Cortex-M0+
  84. // http://forum.arduino.cc/index.php?topic=167414.msg1293679#msg1293679
  85. // http://forum.arduino.cc/index.php?topic=167414.msg1309482#msg1309482
  86. // equivelant code:
  87. // mod = div % 10;
  88. // div = div / 10;
  89. // tmp1 = {random};
  90. // tmp2 = 10;
  91. #if 1
  92. #define divmod10_asm(div, mod, tmp1, tmp2, const3333) \
  93. asm ( \
  94. " lsr %2, %0, #16" "\n\t" \
  95. " mul %2, %4" "\n\t" \
  96. " uxth %1, %0" "\n\t" \
  97. " mul %1, %4" "\n\t" \
  98. " add %1, #1" "\n\t" \
  99. " lsr %0, %2, #16" "\n\t" \
  100. " lsl %2, %2, #16" "\n\t" \
  101. " add %1, %2" "\n\t" \
  102. " mov %3, #0" "\n\t" \
  103. " adc %0, %3" "\n\t" \
  104. " lsl %0, %0, #15" "\n\t" \
  105. " lsr %2, %1, #17" "\n\t" \
  106. " orr %0, %2" "\n\t" \
  107. " lsl %1, %1, #15" "\n\t" \
  108. " lsr %2, %1, #16" "\n\t" \
  109. " lsl %3, %0, #16" "\n\t" \
  110. " orr %2, %3" "\n\t" \
  111. " lsr %3, %0, #16" "\n\t" \
  112. " add %1, %0" "\n\t" \
  113. " adc %0, %1" "\n\t" \
  114. " sub %0, %1" "\n\t" \
  115. " add %1, %2" "\n\t" \
  116. " adc %0, %3" "\n\t" \
  117. " lsr %1, %1, #4" "\n\t" \
  118. " mov %3, #10" "\n\t" \
  119. " mul %1, %3" "\n\t" \
  120. " lsr %1, %1, #28" "\n\t" \
  121. : "+l" (div), \
  122. "=&l" (mod), \
  123. "=&l" (tmp1), \
  124. "=&l" (tmp2) \
  125. : "l" (const3333) \
  126. : \
  127. )
  128. #else
  129. #define divmod10_asm(_div, _mod, _tmp1, _tmp2, _const3333) \
  130. ({ _tmp1 = _div; _div = _div / 10; _mod = _tmp1 - _div * 10; })
  131. // ({_mod = _div % 10, _div = _div / 10; })
  132. #endif
  133. size_t Print::printNumberDec(unsigned long n, uint8_t sign)
  134. {
  135. uint8_t buf[11], *p;
  136. uint32_t digit, t1, t2, c3333=0x3333;
  137. p = buf + (sizeof(buf));
  138. do {
  139. divmod10_asm(n, digit, t1, t2, c3333);
  140. *--p = digit + '0';
  141. } while (n);
  142. if (sign) *--p = '-';
  143. return write(p, sizeof(buf) - (p - buf));
  144. }
  145. size_t Print::printNumberHex(unsigned long n)
  146. {
  147. uint8_t digit, buf[8], *p;
  148. p = buf + (sizeof(buf));
  149. do {
  150. digit = n & 15;
  151. *--p = (digit < 10) ? '0' + digit : 'A' + digit - 10;
  152. n >>= 4;
  153. } while (n);
  154. return write(p, sizeof(buf) - (p - buf));
  155. }
  156. size_t Print::printNumberBin(unsigned long n)
  157. {
  158. uint8_t buf[32], *p;
  159. p = buf + (sizeof(buf));
  160. do {
  161. *--p = '0' + ((uint8_t)n & 1);
  162. n >>= 1;
  163. } while (n);
  164. return write(p, sizeof(buf) - (p - buf));
  165. }
  166. size_t Print::printNumberAny(unsigned long n, uint8_t base)
  167. {
  168. uint8_t digit, buf[21], *p;
  169. uint32_t tmp;
  170. p = buf + sizeof(buf);
  171. do {
  172. tmp = n;
  173. n = n / base;
  174. digit = tmp - n * base;
  175. *--p = (digit < 10) ? '0' + digit : 'A' + digit - 10;
  176. } while (n);
  177. return write(p, sizeof(buf) - (p - buf));
  178. }
  179. #else
  180. size_t Print::printNumber(unsigned long n, uint8_t base, uint8_t sign)
  181. {
  182. uint8_t buf[34];
  183. uint8_t digit, i;
  184. // TODO: make these checks as inline, since base is
  185. // almost always a constant. base = 0 (BYTE) should
  186. // inline as a call directly to write()
  187. if (base == 0) {
  188. return write((uint8_t)n);
  189. } else if (base == 1) {
  190. base = 10;
  191. }
  192. if (n == 0) {
  193. buf[sizeof(buf) - 1] = '0';
  194. i = sizeof(buf) - 1;
  195. } else {
  196. i = sizeof(buf) - 1;
  197. while (1) {
  198. digit = n % base;
  199. buf[i] = ((digit < 10) ? '0' + digit : 'A' + digit - 10);
  200. n /= base;
  201. if (n == 0) break;
  202. i--;
  203. }
  204. }
  205. if (sign) {
  206. i--;
  207. buf[i] = '-';
  208. }
  209. return write(buf + i, sizeof(buf) - i);
  210. }
  211. #endif
  212. size_t Print::printFloat(double number, uint8_t digits)
  213. {
  214. uint8_t sign=0;
  215. size_t count=0;
  216. // Handle negative numbers
  217. if (number < 0.0) {
  218. sign = 1;
  219. number = -number;
  220. }
  221. // Round correctly so that print(1.999, 2) prints as "2.00"
  222. double rounding = 0.5;
  223. for (uint8_t i=0; i<digits; ++i) {
  224. rounding *= 0.1;
  225. }
  226. number += rounding;
  227. // Extract the integer part of the number and print it
  228. unsigned long int_part = (unsigned long)number;
  229. double remainder = number - (double)int_part;
  230. count += printNumber(int_part, 10, sign);
  231. // Print the decimal point, but only if there are digits beyond
  232. if (digits > 0) {
  233. uint8_t n, buf[8], count=1;
  234. buf[0] = '.';
  235. // Extract digits from the remainder one at a time
  236. if (digits > sizeof(buf) - 1) digits = sizeof(buf) - 1;
  237. while (digits-- > 0) {
  238. remainder *= 10.0;
  239. n = (uint8_t)(remainder);
  240. buf[count++] = '0' + n;
  241. remainder -= n;
  242. }
  243. count += write(buf, count);
  244. }
  245. return count;
  246. }