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   315006 use v5.26;
  2         18  
7 2     2   12 use warnings;
  2         5  
  2         67  
8 2     2   11 use Object::Pad 0.800;
  2         13  
  2         87  
9              
10             package Device::Chip::MAX1166x 0.15;
11             class Device::Chip::MAX1166x
12             :isa(Device::Chip);
13              
14 2     2   717 use Future::AsyncAwait;
  2         6  
  2         15  
15              
16 2     2   140 use constant PROTOCOL => "SPI";
  2         4  
  2         1447  
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 410 {
  1         1  
  1         2  
46             return (
47 1         6 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         4 async method read_adc ()
  2         3  
73 2         7 {
74 2         12 my $buf = await $self->protocol->read( 2 );
75              
76 2         11115 return unpack "S>", $buf;
77 2     2 1 353 }
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         2 async method read_adc_ratio ()
  1         2  
89 1         18 {
90             # MAX1166x reads as 14-bit with some trailing zeroes
91             # First bit is invalid so must mask it off
92 1         25 return ( ( await $self->read_adc ) & 0x7FFF ) / 2**14;
93 1     1 1 4830 }
94              
95             =head1 AUTHOR
96              
97             Paul Evans
98              
99             =cut
100              
101             0x55AA;