| 1 | #!/usr/bin/perl | ||||||||||
| 2 | 1 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 3 | |||||||||||
| 4 | package PINE64::MCP9808; | ||||||||||
| 5 | |||||||||||
| 6 | 1 | our $VERSION = '0.901'; | |||||||||
| 7 | |||||||||||
| 8 | #global vars | ||||||||||
| 9 | 1 | my ($i2cbus, $addr) = ('',''); | |||||||||
| 10 | |||||||||||
| 11 | sub new{ | ||||||||||
| 12 | 0 | ▶ | my $class = shift; | ||||||||
|
Subroutine
new: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||
| 13 | 0 | my $self = bless {}, $class; | |||||||||
| 14 | |||||||||||
| 15 | 0 | $i2cbus = $_[0]; | |||||||||
| 16 | 0 | $addr = $_[1]; | |||||||||
| 17 | |||||||||||
| 18 | 0 | ▶ | if($i2cbus eq ''){ | ||||||||
|
Branch: 1 branch
| |||||||||||
| 19 | 0 | $i2cbus = 0; | |||||||||
| 20 | }#end if | ||||||||||
| 21 | |||||||||||
| 22 | 0 | ▶ | if($addr eq ''){ | ||||||||
|
Branch: 1 branch
| |||||||||||
| 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 | ▶ | my $hrdg = `i2cget -y $i2cbus $addr 0x05 w`; | ||||||||
|
Subroutine
read_temperature: not called CC=4 SCAR=30.0
Pod
documented
| |||||||||||
| 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 | ▶ | if($ubyte > 127){$ubyte-=128;} | ||||||||
|
Branch: 1 branch
| |||||||||||
| 48 | 0 | ▶ | if($ubyte > 63){$ubyte-=64;} | ||||||||
|
Branch: 1 branch
| |||||||||||
| 49 | 0 | ▶ | if($ubyte > 31){$ubyte-=32;} | ||||||||
|
Branch: 1 branch
| |||||||||||
| 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 | 1; | |||||||||
| 58 | __END__ | ||||||||||