PiImageStreamer
Loading...
Searching...
No Matches
SWE4211RPi::GPIO Class Referenceabstract

Abstract GPIO wrapper class for usage with C++. Derived classes implement the pin access for a specific library (wiringPi or lgpio). More...

#include <GPIO.h>

Inheritance diagram for SWE4211RPi::GPIO:
Collaboration diagram for SWE4211RPi::GPIO:

Public Types

enum  DIRECTION { GPIO_IN , GPIO_OUT }
 
enum  VALUE { GPIO_LOW = 0 , GPIO_HIGH = 1 }
 
enum  EDGE { GPIO_NONE , GPIO_RISING , GPIO_FALLING , GPIO_BOTH }
 

Public Member Functions

 GPIO ()
 
virtual ~GPIO ()
 
virtual void setValue (GPIO::VALUE value)=0
 
virtual GPIO::VALUE getValue ()=0
 
virtual void enableEdgeInterrupt (EDGE edgeType)=0
 
virtual void disableEdgeInterrupt ()=0
 
virtual int waitForEdge (int maxWait)
 
virtual struct timespec getRisingISRTimestamp ()
 
virtual struct timespec getFallingISRTimestamp ()
 
virtual void setCallbackMethod (CallbackType methodCallback)
 
virtual void callBackThreadHandler ()
 
virtual void handleInterruptDrivenGPIOPin ()=0
 
virtual int getPinNumber ()
 

Static Public Member Functions

static GPIO & getInstance (int number, DIRECTION dir, VALUE vale=GPIO_LOW)
 
static void freeInstance (GPIO &instance)
 

Static Public Attributes

static GPIO * gpio_instances [NUMBER_OF_GPIO_PINS+1]
 
static GenericThreadInfo * threadInstances [NUMBER_OF_GPIO_PINS+1]
 

Protected Attributes

int number =0
 
int instanceCount = 0
 
std::mutex mtx
 
std::condition_variable cv
 
std::condition_variable ccv
 
CallbackType methodCallback =nullptr
 
struct timespec isrRisingTimestamp
 
struct timespec isrFallingTimestamp
 

Detailed Description

Abstract GPIO wrapper class for usage with C++. Derived classes implement the pin access for a specific library (wiringPi or lgpio).

Member Enumeration Documentation

◆ DIRECTION

This enumeration sets up the pin to either be an input or an output pin.

◆ EDGE

This enumeration is used to define whether an ISR is fired based on transitions in pin values.

◆ VALUE

This enumeration defines the values on the pin as being either high or low.

Constructor & Destructor Documentation

◆ GPIO()

SWE4211RPi::GPIO::GPIO ( )

This is the default constructor. Pin setup is done by the derived class constructor.

◆ ~GPIO()

SWE4211RPi::GPIO::~GPIO ( )
virtual

This is the destructor. Any pin cleanup is done by the derived class destructor.

Member Function Documentation

◆ callBackThreadHandler()

void SWE4211RPi::GPIO::callBackThreadHandler ( )
virtual

This method is the body of the callback thread started by setCallbackMethod(). It should never be invoked from outside of the class. In essence, it will loop so long as the callback method is not NULL, waiting for an edge and then invoking the callback method.

Here is the caller graph for this function:

◆ disableEdgeInterrupt()

virtual void SWE4211RPi::GPIO::disableEdgeInterrupt ( )
pure virtual

This method will disable the edge interrupt for the given pin.

Implemented in SWE4211RPi::WiringPiGPIO.

◆ enableEdgeInterrupt()

virtual void SWE4211RPi::GPIO::enableEdgeInterrupt ( EDGE  edgeType)
pure virtual

This method will enable an interrupt to be processed based upon edges. In doing this, callbacks can be made based upon the GPIO pin changing state and methods can block for IO purposes.

Parameters
edgeTypeThis is the edge type that is to be processed as an interrupt.

Implemented in SWE4211RPi::WiringPiGPIO.

◆ freeInstance()

void SWE4211RPi::GPIO::freeInstance ( GPIO &  instance)
static

This method releases a reference obtained from getInstance(). The behavior when the last reference is released depends on the library selected in GPIOcfg.h.

Parameters
instanceThis is the instance that is to be released.

This method releases a reference obtained from getInstance(). With lgpio, the reference count is decremented and the instance is deleted when it reaches zero. With wiringPi, this method currently does nothing.

Parameters
instanceThis is the instance that is to be released.

◆ getFallingISRTimestamp()

struct timespec SWE4211RPi::GPIO::getFallingISRTimestamp ( )
virtual

This method will obtain the timestamp from the last time the pin transitioned low.

Returns
The timestamp of the transition will be returned.

◆ getInstance()

GPIO & SWE4211RPi::GPIO::getInstance ( int  number,
GPIO::DIRECTION  dir,
GPIO::VALUE  value = GPIO_LOW 
)
static

This method returns the GPIO instance for the given pin. If an instance already exists for the pin, its reference count is incremented and the existing instance is returned (the dir and value parameters are ignored in that case). Otherwise a new instance of the library-specific class selected in GPIOcfg.h is created.

Parameters
numberThis is the Raspberry Pi GPIO number, from 0 to NUMBER_OF_GPIO_PINS.
dirThis is the direction for the pin, either input or output.
valeThis is the initial value for an output pin.
Returns
A reference to the GPIO instance for the pin.

