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 end() {
52 // nothing to be done
53 }
54
55 virtual void print(const char* str) {
56 out << str;
57 out.flush();
58 }
59
60 virtual void println(const char* str = "") {
61 out << str << "\n";
62 out.flush();
63 }
64
65 virtual void print(int str) {
66 out << str;
67 out.flush();
68 }
69
70 virtual void println(int str) {
71 out << str << "\n";
72 out.flush();
73 }
74
75 virtual void flush() { out.flush(); }
76
77 virtual void write(const char* str, int len) { out.write(str, len); }
78
79 virtual void write(uint8_t* str, int len) {
80 out.write((const char*)str, len);
81 }
82
83 virtual size_t write(int32_t value) {
84 out.put(value);
85 return 1;
86 }
87
88 virtual size_t write(uint8_t value) {
89 out.put(value);
90 return 1;
91 }
92
93 virtual int available() { return in.rdbuf()->in_avail(); };
94
95 virtual int read() { return in.get(); }
96
97 virtual int peek() { return in.peek(); }
98
99 protected:
100 std::fstream out;
101 std::fstream in;
102};
103
113static FileStream Serial1("/dev/ttyACM0");
114
115} // 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: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.