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 "WiFiUdpStream.h"
30
31namespace arduino {
32
57 public SPISource,
58 public GPIOSource {
59 public:
62
65
67 HardwareSetupRemote(int port) { this->port = port; }
68
70 bool begin(Stream* s, bool asDefault = true, bool doHandShake = true) {
71 p_stream = s;
72
73 i2c.setStream(s);
74 spi.setStream(s);
75 gpio.setStream(s);
76
77 // setup global objects
78 if (asDefault) {
79 Logger.warning("GPIO, I2C, SPI set up for Remote");
80 SPI.setSPI(&spi);
81 Wire.setI2C(&i2c);
82 GPIO.setGPIO(&gpio);
83 }
84
85 if (doHandShake) {
86 handShake(s);
87 }
88 return i2c && spi && gpio;
89 }
90
92 void begin(int port, bool asDefault) {
93 this->port = port;
95 }
96
98 void begin(bool asDefault = true) {
99 is_default_objects_active = asDefault;
100 if (p_stream == nullptr) {
101 default_stream.begin(port);
102 handShake(&default_stream);
103 IPAddress ip = default_stream.remoteIP();
104 int remote_port = default_stream.remotePort();
105 default_stream.setTarget(ip, remote_port);
106 default_stream.write((const uint8_t*)"OK", 2);
107 default_stream.flush();
108 begin(&default_stream, asDefault, false);
109 } else {
110 begin(p_stream, asDefault, true);
111 }
112 }
113
114 void end() {
115 if (is_default_objects_active) {
116 GPIO.setGPIO(nullptr);
117 SPI.setSPI(nullptr);
118 Wire.setI2C(nullptr);
119 }
120 if (p_stream == &default_stream) {
121 default_stream.stop();
122 }
123 }
124
125 HardwareGPIO* getGPIO() { return &gpio; }
126 HardwareI2C* getI2C() { return &i2c; }
127 HardwareSPI* getSPI() { return &spi; }
128
129 protected:
130 WiFiUDPStream default_stream;
131 Stream* p_stream = nullptr;
132 RemoteI2C i2c;
133 RemoteSPI spi;
134 RemoteGPIO gpio;
135 int port;
136 bool is_default_objects_active = false;
137
138 void handShake(Stream* s) {
139 while (true) {
140 Logger.warning("HardwareSetup", "waiting for device...");
141 try {
142 // we wait for the Arduino to send us the Arduino-Emulator string
143 if (s->available() >= 16) {
144 char buffer[30];
145 int len = s->readBytes(buffer, 18);
146 buffer[len] = 0;
147 if (strncmp(buffer, "Arduino-Emulator", 16)) {
148 Logger.info("WiFiUDPStream", "device found!");
149 break;
150 } else {
151 Logger.info("WiFiUDPStream", "unknown command", buffer);
152 }
153 }
154 delay(10000);
155 } catch (const std::exception& ex) {
156 Logger.error("WiFiUDPStream", ex.what());
157 }
158 }
159 }
160};
161
162#if !defined(SKIP_HARDWARE_SETUP)
163static HardwareSetupRemote Remote{7000};
164#endif
165
166} // 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:58
void begin(int port, bool asDefault)
start with udp on the indicatd port
Definition HardwareSetupRemote.h:92
HardwareSetupRemote()=default
default constructor: you need to call begin() afterwards
HardwareSetupRemote(Stream &stream)
HardwareSetup uses the indicated stream.
Definition HardwareSetupRemote.h:64
void begin(bool asDefault=true)
start with the default udp stream.
Definition HardwareSetupRemote.h:98
bool begin(Stream *s, bool asDefault=true, bool doHandShake=true)
assigns the different protocols to the stream
Definition HardwareSetupRemote.h:70
HardwareSetupRemote(int port)
HardwareSetup that uses udp.
Definition HardwareSetupRemote.h:67
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:74
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
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