EX-Display
EX-Display
Loading...
Searching...
No Matches
Stream.h
Go to the documentation of this file.
1/*
2 * © 2024 Peter Cole
3 *
4 * This is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
8 *
9 * It is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this code. If not, see <https://www.gnu.org/licenses/>.
16 */
17
18#ifndef STREAM_H
19#define STREAM_H
20
21#include <gmock/gmock.h>
22#include <string>
23
25class Stream {
26public:
27 // Output buffer
28 std::string buffer;
29
32 void print(const std::string &string) {
33 buffer += string.c_str(); // Append to buffer
34 }
35
38 void println(const std::string &string) {
39 buffer += string.c_str(); // Append to buffer
40 buffer += "\r\n"; // Add newline
41 }
42
45 int available() const { return buffer.length(); }
46
49 int read() {
50 if (buffer.empty()) {
51 return -1; // Returns -1 if none available
52 }
53 char c = buffer[0];
54 buffer.erase(0, 1); // Get first char and remove it from the buffer
55 return static_cast<int>(c); // Return as int for Arduino Stream compatibility
56 }
57
59 void clear() { buffer.clear(); }
60};
61
62#endif // STREAM_H
Class to mock the basic Stream function equivalent of the Arduino framework.
Definition Stream.h:25
int available() const
Check number of characters available in the buffer.
Definition Stream.h:45
int read()
Read a character from the buffer.
Definition Stream.h:49
void println(const std::string &string)
Println method.
Definition Stream.h:38
void clear()
Clear the buffer.
Definition Stream.h:59
void print(const std::string &string)
Print method.
Definition Stream.h:32
std::string buffer
Definition Stream.h:28