File Coverage

blib/lib/PINE64/MCP23008.pm
Criterion Covered Total %
statement 12 46 26.0
branch 0 6 0.0
condition n/a
subroutine 4 10 40.0
pod 6 6 100.0
total 22 68 32.3


line stmt bran cond sub pod time code
1             #!/usr/bin/perl
2 1     1   67173 use strict;
  1         3  
  1         29  
3 1     1   481 use Device::I2C;
  1         11042  
  1         45  
4 1     1   6 use IO::Handle;
  1         2  
  1         33  
5 1     1   5 use Fcntl;
  1         1  
  1         637  
6              
7             package PINE64::MCP23008;
8              
9             our $VERSION = '0.9';
10              
11             #global vars
12             my ($i2cbus, $addr, $gpext);
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 0     0 1   my $class = shift;
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 0         if($i2cbus eq ''){
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     0 1   my $direction = $_[1];
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     0 1   my $en_gppu = $_[1];
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     0 1   my $io_pol = $_[1];
68 0           $gpext->writeByteData(0x01, $io_pol);
69             }#end set_polarity
70              
71             sub write_pin{
72 0     0 1   my $ind = $_[1];
73 0           my $iox = $pin_nums[$ind];
74              
75             #1 or 0
76 0           my $val = $_[2];
77              
78 0 0         if($val == 1){
79 0           $gpregval+=$iox;
80             }#end if
81 0 0         if($val == 0){
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     0 1   my $ind = $_[1];
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;
112             __END__