|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310 |
-
-
- #include "Arduino.h"
- #include "Stream.h"
-
- #define PARSE_TIMEOUT 1000
- #define NO_SKIP_CHAR 1
-
-
- int Stream::timedRead()
- {
- int c;
- unsigned long startMillis = millis();
- do {
- c = read();
- if (c >= 0) return c;
- } while(millis() - startMillis < _timeout);
- return -1;
- }
-
-
- int Stream::timedPeek()
- {
- int c;
- unsigned long startMillis = millis();
- do {
- c = peek();
- if (c >= 0) return c;
- } while(millis() - startMillis < _timeout);
- return -1;
- }
-
-
-
- int Stream::peekNextDigit()
- {
- int c;
- while (1) {
- c = timedPeek();
- if (c < 0) return c;
- if (c == '-') return c;
- if (c >= '0' && c <= '9') return c;
- read();
- }
- }
-
-
-
-
- void Stream::setTimeout(unsigned long timeout)
- {
- _timeout = timeout;
- }
-
-
- bool Stream::find(const char *target)
- {
- return findUntil(target, NULL);
- }
-
-
-
- bool Stream::find(const char *target, size_t length)
- {
- return findUntil(target, length, NULL, 0);
- }
-
-
- bool Stream::findUntil(const char *target, char *terminator)
- {
- return findUntil(target, strlen(target), terminator, strlen(terminator));
- }
-
-
-
-
- bool Stream::findUntil(const char *target, size_t targetLen, char *terminator, size_t termLen)
- {
- size_t index = 0;
- size_t termIndex = 0;
- int c;
-
- if( *target == 0)
- return true;
- while( (c = timedRead()) > 0){
- if( c == target[index]){
-
- if(++index >= targetLen){
- return true;
- }
- }
- else{
- index = 0;
- }
- if(termLen > 0 && c == terminator[termIndex]){
- if(++termIndex >= termLen)
- return false;
- }
- else
- termIndex = 0;
- }
- return false;
- }
-
-
-
-
-
- long Stream::parseInt()
- {
- return parseInt(NO_SKIP_CHAR);
- }
-
-
-
- long Stream::parseInt(char skipChar)
- {
- boolean isNegative = false;
- long value = 0;
- int c;
-
- c = peekNextDigit();
-
- if(c < 0)
- return 0;
-
- do{
- if(c == skipChar)
- ;
- else if(c == '-')
- isNegative = true;
- else if(c >= '0' && c <= '9')
- value = value * 10 + c - '0';
- read();
- c = timedPeek();
- }
- while( (c >= '0' && c <= '9') || c == skipChar );
-
- if(isNegative)
- value = -value;
- return value;
- }
-
-
-
- float Stream::parseFloat()
- {
- return parseFloat(NO_SKIP_CHAR);
- }
-
-
-
- float Stream::parseFloat(char skipChar){
- boolean isNegative = false;
- boolean isFraction = false;
- long value = 0;
- char c;
- float fraction = 1.0;
-
- c = peekNextDigit();
-
- if(c < 0)
- return 0;
-
- do{
- if(c == skipChar)
- ;
- else if(c == '-')
- isNegative = true;
- else if (c == '.')
- isFraction = true;
- else if(c >= '0' && c <= '9') {
- value = value * 10 + c - '0';
- if(isFraction)
- fraction *= 0.1;
- }
- read();
- c = timedPeek();
- }
- while( (c >= '0' && c <= '9') || c == '.' || c == skipChar );
-
- if(isNegative)
- value = -value;
- if(isFraction)
- return value * fraction;
- else
- return value;
- }
-
-
-
-
-
-
- size_t Stream::readBytes(char *buffer, size_t length)
- {
- size_t count = 0;
- while (count < length) {
- int c = timedRead();
- if (c < 0) {
- setReadError();
- break;
- }
- *buffer++ = (char)c;
- count++;
- }
- return count;
- }
-
-
-
-
-
-
- size_t Stream::readBytesUntil(char terminator, char *buffer, size_t length)
- {
- if (length < 1) return 0;
- length--;
- size_t index = 0;
- while (index < length) {
- int c = timedRead();
- if (c == terminator) break;
- if (c < 0) {
- setReadError();
- break;
- }
- *buffer++ = (char)c;
- index++;
- }
- *buffer = 0;
- return index;
- }
-
- String Stream::readString(size_t max)
- {
- String str;
- size_t length = str.length();
- while (length < max) {
- int c = timedRead();
- if (c < 0) {
- setReadError();
- break;
- }
- if (c == 0) break;
- str += (char)c;
- }
- return str;
- }
-
- String Stream::readStringUntil(char terminator, size_t max)
- {
- String str;
- size_t length = str.length();
- while (length < max) {
- int c = timedRead();
- if (c < 0) {
- setReadError();
- break;
- }
- if (c == 0 || c == terminator) break;
- str += (char)c;
- }
- return str;
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
|