File Coverage

blib/lib/Device/Chip/PCF857x.pm
Criterion Covered Total %
statement 68 80 85.0
branch 5 10 50.0
condition 1 2 50.0
subroutine 16 18 88.8
pod 0 4 0.0
total 90 114 78.9


line stmt bran cond sub pod time code
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, 2016-2021 -- leonerd@leonerd.org.uk
5              
6 5     5   43 use v5.26;
  5         13  
7 5     5   28 use Object::Pad 0.43;
  5         46  
  5         18  
8              
9             package Device::Chip::PCF857x 0.04;
10             class Device::Chip::PCF857x
11 1     1   569 :isa(Device::Chip);
  1         14460  
  1         31  
12              
13 5     5   1313 use constant PROTOCOL => "I2C";
  5         9  
  5         308  
14              
15 5     5   26 use Future::AsyncAwait;
  5         9  
  5         25  
16              
17             method I2C_options
18 4     4 0 1175 {
19 4         11 my %opts = @_;
20              
21 4   50     24 my $addr = $opts{addr} // 0x20;
22 4 50       15 $addr = oct $addr if $addr =~ m/^0/;
23              
24             return (
25 4         25 addr => $addr,
26             max_bitrate => 100E3,
27             );
28             }
29              
30 4         5 async method write ( $v )
  4         5  
  4         8  
31 4         13 {
32 4         20 await $self->protocol->write( pack( $self->PACKFMT, $v ) );
33 4     4 0 359 }
34              
35             async method read
36 4         16 {
37 4         28 return unpack $self->PACKFMT, await $self->protocol->read( $self->READLEN );
38 4     4 0 27095 }
39              
40             method as_adapter
41 2     2 0 281 {
42 2         17 return Device::Chip::PCF857x::_Adapter->new( chip => $self );
43             }
44              
45             class # hide from indexer
46             Device::Chip::PCF857x::_Adapter;
47             # Can't 'extend' yet because D:C:A itself has no sub new
48 5     5   2706 use base qw( Device::Chip::Adapter );
  5         7  
  5         860  
49              
50 5     5   4136 use Carp;
  5         8  
  5         373  
51              
52 5     5   26 use Future;
  5         6  
  5         4322  
53              
54             has $_chip :param;
55             has $_mask;
56             has $_gpiobits;
57              
58             ADJUST
59             {
60             $_mask = $_chip->DEFMASK;
61             $_gpiobits = $_chip->GPIOBITS;
62             }
63              
64 0         0 async method power ( $on )
  0         0  
  0         0  
65 0         0 {
66 0         0 await $_chip->protocol->power( $on );
67 0     0   0 }
68              
69 2     2   22 async method make_protocol_GPIO { return $self }
  2         4  
  2         14  
70              
71             method list_gpios
72 2     2   68 {
73 2         5 return sort keys %{ $_gpiobits };
  2         34  
74             }
75              
76 2         3 async method write_gpios ( $gpios )
  2         5  
  2         3  
77 2         8 {
78 2         3 my $newmask = $_mask;
79              
80 2         8 foreach my $pin ( keys %$gpios ) {
81 2 50       10 my $bit = $_gpiobits->{$pin} or
82             croak "Unrecognised GPIO pin name $pin";
83              
84 2         6 $newmask &= ~$bit;
85 2 50       7 $newmask |= $bit if $gpios->{$pin};
86             }
87              
88 2 50       14 return if $newmask == $_mask;
89              
90 2         22 await $_chip->write( $_mask = $newmask );
91 2     2   7166 }
92              
93 2         3 async method read_gpios ( $gpios )
  2         4  
  2         3  
94 2         12 {
95 2         13 my $mask = await $_chip->read;
96              
97 2         15882 my %ret;
98 2         7 foreach my $pin ( @$gpios ) {
99 2 50       9 my $bit = $_gpiobits->{$pin} or
100             croak "Unrecognised GPIO pin name $pin";
101              
102 2         7 $ret{$pin} = !!( $mask & $bit );
103             }
104              
105 2         10 return \%ret;
106 2     2   2266 }
107              
108 0           async method tris_gpios ( $gpios )
  0            
  0            
109 0           {
110 0           await $self->write_gpios( { map { $_ => 1 } @$gpios } );
111 0     0     }
112              
113             0x55AA;