File Coverage

blib/lib/RPi/EEPROM/AT24C32.pm
Criterion Covered Total %
statement 15 47 31.9
branch 0 14 0.0
condition 0 12 0.0
subroutine 5 11 45.4
pod 4 4 100.0
total 24 88 27.2


line stmt bran cond sub pod time code
1             package RPi::EEPROM::AT24C32;
2              
3 1     1   54939 use strict;
  1         3  
  1         24  
4 1     1   4 use warnings;
  1         2  
  1         23  
5              
6 1     1   6 use Carp qw(croak);
  1         1  
  1         36  
7 1     1   503 use Data::Dumper;
  1         5738  
  1         81  
8              
9             our $VERSION = '0.01';
10              
11             require XSLoader;
12             XSLoader::load('RPi::EEPROM::AT24C32', $VERSION);
13              
14             use constant {
15 1         434 ADDR_MIN_VALUE => 0,
16             ADDR_MAX_VALUE => 4095,
17             BYTE_MIN_VALUE => 0,
18             BYTE_MAX_VALUE => 255
19 1     1   7 };
  1         2  
20              
21             sub new {
22 0     0 1   my ($class, %args) = @_;
23              
24 0   0       $args{device} //= '/dev/i2c-1';
25 0   0       $args{address} //= 0x57;
26 0   0       $args{delay} //= 1;
27              
28 0           my $self = bless {%args}, $class;
29              
30 0           my $fd = eeprom_init($args{device}, $args{address}, $args{delay});
31 0           $self->fd($fd);
32              
33 0           return $self;
34             }
35             sub fd {
36 0     0 1   my ($self, $fd) = @_;
37 0 0         $self->{fd} = $fd if defined $fd;
38 0           return $self->{fd};
39             }
40             sub read {
41 0     0 1   my ($self, $addr) = @_;
42 0           _check_addr('read', $addr);
43 0           return eeprom_read($self->fd, $addr);
44             }
45             sub write {
46 0     0 1   my ($self, $addr, $byte) = @_;
47              
48 0           _check_addr('write', $addr);
49 0           _check_byte('write', $byte);
50              
51 0           return eeprom_write($self->fd, $addr, $byte);
52             }
53             sub _check_addr {
54 0     0     my ($sub, $addr) = @_;
55              
56 0 0         croak "_check_addr() requires \$sub param...\n" if ! defined $sub;
57              
58 0 0         if (! defined $addr){
59 0           croak "$sub requires an EEPROM memory address sent in...\n";
60             }
61              
62 0 0 0       if ($addr < ADDR_MIN_VALUE || $addr > ADDR_MAX_VALUE){
63 0           croak "address parameter out of range. Must be between " .
64             ADDR_MIN_VALUE . " and " . ADDR_MAX_VALUE . "\n";
65             }
66              
67 0           return 1;
68             }
69             sub _check_byte {
70 0     0     my ($sub, $byte) = @_;
71              
72 0 0         croak "_check_byte() requires \$sub param...\n" if ! defined $sub;
73              
74 0 0         if (! defined $byte){
75 0           croak "$sub requires a data byte sent in...\n";
76             }
77              
78 0 0 0       if ($byte < BYTE_MIN_VALUE || $byte > BYTE_MAX_VALUE){
79 0           croak "data byte parameter out of range. Must be between " .
80             BYTE_MIN_VALUE . " and " . BYTE_MAX_VALUE . "\n";
81             }
82              
83 0           return 1;
84             }
85              
86             1;
87             __END__