arduino-emulator
Loading...
Searching...
No Matches
HardwareSetupFIR.h
1#pragma once
2/*
3 HardwareSetupFIR.h
4 Copyright (c) 2025 Phil Schatzmann. All right reserved.
5
6 This library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 2.1 of the License, or (at your option) any later version.
10
11 This library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public
17 License along with this library; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19*/
20#if defined(USE_FIRMATA) && !defined(SKIP_HARDWARE_SETUP)
21#include "FirmataTransport.h"
22#include "HardwareGPIO_FIR.h"
23#include "HardwareI2C_FIR.h"
24#include "HardwareSPI_FIR.h"
25
26namespace arduino {
27
41class HardwareSetupFIRMATA : public I2CSource, public SPISource, public GPIOSource {
42 public:
43 HardwareSetupFIRMATA() = default;
44
53
55
62 bool begin(Stream &stream, bool asDefault = true) {
63 Logger.info("Using Firmata hardware interfaces");
64 is_default_objects_active = asDefault;
65
66 transport.begin(stream);
67 bool gpio_ok = gpio.begin(transport);
68 bool i2c_ok = i2c.begin(transport);
69 bool spi_ok = spi.begin(transport);
70
71 // define the global hardware interfaces
72 if (asDefault) {
73 Logger.warning("GPIO, I2C, SPI set up for Firmata");
74 GPIO.setGPIO(&gpio);
75 SPI.setSPI(&spi);
76 Wire.setI2C(&i2c);
77 }
78
79 return gpio_ok && i2c_ok && spi_ok;
80 }
81
85 void end() {
86 if (is_default_objects_active) {
87 GPIO.setGPIO(nullptr);
88 SPI.setSPI(nullptr);
89 Wire.setI2C(nullptr);
90 }
91 gpio.end();
92 i2c.end();
93 spi.end();
94 transport.end();
95 }
96
97 HardwareGPIO_FIRMATA *getGPIO() { return &gpio; }
98 HardwareI2C_FIRMATA *getI2C() { return &i2c; }
99 HardwareSPI_FIRMATA *getSPI() { return &spi; }
100
101 protected:
102 FirmataTransport transport;
103 HardwareGPIO_FIRMATA gpio;
104 HardwareI2C_FIRMATA i2c;
105 HardwareSPI_FIRMATA spi;
106 bool is_default_objects_active = false;
107};
108
119
120} // namespace arduino
121
122#endif
Definition DMAPool.h:103
bool begin(Stream &stream)
Start the background reader thread on the given stream.
Definition FirmataTransport.cpp:32
void end()
Stop the reader thread and detach from the stream.
Definition FirmataTransport.cpp:41
Abstract interface for providing GPIO hardware implementations.
Definition Sources.h:66
void setGPIO(HardwareGPIO *gpio)
Set the GPIO implementation directly.
Definition GPIOWrapper.h:153
GPIO hardware abstraction that speaks the Firmata protocol over an arbitrary Stream (e....
Definition HardwareGPIO_FIR.h:49
void end()
Stop the reader thread (if owned) and detach.
Definition HardwareGPIO_FIR.cpp:74
bool begin(Stream &stream)
Start talking Firmata over the given stream. Owns a private FirmataTransport for this connection alon...
Definition HardwareGPIO_FIR.cpp:53
bool begin(Stream &stream)
Start talking Firmata over the given stream (owns a private transport).
Definition HardwareI2C_FIR.cpp:35
bool begin(Stream &stream)
Start talking Firmata over the given stream (owns a private transport).
Definition HardwareSPI_FIR.cpp:41
Sets up hardware interfaces (GPIO, I2C, SPI) that talk to a remote board running Firmata,...
Definition HardwareSetupFIR.h:41
HardwareSetupFIRMATA(uint8_t spiDeviceId, int8_t spiCsPin=-1)
Definition HardwareSetupFIR.h:51
void end()
Resets hardware pointers to nullptr and stops the transport.
Definition HardwareSetupFIR.h:85
bool begin(Stream &stream, bool asDefault=true)
Start the shared Firmata transport and all three backends.
Definition HardwareSetupFIR.h:62
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
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
static HardwareSetupFIRMATA FIRMATA
Global instance for Firmata hardware setup.
Definition HardwareSetupFIR.h:118