File: MS5611.pm

package info (click to toggle)
libhipi-perl 0.93-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 20,048 kB
  • sloc: perl: 471,917; ansic: 22; makefile: 10
file content (216 lines) | stat: -rw-r--r-- 6,284 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
#########################################################################################
# Package        HiPi::Interface::MS5611
# Description  : Interface to MS5611_01BA03 barometric pressure sensor
# Copyright    : Copyright (c) 2013-2023 Mark Dootson
# License      : This is free software; you can redistribute it and/or modify it under
#                the same terms as the Perl 5 programming language system itself.
#########################################################################################

package HiPi::Interface::MS5611;

#########################################################################################

use strict;
use warnings;
use parent qw( HiPi::Interface::Common::Weather );
use HiPi qw( :i2c :rpi :ms5611);
use HiPi::RaspberryPi;
use Carp;

our $VERSION ='0.89';

__PACKAGE__->create_accessors( qw( backend crc) );

use constant {
    CMD_RESET    => 0x1E,
    CMD_ADC_READ => 0x00, # // ADC read command
    CMD_ADC_CONV => 0x40, # // ADC conversion command
    CMD_ADC_D1   => 0x00, # // ADC D1 conversion
    CMD_ADC_D2   => 0x10, # // ADC D2 conversion
    CMD_PROM_RD  => 0xA0, # // Prom read command 
};

sub new {
    my ($class, %userparams) = @_;
    my $pi = HiPi::RaspberryPi->new();
    
    my %params = (
        devicename   => ( $pi->board_type == RPI_BOARD_TYPE_1 ) ? '/dev/i2c-0' : '/dev/i2c-1',
        address     => 0x76,
        device      => undef,
        backend     => 'i2c',
    );
    
    # get user params
    foreach my $key( keys (%userparams) ) {
        $params{$key} = $userparams{$key};
    }
    
    if( $params{busmode} ) {
        $params{backend} = $params{busmode};
    }
    
    unless( defined($params{device}) ) {
        if ( $params{backend} eq 'bcm2835' ) {
            require HiPi::BCM2835::I2C;
            $params{device} = HiPi::BCM2835::I2C->new(
                address    => $params{address},
                peripheral => ( $params{devicename} eq '/dev/i2c-0' ) ? HiPi::BCM2835::I2C::BB_I2C_PERI_0() : HiPi::BCM2835::I2C::BB_I2C_PERI_1(),
            );
        } else {
            require HiPi::Device::I2C;
            $params{device} = HiPi::Device::I2C->new(
                devicename  => $params{devicename},
                address     => $params{address},
                busmode     => 'i2c', # don't smbus
            );
        }
    }
    
    my $self = $class->SUPER::new(%params);
    
    $self->_init;
    
    return $self;
}

sub reset {
    my $self = shift;
    $self->device->bus_write( CMD_RESET );
    $self->delay( 3 );
    return;
}

sub _read_prom {
    my($self, $coefnum) = @_;
    $self->device->bus_write( CMD_PROM_RD + $coefnum * 2 );
    my @ret = $self->device->bus_read( undef, 2);
    my $prom = $ret[0] * 256 + $ret[1];
    return $prom;
}

sub _crc4 {
    my( $self ) = @_;
    
    # int cnt; // simple counter
    # unsigned int n_rem; // crc reminder
    # unsigned int crc_read; // original value of the crc
    # unsigned char n_bit;
    
    my $n_rem = 0x00;
    my $crc_read = $self->crc->[7];
    $self->crc->[7] = 0;
    for (my $cnt = 0; $cnt < 16; $cnt ++) { #// operation is performed on bytes
        # // choose LSB or MSB
        if ( $cnt % 2 ==1 ) {
            $n_rem ^= (($self->crc->[$cnt>>1]) & 0x00FF);
        } else {
            $n_rem ^= ( $self->crc->[$cnt>>1] >> 8);
        }
        for (my $n_bit = 8; $n_bit > 0; $n_bit--) {
            if ($n_rem & 0x8000) {
                $n_rem = ($n_rem << 1) ^ 0x3000;
            } else {
                $n_rem = $n_rem << 1;
            }
        }
    }
    $n_rem= (0x000F & ($n_rem >> 12)); #// // final 4-bit remainder is CRC code
    $self->crc->[7] = $crc_read; # // restore the crc_read to its original place
    return $n_rem ^ 0x00;
}

