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+ // Undefine DEPRECATED macro from libftdi1 to avoid conflict with Arduino API
22+ #ifdef DEPRECATED
23+ #undef DEPRECATED
24+ #endif
25+ #include " HardwareGPIO.h"
26+ #include < map>
27+ #include < thread>
28+ #include < atomic>
29+ #include < mutex>
30+ #include < chrono>
31+
32+ namespace arduino {
33+
34+ /* *
35+ * @class HardwareGPIO_FIRMATA
36+ */
37+ class HardwareGPIO_FIRMATA : public HardwareGPIO {
38+ public:
39+ /* *
40+ * @brief Constructor for HardwareGPIO_FIRMATA.
41+ */
42+ HardwareGPIO_FIRMA () = default ;
43+
44+ /* *
45+ * @brief Destructor for HardwareGPIO_FTDI.
46+ */
47+ ~HardwareGPIO_FIRMA ();
48+
49+ /* *
50+ * @brief Initialize the GPIO hardware interface for FTDI FT2232HL.
51+ * @param vendor_id USB vendor ID (default: 0x0403)
52+ * @param product_id USB product ID (default: 0x6010 for FT2232HL)
53+ * @param description Device description string (optional)
54+ * @param serial Device serial number (optional)
55+ * @return true if initialization successful, false otherwise
56+ */
57+ bool begin (Stream &stream);
58+
59+ /* *
60+ * @brief Close the FTDI connection and cleanup resources.
61+ */
62+ void end ();
63+
64+ /* *
65+ * @brief Set the mode of a GPIO pin (INPUT, OUTPUT, etc).
66+ * @param pinNumber Pin number (0-15 for FT2232HL)
67+ * @param pinMode Pin mode (INPUT, OUTPUT, INPUT_PULLUP)
68+ */
69+ void pinMode (pin_size_t pinNumber, PinMode pinMode) override ;
70+
71+ /* *
72+ * @brief Write a digital value to a GPIO pin.
73+ * @param pinNumber Pin number (0-15)
74+ * @param status Pin status (HIGH or LOW)
75+ */
76+ void digitalWrite (pin_size_t pinNumber, PinStatus status) override ;
77+
78+ /* *
79+ * @brief Read a digital value from a GPIO pin.
80+ * @param pinNumber Pin number (0-15)
81+ * @return Pin status (HIGH or LOW)
82+ */
83+ PinStatus digitalRead (pin_size_t pinNumber) override ;
84+
85+ /* *
86+ * @brief Read an analog value from a pin (not supported by FT2232HL).
87+ * @param pinNumber Pin number
88+ * @return Always returns 0 (no ADC on FT2232HL)
89+ */
90+ int analogRead (pin_size_t pinNumber) override ;
91+
92+ /* *
93+ * @brief Set the analog reference mode (not supported by FT2232HL).
94+ * @param mode Reference mode (ignored)
95+ */
96+ void analogReference (uint8_t mode) override ;
97+
98+ /* *
99+ * @brief Write an analog value (PWM) to a pin using software PWM.
100+ * @param pinNumber Pin number (0-15)
101+ * @param value PWM duty cycle (0-255, where 0=0% and 255=100%)
102+ */
103+ void analogWrite (pin_size_t pinNumber, int value) override ;
104+
105+ /* *
106+ * @brief Set the PWM frequency for analogWrite() on a specific pin.
107+ * @param pinNumber Pin number (0-15)
108+ * @param frequency PWM frequency in Hz (default: 1000Hz)
109+ */
110+ void analogWriteFrequency (pin_size_t pinNumber, uint32_t frequency);
111+
112+ /* *
113+ * @brief Generate a tone on a pin (not supported by FT2232HL).
114+ * @param _pin Pin number
115+ * @param frequency Frequency in Hz (ignored)
116+ * @param duration Duration in ms (ignored)
117+ */
118+ void tone (uint8_t _pin, unsigned int frequency,
119+ unsigned long duration = 0 ) override ;
120+
121+ /* *
122+ * @brief Stop tone generation on a pin (not supported by FT2232HL).
123+ * @param _pin Pin number (ignored)
124+ */
125+ void noTone (uint8_t _pin) override ;
126+
127+ /* *
128+ * @brief Measure pulse duration on a pin
129+ * @param pin Pin number
130+ * @param state Pin state to measure
131+ * @param timeout Timeout in microseconds
132+ * @return Always returns 0 (not implemented)
133+ */
134+ unsigned long pulseIn (uint8_t pin, uint8_t state,
135+ unsigned long timeout = 1000000L ) override ;
136+
137+ /* *
138+ * @brief Measure long pulse duration on a pin
139+ * @param pin Pin number
140+ * @param state Pin state to measure
141+ * @param timeout Timeout in microseconds
142+ * @return Always returns 0 (not implemented)
143+ */
144+ unsigned long pulseInLong (uint8_t pin, uint8_t state,
145+ unsigned long timeout = 1000000L ) override ;
146+
147+ /* *
148+ * @brief Set the resolution for analogWrite() operations.
149+ * @param bits The resolution in bits (8-bit by default for FTDI)
150+ * @note FT2232HL supports 8-bit PWM resolution (0-255)
151+ */
152+ void analogWriteResolution (uint8_t bits) override ;
153+
154+ /* *
155+ * @brief Boolean conversion operator.
156+ * @return true if the FTDI interface is open and initialized, false otherwise.
157+ */
158+ operator bool () { return is_open && ftdi_context != nullptr ; }
159+
160+
161+ protected:
162+ bool is_open = false ;
163+
164+ };
165+
166+ } // namespace arduino
167+
168+ #endif // USE_FTDI
0 commit comments