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 "api/Stream.h"
31#include "api/Printable.h"
32
33namespace arduino {
34
51class StdioDevice : public Stream {
52 public:
53 StdioDevice(bool autoFlush = true) { auto_flush = autoFlush; }
54
55 ~StdioDevice() {}
56
57 operator bool() const {
58 return true;
59 } // For classic while(!Serial) { ... } pattern for USB ready wait
60
61 virtual void begin(int speed) {
62 // nothing to be done
63 }
64
65 virtual size_t print(const char* str) {
66 std::cout << str;
67 if (auto_flush) flush();
68 return strlen(str);
69 }
70
71 virtual size_t println(const char* str = "") {
72 std::cout << str << "\n";
73 if (auto_flush) flush();
74 return strlen(str) + 1;
75 }
76
77 virtual size_t print(int val, int radix = DEC) {
78 size_t result = Stream::print(val, radix);
79 if (auto_flush) flush();
80 return result;
81 }
82
83 virtual size_t println(int val, int radix = DEC) {
84 size_t result = Stream::println(val, radix);
85 if (auto_flush) flush();
86 return result;
87 }
88
89 virtual size_t println(String& str) { return println(str.c_str()); }
90
91 virtual size_t print(String& str) { return print(str.c_str()); }
92
93 virtual size_t println(Printable& p) {
94 size_t result = p.printTo(*this);
95 std::cout << "\n";
96 if (auto_flush) flush();
97 return result + 1;
98 }
99
100 virtual size_t print(Printable& p) {
101 auto result = p.printTo(*this);
102 if (auto_flush) flush();
103 return result;
104 }
105
106 void flush() override { std::cout.flush(); }
107
108 virtual size_t write(const char* str, size_t len) {
109 std::cout.write(str, len);
110 if (auto_flush) flush();
111 return len;
112 }
113
114 virtual size_t write(uint8_t* str, size_t len) {
115 std::cout.write((const char*)str, len);
116 if (auto_flush) flush();
117 return len;
118 }
119 size_t write(const uint8_t* str, size_t len) override {
120 std::cout.write((const char*)str, len);
121 if (auto_flush) flush();
122 return len;
123 }
124
125 virtual size_t write(int32_t value) {
126 std::cout.put(value);
127 if (auto_flush) flush();
128 return 1;
129 }
130
131 size_t write(uint8_t value) override {
132 std::cout.put(value);
133 if (auto_flush) flush();
134 return 1;
135 }
136
137 int available() override {
138#if defined(__unix__) || defined(__APPLE__)
139 fillInputBuffer();
140 return static_cast<int>(inputBuffer_.size());
141#endif
142 return std::cin.rdbuf()->in_avail();
143 }
144
145 int read() override {
146#if defined(__unix__) || defined(__APPLE__)
147 if (available() == 0) return -1;
148 const int value = inputBuffer_.front();
149 inputBuffer_.pop_front();
150 return value;
151#else
152 return std::cin.get();
153#endif
154 }
155
156 int peek() override {
157#if defined(__unix__) || defined(__APPLE__)
158 return available() > 0 ? inputBuffer_.front() : -1;
159#else
160 return std::cin.peek();
161#endif
162 }
163
164 protected:
165 bool auto_flush = true;
166
167#if defined(__unix__) || defined(__APPLE__)
168 private:
169 void fillInputBuffer() {
170 int byteCount = 0;
171 if (ioctl(STDIN_FILENO, FIONREAD, &byteCount) != 0 || byteCount <= 0) {
172 return;
173 }
174
175 char buffer[256];
176 const size_t bytesToRead = std::min(static_cast<size_t>(byteCount), sizeof(buffer));
177 const ssize_t bytesRead = ::read(STDIN_FILENO, buffer, bytesToRead);
178 if (bytesRead <= 0) {
179 return;
180 }
181 inputBuffer_.insert(inputBuffer_.end(), buffer, buffer + bytesRead);
182 }
183
184 std::deque<unsigned char> inputBuffer_;
185#endif
186};
187
188inline StdioDevice Serial;
189#ifndef USE_RPI
190inline StdioDevice Serial2;
191#endif
192
193} // 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:51
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