arduino-emulator
Loading...
Searching...
No Matches
HardwareSetupRemote.h
1/*
2 HardwareSetupRemote.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
20#pragma once
21#include <exception>
22#include "ArduinoLogger.h"
23#include "GPIOWrapper.h"
24#include "I2CWrapper.h"
25#include "RemoteGPIO.h"
26#include "RemoteI2C.h"
27#include "RemoteSPI.h"
28#include "SPIWrapper.h"
29#include "UDPStream.h"
30#include <cstring>
31
32namespace arduino {
33
58 public SPISource,
59 public GPIOSource {
60 public:
63
66
68 HardwareSetupRemote(int port) { this->port = port; }
69
71 bool begin(Stream* s, bool asDefault = true, bool doHandShake = true) {
72 p_stream = s;
73
74 i2c.setStream(s);
75 spi.setStream(s);
76 gpio.setStream(s);
77
78 // setup global objects
79 if (asDefault) {
80 Logger.warning("GPIO, I2C, SPI set up for Remote");
81 SPI.setSPI(&spi);
82 Wire.setI2C(&i2c);
83 GPIO.setGPIO(&gpio);
84 }
85
86 if (doHandShake) {
87 handShake(s);
88 }
89 return i2c && spi && gpio;
90 }
91
93 void begin(int port, bool asDefault) {
94 this->port = port;
96 }
97
99 void begin(bool asDefault = true) {
100 is_default_objects_active = asDefault;
101 if (p_stream == nullptr) {
102 default_stream.begin(port);
103 handShake(&default_stream);
104 IPAddress ip = default_stream.remoteIP();
105 int remote_port = default_stream.remotePort();
106 default_stream.setTarget(ip, remote_port);
107 default_stream.write((const uint8_t*)"OK", 2);
108 default_stream.flush();
109 begin(&default_stream, asDefault, false);
110 } else {
111 begin(p_stream, asDefault, true);
112 }
113 }
114
115 void end() {
116 if (is_default_objects_active) {
117 GPIO.setGPIO(nullptr);
118 SPI.setSPI(nullptr);
119 Wire.setI2C(nullptr);
120 }
121 if (p_stream == &default_stream) {
122 default_stream.stop();
123 }
124 }
125
126 HardwareGPIO* getGPIO() { return &gpio; }
127 HardwareI2C* getI2C() { return &i2c; }
128 HardwareSPI* getSPI() { return &spi; }
129
130 protected:
131 arduino::UDPStream default_stream;
132 Stream* p_stream = nullptr;
133 RemoteI2C i2c;
134 RemoteSPI spi;
135 RemoteGPIO gpio;
136 int port;
137 bool is_default_objects_active = false;
138
139 void handShake(Stream* s) {
140 while (true) {
141 Logger.warning("HardwareSetup", "waiting for device...");
142 try {
143 // we wait for the Arduino to send us the Arduino-Emulator string
144 if (s->available() >= 16) {
145 char buffer[30];
146 int len = s->readBytes(buffer, 18);
147 buffer[len] = 0;
148 if (strncmp(buffer, "Arduino-Emulator", 16)==0) {
149 Logger.info("UDPStream", "device found!");
150 break;
151 } else {
152 Logger.info("UDPStream", "unknown command", buffer);
153 }
154 }
155 delay(10000);
156 } catch (const std::exception& ex) {
157 Logger.error("UDPStream", ex.what());
158 }
159 }
160 }
161};
162
163#if !defined(SKIP_HARDWARE_SETUP)
164static HardwareSetupRemote Remote{7000};
165#endif
166
167} // namespace arduino
Definition DMAPool.h:103
Abstract interface for providing GPIO hardware implementations.
Definition Sources.h:66
void setGPIO(HardwareGPIO *gpio)
Set the GPIO implementation directly.
Definition GPIOWrapper.h:153
Configures and manages remote hardware interfaces for Arduino emulation.
Definition HardwareSetupRemote.h:59
void begin(int port, bool asDefault)
start with udp on the indicatd port
Definition HardwareSetupRemote.h:93
HardwareSetupRemote()=default
default constructor: you need to call begin() afterwards
HardwareSetupRemote(Stream &stream)
HardwareSetup uses the indicated stream.
Definition HardwareSetupRemote.h:65
void begin(bool asDefault=true)
start with the default udp stream.
Definition HardwareSetupRemote.h:99
bool begin(Stream *s, bool asDefault=true, bool doHandShake=true)
assigns the different protocols to the stream
Definition HardwareSetupRemote.h:71
HardwareSetupRemote(int port)
HardwareSetup that uses udp.
Definition HardwareSetupRemote.h:68
Abstract interface for providing I2C hardware implementations.
Definition Sources.h:36
void setI2C(HardwareI2C *i2c)
defines the i2c implementation: use nullptr to reset.
Definition I2CWrapper.h:75
Definition IPAddress.h:43
Abstract interface for providing SPI hardware implementations.
Definition Sources.h:51
void setSPI(HardwareSPI *spi)
defines the spi implementation: use nullptr to reset.
Definition SPIWrapper.h:72
Definition Stream.h:51
A UDP class which makes sure that we can use UDP as simple Stream w/o additional complexity....
Definition UDPStream.h:22
uint16_t remotePort()
We use the same remote port as defined in begin for write.
Definition UDPStream.h:75
size_t readBytes(uint8_t *data, size_t len)
Reads bytes using WiFi::readBytes.
Definition UDPStream.h:104
IPAddress remoteIP()
We use the same remote ip as defined in begin for write.
Definition UDPStream.h:81
size_t write(const uint8_t *data, size_t len) override
Replys will be sent to the initial remote caller.
Definition UDPStream.h:91
bool begin(IPAddress a, uint16_t port)
Starts to send data to the indicated address / port.
Definition UDPStream.h:53
bool setTarget(IPAddress a, uint16_t port)
Alternative to begin(IPAddress a, uint16_t port)
Definition UDPStream.h:72
We provide the WiFi class to simulate the Arduino WIFI. In in Linux we can expect that networking is ...
Definition CanMsg.cpp:31
I2CWrapper Wire
Global Wire instance used by Arduino API functions and direct access.
Definition I2CWrapper.cpp:24
SPIWrapper SPI
Global SPI instance used by Arduino API and direct access.
Definition SPIWrapper.cpp:27
GPIOWrapper GPIO
Global GPIO instance used by Arduino API functions and direct access.
Definition GPIOWrapper.cpp:35