File Coverage

blib/lib/Device/Altimeter/LPS331AP.pm
Criterion Covered Total %
statement 21 39 53.8
branch n/a
condition n/a
subroutine 7 17 41.1
pod 8 8 100.0
total 36 64 56.2


line stmt bran cond sub pod time code
1 1     1   3 use strict;
  1         1  
  1         23  
2 1     1   3 use warnings;
  1         1  
  1         33  
3              
4             package Device::Altimeter::LPS331AP;
5              
6             # PODNAME: Device::Altimeter::LPS331AP
7             # ABSTRACT: I2C interface to LPS331AP Altimeter using Device::SMBus
8             #
9             # This file is part of Device-LPS331AP
10             #
11             # This software is copyright (c) 2015 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 = '0.005'; # VERSION
17              
18             # Dependencies
19 1     1   15 use 5.010;
  1         2  
  1         18  
20 1     1   2 use POSIX;
  1         1  
  1         9  
21              
22 1     1   1100 use Moose;
  1         1  
  1         5  
23             extends 'Device::SMBus';
24              
25             # Registers for the Altimeter
26              
27              
28 1     1   4584 use constant { CTRL_REG1 => 0x20, };
  1         2  
  1         50  
29              
30             # X, Y and Z Axis magnetic Field Data value in 2's complement
31              
32              
33             use constant {
34 1         314 PRESS_OUT_H => 0x2a,
35             PRESS_OUT_L => 0x29,
36             PRESS_OUT_XL => 0x28,
37              
38             TEMP_OUT_H => 0x2c,
39             TEMP_OUT_L => 0x2b,
40 1     1   3 };
  1         1  
41              
42              
43             has '+I2CDeviceAddress' => (
44             is => 'ro',
45             default => 0x5d,
46             );
47              
48              
49             sub enable {
50 0     0 1   my ($self) = @_;
51 0           $self->writeByteData( CTRL_REG1, 0b11100000 );
52             }
53              
54              
55             sub getRawReading {
56 0     0 1   my ($self) = @_;
57              
58             return (
59 0           pressure => $self->readNBytes( PRESS_OUT_XL, 3 ),
60             temperature => (
61             $self->_typecast_int_to_int16(
62             ( $self->readByteData(TEMP_OUT_H) << 8 ) |
63             $self->readByteData(TEMP_OUT_L)
64             )
65             ),
66             temp => $self->readNBytes( TEMP_OUT_L, 2 ),
67             );
68             }
69              
70              
71             sub getPressureMillibars {
72 0     0 1   my ($self) = @_;
73 0           return $self->readNBytes( PRESS_OUT_XL, 3 ) / 4096;
74             }
75              
76              
77             sub getPressureInchesHg {
78 0     0 1   my ($self) = @_;
79 0           return $self->readNBytes( PRESS_OUT_XL, 3 ) / 138706.5;
80             }
81              
82              
83             sub getPressureToAltitudeMeters {
84 0     0 1   my ( $self, $pressure, $qnh ) = @_;
85 0           $qnh |= 1013.25;
86 0           my $altitude = ( 1 - ( ( $pressure / $qnh )**(0.190263) ) ) * 44330.8;
87             }
88              
89              
90             sub getTemperatureCelsius {
91 0     0 1   my ($self) = @_;
92              
93 0           return 42.5 +
94             $self->_typecast_int_to_int16( $self->readNBytes( TEMP_OUT_L, 2 ) ) / 480;
95             }
96              
97              
98             sub getTemperatureFahrenheit {
99 0     0 1   my ($self) = @_;
100              
101 0           return 108.5 +
102             $self->_typecast_int_to_int16( $self->readNBytes( TEMP_OUT_L, 2 ) ) /
103             480 * 1.8;
104             }
105              
106             sub _typecast_int_to_int16 {
107 0     0     return unpack 's' => pack 'S' => $_[1];
108             }
109              
110             sub _typecast_int_to_int32 {
111 0     0     return unpack 'ss' => pack 'SS' => $_[1];
112             }
113              
114              
115             sub calibrate {
116 0     0 1   my ($self) = @_;
117              
118             }
119              
120             1;
121              
122             __END__
123              
124             =pod
125              
126             =head1 NAME
127              
128             Device::Altimeter::LPS331AP - I2C interface to LPS331AP Altimeter using Device::SMBus
129              
130             =head1 VERSION
131              
132             version 0.005
133              
134             =head1 ATTRIBUTES
135              
136             =head2 I2CDeviceAddress
137              
138             Contains the I2CDevice Address for the bus on which your altimeter is connected. It would look like 0x6b. Default is 0x5d.
139              
140             =head1 METHODS
141              
142             =head2 enable
143              
144             $self->enable()
145              
146             Initializes the device, Call this before you start using the device. This function sets up the appropriate default registers.
147             The Device will not work properly unless you call this function
148              
149             CTRL_REG1
150             power: {0:off,1:on}
151             ODR2:output data rate
152             ODR1
153             ODR0
154             DIFF_EN: Interrupt generation{0:disabled,1:enabled}
155             BDU: Block Data Update{0:continuous update, 1: output registers not updated until MSB and LSB reading}
156             DELTA_EN: (1: delta pressure registers enabled. 0: disable)
157             SIM: SPI Serial Interface Mode selection (0: 4-wire interface; 1: 3-wire interface)
158             default = 1 111 0 1 1 0
159              
160             =head2 getRawReading
161              
162             $self->getRawReading()
163              
164             Return raw readings from registers
165              
166             =head2 getPressureMillibars
167              
168             Get pressure in Millibars
169              
170             =head2 getPressureInchesHg
171              
172             Get pressure in inches of mercury
173              
174             =head2 getPressureToAltitudeMeters
175              
176             converts pressure in mbar to altitude in meters, using 1976 US
177             Standard Atmosphere model (note that this formula only applies to a
178             height of 11 km, or about 36000 ft)
179             If altimeter setting (QNH, barometric pressure adjusted to sea
180             level) is given, this function returns an indicated altitude
181             compensated for actual regional pressure; otherwise, it returns
182             the pressure altitude above the standard pressure level of 1013.25
183             mbar or 29.9213 inHg
184              
185             QNH is the Barometric pressure adjusted to sea level for a particular region. This value helps altitude corrections based on base barometric pressure in your region.
186              
187             (a-((x/b)^c))*d;
188             d - (x^c)*(d/b^c);
189              
190             =head2 getTemperatureCelsius
191              
192             Get Temperature in degrees celsius
193              
194             =head2 getTemperatureFahrenheit
195              
196             Get Temperature in Fahrenheit
197              
198             =head2 calibrate
199              
200             Placeholder for a calibration function
201              
202             =head1 REGISTERS
203              
204             =head2 CTRL_REG1
205              
206             =head2 PRESS_OUT_H
207              
208             =head2 PRESS_OUT_L
209              
210             =head2 PRESS_OUT_XL
211              
212             =head2 TEMP_OUT_H
213              
214             =head2 TEMP_OUT_L
215              
216             =head1 AUTHOR
217              
218             Shantanu Bhadoria <shantanu at cpan dott org>
219              
220             =head1 COPYRIGHT AND LICENSE
221              
222             This software is copyright (c) 2015 by Shantanu Bhadoria.
223              
224             This is free software; you can redistribute it and/or modify it under
225             the same terms as the Perl 5 programming language system itself.
226              
227             =cut