File Coverage

blib/lib/PINE64/MCP23008.pm
Criterion Covered Total %
statement 12 53 22.6
branch 0 6 0.0
condition 0 6 0.0
subroutine 4 11 36.3
pod 7 7 100.0
total 23 83 27.7


line stmt bran cond sub pod time code
1             #!/usr/bin/perl
2 1     1   56177 use strict;
  1         2  
  1         25  
3 1     1   415 use Device::I2C;
  1         9328  
  1         43  
4 1     1   7 use IO::Handle;
  1         3  
  1         28  
5 1     1   4 use Fcntl;
  1         2  
  1         590  
6              
7             package PINE64::MCP23008;
8              
9             our $VERSION = '0.901';
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             #check 0x09 register to see if
79             #pin already high; if so, do nothing
80 0           my $regval = $gpext->readByteData(0x09);
81 0           my $binout = sprintf("%08b", $regval);
82 0           my @pinvals = split(//, $binout);
83 0           @pinvals = reverse(@pinvals);
84 0           my $already_high = $pinvals[$ind];
85              
86 0 0 0       if($val == 1 && $already_high == 0){
87 0           $gpregval+=$iox;
88             }#end if
89 0 0 0       if($val == 0 && $already_high == 1){
90 0           $gpregval-=$iox;
91             }#end if
92              
93 0           $gpext->writeByteData(0x09, $gpregval);
94             }#end write_pin
95              
96             sub read_pin{
97 0     0 1   my $ind = $_[1];
98 0           my $iox = $pin_nums[$ind];
99 0           my $pinval = 0;
100              
101             #read GPIO register
102 0           my $regval = $gpext->readByteData(0x09);
103              
104             #ensure 8 binary places are displayed
105 0           my $binout = sprintf("%08b", $regval);
106              
107             #parse eight binary digits into an array
108 0           my @pinvals = split(//, $binout);
109            
110             #reverse array to match pin #'s
111 0           @pinvals = reverse(@pinvals);
112              
113             #value of pin is index of $pinvals
114 0           $pinval = $pinvals[$ind];
115              
116 0           return $pinval;
117             }#end read_pin
118              
119             sub read_gpio_register{
120             #read GPIO register
121 0     0 1   my $regval = $gpext->readByteData(0x09);
122              
123 0           return $regval;
124             }#end read_gpio_register
125              
126             1;
127             __END__