| 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, 2024-2026 -- leonerd@leonerd.org.uk | ||||||||||||
| 5 | |||||||||||||
| 6 | 2 | ▶ | use v5.26; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 7 | 2 | ▶ | use warnings; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 8 | 2 | ▶ | use Object::Pad 0.800; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 9 | |||||||||||||
| 10 | 2 | ▶ | use utf8; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 11 | |||||||||||||
| 12 | package Device::Chip::SGP4x 0.03; | ||||||||||||
| 13 | class Device::Chip::SGP4x | ||||||||||||
| 14 | 1 | ▶ | :isa(Device::Chip::From::Sensirion); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 15 | |||||||||||||
| 16 | 2 | ▶ | use Sublike::Extended 0.29 'method'; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 17 | |||||||||||||
| 18 | 2 | ▶ | use Future::AsyncAwait; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 19 | |||||||||||||
| 20 | =encoding UTF-8 | ||||||||||||
| 21 | |||||||||||||
| 22 | =head1 NAME | ||||||||||||
| 23 | |||||||||||||
| 24 | C<Device::Chip::SGP4x> - chip driver for F<SGP40> and F<SGP41> | ||||||||||||
| 25 | |||||||||||||
| 26 | =head1 SYNOPSIS | ||||||||||||
| 27 | |||||||||||||
| 28 | =for highlighter language=perl | ||||||||||||
| 29 | |||||||||||||
| 30 | use Device::Chip::SGP4x; | ||||||||||||
| 31 | use Future::AsyncAwait; | ||||||||||||
| 32 | |||||||||||||
| 33 | my $chip = Device::Chip::SGP4x->new; | ||||||||||||
| 34 | await $chip->mount( Device::Chip::Adapter::...->new ); | ||||||||||||
| 35 | |||||||||||||
| 36 | await $chip->execute_conditioning; | ||||||||||||
| 37 | |||||||||||||
| 38 | while(1) { | ||||||||||||
| 39 | await Future::IO->sleep(1); | ||||||||||||
| 40 | |||||||||||||
| 41 | my ( $raw_NOx, $raw_VOC ) = await $chip->measure_raw_signals; | ||||||||||||
| 42 | printf "NOx = %d, VOC = %d\n", $raw_NOx, $raw_VOC; | ||||||||||||
| 43 | } | ||||||||||||
| 44 | |||||||||||||
| 45 | =head1 DESCRIPTION | ||||||||||||
| 46 | |||||||||||||
| 47 | This L<Device::Chip> subclass provides specific communication to a | ||||||||||||
| 48 | F<Sensirion> F<SGP40> or F<SGP41> attached to a computer via I²C adapter. | ||||||||||||
| 49 | |||||||||||||
| 50 | The reader is presumed to be familiar with the general operation of this chip; | ||||||||||||
| 51 | the documentation here will not attempt to explain or define chip-specific | ||||||||||||
| 52 | concepts or features, only the use of this module to access them. | ||||||||||||
| 53 | |||||||||||||
| 54 | =cut | ||||||||||||
| 55 | |||||||||||||
| 56 | =head1 MOUNT PARAMETERS | ||||||||||||
| 57 | |||||||||||||
| 58 | =head2 addr | ||||||||||||
| 59 | |||||||||||||
| 60 | The I²C address of the device. Can be specified in decimal, octal or hex with | ||||||||||||
| 61 | leading C<0> or C<0x> prefixes. | ||||||||||||
| 62 | |||||||||||||
| 63 | =cut | ||||||||||||
| 64 | |||||||||||||
| 65 | 1 | ▶ | method I2C_options ( %params ) | ||||||||||
|
Subroutine
I2C_options: called CC=3 SCAR=11.2
Pod
undocumented
| |||||||||||||
| 66 | { | ||||||||||||
| 67 | 1 | ▶ | my $addr = delete $params{addr} // 0x59; | ||||||||||
|
Condition50%1/2
delete $params{"addr"} // 89
MC/DC0%0/1
delete $params{"addr"} // 89
delete $params{"addr"}
Truth table50%1/2
A
A= delete $params{"addr"}
| |||||||||||||
| 68 | 1 | ▶ | $addr = oct $addr if $addr =~ m/^0/; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 69 | |||||||||||||
| 70 | return ( | ||||||||||||
| 71 | 1 | addr => $addr, | |||||||||||
| 72 | max_bitrate => 400E3, | ||||||||||||
| 73 | ); | ||||||||||||
| 74 | 2 | } | |||||||||||
| 75 | |||||||||||||
| 76 | =head1 METHODS | ||||||||||||
| 77 | |||||||||||||
| 78 | The following methods documented in an C<await> expression return L<Future> | ||||||||||||
| 79 | instances. | ||||||||||||
| 80 | |||||||||||||
| 81 | =cut | ||||||||||||
| 82 | |||||||||||||
| 83 | =head2 execute_conditioning | ||||||||||||
| 84 | |||||||||||||
| 85 | await $chip->execute_conditioning; | ||||||||||||
| 86 | |||||||||||||
| 87 | Performs a conditioning operation. | ||||||||||||
| 88 | |||||||||||||
| 89 | =cut | ||||||||||||
| 90 | |||||||||||||
| 91 | 1 | ▶ | async method execute_conditioning () | ||||||||||
|
Subroutine
execute_conditioning: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||
| 92 | 1 | { | |||||||||||
| 93 | 1 | my $result = await $self->_cmd( 0x2612, | |||||||||||
| 94 | words_out => [ 0x8000, 0x6666 ], | ||||||||||||
| 95 | delay => 0.050, | ||||||||||||
| 96 | read => 1, | ||||||||||||
| 97 | ); | ||||||||||||
| 98 | |||||||||||||
| 99 | 1 | return; | |||||||||||
| 100 | 2 | } | |||||||||||
| 101 | |||||||||||||
| 102 | =head2 execute_self_test | ||||||||||||
| 103 | |||||||||||||
| 104 | await $chip->execute_self_test; | ||||||||||||
| 105 | |||||||||||||
| 106 | Performs a self-test operation. | ||||||||||||
| 107 | |||||||||||||
| 108 | =cut | ||||||||||||
| 109 | |||||||||||||
| 110 | 0 | ▶ | async method execute_self_test () | ||||||||||
|
Subroutine
execute_self_test: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||
| 111 | 0 | { | |||||||||||
| 112 | 0 | my $result = await $self->_cmd( 0x280E, delay => 0.320, read => 1 ); | |||||||||||
| 113 | |||||||||||||
| 114 | 0 | my $NOx_OK = $result & (1<<1); | |||||||||||
| 115 | 0 | my $VOC_OK = $result & (1<<0); | |||||||||||
| 116 | |||||||||||||
| 117 | 0 | ▶ | die "NOx failed" if !$NOx_OK; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 118 | 0 | ▶ | die "VOC failed" if !$VOC_OK; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 119 | |||||||||||||
| 120 | 0 | return; | |||||||||||
| 121 | 2 | } | |||||||||||
| 122 | |||||||||||||
| 123 | =head2 measure_raw_signals | ||||||||||||
| 124 | |||||||||||||
| 125 | ( $adc_VOC, $adc_NOx ) = await $chip->measure_raw_signals; | ||||||||||||
| 126 | |||||||||||||
| 127 | Performs a sampling cycle and returns the raw ADC values from the sensor | ||||||||||||
| 128 | elements. | ||||||||||||
| 129 | |||||||||||||
| 130 | Optionally, compensation values for humidity and temperature can be passed as | ||||||||||||
| 131 | named parameters. If not supplied, the following defaults apply: | ||||||||||||
| 132 | |||||||||||||
| 133 | ( $adc_VOC, $adc_NOx ) = await $chip->measure_raw_signals( | ||||||||||||
| 134 | humidity => 50, # in units of % RH | ||||||||||||
| 135 | temperature => 25, # in units of degrees C | ||||||||||||
| 136 | ); | ||||||||||||
| 137 | |||||||||||||
| 138 | =cut | ||||||||||||
| 139 | |||||||||||||
| 140 | 4 | ▶ | my sub _clamp_u16 ( $val ) | ||||||||||
|
Subroutine
_clamp_u16: called CC=3 SCAR=11.7
| |||||||||||||
| 141 | { | ||||||||||||
| 142 | 4 | ▶ | return 0 if $val < 0; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 143 | 4 | ▶ | return 0xFFFF if $val > 0xFFFF; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 144 | 4 | return int( $val + 0.5 ); | |||||||||||
| 145 | } | ||||||||||||
| 146 | |||||||||||||
| 147 | 2 | ▶ | async method measure_raw_signals ( | ||||||||||
|
Subroutine
measure_raw_signals: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||
| 148 | 2 | :$humidity = 50, | |||||||||||
| 149 | :$temperature = 25, | ||||||||||||
| 150 | 2 | ) { | |||||||||||
| 151 | 2 | my $hword = _clamp_u16 $humidity * 0xFFFF / 100; | |||||||||||
| 152 | 2 | my $tword = _clamp_u16 +( $temperature + 45 ) * 0xFFFF / 175; | |||||||||||
| 153 | |||||||||||||
| 154 | 2 | my @words = await $self->_cmd( 0x2619, | |||||||||||
| 155 | words_out => [ $hword, $tword ], | ||||||||||||
| 156 | delay => 0.050, | ||||||||||||
| 157 | read => 2, | ||||||||||||
| 158 | ); | ||||||||||||
| 159 | |||||||||||||
| 160 | 2 | return @words; | |||||||||||
| 161 | 2 | } | |||||||||||
| 162 | |||||||||||||
| 163 | =head1 AUTHOR | ||||||||||||
| 164 | |||||||||||||
| 165 | Paul Evans <leonerd@leonerd.org.uk> | ||||||||||||
| 166 | |||||||||||||
| 167 | =cut | ||||||||||||
| 168 | |||||||||||||
| 169 | 2 | 0x55AA; | |||||||||||