Print.h 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. /*
  2. Print.h - Base class that provides print() and println()
  3. Copyright (c) 2008 David A. Mellis. All right reserved.
  4. This library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Lesser General Public
  6. License as published by the Free Software Foundation; either
  7. version 2.1 of the License, or (at your option) any later version.
  8. This library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with this library; if not, write to the Free Software
  14. Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  15. */
  16. #ifndef Print_h
  17. #define Print_h
  18. #include <inttypes.h>
  19. #include <stdio.h> // for size_t
  20. #include "WString.h"
  21. #include "Printable.h"
  22. #define DEC 10
  23. #define HEX 16
  24. #define OCT 8
  25. #define BIN 2
  26. class Print
  27. {
  28. private:
  29. int write_error;
  30. size_t printNumber(unsigned long, uint8_t);
  31. size_t printFloat(double, uint8_t);
  32. protected:
  33. void setWriteError(int err = 1) { write_error = err; }
  34. public:
  35. Print() : write_error(0) {}
  36. int getWriteError() { return write_error; }
  37. void clearWriteError() { setWriteError(0); }
  38. virtual size_t write(uint8_t) = 0;
  39. size_t write(const char *str) {
  40. if (str == NULL) return 0;
  41. return write((const uint8_t *)str, strlen(str));
  42. }
  43. virtual size_t write(const uint8_t *buffer, size_t size);
  44. size_t print(const __FlashStringHelper *);
  45. size_t print(const String &);
  46. size_t print(const char[]);
  47. size_t print(char);
  48. size_t print(unsigned char, int = DEC);
  49. size_t print(int, int = DEC);
  50. size_t print(unsigned int, int = DEC);
  51. size_t print(long, int = DEC);
  52. size_t print(unsigned long, int = DEC);
  53. size_t print(double, int = 2);
  54. size_t print(const Printable&);
  55. size_t println(const __FlashStringHelper *);
  56. size_t println(const String &s);
  57. size_t println(const char[]);
  58. size_t println(char);
  59. size_t println(unsigned char, int = DEC);
  60. size_t println(int, int = DEC);
  61. size_t println(unsigned int, int = DEC);
  62. size_t println(long, int = DEC);
  63. size_t println(unsigned long, int = DEC);
  64. size_t println(double, int = 2);
  65. size_t println(const Printable&);
  66. size_t println(void);
  67. };
  68. #endif