arduino-emulator
Loading...
Searching...
No Matches
StdioDevice.h
1/*
2 StdioDevice.h
3 Copyright (c) 2025 Phil Schatzmann. All right reserved.
4
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
9
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public
16 License along with this library; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18*/
19#pragma once
20
21#include <iostream>
22#include <streambuf>
23#include "api/Stream.h"
24#include "api/Printable.h"
25
26namespace arduino {
27
44class StdioDevice : public Stream {
45 public:
46 StdioDevice(bool autoFlush = true) { auto_flush = autoFlush; }
47
48 ~StdioDevice() {}
49
50 operator bool() const {
51 return true;
52 } // For classic while(!Serial) { ... } pattern for USB ready wait
53
54 virtual void begin(int speed) {
55 // nothing to be done
56 }
57
58 virtual size_t print(const char* str) {
59 std::cout << str;
60 if (auto_flush) flush();
61 return strlen(str);
62 }
63
64 virtual size_t println(const char* str = "") {
65 std::cout << str << "\n";
66 if (auto_flush) flush();
67 return strlen(str) + 1;
68 }
69
70 virtual size_t print(int val, int radix = DEC) {
71 size_t result = Stream::print(val, radix);
72 if (auto_flush) flush();
73 return result;
74 }
75
76 virtual size_t println(int val, int radix = DEC) {
77 size_t result = Stream::println(val, radix);
78 if (auto_flush) flush();
79 return result;
80 }
81
82 virtual size_t println(String& str) { return println(str.c_str()); }
83
84 virtual size_t print(String& str) { return print(str.c_str()); }
85
86 virtual size_t println(Printable& p) {
87 size_t result = p.printTo(*this);
88 std::cout << "\n";
89 if (auto_flush) flush();
90 return result + 1;
91 }
92
93 virtual size_t print(Printable& p) {
94 auto result = p.printTo(*this);
95 if (auto_flush) flush();
96 return result;
97 }
98
99 void flush() override { std::cout.flush(); }
100
101 virtual size_t write(const char* str, size_t len) {
102 std::cout.write(str, len);
103 if (auto_flush) flush();
104 return len;
105 }
106
107 virtual size_t write(uint8_t* str, size_t len) {
108 std::cout.write((const char*)str, len);
109 if (auto_flush) flush();
110 return len;
111 }
112 size_t write(const uint8_t* str, size_t len) override {
113 std::cout.write((const char*)str, len);
114 if (auto_flush) flush();
115 return len;
116 }
117
118 virtual size_t write(int32_t value) {
119 std::cout.put(value);
120 if (auto_flush) flush();
121 return 1;
122 }
123
124 size_t write(uint8_t value) override {
125 std::cout.put(value);
126 if (auto_flush) flush();
127 return 1;
128 }
129
130 int available() override { return std::cin.rdbuf()->in_avail(); };
131
132 int read() override { return std::cin.get(); }
133
134 int peek() override { return std::cin.peek(); }
135
136 protected:
137 bool auto_flush = true;
138};
139
140static StdioDevice Serial;
141#ifndef USE_RPI
142static StdioDevice Serial2;
143#endif
144
145} // namespace arduino
Definition DMAPool.h:103
Definition Printable.h:35
Provides a Stream interface for standard input/output operations outside the Arduino environment.
Definition StdioDevice.h:44
Definition Stream.h:51
Definition String.h:53
We provide the WiFi class to simulate the Arduino WIFI. In in Linux we can expect that networking is ...
Definition CanMsg.cpp:31