HiPi
Perl Modules for Raspberry Pi
Version 0.92 - released 28 March 2024

HiPi::Interface::PCF8574

This module provides an interface to the PCF8574 8 bit GPIO extender with I2C interface.

It uses HiPi::Device::I2C as a backend.

Methods

Create a new instance of the module class.

use HiPi qw( :rpi );
use HiPi::Interface::PCF8574;
my $pcf = HiPi::Interface::PCF8574->new(
    devicename => '/dev/i2c-1',
    address    => 0x3f,
);

Read an array of 8 values ( 0 or 1 ) from the device.

bits are populated according to bit numbers as described in
the PCF8574 documentation

$bits[0] is populated from port 0 value
$bits[7] is populated from port 7 value

Examples:

use HiPi qw( :rpi );
use HiPi::Interface::PCF8574;
my $pcf = HiPi::Interface::PCF8574->new(
    devicename => '/dev/i2c-1',
    address    => 0x27,
);<

# get the value ( 1 or 0 ) for port 3

my @bits = $pcf->read_bits();
my $value_port_3 = $bits[3];

Write an array of 8 values ( 0 or 1 ) to the device.

$bits[0] is written to port 0 of the device
$bits[7] is written to port 7 of the device

Examples :

use HiPi qw( :rpi );
my $pcf = HiPi::Interface::PCF8574->new(
    address    => 0x3f,
);

....

# set all ports as output
$pcf->write_bits(0,0,0,0,0,0,0,0);

# set all ports high / input
$pcf->write_bits(1,1,1,1,1,1,1,1);

Read a byte representing the values of the eight ports.

It is often more convenient to use read_bits which calls read_byte internally and separates the returned values into ordered bit values.

my @bytes = $pcf->read_byte();

Write a byte representing the required values on the device ports

$byte is the data to write

It is often more convenient to use write_bits which calls write_byte internally.

$pcf->write_byte( $byte );

Get the value of port number $port

my $bool = $pcf->get_port( 5 );

Set the port number $port with value $value.

$pcf->set_port( 5, $bool );