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 "api/Stream.h"
23
24namespace arduino {
25
30class FileStream : public Stream {
31 public:
32 FileStream(const char* outDevice = "/dev/stdout",
33 const char* inDevice = "/dev/stdin") {
34 open(outDevice, inDevice);
35 }
36
37 ~FileStream() {
38 in.close();
39 out.close();
40 }
41
42 void open(const char* outDevice, const char* inDevice) {
43 if (outDevice != nullptr) out.open(outDevice, std::ios::out);
44 if (inDevice != nullptr) in.open(inDevice, std::ios::in);
45 }
46
47 virtual void begin(int speed) {
48 // nothing to be done
49 }
50
51 virtual void print(const char* str) {
52 out << str;
53 out.flush();
54 }
55
56 virtual void println(const char* str = "") {
57 out << str << "\n";
58 out.flush();
59 }
60
61 virtual void print(int str) {
62 out << str;
63 out.flush();
64 }
65
66 virtual void println(int str) {
67 out << str << "\n";
68 out.flush();
69 }
70
71 virtual void flush() { out.flush(); }
72
73 virtual void write(const char* str, int len) { out.write(str, len); }
74
75 virtual void write(uint8_t* str, int len) {
76 out.write((const char*)str, len);
77 }
78
79 virtual size_t write(int32_t value) {
80 out.put(value);
81 return 1;
82 }
83
84 virtual size_t write(uint8_t value) {
85 out.put(value);
86 return 1;
87 }
88
89 virtual int available() { return in.rdbuf()->in_avail(); };
90
91 virtual int read() { return in.get(); }
92
93 virtual int peek() { return in.peek(); }
94
95 protected:
96 std::fstream out;
97 std::fstream in;
98};
99
109static FileStream Serial1("/dev/ttyACM0");
110
111} // namespace arduino
Definition DMAPool.h:103
We use the FileStream class to be able to provide Serail, Serial1 and Serial2 outside of the Arduino ...
Definition FileStream.h:30
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("/dev/ttyACM0")
Global Serial1 instance for secondary serial communication.