PrintBasic.cpp 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. /**
  2. * Copyright (c) 2011-2020 Bill Greiman
  3. * This file is part of the SdFat library for SD memory cards.
  4. *
  5. * MIT License
  6. *
  7. * Permission is hereby granted, free of charge, to any person obtaining a
  8. * copy of this software and associated documentation files (the "Software"),
  9. * to deal in the Software without restriction, including without limitation
  10. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  11. * and/or sell copies of the Software, and to permit persons to whom the
  12. * Software is furnished to do so, subject to the following conditions:
  13. *
  14. * The above copyright notice and this permission notice shall be included
  15. * in all copies or substantial portions of the Software.
  16. *
  17. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  18. * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  20. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  21. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  22. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  23. * DEALINGS IN THE SOFTWARE.
  24. */
  25. #include "PrintBasic.h"
  26. #if ENABLE_ARDUINO_FEATURES == 0
  27. #include <math.h>
  28. size_t PrintBasic::print(long n, uint8_t base) {
  29. if (n < 0 && base == 10) {
  30. return print('-') + printNum(-n, base);
  31. }
  32. return printNum(n, base);
  33. }
  34. size_t PrintBasic::printNum(unsigned long n, uint8_t base) {
  35. const uint8_t DIM = 8 * sizeof(long);
  36. char buf[DIM];
  37. char *str = &buf[DIM];
  38. if (base < 2) return 0;
  39. do {
  40. char c = n % base;
  41. n /= base;
  42. *--str = c + (c < 10 ? '0' : 'A' - 10);
  43. } while (n);
  44. return write(str, &buf[DIM] - str);
  45. }
  46. size_t PrintBasic::printDouble(double n, uint8_t prec) {
  47. // Max printable 32-bit floating point number. AVR uses 32-bit double.
  48. const double maxfp = static_cast<double>(0XFFFFFF00UL);
  49. size_t rtn = 0;
  50. if (isnan(n)) {
  51. return write("NaN");
  52. }
  53. if (n < 0) {
  54. n = -n;
  55. rtn += print('-');
  56. }
  57. if (isinf(n)) {
  58. return rtn + write("Inf");
  59. }
  60. if (n > maxfp) {
  61. return rtn + write("Ovf");
  62. }
  63. double round = 0.5;
  64. for (uint8_t i = 0; i < prec; ++i) {
  65. round *= 0.1;
  66. }
  67. n += round;
  68. uint32_t whole = (uint32_t)n;
  69. rtn += print(whole);
  70. if (prec) {
  71. rtn += print('.');
  72. double fraction = n - static_cast<double>(whole);
  73. for (uint8_t i = 0; i < prec; i++) {
  74. fraction *= 10.0;
  75. uint8_t digit = fraction;
  76. rtn += print(digit);
  77. fraction -= digit;
  78. }
  79. }
  80. return rtn;
  81. }
  82. #endif // ENABLE_ARDUINO_FEATURES == 0