| 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, 2026 -- leonerd@leonerd.org.uk | ||
| 5 | |||
| 6 | 3 | ▶ | use v5.26; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 7 | 3 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 8 | 3 | ▶ | use Object::Pad 0.800; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 9 | |||
| 10 | package Device::Chip::BME280 0.08; | ||
| 11 | class Device::Chip::BME280 | ||
| 12 | 2 | ▶ | :isa(Device::Chip::BMP280); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 13 | |||
| 14 | 3 | ▶ | use Device::Chip::Sensor 0.27 -declare; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 15 | |||
| 16 | 3 | ▶ | use Future::AsyncAwait; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 17 | |||
| 18 | 3 | ▶ | use utf8; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 19 | |||
| 20 | =encoding UTF-8 | ||
| 21 | |||
| 22 | =head1 NAME | ||
| 23 | |||
| 24 | C<Device::Chip::BME280> - chip driver for F<BME280> | ||
| 25 | |||
| 26 | =head1 SYNOPSIS | ||
| 27 | |||
| 28 | =for highlighter language=perl | ||
| 29 | |||
| 30 | use Device::Chip::BME280; | ||
| 31 | use Future::AsyncAwait; | ||
| 32 | |||
| 33 | my $chip = Device::Chip::BME280->new; | ||
| 34 | await $chip->mount( Device::Chip::Adapter::...->new ); | ||
| 35 | |||
| 36 | await $chip->change_config( | ||
| 37 | OSRS_H => 4, | ||
| 38 | OSRS_P => 4, | ||
| 39 | OSRS_T => 4, | ||
| 40 | MODE => "NORMAL", | ||
| 41 | ); | ||
| 42 | |||
| 43 | my ( $pressure, $temperature, $humidity ) = await $chip->read_sensor; | ||
| 44 | |||
| 45 | printf "Temperature=%.2fC ", $temperature; | ||
| 46 | printf "Pressure=%dPa ", $pressure; | ||
| 47 | printf "Humidity=%.2f%%\n", $humidity; | ||
| 48 | |||
| 49 | =head1 DESCRIPTION | ||
| 50 | |||
| 51 | This L<Device::Chip> subclass provides specific communication to a F<Bosch> | ||
| 52 | F<BME280> attached to a computer via an I²C adapter. As this chip is a | ||
| 53 | variation of the F<BMP280> which adds a humidity sensor, this module is a | ||
| 54 | subclass of L<Device::Chip::BMP280>. See that module for documentation on | ||
| 55 | overall configuration, and access to the temperature and pressure sensors | ||
| 56 | common to both chips. | ||
| 57 | |||
| 58 | The reader is presumed to be familiar with the general operation of this chip; | ||
| 59 | the documentation here will not attempt to explain or define chip-specific | ||
| 60 | concepts or features, only the use of this module to access them. | ||
| 61 | |||
| 62 | =cut | ||
| 63 | |||
| 64 | 14 | ▶ | method _has_humidity () { 1 } |
|
Subroutine
_has_humidity: called CC=1 SCAR=0.0
| |||
| 65 | |||
| 66 | 3 | ▶ | use constant WANTED_ID => 0x60; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 67 | |||
| 68 | =head1 METHODS | ||
| 69 | |||
| 70 | The following methods documented in an C<await> expression return L<Future> | ||
| 71 | instances. | ||
| 72 | |||
| 73 | In addition this module inherits all the methods provided by | ||
| 74 | L<Device::Chip::BMP280>. | ||
| 75 | |||
| 76 | =cut | ||
| 77 | |||
| 78 | =head2 read_config | ||
| 79 | |||
| 80 | $config = await $chip->read_config; | ||
| 81 | |||
| 82 | Returns a C<HASH> reference containing the chip config, which includes all | ||
| 83 | the registers provided by C<Device::Chip::BMP280>, and additionally: | ||
| 84 | |||
| 85 | OSRS_H => SKIP | 1 | 2 | 4 | 8 | 16 | ||
| 86 | |||
| 87 | =head2 change_config | ||
| 88 | |||
| 89 | await $chip->change_config( %changes ); | ||
| 90 | |||
| 91 | Writes updates to the configuration registers. Accepts C<OSRS_H> as defined | ||
| 92 | above. | ||
| 93 | |||
| 94 | =cut | ||
| 95 | |||
| 96 | # No additional methods as the ones in BMP280 already handle this | ||
| 97 | |||
| 98 | =head2 read_raw | ||
| 99 | |||
| 100 | ( $adc_P, $adc_T, $adc_H ) = await $chip->read_raw; | ||
| 101 | |||
| 102 | Returns three integers containing the raw ADC reading values from the sensor. | ||
| 103 | |||
| 104 | This method is mostly for testing or internal purposes only. For converted | ||
| 105 | sensor readings in real-world units you want to use L</read_sensor>. | ||
| 106 | |||
| 107 | =cut | ||
| 108 | |||
| 109 | =head2 read_sensor | ||
| 110 | |||
| 111 | ( $pressure, $temperature, $humidity ) = await $chip->read_sensor; | ||
| 112 | |||
| 113 | Returns the sensor readings appropriately converted into units of Pascals for | ||
| 114 | pressure, degrees Celcius for temperature, and percentage relative for | ||
| 115 | humidity. | ||
| 116 | |||
| 117 | =cut | ||
| 118 | |||
| 119 | declare_sensor humidity => | ||
| 120 | 4 | ▶ | method => async method { |
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||
| 121 | 4 | my ( undef, $rawT, $rawH ) = await $self->_next_read; | |
| 122 | 3 | $self->_compensate_temperature( $rawT ); | |
| 123 | 3 | return await $self->_compensate_humidity( $rawH ); | |
| 124 | 3 | }, | |
| 125 | units => "%RH", | ||
| 126 | sanity_bounds => [ -1, 101 ], # give it slight headroom beyond the 0-100 range for rounding errors/etc | ||
| 127 | precision => 2; | ||
| 128 | |||
| 129 | =head1 AUTHOR | ||
| 130 | |||
| 131 | Paul Evans <leonerd@leonerd.org.uk> | ||
| 132 | |||
| 133 | =cut | ||
| 134 | |||
| 135 | 3 | 0x55AA; | |