Streaming.h 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. /*
  2. Streaming.h - Arduino library for supporting the << streaming operator
  3. Copyright (c) 2010-2012 Mikal Hart. All rights 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 ARDUINO_STREAMING
  17. #define ARDUINO_STREAMING
  18. #if defined(ARDUINO) && ARDUINO >= 100
  19. #include "Arduino.h"
  20. #else
  21. #include "WProgram.h"
  22. #endif
  23. #define STREAMING_LIBRARY_VERSION 5
  24. // Generic template
  25. template<class T>
  26. inline Print &operator <<(Print &stream, T arg)
  27. { stream.print(arg); return stream; }
  28. struct _BASED
  29. {
  30. long val;
  31. int base;
  32. _BASED(long v, int b): val(v), base(b)
  33. {}
  34. };
  35. #if ARDUINO >= 100
  36. struct _BYTE_CODE
  37. {
  38. byte val;
  39. _BYTE_CODE(byte v) : val(v)
  40. {}
  41. };
  42. #define _BYTE(a) _BYTE_CODE(a)
  43. inline Print &operator <<(Print &obj, const _BYTE_CODE &arg)
  44. { obj.write(arg.val); return obj; }
  45. #else
  46. #define _BYTE(a) _BASED(a, BYTE)
  47. #endif
  48. #define _HEX(a) _BASED(a, HEX)
  49. #define _DEC(a) _BASED(a, DEC)
  50. #define _OCT(a) _BASED(a, OCT)
  51. #define _BIN(a) _BASED(a, BIN)
  52. // Specialization for class _BASED
  53. // Thanks to Arduino forum user Ben Combee who suggested this
  54. // clever technique to allow for expressions like
  55. // Serial << _HEX(a);
  56. inline Print &operator <<(Print &obj, const _BASED &arg)
  57. { obj.print(arg.val, arg.base); return obj; }
  58. #if ARDUINO >= 18
  59. // Specialization for class _FLOAT
  60. // Thanks to Michael Margolis for suggesting a way
  61. // to accommodate Arduino 0018's floating point precision
  62. // feature like this:
  63. // Serial << _FLOAT(gps_latitude, 6); // 6 digits of precision
  64. struct _FLOAT
  65. {
  66. float val;
  67. int digits;
  68. _FLOAT(double v, int d): val(v), digits(d)
  69. {}
  70. };
  71. inline Print &operator <<(Print &obj, const _FLOAT &arg)
  72. { obj.print(arg.val, arg.digits); return obj; }
  73. #endif
  74. // Specialization for enum _EndLineCode
  75. // Thanks to Arduino forum user Paul V. who suggested this
  76. // clever technique to allow for expressions like
  77. // Serial << "Hello!" << endl;
  78. enum _EndLineCode { endl };
  79. inline Print &operator <<(Print &obj, _EndLineCode arg)
  80. { obj.println(); return obj; }
  81. #endif