blib/lib/RPi/ADC/ADS.pm

filter: Statementstmt 73.6% 109 / 148 Branchbran 65.0% 39 / 60 Conditioncond 20.0% 3 / 15 MC/DCmcdc 20.0% 2 / 10 Subroutinesub 76.1% 16 / 21 Podpod 100% 10 / 10 totaltotal 67.8% 179 / 264 CC 37 SCAR 22.3
CC
37
Cov
68%
percent
30.0
volts
20.0
raw
20.0
CRAP 9.3
↑ Summary ADS.xs »
1 package RPi::ADC::ADS;
2  
3 19 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 19 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 19 our $VERSION = '1.03';
7  
8 19 require XSLoader;
9 19 XSLoader::load('RPi::ADC::ADS', $VERSION);
10  
11 19 ▶use constant {
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 19 DEFAULT_QUEUE => 0x03, # bits 1-0 (0-3)
14 MAX_QUEUE => 0x03,
15  
16 DEFAULT_POLARITY => 0x00, # bit 3
17 MAX_POLARITY => 0x08,
18  
19 DEFAULT_RATE => 0x00, # bits 7-5
20 MAX_RATE => 0xE0,
21  
22 DEFAULT_MODE => 0x100, # bit 8
23 MAX_MODE => 0x100,
24  
25 DEFAULT_GAIN => 0x200, # bits 11-9
26 MAX_GAIN => 0xE00,
27  
28 DEFAULT_CHANNEL => 0x4000, # bits 14-12
29 MAX_CHANNEL => 0x7000,
30 19 };
31  
32 # channel multiplexer
33  
34 19 my %mux = (
35 # bit 14-12 (most significant bit shown)
36  
37 # single-ended
38 0 => 0x4000, # 01000000, 16384
39 1 => 0x5000, # 01010000, 20480
40 2 => 0x6000, # 01100000, 24576
41 3 => 0x7000, # 01110000, 28672
42  
43 # differential
44 4 => 0x0, # 00000000, 0
45 5 => 0x1000, # 00010000, 4096
46 6 => 0x2000, # 00100000, 8192
47 7 => 0x3000, # 00110000, 12288
48 );
49  
50 # comparitor queue
51  
52 19 my %queue = (
53 # bit 1-0 (least significant bit shown)
54  
55 0 => 0x00, # 00000000, 0
56 1 => 0x01, # 00000001, 1
57 2 => 0x02, # 00000010, 2
58 3 => 0x03, # 00000011, 3
59 );
60  
61 # comparator polarity
62  
63 19 my %polarity = (
64 # bit 3 (least significant bit shown)
65  
66 0 => 0x00, # 00000000, 0
67 1 => 0x08, # 00000001, 8
68 );
69  
70 # data rate
71  
72 19 my %rate = (
73 # bit 7-5 (least significant bit shown)
74  
75 0 => 0x00, # 00000000, 0
76 1 => 0x20, # 00100000, 32
77 2 => 0x40, # 01000000, 64
78 3 => 0x60, # 01100000, 96
79 4 => 0x80, # 10000000, 128
80 5 => 0xA0, # 10100000, 160
81 6 => 0xC0, # 00000001, 192
82 7 => 0xE0, # 00000001, 224
83 );
84  
85 # operating mode
86  
87 19 my %mode = (
88 # bit 8 (both bits shown)
89  
90 0 => 0x00, # 0|00000000, 0
91 1 => 0x100, # 1|00000000, 256
92 );
93  
94 # amplifier gain
95  
96 19 my %gain = (
97 # bit 11-9 (most significant bit shown)
98  
99 0 => 0x00, # 00000000, 0
100 1 => 0x200, # 00000010, 512
101 2 => 0x400, # 00000100, 1024
102 3 => 0x600, # 00000110, 1536
103 4 => 0x800, # 00001000, 2048
104 5 => 0xA00, # 00001010, 2560
105 6 => 0xC00, # 00001100, 3072
106 7 => 0xE00, # 00001110, 3584
107 );
108  
109 # map of all the above config register maps
110  
111 19 my $param_map;
112  
113 19 ▶BEGIN {
Subroutine BEGIN: called CC=2 SCAR=6.9
114  
115 19 $param_map = {
116 channel => \%mux,
117 queue => \%queue,
118 polarity => \%polarity,
119 rate => \%rate,
120 mode => \%mode,
121 gain => \%gain,
122 };
123  
124 19 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
125  
126 19 for my $sub (keys %$param_map) {
127  
128 277 ▶ *$sub = sub {
Subroutine __ANON__: called CC=4 SCAR=13.9
129  
130 277 my ($self, $opt) = @_;
131  
132 277 ▶ if (defined $opt) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opt) 53 224 277
133 53 ▶ if (! exists $param_map->{$sub}{$opt}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $param_map->{$sub}{$opt}) 5 48 53
134 5 die "$sub param requires an integer\n";
135 }
136 48 $self->{$sub} = $param_map->{$sub}{$opt};
137 }
138  
139 272 my $default = "DEFAULT_" . uc $sub;
140 272 my $max = "MAX_" . uc $sub;
141  
142 272 ▶ $self->{$sub} = __PACKAGE__->$default if ! defined $self->{$sub};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{$sub} 213 59 272
143 272 $self->_bit_set($self->{$sub}, __PACKAGE__->$max);
144 272 return $self->{$sub};
145 }
146 114 }
147 }
148  
149 # object methods (public)
150  
151 45 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod documented
152 45 my ($class, %args) = @_;
153  
154 45 my $self = bless {}, $class;
155  
156 # set up the initial default config register
157  
158 45 $self->register(0x80, 0x00);
159  
160 # primary C args
161  
162 45 $self->model($args{model});
163 45 $self->addr($args{addr});
164 45 $self->device($args{device});
165  
166 # control register switches
167  
168 45 $self->channel($args{channel});
169 45 $self->queue($args{queue});
170 45 $self->polarity($args{polarity});
171 45 $self->mode($args{mode});
172 45 $self->gain($args{gain});
173  
174 # default number of conversions to average per read (1 = single read)
175  
176 45 $self->samples($args{samples});
177  
178 45 return $self;
179 }
180 57 ▶sub addr {
Subroutine addr: called CC=4 SCAR=13.9
Pod documented
181 57 my ($self, $addr) = @_;
182  
183 57 ▶ if (defined $addr){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $addr) 8 49 57
184 8 ▶ if (! grep {$addr eq $_} qw(72 73 74 75)){
Branch: 1 branch
BranchTrueFalseTotal
unless (grep {$addr eq $_;} 72, 73, 74, 75) 3 5 8
185 3 die "invalid address. " .
186 "Use 0x48 (72), 0x49 (73), 0x4A (74) or 0x4B (75)\n";
187 }
188 5 $self->{addr} = $addr;
189 }
190  
191 54 ▶ $self->{addr} = 0x48 if ! defined $self->{addr};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"addr"} 44 10 54
192  
193 54 return $self->{addr};
194 }
195 71 ▶sub device {
Subroutine device: called CC=4 SCAR=13.9
Pod documented
196 71 my ($self, $dev) = @_;
197  
198 71 ▶ if (defined $dev){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $dev) 16 55 71
199 16 ▶ if ($dev !~ m|/dev/i2c-\d|){
Branch: 1 branch
BranchTrueFalseTotal
unless ($dev =~ m[/dev/i2c-\d]) 5 11 16
200 5 die "invalid device name: $dev. " .
201 "Must be /dev/i2c-N, where N is 0-9\n";
202 }
203 11 $self->{device} = $dev;
204 }
205  
206 66 ▶ $self->{device} = '/dev/i2c-1' if ! defined $self->{device};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"device"} 44 22 66
207  
208 66 return $self->{device};
209 }
210 69 ▶sub model {
Subroutine model: called CC=4 SCAR=13.9
Pod documented
211 69 my ($self, $model) = @_;
212  
213 69 ▶ if (defined $model){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $model) 24 45 69
214 24 ▶ if ($model !~ /^ADS1[01]1[3458]/){
Branch: 1 branch
BranchTrueFalseTotal
unless ($model =~ /^ADS1[01]1[3458]/) 7 17 24
215 7 die "invalid model name: $model. " .
216 "Must be 'ADS1x1y' where x is 1 or 0, and y is 3, 4, 5 or 8\n";
217 }
218 17 $self->{model} = $model
219 }
220  
221 62 ▶ $self->{model} = 'ADS1015' if ! defined $self->{model};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"model"} 44 18 62
222  
223 62 my ($model_num) = $self->{model} =~ /(\d+)/;
224  
225 62 $self->_resolution($model_num);
226  
227 62 return $self->{model};
228 }
229  
230 # operational methods (public)
231  
232 314 ▶sub bits {
Subroutine bits: called CC=1 SCAR=0.0
Pod documented
233 314 my $self = shift;
234  
235 314 my @bytes = $self->register;
236  
237 314 my $bits = ($bytes[0] << 8) | $bytes[1];
238  
239 314 return $bits;
240 }
241 670 ▶sub register {
Subroutine register: called CC=5 SCAR=17.2
Pod documented
242 670 my ($self, $msb, $lsb) = @_;
243  
244 # config register
245  
246 670 ▶ if (defined $msb){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $msb) 317 353 670
247 317 ▶ if (! defined $lsb){
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $lsb) 0 317 317
248 0 die "register() requires \$msb and \$lsb params\n";
249 }
250 317 ▶ if (! grep {$msb == $_} (0..255)){
Branch: 1 branch
BranchTrueFalseTotal
unless (grep {$msb == $_;} 0..255) 0 317 317
251 0 die "msg param requires an int 0..255\n";
252 }
253 317 ▶ if (! grep {$lsb == $_} (0..255)){
Branch: 1 branch
BranchTrueFalseTotal
unless (grep {$lsb == $_;} 0..255) 0 317 317
254 0 die "lsb param requires an int 0..255\n";
255 }
256  
257 317 $self->{register_data} = [$msb, $lsb];
258 }
259  
260 670 return @{ $self->{register_data} };
261 }
262  
263 # private methods
264  
265 272 ▶sub _bit_set {
Subroutine _bit_set: called CC=1 SCAR=0.0
266 # unset and set config register bits
267  
268 272 my ($self, $value, $max) = @_;
269  
270 272 my $bits = $self->bits;
271  
272 # unset
273 272 $bits &= ~$max;
274  
275 # set
276 272 $bits |= $value;
277  
278 272 my $lsb = $bits & 0xFF;
279 272 my $msb = $bits >> 8;
280  
281 272 $self->register($msb, $lsb);
282 }
283 6 ▶sub _register_data {
Subroutine _register_data: called CC=1 SCAR=0.0
284  
285 # for testing/validation purposes
286  
287 6 my $tables = {
288 mux => \%mux,
289 queue => \%queue,
290 polarity => \%polarity,
291 rate => \%rate,
292 mode => \%mode,
293 gain => \%gain,
294 };
295  
296 6 return $tables;
297 }
298 71 ▶sub _resolution {
Subroutine _resolution: called CC=3 SCAR=11.0
299 # decides/sets resolution to 12 or 16 bits
300  
301 71 my ($self, $model) = @_;
302  
303 71 ▶ if (defined $model){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $model) 62 9 71
304 62 ▶ if ($model =~ /11\d{2}/){
Branch: 1 branch
BranchTrueFalseTotal
if ($model =~ /11\d{2}/) { } 9 53 62
305 9 $self->{resolution} = 16;
306 }
307 else {
308 53 $self->{resolution} = 12;
309 }
310 }
311 71 return $self->{resolution};
312 }
313  
314 # device methods
315  
316 55 ▶sub samples {
Subroutine samples: called CC=5 SCAR=16.1
Pod documented
317 55 my ($self, $samples) = @_;
318  
319 55 ▶ if (defined $samples){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $samples) 7 48 55
320 7 ▶ if ($samples !~ /^\d+$/ || $samples < 1){
Branch: 1 branch
BranchTrueFalseTotal
if (not $samples =~ /^\d+$/ or $samples < 1) 5 2 7
Condition100%3/3
not $samples =~ /^\d+$/ or $samples < 1
l!l&&r!l&&!r
412
MC/DC100%2/2
not $samples =~ /^\d+$/ or $samples < 1
not $samples =~ /^\d+$/ $samples < 1
Truth table100%3/3
not(A) or B
A= not $samples =~ /^\d+$/B= $samples < 1
ABresult
1X1
011
000
321 5 die "samples() requires a positive integer\n";
322 }
323 2 $self->{samples} = $samples;
324 }
325  
326 50 ▶ $self->{samples} = 1 if ! defined $self->{samples};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"samples"} 44 6 50
327  
328 50 return $self->{samples};
329 }
330 0 ▶sub volts {
Subroutine volts: not called CC=4 SCAR=30.0
Pod documented
331 0 my ($self, $channel, $samples) = @_;
332  
333 0 ▶ if (defined $channel){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $channel) 0 0 0
334 0 $self->channel($channel);
335 }
336  
337 0 my $addr = $self->addr;
338 0 my $dev = $self->device;
339 0 my @write_buf = $self->register;
340  
341 0 my $v = voltage_c(
342 $addr, $dev, $write_buf[0], $write_buf[1], $self->_resolution,
343 $self->_samples($samples),
344 );
345  
346 0 ▶ if ($self->channel > 3 && $v < 0){
Branch: 1 branch
BranchTrueFalseTotal
if ($self->channel > 3 and $v < 0) 0 0 0
Condition0%0/3
$self->channel > 3 and $v < 0
!ll&&!rl&&r
000
MC/DC0%0/2
$self->channel > 3 and $v < 0
$self->channel > 3 $v < 0
Truth table0%0/3
A and B
A= $self->channel > 3B= $v < 0
ABresult
0X0
100
111
347 0 return 0;
348 }
349  
350 0 return $v;
351 }
352 0 ▶sub raw {
Subroutine raw: not called CC=4 SCAR=30.0
Pod documented
353 0 my ($self, $channel, $samples) = @_;
354  
355 0 ▶ if (defined $channel){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $channel) 0 0 0
356 0 $self->channel($channel);
357 }
358  
359 0 my $addr = $self->addr;
360 0 my $dev = $self->device;
361 0 my @write_buf = $self->register;
362  
363 0 my $r = raw_c(
364 $addr, $dev, $write_buf[0], $write_buf[1], $self->_resolution,
365 $self->_samples($samples),
366 );
367  
368 0 ▶ if ($self->channel > 3 && $r < 0){
Branch: 1 branch
BranchTrueFalseTotal
if ($self->channel > 3 and $r < 0) 0 0 0
Condition0%0/3
$self->channel > 3 and $r < 0
!ll&&!rl&&r
000
MC/DC0%0/2
$self->channel > 3 and $r < 0
$self->channel > 3 $r < 0
Truth table0%0/3
A and B
A= $self->channel > 3B= $r < 0
ABresult
0X0
100
111
369 0 return 0;
370 }
371  
372 0 return $r;
373  
374 }
375 0 ▶sub percent {
Subroutine percent: not called CC=5 SCAR=34.0
Pod documented
376 0 my ($self, $channel, $samples) = @_;
377  
378 0 ▶ if (defined $channel){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $channel) 0 0 0
379 0 $self->channel($channel);
380 }
381  
382 0 my $addr = $self->addr;
383 0 my $dev = $self->device;
384 0 my @write_buf = $self->register;
385  
386 0 my $percent = percent_c(
387 $addr, $dev, $write_buf[0], $write_buf[1], $self->_resolution,
388 $self->_samples($samples),
389 );
390  
391 0 ▶ $percent = 100 if $percent > 100;
Branch: 1 branch
BranchTrueFalseTotal
if $percent > 100 0 0 0
392  
393 0 ▶ if ($self->channel > 3 && $percent < 0){
Branch: 1 branch
BranchTrueFalseTotal
if ($self->channel > 3 and $percent < 0) 0 0 0
Condition0%0/3
$self->channel > 3 and $percent < 0
!ll&&!rl&&r
000
MC/DC0%0/2
$self->channel > 3 and $percent < 0
$self->channel > 3 $percent < 0
Truth table0%0/3
A and B
A= $self->channel > 3B= $percent < 0
ABresult
0X0
100
111
394 0 return 0;
395 }
396  
397 0 return sprintf("%.2f", $percent);
398 }
399  
400 0 ▶sub _samples {
Subroutine _samples: not called CC=4 SCAR=30.0
401 0 my ($self, $samples) = @_;
402  
403 # Per-call override if given, otherwise the object default; validate either.
404 0 ▶ $samples = $self->samples if ! defined $samples;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $samples 0 0 0
405  
406 0 ▶ if ($samples !~ /^\d+$/ || $samples < 1){
Branch: 1 branch
BranchTrueFalseTotal
if (not $samples =~ /^\d+$/ or $samples < 1) 0 0 0
Condition0%0/3
not $samples =~ /^\d+$/ or $samples < 1
l!l&&r!l&&!r
000
MC/DC0%0/2
not $samples =~ /^\d+$/ or $samples < 1
not $samples =~ /^\d+$/ $samples < 1
Truth table0%0/3
not(A) or B
A= not $samples =~ /^\d+$/B= $samples < 1
ABresult
1X1
011
000
407 0 die "samples must be a positive integer\n";
408 }
409  
410 0 return $samples;
411 }
412  
413 0 ▶sub _vim {}
Subroutine _vim: not called CC=1 SCAR=0.0
414  
415 19 1;
416  
417 __END__
↑ Summary ADS.xs »