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.

Print.cpp 9.3KB

5 yıl önce
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  1. /* Teensyduino Core Library
  2. * http://www.pjrc.com/teensy/
  3. * Copyright (c) 2017 PJRC.COM, LLC.
  4. *
  5. * Permission is hereby granted, free of charge, to any person obtaining
  6. * a copy of this software and associated documentation files (the
  7. * "Software"), to deal in the Software without restriction, including
  8. * without limitation the rights to use, copy, modify, merge, publish,
  9. * distribute, sublicense, and/or sell copies of the Software, and to
  10. * permit persons to whom the Software is furnished to do so, subject to
  11. * the following conditions:
  12. *
  13. * 1. The above copyright notice and this permission notice shall be
  14. * included in all copies or substantial portions of the Software.
  15. *
  16. * 2. If the Software is incorporated into a build system that allows
  17. * selection among a list of target devices, then similar target
  18. * devices manufactured by PJRC.COM must be included in the list of
  19. * target devices and selectable in the same manner.
  20. *
  21. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  22. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  23. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  24. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  25. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  26. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  27. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  28. * SOFTWARE.
  29. */
  30. // Long ago this file contained code from Arduino.cc, which was
  31. // Copyright (c) 2008 David A. Mellis. No substantial portion of
  32. // Arduino's original code remains. In fact, several improvements
  33. // developed for Teensyduino have made their way back into
  34. // Arduino's code base. :-)
  35. #include <Arduino.h>
  36. size_t Print::write(const uint8_t *buffer, size_t size)
  37. {
  38. if (buffer == nullptr) return 0;
  39. size_t count = 0;
  40. while (size--) count += write(*buffer++);
  41. return count;
  42. }
  43. size_t Print::print(const String &s)
  44. {
  45. uint8_t buffer[33];
  46. size_t count = 0;
  47. unsigned int index = 0;
  48. unsigned int len = s.length();
  49. while (len > 0) {
  50. s.getBytes(buffer, sizeof(buffer), index);
  51. unsigned int nbytes = len;
  52. if (nbytes > sizeof(buffer)-1) nbytes = sizeof(buffer)-1;
  53. index += nbytes;
  54. len -= nbytes;
  55. count += write(buffer, nbytes);
  56. }
  57. return count;
  58. }
  59. size_t Print::print(long n)
  60. {
  61. uint8_t sign=0;
  62. if (n < 0) {
  63. sign = '-';
  64. n = -n;
  65. }
  66. return printNumber(n, 10, sign);
  67. }
  68. size_t Print::println(void)
  69. {
  70. uint8_t buf[2]={'\r', '\n'};
  71. return write(buf, 2);
  72. }
  73. extern "C" {
  74. __attribute__((weak))
  75. int _write(int file, char *ptr, int len)
  76. {
  77. ((class Print *)file)->write((uint8_t *)ptr, len);
  78. return len;
  79. }
  80. }
  81. int Print::printf(const char *format, ...)
  82. {
  83. va_list ap;
  84. va_start(ap, format);
  85. #ifdef __STRICT_ANSI__
  86. return 0; // TODO: make this work with -std=c++0x
  87. #else
  88. return vdprintf((int)this, format, ap);
  89. #endif
  90. }
  91. int Print::printf(const __FlashStringHelper *format, ...)
  92. {
  93. va_list ap;
  94. va_start(ap, format);
  95. #ifdef __STRICT_ANSI__
  96. return 0;
  97. #else
  98. return vdprintf((int)this, (const char *)format, ap);
  99. #endif
  100. }
  101. #ifdef __MKL26Z64__
  102. // optimized code inspired by Stimmer's optimization
  103. // obviously a dit different, adapted to 32 bit Cortex-M0+
  104. // http://forum.arduino.cc/index.php?topic=167414.msg1293679#msg1293679
  105. // http://forum.arduino.cc/index.php?topic=167414.msg1309482#msg1309482
  106. // equivelant code:
  107. // mod = div % 10;
  108. // div = div / 10;
  109. // tmp1 = {random};
  110. // tmp2 = 10;
  111. #if 1
  112. // https://forum.pjrc.com/threads/28932-LC-is-10-9-times-slower-than-T3-1?p=76072&viewfull=1#post76072
  113. void inline divmod10_v2(uint32_t n,uint32_t *div,uint32_t *mod) {
  114. uint32_t p,q;
  115. /* Using 32.16 fixed point representation p.q */
  116. /* p.q = (n+1)/512 */
  117. q = (n&0xFFFF) + 1;
  118. p = (n>>16);
  119. /* p.q = 51*(n+1)/512 */
  120. q = 13107*q;
  121. p = 13107*p;
  122. /* p.q = (1+1/2^8+1/2^16+1/2^24)*51*(n+1)/512 */
  123. q = q + (q>>16) + (p&0xFFFF);
  124. p = p + (p>>16) + (q>>16);
  125. /* divide by 2 */
  126. p = p>>1;
  127. *div = p;
  128. *mod = n-10*p;
  129. }
  130. #define divmod10_asm(div, mod, tmp1, tmp2, const3333) \
  131. divmod10_v2(div, &div, &mod);
  132. /*
  133. #define divmod10_asm(div, mod, tmp1, tmp2, const3333) \
  134. asm ( \
  135. " lsr %2, %0, #16" "\n\t" \
  136. " mul %2, %4" "\n\t" \
  137. " uxth %1, %0" "\n\t" \
  138. " mul %1, %4" "\n\t" \
  139. " add %1, #1" "\n\t" \
  140. " lsr %0, %2, #16" "\n\t" \
  141. " lsl %2, %2, #16" "\n\t" \
  142. " add %1, %2" "\n\t" \
  143. " mov %3, #0" "\n\t" \
  144. " adc %0, %3" "\n\t" \
  145. " lsl %0, %0, #15" "\n\t" \
  146. " lsr %2, %1, #17" "\n\t" \
  147. " orr %0, %2" "\n\t" \
  148. " lsl %1, %1, #15" "\n\t" \
  149. " lsr %2, %1, #16" "\n\t" \
  150. " lsl %3, %0, #16" "\n\t" \
  151. " orr %2, %3" "\n\t" \
  152. " lsr %3, %0, #16" "\n\t" \
  153. " add %1, %0" "\n\t" \
  154. " adc %0, %1" "\n\t" \
  155. " sub %0, %1" "\n\t" \
  156. " add %1, %2" "\n\t" \
  157. " adc %0, %3" "\n\t" \
  158. " lsr %1, %1, #4" "\n\t" \
  159. " mov %3, #10" "\n\t" \
  160. " mul %1, %3" "\n\t" \
  161. " lsr %1, %1, #28" "\n\t" \
  162. : "+l" (div), \
  163. "=&l" (mod), \
  164. "=&l" (tmp1), \
  165. "=&l" (tmp2) \
  166. : "l" (const3333) \
  167. : \
  168. )
  169. */
  170. #else
  171. #define divmod10_asm(_div, _mod, _tmp1, _tmp2, _const3333) \
  172. ({ _tmp1 = _div; _div = _div / 10; _mod = _tmp1 - _div * 10; })
  173. // ({_mod = _div % 10, _div = _div / 10; })
  174. #endif
  175. size_t Print::printNumberDec(unsigned long n, uint8_t sign)
  176. {
  177. uint8_t buf[11], *p;
  178. uint32_t digit;
  179. //uint32_t t1, t2, c3333=0x3333;
  180. p = buf + (sizeof(buf));
  181. do {
  182. uint32_t div;
  183. divmod10_v2(n, &div, &digit);
  184. n = div;
  185. //divmod10_asm(n, digit, t1, t2, c3333);
  186. *--p = digit + '0';
  187. } while (n);
  188. if (sign) *--p = '-';
  189. return write(p, sizeof(buf) - (p - buf));
  190. }
  191. size_t Print::printNumberHex(unsigned long n)
  192. {
  193. uint8_t digit, buf[8], *p;
  194. p = buf + (sizeof(buf));
  195. do {
  196. digit = n & 15;
  197. *--p = (digit < 10) ? '0' + digit : 'A' + digit - 10;
  198. n >>= 4;
  199. } while (n);
  200. return write(p, sizeof(buf) - (p - buf));
  201. }
  202. size_t Print::printNumberBin(unsigned long n)
  203. {
  204. uint8_t buf[32], *p;
  205. p = buf + (sizeof(buf));
  206. do {
  207. *--p = '0' + ((uint8_t)n & 1);
  208. n >>= 1;
  209. } while (n);
  210. return write(p, sizeof(buf) - (p - buf));
  211. }
  212. size_t Print::printNumberAny(unsigned long n, uint8_t base)
  213. {
  214. uint8_t digit, buf[21], *p;
  215. uint32_t tmp;
  216. p = buf + sizeof(buf);
  217. do {
  218. tmp = n;
  219. n = n / base;
  220. digit = tmp - n * base;
  221. *--p = (digit < 10) ? '0' + digit : 'A' + digit - 10;
  222. } while (n);
  223. return write(p, sizeof(buf) - (p - buf));
  224. }
  225. #else
  226. size_t Print::printNumber(unsigned long n, uint8_t base, uint8_t sign)
  227. {
  228. uint8_t buf[34];
  229. uint8_t digit, i;
  230. // TODO: make these checks as inline, since base is
  231. // almost always a constant. base = 0 (BYTE) should
  232. // inline as a call directly to write()
  233. if (base == 0) {
  234. return write((uint8_t)n);
  235. } else if (base == 1) {
  236. base = 10;
  237. }
  238. if (n == 0) {
  239. buf[sizeof(buf) - 1] = '0';
  240. i = sizeof(buf) - 1;
  241. } else {
  242. i = sizeof(buf) - 1;
  243. while (1) {
  244. digit = n % base;
  245. buf[i] = ((digit < 10) ? '0' + digit : 'A' + digit - 10);
  246. n /= base;
  247. if (n == 0) break;
  248. i--;
  249. }
  250. }
  251. if (sign) {
  252. i--;
  253. buf[i] = '-';
  254. }
  255. return write(buf + i, sizeof(buf) - i);
  256. }
  257. #endif
  258. size_t Print::print(int64_t n)
  259. {
  260. if (n < 0) return printNumber64(-n, 10, 1);
  261. return printNumber64(n, 10, 0);
  262. }
  263. size_t Print::printNumber64(uint64_t n, uint8_t base, uint8_t sign)
  264. {
  265. uint8_t buf[66];
  266. uint8_t digit, i;
  267. if (base < 2) return 0;
  268. if (n == 0) {
  269. buf[sizeof(buf) - 1] = '0';
  270. i = sizeof(buf) - 1;
  271. } else {
  272. i = sizeof(buf) - 1;
  273. while (1) {
  274. digit = n % base;
  275. buf[i] = ((digit < 10) ? '0' + digit : 'A' + digit - 10);
  276. n /= base;
  277. if (n == 0) break;
  278. i--;
  279. }
  280. }
  281. if (sign) {
  282. i--;
  283. buf[i] = '-';
  284. }
  285. return write(buf + i, sizeof(buf) - i);
  286. }
  287. size_t Print::printFloat(double number, uint8_t digits)
  288. {
  289. uint8_t sign=0;
  290. size_t count=0;
  291. if (isnan(number)) return print("nan");
  292. if (isinf(number)) return print("inf");
  293. if (number > 4294967040.0f) return print("ovf"); // constant determined empirically
  294. if (number <-4294967040.0f) return print("ovf"); // constant determined empirically
  295. // Handle negative numbers
  296. if (number < 0.0) {
  297. sign = 1;
  298. number = -number;
  299. }
  300. // Round correctly so that print(1.999, 2) prints as "2.00"
  301. double rounding = 0.5;
  302. for (uint8_t i=0; i<digits; ++i) {
  303. rounding *= 0.1;
  304. }
  305. number += rounding;
  306. // Extract the integer part of the number and print it
  307. unsigned long int_part = (unsigned long)number;
  308. double remainder = number - (double)int_part;
  309. count += printNumber(int_part, 10, sign);
  310. // Print the decimal point, but only if there are digits beyond
  311. if (digits > 0) {
  312. uint8_t n, buf[16], count=1;
  313. buf[0] = '.';
  314. // Extract digits from the remainder one at a time
  315. if (digits > sizeof(buf) - 1) digits = sizeof(buf) - 1;
  316. while (digits-- > 0) {
  317. remainder *= 10.0;
  318. n = (uint8_t)(remainder);
  319. buf[count++] = '0' + n;
  320. remainder -= n;
  321. }
  322. count += write(buf, count);
  323. }
  324. return count;
  325. }