arduino-emulator
Loading...
Searching...
No Matches
HardwareI2C_FIR.h
1#pragma once
2/*
3 HardwareI2C_FIR.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#ifdef USE_FIRMATA
21#include "api/HardwareI2C.h"
22#include "FirmataTransport.h"
23#include <condition_variable>
24#include <deque>
25#include <memory>
26#include <mutex>
27#include <vector>
28
29namespace arduino {
30
50 public:
51 HardwareI2C_FIRMATA() = default;
53
55 bool begin(Stream &stream);
58 bool begin(FirmataTransport &transport);
59
60 void begin() override;
61 void begin(uint8_t address) override; // I2C slave mode: unsupported, no-op
62 void end() override;
63
64 void setClock(uint32_t freq) override; // no-op, see class note
65
66 void beginTransmission(uint8_t address) override;
67 size_t write(uint8_t data) override;
68 size_t write(const uint8_t *data, size_t len) override;
69 uint8_t endTransmission(bool stopBit) override;
70 uint8_t endTransmission(void) override;
71
72 size_t requestFrom(uint8_t address, size_t len, bool stopBit) override;
73 size_t requestFrom(uint8_t address, size_t len) override;
74
75 int available() override;
76 int peek() override;
77 int read() override;
78 void flush() override {}
79
80 void onReceive(void (*)(int)) override {} // slave mode unsupported
81 void onRequest(void (*)(void)) override {} // slave mode unsupported
82
84 void setTimeout(unsigned long timeout_ms) { timeout_ms_ = timeout_ms; }
85
86 operator bool() { return is_open; }
87
88 protected:
89 FirmataTransport *transport_ = nullptr; // non-owning
90 std::unique_ptr<FirmataTransport> owned_transport_; // set by begin(Stream&)
91 bool is_open = false;
92 unsigned long timeout_ms_ = 1000;
93
94 uint8_t current_address_ = 0;
95 std::vector<uint8_t> tx_buffer_;
96
97 std::mutex rx_mutex_;
98 std::condition_variable rx_cv_;
99 std::deque<uint8_t> rx_buffer_;
100 bool request_pending_ = false;
101
102 void handleSysex(const FirmataSysexMessage &msg);
103};
104
105} // namespace arduino
106
107#endif // USE_FIRMATA
Definition DMAPool.h:103
Single reader thread + byte-stream parser shared by every Firmata-based hardware backend (GPIO,...
Definition FirmataTransport.h:66
I2C master implementation that speaks the Firmata protocol (I2C_REQUEST/I2C_REPLY/I2C_CONFIG SysEx me...
Definition HardwareI2C_FIR.h:49
void setTimeout(unsigned long timeout_ms)
Maximum time requestFrom() waits for the I2C_REPLY, in milliseconds.
Definition HardwareI2C_FIR.h:84
bool begin(Stream &stream)
Start talking Firmata over the given stream (owns a private transport).
Definition HardwareI2C_FIR.cpp:35
Definition HardwareI2C.h:28
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