NAME
PINE64::MCP23008 - Perl interface to the MCP23008 GPIO extender. Can be used on any single board computer that has I2C capabilities.
SYNOPSIS
#--------READ PIN EXAMPLE---------------
my $gpext = PINE64::MCP23008->new(0x20);
$gpext->set_direction(255); #all input
$gpext->enable_pullup(255); #enable internal pullup resistors
$gpext->set_polarity(255); #reverse io polarity
#read pins 0 - 7
for(my $i=0;$i<8;$i++){
my $pinval = $gpext->read_pin($i);
print "pin $i:\t$pinval\n";
sleep(1);
}#end inner for
print "---------------------------\n";
#------WRITE PIN EXAMPLE---------------
$gpext->set_direction(0); #all output
$gpext->enable_pullup(0); #disable internal pullup resistors
$gpext->set_polarity(0); #normal io polarity
$gpext->write_pin(2, 1); #set gpio 2 high
METHODS
new($address, $i2cbus)
Takes hexadecimal address of the i2c gpio extender chip and optionally, a string containing the path of the i2c controller on your single board computer i.e. /dev/i2c-1. Defaults to /dev/i2c-0.
set_direction()
Takes decimal from 0-255 as an argument and writes it to the IO direction register, 0x00. For example, if you want all the pins to be output, pass 0; all input pass 255; pin 7 and pin 1 as input, all others output, pass 130 (128 + 2).
set_polarity()
Takes a decimal from 0 to 255 as an argument and writes it to the IO polarity register, 0x01. Default is normal polarity. To reverse polarity on all pins, pass 255. If just to pins 2 & 3, pass 10 (4 + 8).
enable_pullup()
Takes a decimal from 0 - 255 as an argument and writes that value to the GPPU register, 0x06. A high value for a pin (that is configured as an input) will enable the internal 100K pullup resistors. The pin will be a logical high unless pulled low externall.
This method is most useful when used on pins configured as inputs. For example, you can use with reverse polarity (see input example in the SYNOPSIS).
write_pin($pin_number, $value)
Sets GPIO $pin_number (0 - 7) to $value (0 or 1) when pin is configured as an output.
read_pin($pin_number)
Returns the value of $pin_number. As stated before, use with reversed polarity, and enable the pullup resistors (or build an external pull up resistor).
read_gpio_register()
Returns the decimal value (0-255) of the 0x09 gpio register