File Coverage

blib/lib/Device/Magnetometer/LSM303DLHC.pm
Criterion Covered Total %
statement 21 31 67.7
branch n/a
condition n/a
subroutine 7 11 63.6
pod 3 3 100.0
total 31 45 68.8


line stmt bran cond sub pod time code
1 1     1   3 use strict;
  1         2  
  1         27  
2 1     1   3 use warnings;
  1         2  
  1         37  
3              
4             package Device::Magnetometer::LSM303DLHC;
5              
6             # PODNAME: Device::Magnetometer::LSM303DLHC
7             # ABSTRACT: I2C interface to Magnetometer on the LSM303DLHC 3 axis magnetometer(compass) and accelerometer using Device::SMBus
8             #
9             # This file is part of Device-LSM303DLHC
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.013'; # VERSION
17              
18             # Dependencies
19 1     1   13 use 5.010;
  1         2  
  1         28  
20 1     1   2 use POSIX;
  1         1  
  1         6  
21              
22 1     1   1288 use Moose;
  1         2  
  1         6  
23             extends 'Device::SMBus';
24              
25              
26             has '+I2CDeviceAddress' => (
27             is => 'ro',
28             default => 0x1e,
29             );
30              
31              
32             # Registers for the Magnetometer
33 1     1   4720 use constant { MR_REG_M => 0x02, };
  1         2  
  1         100  
34              
35              
36             # X, Y and Z Axis magnetic Field Data value in 2's complement
37             use constant {
38 1         277 OUT_X_H_M => 0x03,
39             OUT_X_L_M => 0x04,
40              
41             OUT_Y_H_M => 0x07,
42             OUT_Y_L_M => 0x08,
43              
44             OUT_Z_H_M => 0x05,
45             OUT_Z_L_M => 0x06,
46 1     1   3 };
  1         2  
47              
48             has magnetometerMaxVector => (
49             is => 'rw',
50             default => sub {
51             return {
52             x => 424,
53             y => 295,
54             z => 472,
55             };
56             },
57             );
58              
59             has magnetometerMinVector => (
60             is => 'rw',
61             default => sub {
62             return {
63             x => -421,
64             y => -639,
65             z => -238,
66             };
67             },
68             );
69              
70              
71             sub enable {
72 0     0 1   my ($self) = @_;
73 0           $self->writeByteData( MR_REG_M, 0x00 );
74             }
75              
76              
77             sub getRawReading {
78 0     0 1   my ($self) = @_;
79              
80             return {
81 0           x => $self->_typecast_int_to_int16(
82             ( $self->readByteData(OUT_X_H_M) << 8 ) |
83             $self->readByteData(OUT_X_L_M)
84             ),
85             y => $self->_typecast_int_to_int16(
86             ( $self->readByteData(OUT_Y_H_M) << 8 ) |
87             $self->readByteData(OUT_Y_L_M)
88             ),
89             z => $self->_typecast_int_to_int16(
90             ( $self->readByteData(OUT_Z_H_M) << 8 ) |
91             $self->readByteData(OUT_Z_L_M)
92             ),
93             };
94             }
95              
96              
97             sub getMagnetometerScale1 {
98 0     0 1   my ($self) = @_;
99 0           my $rawReading = $self->getRawReading;
100 0           my $magnetometerMaxVector = $self->magnetometerMaxVector;
101 0           my $magnetometerMinVector = $self->magnetometerMinVector;
102             return {
103 0           x => ( $rawReading->{x} - $magnetometerMinVector->{x} ) /
104             ( $magnetometerMaxVector->{x} - $magnetometerMinVector->{x} ),
105             y => ( $rawReading->{y} - $magnetometerMinVector->{y} ) /
106             ( $magnetometerMaxVector->{y} - $magnetometerMinVector->{y} ),
107             z => ( $rawReading->{z} - $magnetometerMinVector->{z} ) /
108             ( $magnetometerMaxVector->{z} - $magnetometerMinVector->{z} ),
109             };
110             }
111              
112             sub _typecast_int_to_int16 {
113 0     0     return unpack 's' => pack 'S' => $_[1];
114             }
115              
116             1;
117              
118             __END__
119              
120             =pod
121              
122             =head1 NAME
123              
124             Device::Magnetometer::LSM303DLHC - I2C interface to Magnetometer on the LSM303DLHC 3 axis magnetometer(compass) and accelerometer using Device::SMBus
125              
126             =head1 VERSION
127              
128             version 0.013
129              
130             =head1 ATTRIBUTES
131              
132             =head2 I2CDeviceAddress
133              
134             Contains the I2CDevice Address for the bus on which your Magnetometer is connected. It would look like 0x6b. Default is 0x1e.
135              
136             =head1 METHODS
137              
138             =head2 enable
139              
140             $self->enable()
141              
142             Initializes the device, Call this before you start using the device. This function sets up the appropriate default registers.
143             The Device will not work properly unless you call this function
144              
145             =head2 getRawReading
146              
147             $self->getRawReading()
148              
149             Return raw readings from accelerometer registers
150              
151             =head2 getMagnetometerScale1
152              
153             $self->getMagnetometerScale1()
154              
155             Return proper calculated readings from the magnetometer scaled between +0.5 and
156             -0.5
157              
158             =head1 REGISTERS
159              
160             =head2 MR_REG_M
161              
162             =head2 OUT_X_H_M
163              
164             =head2 OUT_X_L_M
165              
166             =head2 OUT_Y_H_M
167              
168             =head2 OUT_Y_L_M
169              
170             =head2 OUT_Z_H_M
171              
172             =head2 OUT_Z_L_M
173              
174             =head1 AUTHOR
175              
176             Shantanu Bhadoria <shantanu at cpan dott org>
177              
178             =head1 COPYRIGHT AND LICENSE
179              
180             This software is copyright (c) 2015 by Shantanu Bhadoria.
181              
182             This is free software; you can redistribute it and/or modify it under
183             the same terms as the Perl 5 programming language system itself.
184              
185             =cut