MCP4725.pm

filter: Statementstmt 90.3% 56 / 62 Branchbran 66.6% 4 / 6 Conditioncond 33.3% 2 / 6 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 90.9% 10 / 11 Podpod 80.0% 4 / 5 totaltotal 81.7% 76 / 93 CC 9 SCAR 9.9
CC
9
Cov
84%
write_dac_and_eeprom
4.2
write_dac
4.0
I2C_options
3.2
CRAP 2.7
↑ Summary MAX11200.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, 2016-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::MCP4725 0.16;
11 class Device::Chip::MCP4725
12 :isa(Device::Chip);
13  
14 2 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 2 ▶use Future::AsyncAwait;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 2 ▶use constant PROTOCOL => "I2C";
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 =encoding UTF-8
20  
21 =head1 NAME
22  
23 C<Device::Chip::MCP4725> - chip driver for F<MCP4725>
24  
25 =head1 SYNOPSIS
26  
27 use Device::Chip::MCP4725;
28 use Future::AsyncAwait;
29  
30 my $chip = Device::Chip::MCP4725->new;
31 await $chip->mount( Device::Chip::Adapter::...->new );
32  
33 # Presuming Vcc = 5V
34 await $chip->write_dac_ratio( 1.23 / 5 );
35 print "Output is now set to 1.23V\n";
36  
37 =head1 DESCRIPTION
38  
39 This L<Device::Chip> subclass provides specific communication to a
40 F<Microchip> F<MCP4725> attached to a computer via an I²C adapter.
41  
42 The reader is presumed to be familiar with the general operation of this chip;
43 the documentation here will not attempt to explain or define chip-specific
44 concepts or features, only the use of this module to access them.
45  
46 =cut
47  
48 =head1 MOUNT PARAMETERS
49  
50 =head2 addr
51  
52 The I²C address of the device. Can be specified in decimal, octal or hex with
53 leading C<0> or C<0x> prefixes.
54  
55 =cut
56  
57 1 ▶sub I2C_options ( $, %params )
Subroutine I2C_options: called CC=3 SCAR=11.7
Pod undocumented
58 {
59 1 ▶ my $addr = delete $params{addr} // 0x60;
Condition50%1/2
delete $params{"addr"} // 96
l!l
01
MC/DC0%0/1
delete $params{"addr"} // 96
delete $params{"addr"}
Truth table50%1/2
A
A= delete $params{"addr"}
Aresult
11
00
60 1 ▶ $addr = oct $addr if $addr =~ m/^0/;
Branch: 1 branch
BranchTrueFalseTotal
if $addr =~ /^0/u 0 1 1
61  
62 return (
63 1 addr => $addr,
64 max_bitrate => 400E3,
65 );
66 }
67  
68 =head1 ACCESSORS
69  
70 The following methods documented in an C<await> expression return L<Future>
71 instances.
72  
73 =cut
74  
75 2 my @POWERDOWN_TO_NAME = qw( normal 1k 100k 500k );
76 2 my %NAME_TO_POWERDOWN = map { $POWERDOWN_TO_NAME[$_] => $_ } 0 .. $#POWERDOWN_TO_NAME;
77  
78 =head2 read_config
79  
80 $config = await $chip->read_config;
81  
82 Returns a C<HASH> reference containing the chip's current configuration
83  
84 RDY => 0 | 1
85 POR => 0 | 1
86  
87 PD => "normal" | "1k" | "100k" | "500k"
88 DAC => 0 .. 4095
89  
90 EEPROM_PD => "normal" | "1k" | "100k" | "500k"
91 EEPROM_DAC => 0 .. 4095
92  
93 =cut
94  
95 1 ▶async method read_config ()
Subroutine read_config: called CC=1 SCAR=0.0
Pod documented
96 1 {
97 1 my $bytes = await $self->protocol->read( 5 );
98 1 my ( $status, $dac, $eeprom ) = unpack( "C S> S>", $bytes );
99  
100 return {
101 1 RDY => !!( $status & 0x80 ),
102 POR => !!( $status & 0x40 ),
103 PD => $POWERDOWN_TO_NAME[ ( $status & 0x06 ) >> 1 ],
104  
105 DAC => $dac >> 4,
106  
107 EEPROM_PD => $POWERDOWN_TO_NAME[ ( $eeprom & 0x6000 ) >> 13 ],
108 EEPROM_DAC => ( $eeprom & 0x0FFF ),
109 };
110 2 }
111  
112 =head1 METHODS
113  
114 =cut
115  
116 =head2 write_dac
117  
118 await $chip->write_dac( $dac, $powerdown );
119  
120 Writes a new value for the DAC output and powerdown state in "fast" mode.
121  
122 C<$powerdown> is optional and will default to 0 if not provided.
123  
124 =cut
125  
126 2 ▶async method write_dac ( $dac, $powerdown = undef )
Subroutine write_dac: called CC=4 SCAR=13.9
Pod documented
127 2 {
128 2 $dac &= 0x0FFF;
129  
130 2 my $pd = 0;
131 2 ▶ $pd = $NAME_TO_POWERDOWN{$powerdown} // croak "Unrecognised powerdown state '$powerdown'"
Branch: 1 branch
BranchTrueFalseTotal
if defined $powerdown 1 1 2
Condition50%1/2
$NAME_TO_POWERDOWN{$powerdown} // croak("Unrecognised powerdown state '${powerdown}'")
l!l
10
MC/DC0%0/1
$NAME_TO_POWERDOWN{$powerdown} // croak("Unrecognised powerdown state '${powerdown}'")
$NAME_TO_POWERDOWN{$powerdown}
Truth table50%1/2
A
A= $NAME_TO_POWERDOWN{$powerdown}
Aresult
11
00
132 if defined $powerdown;
133  
134 2 await $self->protocol->write( pack "S>", $pd << 12 | $dac );
135 2 }
136  
137 =head2 write_dac_ratio
138  
139 await $chip->write_dac_ratio( $ratio );
140  
141 Writes a new value for the DAC output, setting it to normal output for a given
142 ratio between 0 and 1.
143  
144 =cut
145  
146 0 ▶async method write_dac_ratio ( $ratio )
Subroutine write_dac_ratio: not called CC=1 SCAR=6.9
Pod documented
147 0 {
148 0 await $self->write_dac_ratio( $ratio * 2**12 );
149 2 }
150  
151 =head2 write_dac_and_eeprom
152  
153 $chip->write_dac_and_eeprom( $dac, $powerdown )
154  
155 As L</write_dac> but also updates the EEPROM with the same values.
156  
157 =cut
158  
159 1 ▶async method write_dac_and_eeprom ( $dac, $powerdown = undef )
Subroutine write_dac_and_eeprom: called CC=4 SCAR=14.2
Pod documented
160 1 {
161 1 $dac &= 0x0FFF;
162  
163 1 my $pd = 0;
164 1 ▶ $pd = $NAME_TO_POWERDOWN{$powerdown} // croak "Unrecognised powerdown state '$powerdown'"
Branch: 1 branch
BranchTrueFalseTotal
if defined $powerdown 0 1 1
Condition0%0/2
$NAME_TO_POWERDOWN{$powerdown} // croak("Unrecognised powerdown state '${powerdown}'")
l!l
00
MC/DC0%0/1
$NAME_TO_POWERDOWN{$powerdown} // croak("Unrecognised powerdown state '${powerdown}'")
$NAME_TO_POWERDOWN{$powerdown}
Truth table0%0/2
A
A= $NAME_TO_POWERDOWN{$powerdown}
Aresult
11
00
165 if defined $powerdown;
166  
167 1 await $self->protocol->write( pack "C S>", 0x60 | $pd << 1, $dac << 4 );
168 2 }
169  
170 =head1 AUTHOR
171  
172 Paul Evans <leonerd@leonerd.org.uk>
173  
174 =cut
175  
176 2 0x55AA;
↑ Summary MAX11200.pm »