Teensy 4.1 core updated for C++20
Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

313 lines
7.5KB

  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. char 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 = str.length();
  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. }
  236. return str;
  237. }
  238. String Stream::readStringUntil(char terminator, size_t max)
  239. {
  240. String str;
  241. size_t length = str.length();
  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. }
  251. return str;
  252. }