sub _init {
    my $self = shift;
    
    my @promvals = ();
    
    # get callibration coeffs
    for ( my $i = 0; $i < 8 ; $i++ ) {
        my $promval = $self->_read_prom( $i );
        push @promvals, $promval;
    }
    
    $self->crc( \@promvals );
    my $n_crc = $self->_crc4( @promvals );
    # is the crc check worth it ?????
}

sub _adc_cmd {
    my( $self, $cmd ) = @_;
    $self->device->bus_write( CMD_ADC_CONV + $cmd );
    
    my $osr = $cmd &  0x0F;
    if( $osr == MS5611_OSR_256 ) {
        $self->delay(1);
    } elsif($osr == MS5611_OSR_512 ) {
        $self->delay(3);
    } elsif($osr == MS5611_OSR_1024 ) {
        $self->delay(4);
    } elsif($osr == MS5611_OSR_2048 ) {
        $self->delay(6);
    } else {
        $self->delay(10);
    }
    
    $self->device->bus_write( CMD_ADC_READ );
    
    my @ret = $self->device->bus_read( undef, 3);
    
    my $result = ($ret[0] * 65536 ) + ($ret[1] * 256 ) + $ret[2];
    return $result;
}

sub _internal_read_p_t {
    my($self, $pres_osr, $temp_osr ) = @_;
    $pres_osr //= MS5611_OSR_4096;
    $temp_osr //= MS5611_OSR_4096;
    
    my $D2 = $self->_adc_cmd( CMD_ADC_D2 + $temp_osr);
    my $D1 = $self->_adc_cmd( CMD_ADC_D1 + $pres_osr);
    
    my $dT = $D2 - $self->crc->[5] * (2**8);
    
    my $OFF  = $self->crc->[2] * (2**16) + $dT * $self->crc->[4] / (2**7);
    my $SENS = $self->crc->[1] * (2**15) + $dT * $self->crc->[3] / (2**8);

    my $TEMP = 2000 + ( $dT * $self->crc->[6]) / ( 2**23 );
        
    if( $TEMP < 2000 ) {
        my $T2 = ($dT**2) / (2**31);
        my $OFF2 = 5 * ($TEMP - 2000)**2 / 2;
        my $SENS2 = 5 * ($TEMP - 2000)**2 / 2**2;
        if( $TEMP < -1500 ) {
            $OFF2 = $OFF2 + 7 * ( $TEMP + 1500 )**2;
            $SENS2 = $SENS2 + 11 * ($TEMP + 1500)**2 / 2;
        }
        $TEMP -= $T2;
        $SENS -= $SENS2;
        $OFF -= $OFF2;
    }
    
    my $P = ( ($D1 * $SENS) / (2**21) - $OFF ) / ( 2**15 );
        
    return ( $P , $TEMP );
}

sub read_pressure_temp {
    my($self, $pres_osr, $temp_osr ) = @_;
    
    my ( $pressure, $temperature ) = $self->_internal_read_p_t( $pres_osr, $temp_osr );
            
    return ( sprintf('%.4f', $pressure / 100), sprintf('%.2f', $temperature / 100 ) );
}

sub read_pressure_pa_temp {
    my($self, $pres_osr, $temp_osr ) = @_;
    
    my ( $pressure, $temperature ) = $self->_internal_read_p_t( $pres_osr, $temp_osr );
        
    return ( sprintf('%.2f', $pressure ), sprintf('%.2f', $temperature / 100 ) );
}


1;

__END__