Teensy 4.1 core updated for C++20
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

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