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.

341 lines
8.8KB

  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 <inttypes.h>
  36. #include <math.h>
  37. #include "Print.h"
  38. size_t Print::write(const uint8_t *buffer, size_t size)
  39. {
  40. size_t count = 0;
  41. while (size--) count += write(*buffer++);
  42. return count;
  43. }
  44. size_t Print::print(const String &s)
  45. {
  46. uint8_t buffer[33];
  47. size_t count = 0;
  48. unsigned int index = 0;
  49. unsigned int len = s.length();
  50. while (len > 0) {
  51. s.getBytes(buffer, sizeof(buffer), index);
  52. unsigned int nbytes = len;
  53. if (nbytes > sizeof(buffer)-1) nbytes = sizeof(buffer)-1;
  54. index += nbytes;
  55. len -= nbytes;
  56. count += write(buffer, nbytes);
  57. }
  58. return count;
  59. }
  60. size_t Print::print(long n)
  61. {
  62. uint8_t sign=0;
  63. if (n < 0) {
  64. sign = '-';
  65. n = -n;
  66. }
  67. return printNumber(n, 10, sign);
  68. }
  69. size_t Print::println(void)
  70. {
  71. uint8_t buf[2]={'\r', '\n'};
  72. return write(buf, 2);
  73. }
  74. extern "C" {
  75. __attribute__((weak))
  76. int _write(int file, char *ptr, int len)
  77. {
  78. ((class Print *)file)->write((uint8_t *)ptr, len);
  79. return len;
  80. }
  81. }
  82. int Print::printf(const char *format, ...)
  83. {
  84. va_list ap;
  85. va_start(ap, format);
  86. #ifdef __STRICT_ANSI__
  87. return 0; // TODO: make this work with -std=c++0x
  88. #else
  89. return vdprintf((int)this, format, ap);
  90. #endif
  91. }
  92. int Print::printf(const __FlashStringHelper *format, ...)
  93. {
  94. va_list ap;
  95. va_start(ap, format);
  96. #ifdef __STRICT_ANSI__
  97. return 0;
  98. #else
  99. return vdprintf((int)this, (const char *)format, ap);
  100. #endif
  101. }
  102. #ifdef __MKL26Z64__
  103. // optimized code inspired by Stimmer's optimization
  104. // obviously a dit different, adapted to 32 bit Cortex-M0+
  105. // http://forum.arduino.cc/index.php?topic=167414.msg1293679#msg1293679
  106. // http://forum.arduino.cc/index.php?topic=167414.msg1309482#msg1309482
  107. // equivelant code:
  108. // mod = div % 10;
  109. // div = div / 10;
  110. // tmp1 = {random};
  111. // tmp2 = 10;
  112. #if 1
  113. // https://forum.pjrc.com/threads/28932-LC-is-10-9-times-slower-than-T3-1?p=76072&viewfull=1#post76072
  114. void inline divmod10_v2(uint32_t n,uint32_t *div,uint32_t *mod) {
  115. uint32_t p,q;
  116. /* Using 32.16 fixed point representation p.q */
  117. /* p.q = (n+1)/512 */
  118. q = (n&0xFFFF) + 1;
  119. p = (n>>16);
  120. /* p.q = 51*(n+1)/512 */
  121. q = 13107*q;
  122. p = 13107*p;
  123. /* p.q = (1+1/2^8+1/2^16+1/2^24)*51*(n+1)/512 */
  124. q = q + (q>>16) + (p&0xFFFF);
  125. p = p + (p>>16) + (q>>16);
  126. /* divide by 2 */
  127. p = p>>1;
  128. *div = p;
  129. *mod = n-10*p;
  130. }
  131. #define divmod10_asm(div, mod, tmp1, tmp2, const3333) \
  132. divmod10_v2(div, &div, &mod);
  133. /*
  134. #define divmod10_asm(div, mod, tmp1, tmp2, const3333) \
  135. asm ( \
  136. " lsr %2, %0, #16" "\n\t" \
  137. " mul %2, %4" "\n\t" \
  138. " uxth %1, %0" "\n\t" \
  139. " mul %1, %4" "\n\t" \
  140. " add %1, #1" "\n\t" \
  141. " lsr %0, %2, #16" "\n\t" \
  142. " lsl %2, %2, #16" "\n\t" \
  143. " add %1, %2" "\n\t" \
  144. " mov %3, #0" "\n\t" \
  145. " adc %0, %3" "\n\t" \
  146. " lsl %0, %0, #15" "\n\t" \
  147. " lsr %2, %1, #17" "\n\t" \
  148. " orr %0, %2" "\n\t" \
  149. " lsl %1, %1, #15" "\n\t" \
  150. " lsr %2, %1, #16" "\n\t" \
  151. " lsl %3, %0, #16" "\n\t" \
  152. " orr %2, %3" "\n\t" \
  153. " lsr %3, %0, #16" "\n\t" \
  154. " add %1, %0" "\n\t" \
  155. " adc %0, %1" "\n\t" \
  156. " sub %0, %1" "\n\t" \
  157. " add %1, %2" "\n\t" \
  158. " adc %0, %3" "\n\t" \
  159. " lsr %1, %1, #4" "\n\t" \
  160. " mov %3, #10" "\n\t" \
  161. " mul %1, %3" "\n\t" \
  162. " lsr %1, %1, #28" "\n\t" \
  163. : "+l" (div), \
  164. "=&l" (mod), \
  165. "=&l" (tmp1), \
  166. "=&l" (tmp2) \
  167. : "l" (const3333) \
  168. : \
  169. )
  170. */
  171. #else
  172. #define divmod10_asm(_div, _mod, _tmp1, _tmp2, _const3333) \
  173. ({ _tmp1 = _div; _div = _div / 10; _mod = _tmp1 - _div * 10; })
  174. // ({_mod = _div % 10, _div = _div / 10; })
  175. #endif
  176. size_t Print::printNumberDec(unsigned long n, uint8_t sign)
  177. {
  178. uint8_t buf[11], *p;
  179. uint32_t digit;
  180. //uint32_t t1, t2, c3333=0x3333;
  181. p = buf + (sizeof(buf));
  182. do {
  183. uint32_t div;
  184. divmod10_v2(n, &div, &digit);
  185. n = div;
  186. //divmod10_asm(n, digit, t1, t2, c3333);
  187. *--p = digit + '0';
  188. } while (n);
  189. if (sign) *--p = '-';
  190. return write(p, sizeof(buf) - (p - buf));
  191. }
  192. size_t Print::printNumberHex(unsigned long n)
  193. {
  194. uint8_t digit, buf[8], *p;
  195. p = buf + (sizeof(buf));
  196. do {
  197. digit = n & 15;
  198. *--p = (digit < 10) ? '0' + digit : 'A' + digit - 10;
  199. n >>= 4;
  200. } while (n);
  201. return write(p, sizeof(buf) - (p - buf));
  202. }
  203. size_t Print::printNumberBin(unsigned long n)
  204. {
  205. uint8_t buf[32], *p;
  206. p = buf + (sizeof(buf));
  207. do {
  208. *--p = '0' + ((uint8_t)n & 1);
  209. n >>= 1;
  210. } while (n);
  211. return write(p, sizeof(buf) - (p - buf));
  212. }
  213. size_t Print::printNumberAny(unsigned long n, uint8_t base)
  214. {
  215. uint8_t digit, buf[21], *p;
  216. uint32_t tmp;
  217. p = buf + sizeof(buf);
  218. do {
  219. tmp = n;
  220. n = n / base;
  221. digit = tmp - n * base;
  222. *--p = (digit < 10) ? '0' + digit : 'A' + digit - 10;
  223. } while (n);
  224. return write(p, sizeof(buf) - (p - buf));
  225. }
  226. #else
  227. size_t Print::printNumber(unsigned long n, uint8_t base, uint8_t sign)
  228. {
  229. uint8_t buf[34];
  230. uint8_t digit, i;
  231. // TODO: make these checks as inline, since base is
  232. // almost always a constant. base = 0 (BYTE) should
  233. // inline as a call directly to write()
  234. if (base == 0) {
  235. return write((uint8_t)n);
  236. } else if (base == 1) {
  237. base = 10;
  238. }
  239. if (n == 0) {
  240. buf[sizeof(buf) - 1] = '0';
  241. i = sizeof(buf) - 1;
  242. } else {
  243. i = sizeof(buf) - 1;
  244. while (1) {
  245. digit = n % base;
  246. buf[i] = ((digit < 10) ? '0' + digit : 'A' + digit - 10);
  247. n /= base;
  248. if (n == 0) break;
  249. i--;
  250. }
  251. }
  252. if (sign) {
  253. i--;
  254. buf[i] = '-';
  255. }
  256. return write(buf + i, sizeof(buf) - i);
  257. }
  258. #endif
  259. size_t Print::printFloat(double number, uint8_t digits)
  260. {
  261. uint8_t sign=0;
  262. size_t count=0;
  263. if (isnan(number)) return print("nan");
  264. if (isinf(number)) return print("inf");
  265. if (number > 4294967040.0f) return print("ovf"); // constant determined empirically
  266. if (number <-4294967040.0f) return print("ovf"); // constant determined empirically
  267. // Handle negative numbers
  268. if (number < 0.0) {
  269. sign = 1;
  270. number = -number;
  271. }
  272. // Round correctly so that print(1.999, 2) prints as "2.00"
  273. double rounding = 0.5;
  274. for (uint8_t i=0; i<digits; ++i) {
  275. rounding *= 0.1;
  276. }
  277. number += rounding;
  278. // Extract the integer part of the number and print it
  279. unsigned long int_part = (unsigned long)number;
  280. double remainder = number - (double)int_part;
  281. count += printNumber(int_part, 10, sign);
  282. // Print the decimal point, but only if there are digits beyond
  283. if (digits > 0) {
  284. uint8_t n, buf[16], count=1;
  285. buf[0] = '.';
  286. // Extract digits from the remainder one at a time
  287. if (digits > sizeof(buf) - 1) digits = sizeof(buf) - 1;
  288. while (digits-- > 0) {
  289. remainder *= 10.0;
  290. n = (uint8_t)(remainder);
  291. buf[count++] = '0' + n;
  292. remainder -= n;
  293. }
  294. count += write(buf, count);
  295. }
  296. return count;
  297. }