SCD4x.pm

filter: Statementstmt 92.2% 71 / 77 Branchbran 75.0% 3 / 4 Conditioncond 57.1% 8 / 14 MC/DCmcdc 0.0% 0 / 9 Subroutinesub 94.4% 17 / 18 Podpod 66.6% 4 / 6 totaltotal 80.4% 103 / 128 CC 8 SCAR 6.9
CC
8
Cov
86%
read_config
4.2
I2C_options
3.1
_next_read
2.3
CRAP 2.0
« SGP4x.pm ↑ Summary
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 -- leonerd@leonerd.org.uk
5  
6 5 ▶use v5.26;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 5 ▶use Object::Pad 0.800;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 5 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 package Device::Chip::SCD4x 0.03;
13 class Device::Chip::SCD4x
14 4 ▶ :isa(Device::Chip::From::Sensirion);
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 5 ▶use Future::AsyncAwait;
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 5 ▶use Device::Chip::Sensor 0.23 -declare;
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 =encoding UTF-8
21  
22 =head1 NAME
23  
24 C<Device::Chip::SCD4x> - chip driver for F<SCD40> and F<SCD41>
25  
26 =head1 SYNOPSIS
27  
28 =for highlighter language=perl
29  
30 use Device::Chip::SCD4x;
31 use Future::AsyncAwait;
32  
33 my $chip = Device::Chip::SCD4x->new;
34 await $chip->mount( Device::Chip::Adapter::...->new );
35  
36 await $chip->start_periodic_measurement;
37  
38 while(1) {
39 await Future::IO->sleep(1);
40  
41 my ( $co2, $temp, $humid ) = await $chip->maybe_read_measurement
42 or next;
43  
44 printf "CO2 concentration=%dppm ", $co2;
45 printf "Temperature=%.2fC ", $temp;
46 printf "Humidity=%.2f%%\n", $hum;
47 }
48  
49 =head1 DESCRIPTION
50  
51 This L<Device::Chip> subclass provides specific communication to a
52 F<Sensirion> F<SCD40> or F<SCD41> attached to a computer via an I²C adapter.
53  
54 The reader is presumed to be familiar with the general operation of this chip;
55 the documentation here will not attempt to explain or define chip-specific
56 concepts or features, only the use of this module to access them.
57  
58 =cut
59  
60 =head1 MOUNT PARAMETERS
61  
62 =head2 addr
63  
64 The I²C address of the device. Can be specified in decimal, octal or hex with
65 leading C<0> or C<0x> prefixes.
66  
67 =cut
68  
69 4 ▶method I2C_options ( %params )
Subroutine I2C_options: called CC=3 SCAR=11.2
Pod undocumented
70 {
71 4 ▶ my $addr = delete $params{addr} // 0x62;
Condition50%1/2
delete $params{"addr"} // 98
l!l
04
MC/DC0%0/1
delete $params{"addr"} // 98
delete $params{"addr"}
Truth table50%1/2
A
A= delete $params{"addr"}
Aresult
11
00
72 4 ▶ $addr = oct $addr if $addr =~ m/^0/;
Branch: 1 branch
BranchTrueFalseTotal
if $addr =~ /^0/u 0 4 4
73  
74 return (
75 4 addr => $addr,
76 max_bitrate => 400E3,
77 );
78 5 }
79  
80 =head1 METHODS
81  
82 The following methods documented in an C<await> expression return L<Future>
83 instances.
84  
85 =cut
86  
87 =head2 read_config
88  
89 $config = await $chip->read_config;
90  
91 Returns a C<HASH> reference containing the compensation values from chip
92 config.
93  
94 =for highlighter
95  
96 temperature_offset # in degrees C
97 sensor_altitude # in metres
98 ambient_pressure # in hPa
99  
100 =cut
101  
102 5 field %config_f;
103  
104 async method read_config
105 2 ▶{
Subroutine read_config: called CC=4 SCAR=14.5
Pod documented
106 await Future->needs_all(
107 $config_f{temperature_offset} //= $self->_read( 0x2318, 1 ),
108 $config_f{sensor_altitude} //= $self->_read( 0x2322, 1 ),
109 2 ▶ $config_f{ambient_pressure} //= $self->_read( 0xe000, 1 ),
Condition66%6/9
$config_f{"temperature_offset"} //= $self->_read(8984, 1)
l!l&&r!l&&!r
110
$config_f{"sensor_altitude"} //= $self->_read(8994, 1)
l!l&&r!l&&!r
110
$config_f{"ambient_pressure"} //= $self->_read(57344, 1)
l!l&&r!l&&!r
110
MC/DC0%0/2
$config_f{"temperature_offset"} //= $self->_read(8984, 1)
$config_f{"temperature_offset"} $self->_read(8984, 1)
MC/DC0%0/2
$config_f{"sensor_altitude"} //= $self->_read(8994, 1)
$config_f{"sensor_altitude"} $self->_read(8994, 1)
MC/DC0%0/2
$config_f{"ambient_pressure"} //= $self->_read(57344, 1)
$config_f{"ambient_pressure"} $self->_read(57344, 1)
Truth table66%2/3
A //= B
A= $config_f{"temperature_offset"}B= $self->_read(8984, 1)
ABresult
1X1
011
000
Truth table66%2/3
A //= B
A= $config_f{"sensor_altitude"}B= $self->_read(8994, 1)
ABresult
1X1
011
000
Truth table66%2/3
A //= B
A= $config_f{"ambient_pressure"}B= $self->_read(57344, 1)
ABresult
1X1
011
000
110 );
111  
112 return {
113 temperature_offset => ( $config_f{temperature_offset}->result * 175 ) / 0xFFFF,
114 sensor_altitude => $config_f{sensor_altitude}->result,
115 2 ambient_pressure => $config_f{ambient_pressure}->result * 100,
116 };
117 5 }
118  
119 =head2 start_periodic_measurement
120  
121 =for highlighter language=perl
122  
123 await $chip->start_periodic_measurement;
124  
125 Starts periodic measurement mode.
126  
127 =cut
128  
129 1 ▶async method start_periodic_measurement ()
Subroutine start_periodic_measurement: called CC=1 SCAR=0.0
Pod documented
130 1 {
131 1 await $self->_cmd( 0x21b1 );
132 5 }
133  
134 =head2 read_measurement
135  
136 ( $co2concentration, $temperature, $humidity ) = await $chip->read_measurement();
137  
138 Returns the latest sensor reading values. Returns a 3-element list, containing
139 the CO₂ concentration in PPM, temperature in degrees C, and humidity in %RH.
140  
141 =cut
142  
143 4 ▶async method read_measurement ()
Subroutine read_measurement: called CC=1 SCAR=0.0
Pod documented
144 4 {
145 4 my @words = await $self->_read( 0xec05, 3 );
146  
147 return (
148 # CO2
149 4 $words[0],
150 # Temperature
151 -45 + 175 * ( $words[1] / 0xFFFF ),
152 # Humidity
153 # Note: different conversion formula as per SHT4x
154 100 * ( $words[2] / 0xFFFF ),
155 );
156 5 }
157  
158 =head2 maybe_read_measurement
159  
160 ( $co2concentration, $temperature, $humidity ) = await $chip->maybe_read_measurement();
161  
162 If the sensor has a new measurement ready, returns it. Otherwise, returns the
163 last successful measurement reading. After initial startup, this will return
164 an empty list before the first reading is available.
165  
166 =cut
167  
168 5 field @last_measurement;
169 5 ▶async method maybe_read_measurement ()
Subroutine maybe_read_measurement: called CC=2 SCAR=6.9
Pod documented
170 5 {
171 # Ready if the lowest 11 bits are nonzero
172 5 my $ready = 0x07FF & await $self->_read( 0xe4b8, 1 );
173 5 ▶ $ready or return @last_measurement;
Branch: 1 branch
BranchTrueFalseTotal
$ready or return @last_measurement 3 2 5
174  
175 3 return @last_measurement = await $self->read_measurement;
176 5 }
177  
178 0 ▶async method initialize_sensors ()
Subroutine initialize_sensors: not called CC=1 SCAR=6.9
Pod undocumented
179 0 {
180 # Startup delay
181 0 await Future::IO->sleep( 0.05 );
182  
183 0 await $self->start_periodic_measurement;
184 5 }
185  
186 5 field $_pending_next_read_f;
187 method _next_read
188 5 ▶{
Subroutine _next_read: called CC=2 SCAR=8.1
189 return $_pending_next_read_f //=
190 5 ▶ $self->maybe_read_measurement->on_ready(sub { undef $_pending_next_read_f });
Subroutine __ANON__: called CC=1 SCAR=0.6
Condition33%1/3
$_pending_next_read_f //= $self->maybe_read_measurement->on_ready(sub { undef $_pending_next_read_f; } )
l!l&&r!l&&!r
050
MC/DC0%0/2
$_pending_next_read_f //= $self->maybe_read_measurement->on_ready(sub { undef $_pending_next_read_f; } )
$_pending_next_read_f $self->maybe_read_measurement->on_ready(sub { undef $_pending_next_read_f; } )
Truth table33%1/3
A //= B
A= $_pending_next_read_fB= $self->maybe_read_measurement->on_ready(sub { undef $_pending_next_read_f; } )
ABresult
1X1
011
000
191 5 }
192  
193 declare_sensor co2_concentration =>
194 3 ▶ method => async method {
Subroutine __ANON__: called CC=1 SCAR=0.0
195 3 return ( await $self->_next_read )[0];
196 5 },
197 units => "ppm",
198 sanity_bounds => [ 300, 8000 ],
199 precision => 0;
200  
201 declare_sensor temperature =>
202 1 ▶ method => async method {
Subroutine __ANON__: called CC=1 SCAR=0.0
203 1 return ( await $self->_next_read )[1];
204 5 },
205 units => "°C",
206 sanity_bounds => [ -50, 80 ],
207 precision => 2;
208  
209 declare_sensor humidity =>
210 1 ▶ method => async method {
Subroutine __ANON__: called CC=1 SCAR=0.0
211 1 return ( await $self->_next_read )[2];
212 5 },
213 units => "%RH",
214 sanity_bounds => [ -1, 101 ], # give it slight headroom beyond the 0-100 range for rounding errors/etc
215 precision => 2;
216  
217 =head1 AUTHOR
218  
219 Paul Evans <leonerd@leonerd.org.uk>
220  
221 =cut
222  
223 5 0x55AA;
« SGP4x.pm ↑ Summary