| 1 | #!/usr/bin/perl | ||||||||||
| 2 | 1 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 3 | 1 | ▶ | use Device::I2C; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 1 | ▶ | use IO::Handle; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | 1 | ▶ | use Fcntl; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | |||||||||||
| 7 | package PINE64::MCP23008; | ||||||||||
| 8 | |||||||||||
| 9 | 1 | our $VERSION = '0.9'; | |||||||||
| 10 | |||||||||||
| 11 | #global vars | ||||||||||
| 12 | 1 | my ($i2cbus, $addr, $gpext); | |||||||||
| 13 | 1 | my $gpregval = 0; #init gpio register value to 0 | |||||||||
| 14 | |||||||||||
| 15 | 1 | my @pin_nums = (1,2,4,8,16,32,64,128); | |||||||||
| 16 | |||||||||||
| 17 | sub new{ | ||||||||||
| 18 | 0 | ▶ | my $class = shift; | ||||||||
|
Subroutine
new: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||
| 19 | 0 | my $self = bless {}, $class; | |||||||||
| 20 | |||||||||||
| 21 | #first arg is device address | ||||||||||
| 22 | 0 | $addr = $_[0]; | |||||||||
| 23 | |||||||||||
| 24 | #second arg i2c bus; optional | ||||||||||
| 25 | 0 | $i2cbus = $_[1]; | |||||||||
| 26 | |||||||||||
| 27 | 0 | ▶ | if($i2cbus eq ''){ | ||||||||
|
Branch: 1 branch
| |||||||||||
| 28 | 0 | $i2cbus = '/dev/i2c-0'; | |||||||||
| 29 | }#end if | ||||||||||
| 30 | |||||||||||
| 31 | 0 | $gpext = Device::I2C->new($i2cbus, "r+"); | |||||||||
| 32 | |||||||||||
| 33 | #init i2c device | ||||||||||
| 34 | 0 | $gpext->checkDevice($addr); | |||||||||
| 35 | 0 | $gpext->selectDevice($addr); | |||||||||
| 36 | |||||||||||
| 37 | #init gp register val to all off | ||||||||||
| 38 | 0 | $gpregval = 0; | |||||||||
| 39 | |||||||||||
| 40 | 0 | return $self; | |||||||||
| 41 | }#end new | ||||||||||
| 42 | |||||||||||
| 43 | sub set_direction{ | ||||||||||
| 44 | #sets value of the IO direction register | ||||||||||
| 45 | #ie 255 makes all input; 0 makes all output | ||||||||||
| 46 | |||||||||||
| 47 | 0 | ▶ | my $direction = $_[1]; | ||||||||
|
Subroutine
set_direction: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||
| 48 | 0 | $gpext->writeByteData(0x00, $direction); | |||||||||
| 49 | }#end set_direction | ||||||||||
| 50 | |||||||||||
| 51 | sub enable_pullup{ | ||||||||||
| 52 | #when a pin is configured as an input | ||||||||||
| 53 | #you can enable internal 100K pull-up | ||||||||||
| 54 | #resistors by writing to the GPPU register | ||||||||||
| 55 | #0-255 are valid values: 0 - all disabled | ||||||||||
| 56 | #255 - all enabled | ||||||||||
| 57 | |||||||||||
| 58 | 0 | ▶ | my $en_gppu = $_[1]; | ||||||||
|
Subroutine
enable_pullup: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||
| 59 | 0 | $gpext->writeByteData(0x06, $en_gppu); | |||||||||
| 60 | }#end enable_pullup | ||||||||||
| 61 | |||||||||||
| 62 | sub set_polarity{ | ||||||||||
| 63 | #sets the polarity of the gpio pins; | ||||||||||
| 64 | #0 is normal polarity | ||||||||||
| 65 | #255 is all pins reversed | ||||||||||
| 66 | |||||||||||
| 67 | 0 | ▶ | my $io_pol = $_[1]; | ||||||||
|
Subroutine
set_polarity: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||
| 68 | 0 | $gpext->writeByteData(0x01, $io_pol); | |||||||||
| 69 | }#end set_polarity | ||||||||||
| 70 | |||||||||||
| 71 | sub write_pin{ | ||||||||||
| 72 | 0 | ▶ | my $ind = $_[1]; | ||||||||
|
Subroutine
write_pin: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||
| 73 | 0 | my $iox = $pin_nums[$ind]; | |||||||||
| 74 | |||||||||||
| 75 | #1 or 0 | ||||||||||
| 76 | 0 | my $val = $_[2]; | |||||||||
| 77 | |||||||||||
| 78 | 0 | ▶ | if($val == 1){ | ||||||||
|
Branch: 1 branch
| |||||||||||
| 79 | 0 | $gpregval+=$iox; | |||||||||
| 80 | }#end if | ||||||||||
| 81 | 0 | ▶ | if($val == 0){ | ||||||||
|
Branch: 1 branch
| |||||||||||
| 82 | 0 | $gpregval-=$iox; | |||||||||
| 83 | }#end if | ||||||||||
| 84 | |||||||||||
| 85 | 0 | $gpext->writeByteData(0x09, $gpregval); | |||||||||
| 86 | }#end write_pin | ||||||||||
| 87 | |||||||||||
| 88 | sub read_pin{ | ||||||||||
| 89 | 0 | ▶ | my $ind = $_[1]; | ||||||||
|
Subroutine
read_pin: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||
| 90 | 0 | my $iox = $pin_nums[$ind]; | |||||||||
| 91 | 0 | my $pinval = 0; | |||||||||
| 92 | |||||||||||
| 93 | #read GPIO register | ||||||||||
| 94 | 0 | my $regval = $gpext->readByteData(0x09); | |||||||||
| 95 | |||||||||||
| 96 | #ensure 8 binary places are displayed | ||||||||||
| 97 | 0 | my $binout = sprintf("%08b", $regval); | |||||||||
| 98 | |||||||||||
| 99 | #parse eight binary digits into an array | ||||||||||
| 100 | 0 | my @pinvals = split(//, $binout); | |||||||||
| 101 | |||||||||||
| 102 | #reverse array to match pin #'s | ||||||||||
| 103 | 0 | @pinvals = reverse(@pinvals); | |||||||||
| 104 | |||||||||||
| 105 | #value of pin is index of $pinvals | ||||||||||
| 106 | 0 | $pinval = $pinvals[$ind]; | |||||||||
| 107 | |||||||||||
| 108 | 0 | return $pinval; | |||||||||
| 109 | }#end read_pin | ||||||||||
| 110 | |||||||||||
| 111 | 1 | 1; | |||||||||
| 112 | __END__ | ||||||||||