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.

337 line
8.7KB

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