line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
#!/usr/bin/perl |
2
|
1
|
|
|
1
|
|
67640
|
use strict; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
344
|
|
3
|
|
|
|
|
|
|
|
4
|
|
|
|
|
|
|
package PINE64::MCP9808; |
5
|
|
|
|
|
|
|
|
6
|
|
|
|
|
|
|
our $VERSION = '0.905'; |
7
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
#global vars |
9
|
|
|
|
|
|
|
my ($i2cbus, $addr) = ('',''); |
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
sub new{ |
12
|
0
|
|
|
0
|
1
|
|
my $class = shift; |
13
|
0
|
|
|
|
|
|
my $self = bless {}, $class; |
14
|
|
|
|
|
|
|
|
15
|
0
|
|
|
|
|
|
$i2cbus = $_[0]; |
16
|
0
|
|
|
|
|
|
$addr = $_[1]; |
17
|
|
|
|
|
|
|
|
18
|
0
|
0
|
|
|
|
|
if($i2cbus eq ''){ |
19
|
0
|
|
|
|
|
|
$i2cbus = 0; |
20
|
|
|
|
|
|
|
}#end if |
21
|
|
|
|
|
|
|
|
22
|
0
|
0
|
|
|
|
|
if($addr eq ''){ |
23
|
0
|
|
|
|
|
|
$addr = '0x18'; |
24
|
|
|
|
|
|
|
}#end if |
25
|
|
|
|
|
|
|
|
26
|
0
|
|
|
|
|
|
return $self; |
27
|
|
|
|
|
|
|
}#end new |
28
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
sub read_temperature{ |
30
|
|
|
|
|
|
|
|
31
|
|
|
|
|
|
|
#reads ambient temperature reguster 0x05 |
32
|
|
|
|
|
|
|
|
33
|
0
|
|
|
0
|
1
|
|
my $hrdg = `i2cget -y $i2cbus $addr 0x05 w`; |
34
|
0
|
|
|
|
|
|
my ($ubyte, $lbyte) = (0,0); |
35
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
#split into uppper and lower bytes |
37
|
0
|
|
|
|
|
|
$hrdg =~ /(..)(..)(..)/; |
38
|
0
|
|
|
|
|
|
$ubyte = $3; |
39
|
0
|
|
|
|
|
|
$lbyte = $2; |
40
|
|
|
|
|
|
|
|
41
|
|
|
|
|
|
|
#convert hex to decimal |
42
|
0
|
|
|
|
|
|
$ubyte = hex $ubyte; |
43
|
0
|
|
|
|
|
|
$lbyte = hex $lbyte; |
44
|
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
#convert reading to decimal temp |
46
|
|
|
|
|
|
|
#strip 15-13b |
47
|
0
|
0
|
|
|
|
|
if($ubyte > 127){$ubyte-=128;} |
|
0
|
|
|
|
|
|
|
48
|
0
|
0
|
|
|
|
|
if($ubyte > 63){$ubyte-=64;} |
|
0
|
|
|
|
|
|
|
49
|
0
|
0
|
|
|
|
|
if($ubyte > 31){$ubyte-=32;} |
|
0
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
|
51
|
0
|
|
|
|
|
|
my $celsius = ($ubyte * 16) + ($lbyte * 0.0625); |
52
|
0
|
|
|
|
|
|
my $fahrenheit = ($celsius * 1.8) + 32; |
53
|
|
|
|
|
|
|
|
54
|
0
|
|
|
|
|
|
return($celsius, $fahrenheit); |
55
|
|
|
|
|
|
|
}#end read_temperature |
56
|
|
|
|
|
|
|
|
57
|
|
|
|
|
|
|
1; |
58
|
|
|
|
|
|
|
__END__ |