ADS1115.pm

filter: Statementstmt 100% 77 / 77 Branchbran 50.0% 1 / 2 Conditioncond 33.3% 2 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 100% 15 / 15 Podpod 83.3% 5 / 6 totaltotal 90.9% 100 / 110 CC 4 SCAR 3.8
CC
4
Cov
94%
change_config
3.1
_fullscale
2.0
BEGIN
1.0
CRAP 1.5
« MCP3221.pm ↑ Summary ADC121Sx.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, 2017-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::ADS1115 0.16;
11 class Device::Chip::ADS1115
12 2 ▶ :isa(Device::Chip::Base::RegisteredI2C);
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 2 Device::Chip::Base::RegisteredI2C->VERSION( '0.10' );
15  
16 2 ▶use Future::AsyncAwait 0.13; # list-context bugfix
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 2 ▶use Data::Bitfield 0.02 qw( bitfield boolfield enumfield );
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 2 ▶use constant REG_DATA_SIZE => 16;
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 =head1 NAME
23  
24 C<Device::Chip::ADS1115> - chip driver for F<ADS1115>
25  
26 =head1 SYNOPSIS
27  
28 use Device::Chip::ADS1115;
29 use Future::AsyncAwait;
30  
31 my $chip = Device::Chip::ADS1115->new;
32 await $chip->mount( Device::Chip::Adapter::...->new );
33  
34 await $chip->change_config( MUX => "0" );
35 await $chip->trigger;
36  
37 printf "The voltage is %.2fV\n", await $chip->read_adc_voltage;
38  
39 =head1 DESCRIPTION
40  
41 This L<Device::Chip> subclass provides specific communications to a chip in
42 the F<Texas Instruments> F<ADS111x> family, such as the F<ADS1113>, F<ADS1114>
43 or F<ADS1115>. Due to similarities in hardware, it also works for the
44 F<ADS101x> family, consisting of F<ADS1013>, F<ADS1014> and F<ADS1015>.
45  
46 The reader is presumed to be familiar with the general operation of this chip;
47 the documentation here will not attempt to explain or define chip-specific
48 concepts or features, only the use of this module to access them.
49  
50 =cut
51  
52 2 ▶use constant {
Subroutine BEGIN: called CC=1 SCAR=0.0
53 2 REG_RESULT => 0,
54 REG_CONFIG => 1,
55 # TODO: threshold config
56 2 };
57  
58 sub I2C_options
59 1 ▶{
Subroutine I2C_options: called CC=1 SCAR=0.0
Pod undocumented
60 return (
61 1 addr => 0x48,
62 max_bitrate => 400E3,
63 );
64 }
65  
66 =head1 METHODS
67  
68 The following methods documented in an C<await> expression return L<Future>
69 instances.
70  
71 =cut
72  
73 =head2 read_config
74  
75 $config = await $chip->read_config;
76  
77 Returns a C<HASH> reference containing the chip's current configuration.
78  
79 OS => 0 | 1
80 MUX => "0" | "1" | "2" | "3" # single-ended
81 | "0-1" | "0-3" | "1-3" | "2-3" # bipolar
82 PGA => "6.144V" | "4.096V" | "2.048V" | "1.024V" | "0.512V" | "0.256V"
83 MODE => "CONT" | "SINGLE"
84 DR => 8 | 16 | 32 | 64 | 128 | 250 | 475 | 860
85  
86 COMP_MODE => "TRAD" | "WINDOW"
87 COMP_POL => "LOW" | "HIGH"
88 COMP_LAT => 0 | 1
89 COMP_QUE => 1 | 2 | 4 | "DIS"
90  
91 =cut
92  
93 2 bitfield { format => "integer" }, CONFIG =>
94 OS => boolfield(15),
95 MUX => enumfield(12, qw( 0-1 0-3 1-3 2-3 0 1 2 3 )),
96 PGA => enumfield( 9, qw( 6.144V 4.096V 2.048V 1.024V 0.512V 0.256V )),
97 MODE => enumfield( 8, qw( CONT SINGLE )),
98 DR => enumfield( 5, qw( 8 16 32 64 128 250 475 860 )),
99 COMP_MODE => enumfield(4, qw( TRAD WINDOW )),
100 COMP_POL => enumfield(3, qw( LOW HIGH )),
101 COMP_LAT => boolfield(2),
102 COMP_QUE => enumfield(0, qw( 1 2 4 DIS ));
103  
104 2 field $_config;
105 2 field $_fullscale_f;
106  
107 3 ▶async method read_config ()
Subroutine read_config: called CC=1 SCAR=0.0
Pod documented
108 3 {
109 3 my $bytes = await $self->cached_read_reg( REG_CONFIG, 1 );
110  
111 3 return $_config = { unpack_CONFIG( unpack "S>", $bytes ) };
112 2 }
113  
114 =head2 change_config
115  
116 await $chip->change_config( %changes );
117  
118 Changes the configuration. Any field names not mentioned will be preserved at
119 their existing values.
120  
121 =cut
122  
123 1 ▶async method change_config ( %changes )
Subroutine change_config: called CC=3 SCAR=11.3
Pod documented
124 1 {
125 1 ▶ my $config = $_config // await $self->read_config;
Condition33%1/3
$_config // <custom op await>
l!l&&r!l&&!r
100
MC/DC0%0/2
$_config // <custom op await>
$_config <custom op await>
Truth table33%1/3
A // B
A= $_configB= <custom op await>
ABresult
1X1
011
000
126  
127 1 %$config = ( %$config, %changes );
128  
129 1 ▶ undef $_fullscale_f if exists $changes{PGA};
Branch: 1 branch
BranchTrueFalseTotal
if exists $changes{"PGA"} 1 0 1
130  
131 1 await $self->cached_write_reg( REG_CONFIG, pack "S>", pack_CONFIG( %$config ) );
132 2 }
133  
134 =head2 trigger
135  
136 await $chip->trigger;
137  
138 Set the C<OS> bit configuration bit, which will cause the chip to take a new
139 reading of the currently-selected input channel when in single-shot mode.
140  
141 =cut
142  
143 1 ▶async method trigger ()
Subroutine trigger: called CC=1 SCAR=0.0
Pod documented
144 1 {
145 1 my $config = await $self->read_config;
146  
147 # Not "cached" as OS is a volatile bit
148 1 await $self->write_reg( REG_CONFIG, pack "S>", pack_CONFIG( %$config, OS => 1 ) );
149 2 }
150  
151 =head2 read_adc
152  
153 $value = await $chip->read_adc;
154  
155 Reads the most recent reading from the result register on the chip. This
156 method should be called after a suitable delay after the L</trigger> method
157 when in single-shot mode, or at any time when in continuous mode.
158  
159 The reading is returned directly from the chip as a plain 16-bit signed
160 integer. To convert this into voltage use the L</read_adc_voltage> method.
161  
162 =cut
163  
164 2 ▶async method read_adc ()
Subroutine read_adc: called CC=1 SCAR=0.0
Pod documented
165 2 {
166 2 my $bytes = await $self->read_reg( REG_RESULT, 1 );
167  
168 2 return unpack "S>", $bytes;
169 2 }
170  
171 1 ▶async method _fullscale ()
Subroutine _fullscale: called CC=2 SCAR=7.1
172 1 {
173 1 ▶ return $_fullscale_f //= do {
Condition33%1/3
$_fullscale_f //= do { my $config = <custom op await>; ($config->{"PGA"} =~ /(\d\.\d+)V/u)[0] }
l!l&&r!l&&!r
010
MC/DC0%0/2
$_fullscale_f //= do { my $config = <custom op await>; ($config->{"PGA"} =~ /(\d\.\d+)V/u)[0] }
$_fullscale_f do { my $config = <custom op await>; ($config->{"PGA"} =~ /(\d\.\d+)V/u)[0] }
Truth table33%1/3
A //= B
A= $_fullscale_fB= do { my $config = <custom op await>; ($config->{"PGA"} =~ /(\d\.\d+)V/u)[0] }
ABresult
1X1
011
000
174 1 my $config = await $self->read_config;
175 1 ( $config->{PGA} =~ m/(\d\.\d+)V/ )[0];
176 };
177 2 }
178  
179 =head2 read_adc_voltage
180  
181 $voltage = await $chip->read_adc_voltage;
182  
183 Reads the most recent reading as per L</read_adc> and converts it into a
184 voltage level by taking into account the current setting of the C<PGA>
185 configuration option to scale it.
186  
187 =cut
188  
189 1 ▶async method read_adc_voltage ()
Subroutine read_adc_voltage: called CC=1 SCAR=0.0
Pod documented
190 1 {
191 1 my $f = Future->needs_all( $self->_fullscale, $self->read_adc );
192 1 my ( $fullscale, $reading ) = await $f;
193  
194 1 return ( $reading * $fullscale ) / ( 1 << 15 );
195 2 }
196  
197 =head1 AUTHOR
198  
199 Paul Evans <leonerd@leonerd.org.uk>
200  
201 =cut
202  
203 2 0x55AA;
« MCP3221.pm ↑ Summary ADC121Sx.pm »