Smart-Home am Beispiel der Präsenzerkennung im Raum Projektarbeit Lennart Heimbs, Johannes Krug, Sebastian Dohle und Kevin Holzschuh bei Prof. Oliver Hofmann SS2019
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.cpp 7.2KB

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