PIDUINO
Loading...
Searching...
No Matches
gpiodevice.h
1/* Copyright © 2018-2025 Pascal JEAN, All rights reserved.
2 This file is part of the Piduino Library.
3
4 The Piduino Library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 3 of the License, or (at your option) any later version.
8
9 The Piduino Library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Lesser General Public License for more details.
13
14 You should have received a copy of the GNU Lesser General Public License
15 along with the Piduino Library; if not, see <http://www.gnu.org/licenses/>.
16*/
17#pragma once
18
19#include <piduino/board.h>
20#include <piduino/gpio.h>
21
22namespace Piduino {
23
24
34 class GpioDevice {
35
36 public:
37
38
48
54 virtual ~GpioDevice();
55
56 // -----------------------------------------------------------------
57 //
58 // Mandatory API
59 //
60 // -----------------------------------------------------------------
66 virtual bool open() = 0;
67
71 virtual void close() = 0;
72
79 virtual void write (const Pin *pin, bool v) = 0;
80
87 virtual bool read (const Pin *pin) const = 0;
88
95 virtual void setMode (const Pin *pin, Pin::Mode m) = 0;
96
103 virtual Pin::Mode mode (const Pin *pin) const = 0;
104
111 virtual void setPull (const Pin *pin, Pin::Pull p) = 0;
112
118 virtual AccessLayer preferedAccessLayer() const = 0;
119
125 virtual const std::map<Pin::Mode, std::string> &modes() const = 0;
126
127 // -----------------------------------------------------------------
128 //
129 // Optional API
130 //
131 // -----------------------------------------------------------------
132
140 virtual void toggle (const Pin *pin);
141
151 virtual Pin::Pull pull (const Pin *pin) const;
152
162 virtual void setDrive (const Pin *pin, int d);
163
173 virtual int drive (const Pin *pin) const;
174
187 virtual int waitForInterrupt (const Pin *pin, Pin::Edge edge, int timeout_ms);
188
196 virtual void setDebounce (const Pin *pin, uint32_t debounce_ms);
197
206 virtual uint32_t debounce (const Pin *pin) const;
207
223 virtual void setActiveLow (const Pin *pin, bool activeLow);
224
237 virtual bool isActiveLow (const Pin *pin) const;
238
252 enum {
253 hasToggle = 0x00000001,
254 hasPullRead = 0x00000002,
255 hasAltRead = 0x00000004,
256 hasDrive = 0x00000008,
257 hasWfi = 0x00000010,
258 hasActiveLow = 0x00000020,
259 hasDebounce = 0x00000040,
260 useGpioMem = 0x00010000,
261 };
262
269 virtual unsigned int flags() const;
270
276 virtual bool isOpen() const;
277
283 virtual bool isDebug() const;
284
290 virtual void setDebug (bool enable);
291
292 protected:
299 class Private;
300
309
318 std::unique_ptr<Private> d_ptr;
319
320 private:
325 };
326}
327/* ========================================================================== */
Abstract base class for GPIO devices.
Definition gpiodevice.h:34
virtual ~GpioDevice()
Destructor.
virtual Pin::Pull pull(const Pin *pin) const
Gets the pull-up/pull-down configuration of a GPIO pin.
virtual void setMode(const Pin *pin, Pin::Mode m)=0
Sets the mode of a GPIO pin.
virtual bool isDebug() const
Checks if the GPIO device is in debug mode.
virtual void toggle(const Pin *pin)
Toggles the state of a GPIO pin.
virtual bool open()=0
Opens the GPIO device.
virtual const std::map< Pin::Mode, std::string > & modes() const =0
Gets the supported modes for the GPIO device.
virtual void setDebug(bool enable)
Sets the debug mode of the GPIO device.
virtual void setActiveLow(const Pin *pin, bool activeLow)
Sets the active low configuration for a GPIO pin.
virtual int drive(const Pin *pin) const
Gets the drive strength of a GPIO pin.
virtual uint32_t debounce(const Pin *pin) const
Gets the debounce period for the GPIO line.
virtual Pin::Mode mode(const Pin *pin) const =0
Gets the mode of a GPIO pin.
virtual void write(const Pin *pin, bool v)=0
Writes a boolean value to a GPIO pin.
virtual void close()=0
Closes the GPIO device.
virtual void setDrive(const Pin *pin, int d)
Sets the drive strength of a GPIO pin.
virtual bool isActiveLow(const Pin *pin) const
Checks if a GPIO pin is configured as active low.
@ useGpioMem
Use /dev/gpiomem for GPIO access.
Definition gpiodevice.h:260
virtual int waitForInterrupt(const Pin *pin, Pin::Edge edge, int timeout_ms)
Waits for an interrupt on a GPIO pin.
virtual bool isOpen() const
Checks if the GPIO device is open.
virtual unsigned int flags() const
Gets the capability flags of the GPIO device.
virtual void setDebounce(const Pin *pin, uint32_t debounce_ms)
Sets the debounce period for the GPIO line. The default implementation does nothing.
GpioDevice()
Public Constructor.
std::unique_ptr< Private > d_ptr
A unique pointer to the private implementation (PIMPL idiom).
Definition gpiodevice.h:318
virtual void setPull(const Pin *pin, Pin::Pull p)=0
Sets the pull-up/pull-down configuration of a GPIO pin.
virtual AccessLayer preferedAccessLayer() const =0
Gets the preferred access layer for the GPIO device.
virtual bool read(const Pin *pin) const =0
Reads the value of a GPIO pin.
GpioDevice(Private &dd)
Protected Constructor.
Represents a general-purpose input/output (GPIO) pin and its configuration.
Definition gpiopin.h:68
Pull
Enumerates the possible pull resistor configurations.
Definition gpiopin.h:98
Mode
Enumerates the possible modes for a pin.
Definition gpiopin.h:76
Edge
Enumerates the possible edge detection types for interrupts.
Definition gpiopin.h:109
Internal implementation class for GpioDevice.
#define PIMP_DECLARE_PRIVATE(Class)
PIMP_DECLARE_PRIVATE.
Definition global.h:82
Global namespace for Piduino.
Definition board.h:28
AccessLayer
Enumerates the possible hardware access layers for GPIO operations.
Definition gpiopin.h:48