File Coverage

blib/lib/Device/Chip/MCP3221.pm
Criterion Covered Total %
statement 31 40 77.5
branch 1 2 50.0
condition 1 2 50.0
subroutine 8 10 80.0
pod 2 5 40.0
total 43 59 72.8


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