arduino-emulator
Loading...
Searching...
No Matches
HardwareGPIO_FIR.h
1#pragma once
2/*
3 HardwareGPIO_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 "HardwareGPIO.h"
22#include "FirmataTransport.h"
23#include <map>
24#include <memory>
25#include <mutex>
26
27namespace arduino {
28
50 public:
51 HardwareGPIO_FIRMATA() = default;
53
60 bool begin(Stream &stream);
61
68 bool begin(FirmataTransport &transport);
69
73 void end();
74
75 void pinMode(pin_size_t pinNumber, PinMode pinMode) override;
76 void digitalWrite(pin_size_t pinNumber, PinStatus status) override;
77 PinStatus digitalRead(pin_size_t pinNumber) override;
78 int analogRead(pin_size_t pinNumber) override;
79 void analogReference(uint8_t mode) override;
80 void analogWrite(pin_size_t pinNumber, int value) override;
81 void analogWriteFrequency(pin_size_t pinNumber, uint32_t frequency);
82 void tone(uint8_t _pin, unsigned int frequency,
83 unsigned long duration = 0) override;
84 void noTone(uint8_t _pin) override;
85 unsigned long pulseIn(uint8_t pin, uint8_t state,
86 unsigned long timeout = 1000000L) override;
87 unsigned long pulseInLong(uint8_t pin, uint8_t state,
88 unsigned long timeout = 1000000L) override;
89 void analogWriteResolution(uint8_t bits) override;
90
91 operator bool() { return is_open; }
92
93 protected:
94 FirmataTransport *transport_ = nullptr; // non-owning
95 std::unique_ptr<FirmataTransport> owned_transport_; // set by begin(Stream&)
96 bool is_open = false;
97
98 std::mutex state_mutex_;
99 std::map<pin_size_t, PinMode> pin_modes_;
100 std::map<pin_size_t, PinStatus> pin_states_;
101 std::map<pin_size_t, int> analog_values_;
102 std::map<uint8_t, bool> digital_reporting_enabled_;
103 std::map<pin_size_t, bool> analog_reporting_enabled_;
104
105 void handleDigitalMessage(const FirmataDigitalMessage &msg);
106 void handleAnalogMessage(const FirmataAnalogMessage &msg);
107
109 void enableDigitalReporting(pin_size_t pinNumber);
111 void enableAnalogReporting(pin_size_t pinNumber);
112
114 static uint8_t toFirmataPinMode(PinMode mode);
115};
116
117} // namespace arduino
118
119#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
GPIO hardware abstraction that speaks the Firmata protocol over an arbitrary Stream (e....
Definition HardwareGPIO_FIR.h:49
void analogWriteResolution(uint8_t bits) override
Set the resolution of the analogWrite() values.
Definition HardwareGPIO_FIR.cpp:235
void enableAnalogReporting(pin_size_t pinNumber)
Enable REPORT_ANALOG for pinNumber, once.
Definition HardwareGPIO_FIR.cpp:119
unsigned long pulseIn(uint8_t pin, uint8_t state, unsigned long timeout=1000000L) override
Read a pulse (HIGH or LOW) on a pin.
Definition HardwareGPIO_FIR.cpp:223
int analogRead(pin_size_t pinNumber) override
Read the value from the specified analog pin.
Definition HardwareGPIO_FIR.cpp:183
void end()
Stop the reader thread (if owned) and detach.
Definition HardwareGPIO_FIR.cpp:74
void enableDigitalReporting(pin_size_t pinNumber)
Enable REPORT_DIGITAL for the port containing pinNumber, once.
Definition HardwareGPIO_FIR.cpp:105
PinStatus digitalRead(pin_size_t pinNumber) override
Read the value from a specified digital pin.
Definition HardwareGPIO_FIR.cpp:174
void analogWrite(pin_size_t pinNumber, int value) override
Write an analog value (PWM wave) to a pin.
Definition HardwareGPIO_FIR.cpp:196
void noTone(uint8_t _pin) override
Stop the generation of a square wave triggered by tone()
Definition HardwareGPIO_FIR.cpp:219
void digitalWrite(pin_size_t pinNumber, PinStatus status) override
Write a HIGH or LOW value to a digital pin.
Definition HardwareGPIO_FIR.cpp:151
bool begin(Stream &stream)
Start talking Firmata over the given stream. Owns a private FirmataTransport for this connection alon...
Definition HardwareGPIO_FIR.cpp:53
void tone(uint8_t _pin, unsigned int frequency, unsigned long duration=0) override
Generate a square wave of the specified frequency on a pin.
Definition HardwareGPIO_FIR.cpp:214
void analogWriteFrequency(pin_size_t pinNumber, uint32_t frequency)
Set the PWM frequency for analogWrite() on the specified pin.
Definition HardwareGPIO_FIR.cpp:209
void analogReference(uint8_t mode) override
Configure the reference voltage used for analog input.
Definition HardwareGPIO_FIR.cpp:192
static uint8_t toFirmataPinMode(PinMode mode)
Translate the Arduino PinMode enum to the Firmata SET_PIN_MODE value.
Definition HardwareGPIO_FIR.cpp:38
unsigned long pulseInLong(uint8_t pin, uint8_t state, unsigned long timeout=1000000L) override
Alternative to pulseIn() which is better at handling long pulses.
Definition HardwareGPIO_FIR.cpp:229
void pinMode(pin_size_t pinNumber, PinMode pinMode) override
Configure the specified pin to behave as an input or output.
Definition HardwareGPIO_FIR.cpp:133
Abstract base class for GPIO (General Purpose Input/Output) functions.
Definition HardwareGPIO.h:51
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