File Coverage

blib/lib/Device/Chip/PCF857x.pm
Criterion Covered Total %
statement 71 83 85.5
branch 5 10 50.0
condition 1 2 50.0
subroutine 17 19 89.4
pod 0 4 0.0
total 94 118 79.6


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