| line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
|
1
|
|
|
|
|
|
|
package Device::Temperature::TMP102; |
|
2
|
1
|
|
|
1
|
|
25285
|
use Moose; |
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
|
|
4
|
|
|
|
|
|
|
our $VERSION = '0.0.8'; # VERSION |
|
5
|
|
|
|
|
|
|
|
|
6
|
|
|
|
|
|
|
extends 'Device::SMBus'; |
|
7
|
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
has '+I2CDeviceAddress' => ( |
|
9
|
|
|
|
|
|
|
is => 'ro', |
|
10
|
|
|
|
|
|
|
default => 0x48, |
|
11
|
|
|
|
|
|
|
); |
|
12
|
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
has debug => ( |
|
14
|
|
|
|
|
|
|
is => 'ro', |
|
15
|
|
|
|
|
|
|
default => 0, |
|
16
|
|
|
|
|
|
|
); |
|
17
|
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
sub getTemp { |
|
19
|
|
|
|
|
|
|
my ( $self ) = @_; |
|
20
|
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
my $results = $self->readWordData( $self->I2CDeviceAddress ); |
|
22
|
|
|
|
|
|
|
|
|
23
|
|
|
|
|
|
|
unless ( $results ) { |
|
24
|
|
|
|
|
|
|
die( "ERROR: failed to get temperature reading" ); |
|
25
|
|
|
|
|
|
|
} |
|
26
|
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
if ( $results eq "-1" ) { |
|
28
|
|
|
|
|
|
|
die( "ERROR: got back an error code from I2C Device" ); |
|
29
|
|
|
|
|
|
|
} |
|
30
|
|
|
|
|
|
|
|
|
31
|
|
|
|
|
|
|
return $self->convertTemp( $results ); |
|
32
|
|
|
|
|
|
|
} |
|
33
|
|
|
|
|
|
|
|
|
34
|
|
|
|
|
|
|
sub convertTemp { |
|
35
|
|
|
|
|
|
|
my ( $self, $value ) = @_; |
|
36
|
|
|
|
|
|
|
|
|
37
|
|
|
|
|
|
|
my $lsb = ( $value & 0xff00 ); |
|
38
|
|
|
|
|
|
|
$lsb = $lsb >> 8; |
|
39
|
|
|
|
|
|
|
|
|
40
|
|
|
|
|
|
|
my $msb = $value & 0x00ff; |
|
41
|
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
printf( "results: %04x\n", $value ) if $self->debug; |
|
43
|
|
|
|
|
|
|
printf( "msb: %02x\n", $msb ) if $self->debug; |
|
44
|
|
|
|
|
|
|
printf( "lsb: %02x\n", $lsb ) if $self->debug; |
|
45
|
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
my $temp = ( $msb << 8 ) | $lsb; |
|
47
|
|
|
|
|
|
|
|
|
48
|
|
|
|
|
|
|
# The TMP102 temperature registers are left justified, correctly |
|
49
|
|
|
|
|
|
|
# right justify them |
|
50
|
|
|
|
|
|
|
$temp = $temp >> 4; |
|
51
|
|
|
|
|
|
|
|
|
52
|
|
|
|
|
|
|
# test for negative numbers |
|
53
|
|
|
|
|
|
|
if ( $temp & ( 1 << 11 ) ) { |
|
54
|
|
|
|
|
|
|
|
|
55
|
|
|
|
|
|
|
# twos compliment plus one, per the docs |
|
56
|
|
|
|
|
|
|
$temp = ~$temp + 1; |
|
57
|
|
|
|
|
|
|
|
|
58
|
|
|
|
|
|
|
# keep only our 12 bits |
|
59
|
|
|
|
|
|
|
$temp &= 0xfff; |
|
60
|
|
|
|
|
|
|
|
|
61
|
|
|
|
|
|
|
# negative |
|
62
|
|
|
|
|
|
|
$temp *= -1; |
|
63
|
|
|
|
|
|
|
} |
|
64
|
|
|
|
|
|
|
|
|
65
|
|
|
|
|
|
|
# convert to a celsius temp value |
|
66
|
|
|
|
|
|
|
$temp = $temp / 16; |
|
67
|
|
|
|
|
|
|
|
|
68
|
|
|
|
|
|
|
return $temp; |
|
69
|
|
|
|
|
|
|
} |
|
70
|
|
|
|
|
|
|
|
|
71
|
|
|
|
|
|
|
__PACKAGE__->meta->make_immutable; |
|
72
|
|
|
|
|
|
|
|
|
73
|
|
|
|
|
|
|
1; |
|
74
|
|
|
|
|
|
|
|
|
75
|
|
|
|
|
|
|
__END__ |
|
76
|
|
|
|
|
|
|
|
|
77
|
|
|
|
|
|
|
=pod |
|
78
|
|
|
|
|
|
|
|
|
79
|
|
|
|
|
|
|
=head1 NAME |
|
80
|
|
|
|
|
|
|
|
|
81
|
|
|
|
|
|
|
Device::Temperature::TMP102 - I2C interface to TMP102 temperature sensor using Device::SMBus |
|
82
|
|
|
|
|
|
|
|
|
83
|
|
|
|
|
|
|
=head1 SYNOPSIS |
|
84
|
|
|
|
|
|
|
|
|
85
|
|
|
|
|
|
|
use Device::Temperature::TMP102; |
|
86
|
|
|
|
|
|
|
|
|
87
|
|
|
|
|
|
|
my $device = shift @ARGV || '/dev/i2c-1'; |
|
88
|
|
|
|
|
|
|
|
|
89
|
|
|
|
|
|
|
my $dev = Device::Temperature::TMP102->new( I2CBusDevicePath => $device ); |
|
90
|
|
|
|
|
|
|
|
|
91
|
|
|
|
|
|
|
my $temp = $dev->getTemp(); |
|
92
|
|
|
|
|
|
|
|
|
93
|
|
|
|
|
|
|
print "Temp:\n"; |
|
94
|
|
|
|
|
|
|
printf ( "\t%2.2f C\n", $temp ); |
|
95
|
|
|
|
|
|
|
printf ( "\t%2.2f F\n", $temp * 1.8 + 32 ); |
|
96
|
|
|
|
|
|
|
|
|
97
|
|
|
|
|
|
|
|
|
98
|
|
|
|
|
|
|
=head1 DESCRIPTION |
|
99
|
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
Read temperature for a TMP102 temperature sensor over I2C. |
|
101
|
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
This library correctly handles temperatures below freezing (0 degrees Celsius). |
|
103
|
|
|
|
|
|
|
|
|
104
|
|
|
|
|
|
|
=head1 TROUBLESHOOTING |
|
105
|
|
|
|
|
|
|
|
|
106
|
|
|
|
|
|
|
Check for your device on i2cbus 1 using the i2cdetect command, e.g.: |
|
107
|
|
|
|
|
|
|
|
|
108
|
|
|
|
|
|
|
$ i2cdetect -y 1 |
|
109
|
|
|
|
|
|
|
|
|
110
|
|
|
|
|
|
|
0 1 2 3 4 5 6 7 8 9 a b c d e f |
|
111
|
|
|
|
|
|
|
00: -- -- -- -- -- -- -- -- -- -- -- -- -- |
|
112
|
|
|
|
|
|
|
10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- |
|
113
|
|
|
|
|
|
|
20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- |
|
114
|
|
|
|
|
|
|
30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- |
|
115
|
|
|
|
|
|
|
40: -- -- -- -- -- -- -- -- 48 -- -- -- -- -- -- -- |
|
116
|
|
|
|
|
|
|
50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- |
|
117
|
|
|
|
|
|
|
60: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- |
|
118
|
|
|
|
|
|
|
70: -- -- -- -- -- -- -- -- |
|
119
|
|
|
|
|
|
|
|
|
120
|
|
|
|
|
|
|
This indicates my TMP102 chip address is 0x48 on i2cbus 1. |
|
121
|
|
|
|
|
|
|
|
|
122
|
|
|
|
|
|
|
Use the command-line tool i2cget to try to get a 12-bit reading from |
|
123
|
|
|
|
|
|
|
the device, e.g.: |
|
124
|
|
|
|
|
|
|
|
|
125
|
|
|
|
|
|
|
$ i2cget -y 1 0x48 0x00 w |
|
126
|
|
|
|
|
|
|
0x2003 |
|
127
|
|
|
|
|
|
|
|
|
128
|
|
|
|
|
|
|
Refer to the documentation on L<Device::SMBus> for information on |
|
129
|
|
|
|
|
|
|
enabling the i2c driver. |
|
130
|
|
|
|
|
|
|
|
|
131
|
|
|
|
|
|
|
I also found this page to be helpful: |
|
132
|
|
|
|
|
|
|
|
|
133
|
|
|
|
|
|
|
http://donalmorrissey.blogspot.co.uk/2012/09/raspberry-pi-i2c-tutorial.html |
|
134
|
|
|
|
|
|
|
|
|
135
|
|
|
|
|
|
|
In the process of testing this on raspberry pi, I saw this error: |
|
136
|
|
|
|
|
|
|
|
|
137
|
|
|
|
|
|
|
perl: symbol lookup error: .../Device/SMBus/SMBus.so: undefined symbol: i2c_smbus_write_byte |
|
138
|
|
|
|
|
|
|
|
|
139
|
|
|
|
|
|
|
The fix was to install the package libi2c-dev. |
|
140
|
|
|
|
|
|
|
|
|
141
|
|
|
|
|
|
|
=head1 SEE ALSO |
|
142
|
|
|
|
|
|
|
|
|
143
|
|
|
|
|
|
|
https://www.sparkfun.com/products/11931 |
|
144
|
|
|
|
|
|
|
|
|
145
|
|
|
|
|
|
|
https://www.sparkfun.com/datasheets/Sensors/Temperature/tmp102.pdf |
|
146
|
|
|
|
|
|
|
|
|
147
|
|
|
|
|
|
|
http://donalmorrissey.blogspot.com/2012/09/raspberry-pi-i2c-tutorial.html |
|
148
|
|
|
|
|
|
|
|
|
149
|
|
|
|
|
|
|
https://github.com/sparkfun/Digital_Temperature_Sensor_Breakout_-_TMP102 |
|
150
|
|
|
|
|
|
|
|
|
151
|
|
|
|
|
|
|
=head1 SOURCE |
|
152
|
|
|
|
|
|
|
|
|
153
|
|
|
|
|
|
|
With code and comments taken from example code for the ATmega328: |
|
154
|
|
|
|
|
|
|
|
|
155
|
|
|
|
|
|
|
http://www.sparkfun.com/datasheets/Sensors/Temperature/tmp102.zip |
|
156
|
|
|
|
|
|
|
|
|
157
|
|
|
|
|
|
|
/* |
|
158
|
|
|
|
|
|
|
TMP Test Code |
|
159
|
|
|
|
|
|
|
5-31-10 |
|
160
|
|
|
|
|
|
|
Copyright Spark Fun Electronics 2010 |
|
161
|
|
|
|
|
|
|
Nathan Seidle |
|
162
|
|
|
|
|
|
|
|
|
163
|
|
|
|
|
|
|
Example code for the TMP102 11-bit I2C temperature sensor |
|
164
|
|
|
|
|
|
|
|
|
165
|
|
|
|
|
|
|
You will need to connect the ADR0 pin to one of four places. This |
|
166
|
|
|
|
|
|
|
code assumes ADR0 is tied to VCC. This results in an I2C address |
|
167
|
|
|
|
|
|
|
of 0x93 for read, and 0x92 for write. |
|
168
|
|
|
|
|
|
|
|
|
169
|
|
|
|
|
|
|
This code assumes regular 12 bit readings. If you want the |
|
170
|
|
|
|
|
|
|
extended mode for higher temperatures, the code will have to be |
|
171
|
|
|
|
|
|
|
modified slightly. |
|
172
|
|
|
|
|
|
|
|
|
173
|
|
|
|
|
|
|
*/ |
|
174
|
|
|
|
|
|
|
|
|
175
|
|
|
|
|
|
|
=head1 VERSION |
|
176
|
|
|
|
|
|
|
|
|
177
|
|
|
|
|
|
|
=head1 ATTRIBUTES |
|
178
|
|
|
|
|
|
|
|
|
179
|
|
|
|
|
|
|
=head2 I2CDeviceAddress |
|
180
|
|
|
|
|
|
|
|
|
181
|
|
|
|
|
|
|
Contains the I2CDevice Address for the bus on which your TMP102 is |
|
182
|
|
|
|
|
|
|
connected. The default value is 0x48. |
|
183
|
|
|
|
|
|
|
|
|
184
|
|
|
|
|
|
|
=head1 METHODS |
|
185
|
|
|
|
|
|
|
|
|
186
|
|
|
|
|
|
|
=head2 getTemp() |
|
187
|
|
|
|
|
|
|
|
|
188
|
|
|
|
|
|
|
$self->getTemp() |
|
189
|
|
|
|
|
|
|
|
|
190
|
|
|
|
|
|
|
Returns the current temperature, in degrees Celsius. |
|
191
|
|
|
|
|
|
|
|
|
192
|
|
|
|
|
|
|
=head2 convertTemp() |
|
193
|
|
|
|
|
|
|
|
|
194
|
|
|
|
|
|
|
$self->convertTemp( $reading ) |
|
195
|
|
|
|
|
|
|
|
|
196
|
|
|
|
|
|
|
Given a value read from the TMP102, convert the value to degrees |
|
197
|
|
|
|
|
|
|
Celsius. |
|
198
|
|
|
|
|
|
|
|
|
199
|
|
|
|
|
|
|
=head1 LICENSE |
|
200
|
|
|
|
|
|
|
|
|
201
|
|
|
|
|
|
|
This software is Copyright (c) 2014 by Alex White. |
|
202
|
|
|
|
|
|
|
|
|
203
|
|
|
|
|
|
|
This is free software, licensed under: |
|
204
|
|
|
|
|
|
|
|
|
205
|
|
|
|
|
|
|
The (three-clause) BSD License |
|
206
|
|
|
|
|
|
|
|
|
207
|
|
|
|
|
|
|
The BSD License |
|
208
|
|
|
|
|
|
|
|
|
209
|
|
|
|
|
|
|
Redistribution and use in source and binary forms, with or without |
|
210
|
|
|
|
|
|
|
modification, are permitted provided that the following conditions are |
|
211
|
|
|
|
|
|
|
met: |
|
212
|
|
|
|
|
|
|
|
|
213
|
|
|
|
|
|
|
* Redistributions of source code must retain the above copyright |
|
214
|
|
|
|
|
|
|
notice, this list of conditions and the following disclaimer. |
|
215
|
|
|
|
|
|
|
|
|
216
|
|
|
|
|
|
|
* Redistributions in binary form must reproduce the above copyright |
|
217
|
|
|
|
|
|
|
notice, this list of conditions and the following disclaimer in the |
|
218
|
|
|
|
|
|
|
documentation and/or other materials provided with the distribution. |
|
219
|
|
|
|
|
|
|
|
|
220
|
|
|
|
|
|
|
* Neither the name of Alex White nor the names of its |
|
221
|
|
|
|
|
|
|
contributors may be used to endorse or promote products derived from |
|
222
|
|
|
|
|
|
|
this software without specific prior written permission. |
|
223
|
|
|
|
|
|
|
|
|
224
|
|
|
|
|
|
|
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS |
|
225
|
|
|
|
|
|
|
IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED |
|
226
|
|
|
|
|
|
|
TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A |
|
227
|
|
|
|
|
|
|
PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR |
|
228
|
|
|
|
|
|
|
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
|
229
|
|
|
|
|
|
|
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
|
230
|
|
|
|
|
|
|
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
|
231
|
|
|
|
|
|
|
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
|
232
|
|
|
|
|
|
|
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
|
233
|
|
|
|
|
|
|
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
|
234
|
|
|
|
|
|
|
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |