File Coverage

blib/lib/Device/Chip/MAX1166x.pm
Criterion Covered Total %
statement 29 38 76.3
branch n/a
condition n/a
subroutine 8 10 80.0
pod 2 5 40.0
total 39 53 73.5


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, 2020-2023 -- leonerd@leonerd.org.uk
5              
6 2     2   358145 use v5.26;
  2         8  
7 2     2   14 use warnings;
  2         5  
  2         181  
8 2     2   14 use Object::Pad 0.800;
  2         19  
  2         101  
9              
10             package Device::Chip::MAX1166x 0.16;
11             class Device::Chip::MAX1166x
12             :isa(Device::Chip);
13              
14 2     2   995 use Future::AsyncAwait;
  2         9  
  2         17  
15              
16 2     2   203 use constant PROTOCOL => "SPI";
  2         5  
  2         2260  
17              
18             =head1 NAME
19              
20             C - chip driver for F family
21              
22             =head1 SYNOPSIS
23              
24             use Device::Chip::MAX1166x;
25             use Future::AsyncAwait;
26              
27             my $chip = Device::Chip::MAX1166x->new;
28             await $chip->mount( Device::Chip::Adapter::...->new );
29              
30             printf "The reading is %d\n", await $chip->read_adc;
31              
32             =head1 DESCRIPTION
33              
34             This L subclass provides specific communications to a chip in
35             the F F family, such as F, F or
36             F.
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             sub SPI_options ( $, %params )
45 1     1 0 560 {
  1         3  
  1         20  
46             return (
47 1         12 mode => 2,
48             max_bitrate => 8E6,
49             );
50             }
51              
52             =head1 METHODS
53              
54             The following methods documented in an C expression return L
55             instances.
56              
57             =cut
58              
59             # Chip has no config registers
60 0     0 0 0 async method read_config () { return {} }
  0         0  
  0         0  
  0         0  
  0         0  
61 0     0 0 0 async method change_config (%) { }
  0         0  
  0         0  
  0         0  
62              
63             =head2 read_adc
64              
65             $value = await $chip->read_adc;
66              
67             Performs a conversion and returns the result as a plain unsigned 12-bit
68             integer.
69              
70             =cut
71              
72 2     2 1 1732 async method read_adc ()
  2         8  
  2         3  
73 2         4 {
74 2         11 my $buf = await $self->protocol->read( 2 );
75              
76 2         7018 return unpack "S>", $buf;
77             }
78              
79             =head2 read_adc_ratio
80              
81             $ratio = await $chip->read_adc_ratio;
82              
83             Performs a conversion and returns the result as a floating-point number
84             between 0 and 1.
85              
86             =cut
87              
88 1     1 1 3445 async method read_adc_ratio ()
  1         3  
  1         2  
89 1         1 {
90             # MAX1166x reads as 14-bit with some trailing zeroes
91             # First bit is invalid so must mask it off
92 1         3 return ( ( await $self->read_adc ) & 0x7FFF ) / 2**14;
93             }
94              
95             =head1 AUTHOR
96              
97             Paul Evans
98              
99             =cut
100              
101             0x55AA;