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.

74 line
3.4KB

  1. /*
  2. Stream.h - base class for character-based streams.
  3. Copyright (c) 2010 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 Stream_h
  17. #define Stream_h
  18. #include <inttypes.h>
  19. #include "Print.h"
  20. class Stream : public Print
  21. {
  22. public:
  23. constexpr Stream() : _timeout(1000), read_error(0) {}
  24. virtual int available() = 0;
  25. virtual int read() = 0;
  26. virtual int peek() = 0;
  27. void setTimeout(unsigned long timeout);
  28. bool find(const char *target);
  29. bool find(const uint8_t *target) { return find ((const char *)target); }
  30. bool find(const String &target) { return find(target.c_str()); }
  31. bool find(const char *target, size_t length);
  32. bool find(const uint8_t *target, size_t length) { return find ((const char *)target, length); }
  33. bool find(const String &target, size_t length) { return find(target.c_str(), length); }
  34. bool findUntil(const char *target, const char *terminator);
  35. bool findUntil(const uint8_t *target, const char *terminator) { return findUntil((const char *)target, terminator); }
  36. bool findUntil(const String &target, const char *terminator) { return findUntil(target.c_str(), terminator); }
  37. bool findUntil(const char *target, const String &terminator) { return findUntil(target, terminator.c_str()); }
  38. bool findUntil(const String &target, const String &terminator) { return findUntil(target.c_str(), terminator.c_str()); }
  39. bool findUntil(const char *target, size_t targetLen, const char *terminate, size_t termLen);
  40. bool findUntil(const uint8_t *target, size_t targetLen, const char *terminate, size_t termLen) {return findUntil((const char *)target, targetLen, terminate, termLen); }
  41. bool findUntil(const String &target, size_t targetLen, const char *terminate, size_t termLen);
  42. bool findUntil(const char *target, size_t targetLen, const String &terminate, size_t termLen);
  43. bool findUntil(const String &target, size_t targetLen, const String &terminate, size_t termLen);
  44. long parseInt();
  45. long parseInt(char skipChar);
  46. float parseFloat();
  47. float parseFloat(char skipChar);
  48. size_t readBytes(char *buffer, size_t length);
  49. size_t readBytes(uint8_t *buffer, size_t length) { return readBytes((char *)buffer, length); }
  50. size_t readBytesUntil(char terminator, char *buffer, size_t length);
  51. size_t readBytesUntil(char terminator, uint8_t *buffer, size_t length) { return readBytesUntil(terminator, (char *)buffer, length); }
  52. String readString(size_t max = 120);
  53. String readStringUntil(char terminator, size_t max = 120);
  54. int getReadError() { return read_error; }
  55. void clearReadError() { setReadError(0); }
  56. protected:
  57. void setReadError(int err = 1) { read_error = err; }
  58. unsigned long _timeout;
  59. private:
  60. char read_error;
  61. int timedRead();
  62. int timedPeek();
  63. int peekNextDigit();
  64. };
  65. #endif