Skip to content

Commit 8b67051

Browse files
committed
add StreamString class (implement the Stream interface for String)
1 parent fd19d90 commit 8b67051

File tree

2 files changed

+108
-0
lines changed

2 files changed

+108
-0
lines changed

cores/esp8266/StreamString.cpp

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/**
2+
StreamString.cpp
3+
4+
Copyright (c) 2015 Markus Sattler. All rights reserved.
5+
This file is part of the esp8266 core for Arduino environment.
6+
7+
This library is free software; you can redistribute it and/or
8+
modify it under the terms of the GNU Lesser General Public
9+
License as published by the Free Software Foundation; either
10+
version 2.1 of the License, or (at your option) any later version.
11+
12+
This library is distributed in the hope that it will be useful,
13+
but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15+
Lesser General Public License for more details.
16+
17+
You should have received a copy of the GNU Lesser General Public
18+
License along with this library; if not, write to the Free Software
19+
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20+
21+
*/
22+
23+
#include <Arduino.h>
24+
#include "StreamString.h"
25+
26+
size_t StreamString::write(const uint8_t *buffer, size_t size) {
27+
if(reserve(length() + size + 1)) {
28+
for(size_t i = 0; i < size; i++) {
29+
if(write(*buffer)) {
30+
buffer++;
31+
} else {
32+
return i;
33+
}
34+
}
35+
36+
}
37+
return 0;
38+
}
39+
40+
size_t StreamString::write(uint8_t data) {
41+
return concat((char) data);
42+
}
43+
44+
int StreamString::available() {
45+
return length();
46+
}
47+
48+
int StreamString::read() {
49+
if(length()) {
50+
char c = charAt(0);
51+
remove(0, 1);
52+
return c;
53+
54+
}
55+
return -1;
56+
}
57+
58+
int StreamString::peek() {
59+
if(length()) {
60+
char c = charAt(0);
61+
return c;
62+
}
63+
return -1;
64+
}
65+
66+
void StreamString::flush() {
67+
}
68+

cores/esp8266/StreamString.h

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/**
2+
StreamString.h
3+
4+
Copyright (c) 2015 Markus Sattler. All rights reserved.
5+
This file is part of the esp8266 core for Arduino environment.
6+
7+
This library is free software; you can redistribute it and/or
8+
modify it under the terms of the GNU Lesser General Public
9+
License as published by the Free Software Foundation; either
10+
version 2.1 of the License, or (at your option) any later version.
11+
12+
This library is distributed in the hope that it will be useful,
13+
but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15+
Lesser General Public License for more details.
16+
17+
You should have received a copy of the GNU Lesser General Public
18+
License along with this library; if not, write to the Free Software
19+
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20+
21+
*/
22+
23+
#ifndef STREAMSTRING_H_
24+
#define STREAMSTRING_H_
25+
26+
27+
class StreamString: public Stream, public String {
28+
29+
size_t write(const uint8_t *buffer, size_t size);
30+
size_t write(uint8_t data);
31+
32+
int available();
33+
int read();
34+
int peek();
35+
void flush();
36+
37+
};
38+
39+
40+
#endif /* STREAMSTRING_H_ */

0 commit comments

Comments
 (0)