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ů.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484
  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. //#define USE_HACKER_DELIGHT_OPTIMIZATION
  134. #define USE_STIMMER_OPTIMIZATION
  135. //#define USE_BENCHMARK_CODE
  136. #ifdef USE_HACKER_DELIGHT_OPTIMIZATION
  137. // Adapted from Hacker's Delight (Henry Warren, ISBN 0321842685) www.hackersdelight.org
  138. // by Rob Tillaart, Tom Carpenter, "genom2" with input from others...
  139. // http://forum.arduino.cc/index.php?topic=167414.0
  140. //
  141. #define divmod10_asm(in32, tmp32, mod8) \
  142. asm volatile ( \
  143. "mov %2, %A0 \n\t" /* mod = in */ \
  144. "ori %A0, 1 \n\t" /* q = in | 1 */ \
  145. "movw %A1, %A0 \n\t" /* x = q */ \
  146. "movw %C1, %C0 \n\t" \
  147. "lsr %D1 \n\t" /* x = x >> 2 */ \
  148. "ror %C1 \n\t" \
  149. "ror %B1 \n\t" \
  150. "ror %A1 \n\t" \
  151. "lsr %D1 \n\t" \
  152. "ror %C1 \n\t" \
  153. "ror %B1 \n\t" \
  154. "ror %A1 \n\t" \
  155. "sub %A0, %A1 \n\t" /* q = q - x */ \
  156. "sbc %B0, %B1 \n\t" \
  157. "sbc %C0, %C1 \n\t" \
  158. "sbc %D0, %D1 \n\t" \
  159. "movw %A1, %A0 \n\t" /* x = q */ \
  160. "movw %C1, %C0 \n\t" \
  161. "lsr %D1 \n\t" /* x = x >> 4 */ \
  162. "ror %C1 \n\t" \
  163. "ror %B1 \n\t" \
  164. "ror %A1 \n\t" \
  165. "lsr %D1 \n\t" \
  166. "ror %C1 \n\t" \
  167. "ror %B1 \n\t" \
  168. "ror %A1 \n\t" \
  169. "lsr %D1 \n\t" \
  170. "ror %C1 \n\t" \
  171. "ror %B1 \n\t" \
  172. "ror %A1 \n\t" \
  173. "lsr %D1 \n\t" \
  174. "ror %C1 \n\t" \
  175. "ror %B1 \n\t" \
  176. "ror %A1 \n\t" \
  177. "add %A1, %A0 \n\t" /* x = x + q */ \
  178. "adc %B1, %B0 \n\t" \
  179. "adc %C1, %C0 \n\t" \
  180. "adc %D1, %D0 \n\t" \
  181. "movw %A0, %A1 \n\t" /* q = x */ \
  182. "movw %C0, %C1 \n\t" \
  183. "add %A0, %B1 \n\t" /* q = q + (x >> 8) */ \
  184. "adc %B0, %C1 \n\t" \
  185. "adc %C0, %D1 \n\t" \
  186. "adc %D0, r1 \n\t" \
  187. "mov %A0, %B0 \n\t" /* q = q >> 8 */ \
  188. "mov %B0, %C0 \n\t" \
  189. "mov %C0, %D0 \n\t" \
  190. "eor %D0, %D0 \n\t" \
  191. "add %A0, %A1 \n\t" /* q = q + x */ \
  192. "adc %B0, %B1 \n\t" \
  193. "adc %C0, %C1 \n\t" \
  194. "adc %D0, %D1 \n\t" \
  195. "mov %A0, %B0 \n\t" /* q = q >> 8 */ \
  196. "mov %B0, %C0 \n\t" \
  197. "mov %C0, %D0 \n\t" \
  198. "eor %D0, %D0 \n\t" \
  199. "add %A0, %A1 \n\t" /* q = q + x */ \
  200. "adc %B0, %B1 \n\t" \
  201. "adc %C0, %C1 \n\t" \
  202. "adc %D0, %D1 \n\t" \
  203. "mov %A0, %B0 \n\t" /* q = q >> 8 */ \
  204. "mov %B0, %C0 \n\t" \
  205. "mov %C0, %D0 \n\t" \
  206. "eor %D0, %D0 \n\t" \
  207. "add %A0, %A1 \n\t" /* q = q + x */ \
  208. "adc %B0, %B1 \n\t" \
  209. "adc %C0, %C1 \n\t" \
  210. "adc %D0, %D1 \n\t" \
  211. "andi %A0, 0xF8 \n\t" /* q = q & ~0x7 */ \
  212. "sub %2, %A0 \n\t" /* mod = mod - q */ \
  213. "lsr %D0 \n\t" /* q = q >> 2 */ \
  214. "ror %C0 \n\t" \
  215. "ror %B0 \n\t" \
  216. "ror %A0 \n\t" \
  217. "lsr %D0 \n\t" \
  218. "ror %C0 \n\t" \
  219. "ror %B0 \n\t" \
  220. "ror %A0 \n\t" \
  221. "sub %2, %A0 \n\t" /* mod = mod - q */ \
  222. "lsr %D0 \n\t" /* q = q >> 1 */ \
  223. "ror %C0 \n\t" \
  224. "ror %B0 \n\t" \
  225. "ror %A0 \n\t" \
  226. : "+d" (in32), "=r" (tmp32), "=r" (mod8) : : "r0" \
  227. )
  228. #endif // USE_HACKER_DELIGHT_OPTIMIZATION
  229. #ifdef USE_STIMMER_OPTIMIZATION
  230. // http://forum.arduino.cc/index.php?topic=167414.msg1293679#msg1293679
  231. // http://forum.arduino.cc/index.php?topic=167414.msg1309482#msg1309482
  232. // equivelant code:
  233. // mod8 = in32 % 10;
  234. // in32 = in32 / 10;
  235. // tmp8 = 10;
  236. #define divmod10_asm(in32, mod8, tmp8) \
  237. asm volatile ( \
  238. " ldi %2,51 \n\t" \
  239. " mul %A0,%2 \n\t" \
  240. " clr %A0 \n\t" \
  241. " add r0,%2 \n\t" \
  242. " adc %A0,r1 \n\t" \
  243. " mov %1,r0 \n\t" \
  244. " mul %B0,%2 \n\t" \
  245. " clr %B0 \n\t" \
  246. " add %A0,r0 \n\t" \
  247. " adc %B0,r1 \n\t" \
  248. " mul %C0,%2 \n\t" \
  249. " clr %C0 \n\t" \
  250. " add %B0,r0 \n\t" \
  251. " adc %C0,r1 \n\t" \
  252. " mul %D0,%2 \n\t" \
  253. " clr %D0 \n\t" \
  254. " add %C0,r0 \n\t" \
  255. " adc %D0,r1 \n\t" \
  256. " clr r1 \n\t" \
  257. " add %1,%A0 \n\t" \
  258. " adc %A0,%B0 \n\t" \
  259. " adc %B0,%C0 \n\t" \
  260. " adc %C0,%D0 \n\t" \
  261. " adc %D0,r1 \n\t" \
  262. " add %1,%B0 \n\t" \
  263. " adc %A0,%C0 \n\t" \
  264. " adc %B0,%D0 \n\t" \
  265. " adc %C0,r1 \n\t" \
  266. " adc %D0,r1 \n\t" \
  267. " add %1,%D0 \n\t" \
  268. " adc %A0,r1 \n\t" \
  269. " adc %B0,r1 \n\t" \
  270. " adc %C0,r1 \n\t" \
  271. " adc %D0,r1 \n\t" \
  272. " lsr %D0 \n\t" \
  273. " ror %C0 \n\t" \
  274. " ror %B0 \n\t" \
  275. " ror %A0 \n\t" \
  276. " ror %1 \n\t" \
  277. " ldi %2,10 \n\t" \
  278. " mul %1,%2 \n\t" \
  279. " mov %1,r1 \n\t" \
  280. " clr r1 \n\t" \
  281. :"+r"(in32),"=d"(mod8),"=d"(tmp8) : : "r0")
  282. #endif // USE_STIMMER_OPTIMIZATION
  283. #ifdef USE_BENCHMARK_CODE
  284. uint32_t usec_print = 0;
  285. #endif
  286. #if ARDUINO >= 100
  287. size_t Print::printNumberDec(unsigned long n, uint8_t sign)
  288. #else
  289. void Print::printNumberDec(unsigned long n, uint8_t sign)
  290. #endif
  291. {
  292. uint8_t digit, buf[11], *p;
  293. #if defined(USE_HACKER_DELIGHT_OPTIMIZATION)
  294. uint32_t tmp32;
  295. #elif defined(USE_STIMMER_OPTIMIZATION)
  296. uint8_t tmp8;
  297. #endif
  298. #ifdef USE_BENCHMARK_CODE
  299. uint32_t usec = micros();
  300. #endif
  301. p = buf + (sizeof(buf)-1);
  302. do {
  303. #if defined(USE_HACKER_DELIGHT_OPTIMIZATION)
  304. divmod10_asm(n, tmp32, digit);
  305. #elif defined(USE_STIMMER_OPTIMIZATION)
  306. divmod10_asm(n, digit, tmp8);
  307. #else
  308. tmp32 = n;
  309. n = n / 10;
  310. digit = tmp32 - n * 10;
  311. #endif
  312. *--p = digit + '0';
  313. } while (n);
  314. if (sign) *--p = '-';
  315. #ifdef USE_BENCHMARK_CODE
  316. usec_print += micros() - usec;
  317. #endif
  318. #if ARDUINO >= 100
  319. return write(p, sizeof(buf)-1 - (p - buf));
  320. #else
  321. write(p, sizeof(buf)-1 - (p - buf));
  322. #endif
  323. }
  324. #if ARDUINO >= 100
  325. size_t Print::printNumberHex(unsigned long n)
  326. #else
  327. void Print::printNumberHex(unsigned long n)
  328. #endif
  329. {
  330. uint8_t digit, buf[8], *p;
  331. p = buf + (sizeof(buf)-1);
  332. do {
  333. digit = n & 15;
  334. *--p = (digit < 10) ? '0' + digit : 'A' + digit - 10;
  335. n >>= 4;
  336. } while (n);
  337. #if ARDUINO >= 100
  338. return write(p, sizeof(buf)-1 - (p - buf));
  339. #else
  340. write(p, sizeof(buf)-1 - (p - buf));
  341. #endif
  342. }
  343. #if ARDUINO >= 100
  344. size_t Print::printNumberBin(unsigned long n)
  345. #else
  346. void Print::printNumberBin(unsigned long n)
  347. #endif
  348. {
  349. uint8_t buf[32], *p;
  350. p = buf + (sizeof(buf)-1);
  351. do {
  352. *--p = '0' + ((uint8_t)n & 1);
  353. n >>= 1;
  354. } while (n);
  355. #if ARDUINO >= 100
  356. return write(p, sizeof(buf)-1 - (p - buf));
  357. #else
  358. write(p, sizeof(buf)-1 - (p - buf));
  359. #endif
  360. }
  361. #if ARDUINO >= 100
  362. size_t Print::printNumberAny(unsigned long n, uint8_t base)
  363. #else
  364. void Print::printNumberAny(unsigned long n, uint8_t base)
  365. #endif
  366. {
  367. uint8_t digit, buf[21], *p;
  368. uint32_t tmp;
  369. //uint32_t usec;
  370. //usec = micros();
  371. p = buf + (sizeof(buf)-1);
  372. do {
  373. tmp = n;
  374. n = n / base;
  375. digit = tmp - n * base;
  376. *--p = (digit < 10) ? '0' + digit : 'A' + digit - 10;
  377. } while (n);
  378. //usec_print += micros() - usec;
  379. #if ARDUINO >= 100
  380. return write(p, sizeof(buf)-1 - (p - buf));
  381. #else
  382. write(p, sizeof(buf)-1 - (p - buf));
  383. #endif
  384. }
  385. #if ARDUINO >= 100
  386. size_t Print::printFloat(double number, uint8_t digits)
  387. #else
  388. void Print::printFloat(double number, uint8_t digits)
  389. #endif
  390. {
  391. uint8_t sign=0;
  392. #if ARDUINO >= 100
  393. size_t count=0;
  394. #endif
  395. // Handle negative numbers
  396. if (number < 0.0) {
  397. sign = 1;
  398. number = -number;
  399. }
  400. // Round correctly so that print(1.999, 2) prints as "2.00"
  401. double rounding = 0.5;
  402. for (uint8_t i=0; i<digits; ++i) {
  403. rounding *= 0.1;
  404. }
  405. number += rounding;
  406. // Extract the integer part of the number and print it
  407. unsigned long int_part = (unsigned long)number;
  408. double remainder = number - (double)int_part;
  409. #if ARDUINO >= 100
  410. count += printNumber(int_part, sign, 10);
  411. #else
  412. printNumber(int_part, sign, 10);
  413. #endif
  414. // Print the decimal point, but only if there are digits beyond
  415. if (digits > 0) {
  416. uint8_t n, buf[8], count=1;
  417. buf[0] = '.';
  418. // Extract digits from the remainder one at a time
  419. if (digits > sizeof(buf) - 1) digits = sizeof(buf) - 1;
  420. while (digits-- > 0) {
  421. remainder *= 10.0;
  422. n = (uint8_t)(remainder);
  423. buf[count++] = '0' + n;
  424. remainder -= n;
  425. }
  426. #if ARDUINO >= 100
  427. count += write(buf, count);
  428. #else
  429. write(buf, count);
  430. #endif
  431. }
  432. #if ARDUINO >= 100
  433. return count;
  434. #endif
  435. }