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 <cstring>
23#include <algorithm>
24#include <deque>
25#if defined(__unix__) || defined(__APPLE__)
26#include <sys/ioctl.h>
27#include <unistd.h>
28#endif
29#include <streambuf>
30#include <cstdarg>
31#include <cstdio>
32#include <string>
33#include "api/Stream.h"
34#include "api/HardwareSerial.h" // SERIAL_8N1 ...
35#include "api/Printable.h"
36
37namespace arduino {
38
55class StdioDevice : public Stream {
56 public:
57 StdioDevice(bool autoFlush = true) { auto_flush = autoFlush; }
58
59 ~StdioDevice() {}
60
61 operator bool() const {
62 return true;
63 } // For classic while(!Serial) { ... } pattern for USB ready wait
64
65 virtual void begin(int speed) {
66 // nothing to be done
67 }
68
71 virtual void begin(unsigned long speed, uint32_t config, int8_t rxPin = -1,
72 int8_t txPin = -1, bool invert = false,
73 unsigned long timeoutMs = 20000UL) {
74 begin(static_cast<int>(speed));
75 }
76
78 __attribute__((format(printf, 2, 3))) size_t printf(const char* format, ...) {
79 char buffer[256];
80 va_list args;
81 va_start(args, format);
82 int len = vsnprintf(buffer, sizeof(buffer), format, args);
83 va_end(args);
84 if (len < 0) return 0;
85 if (len < static_cast<int>(sizeof(buffer))) return print(buffer);
86 // longer output
87 std::string text(len + 1, '\0');
88 va_start(args, format);
89 vsnprintf(&text[0], text.size(), format, args);
90 va_end(args);
91 return print(text.c_str());
92 }
93
94 // Keep all of Print's overloads (double, long, unsigned, char, ...)
95 // visible: declaring print()/println() here would otherwise hide them,
96 // silently routing e.g. print(3.14, 2) to print(int, radix).
97 using Stream::print;
98 using Stream::println;
99
100 virtual size_t print(const char* str) {
101 std::cout << str;
102 if (auto_flush) flush();
103 return strlen(str);
104 }
105
106 virtual size_t println() { return println(""); }
107
108 virtual size_t println(const char* str) {
109 std::cout << str << "\n";
110 if (auto_flush) flush();
111 return strlen(str) + 1;
112 }
113
114 virtual size_t print(int val, int radix = DEC) {
115 size_t result = Stream::print(val, radix);
116 if (auto_flush) flush();
117 return result;
118 }
119
120 virtual size_t println(int val, int radix = DEC) {
121 size_t result = Stream::println(val, radix);
122 if (auto_flush) flush();
123 return result;
124 }
125
126 virtual size_t println(String& str) { return println(str.c_str()); }
127
128 virtual size_t print(String& str) { return print(str.c_str()); }
129
130 virtual size_t println(Printable& p) {
131 size_t result = p.printTo(*this);
132 std::cout << "\n";
133 if (auto_flush) flush();
134 return result + 1;
135 }
136
137 virtual size_t print(Printable& p) {
138 auto result = p.printTo(*this);
139 if (auto_flush) flush();
140 return result;
141 }
142
143 void flush() override { std::cout.flush(); }
144
145 virtual size_t write(const char* str, size_t len) {
146 std::cout.write(str, len);
147 if (auto_flush) flush();
148 return len;
149 }
150
151 virtual size_t write(uint8_t* str, size_t len) {
152 std::cout.write((const char*)str, len);
153 if (auto_flush) flush();
154 return len;
155 }
156 size_t write(const uint8_t* str, size_t len) override {
157 std::cout.write((const char*)str, len);
158 if (auto_flush) flush();
159 return len;
160 }
161
162 virtual size_t write(int32_t value) {
163 std::cout.put(value);
164 if (auto_flush) flush();
165 return 1;
166 }
167
168 size_t write(uint8_t value) override {
169 std::cout.put(value);
170 if (auto_flush) flush();
171 return 1;
172 }
173
174 int available() override {
175#if defined(__unix__) || defined(__APPLE__)
176 fillInputBuffer();
177 return static_cast<int>(inputBuffer_.size());
178#endif
179 return std::cin.rdbuf()->in_avail();
180 }
181
182 int read() override {
183#if defined(__unix__) || defined(__APPLE__)
184 if (available() == 0) return -1;
185 const int value = inputBuffer_.front();
186 inputBuffer_.pop_front();
187 return value;
188#else
189 return std::cin.get();
190#endif
191 }
192
193 int peek() override {
194#if defined(__unix__) || defined(__APPLE__)
195 return available() > 0 ? inputBuffer_.front() : -1;
196#else
197 return std::cin.peek();
198#endif
199 }
200
201 protected:
202 bool auto_flush = true;
203
204#if defined(__unix__) || defined(__APPLE__)
205 private:
206 void fillInputBuffer() {
207 int byteCount = 0;
208 if (ioctl(STDIN_FILENO, FIONREAD, &byteCount) != 0 || byteCount <= 0) {
209 return;
210 }
211
212 char buffer[256];
213 const size_t bytesToRead = std::min(static_cast<size_t>(byteCount), sizeof(buffer));
214 const ssize_t bytesRead = ::read(STDIN_FILENO, buffer, bytesToRead);
215 if (bytesRead <= 0) {
216 return;
217 }
218 inputBuffer_.insert(inputBuffer_.end(), buffer, buffer + bytesRead);
219 }
220
221 std::deque<unsigned char> inputBuffer_;
222#endif
223};
224
225inline StdioDevice Serial;
226#ifndef USE_RPI
227inline StdioDevice Serial2;
228#endif
229
230} // namespace arduino
Provides a Stream interface for standard input/output operations outside the Arduino environment.
Definition StdioDevice.h:55
__attribute__((format(printf, 2, 3))) size_t printf(const char *format
printf (ESP32, RP2040 ...)
virtual void begin(unsigned long speed, uint32_t config, int8_t rxPin=-1, int8_t txPin=-1, bool invert=false, unsigned long timeoutMs=20000UL)
Definition StdioDevice.h:71
We provide the WiFi class to simulate the Arduino WIFI. In in Linux we can expect that networking is ...
Definition CanMsg.cpp:31