Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

316 Zeilen
7.7KB

  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. // https://forum.pjrc.com/threads/28932-LC-is-10-9-times-slower-than-T3-1?p=76072&viewfull=1#post76072
  93. void inline divmod10_v2(uint32_t n,uint32_t *div,uint32_t *mod) {
  94. uint32_t p,q;
  95. /* Using 32.16 fixed point representation p.q */
  96. /* p.q = (n+1)/512 */
  97. q = (n&0xFFFF) + 1;
  98. p = (n>>16);
  99. /* p.q = 51*(n+1)/512 */
  100. q = 13107*q;
  101. p = 13107*p;
  102. /* p.q = (1+1/2^8+1/2^16+1/2^24)*51*(n+1)/512 */
  103. q = q + (q>>16) + (p&0xFFFF);
  104. p = p + (p>>16) + (q>>16);
  105. /* divide by 2 */
  106. p = p>>1;
  107. *div = p;
  108. *mod = n-10*p;
  109. }
  110. #define divmod10_asm(div, mod, tmp1, tmp2, const3333) \
  111. divmod10_v2(div, &div, &mod);
  112. /*
  113. #define divmod10_asm(div, mod, tmp1, tmp2, const3333) \
  114. asm ( \
  115. " lsr %2, %0, #16" "\n\t" \
  116. " mul %2, %4" "\n\t" \
  117. " uxth %1, %0" "\n\t" \
  118. " mul %1, %4" "\n\t" \
  119. " add %1, #1" "\n\t" \
  120. " lsr %0, %2, #16" "\n\t" \
  121. " lsl %2, %2, #16" "\n\t" \
  122. " add %1, %2" "\n\t" \
  123. " mov %3, #0" "\n\t" \
  124. " adc %0, %3" "\n\t" \
  125. " lsl %0, %0, #15" "\n\t" \
  126. " lsr %2, %1, #17" "\n\t" \
  127. " orr %0, %2" "\n\t" \
  128. " lsl %1, %1, #15" "\n\t" \
  129. " lsr %2, %1, #16" "\n\t" \
  130. " lsl %3, %0, #16" "\n\t" \
  131. " orr %2, %3" "\n\t" \
  132. " lsr %3, %0, #16" "\n\t" \
  133. " add %1, %0" "\n\t" \
  134. " adc %0, %1" "\n\t" \
  135. " sub %0, %1" "\n\t" \
  136. " add %1, %2" "\n\t" \
  137. " adc %0, %3" "\n\t" \
  138. " lsr %1, %1, #4" "\n\t" \
  139. " mov %3, #10" "\n\t" \
  140. " mul %1, %3" "\n\t" \
  141. " lsr %1, %1, #28" "\n\t" \
  142. : "+l" (div), \
  143. "=&l" (mod), \
  144. "=&l" (tmp1), \
  145. "=&l" (tmp2) \
  146. : "l" (const3333) \
  147. : \
  148. )
  149. */
  150. #else
  151. #define divmod10_asm(_div, _mod, _tmp1, _tmp2, _const3333) \
  152. ({ _tmp1 = _div; _div = _div / 10; _mod = _tmp1 - _div * 10; })
  153. // ({_mod = _div % 10, _div = _div / 10; })
  154. #endif
  155. size_t Print::printNumberDec(unsigned long n, uint8_t sign)
  156. {
  157. uint8_t buf[11], *p;
  158. uint32_t digit;
  159. //uint32_t t1, t2, c3333=0x3333;
  160. p = buf + (sizeof(buf));
  161. do {
  162. divmod10_v2(n, &n, &digit);
  163. //divmod10_asm(n, digit, t1, t2, c3333);
  164. *--p = digit + '0';
  165. } while (n);
  166. if (sign) *--p = '-';
  167. return write(p, sizeof(buf) - (p - buf));
  168. }
  169. size_t Print::printNumberHex(unsigned long n)
  170. {
  171. uint8_t digit, buf[8], *p;
  172. p = buf + (sizeof(buf));
  173. do {
  174. digit = n & 15;
  175. *--p = (digit < 10) ? '0' + digit : 'A' + digit - 10;
  176. n >>= 4;
  177. } while (n);
  178. return write(p, sizeof(buf) - (p - buf));
  179. }
  180. size_t Print::printNumberBin(unsigned long n)
  181. {
  182. uint8_t buf[32], *p;
  183. p = buf + (sizeof(buf));
  184. do {
  185. *--p = '0' + ((uint8_t)n & 1);
  186. n >>= 1;
  187. } while (n);
  188. return write(p, sizeof(buf) - (p - buf));
  189. }
  190. size_t Print::printNumberAny(unsigned long n, uint8_t base)
  191. {
  192. uint8_t digit, buf[21], *p;
  193. uint32_t tmp;
  194. p = buf + sizeof(buf);
  195. do {
  196. tmp = n;
  197. n = n / base;
  198. digit = tmp - n * base;
  199. *--p = (digit < 10) ? '0' + digit : 'A' + digit - 10;
  200. } while (n);
  201. return write(p, sizeof(buf) - (p - buf));
  202. }
  203. #else
  204. size_t Print::printNumber(unsigned long n, uint8_t base, uint8_t sign)
  205. {
  206. uint8_t buf[34];
  207. uint8_t digit, i;
  208. // TODO: make these checks as inline, since base is
  209. // almost always a constant. base = 0 (BYTE) should
  210. // inline as a call directly to write()
  211. if (base == 0) {
  212. return write((uint8_t)n);
  213. } else if (base == 1) {
  214. base = 10;
  215. }
  216. if (n == 0) {
  217. buf[sizeof(buf) - 1] = '0';
  218. i = sizeof(buf) - 1;
  219. } else {
  220. i = sizeof(buf) - 1;
  221. while (1) {
  222. digit = n % base;
  223. buf[i] = ((digit < 10) ? '0' + digit : 'A' + digit - 10);
  224. n /= base;
  225. if (n == 0) break;
  226. i--;
  227. }
  228. }
  229. if (sign) {
  230. i--;
  231. buf[i] = '-';
  232. }
  233. return write(buf + i, sizeof(buf) - i);
  234. }
  235. #endif
  236. size_t Print::printFloat(double number, uint8_t digits)
  237. {
  238. uint8_t sign=0;
  239. size_t count=0;
  240. // Handle negative numbers
  241. if (number < 0.0) {
  242. sign = 1;
  243. number = -number;
  244. }
  245. // Round correctly so that print(1.999, 2) prints as "2.00"
  246. double rounding = 0.5;
  247. for (uint8_t i=0; i<digits; ++i) {
  248. rounding *= 0.1;
  249. }
  250. number += rounding;
  251. // Extract the integer part of the number and print it
  252. unsigned long int_part = (unsigned long)number;
  253. double remainder = number - (double)int_part;
  254. count += printNumber(int_part, 10, sign);
  255. // Print the decimal point, but only if there are digits beyond
  256. if (digits > 0) {
  257. uint8_t n, buf[16], count=1;
  258. buf[0] = '.';
  259. // Extract digits from the remainder one at a time
  260. if (digits > sizeof(buf) - 1) digits = sizeof(buf) - 1;
  261. while (digits-- > 0) {
  262. remainder *= 10.0;
  263. n = (uint8_t)(remainder);
  264. buf[count++] = '0' + n;
  265. remainder -= n;
  266. }
  267. count += write(buf, count);
  268. }
  269. return count;
  270. }