Teensy 4.1 core updated for C++20
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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. /*
  2. Stream.cpp - adds parsing methods to Stream class
  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. Created July 2011
  16. parsing functions based on TextFinder library by Michael Margolis
  17. */
  18. #include <Arduino.h>
  19. #define PARSE_TIMEOUT 1000 // default number of milli-seconds to wait
  20. #define NO_SKIP_CHAR 1 // a magic char not found in a valid ASCII numeric field
  21. // private method to read stream with timeout
  22. int Stream::timedRead()
  23. {
  24. int c;
  25. unsigned long startMillis = millis();
  26. do {
  27. c = read();
  28. if (c >= 0) return c;
  29. yield();
  30. } while(millis() - startMillis < _timeout);
  31. return -1; // -1 indicates timeout
  32. }
  33. // private method to peek stream with timeout
  34. int Stream::timedPeek()
  35. {
  36. int c;
  37. unsigned long startMillis = millis();
  38. do {
  39. c = peek();
  40. if (c >= 0) return c;
  41. yield();
  42. } while(millis() - startMillis < _timeout);
  43. return -1; // -1 indicates timeout
  44. }
  45. // returns peek of the next digit in the stream or -1 if timeout
  46. // discards non-numeric characters
  47. int Stream::peekNextDigit()
  48. {
  49. int c;
  50. while (1) {
  51. c = timedPeek();
  52. if (c < 0) return c; // timeout
  53. if (c == '-') return c;
  54. if (c >= '0' && c <= '9') return c;
  55. read(); // discard non-numeric
  56. }
  57. }
  58. // Public Methods
  59. //////////////////////////////////////////////////////////////
  60. void Stream::setTimeout(unsigned long timeout) // sets the maximum number of milliseconds to wait
  61. {
  62. _timeout = timeout;
  63. }
  64. // find returns true if the target string is found
  65. bool Stream::find(const char *target)
  66. {
  67. return findUntil(target, NULL);
  68. }
  69. // reads data from the stream until the target string of given length is found
  70. // returns true if target string is found, false if timed out
  71. bool Stream::find(const char *target, size_t length)
  72. {
  73. return findUntil(target, length, NULL, 0);
  74. }
  75. // as find but search ends if the terminator string is found
  76. bool Stream::findUntil(const char *target, const char *terminator)
  77. {
  78. return findUntil(target, strlen(target), terminator, strlen(terminator));
  79. }
  80. // reads data from the stream until the target string of the given length is found
  81. // search terminated if the terminator string is found
  82. // returns true if target string is found, false if terminated or timed out
  83. bool Stream::findUntil(const char *target, size_t targetLen, const char *terminator, size_t termLen)
  84. {
  85. size_t index = 0; // maximum target string length is 64k bytes!
  86. size_t termIndex = 0;
  87. int c;
  88. if( *target == 0)
  89. return true; // return true if target is a null string
  90. while( (c = timedRead()) > 0){
  91. if( c == target[index]){
  92. //////Serial.print("found "); Serial.write(c); Serial.print("index now"); Serial.println(index+1);
  93. if(++index >= targetLen){ // return true if all chars in the target match
  94. return true;
  95. }
  96. }
  97. else{
  98. index = 0; // reset index if any char does not match
  99. }
  100. if(termLen > 0 && c == terminator[termIndex]){
  101. if(++termIndex >= termLen)
  102. return false; // return false if terminate string found before target string
  103. }
  104. else
  105. termIndex = 0;
  106. }
  107. return false;
  108. }
  109. // returns the first valid (long) integer value from the current position.
  110. // initial characters that are not digits (or the minus sign) are skipped
  111. // function is terminated by the first character that is not a digit.
  112. long Stream::parseInt()
  113. {
  114. return parseInt(NO_SKIP_CHAR); // terminate on first non-digit character (or timeout)
  115. }
  116. // as above but a given skipChar is ignored
  117. // this allows format characters (typically commas) in values to be ignored
  118. long Stream::parseInt(char skipChar)
  119. {
  120. boolean isNegative = false;
  121. long value = 0;
  122. int c;
  123. c = peekNextDigit();
  124. // ignore non numeric leading characters
  125. if(c < 0)
  126. return 0; // zero returned if timeout
  127. do{
  128. if(c == skipChar)
  129. ; // ignore this charactor
  130. else if(c == '-')
  131. isNegative = true;
  132. else if(c >= '0' && c <= '9') // is c a digit?
  133. value = value * 10 + c - '0';
  134. read(); // consume the character we got with peek
  135. c = timedPeek();
  136. }
  137. while( (c >= '0' && c <= '9') || c == skipChar );
  138. if(isNegative)
  139. value = -value;
  140. return value;
  141. }
  142. // as parseInt but returns a floating point value
  143. float Stream::parseFloat()
  144. {
  145. return parseFloat(NO_SKIP_CHAR);
  146. }
  147. // as above but the given skipChar is ignored
  148. // this allows format characters (typically commas) in values to be ignored
  149. float Stream::parseFloat(char skipChar){
  150. boolean isNegative = false;
  151. boolean isFraction = false;
  152. long value = 0;
  153. int c;
  154. float fraction = 1.0;
  155. c = peekNextDigit();
  156. // ignore non numeric leading characters
  157. if(c < 0)
  158. return 0; // zero returned if timeout
  159. do{
  160. if(c == skipChar)
  161. ; // ignore
  162. else if(c == '-')
  163. isNegative = true;
  164. else if (c == '.')
  165. isFraction = true;
  166. else if(c >= '0' && c <= '9') { // is c a digit?
  167. value = value * 10 + c - '0';
  168. if(isFraction)
  169. fraction *= 0.1;
  170. }
  171. read(); // consume the character we got with peek
  172. c = timedPeek();
  173. }
  174. while( (c >= '0' && c <= '9') || c == '.' || c == skipChar );
  175. if(isNegative)
  176. value = -value;
  177. if(isFraction)
  178. return value * fraction;
  179. else
  180. return value;
  181. }
  182. // read characters from stream into buffer
  183. // terminates if length characters have been read, or timeout (see setTimeout)
  184. // returns the number of characters placed in the buffer
  185. // the buffer is NOT null terminated.
  186. //
  187. size_t Stream::readBytes(char *buffer, size_t length)
  188. {
  189. size_t count = 0;
  190. while (count < length) {
  191. int c = timedRead();
  192. if (c < 0) {
  193. setReadError();
  194. break;
  195. }
  196. *buffer++ = (char)c;
  197. count++;
  198. }
  199. return count;
  200. }
  201. // as readBytes with terminator character
  202. // terminates if length characters have been read, timeout, or if the terminator character detected
  203. // returns the number of characters placed in the buffer (0 means no valid data found)
  204. size_t Stream::readBytesUntil(char terminator, char *buffer, size_t length)
  205. {
  206. if (length < 1) return 0;
  207. length--;
  208. size_t index = 0;
  209. while (index < length) {
  210. int c = timedRead();
  211. if (c == terminator) break;
  212. if (c < 0) {
  213. setReadError();
  214. break;
  215. }
  216. *buffer++ = (char)c;
  217. index++;
  218. }
  219. *buffer = 0;
  220. return index; // return number of characters, not including null terminator
  221. }
  222. String Stream::readString(size_t max)
  223. {
  224. String str;
  225. size_t length = 0;
  226. while (length < max) {
  227. int c = timedRead();
  228. if (c < 0) {
  229. setReadError();
  230. break; // timeout
  231. }
  232. if (c == 0) break;
  233. str += (char)c;
  234. length++;
  235. }
  236. return str;
  237. }
  238. String Stream::readStringUntil(char terminator, size_t max)
  239. {
  240. String str;
  241. size_t length = 0;
  242. while (length < max) {
  243. int c = timedRead();
  244. if (c < 0) {
  245. setReadError();
  246. break; // timeout
  247. }
  248. if (c == 0 || c == terminator) break;
  249. str += (char)c;
  250. length++;
  251. }
  252. return str;
  253. }