My Marlin configs for Fabrikator Mini and CTC i3 Pro B
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.

Stream.h 5.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. parsing functions based on TextFinder library by Michael Margolis
  16. */
  17. #ifndef Stream_h
  18. #define Stream_h
  19. #include <stdint.h>
  20. #include <inttypes.h>
  21. #include "Print.h"
  22. #include "WString.h"
  23. // compatability macros for testing
  24. /*
  25. #define getInt() parseInt()
  26. #define getInt(skipChar) parseInt(skipchar)
  27. #define getFloat() parseFloat()
  28. #define getFloat(skipChar) parseFloat(skipChar)
  29. #define getString( pre_string, post_string, buffer, length)
  30. readBytesBetween( pre_string, terminator, buffer, length)
  31. */
  32. class Stream : public Print
  33. {
  34. protected:
  35. unsigned long _timeout; // number of milliseconds to wait for the next char before aborting timed read
  36. unsigned long _startMillis; // used for timeout measurement
  37. int timedRead(); // private method to read stream with timeout
  38. int timedPeek(); // private method to peek stream with timeout
  39. int peekNextDigit(); // returns the next numeric digit in the stream or -1 if timeout
  40. public:
  41. virtual int available() = 0;
  42. virtual int read() = 0;
  43. virtual int peek() = 0;
  44. virtual void flush() = 0;
  45. Stream() {_timeout=1000;}
  46. // parsing methods
  47. void setTimeout(unsigned long timeout); // sets maximum milliseconds to wait for stream data, default is 1 second
  48. bool find(char *target); // reads data from the stream until the target string is found
  49. bool find(uint8_t *target) { return find ((char *)target); }
  50. // returns true if target string is found, false if timed out (see setTimeout)
  51. bool find(char *target, size_t length); // reads data from the stream until the target string of given length is found
  52. bool find(uint8_t *target, size_t length) { return find ((char *)target, length); }
  53. // returns true if target string is found, false if timed out
  54. bool find(char target) { return find (&target, 1); }
  55. bool findUntil(char *target, char *terminator); // as find but search ends if the terminator string is found
  56. bool findUntil(uint8_t *target, char *terminator) { return findUntil((char *)target, terminator); }
  57. bool findUntil(char *target, size_t targetLen, char *terminate, size_t termLen); // as above but search ends if the terminate string is found
  58. bool findUntil(uint8_t *target, size_t targetLen, char *terminate, size_t termLen) {return findUntil((char *)target, targetLen, terminate, termLen); }
  59. long parseInt(); // returns the first valid (long) integer value from the current position.
  60. // initial characters that are not digits (or the minus sign) are skipped
  61. // integer is terminated by the first character that is not a digit.
  62. float parseFloat(); // float version of parseInt
  63. size_t readBytes( char *buffer, size_t length); // read chars from stream into buffer
  64. size_t readBytes( uint8_t *buffer, size_t length) { return readBytes((char *)buffer, length); }
  65. // terminates if length characters have been read or timeout (see setTimeout)
  66. // returns the number of characters placed in the buffer (0 means no valid data found)
  67. size_t readBytesUntil( char terminator, char *buffer, size_t length); // as readBytes with terminator character
  68. size_t readBytesUntil( char terminator, uint8_t *buffer, size_t length) { return readBytesUntil(terminator, (char *)buffer, length); }
  69. // terminates if length characters have been read, timeout, or if the terminator character detected
  70. // returns the number of characters placed in the buffer (0 means no valid data found)
  71. // Arduino String functions to be added here
  72. String readString();
  73. String readStringUntil(char terminator);
  74. protected:
  75. long parseInt(char skipChar); // as above but the given skipChar is ignored
  76. // as above but the given skipChar is ignored
  77. // this allows format characters (typically commas) in values to be ignored
  78. float parseFloat(char skipChar); // as above but the given skipChar is ignored
  79. struct MultiTarget {
  80. const char *str; // string you're searching for
  81. size_t len; // length of string you're searching for
  82. size_t index; // index used by the search routine.
  83. };
  84. // This allows you to search for an arbitrary number of strings.
  85. // Returns index of the target that is found first or -1 if timeout occurs.
  86. int findMulti(struct MultiTarget *targets, int tCount);
  87. };
  88. #endif