Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

Print.cpp 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515
  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. #if ARDUINO >= 100
  26. #else
  27. void Print::write(const char *str)
  28. {
  29. write((const uint8_t *)str, strlen(str));
  30. }
  31. #endif
  32. #if ARDUINO >= 100
  33. size_t Print::write(const uint8_t *buffer, size_t size)
  34. {
  35. size_t count = 0;
  36. while (size--) count += write(*buffer++);
  37. return count;
  38. }
  39. #else
  40. void Print::write(const uint8_t *buffer, size_t size)
  41. {
  42. while (size--) write(*buffer++);
  43. }
  44. #endif
  45. #if ARDUINO >= 100
  46. size_t Print::print(const String &s)
  47. {
  48. uint8_t buffer[33];
  49. size_t count = 0;
  50. unsigned int index = 0;
  51. unsigned int len = s.length();
  52. while (len > 0) {
  53. s.getBytes(buffer, sizeof(buffer), index);
  54. unsigned int nbytes = len;
  55. if (nbytes > sizeof(buffer)-1) nbytes = sizeof(buffer)-1;
  56. index += nbytes;
  57. len -= nbytes;
  58. count += write(buffer, nbytes);
  59. }
  60. return count;
  61. }
  62. #else
  63. void Print::print(const String &s)
  64. {
  65. unsigned int len = s.length();
  66. for (unsigned int i=0; i < len; i++) {
  67. write(s[i]);
  68. }
  69. }
  70. #endif
  71. #if ARDUINO >= 100
  72. size_t Print::print(const __FlashStringHelper *ifsh)
  73. {
  74. uint8_t buffer[32];
  75. size_t count = 0;
  76. const char PROGMEM *p = (const char PROGMEM *)ifsh;
  77. unsigned int len = strlen_P(p);
  78. while (len > 0) {
  79. unsigned int nbytes = len;
  80. if (nbytes > sizeof(buffer)) nbytes = sizeof(buffer);
  81. memcpy_P(buffer, p, nbytes);
  82. p += nbytes;
  83. len -= nbytes;
  84. count += write(buffer, nbytes);
  85. }
  86. return count;
  87. }
  88. #else
  89. void Print::print(const __FlashStringHelper *ifsh)
  90. {
  91. const char PROGMEM *p = (const char PROGMEM *)ifsh;
  92. while (1) {
  93. unsigned char c = pgm_read_byte(p++);
  94. if (c == 0) return;
  95. write(c);
  96. }
  97. }
  98. #endif
  99. #if ARDUINO >= 100
  100. size_t Print::print(long n)
  101. {
  102. uint8_t sign=0;
  103. if (n < 0) {
  104. sign = 1;
  105. n = -n;
  106. }
  107. return printNumber(n, sign, 10);
  108. }
  109. #else
  110. void Print::print(long n)
  111. {
  112. uint8_t sign=0;
  113. if (n < 0) {
  114. sign = 1;
  115. n = -n;
  116. }
  117. printNumber(n, sign, 10);
  118. }
  119. #endif
  120. #if ARDUINO >= 100
  121. size_t Print::println(void)
  122. {
  123. uint8_t buf[2]={'\r', '\n'};
  124. return write(buf, 2);
  125. }
  126. #else
  127. void Print::println(void)
  128. {
  129. uint8_t buf[2]={'\r', '\n'};
  130. write(buf, 2);
  131. }
  132. #endif
  133. #if ARDUINO >= 100
  134. static int printf_putchar(char c, FILE *fp)
  135. {
  136. ((class Print *)(fdev_get_udata(fp)))->write((uint8_t)c);
  137. return 0;
  138. }
  139. int Print::printf(const char *format, ...)
  140. {
  141. FILE f;
  142. va_list ap;
  143. fdev_setup_stream(&f, printf_putchar, NULL, _FDEV_SETUP_WRITE);
  144. fdev_set_udata(&f, this);
  145. va_start(ap, format);
  146. return vfprintf(&f, format, ap);
  147. }
  148. int Print::printf(const __FlashStringHelper *format, ...)
  149. {
  150. FILE f;
  151. va_list ap;
  152. fdev_setup_stream(&f, printf_putchar, NULL, _FDEV_SETUP_WRITE);
  153. fdev_set_udata(&f, this);
  154. va_start(ap, format);
  155. return vfprintf_P(&f, (const char *)format, ap);
  156. }
  157. #endif
  158. //#define USE_HACKER_DELIGHT_OPTIMIZATION
  159. #define USE_STIMMER_OPTIMIZATION
  160. //#define USE_BENCHMARK_CODE
  161. #ifdef USE_HACKER_DELIGHT_OPTIMIZATION
  162. // Adapted from Hacker's Delight (Henry Warren, ISBN 0321842685) www.hackersdelight.org
  163. // by Rob Tillaart, Tom Carpenter, "genom2" with input from others...
  164. // http://forum.arduino.cc/index.php?topic=167414.0
  165. //
  166. #define divmod10_asm(in32, tmp32, mod8) \
  167. asm volatile ( \
  168. "mov %2, %A0 \n\t" /* mod = in */ \
  169. "ori %A0, 1 \n\t" /* q = in | 1 */ \
  170. "movw %A1, %A0 \n\t" /* x = q */ \
  171. "movw %C1, %C0 \n\t" \
  172. "lsr %D1 \n\t" /* x = x >> 2 */ \
  173. "ror %C1 \n\t" \
  174. "ror %B1 \n\t" \
  175. "ror %A1 \n\t" \
  176. "lsr %D1 \n\t" \
  177. "ror %C1 \n\t" \
  178. "ror %B1 \n\t" \
  179. "ror %A1 \n\t" \
  180. "sub %A0, %A1 \n\t" /* q = q - x */ \
  181. "sbc %B0, %B1 \n\t" \
  182. "sbc %C0, %C1 \n\t" \
  183. "sbc %D0, %D1 \n\t" \
  184. "movw %A1, %A0 \n\t" /* x = q */ \
  185. "movw %C1, %C0 \n\t" \
  186. "lsr %D1 \n\t" /* x = x >> 4 */ \
  187. "ror %C1 \n\t" \
  188. "ror %B1 \n\t" \
  189. "ror %A1 \n\t" \
  190. "lsr %D1 \n\t" \
  191. "ror %C1 \n\t" \
  192. "ror %B1 \n\t" \
  193. "ror %A1 \n\t" \
  194. "lsr %D1 \n\t" \
  195. "ror %C1 \n\t" \
  196. "ror %B1 \n\t" \
  197. "ror %A1 \n\t" \
  198. "lsr %D1 \n\t" \
  199. "ror %C1 \n\t" \
  200. "ror %B1 \n\t" \
  201. "ror %A1 \n\t" \
  202. "add %A1, %A0 \n\t" /* x = x + q */ \
  203. "adc %B1, %B0 \n\t" \
  204. "adc %C1, %C0 \n\t" \
  205. "adc %D1, %D0 \n\t" \
  206. "movw %A0, %A1 \n\t" /* q = x */ \
  207. "movw %C0, %C1 \n\t" \
  208. "add %A0, %B1 \n\t" /* q = q + (x >> 8) */ \
  209. "adc %B0, %C1 \n\t" \
  210. "adc %C0, %D1 \n\t" \
  211. "adc %D0, r1 \n\t" \
  212. "mov %A0, %B0 \n\t" /* q = q >> 8 */ \
  213. "mov %B0, %C0 \n\t" \
  214. "mov %C0, %D0 \n\t" \
  215. "eor %D0, %D0 \n\t" \
  216. "add %A0, %A1 \n\t" /* q = q + x */ \
  217. "adc %B0, %B1 \n\t" \
  218. "adc %C0, %C1 \n\t" \
  219. "adc %D0, %D1 \n\t" \
  220. "mov %A0, %B0 \n\t" /* q = q >> 8 */ \
  221. "mov %B0, %C0 \n\t" \
  222. "mov %C0, %D0 \n\t" \
  223. "eor %D0, %D0 \n\t" \
  224. "add %A0, %A1 \n\t" /* q = q + x */ \
  225. "adc %B0, %B1 \n\t" \
  226. "adc %C0, %C1 \n\t" \
  227. "adc %D0, %D1 \n\t" \
  228. "mov %A0, %B0 \n\t" /* q = q >> 8 */ \
  229. "mov %B0, %C0 \n\t" \
  230. "mov %C0, %D0 \n\t" \
  231. "eor %D0, %D0 \n\t" \
  232. "add %A0, %A1 \n\t" /* q = q + x */ \
  233. "adc %B0, %B1 \n\t" \
  234. "adc %C0, %C1 \n\t" \
  235. "adc %D0, %D1 \n\t" \
  236. "andi %A0, 0xF8 \n\t" /* q = q & ~0x7 */ \
  237. "sub %2, %A0 \n\t" /* mod = mod - q */ \
  238. "lsr %D0 \n\t" /* q = q >> 2 */ \
  239. "ror %C0 \n\t" \
  240. "ror %B0 \n\t" \
  241. "ror %A0 \n\t" \
  242. "lsr %D0 \n\t" \
  243. "ror %C0 \n\t" \
  244. "ror %B0 \n\t" \
  245. "ror %A0 \n\t" \
  246. "sub %2, %A0 \n\t" /* mod = mod - q */ \
  247. "lsr %D0 \n\t" /* q = q >> 1 */ \
  248. "ror %C0 \n\t" \
  249. "ror %B0 \n\t" \
  250. "ror %A0 \n\t" \
  251. : "+d" (in32), "=r" (tmp32), "=r" (mod8) : : "r0" \
  252. )
  253. #endif // USE_HACKER_DELIGHT_OPTIMIZATION
  254. #ifdef USE_STIMMER_OPTIMIZATION
  255. // http://forum.arduino.cc/index.php?topic=167414.msg1293679#msg1293679
  256. // http://forum.arduino.cc/index.php?topic=167414.msg1309482#msg1309482
  257. // equivelant code:
  258. // mod8 = in32 % 10;
  259. // in32 = in32 / 10;
  260. // tmp8 = 10;
  261. #define divmod10_asm(in32, mod8, tmp8) \
  262. asm volatile ( \
  263. " ldi %2,51 \n\t" \
  264. " mul %A0,%2 \n\t" \
  265. " clr %A0 \n\t" \
  266. " add r0,%2 \n\t" \
  267. " adc %A0,r1 \n\t" \
  268. " mov %1,r0 \n\t" \
  269. " mul %B0,%2 \n\t" \
  270. " clr %B0 \n\t" \
  271. " add %A0,r0 \n\t" \
  272. " adc %B0,r1 \n\t" \
  273. " mul %C0,%2 \n\t" \
  274. " clr %C0 \n\t" \
  275. " add %B0,r0 \n\t" \
  276. " adc %C0,r1 \n\t" \
  277. " mul %D0,%2 \n\t" \
  278. " clr %D0 \n\t" \
  279. " add %C0,r0 \n\t" \
  280. " adc %D0,r1 \n\t" \
  281. " clr r1 \n\t" \
  282. " add %1,%A0 \n\t" \
  283. " adc %A0,%B0 \n\t" \
  284. " adc %B0,%C0 \n\t" \
  285. " adc %C0,%D0 \n\t" \
  286. " adc %D0,r1 \n\t" \
  287. " add %1,%B0 \n\t" \
  288. " adc %A0,%C0 \n\t" \
  289. " adc %B0,%D0 \n\t" \
  290. " adc %C0,r1 \n\t" \
  291. " adc %D0,r1 \n\t" \
  292. " add %1,%D0 \n\t" \
  293. " adc %A0,r1 \n\t" \
  294. " adc %B0,r1 \n\t" \
  295. " adc %C0,r1 \n\t" \
  296. " adc %D0,r1 \n\t" \
  297. " lsr %D0 \n\t" \
  298. " ror %C0 \n\t" \
  299. " ror %B0 \n\t" \
  300. " ror %A0 \n\t" \
  301. " ror %1 \n\t" \
  302. " ldi %2,10 \n\t" \
  303. " mul %1,%2 \n\t" \
  304. " mov %1,r1 \n\t" \
  305. " clr r1 \n\t" \
  306. :"+r"(in32),"=d"(mod8),"=d"(tmp8) : : "r0")
  307. #endif // USE_STIMMER_OPTIMIZATION
  308. #ifdef USE_BENCHMARK_CODE
  309. uint32_t usec_print = 0;
  310. #endif
  311. #if ARDUINO >= 100
  312. size_t Print::printNumberDec(unsigned long n, uint8_t sign)
  313. #else
  314. void Print::printNumberDec(unsigned long n, uint8_t sign)
  315. #endif
  316. {
  317. uint8_t digit, buf[11], *p;
  318. #if defined(USE_HACKER_DELIGHT_OPTIMIZATION)
  319. uint32_t tmp32;
  320. #elif defined(USE_STIMMER_OPTIMIZATION)
  321. uint8_t tmp8;
  322. #endif
  323. #ifdef USE_BENCHMARK_CODE
  324. uint32_t usec = micros();
  325. #endif
  326. p = buf + (sizeof(buf)-1);
  327. do {
  328. #if defined(USE_HACKER_DELIGHT_OPTIMIZATION)
  329. divmod10_asm(n, tmp32, digit);
  330. #elif defined(USE_STIMMER_OPTIMIZATION)
  331. divmod10_asm(n, digit, tmp8);
  332. #else
  333. tmp32 = n;
  334. n = n / 10;
  335. digit = tmp32 - n * 10;
  336. #endif
  337. *--p = digit + '0';
  338. } while (n);
  339. if (sign) *--p = '-';
  340. #ifdef USE_BENCHMARK_CODE
  341. usec_print += micros() - usec;
  342. #endif
  343. #if ARDUINO >= 100
  344. return write(p, sizeof(buf)-1 - (p - buf));
  345. #else
  346. write(p, sizeof(buf)-1 - (p - buf));
  347. #endif
  348. }
  349. #if ARDUINO >= 100
  350. size_t Print::printNumberHex(unsigned long n)
  351. #else
  352. void Print::printNumberHex(unsigned long n)
  353. #endif
  354. {
  355. uint8_t digit, buf[8], *p;
  356. p = buf + (sizeof(buf)-1);
  357. do {
  358. digit = n & 15;
  359. *--p = (digit < 10) ? '0' + digit : 'A' + digit - 10;
  360. n >>= 4;
  361. } while (n);
  362. #if ARDUINO >= 100
  363. return write(p, sizeof(buf)-1 - (p - buf));
  364. #else
  365. write(p, sizeof(buf)-1 - (p - buf));
  366. #endif
  367. }
  368. #if ARDUINO >= 100
  369. size_t Print::printNumberBin(unsigned long n)
  370. #else
  371. void Print::printNumberBin(unsigned long n)
  372. #endif
  373. {
  374. uint8_t buf[32], *p;
  375. p = buf + (sizeof(buf)-1);
  376. do {
  377. *--p = '0' + ((uint8_t)n & 1);
  378. n >>= 1;
  379. } while (n);
  380. #if ARDUINO >= 100
  381. return write(p, sizeof(buf)-1 - (p - buf));
  382. #else
  383. write(p, sizeof(buf)-1 - (p - buf));
  384. #endif
  385. }
  386. #if ARDUINO >= 100
  387. size_t Print::printNumberAny(unsigned long n, uint8_t base)
  388. #else
  389. void Print::printNumberAny(unsigned long n, uint8_t base)
  390. #endif
  391. {
  392. uint8_t digit, buf[21], *p;
  393. uint32_t tmp;
  394. //uint32_t usec;
  395. //usec = micros();
  396. p = buf + (sizeof(buf)-1);
  397. do {
  398. tmp = n;
  399. n = n / base;
  400. digit = tmp - n * base;
  401. *--p = (digit < 10) ? '0' + digit : 'A' + digit - 10;
  402. } while (n);
  403. //usec_print += micros() - usec;
  404. #if ARDUINO >= 100
  405. return write(p, sizeof(buf)-1 - (p - buf));
  406. #else
  407. write(p, sizeof(buf)-1 - (p - buf));
  408. #endif
  409. }
  410. #if ARDUINO >= 100
  411. size_t Print::printFloat(double number, uint8_t digits)
  412. #else
  413. void Print::printFloat(double number, uint8_t digits)
  414. #endif
  415. {
  416. uint8_t sign=0;
  417. #if ARDUINO >= 100
  418. size_t count=0;
  419. #endif
  420. // Handle negative numbers
  421. if (number < 0.0) {
  422. sign = 1;
  423. number = -number;
  424. }
  425. // Round correctly so that print(1.999, 2) prints as "2.00"
  426. double rounding = 0.5;
  427. for (uint8_t i=0; i<digits; ++i) {
  428. rounding *= 0.1;
  429. }
  430. number += rounding;
  431. // Extract the integer part of the number and print it
  432. unsigned long int_part = (unsigned long)number;
  433. double remainder = number - (double)int_part;
  434. #if ARDUINO >= 100
  435. count += printNumber(int_part, sign, 10);
  436. #else
  437. printNumber(int_part, sign, 10);
  438. #endif
  439. // Print the decimal point, but only if there are digits beyond
  440. if (digits > 0) {
  441. uint8_t n, buf[8], count=1;
  442. buf[0] = '.';
  443. // Extract digits from the remainder one at a time
  444. if (digits > sizeof(buf) - 1) digits = sizeof(buf) - 1;
  445. while (digits-- > 0) {
  446. remainder *= 10.0;
  447. n = (uint8_t)(remainder);
  448. buf[count++] = '0' + n;
  449. remainder -= n;
  450. }
  451. #if ARDUINO >= 100
  452. count += write(buf, count);
  453. #else
  454. write(buf, count);
  455. #endif
  456. }
  457. #if ARDUINO >= 100
  458. return count;
  459. #endif
  460. }