| 1 | #!/usr/bin/perl | ||
| 2 | 1 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 3 | 0 | ▶ | use Device::I2C; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 4 | use IO::Handle; | ||
| 5 | use Fcntl; | ||
| 6 | |||
| 7 | package PINE64::PCF8574; | ||
| 8 | |||
| 9 | our $VERSION = "0.101"; | ||
| 10 | |||
| 11 | #global vars | ||
| 12 | my ($i2cbus, $addr, $gpext, $debug); | ||
| 13 | my $gpregval = 0; #init gpio register value to 0 | ||
| 14 | |||
| 15 | my @pin_nums = (1,2,4,8,16,32,64,128); | ||
| 16 | |||
| 17 | sub new{ | ||
| 18 | my $class = shift; | ||
| 19 | my $self = bless {}, $class; | ||
| 20 | |||
| 21 | #first arg is device address | ||
| 22 | $addr = $_[0]; | ||
| 23 | |||
| 24 | #second optional turn on debug | ||
| 25 | $debug = $_[1]; | ||
| 26 | |||
| 27 | #third arg i2c bus; optional | ||
| 28 | $i2cbus = $_[2]; | ||
| 29 | |||
| 30 | if($i2cbus eq ''){ | ||
| 31 | $i2cbus = '/dev/i2c-1'; | ||
| 32 | }#end if | ||
| 33 | |||
| 34 | $gpext = Device::I2C->new($i2cbus, "r+"); | ||
| 35 | |||
| 36 | #init i2c device | ||
| 37 | $gpext->checkDevice($addr); | ||
| 38 | $gpext->selectDevice($addr); | ||
| 39 | |||
| 40 | #init gp register val to all off | ||
| 41 | $gpregval = 255; | ||
| 42 | |||
| 43 | print "successfully created pcf8574 object....\n"; | ||
| 44 | |||
| 45 | #set gpext to 0x00 | ||
| 46 | $gpext->writeByte(0xff); | ||
| 47 | |||
| 48 | return $self; | ||
| 49 | }#end new: | ||
| 50 | |||
| 51 | sub write_pin{ | ||
| 52 | my $ind = $_[1]; | ||
| 53 | my $iox = $pin_nums[$ind]; | ||
| 54 | |||
| 55 | #1 or 0 | ||
| 56 | my $val = $_[2]; | ||
| 57 | |||
| 58 | #see current pin states | ||
| 59 | my $regval = $gpext->readByte(); | ||
| 60 | #my $regval = `i2cget -y 1 0x20`; | ||
| 61 | #print "regval: $regval\n"; | ||
| 62 | my $binout = sprintf("%08b", $regval); | ||
| 63 | my @pinvals = split(//, $binout); | ||
| 64 | @pinvals = reverse(@pinvals); | ||
| 65 | |||
| 66 | if($debug == 1){ | ||
| 67 | print "@pinvals \n"; | ||
| 68 | }#end if | ||
| 69 | |||
| 70 | my $already_high = $pinvals[$ind]; | ||
| 71 | |||
| 72 | if($val == 1 && $already_high == 0){ | ||
| 73 | $gpregval+=$iox; | ||
| 74 | }#end if | ||
| 75 | if($val == 0 && $already_high == 1){ | ||
| 76 | $gpregval-=$iox; | ||
| 77 | }#end if | ||
| 78 | |||
| 79 | $gpext->writeByte($gpregval); | ||
| 80 | |||
| 81 | }#end write_pin | ||
| 82 | |||
| 83 | sub read_pin{ | ||
| 84 | my $ind = $_[1]; | ||
| 85 | my $iox = $pin_nums[$ind]; | ||
| 86 | my $pinval = 0; | ||
| 87 | |||
| 88 | #read GPIO register | ||
| 89 | my $regval = $gpext->readByte(); | ||
| 90 | |||
| 91 | #ensure 8 binary places are displayed | ||
| 92 | my $binout = sprintf("%08b", $regval); | ||
| 93 | |||
| 94 | #parse eight binary digits into an array | ||
| 95 | my @pinvals = split(//, $binout); | ||
| 96 | |||
| 97 | #reverse array to match pin #'s | ||
| 98 | @pinvals = reverse(@pinvals); | ||
| 99 | |||
| 100 | if($debug == 1){ | ||
| 101 | print "@pinvals \n"; | ||
| 102 | }#end if | ||
| 103 | |||
| 104 | #value of pin is index of $pinvals | ||
| 105 | $pinval = $pinvals[$ind]; | ||
| 106 | |||
| 107 | return $pinval; | ||
| 108 | }#end read_pin | ||
| 109 | |||
| 110 | 1; | ||
| 111 | |||
| 112 | __END__ | ||