arduino-emulator
Loading...
Searching...
No Matches
FirmataTransport.h
1#pragma once
2/*
3 FirmataTransport.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/Stream.h"
22#include <atomic>
23#include <cstdint>
24#include <functional>
25#include <map>
26#include <mutex>
27#include <thread>
28#include <vector>
29
30namespace arduino {
31
34 uint8_t port;
35 uint16_t value;
36};
37
40 uint8_t pin;
41 int value;
42};
43
47 uint8_t command;
48 std::vector<uint8_t> data;
49};
50
67 public:
68 using DigitalHandler = std::function<void(const FirmataDigitalMessage&)>;
69 using AnalogHandler = std::function<void(const FirmataAnalogMessage&)>;
70 using SysexHandler = std::function<void(const FirmataSysexMessage&)>;
71
73
75 bool begin(Stream& stream);
76
78 void end();
79
80 operator bool() const { return running_.load(); }
81
83 void writeBytes(const uint8_t* data, size_t len);
84 void writeByte(uint8_t b) { writeBytes(&b, 1); }
85
87 void onDigitalMessage(DigitalHandler handler) {
88 digital_handler_ = std::move(handler);
89 }
90
92 void onAnalogMessage(AnalogHandler handler) {
93 analog_handler_ = std::move(handler);
94 }
95
99 void addSysexHandler(uint8_t command, SysexHandler handler);
100
102 void removeSysexHandler(uint8_t command);
103
104 private:
105 Stream* stream_ = nullptr;
106 std::atomic<bool> running_{false};
107 std::thread thread_;
108 std::mutex write_mutex_;
109
110 DigitalHandler digital_handler_;
111 AnalogHandler analog_handler_;
112 std::mutex sysex_handlers_mutex_;
113 std::map<uint8_t, SysexHandler> sysex_handlers_;
114
115 void readerLoop();
116};
117
118} // namespace arduino
119
120#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
void onDigitalMessage(DigitalHandler handler)
Register the (single) handler for digital port reports.
Definition FirmataTransport.h:87
void addSysexHandler(uint8_t command, SysexHandler handler)
Definition FirmataTransport.cpp:57
void onAnalogMessage(AnalogHandler handler)
Register the (single) handler for analog pin reports.
Definition FirmataTransport.h:92
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
void writeBytes(const uint8_t *data, size_t len)
Thread-safe write of a raw byte sequence (e.g. a whole SysEx message).
Definition FirmataTransport.cpp:49
void removeSysexHandler(uint8_t command)
Remove a previously registered SysEx handler.
Definition FirmataTransport.cpp:62
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
A single analog pin report (device -> host).
Definition FirmataTransport.h:39
A digital port report (device -> host), 8 pins packed into one message.
Definition FirmataTransport.h:33
Definition FirmataTransport.h:46