File Coverage

blib/lib/Device/SMBus.pm
Criterion Covered Total %
statement 23 69 33.3
branch 0 4 0.0
condition n/a
subroutine 8 23 34.7
pod 13 13 100.0
total 44 109 40.3


line stmt bran cond sub pod time code
1 1     1   13299 use strict;
  1         2  
  1         23  
2 1     1   3 use warnings;
  1         1  
  1         45  
3              
4             package Device::SMBus;
5              
6             # PODNAME: Device::SMBus
7             # ABSTRACT: Control and read hardware devices with i2c(SMBus)
8             #
9             # This file is part of Device-SMBus
10             #
11             # This software is copyright (c) 2016 by Shantanu Bhadoria.
12             #
13             # This is free software; you can redistribute it and/or modify it under
14             # the same terms as the Perl 5 programming language system itself.
15             #
16             our $VERSION = '1.15'; # VERSION
17              
18             # Dependencies
19 1     1   14 use 5.010000;
  1         2  
20              
21 1     1   434 use Moo;
  1         9304  
  1         5  
22 1     1   1050 use Carp;
  1         2  
  1         54  
23              
24 1     1   495 use IO::File;
  1         8709  
  1         136  
25 1     1   7 use Fcntl;
  1         3  
  1         418  
26              
27             require XSLoader;
28             XSLoader::load( 'Device::SMBus', $VERSION );
29              
30              
31 1     1   5 use constant I2C_SLAVE => 0x0703;
  1         2  
  1         927  
32              
33              
34             has I2CBusDevicePath => ( is => 'ro', );
35              
36             has I2CBusFileHandle => (
37             is => 'ro',
38             lazy => 1,
39             builder => '_build_I2CBusFileHandle',
40             );
41              
42              
43             has I2CDeviceAddress => ( is => 'ro', );
44              
45             has I2CBusFilenumber => (
46             is => 'ro',
47             lazy => 1,
48             builder => '_build_I2CBusFilenumber',
49             );
50              
51             sub _build_I2CBusFileHandle {
52 0     0     my ($self) = @_;
53 0           my $fh = IO::File->new( $self->I2CBusDevicePath, O_RDWR );
54 0 0         if ( !$fh ) {
55 0           croak "Unable to open I2C Device File at $self->I2CBusDevicePath";
56 0           return -1;
57             }
58 0           $fh->ioctl( I2C_SLAVE, $self->I2CDeviceAddress );
59 0           return $fh;
60             }
61              
62             # Implicitly Call the lazy builder for the file handle by using it and get the filenumber
63             sub _build_I2CBusFilenumber {
64 0     0     my ($self) = @_;
65 0           $self->I2CBusFileHandle->fileno();
66             }
67              
68              
69             sub fileError {
70 0     0 1   my ($self) = @_;
71 0           return $self->I2CBusFileHandle->error();
72             }
73              
74              
75             sub writeQuick {
76 0     0 1   my ( $self, $value ) = @_;
77 0           my $retval = Device::SMBus::_writeQuick( $self->I2CBusFilenumber, $value );
78             }
79              
80              
81             sub readByte {
82 0     0 1   my ($self) = @_;
83 0           my $retval = Device::SMBus::_readByte( $self->I2CBusFilenumber );
84 0           return $retval;
85             }
86              
87              
88             sub writeByte {
89 0     0 1   my ( $self, $value ) = @_;
90 0           my $retval = Device::SMBus::_writeByte( $self->I2CBusFilenumber, $value );
91             }
92              
93              
94             sub readByteData {
95 0     0 1   my ( $self, $register_address ) = @_;
96 0           my $retval = Device::SMBus::_readByteData( $self->I2CBusFilenumber,
97             $register_address );
98 0           return $retval;
99             }
100              
101              
102             sub writeByteData {
103 0     0 1   my ( $self, $register_address, $value ) = @_;
104 0           my $retval = Device::SMBus::_writeByteData( $self->I2CBusFilenumber,
105             $register_address, $value );
106             }
107              
108              
109             sub readNBytes {
110 0     0 1   my ( $self, $reg, $numBytes ) = @_;
111 0           my $retval = 0;
112             $retval = ( $retval << 8 ) | $self->readByteData( $reg + $numBytes - $_ )
113 0           for ( 1 .. $numBytes );
114 0           return $retval;
115             }
116              
117              
118             sub readWordData {
119 0     0 1   my ( $self, $register_address ) = @_;
120 0           my $retval = Device::SMBus::_readWordData( $self->I2CBusFilenumber,
121             $register_address );
122 0           return $retval;
123             }
124              
125              
126             sub writeWordData {
127 0     0 1   my ( $self, $register_address, $value ) = @_;
128 0           my $retval = Device::SMBus::_writeWordData( $self->I2CBusFilenumber,
129             $register_address, $value );
130             }
131              
132              
133             sub processCall {
134 0     0 1   my ( $self, $register_address, $value ) = @_;
135 0           my $retval =
136             Device::SMBus::_processCall( $self->I2CBusFilenumber, $register_address,
137             $value );
138             }
139              
140              
141             sub writeBlockData {
142 0     0 1   my ( $self, $register_address, $values ) = @_;
143              
144 0           my $value = pack "C*", @{$values};
  0            
145              
146 0           my $retval = Device::SMBus::_writeI2CBlockData( $self->I2CBusFilenumber,
147             $register_address, $value );
148 0           return $retval;
149             }
150              
151              
152             sub readBlockData {
153 0     0 1   my ( $self, $register_address, $numBytes ) = @_;
154              
155 0           my $read_val = '0' x ($numBytes);
156              
157 0           my $retval = Device::SMBus::_readI2CBlockData( $self->I2CBusFilenumber,
158             $register_address, $read_val );
159              
160 0           my @result = unpack( "C*", $read_val );
161 0           return @result;
162             }
163              
164             # Preloaded methods go here.
165              
166              
167             sub DEMOLISH {
168 0     0 1   my ($self) = @_;
169 0 0         $self->I2CBusFileHandle->close() if defined( $self->I2CBusFileHandle );
170             }
171              
172             1;
173              
174             __END__