SHT4x.pm

filter: Statementstmt 100% 139 / 139 Branchbran 82.3% 28 / 34 Conditioncond 69.2% 18 / 26 MC/DCmcdc 53.3% 8 / 15 Subroutinesub 100% 22 / 22 Podpod 66.6% 4 / 6 totaltotal 90.5% 219 / 242 CC 28 SCAR 17.4
CC
28
Cov
93%
read_measurement
11.0
change_config
10.2
should_use_heater
4.0
CRAP 5.7
↑ Summary From/Sensirion.pm »
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 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 4 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 package Device::Chip::SHT4x 0.03;
13 class Device::Chip::SHT4x
14 4 ▶ :isa(Device::Chip::From::Sensirion);
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 4 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 4 ▶use Future::AsyncAwait;
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 4 ▶use List::Util qw( first );
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 4 ▶use Device::Chip::Sensor 0.23 -declare;
Subroutine BEGIN: called CC=1 SCAR=0.0
23  
24 =encoding UTF-8
25  
26 =head1 NAME
27  
28 C<Device::Chip::SHT4x> - chip driver for F<SHT40> and F<SHT41>
29  
30 =head1 SYNOPSIS
31  
32 =for highlighter language=perl
33  
34 use Device::Chip::SHT4x;
35 use Future::AsyncAwait;
36  
37 my $chip = Device::Chip::SHT4x->new;
38 await $chip->mount( Device::Chip::Adapter::...->new );
39  
40 while(1) {
41 await Future::IO->sleep(1);
42  
43 my ( $temp, $humid ) = await $chip->read_measurement;
44  
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<SHT40> or F<SHT41> 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 =head2 Heater Operation
59  
60 By default, the heater ability of the chip is not used by the
61 L</read_measurement> method.
62  
63 If enabled by the C<heater_power> and C<heater_duration> settings, then the
64 method might enable the heater at each measurement.
65  
66 If C<heater_interval> is not defined, then the heater will be used for
67 I<every> call to L</read_measurement>, and thus the returned readings will
68 have been performed with that in effect. This will cause the sensor to report
69 the temperature higher than, and humidity lower than, the actual ambient
70 environment.
71  
72 If C<heater_interval> is defined, then the heater will only be activated each
73 call if it hasn't been activated within that time before. Any measurement made
74 within C<heater_cooldown> seconds of the most recent heater operation will not
75 report the actual sensor values, but instead will report the (cached) readings
76 from the most recent non-heater measurement cycle.
77  
78 When using the periodic heater, the heater will only activate each cycle if
79 the previous reading was within both limits set by C<heater_max_temperature>
80 and C<heater_min_humidity>. If either limit is exceeded then the heater will
81 not be activated.
82  
83 =cut
84  
85 =head1 MOUNT PARAMETERS
86  
87 =head2 addr
88  
89 The I²C address of the device. Can be specified in decimal, octal or hex with
90 leading C<0> or C<0x> prefixes.
91  
92 =cut
93  
94 4 ▶method I2C_options ( %params )
Subroutine I2C_options: called CC=3 SCAR=11.2
Pod undocumented
95 {
96 4 ▶ my $addr = delete $params{addr} // 0x44;
Condition50%1/2
delete $params{"addr"} // 68
l!l
04
MC/DC0%0/1
delete $params{"addr"} // 68
delete $params{"addr"}
Truth table50%1/2
A
A= delete $params{"addr"}
Aresult
11
00
97 4 ▶ $addr = oct $addr if $addr =~ m/^0/;
Branch: 1 branch
BranchTrueFalseTotal
if $addr =~ /^0/u 0 4 4
98  
99 return (
100 4 addr => $addr,
101 max_bitrate => 400E3,
102 );
103 4 }
104  
105 # SHT4x uses only 8bit commands
106 4 ▶use constant PACK_FORMAT_CMD => "C";
Subroutine BEGIN: called CC=1 SCAR=0.0
107  
108 =head1 METHODS
109  
110 The following methods documented in an C<await> expression return L<Future>
111 instances.
112  
113 =cut
114  
115 4 ▶use constant CMD_READ_HIGH_NO_HEATER => 0xFD;
Subroutine BEGIN: called CC=1 SCAR=0.0
116 # command to read normally
117 field $read_cmd = CMD_READ_HIGH_NO_HEATER;
118  
119 4 field $heater_interval;
120 field $heater_cooldown = 5;
121 # Don't run the heater past these values
122 4 field $heater_max_temperature = 65;
123 4 field $heater_min_humidity = 80;
124  
125 4 my %CMD_CONFIG = (
126 # cmd => [ precision, heater power, heater duration ],
127 0xE0, [ "low", "off", "off" ],
128 0xF6, [ "medium", "off", "off" ],
129 0xFD, [ "high", "off", "off" ],
130 0x15, [ "high", "20mW", "0.1s" ],
131 0x1E, [ "high", "20mW", "1s" ],
132 0x24, [ "high", "110mW", "0.1s" ],
133 0x2F, [ "high", "110mW", "1s" ],
134 0x32, [ "high", "200mW", "0.1s" ],
135 0x39, [ "high", "200mW", "1s" ],
136 );
137  
138 =head2 read_config
139  
140 $config = await $chip->read_config;
141  
142 Returns a C<HASH> reference containing the configuration.
143  
144 While the chip itself does not have any configuration registers, this driver
145 module stores the following fields directly to influence which measurement
146 commands it uses to request readings from the chip.
147  
148 =for highlighter
149  
150 precision => low | medium | high
151 heater_power => off | 20mW | 110mW | 200mW
152 heater_duration => off | 0.1s | 1s
153 heater_interval => undef | NUM
154 heater_cooldown => undef | NUM
155 heater_max_temperature => NUM
156 heater_min_humidity => NUM
157  
158 The module defaults to C<high> precision, heater off.
159  
160 Due to the way the F<SHT4x> works, the precision field is ignored if the
161 heater is enabled; measurements are always high-precision.
162  
163 The various C<heater_*> settings are described above, under
164 L</Heater Operation>. The C<heater_cooldown> setting defaults to 5 seconds,
165 C<heater_max_temperature> to 65 C, and C<heater_min_humidity> to 80 %RH.
166  
167 =cut
168  
169 8 ▶async method read_config ()
Subroutine read_config: called CC=1 SCAR=0.0
Pod documented
170 8 {
171 8 my ( $precision, $heater_power, $heater_duration ) = @{ $CMD_CONFIG{$read_cmd} };
172  
173 return {
174 8 precision => $precision,
175 heater_power => $heater_power,
176 heater_duration => $heater_duration,
177 heater_interval => $heater_interval,
178 heater_cooldown => $heater_cooldown,
179 heater_max_temperature => $heater_max_temperature,
180 heater_min_humidity => $heater_min_humidity,
181 };
182 4 }
183  
184 =head2 change_config
185  
186 =for highlighter language=perl
187  
188 await $chip->change_config( %changes );
189  
190 Writes updates to the stored configuration settings, which will take effect
191 in the next call to the measurement method.
192  
193 =cut
194  
195 5 ▶async method change_config ( %changes )
Subroutine change_config: called CC=10 SCAR=23.2
Pod documented
196 5 {
197 5 my %config = ( %{ await $self->read_config }, %changes );
198  
199 5 my $new_precision = delete $config{precision};
200 5 ▶ grep { $new_precision eq $_ } qw( high medium low ) or
Branch: 1 branch
BranchTrueFalseTotal
grep {$new_precision eq $_;} "high", "medium", "low" or croak("Unrecognised value '${new_precision}' for 'precision'") 5 0 5
201 croak "Unrecognised value '$new_precision' for 'precision'";
202  
203 5 ▶ my $new_power = delete $config{heater_power} // "off";
Condition50%1/2
delete $config{"heater_power"} // "off"
l!l
50
MC/DC0%0/1
delete $config{"heater_power"} // "off"
delete $config{"heater_power"}
Truth table50%1/2
A
A= delete $config{"heater_power"}
Aresult
11
00
204 5 ▶ grep { $new_power eq $_ } qw( off 20mW 110mW 200mW ) or
Branch: 1 branch
BranchTrueFalseTotal
grep {$new_power eq $_;} "off", "20mW", "110mW", "200mW" or croak("Unrecognised value '${new_power}' for 'heater_power'") 5 0 5
205 croak "Unrecognised value '$new_power' for 'heater_power'";
206  
207 5 ▶ my $new_duration = delete $config{heater_duration} // "off";
Condition50%1/2
delete $config{"heater_duration"} // "off"
l!l
50
MC/DC0%0/1
delete $config{"heater_duration"} // "off"
delete $config{"heater_duration"}
Truth table50%1/2
A
A= delete $config{"heater_duration"}
Aresult
11
00
208 5 ▶ grep { $new_duration eq $_ } qw( off 0.1s 1s ) or
Branch: 1 branch
BranchTrueFalseTotal
grep {$new_duration eq $_;} "off", "0.1s", "1s" or croak("Unrecognised value '${new_duration}' for 'heater_duration'") 5 0 5
209 croak "Unrecognised value '$new_duration' for 'heater_duration'";
210  
211 5 $heater_interval = delete $config{heater_interval};
212  
213 5 $heater_cooldown = delete $config{heater_cooldown};
214  
215 5 $heater_max_temperature = delete $config{heater_max_temperature};
216  
217 5 $heater_min_humidity = delete $config{heater_min_humidity};
218  
219 5 ▶ keys %config and
Branch: 1 branch
BranchTrueFalseTotal
keys %config and croak('Unrecognised configuration keys: ' . join(", ", sort(keys %config))) 0 5 5
220 croak "Unrecognised configuration keys: " . join( ", ", sort keys %config );
221  
222 5 ▶ if( $new_power eq "off" ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($new_power eq 'off') { } 2 3 5
223 2 $new_duration = "off";
224 }
225 else {
226 # with heater on, precision must be high
227 3 $new_precision = "high";
228 }
229  
230 27 ▶ my $new_cmd = first {
Subroutine __ANON__: called CC=3 SCAR=11.0
231 27 my $conf = $CMD_CONFIG{$_};
232 27 ▶ $conf->[0] eq $new_precision &&
Branch: 1 branch
BranchTrueFalseTotal
$conf->[0] eq $new_precision and $conf->[1] eq $new_power and $conf->[2] eq $new_duration 8 19 27
Condition100%3/3
$conf->[0] eq $new_precision and $conf->[1] eq $new_power
!ll&&!rl&&r
1098
MC/DC100%2/2
$conf->[0] eq $new_precision and $conf->[1] eq $new_power
$conf->[0] eq $new_precision $conf->[1] eq $new_power
Truth table100%3/3
A and B
A= $conf->[0] eq $new_precisionB= $conf->[1] eq $new_power
ABresult
0X0
100
111
233 $conf->[1] eq $new_power &&
234 $conf->[2] eq $new_duration
235 5 } keys %CMD_CONFIG;
236  
237 5 ▶ defined $new_cmd or
Branch: 1 branch
BranchTrueFalseTotal
defined $new_cmd or croak("Unable to find a read command for prec=$new_precision pow=$new_power dur=$new_duration") 5 0 5
238 croak "Unable to find a read command for prec=$new_precision pow=$new_power dur=$new_duration";
239  
240 5 ▶ if( $new_power eq "off" ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($new_power eq 'off') { } 2 3 5
241 2 $read_cmd = $new_cmd;
242 2 $heater_interval = undef;
243 }
244 else {
245 3 $read_cmd = $new_cmd;
246 }
247 4 }
248  
249 1 ▶async method get_serial_number ()
Subroutine get_serial_number: called CC=1 SCAR=0.0
Pod documented
250 1 {
251 1 my @words = await $self->_read( 0x89, 2 );
252 1 return pack( "(S>)*", @words );
253 4 }
254  
255 =head2 read_measurement
256  
257 ( $temperature, $humidity ) = await $chip->read_measurement();
258  
259 Performs a measurement and returns the sensor reading values. Returns a
260 2-element list, containing temperature in degrees C, and humidity in %RH.
261  
262 =cut
263  
264 4 my %DELAY_FOR_CMD = (
265 # cmd, delay
266 0x15, 0.110, # high, 20mW, 0.1s
267 0x1E, 1.010, # high, 20mW, 1s
268 0x24, 0.110, # high, 110mW, 0.1s
269 0x2F, 1.010, # high, 110mW, 1s
270 0x32, 0.110, # high, 200mW, 0.1s
271 0x39, 1.010, # high, 200mW, 1s
272 0xE0, 0.002, # high, off
273 0xF6, 0.005, # medium, off
274 0xFD, 0.010, # low, off
275 );
276  
277 4 field $last_read_with_heater;
278 4 field $last_temperature;
279 4 field $last_humidity;
280  
281 6 ▶method should_use_heater ( $now )
Subroutine should_use_heater: called CC=4 SCAR=13.9
Pod undocumented
282 {
283 6 ▶ return 0 if $now < $last_read_with_heater + $heater_interval;
Branch: 1 branch
BranchTrueFalseTotal
if $now < $last_read_with_heater + $heater_interval 2 4 6
284  
285 4 ▶ return 0 if $last_temperature > $heater_max_temperature;
Branch: 1 branch
BranchTrueFalseTotal
if $last_temperature > $heater_max_temperature 1 3 4
286 3 ▶ return 0 if $last_humidity < $heater_min_humidity;
Branch: 1 branch
BranchTrueFalseTotal
if $last_humidity < $heater_min_humidity 1 2 3
287  
288 2 return 1;
289 4 }
290  
291 12 ▶async method read_measurement ()
Subroutine read_measurement: called CC=11 SCAR=24.0
Pod documented
292 12 {
293 12 my $use_heater;
294 my $use_last_cache;
295  
296 12 ▶ if( defined $last_temperature and defined $heater_interval ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $last_temperature and defined $heater_interval) 6 6 12
Condition100%3/3
defined $last_temperature and defined $heater_interval
!ll&&!rl&&r
246
MC/DC100%2/2
defined $last_temperature and defined $heater_interval
defined $last_temperature defined $heater_interval
Truth table100%3/3
A and B
A= defined $last_temperatureB= defined $heater_interval
ABresult
0X0
100
111
297 6 my $now = time();
298 6 ▶ $last_read_with_heater //= 0;
Condition100%2/2
$last_read_with_heater //= 0
l!l
51
MC/DC100%1/1
$last_read_with_heater //= 0
$last_read_with_heater
Truth table100%2/2
A
A= $last_read_with_heater
Aresult
11
00
299 6 ▶ if( $self->should_use_heater( $now ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->should_use_heater($now)) 2 4 6
300 2 $use_heater = 1;
301 2 $last_read_with_heater = $now;
302 }
303  
304 6 ▶ $use_last_cache = defined $last_temperature &&
Condition50%3/6
defined $last_temperature && defined $heater_cooldown && $now - $last_read_with_heater < $heater_cooldown
!ll&&!rl&&r
033
defined $last_temperature and defined $heater_cooldown
!ll&&!rl&&r
006
MC/DC33%1/3
defined $last_temperature && defined $heater_cooldown && $now - $last_read_with_heater < $heater_cooldown
defined $last_temperature defined $heater_cooldown $now - $last_read_with_heater < $heater_cooldown
Truth table50%2/4
A and B && C
A= defined $last_temperatureB= defined $heater_cooldownC= $now - $last_read_with_heater < $heater_cooldown
ABCresult
0XX0
10X0
1100
1111
305 defined $heater_cooldown &&
306 $now - $last_read_with_heater < $heater_cooldown;
307 }
308  
309 12 my $cmd = $read_cmd;
310 12 ▶ if( defined $heater_interval and !$use_heater ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $heater_interval and not $use_heater) 4 8 12
Condition100%3/3
defined $heater_interval and not $use_heater
!ll&&!rl&&r
624
MC/DC100%2/2
defined $heater_interval and not $use_heater
defined $heater_interval not $use_heater
Truth table100%3/3
A and not(B)
A= defined $heater_intervalB= not $use_heater
ABresult
0X0
100
111
311 4 $cmd = CMD_READ_HIGH_NO_HEATER;
312  
313 4 ▶ return ( $last_temperature, $last_humidity ) if $use_last_cache;
Branch: 1 branch
BranchTrueFalseTotal
if $use_last_cache 1 3 4
314 }
315  
316 my @words = await $self->_cmd( $cmd,
317 11 delay => $DELAY_FOR_CMD{$cmd},
318 read => 2,
319 );
320  
321 11 ▶ return ( $last_temperature, $last_humidity ) if $use_last_cache;
Branch: 1 branch
BranchTrueFalseTotal
if $use_last_cache 2 9 11
322  
323 return (
324 # Temperature
325 9 $last_temperature = -45 + 175 * ( $words[0] / 0xFFFF ),
326 # Humidity
327 # Note: different conversion formula as per SCD4x
328 $last_humidity = -6 + 125 * ( $words[1] / 0xFFFF ),
329 );
330 4 }
331  
332 4 field $_pending_next_read_f;
333 method _next_read
334 2 ▶{
Subroutine _next_read: called CC=2 SCAR=8.1
335 return $_pending_next_read_f //=
336 2 ▶ $self->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->read_measurement->on_ready(sub { undef $_pending_next_read_f; } )
l!l&&r!l&&!r
020
MC/DC0%0/2
$_pending_next_read_f //= $self->read_measurement->on_ready(sub { undef $_pending_next_read_f; } )
$_pending_next_read_f $self->read_measurement->on_ready(sub { undef $_pending_next_read_f; } )
Truth table33%1/3
A //= B
A= $_pending_next_read_fB= $self->read_measurement->on_ready(sub { undef $_pending_next_read_f; } )
ABresult
1X1
011
000
337 4 }
338  
339 declare_sensor temperature =>
340 1 ▶ method => async method {
Subroutine __ANON__: called CC=1 SCAR=0.0
341 1 return ( await $self->_next_read )[0];
342 4 },
343 units => "°C",
344 sanity_bounds => [ -50, 80 ],
345 precision => 2;
346  
347 declare_sensor humidity =>
348 1 ▶ method => async method {
Subroutine __ANON__: called CC=1 SCAR=0.0
349 1 return ( await $self->_next_read )[1];
350 4 },
351 units => "%RH",
352 sanity_bounds => [ -1, 101 ], # give it slight headroom beyond the 0-100 range for rounding errors/etc
353 precision => 2;
354  
355 =head1 AUTHOR
356  
357 Paul Evans <leonerd@leonerd.org.uk>
358  
359 =cut
360  
361 4 0x55AA;
↑ Summary From/Sensirion.pm »