Teensy 4.1 core updated for C++20
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.

201 lines
4.9KB

  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. #include "debug/printf.h"
  37. #undef printf
  38. size_t Print::write(const uint8_t *buffer, size_t size)
  39. {
  40. if (buffer == nullptr) return 0;
  41. size_t count = 0;
  42. while (size--) count += write(*buffer++);
  43. return count;
  44. }
  45. size_t Print::print(const String &s)
  46. {
  47. uint8_t buffer[33];
  48. size_t count = 0;
  49. unsigned int index = 0;
  50. unsigned int len = s.length();
  51. while (len > 0) {
  52. s.getBytes(buffer, sizeof(buffer), index);
  53. unsigned int nbytes = len;
  54. if (nbytes > sizeof(buffer)-1) nbytes = sizeof(buffer)-1;
  55. index += nbytes;
  56. len -= nbytes;
  57. count += write(buffer, nbytes);
  58. }
  59. return count;
  60. }
  61. size_t Print::print(long n)
  62. {
  63. uint8_t sign=0;
  64. if (n < 0) {
  65. sign = '-';
  66. n = -n;
  67. }
  68. return printNumber(n, 10, sign);
  69. }
  70. size_t Print::println(void)
  71. {
  72. uint8_t buf[2]={'\r', '\n'};
  73. return write(buf, 2);
  74. }
  75. extern "C" {
  76. __attribute__((weak))
  77. int _write(int file, char *ptr, int len)
  78. {
  79. ((class Print *)file)->write((uint8_t *)ptr, len);
  80. return len;
  81. }
  82. }
  83. int Print::printf(const char *format, ...)
  84. {
  85. va_list ap;
  86. va_start(ap, format);
  87. #ifdef __STRICT_ANSI__
  88. return 0; // TODO: make this work with -std=c++0x
  89. #else
  90. return vdprintf((int)this, format, ap);
  91. #endif
  92. }
  93. int Print::printf(const __FlashStringHelper *format, ...)
  94. {
  95. va_list ap;
  96. va_start(ap, format);
  97. #ifdef __STRICT_ANSI__
  98. return 0;
  99. #else
  100. return vdprintf((int)this, (const char *)format, ap);
  101. #endif
  102. }
  103. size_t Print::printNumber(unsigned long n, uint8_t base, uint8_t sign)
  104. {
  105. uint8_t buf[34];
  106. uint8_t digit, i;
  107. // TODO: make these checks as inline, since base is
  108. // almost always a constant. base = 0 (BYTE) should
  109. // inline as a call directly to write()
  110. if (base == 0) {
  111. return write((uint8_t)n);
  112. } else if (base == 1) {
  113. base = 10;
  114. }
  115. if (n == 0) {
  116. buf[sizeof(buf) - 1] = '0';
  117. i = sizeof(buf) - 1;
  118. } else {
  119. i = sizeof(buf) - 1;
  120. while (1) {
  121. digit = n % base;
  122. buf[i] = ((digit < 10) ? '0' + digit : 'A' + digit - 10);
  123. n /= base;
  124. if (n == 0) break;
  125. i--;
  126. }
  127. }
  128. if (sign) {
  129. i--;
  130. buf[i] = '-';
  131. }
  132. return write(buf + i, sizeof(buf) - i);
  133. }
  134. size_t Print::printFloat(double number, uint8_t digits)
  135. {
  136. uint8_t sign=0;
  137. size_t count=0;
  138. if (isnan(number)) return print("nan");
  139. if (isinf(number)) return print("inf");
  140. if (number > 4294967040.0f) return print("ovf"); // constant determined empirically
  141. if (number <-4294967040.0f) return print("ovf"); // constant determined empirically
  142. // Handle negative numbers
  143. if (number < 0.0) {
  144. sign = 1;
  145. number = -number;
  146. }
  147. // Round correctly so that print(1.999, 2) prints as "2.00"
  148. double rounding = 0.5;
  149. for (uint8_t i=0; i<digits; ++i) {
  150. rounding *= 0.1;
  151. }
  152. number += rounding;
  153. // Extract the integer part of the number and print it
  154. unsigned long int_part = (unsigned long)number;
  155. double remainder = number - (double)int_part;
  156. count += printNumber(int_part, 10, sign);
  157. // Print the decimal point, but only if there are digits beyond
  158. if (digits > 0) {
  159. uint8_t n, buf[16], count=1;
  160. buf[0] = '.';
  161. // Extract digits from the remainder one at a time
  162. if (digits > sizeof(buf) - 1) digits = sizeof(buf) - 1;
  163. while (digits-- > 0) {
  164. remainder *= 10.0;
  165. n = (uint8_t)(remainder);
  166. buf[count++] = '0' + n;
  167. remainder -= n;
  168. }
  169. count += write(buf, count);
  170. }
  171. return count;
  172. }