blib/lib/Device/Chip/INA219.pm

filter: Statementstmt 100% 77 / 77 Branchbran 50.0% 2 / 4 Conditioncond 50.0% 1 / 2 MC/DCmcdc 0.0% 0 / 1 Subroutinesub 100% 15 / 15 Podpod 80.0% 4 / 5 totaltotal 95.1% 99 / 104 CC 4 SCAR 3.7
CC
4
Cov
96%
I2C_options
3.1
change_config
2.0
BEGIN
1.0
CRAP 1.5
↑ Summary
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, 2014-2023 -- leonerd@leonerd.org.uk
5  
6 3 ▶use v5.26;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 3 ▶use Object::Pad 0.800;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 package Device::Chip::INA219 0.10;
11 class Device::Chip::INA219
12 3 ▶ :isa(Device::Chip::Base::RegisteredI2C 0.10);
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 3 ▶use constant REG_DATA_SIZE => 16;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 3 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 3 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 3 ▶use Future::AsyncAwait;
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 3 ▶use Data::Bitfield qw( bitfield boolfield enumfield );
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23 =encoding UTF-8
24  
25 =head1 NAME
26  
27 C<Device::Chip::INA219> - chip driver for an F<INA219>
28  
29 =head1 SYNOPSIS
30  
31 use Device::Chip::INA219;
32 use Future::AsyncAwait;
33  
34 my $chip = Device::Chip::INA219->new;
35 await $chip->mount( Device::Chip::Adapter::...->new );
36  
37 printf "Current bus voltage is %d mV, shunt voltage is %d uV\n",
38 await $chip->read_bus_voltage, await $chip->read_shunt_voltage;
39  
40 =head1 DESCRIPTION
41  
42 This L<Device::Chip> subclass provides specific communication to a
43 F<Texas Instruments> F<INA219> attached to a computer via an I²C adapter.
44  
45 The reader is presumed to be familiar with the general operation of this chip;
46 the documentation here will not attempt to explain or define chip-specific
47 concepts or features, only the use of this module to access them.
48  
49 =cut
50  
51 =head1 MOUNT PARAMETERS
52  
53 =head2 addr
54  
55 The I²C address of the device. Can be specified in decimal, octal or hex with
56 leading C<0> or C<0x> prefixes.
57  
58 =cut
59  
60 2 ▶method I2C_options ( %params )
Subroutine I2C_options: called CC=3 SCAR=11.2
Pod undocumented
61 {
62 2 ▶ my $addr = delete $params{addr} // 0x40;
Condition50%1/2
delete $params{"addr"} // 64
l!l
02
MC/DC0%0/1
delete $params{"addr"} // 64
delete $params{"addr"}
Truth table50%1/2
A
A= delete $params{"addr"}
Aresult
11
00
63 2 ▶ $addr = oct $addr if $addr =~ m/^0/;
Branch: 1 branch
BranchTrueFalseTotal
if $addr =~ /^0/u 0 2 2
64  
65 return (
66 2 addr => $addr,
67 max_bitrate => 100E3,
68 );
69 3 }
70  
71 =head1 METHODS
72  
73 The following methods documented in an C<await> expression return L<Future>
74 instances.
75  
76 =cut
77  
78 3 ▶use constant {
Subroutine BEGIN: called CC=1 SCAR=0.0
79 3 REG_CONFIG => 0x00, # R/W
80 REG_VSHUNT => 0x01, # R
81 REG_VBUS => 0x02, # R
82 REG_POWER => 0x03, # R
83 REG_CURRENT => 0x04, # R
84 REG_CALIB => 0x05, # R/W
85 3 };
86  
87 3 my @ADCs = qw( 9b 10b 11b 12b . . . . 1 2 4 8 16 32 64 128 );
88  
89 # We'll use integer encoding and pack/unpack it ourselves because the BADC
90 # field spans a byte boundary the wrong way
91 3 bitfield { format => "integer" }, CONFIG =>
92 RST => boolfield(15),
93 BRNG => enumfield(13, qw( 16V 32V )),
94 PG => enumfield(11, qw( 40mV 80mV 160mV 320mV )),
95 BADC => enumfield( 7, @ADCs),
96 SADC => enumfield( 3, @ADCs),
97 MODE_CONT => boolfield(2),
98 MODE_BUS => boolfield(1),
99 MODE_SHUNT => boolfield(0);
100  
101 =head2 read_config
102  
103 $config = await $chip->read_config;
104  
105 Reads and returns the current chip configuration as a C<HASH> reference.
106  
107 RST => BOOL
108 BRNG => "16V" | "32V"
109 PG => "40mV" | "80mV" | "160mV" | "320mV"
110 BADC => "9b" | "10b" | "11b" | "12b" | 1 | 2 | 4 | 8 | 16 | 32 | 64 | 128
111 SADC => as above
112 MODE_CONT => BOOL
113 MODE_BUS => BOOL
114 MODE_SHUNT => BOOL
115  
116 =cut
117  
118 3 field $_config;
119  
120 1 ▶async method read_config ()
Subroutine read_config: called CC=1 SCAR=0.0
Pod documented
121 1 {
122 1 my $bytes = await $self->cached_read_reg( REG_CONFIG, 1 );
123  
124 1 return $_config = { unpack_CONFIG( unpack "S>", $bytes ) };
125 3 }
126  
127 =head2 change_config
128  
129 await $chip->change_config( %config );
130  
131 Changes the configuration. Any field names not mentioned will be preserved.
132  
133 =cut
134  
135 1 ▶async method change_config ( %changes )
Subroutine change_config: called CC=2 SCAR=6.9
Pod documented
136 1 {
137 1 ▶ defined $_config or await $self->read_config;
Branch: 1 branch
BranchTrueFalseTotal
defined $_config or <custom op await> 1 0 1
138  
139 1 my %config = ( %{ $_config }, %changes );
140  
141 1 undef $_config; # invalidate the cache
142  
143 1 await $self->write_reg( REG_CONFIG, pack "S>", pack_CONFIG( %config ) );
144 3 }
145  
146 =head2 read_shunt_voltage
147  
148 $uv = await $chip->read_shunt_voltage;
149  
150 Returns the current shunt voltage reading scaled integer in microvolts.
151  
152 =cut
153  
154 1 ▶async method read_shunt_voltage ()
Subroutine read_shunt_voltage: called CC=1 SCAR=0.0
Pod documented
155 1 {
156 1 my $vraw = unpack "s>", await $self->read_reg( REG_VSHUNT, 1 );
157  
158 # Each $vraw graduation is 10uV
159 1 return $vraw * 10;
160 3 }
161  
162 =head2 read_bus_voltage
163  
164 $mv = await $chip->read_bus_voltage;
165  
166 ( $mv, $ovf, $cnvr ) = await $chip->read_bus_voltage;
167  
168 Returns the current bus voltage reading, as a scaled integer in milivolts.
169  
170 The returned L<Future> also yields the OVF and CNVR flags.
171  
172 =cut
173  
174 1 ▶async method read_bus_voltage ()
Subroutine read_bus_voltage: called CC=1 SCAR=0.0
Pod documented
175 1 {
176 1 my $value = unpack "s>", await $self->read_reg( REG_VBUS, 1 );
177  
178 1 my $ovf = ( $value & 1<<0 );
179 1 my $cnvr = ( $value & 1<<1 );
180 1 my $vraw = $value >> 3;
181  
182 # Each $vraw graduation is 4mV
183 1 return $vraw * 4, $cnvr, $ovf;;
184 3 }
185  
186 =head1 AUTHOR
187  
188 Paul Evans <leonerd@leonerd.org.uk>
189  
190 =cut
191  
192 3 0x55AA;
↑ Summary