| 1 | # You may distribute under the terms of either the GNU General Public License | ||||||||||||||||||||
| 2 | # or the Artistic License (the same terms as Perl itself) | ||||||||||||||||||||
| 3 | # | ||||||||||||||||||||
| 4 | # (C) Paul Evans, 2020-2026 -- leonerd@leonerd.org.uk | ||||||||||||||||||||
| 5 | |||||||||||||||||||||
| 6 | 4 | ▶ | use v5.26; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 7 | 4 | ▶ | use warnings; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 8 | 4 | ▶ | use Object::Pad 0.800; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 9 | |||||||||||||||||||||
| 10 | package Device::Chip::BMP280 0.08; | ||||||||||||||||||||
| 11 | class Device::Chip::BMP280 | ||||||||||||||||||||
| 12 | 4 | ▶ | :isa(Device::Chip::Base::RegisteredI2C); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 13 | |||||||||||||||||||||
| 14 | 4 | ▶ | use Device::Chip::Sensor 0.23 -declare; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 15 | |||||||||||||||||||||
| 16 | 4 | ▶ | use Data::Bitfield qw( bitfield enumfield boolfield ); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 17 | 4 | ▶ | use Future::AsyncAwait; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 18 | |||||||||||||||||||||
| 19 | 4 | ▶ | use utf8; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 20 | |||||||||||||||||||||
| 21 | =encoding UTF-8 | ||||||||||||||||||||
| 22 | |||||||||||||||||||||
| 23 | =head1 NAME | ||||||||||||||||||||
| 24 | |||||||||||||||||||||
| 25 | C<Device::Chip::BMP280> - chip driver for F<BMP280> | ||||||||||||||||||||
| 26 | |||||||||||||||||||||
| 27 | =head1 SYNOPSIS | ||||||||||||||||||||
| 28 | |||||||||||||||||||||
| 29 | =for highlighter language=perl | ||||||||||||||||||||
| 30 | |||||||||||||||||||||
| 31 | use Device::Chip::BMP280; | ||||||||||||||||||||
| 32 | use Future::AsyncAwait; | ||||||||||||||||||||
| 33 | |||||||||||||||||||||
| 34 | my $chip = Device::Chip::BMP280->new; | ||||||||||||||||||||
| 35 | await $chip->mount( Device::Chip::Adapter::...->new ); | ||||||||||||||||||||
| 36 | |||||||||||||||||||||
| 37 | await $chip->change_config( | ||||||||||||||||||||
| 38 | OSRS_P => 4, | ||||||||||||||||||||
| 39 | OSRS_T => 4, | ||||||||||||||||||||
| 40 | MODE => "NORMAL", | ||||||||||||||||||||
| 41 | ); | ||||||||||||||||||||
| 42 | |||||||||||||||||||||
| 43 | my ( $pressure, $temperature ) = await $chip->read_sensor; | ||||||||||||||||||||
| 44 | |||||||||||||||||||||
| 45 | printf "Temperature=%.2fC ", $temperature; | ||||||||||||||||||||
| 46 | printf "Pressure=%dPa ", $pressure; | ||||||||||||||||||||
| 47 | |||||||||||||||||||||
| 48 | =head1 DESCRIPTION | ||||||||||||||||||||
| 49 | |||||||||||||||||||||
| 50 | This L<Device::Chip> subclass provides specific communication to a F<Bosch> | ||||||||||||||||||||
| 51 | F<BMP280> attached to a computer via an I²C adapter. This module can also | ||||||||||||||||||||
| 52 | communicate with the F<BME280> chip; though to gain access to the additional | ||||||||||||||||||||
| 53 | humidity sensor on that chip see instead L<Device::Chip::BME280>. | ||||||||||||||||||||
| 54 | |||||||||||||||||||||
| 55 | The reader is presumed to be familiar with the general operation of this chip; | ||||||||||||||||||||
| 56 | the documentation here will not attempt to explain or define chip-specific | ||||||||||||||||||||
| 57 | concepts or features, only the use of this module to access them. | ||||||||||||||||||||
| 58 | |||||||||||||||||||||
| 59 | =cut | ||||||||||||||||||||
| 60 | |||||||||||||||||||||
| 61 | =head1 MOUNT PARAMETERS | ||||||||||||||||||||
| 62 | |||||||||||||||||||||
| 63 | =head2 addr | ||||||||||||||||||||
| 64 | |||||||||||||||||||||
| 65 | The I²C address of the device. Can be specified in decimal, octal or hex with | ||||||||||||||||||||
| 66 | leading C<0> or C<0x> prefixes. | ||||||||||||||||||||
| 67 | |||||||||||||||||||||
| 68 | =cut | ||||||||||||||||||||
| 69 | |||||||||||||||||||||
| 70 | 3 | ▶ | method I2C_options ( %params ) | ||||||||||||||||||
|
Subroutine
I2C_options: called CC=3 SCAR=11.2
Pod
undocumented
| |||||||||||||||||||||
| 71 | { | ||||||||||||||||||||
| 72 | 3 | ▶ | my $addr = delete $params{addr} // 0x76; | ||||||||||||||||||
|
Condition50%1/2
delete $params{"addr"} // 118
MC/DC0%0/1
delete $params{"addr"} // 118
delete $params{"addr"}
Truth table50%1/2
A
A= delete $params{"addr"}
| |||||||||||||||||||||
| 73 | 3 | ▶ | $addr = oct $addr if $addr =~ m/^0/; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 74 | |||||||||||||||||||||
| 75 | return ( | ||||||||||||||||||||
| 76 | 3 | addr => $addr, | |||||||||||||||||||
| 77 | max_bitrate => 400E3, | ||||||||||||||||||||
| 78 | ); | ||||||||||||||||||||
| 79 | 4 | } | |||||||||||||||||||
| 80 | |||||||||||||||||||||
| 81 | 4 | ▶ | use constant { | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 82 | 4 | REG_DIG_T1 => 0x88, | |||||||||||||||||||
| 83 | REG_DIG_P1 => 0x8E, | ||||||||||||||||||||
| 84 | REG_DIG_H1 => 0xA1, | ||||||||||||||||||||
| 85 | REG_ID => 0xD0, | ||||||||||||||||||||
| 86 | REG_RESET => 0xE0, | ||||||||||||||||||||
| 87 | REG_DIG_H2 => 0xE1, | ||||||||||||||||||||
| 88 | REG_CTRL_HUM => 0xF2, | ||||||||||||||||||||
| 89 | REG_STATUS => 0xF3, | ||||||||||||||||||||
| 90 | REG_CTRL_MEAS => 0xF4, | ||||||||||||||||||||
| 91 | REG_CONFIG => 0xF5, | ||||||||||||||||||||
| 92 | REG_PRESS => 0xF7, # 24bit | ||||||||||||||||||||
| 93 | REG_TEMP => 0xFA, # 24bit | ||||||||||||||||||||
| 94 | 4 | }; | |||||||||||||||||||
| 95 | |||||||||||||||||||||
| 96 | 4 | bitfield { format => "bytes-LE" }, config => | |||||||||||||||||||
| 97 | # REG_CTRL_HUM | ||||||||||||||||||||
| 98 | OSRS_H => enumfield( 0, qw( SKIP 1 2 4 8 16 16 16 ) ), | ||||||||||||||||||||
| 99 | # REG_CTRL_MEAS | ||||||||||||||||||||
| 100 | MODE => enumfield( 2*8+0, qw( SLEEP FORCED FORCED NORMAL ) ), | ||||||||||||||||||||
| 101 | OSRS_P => enumfield( 2*8+2, qw( SKIP 1 2 4 8 16 16 16 ) ), | ||||||||||||||||||||
| 102 | OSRS_T => enumfield( 2*8+5, qw( SKIP 1 2 4 8 16 16 16 ) ), | ||||||||||||||||||||
| 103 | # REG_CONFIG | ||||||||||||||||||||
| 104 | SPI3W_EN => boolfield( 3*8+0 ), | ||||||||||||||||||||
| 105 | FILTER => enumfield( 3*8+2, qw( OFF 2 4 8 16 16 16 16 ) ), | ||||||||||||||||||||
| 106 | T_SB => enumfield( 3*8+5, qw( 0.5 62.5 125 250 500 1000 10 20 ) ); | ||||||||||||||||||||
| 107 | |||||||||||||||||||||
| 108 | # This module actually contains code for both BMP280 and BME280, gated on this | ||||||||||||||||||||
| 109 | # method. The BME280 class sets this true and adds little else of interest. | ||||||||||||||||||||
| 110 | 7 | ▶ | method _has_humidity () { 0 } | ||||||||||||||||||
|
Subroutine
_has_humidity: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 111 | |||||||||||||||||||||
| 112 | 4 | ▶ | use constant WANTED_ID => 0x58; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 113 | |||||||||||||||||||||
| 114 | =head1 METHODS | ||||||||||||||||||||
| 115 | |||||||||||||||||||||
| 116 | The following methods documented in an C<await> expression return L<Future> | ||||||||||||||||||||
| 117 | instances. | ||||||||||||||||||||
| 118 | |||||||||||||||||||||
| 119 | =cut | ||||||||||||||||||||
| 120 | |||||||||||||||||||||
| 121 | =head2 read_id | ||||||||||||||||||||
| 122 | |||||||||||||||||||||
| 123 | $id = await $chip->read_id; | ||||||||||||||||||||
| 124 | |||||||||||||||||||||
| 125 | Returns the chip ID. | ||||||||||||||||||||
| 126 | |||||||||||||||||||||
| 127 | =cut | ||||||||||||||||||||
| 128 | |||||||||||||||||||||
| 129 | async method read_id | ||||||||||||||||||||
| 130 | 8 | ▶ | { | ||||||||||||||||||
|
Subroutine
read_id: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||
| 131 | 8 | return unpack "C", await $self->read_reg( REG_ID, 1 ); | |||||||||||||||||||
| 132 | 4 | } | |||||||||||||||||||
| 133 | |||||||||||||||||||||
| 134 | =head2 check_id | ||||||||||||||||||||
| 135 | |||||||||||||||||||||
| 136 | await $chip->check_id; | ||||||||||||||||||||
| 137 | |||||||||||||||||||||
| 138 | Reads the ID register from the chip and checks for a valid ID result. If the | ||||||||||||||||||||
| 139 | chip's ID matches what is expected, nothing is returned. Otherwise an | ||||||||||||||||||||
| 140 | exception is thrown. | ||||||||||||||||||||
| 141 | |||||||||||||||||||||
| 142 | If a F<BME280> chip is encountered by the F<BMP280> driver, or vice versa, the | ||||||||||||||||||||
| 143 | message indicates this, suggesting you may wish to use the other driver module. | ||||||||||||||||||||
| 144 | |||||||||||||||||||||
| 145 | =cut | ||||||||||||||||||||
| 146 | |||||||||||||||||||||
| 147 | async method check_id | ||||||||||||||||||||
| 148 | 6 | ▶ | { | ||||||||||||||||||
|
Subroutine
check_id: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||
| 149 | 6 | my $got_id = await $self->read_id; | |||||||||||||||||||
| 150 | |||||||||||||||||||||
| 151 | 6 | ▶ | return if $got_id == $self->WANTED_ID; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 152 | |||||||||||||||||||||
| 153 | 4 | ▶ | die sprintf "ID check failed: Got 0x%02X, expected 0x%02X%s\n", | ||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||
| 154 | $got_id, $self->WANTED_ID, | ||||||||||||||||||||
| 155 | $got_id == 0x58 ? " (maybe this is BMP280?)" : | ||||||||||||||||||||
| 156 | $got_id == 0x60 ? " (maybe this is BME280?)" : ""; | ||||||||||||||||||||
| 157 | 4 | } | |||||||||||||||||||
| 158 | |||||||||||||||||||||
| 159 | =head2 read_config | ||||||||||||||||||||
| 160 | |||||||||||||||||||||
| 161 | $config = await $chip->read_config; | ||||||||||||||||||||
| 162 | |||||||||||||||||||||
| 163 | Returns a C<HASH> reference containing the chip config, using fields named | ||||||||||||||||||||
| 164 | from the data sheet. | ||||||||||||||||||||
| 165 | |||||||||||||||||||||
| 166 | FILTER => OFF | 2 | 4 | 8 | 16 | ||||||||||||||||||||
| 167 | MODE => SLEEP | FORCED | NORMAL | ||||||||||||||||||||
| 168 | OSRS_H => SKIP | 1 | 2 | 4 | 8 | 16 | ||||||||||||||||||||
| 169 | OSRS_P => SKIP | 1 | 2 | 4 | 8 | 16 | ||||||||||||||||||||
| 170 | OSRS_T => SKIP | 1 | 2 | 4 | 8 | 16 | ||||||||||||||||||||
| 171 | SPI3W_EN => 0 | 1 | ||||||||||||||||||||
| 172 | T_SB => 0.5 | 10 | 20 | 62.5 | 125 | 250 | 500 | 1000 | ||||||||||||||||||||
| 173 | |||||||||||||||||||||
| 174 | =cut | ||||||||||||||||||||
| 175 | |||||||||||||||||||||
| 176 | 9 | ▶ | async method read_config () | ||||||||||||||||||
|
Subroutine
read_config: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||
| 177 | 9 | { | |||||||||||||||||||
| 178 | 9 | my $bytes = await $self->cached_read_reg( REG_CTRL_HUM, 4 ); | |||||||||||||||||||
| 179 | |||||||||||||||||||||
| 180 | 9 | my %config = unpack_config( $bytes ); | |||||||||||||||||||
| 181 | |||||||||||||||||||||
| 182 | $self->_has_humidity or | ||||||||||||||||||||
| 183 | 9 | ▶ | delete $config{OSRS_H}; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 184 | |||||||||||||||||||||
| 185 | 9 | return \%config; | |||||||||||||||||||
| 186 | 4 | } | |||||||||||||||||||
| 187 | |||||||||||||||||||||
| 188 | =head2 change_config | ||||||||||||||||||||
| 189 | |||||||||||||||||||||
| 190 | await $chip->change_config( %changes ); | ||||||||||||||||||||
| 191 | |||||||||||||||||||||
| 192 | Writes updates to the configuration registers. | ||||||||||||||||||||
| 193 | |||||||||||||||||||||
| 194 | Note that these two methods use a cache of configuration bytes to make | ||||||||||||||||||||
| 195 | subsequent modifications more efficient. | ||||||||||||||||||||
| 196 | |||||||||||||||||||||
| 197 | =cut | ||||||||||||||||||||
| 198 | |||||||||||||||||||||
| 199 | 2 | ▶ | async method change_config ( %changes ) | ||||||||||||||||||
|
Subroutine
change_config: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||
| 200 | 2 | { | |||||||||||||||||||
| 201 | 2 | my $config = await $self->read_config; | |||||||||||||||||||
| 202 | |||||||||||||||||||||
| 203 | 2 | my $bytes = pack_config( %$config, %changes ); | |||||||||||||||||||
| 204 | |||||||||||||||||||||
| 205 | 2 | ▶ | $self->_has_humidity and | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 206 | await $self->cached_write_reg( REG_CTRL_HUM, substr( $bytes, 0, 1 ) ); | ||||||||||||||||||||
| 207 | |||||||||||||||||||||
| 208 | # Don't write REG_STATUS | ||||||||||||||||||||
| 209 | |||||||||||||||||||||
| 210 | 2 | await $self->cached_write_reg( REG_CTRL_MEAS, substr( $bytes, 2, 2 ) ); | |||||||||||||||||||
| 211 | 4 | } | |||||||||||||||||||
| 212 | |||||||||||||||||||||
| 213 | 0 | ▶ | async method initialize_sensors () | ||||||||||||||||||
|
Subroutine
initialize_sensors: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||
| 214 | 0 | { | |||||||||||||||||||
| 215 | 0 | await $self->check_id; | |||||||||||||||||||
| 216 | |||||||||||||||||||||
| 217 | 0 | await $self->change_config( | |||||||||||||||||||
| 218 | MODE => "NORMAL", | ||||||||||||||||||||
| 219 | OSRS_H => 4, # no harm in sending this anyway on BMP280 | ||||||||||||||||||||
| 220 | OSRS_P => 4, | ||||||||||||||||||||
| 221 | OSRS_T => 4, | ||||||||||||||||||||
| 222 | FILTER => 4, | ||||||||||||||||||||
| 223 | ); | ||||||||||||||||||||
| 224 | |||||||||||||||||||||
| 225 | # First read after startup contains junk values | ||||||||||||||||||||
| 226 | 0 | await $self->read_sensor; | |||||||||||||||||||
| 227 | 4 | } | |||||||||||||||||||
| 228 | |||||||||||||||||||||
| 229 | =head2 read_status | ||||||||||||||||||||
| 230 | |||||||||||||||||||||
| 231 | $status = await $chip->read_status; | ||||||||||||||||||||
| 232 | |||||||||||||||||||||
| 233 | =cut | ||||||||||||||||||||
| 234 | |||||||||||||||||||||
| 235 | 0 | ▶ | async method read_status () | ||||||||||||||||||
|
Subroutine
read_status: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||
| 236 | 0 | { | |||||||||||||||||||
| 237 | 0 | my $byte = await $self->read_reg( REG_STATUS, 1 ); | |||||||||||||||||||
| 238 | |||||||||||||||||||||
| 239 | return { | ||||||||||||||||||||
| 240 | 0 | MEASURING => !!( $byte & (1<<3) ), | |||||||||||||||||||
| 241 | IM_UPDATE => !!( $byte & (1<<0) ), | ||||||||||||||||||||
| 242 | }; | ||||||||||||||||||||
| 243 | 4 | } | |||||||||||||||||||
| 244 | |||||||||||||||||||||
| 245 | =head2 read_raw | ||||||||||||||||||||
| 246 | |||||||||||||||||||||
| 247 | ( $adc_P, $adc_T ) = await $chip->read_raw; | ||||||||||||||||||||
| 248 | |||||||||||||||||||||
| 249 | Returns two integers containing the raw ADC reading values from the sensor. | ||||||||||||||||||||
| 250 | |||||||||||||||||||||
| 251 | This method is mostly for testing or internal purposes only. For converted | ||||||||||||||||||||
| 252 | sensor readings in real-world units you want to use L</read_sensor>. | ||||||||||||||||||||
| 253 | |||||||||||||||||||||
| 254 | =cut | ||||||||||||||||||||
| 255 | |||||||||||||||||||||
| 256 | 10 | ▶ | async method read_raw () | ||||||||||||||||||
|
Subroutine
read_raw: called CC=6 SCAR=18.0
Pod
documented
| |||||||||||||||||||||
| 257 | 10 | { | |||||||||||||||||||
| 258 | 10 | ▶ | my ( $bytesP, $bytesT, $bytesH ) = unpack "a3 a3 a2", | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 259 | await $self->read_reg( REG_PRESS, 6 + ( $self->_has_humidity ? 2 : 0 ) ); | ||||||||||||||||||||
| 260 | |||||||||||||||||||||
| 261 | 9 | my $adc_P = unpack( "L>", "\x00" . $bytesP ) >> 4; | |||||||||||||||||||
| 262 | 9 | ▶ | $adc_P or die "ADC_P returned all-zero result - likely faulty sensor\n"; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 263 | |||||||||||||||||||||
| 264 | 9 | my $adc_T = unpack( "L>", "\x00" . $bytesT ) >> 4; | |||||||||||||||||||
| 265 | 9 | ▶ | $adc_T or die "ADC_T returned all-zero result - likely faulty sensor\n"; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 266 | |||||||||||||||||||||
| 267 | 9 | ▶ | return ( $adc_P, $adc_T ) unless length $bytesH; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 268 | |||||||||||||||||||||
| 269 | 7 | my $adc_H = unpack( "S>", $bytesH ); | |||||||||||||||||||
| 270 | 7 | ▶ | $adc_H or die "ADC_H returned all-zero result - likely faulty sensor\n"; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 271 | |||||||||||||||||||||
| 272 | 7 | return ( $adc_P, $adc_T, $adc_H ); | |||||||||||||||||||
| 273 | 4 | } | |||||||||||||||||||
| 274 | |||||||||||||||||||||
| 275 | # Compensation formulae directly from BME280 datasheet section 8.1 | ||||||||||||||||||||
| 276 | |||||||||||||||||||||
| 277 | 4 | field $_t_fine; | |||||||||||||||||||
| 278 | |||||||||||||||||||||
| 279 | 4 | field @_dig_T; | |||||||||||||||||||
| 280 | |||||||||||||||||||||
| 281 | 11 | ▶ | async method _compensate_temperature ( $adc_T ) | ||||||||||||||||||
|
Subroutine
_compensate_temperature: called CC=2 SCAR=6.9
| |||||||||||||||||||||
| 282 | 11 | { | |||||||||||||||||||
| 283 | 11 | ▶ | @_dig_T or | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 284 | @_dig_T = ( undef, unpack "S< s< s<", await $self->read_reg( REG_DIG_T1, 6 ) ); | ||||||||||||||||||||
| 285 | |||||||||||||||||||||
| 286 | 11 | my $var1 = ($adc_T / 16384 - $_dig_T[1] / 1024) * $_dig_T[2]; | |||||||||||||||||||
| 287 | 11 | my $var2 = ($adc_T / 131072 - $_dig_T[1] / 8192) ** 2 * $_dig_T[3]; | |||||||||||||||||||
| 288 | |||||||||||||||||||||
| 289 | 11 | $_t_fine = int( $var1 + $var2 ); | |||||||||||||||||||
| 290 | 11 | my $T = ( $var1 + $var2 ) / 5120.0; | |||||||||||||||||||
| 291 | 11 | return $T; | |||||||||||||||||||
| 292 | 4 | } | |||||||||||||||||||
| 293 | |||||||||||||||||||||
| 294 | 4 | field @_dig_P; | |||||||||||||||||||
| 295 | |||||||||||||||||||||
| 296 | 5 | ▶ | async method _compensate_pressure ( $adc_P ) | ||||||||||||||||||
|
Subroutine
_compensate_pressure: called CC=3 SCAR=11.0
| |||||||||||||||||||||
| 297 | 5 | { | |||||||||||||||||||
| 298 | 5 | ▶ | @_dig_P or | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 299 | @_dig_P = ( undef, unpack "S< s< s< s< s< s< s< s< s<", await $self->read_reg( REG_DIG_P1, 18 ) ); | ||||||||||||||||||||
| 300 | |||||||||||||||||||||
| 301 | 5 | my $var1 = ($_t_fine / 2) - 64000; | |||||||||||||||||||
| 302 | 5 | my $var2 = $var1 * $var1 * $_dig_P[6] / 32768; | |||||||||||||||||||
| 303 | 5 | $var2 = $var2 + $var1 * $_dig_P[5] * 2; | |||||||||||||||||||
| 304 | 5 | $var2 = ($var2 / 4) + ($_dig_P[4] * 65536); | |||||||||||||||||||
| 305 | 5 | $var1 = ($_dig_P[3] * $var1 * $var1 / 524288 + $_dig_P[2] * $var1) / 524288; | |||||||||||||||||||
| 306 | 5 | $var1 = (1 + $var1 / 32768) * $_dig_P[1]; | |||||||||||||||||||
| 307 | 5 | ▶ | return 0 if $var1 == 0; # avoid exception caused by divide-by-zero | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 308 | 5 | my $P = 1048576 - $adc_P; | |||||||||||||||||||
| 309 | 5 | $P = ($P - ($var2 / 4096)) * 6250 / $var1; | |||||||||||||||||||
| 310 | 5 | $var1 = $_dig_P[9] * $P * $P / 2147483648; | |||||||||||||||||||
| 311 | 5 | $var2 = $P * $_dig_P[8] / 32768; | |||||||||||||||||||
| 312 | 5 | $P = $P + ($var1 + $var2 + $_dig_P[7]) / 16; | |||||||||||||||||||
| 313 | 5 | return $P; | |||||||||||||||||||
| 314 | 4 | } | |||||||||||||||||||
| 315 | |||||||||||||||||||||
| 316 | 4 | field @_dig_H; | |||||||||||||||||||
| 317 | |||||||||||||||||||||
| 318 | 4 | ▶ | async method _compensate_humidity ( $adc_H ) | ||||||||||||||||||
|
Subroutine
_compensate_humidity: called CC=4 SCAR=13.9
| |||||||||||||||||||||
| 319 | 4 | { | |||||||||||||||||||
| 320 | 4 | ▶ | unless( @_dig_H ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 321 | 2 | @_dig_H = ( | |||||||||||||||||||
| 322 | undef, | ||||||||||||||||||||
| 323 | unpack( "C", await $self->read_reg( REG_DIG_H1, 1 ) ), | ||||||||||||||||||||
| 324 | unpack( "s< C ccc c", await $self->read_reg( REG_DIG_H2, 7 ) ), | ||||||||||||||||||||
| 325 | ); | ||||||||||||||||||||
| 326 | # Reshape the two 12bit values | ||||||||||||||||||||
| 327 | 2 | my ( $b0, $b1, $b2 ) = splice @_dig_H, 4, 3; | |||||||||||||||||||
| 328 | 2 | splice @_dig_H, 4, 0, | |||||||||||||||||||
| 329 | ( $b0 << 4 | $b1 & 0x0F ), # H4 | ||||||||||||||||||||
| 330 | ( $b1 >> 4 | $b2 << 4 ); # H5 | ||||||||||||||||||||
| 331 | } | ||||||||||||||||||||
| 332 | |||||||||||||||||||||
| 333 | 4 | my $var_H = $_t_fine - 76800; | |||||||||||||||||||
| 334 | 4 | $var_H = ($adc_H - ($_dig_H[4] * 64.0 + $_dig_H[5] / 16384.0 * $var_H)) * | |||||||||||||||||||
| 335 | ($_dig_H[2] / 65536.0 * (1.0 + $_dig_H[6] / 67108864.0 * $var_H * (1.0 + $_dig_H[3] / 67108864.0 * $var_H))); | ||||||||||||||||||||
| 336 | 4 | $var_H = $var_H * (1.0 - $_dig_H[1] * $var_H / 524288.0); | |||||||||||||||||||
| 337 | |||||||||||||||||||||
| 338 | 4 | ▶ | return 0 if $var_H < 0; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 339 | 4 | ▶ | return 100 if $var_H > 100; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 340 | 4 | return $var_H; | |||||||||||||||||||
| 341 | 4 | } | |||||||||||||||||||
| 342 | |||||||||||||||||||||
| 343 | =head2 read_sensor | ||||||||||||||||||||
| 344 | |||||||||||||||||||||
| 345 | ( $pressure, $temperature ) = await $chip->read_sensor; | ||||||||||||||||||||
| 346 | |||||||||||||||||||||
| 347 | Returns the sensor readings appropriately converted into units of Pascals for | ||||||||||||||||||||
| 348 | pressure and degrees Celcius for temperature. | ||||||||||||||||||||
| 349 | |||||||||||||||||||||
| 350 | =cut | ||||||||||||||||||||
| 351 | |||||||||||||||||||||
| 352 | async method read_sensor | ||||||||||||||||||||
| 353 | 2 | ▶ | { | ||||||||||||||||||
|
Subroutine
read_sensor: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||
| 354 | 2 | my ( $adc_P, $adc_T, $adc_H ) = await $self->read_raw; | |||||||||||||||||||
| 355 | |||||||||||||||||||||
| 356 | # Must do temperature first | ||||||||||||||||||||
| 357 | 2 | my $T = await $self->_compensate_temperature( $adc_T ); | |||||||||||||||||||
| 358 | |||||||||||||||||||||
| 359 | 2 | my $P = await $self->_compensate_pressure( $adc_P ); | |||||||||||||||||||
| 360 | |||||||||||||||||||||
| 361 | 2 | ▶ | return ( $P, $T ) unless defined $adc_H; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 362 | |||||||||||||||||||||
| 363 | 1 | my $H = await $self->_compensate_humidity( $adc_H ); | |||||||||||||||||||
| 364 | |||||||||||||||||||||
| 365 | 1 | return ( $P, $T, $H ); | |||||||||||||||||||
| 366 | 4 | } | |||||||||||||||||||
| 367 | |||||||||||||||||||||
| 368 | 4 | field $_pending_read_f; | |||||||||||||||||||
| 369 | |||||||||||||||||||||
| 370 | method _next_read | ||||||||||||||||||||
| 371 | 12 | ▶ | { | ||||||||||||||||||
|
Subroutine
_next_read: called CC=2 SCAR=7.1
| |||||||||||||||||||||
| 372 | return $_pending_read_f //= | ||||||||||||||||||||
| 373 | 12 | ▶ | $self->read_raw->on_ready(sub { undef $_pending_read_f }); | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.1
Condition66%2/3
$_pending_read_f //= $self->read_raw->on_ready(sub {
undef $_pending_read_f;
}
)
MC/DC0%0/2
$_pending_read_f //= $self->read_raw->on_ready(sub {
undef $_pending_read_f;
}
)
$_pending_read_f
$self->read_raw->on_ready(sub {
undef $_pending_read_f;
}
)
Truth table66%2/3
A //= B
A= $_pending_read_fB= $self->read_raw->on_ready(sub {
undef $_pending_read_f;
}
)
| |||||||||||||||||||||
| 374 | 4 | } | |||||||||||||||||||
| 375 | |||||||||||||||||||||
| 376 | declare_sensor pressure => | ||||||||||||||||||||
| 377 | 4 | ▶ | method => async method { | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 378 | 4 | my ( $rawP, $rawT, undef ) = await $self->_next_read; | |||||||||||||||||||
| 379 | 3 | $self->_compensate_temperature( $rawT ); | |||||||||||||||||||
| 380 | 3 | return await $self->_compensate_pressure( $rawP ); | |||||||||||||||||||
| 381 | 4 | }, | |||||||||||||||||||
| 382 | units => "pascals", | ||||||||||||||||||||
| 383 | sanity_bounds => [ 80_000, 120_000 ], | ||||||||||||||||||||
| 384 | precision => 0; | ||||||||||||||||||||
| 385 | |||||||||||||||||||||
| 386 | declare_sensor temperature => | ||||||||||||||||||||
| 387 | 4 | ▶ | method => async method { | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 388 | 4 | my ( undef, $rawT, undef ) = await $self->_next_read; | |||||||||||||||||||
| 389 | 3 | return await $self->_compensate_temperature( $rawT ); | |||||||||||||||||||
| 390 | 4 | }, | |||||||||||||||||||
| 391 | units => "°C", | ||||||||||||||||||||
| 392 | sanity_bounds => [ -50, 80 ], | ||||||||||||||||||||
| 393 | precision => 2; | ||||||||||||||||||||
| 394 | |||||||||||||||||||||
| 395 | =head1 AUTHOR | ||||||||||||||||||||
| 396 | |||||||||||||||||||||
| 397 | Paul Evans <leonerd@leonerd.org.uk> | ||||||||||||||||||||
| 398 | |||||||||||||||||||||
| 399 | =cut | ||||||||||||||||||||
| 400 | |||||||||||||||||||||
| 401 | 4 | 0x55AA; | |||||||||||||||||||