File Coverage

blib/lib/Device/Chip/ADC121Sx.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   318409 use v5.26;
  2         18  
7 2     2   12 use warnings;
  2         4  
  2         67  
8 2     2   13 use Object::Pad 0.800;
  2         16  
  2         108  
9              
10             package Device::Chip::ADC121Sx 0.15;
11             class Device::Chip::ADC121Sx
12             :isa(Device::Chip);
13              
14 2     2   676 use Future::AsyncAwait;
  2         6  
  2         25  
15              
16 2     2   134 use constant PROTOCOL => "SPI";
  2         4  
  2         1530  
17              
18             =head1 NAME
19              
20             C - chip driver for F family
21              
22             =head1 SYNOPSIS
23              
24             use Device::Chip::ADC121Sx;
25             use Future::AsyncAwait;
26              
27             my $chip = Device::Chip::ADC121Sx->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,
36             F or 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 441 {
  1         2  
  1         2  
46             return (
47 1         6 mode => 2,
48             max_bitrate => 20E6,
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         13 async method read_adc ()
  2         4  
73 2         6 {
74 2         12 my $buf = await $self->protocol->read( 2 );
75              
76 2         10986 return unpack "S>", $buf;
77 2     2 1 361 }
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         4 async method read_adc_ratio ()
  1         2  
89 1         30 {
90             # ADC121Sx is 12-bit
91 1         9 return ( await $self->read_adc ) / 2**12;
92 1     1 1 4879 }
93              
94             =head1 AUTHOR
95              
96             Paul Evans
97              
98             =cut
99              
100             0x55AA;