HiPi
Perl Modules for Raspberry Pi
Version 0.91 - released 25 February 2024

HiPi::Device::GPIO

Provides access to the GPIO pins using the '/sys/class/gpio' file interface.

Module HiPi::GPIO offers access to the GPIO using the /dev/gpiomem(0) interface and is the preferred option unless you wish to experiment capturing pin interrupts. ( rather than polling for pin value ).

Use this module if you wish to experiment capturing pin interrupts.

Example

Methods

Create a new instance of the HiPi::Device::GPIO class.

use HiPi::Device::GPIO;
my $device =  HiPi::Device::GPIO->new;

Export the /sys/class/gpio files for the $gpio. All the functions that access pins call this method internally if the pin has not already been exported.

$device->export_pin( RPI_PIN_11 );

Remove the exported files from the /sys/class/gpio file interface for this $gpio

$device->unexport_pin( RPI_PIN_11 );

Remove the exported files from the /sys/class/gpio file interface for all exported gpio pins. Returns the number of pins unexported.

my $pincount = $device->unexport_all();

Return true ( 1 ) if pin has been exported

my $is_exported = $device->pin_status( RPI_PIN_11 );

Exports the /sys/class/gpio files for the $gpio and returns a new instance of the HiPi::Device::GPIO::Pin class.

use HiPi qw( :rpi );
use HiPi::Device::GPIO;
my $device =  HiPi::Device::GPIO->new;
my $pin = $device->get_pin( RPI_PIN_11 );

Set the pin level high or low. The method 'pin_write' is an alias for this method. Returns the value set.

use HiPi qw( :rpi );
...
$device->set_pin_level( RPI_PIN_36, RPI_HIGH );
$device->set_pin_level( RPI_PIN_36, 1 );
$device->set_pin_level( RPI_PIN_36, RPI_LOW );
$device->set_pin_level( RPI_PIN_36, 0 );
$device->pin_write( RPI_PIN_36, RPI_HIGH )
$device->pin_write( RPI_PIN_36, 1 )

Get the level ( RPI_HIGH / RPI_LOW, 1 / 0 ) of a gpio pin. The method 'pin_read' is an alias for this method.

use HiPi qw( :rpi );
...
my $value = $device->get_pin_level( RPI_PIN_36 );
$value = $device->get_pin_level( 16 );
$value = $device->pin_read( RPI_PIN_36 );

Set the pint mode. Will accept the following two constants:

RPI_MODE_INPUT
RPI_MODE_OUTPUT

Returns the mode set.

use HiPi qw( :rpi );
...
$device->set_pin_mode( RPI_PIN_36, RPI_MODE_OUTPUT );

Get the current pin mode. Returns one of the constants:

RPI_MODE_INPUT
RPI_MODE_OUTPUT
use HiPi qw( :rpi );
...
my $mode = $device->get_pin_mode( RPI_PIN_36 );

Set or remove the internal pull up or pull down resistor on a gpio pin. Accepts constants:

RPI_PUD_OFF
RPI_PUD_DOWN
RPI_PUD_UP
use HiPi qw( :rpi );
...
$device->set_pin_pud( RPI_PIN_36, RPI_PUD_UP );

On the BCM2711 based Raspberry Pi 4 you can retrieve the current pull up / down setting for a gpio pin. Returns one of the constants:

RPI_PUD_OFF
RPI_PUD_DOWN
RPI_PUD_UP
RPI_PUD_UNSET

For BCM2835/6/7 based Raspberry Pi's this method will always return RPI_PUD_UNSET

use HiPi qw( :rpi );
...
my $setting = $device->get_pin_pud( RPI_PIN_36 );

Returns descriptive text for the current pin function.

use HiPi qw( :rpi );
my $description = $device->get_pin_function( RPI_PIN_36 );

Set the active low status of the pin. $val can be 1 or 0. If a pin is set 'active low' then all functions return the inverse of the pin's value.

$device->set_pin_active_low( RPI_PIN_36, 1 );

Get the active low status for the $gpio pin.

use HiPi qw( :rpi );
my $state = $device->get_pin_activelow(RPII_PIN_36 );

Set the pin state change or changes that you want to produce interrupts. Can be one of the following constants:

RPI_INT_NONE       
RPI_INT_FALL           
RPI_INT_RISE        
RPI_INT_BOTH
use HiPi qw( :rpi );
$device->set_pin_interrupt( RPI_PIN_36, RPI_INT_RISE );

Returns the $gpio pin interrupt setting. Can be one of the following constants:

RPI_INT_NONE       
RPI_INT_FALL           
RPI_INT_RISE        
RPI_INT_BOTH
use HiPi qw( :rpi );
my $waching = $device->get_pin_interrupt( RPI_PIN_36 );

To watch for interrupts you must epoll or poll the filepath returned by this method.

use HiPi qw( :rpi );
my $filepath = $device->get_pin_interrupt_filepath( RPI_PIN_36 );

Module Use Examples

use HiPi qw( :rpi );
use HiPi::Device::GPIO;
my $device = HiPi::GPIO->new;

# set GPIO_17 as an output pin
$device->set_pin_mode( RPI_PIN_11, RPI_MODE_OUPUT );
# or without pin constant
$device->set_pin_mode( 17, RPI_MODE_OUPUT );

# set GPIO_17 high
$device->set_pin_level( RPI_PIN_11, RPI_HIGH );
# or without constants
$device->set_pin_level( 17, 1 );

# set GPIO_17 low
$device->set_pin_level( RPI_PIN_11, RPI_LOW );
# or without constants
$device->set_pin_level( 17, 0 );

# set GPIO_17 as an input pin
$device->set_pin_mode( RPI_PIN_11, RPI_MODE_INPUT );

# activate pull up resistor on GPIO_17
$device->set_pin_pud( RPI_PIN_11, RPI_PUD_UP );

# read value of GPIO_17
my $val = $device->get_pin_level( RPI_PIN_11 );

# activate pull down resistor on GPIO_17
$device->set_pin_pud( RPI_PIN_11, RPI_PUD_DOWN );

# remove pull up / down resistor from GPIO_17
$device->set_pin_pud( RPI_PIN_11, RPI_PUD_OFF );