arduino-emulator
Loading...
Searching...
No Matches
FileStream.h
1/*
2 FileStream.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#include <fstream>
21#include <iostream>
22#include "Platform.h"
23#include "api/Stream.h"
24
25namespace arduino {
26
31class FileStream : public Stream {
32 public:
33 FileStream(const char* outDevice = "/dev/stdout",
34 const char* inDevice = "/dev/stdin") {
35 open(outDevice, inDevice);
36 }
37
38 ~FileStream() {
39 in.close();
40 out.close();
41 }
42
43 void open(const char* outDevice, const char* inDevice) {
44 if (outDevice != nullptr) out.open(outDevice, std::ios::out);
45 if (inDevice != nullptr) in.open(inDevice, std::ios::in);
46 }
47
48 virtual void begin(int speed) {
49 // nothing to be done
50 }
51
52 virtual void end() {
53 // nothing to be done
54 }
55
56 virtual void print(const char* str) {
57 out << str;
58 out.flush();
59 }
60
61 virtual void println(const char* str = "") {
62 out << str << "\n";
63 out.flush();
64 }
65
66 virtual void print(int str) {
67 out << str;
68 out.flush();
69 }
70
71 virtual void println(int str) {
72 out << str << "\n";
73 out.flush();
74 }
75
76 virtual void flush() { out.flush(); }
77
78 virtual void write(const char* str, int len) { out.write(str, len); }
79
80 virtual void write(uint8_t* str, int len) {
81 out.write((const char*)str, len);
82 }
83
84 virtual size_t write(int32_t value) {
85 out.put(value);
86 return 1;
87 }
88
89 virtual size_t write(uint8_t value) {
90 out.put(value);
91 return 1;
92 }
93
94 virtual int available() { return in.rdbuf()->in_avail(); };
95
96 virtual int read() { return in.get(); }
97
98 virtual int peek() { return in.peek(); }
99
100 protected:
101 std::fstream out;
102 std::fstream in;
103};
104
114#if ARDUINO_EMULATOR_WINDOWS
115// No physical COM port is assumed on Windows. Use SerialImpl and pass the
116// selected device explicitly when hardware serial is required.
117static FileStream Serial1(nullptr, nullptr);
118#else
119static FileStream Serial1("/dev/ttyACM0");
120#endif
121
122} // namespace arduino
Definition DMAPool.h:103
We use the FileStream class to be able to provide Serial, Serial1 and Serial2 outside of the Arduino ...
Definition FileStream.h:31
Definition Stream.h:51
We provide the WiFi class to simulate the Arduino WIFI. In in Linux we can expect that networking is ...
Definition CanMsg.cpp:31
static FileStream Serial1(nullptr, nullptr)
Global Serial1 instance for secondary serial communication.