File Coverage

blib/lib/Device/Chip/MCP3221.pm
Criterion Covered Total %
statement 28 37 75.6
branch 1 2 50.0
condition 1 2 50.0
subroutine 7 9 77.7
pod 2 5 40.0
total 39 55 70.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, 2018-2022 -- leonerd@leonerd.org.uk
5              
6 2     2   106262 use v5.26;
  2         18  
7 2     2   10 use Object::Pad 0.57;
  2         21  
  2         9  
8              
9             package Device::Chip::MCP3221 0.13;
10             class Device::Chip::MCP3221
11             :isa(Device::Chip);
12              
13 2     2   498 use Future::AsyncAwait;
  2         2  
  2         17  
14              
15 2     2   81 use constant PROTOCOL => "I2C";
  2         3  
  2         1073  
16              
17             =encoding UTF-8
18              
19             =head1 NAME
20              
21             C - chip driver for F
22              
23             =head1 SYNOPSIS
24              
25             use Device::Chip::MCP3221;
26             use Future::AsyncAwait;
27              
28             my $chip = Device::Chip::MCP3221->new;
29             await $chip->mount( Device::Chip::Adapter::...->new );
30              
31             printf "The reading is %d\n", await $chip->read_adc;
32              
33             =head1 DESCRIPTION
34              
35             This L subclass provides specific communications to a
36             F F chip.
37              
38             The reader is presumed to be familiar with the general operation of this chip;
39             the documentation here will not attempt to explain or define chip-specific
40             concepts or features, only the use of this module to access them.
41              
42             =cut
43              
44             =head1 MOUNT PARAMETERS
45              
46             =head2 addr
47              
48             The I²C address of the device. Can be specified in decimal, octal or hex with
49             leading C<0> or C<0x> prefixes.
50              
51             =cut
52              
53             sub I2C_options ( $, %params )
54 1     1 0 318 {
  1         1  
  1         2  
55 1   50     7 my $addr = delete $params{addr} // 0x4D;
56 1 50       5 $addr = oct $addr if $addr =~ m/^0/;
57              
58             return (
59 1         6 addr => $addr,
60             max_bitrate => 400E3,
61             );
62             }
63              
64             =head1 METHODS
65              
66             The following methods documented in an C expression return L
67             instances.
68              
69             =cut
70              
71             # Chip has no config registers
72 0     0 0 0 async method read_config () { return {} }
  0         0  
  0         0  
  0         0  
  0         0  
73 0     0 0 0 async method change_config (%) { }
  0         0  
  0         0  
  0         0  
74              
75             =head2 read_adc
76              
77             $value = await $chip->read_adc;
78              
79             Performs a conversion and returns the result as a plain unsigned 12-bit
80             integer.
81              
82             =cut
83              
84 2         3 async method read_adc ()
  2         2  
85 2         6 {
86 2         6 my $buf = await $self->protocol->read( 2 );
87              
88 2         8610 return unpack "S>", $buf;
89 2     2 1 210 }
90              
91             =head2 read_adc_ratio
92              
93             $ratio = await $chip->read_adc_ratio;
94              
95             Performs a conversion and returns the result as a floating-point number
96             between 0 and 1.
97              
98             =cut
99              
100 1         3 async method read_adc_ratio ()
  1         1  
101 1         3 {
102             # MCP3221 is 12-bit
103 1         23 return ( await $self->read_adc ) / 2**12;
104 1     1 1 3181 }
105              
106             =head1 AUTHOR
107              
108             Paul Evans
109              
110             =cut
111              
112             0x55AA;