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.

90 lines
2.5KB

  1. /**
  2. * Copyright (c) 2011-2019 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. #include <math.h>
  27. size_t PrintBasic::print(long n, uint8_t base) {
  28. if (n < 0 && base == 10) {
  29. return print('-') + printNum(-n, base);
  30. }
  31. return printNum(n, base);
  32. }
  33. size_t PrintBasic::printNum(unsigned long n, uint8_t base) {
  34. const uint8_t DIM = 8*sizeof(long);
  35. char buf[DIM];
  36. char *str = &buf[DIM];
  37. if (base < 2) return 0;
  38. do {
  39. char c = n%base;
  40. n /= base;
  41. *--str = c + (c < 10 ? '0' : 'A' - 10);
  42. } while (n);
  43. return write(str, &buf[DIM] - str);
  44. }
  45. size_t PrintBasic::printDouble(double n, uint8_t prec) {
  46. // Max printable 32-bit floating point number. AVR uses 32-bit double.
  47. const double maxfp = static_cast<double>(0XFFFFFF00UL);
  48. size_t rtn = 0;
  49. if (isnan(n)) {
  50. return write("NaN");
  51. }
  52. if (n < 0) {
  53. n = -n;
  54. rtn += print('-');
  55. }
  56. if (isinf(n)) {
  57. return rtn + write("Inf");
  58. }
  59. if (n > maxfp) {
  60. return rtn + write("Ovf");
  61. }
  62. double round = 0.5;
  63. for (uint8_t i = 0; i < prec; ++i) {
  64. round *= 0.1;
  65. }
  66. n += round;
  67. uint32_t whole = (uint32_t)n;
  68. rtn += print(whole);
  69. if (prec) {
  70. rtn += print('.');
  71. double fraction = n - static_cast<double>(whole);
  72. for (uint8_t i = 0; i < prec; i++) {
  73. fraction *= 10.0;
  74. uint8_t digit = fraction;
  75. rtn += print(digit);
  76. fraction -= digit;
  77. }
  78. }
  79. return rtn;
  80. }