MCP23x17/Adapter.pm

filter: Statementstmt 100% 62 / 62 Branchbran 58.3% 7 / 12 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 10 / 10 Podpod 75.0% 3 / 4 totaltotal 93.1% 82 / 88 CC 10 SCAR 13.0
CC
10
Cov
93%
read_gpios
6.0
write_gpios
5.1
BEGIN
1.0
CRAP 3.7
↑ Summary MCP23x17.pm »
1 # You may distribute under the terms of either the GNU General Public License
2 # or the Artistic License (the same terms as Perl itself)
3 #
4 # (C) Paul Evans, 2015-2023 -- leonerd@leonerd.org.uk
5  
6 2 ▶use v5.26;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use Object::Pad 0.800;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 package Device::Chip::MCP23x17::Adapter 0.07;
11 class Device::Chip::MCP23x17::Adapter;
12 # can't 'extends Device::Chip::Adapter' because that doesn't provide a SUPER::new
13 2 ▶use base qw( Device::Chip::Adapter );
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 2 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 2 ▶use Future::AsyncAwait;
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 =head1 NAME
20  
21 C<Device::Chip::MCP23x17::Adapter> - C<Device::Chip::Adapter> over F<MCP23x17> chip
22  
23 =head1 SYNOPSIS
24  
25 use Device::Chip::MCP23S17;
26 use Future::AsyncAwait;
27  
28 my $chip = Device::Chip::MCP23S17->new;
29 await $chip->mount( Device::Chip::Adapter::...->new );
30  
31 my $adapter = $chip->as_adapter;
32  
33 my $second_chip = Device::Chip::...->new;
34 await $second_chip->mount( $adapter );
35  
36 =head1 DESCRIPTION
37  
38 This implementation of the L<Device::Chip::Adapter> API provides the C<GPIO>
39 protocol, by exposing the 16bit GPIO registers of a F<MCP23x17> chip as 16
40 named GPIO pins. It allows, for example, a second instance of some
41 L<Device::Chip> implementation that uses the GPIO protocol, to be attached via
42 the F<MCP23x17> chip.
43  
44 Instances of this class are not created directly; they are returned by
45 L<Device::Chip::MCP23x17/as_adapter>.
46  
47 =cut
48  
49 2 field $_chip :param;
50  
51 # Only supports GPIO
52 1 ▶method make_protocol_GPIO () { $self }
Subroutine make_protocol_GPIO: called CC=1 SCAR=0.0
Pod undocumented
53  
54 my %GPIOs = (
55 16 ( map { +"A$_", ( 1 << $_ ) } 0 .. 7 ),
56 2 ( map { +"B$_", ( 1 << $_ ) << 8 } 0 .. 7 ),
57 );
58  
59 1 ▶method list_gpios ()
Subroutine list_gpios: called CC=1 SCAR=0.0
Pod documented
60 {
61 1 return sort keys %GPIOs;
62 2 }
63  
64 1 ▶async method write_gpios ( $gpios )
Subroutine write_gpios: called CC=5 SCAR=16.3
Pod documented
65 1 {
66 1 my $val = 0;
67 1 my $mask = 0;
68  
69 1 foreach ( keys %$gpios ) {
70 2 ▶ my $bitmask = $GPIOs{$_} or croak "Unrecognised GPIO name $_";
Branch: 1 branch
BranchTrueFalseTotal
my $bitmask = $GPIOs{$_} or croak("Unrecognised GPIO name $_") 2 0 2
71  
72 2 ▶ $val |= $bitmask if $gpios->{$_};
Branch: 1 branch
BranchTrueFalseTotal
if $gpios->{$_} 2 0 2
73 2 $mask |= $bitmask;
74 }
75  
76 1 ▶ $mask or return;
Branch: 1 branch
BranchTrueFalseTotal
$mask or return 1 0 1
77  
78 1 await $_chip->write_gpio( $val, $mask );
79 2 }
80  
81 1 ▶async method read_gpios ( $gpios )
Subroutine read_gpios: called CC=6 SCAR=18.0
Pod documented
82 1 {
83 1 my $mask = 0;
84  
85 1 foreach ( @$gpios ) {
86 2 ▶ my $bitmask = $GPIOs{$_} or croak "Unrecognised GPIO name $_";
Branch: 1 branch
BranchTrueFalseTotal
my $bitmask = $GPIOs{$_} or croak("Unrecognised GPIO name $_") 2 0 2
87  
88 2 $mask |= $bitmask;
89 }
90  
91 1 ▶ $mask or return {};
Branch: 1 branch
BranchTrueFalseTotal
$mask or return {} 1 0 1
92  
93 1 my $bits = await $_chip->read_gpio( $mask );
94  
95 1 my %ret;
96  
97 1 ▶ $ret{$_} = $bits & $GPIOs{$_} ? 1 : 0 for @$gpios;
Branch: 1 branch
BranchTrueFalseTotal
$bits & $GPIOs{$_} ? : 1 1 2
98  
99 1 return \%ret;
100 2 }
101  
102 =head1 AUTHOR
103  
104 Paul Evans <leonerd@leonerd.org.uk>
105  
106 =cut
107  
108 2 0x55AA;
↑ Summary MCP23x17.pm »