PIDUINO
Loading...
Searching...
No Matches
gpiopin.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
18#pragma once
19
20#include <string>
21#include <map>
22#include <piduino/converter.h>
23#include <piduino/gpio2.h>
24
25
26namespace Piduino {
27
28 class Gpio;
29 class GpioDevice;
30 class Connector;
31
56
68 class Pin {
69 public:
70 friend class Connector;
71
93
104
116
127
142
147 class Number {
148 public:
150 int mcu;
151 int system;
152 int row;
153 int column;
154 int chip;
155 int offset;
159 Number (int logical = -1, int mcu = -1, int system = -1,
160 int row = -1, int column = -1, int chip = -1, int offset = -1)
161 : logical (logical), mcu (mcu), system (system), row (row),
163
164 };
165
171 public:
174 long long id;
175 std::map<Mode, std::string> name;
179 Descriptor (long long pinId = -1, int pinRow = -1, int pinColumn = -1);
184 bool insert();
191 bool hasModeName (Mode m, long long nameId) const;
196 long long findId() const;
202 long long findName (const std::string &name) const;
203 private:
209 void insertModeName (Mode m, const std::string &n);
210 };
211
216 class SpiCs {
217 public:
218 long long pin;
219 int bus;
220 int cs;
222 };
223
229
235
240 bool isOpen() const;
241
247 int number (Numbering numbering) const;
248
253 static const std::map<Numbering, std::string> &numberings();
254
260 static const std::string &numberingName (Numbering n);
261
266 int logicalNumber() const;
267
272 int mcuNumber() const;
273
278 int systemNumber() const;
279
284 int physicalNumber() const;
285
290 int chipNumber() const;
291
296 int chipOffset() const;
297
302 int row() const;
303
308 int column() const;
309
314 Type type() const;
315
327 unsigned int flags() const;
328
333 const std::string &typeName() const;
334
340 static const std::string &typeName (Type t);
341
346 static const std::map<Type, std::string> &types();
347
352 const std::string &name() const;
353
359 const std::string &name (Mode mode) const;
360
366
371 Mode mode() const;
372
377 const std::map<Mode, std::string> &modes() const;
378
383 const std::string &modeName();
384
390 const std::string &modeName (Mode mode) const;
391
397
402 Pull pull() const;
403
408 static const std::map<Pull, std::string> &pulls();
409
414 const std::string &pullName();
415
421 static const std::string &pullName (Pull n);
422
427 int drive();
428
433 void setDrive (int drive);
434
439 void write (bool value);
440
445 bool read() const;
446
450 void toggle();
451
455 void release();
456
462 void waitForInterrupt (Edge edge, int timeout_ms = -1);
463
470 void waitForInterrupt (Edge edge, Event &event, int timeout_ms = -1);
471
479 void waitForInterrupt (Edge edge, int debounce_ms, Event &event, int timeout_ms = -1);
480
487 typedef void (*Isr) (Event event, void *userData);
488
495 void attachInterrupt (Isr isr, Edge edge, void *userData = nullptr);
496
504 void attachInterrupt (Isr isr, Edge edge, int debounce_ms, void *userData = nullptr);
505
510
515 bool isGpioDevEnabled() const;
516
522 bool enableGpioDev (bool enable = true);
523
528 void analogWrite (long value);
529
535
542
546 void resetDac();
547
552 long long id() const;
553
559
564 Gpio *gpio() const;
565
570 bool isDebug() const;
571
576 void setDebug (bool enable);
577
598 friend std::ostream &operator<< (std::ostream &os, const Pin &p);
599
605 Pin (Connector *parent, const Descriptor *desc);
606
610 virtual ~Pin();
611
612 protected:
618
623 bool open();
624
628 void close();
629
630 protected:
635 class Private;
636
642
646 std::unique_ptr<Private> d_ptr;
647
648 private:
653 };
654}
655
656/* ========================================================================== */
The Converter class provides an interface for analog-to-digital and digital-to-analog converters.
Definition converter.h:34
Abstract base class for GPIO devices.
Definition gpiodevice.h:34
Classe Gpio.
Definition gpio.h:54
Describes a pin's type, numbering, and associated names.
Definition gpiopin.h:170
void insertModeName(Mode m, const std::string &n)
Inserts a mode-name pair into the descriptor.
Number num
Pin numbering information.
Definition gpiopin.h:173
long long findId() const
Finds the unique identifier for the pin.
long long id
Unique identifier for the pin.
Definition gpiopin.h:174
bool insert()
Inserts the descriptor into the system.
bool hasModeName(Mode m, long long nameId) const
Checks if a mode name exists for a given mode and name ID.
std::map< Mode, std::string > name
Map of mode to pin name.
Definition gpiopin.h:175
Type type
Pin type.
Definition gpiopin.h:172
long long findName(const std::string &name) const
Finds the identifier for a given name.
Descriptor(long long pinId=-1, int pinRow=-1, int pinColumn=-1)
Constructs a Descriptor with optional parameters.
Holds various numbering representations for a pin.
Definition gpiopin.h:147
int mcu
MCU-specific pin number.
Definition gpiopin.h:150
int system
System pin number.
Definition gpiopin.h:151
int chip
Chip number (for multi-chip systems).
Definition gpiopin.h:154
int logical
Logical pin number.
Definition gpiopin.h:149
int row
Row position (if applicable).
Definition gpiopin.h:152
int column
Column position (if applicable).
Definition gpiopin.h:153
int offset
Offset within the chip.
Definition gpiopin.h:155
Number(int logical=-1, int mcu=-1, int system=-1, int row=-1, int column=-1, int chip=-1, int offset=-1)
Constructs a Number object with optional parameters.
Definition gpiopin.h:159
Represents a SPI chip select configuration for a pin.
Definition gpiopin.h:216
Pin::Mode mode
Pin mode for SPI.
Definition gpiopin.h:221
int cs
Chip select number.
Definition gpiopin.h:220
long long pin
Pin identifier.
Definition gpiopin.h:218
int bus
SPI bus number.
Definition gpiopin.h:219
Represents a general-purpose input/output (GPIO) pin and its configuration.
Definition gpiopin.h:68
static const std::map< Pull, std::string > & pulls()
Returns the map of available pull configurations and their names.
Numbering
Enumerates the possible pin numbering schemes.
Definition gpiopin.h:121
@ NumberingUnknown
Unknown or uninitialized numbering.
Definition gpiopin.h:125
@ NumberingSystem
System numbering.
Definition gpiopin.h:124
@ NumberingLogical
Logical numbering.
Definition gpiopin.h:122
@ NumberingMcu
MCU-specific numbering.
Definition gpiopin.h:123
Connector * connector() const
Returns the parent connector of the pin.
static const std::map< Numbering, std::string > & numberings()
Returns the map of available numbering schemes and their names.
int mcuNumber() const
Returns the MCU-specific pin number.
std::unique_ptr< Private > d_ptr
Unique pointer to the private implementation.
Definition gpiopin.h:646
int drive()
Returns the drive strength of the pin.
bool read() const
Reads the digital value from the pin.
static const std::string & pullName(Pull n)
Returns the name of a specific pull configuration.
void release()
Releases the pin, closing any open resources.
int chipOffset() const
Returns the offset within the chip.
const std::string & pullName()
Returns the name of the current pull configuration.
void waitForInterrupt(Edge edge, Event &event, int timeout_ms=-1)
Waits for an interrupt event and returns the event data.
friend std::ostream & operator<<(std::ostream &os, const Pin &p)
Overloaded output stream operator for Pin.
int physicalNumber() const
Returns the physical pin number.
int row() const
Returns the row position of the pin.
bool isDebug() const
Checks if debug mode is enabled for the pin.
void write(bool value)
Writes a digital value to the pin.
Mode mode() const
Returns the current mode of the pin.
const std::string & modeName(Mode mode) const
Returns the name of a specific mode.
void setDebug(bool enable)
Enables or disables debug mode for the pin.
void waitForInterrupt(Edge edge, int debounce_ms, Event &event, int timeout_ms=-1)
Waits for an interrupt event with debounce and returns the event data.
static const std::map< Type, std::string > & types()
Returns the map of available pin types and their names.
static const std::string & numberingName(Numbering n)
Returns the name of a numbering scheme.
Type
Enumerates the possible types of pins.
Definition gpiopin.h:132
@ TypeSound
Sound pin.
Definition gpiopin.h:136
@ TypeUnknown
Unknown or uninitialized type.
Definition gpiopin.h:140
@ TypeNet
Network pin.
Definition gpiopin.h:139
@ TypeNotConnected
Not connected.
Definition gpiopin.h:138
@ TypeVideo
Video pin.
Definition gpiopin.h:137
@ TypePower
Power pin.
Definition gpiopin.h:134
@ TypeGpio
General-purpose I/O pin.
Definition gpiopin.h:133
@ TypeUsb
USB pin.
Definition gpiopin.h:135
int systemNumber() const
Returns the system pin number.
void resetDac()
Resets the DAC converter for the pin.
bool isGpioDevEnabled() const
Checks if the GPIO device is enabled.
int logicalNumber() const
Returns the logical pin number.
Type type() const
Returns the type of the pin.
Pin(Connector *parent, const Descriptor *desc)
Constructs a Pin object.
const std::map< Mode, std::string > & modes() const
Returns the map of available modes and their names.
bool enableGpioDev(bool enable=true)
Enables or disables the GPIO device.
int number(Numbering numbering) const
Returns the pin number according to the specified numbering scheme.
int column() const
Returns the column position of the pin.
void setMode(Mode mode)
Sets the mode of the pin.
GpioDevice * device() const
Returns the underlying GPIO device.
virtual ~Pin()
Destructor for the Pin object.
Pin(Private &dd)
Constructs a Pin object from a Private data reference.
Pull pull() const
Returns the current pull resistor configuration.
void attachInterrupt(Isr isr, Edge edge, void *userData=nullptr)
Attaches an interrupt handler to the pin.
Gpio * gpio() const
Returns the GPIO object associated with the pin.
AccessLayer accessLayer() const
Returns the access layer used by the pin.
long long id() const
Returns the unique identifier of the pin.
bool open()
Opens the pin for use.
void close()
Closes the pin and releases resources.
unsigned int flags() const
Gets the capability flags of the GPIO Pin.
void analogWrite(long value)
Writes an analog value to the pin (PWM or DAC).
Converter * dac()
Returns the DAC converter associated with the pin.
const std::string & modeName()
Returns the name of the current mode.
const std::string & typeName() const
Returns the name of the pin type.
void attachInterrupt(Isr isr, Edge edge, int debounce_ms, void *userData=nullptr)
Attaches an interrupt handler with debounce.
void detachInterrupt()
Detaches the interrupt handler from the pin.
void(* Isr)(Event event, void *userData)
Interrupt service routine callback type.
Definition gpiopin.h:487
void toggle()
Toggles the digital output value of the pin.
Pull
Enumerates the possible pull resistor configurations.
Definition gpiopin.h:98
@ PullDown
Pull-down resistor enabled.
Definition gpiopin.h:100
@ PullOff
No pull resistor.
Definition gpiopin.h:99
@ PullUp
Pull-up resistor enabled.
Definition gpiopin.h:101
@ PullUnknown
Unknown or uninitialized pull configuration.
Definition gpiopin.h:102
void setDrive(int drive)
Sets the drive strength of the pin.
void setPull(Pull pull)
Sets the pull resistor configuration.
Mode
Enumerates the possible modes for a pin.
Definition gpiopin.h:76
@ ModeAlt2
Alternate function 2.
Definition gpiopin.h:83
@ ModePwm
Pin is configured for PWM output.
Definition gpiopin.h:80
@ ModeInput
Pin is configured as input.
Definition gpiopin.h:77
@ ModeOutput
Pin is configured as output.
Definition gpiopin.h:78
@ ModeAlt9
Alternate function 9.
Definition gpiopin.h:90
@ ModeAlt8
Alternate function 8.
Definition gpiopin.h:89
@ ModeAlt1
Alternate function 1.
Definition gpiopin.h:82
@ ModeAlt5
Alternate function 5.
Definition gpiopin.h:86
@ ModeAlt6
Alternate function 6.
Definition gpiopin.h:87
@ ModeDisabled
Pin is disabled.
Definition gpiopin.h:79
@ ModeAlt3
Alternate function 3.
Definition gpiopin.h:84
@ ModeAlt4
Alternate function 4.
Definition gpiopin.h:85
@ ModeAlt7
Alternate function 7.
Definition gpiopin.h:88
@ ModeUnknown
Unknown or uninitialized mode.
Definition gpiopin.h:91
@ ModeAlt0
Alternate function 0.
Definition gpiopin.h:81
Edge
Enumerates the possible edge detection types for interrupts.
Definition gpiopin.h:109
@ EdgeUnknown
Unknown or uninitialized edge type.
Definition gpiopin.h:114
@ EdgeFalling
Detect falling edge.
Definition gpiopin.h:112
@ EdgeNone
No edge detection.
Definition gpiopin.h:110
@ EdgeBoth
Detect both rising and falling edges.
Definition gpiopin.h:113
@ EdgeRising
Detect rising edge.
Definition gpiopin.h:111
bool isOpen() const
Checks if the pin is open.
void waitForInterrupt(Edge edge, int timeout_ms=-1)
Waits for an interrupt event on the pin.
const std::string & name(Mode mode) const
Returns the name of the pin for a specific mode.
int chipNumber() const
Returns the chip number for the pin.
bool setDac(Converter *dac)
Sets the DAC converter for the pin.
const std::string & name() const
Returns the default name of the pin.
static const std::string & typeName(Type t)
Returns the name of a given pin type.
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
@ AccessLayerGpioDev
Definition gpiopin.h:52
@ AccessLayerSysFs
Definition gpiopin.h:51
@ AccessLayerUnk
Definition gpiopin.h:54
@ AccessLayerAuto
Definition gpiopin.h:49
@ AccessLayerAll
Definition gpiopin.h:53
@ AccessLayerIoMap
Definition gpiopin.h:50
The actual event being pushed to userspace.
Definition gpio2.h:497