-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStream.cpp
More file actions
33 lines (29 loc) · 791 Bytes
/
Stream.cpp
File metadata and controls
33 lines (29 loc) · 791 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include "Arduino.h"
#include "Stream.h"
#define PARSE_TIMEOUT 1000 // default number of milli-seconds to wait
// Private method to read stream with timeout
int Stream::timedRead()
{
int c;
_startMillis = millis();
do {
c = read();
if (c >= 0) return c;
} while(millis() - _startMillis < _timeout);
return -1; // -1 indicates timeout
}
// Rad characters from stream into buffer,
// terminates if length characters have been read, or timeout (see setTimeout).
// Rturns the number of characters placed in the buffer,
// the buffer is NOT null terminated.
size_t Stream::readBytes(char *buffer, size_t length)
{
size_t count = 0;
while (count < length) {
int c = timedRead();
if (c < 0) break;
*buffer++ = (char)c;
count++;
}
return count;
}