This method returns the GPIO instance for the given pin. If an instance already exists for the pin, its reference count is incremented and the existing instance is returned (the dir and value parameters are ignored in that case). Otherwise a new instance of the library-specific class selected in GPIOcfg.h is created.

Parameters
numberThis is the Raspberry Pi GPIO number, from 0 to NUMBER_OF_GPIO_PINS.
dirThis is the direction for the pin, either input or output.
valueThis is the initial value for an output pin.
Returns
A reference to the GPIO instance for the pin.

◆ getPinNumber()

int SWE4211RPi::GPIO::getPinNumber ( )
virtual

This method will obtain the GPIO Pin number associated with this instance of the GPIO class.

Returns
The unique pin number identifying the pin will be returned.

◆ getRisingISRTimestamp()

struct timespec SWE4211RPi::GPIO::getRisingISRTimestamp ( )
virtual

This method will obtain the timestamp from the last time the pin transitioned high.

Returns
The timestamp of the transition will be returned.

◆ getValue()

virtual GPIO::VALUE SWE4211RPi::GPIO::getValue ( )
pure virtual

This method will return the current state of this GPIO pin.

Returns
The return will be high or low depending on the state of the pin.

Implemented in SWE4211RPi::WiringPiGPIO.

◆ handleInterruptDrivenGPIOPin()

virtual void SWE4211RPi::GPIO::handleInterruptDrivenGPIOPin ( )
pure virtual

This method will be invoked if an interrupt is enabled whenever a transition in pin value occurs.

Implemented in SWE4211RPi::WiringPiGPIO.

Here is the caller graph for this function:

◆ setCallbackMethod()

void SWE4211RPi::GPIO::setCallbackMethod ( CallbackType  methodCallback)
virtual

This method will set the callback handler. If this is NULL, no method will be invoked. However, if it is non-NULL and the class triggers on edges, the method described here will be called when an edge transition occurs. The first time a non-NULL callback is set, a new thread is started to make the callbacks. Setting the callback back to NULL causes that thread to exit after the next edge.

Parameters
methodCallbackThis is a function pointer that is to be used on the callback.

This method will set the callback handler. If this is NULL, no method will be invoked. However, if it is non-NULL and the class triggers on edges, the method described here will be called when an edge transition occurs.

Parameters
methodCallbackThis is a function pointer that is to be used on the callback.
Here is the call graph for this function:

◆ setValue()

virtual void SWE4211RPi::GPIO::setValue ( GPIO::VALUE  value)
pure virtual

This method will drive an output pin to the appropriate value.

Parameters
valueThis is the value that is to be written to the pin.

Implemented in SWE4211RPi::WiringPiGPIO.

Here is the caller graph for this function:

◆ waitForEdge()

int SWE4211RPi::GPIO::waitForEdge ( int  maxWait)
virtual

This method will block the calling thread for up to maxWait ms unless an edge occurs. Note that this should only be called by one thread at a time, as multiple invocations will result in non-deterministic behavior. Edge interrupts must be enabled with enableEdgeInterrupt() for an edge to be detected.

Parameters
maxWaitThis is the maximum blocked period in ms.
Returns
0 if the edge occurred or -1 if a timeout occurred.

This method will block the calling thread for up to maxWait ms unless an edge occurs.

Parameters
maxWaitThis is the maximum blocked period in ms.
Returns
0 if the edge occurred or -1 if a timeout occurred.

Member Data Documentation

◆ ccv

std::condition_variable SWE4211RPi::GPIO::ccv
protected

This condition variable is notified on each edge. It is used to wake the callback thread so that it can invoke the callback method.

◆ cv

std::condition_variable SWE4211RPi::GPIO::cv
protected

This condition variable is notified on each edge. It is used to wake a thread blocked in waitForEdge().

◆ gpio_instances

GPIO * SWE4211RPi::GPIO::gpio_instances
static

This static variable keeps track of the GPIO instances as they are instantiated. It is used to ensure that we can reference a given instance based upon the GPIO number of the given pin.

◆ instanceCount

int SWE4211RPi::GPIO::instanceCount = 0
protected

This is the number of references to the GPIO pin that have been returned by getInstance() and not yet released with freeInstance().

◆ isrFallingTimestamp

struct timespec SWE4211RPi::GPIO::isrFallingTimestamp
protected

This is a timestamp marked when a falling edge is detected within the system, if the system is setup to handle edges.

◆ isrRisingTimestamp

struct timespec SWE4211RPi::GPIO::isrRisingTimestamp
protected

This is a timestamp marked when a rising edge is detected within the system, if the system is setup to handle edges.

◆ methodCallback

CallbackType SWE4211RPi::GPIO::methodCallback =nullptr
protected

This is a callback variable that is used to invoke a method when a transition in the GPIO value occurs.

◆ mtx

std::mutex SWE4211RPi::GPIO::mtx
protected

This is a mutex that is used to protect critical resources within this class.

◆ number

int SWE4211RPi::GPIO::number =0
protected

This is the GPIO pin number for the given pin.

◆ threadInstances

GenericThreadInfo * SWE4211RPi::GPIO::threadInstances
static

This static variable holds thread diagnostic information for the thread that services edge interrupts on each pin. An entry is created the first time an interrupt fires on the given pin, so that the interrupt thread appears in the thread diagnostic printout.

This static variable holds thread diagnostic information for the interrupt thread of each pin.


The documentation for this class was generated from the following files: