// as above but a given skipChar is ignored // this allows format characters (typically commas) in values to be ignored long Stream::parseInt(char skipChar) { boolean isNegative = false; long value = 0; int c; c = peekNextDigit(); // ignore non numeric leading characters if(c < 0) return 0; // zero returned if timeout do{ if(c == skipChar) ; // ignore this charactor else if(c == '-') isNegative = true; else if(c >= '0' && c <= '9') // is c a digit? value = value * 10 + c - '0'; read(); // consume the character we got with peek c = timedPeek(); } while( (c >= '0' && c <= '9') || c == skipChar ); if(isNegative) value = -value; return value; }
// as above but the given skipChar is ignored // this allows format characters (typically commas) in values to be ignored float Stream::parseFloat(char skipChar) { boolean isNegative = false; boolean isFraction = false; long value = 0; int c; float fraction = 1.0; c = peekNextDigit(); // ignore non numeric leading characters if(c < 0) return 0; // zero returned if timeout do { if(c == skipChar) ; // ignore else if(c == '-') isNegative = true; else if(c == '.') isFraction = true; else if(c >= '0' && c <= '9') { // is c a digit? value = value * 10 + c - '0'; if(isFraction) fraction *= 0.1; } read(); // consume the character we got with peek c = timedPeek(); } while((c >= '0' && c <= '9') || c == '.' || c == skipChar); if(isNegative) value = -value; if(isFraction) return value * fraction; else return value; }
// returns peek of the next digit in the stream or -1 if timeout // discards non-numeric characters int Stream::peekNextDigit() { int c; while (1) { c = timedPeek(); if (c < 0) return c; // timeout if (c == '-') return c; if (c >= '0' && c <= '9') return c; read(); // discard non-numeric } }
size_t Adafruit_BLE::readln( char *buffer, size_t length) { size_t len = readBytesUntil('\r', buffer, length); if ( (len > 0) && (len < length) ) { // Add null terminator buffer[len] = 0; // skip \n // if (peek() == '\n' ) read(); if ( timedPeek() == '\n' ) read(); } return len; }
int SUIStream::peekNextDigit(bool includeHex) { int c; while (1) { c = timedPeek(); if (c < 0) return c; // timeout if (c == '-') return c; if (c >= '0' && c <= '9') return c; if (includeHex) { if ( (c >= 'A' && c <= 'F') || (c >= 'a' && c <= 'f')) return c; } read(); // discard non-numeric } return -1; }
unsigned long SUIStream::parseULong(char skipChar) { uint32_t value = 0; int c; c = peekNextDigit(); // ignore non numeric leading characters if (c < 0) return 0; // zero returned if timeout do { if (c == skipChar) ; // ignore this character else if (c >= '0' && c <= '9') // is c a digit? value = value * 10 + c - '0'; read(); // consume the character we got with peek c = timedPeek(); } while ((c >= '0' && c <= '9') || c == skipChar); return value; }