blib/lib/Device/Chip/HTU21D.pm

filter: Statementstmt 100% 102 / 102 Branchbran 62.5% 5 / 8 Conditioncond 66.6% 4 / 6 MC/DCmcdc 25.0% 1 / 4 Subroutinesub 100% 21 / 21 Podpod 80.0% 4 / 5 totaltotal 93.8% 137 / 146 CC 8 SCAR 7.0
CC
8
Cov
96%
__ANON__
5.1
change_config
3.0
_trigger_nohold
2.0
CRAP 2.0
↑ 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, 2016-2023 -- 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::HTU21D 0.10;
11 class Device::Chip::HTU21D
12 1 ▶ :isa(Device::Chip);
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 4 ▶use utf8;
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 Data::Bitfield 0.02 qw( bitfield boolfield );
Subroutine BEGIN: called CC=1 SCAR=0.0
19 4 ▶use List::Util qw( first );
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 4 ▶use Future::AsyncAwait 0.38; # async method
Subroutine BEGIN: called CC=1 SCAR=0.0
22 4 ▶use Future::Mutex;
Subroutine BEGIN: called CC=1 SCAR=0.0
23  
24 4 ▶use Device::Chip::Sensor -declare;
Subroutine BEGIN: called CC=1 SCAR=0.0
25  
26 4 ▶use constant PROTOCOL => "I2C";
Subroutine BEGIN: called CC=1 SCAR=0.0
27  
28 =encoding UTF-8
29  
30 =head1 NAME
31  
32 C<Device::Chip::HTU21D> - chip driver for F<HTU21D>
33  
34 =head1 SYNOPSIS
35  
36 use Device::Chip::HTU21D;
37 use Future::AsyncAwait;
38  
39 my $chip = Device::Chip::HTU21D->new;
40 await $chip->mount( Device::Chip::Adapter::...->new );
41  
42 printf "Current temperature is is %.2f C\n",
43 await $chip->read_temperature;
44  
45 =head1 DESCRIPTION
46  
47 This L<Device::Chip> subclass provides specific communication to a
48 F<TE Connectivity> F<HTU21D> attached to a computer via an 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 sub I2C_options
57 3 ▶{
Subroutine I2C_options: called CC=1 SCAR=0.0
Pod undocumented
58 3 my $self = shift;
59  
60 return (
61 3 addr => 0x40,
62 max_bitrate => 400E3,
63 );
64 }
65  
66 =head1 ACCESSORS
67  
68 The following methods documented in an C<await> expression return L<Future>
69 instances.
70  
71 =cut
72  
73 4 ▶use constant {
Subroutine BEGIN: called CC=1 SCAR=0.0
74 # First-byte commands
75 4 CMD_TRIGGER_TEMP_HOLD => 0xE3,
76 CMD_TRIGGER_HUMID_HOLD => 0xE5,
77 CMD_TRIGGER_TEMP_NOHOLD => 0xF3,
78 CMD_TRIGGER_HUMID_NOHOLD => 0xF5,
79 CMD_WRITE_REG => 0xE6,
80 CMD_READ_REG => 0xE7,
81 CMD_SOFT_RESET => 0xFE,
82 4 };
83  
84 4 bitfield { format => "bytes-LE" }, REG_USER =>
85 RES0 => boolfield( 0 ),
86 OTPDISABLE => boolfield( 1 ),
87 HEATER => boolfield( 2 ),
88 ENDOFBATT => boolfield( 6 ),
89 RES1 => boolfield( 7 );
90  
91 =head2 read_config
92  
93 $config = await $chip->read_config;
94  
95 Returns a C<HASH> reference of the contents of the user register.
96  
97 RES => "12/14" | "11/11" | "10/13" | "8/12"
98 OTPDISABLE => 0 | 1
99 HEATER => 0 | 1
100 ENDOFBATT => 0 | 1
101  
102 =head2 change_config
103  
104 await $chip->change_config( %changes );
105  
106 Writes updates to the user register.
107  
108 =cut
109  
110 4 my @RES_VALUES = ( "12/14", "8/12", "10/13", "11/11" );
111  
112 3 ▶async method read_config ()
Subroutine read_config: called CC=1 SCAR=0.0
Pod documented
113 3 {
114 3 my %config = unpack_REG_USER(
115 await $self->protocol->write_then_read( pack( "C", CMD_READ_REG ), 1 )
116 );
117  
118 3 my $res = ( delete $config{RES0} ) | ( delete $config{RES1} ) << 1;
119 3 $config{RES} = $RES_VALUES[$res];
120  
121 3 return \%config;
122 4 }
123  
124 2 ▶async method change_config ( %changes )
Subroutine change_config: called CC=3 SCAR=11.0
Pod documented
125 2 {
126 2 my $config = await $self->read_config;
127  
128 2 $config->{$_} = $changes{$_} for keys %changes;
129  
130 2 my $res = delete $config->{RES};
131  
132 2 ▶ $res = first { $RES_VALUES[$_] eq $res } 0 .. 3;
Subroutine __ANON__: called CC=1 SCAR=0.0
133 2 ▶ defined $res or
Branch: 1 branch
BranchTrueFalseTotal
defined $res or croak(qq[Unrecognised new value for RES - '$changes{"RES"}']) 2 0 2
134 croak "Unrecognised new value for RES - '$changes{RES}'";
135  
136 2 my $val = pack_REG_USER(
137 RES0 => $res & ( 1<<0 ),
138 RES1 => $res & ( 1<<1 ),
139 %$config,
140 );
141  
142 2 await $self->protocol->write( pack "C a", CMD_WRITE_REG, $val );
143 4 }
144  
145 4 field $_mutex;
146  
147 7 ▶async method _trigger_nohold ( $cmd )
Subroutine _trigger_nohold: called CC=2 SCAR=7.0
148 7 {
149 7 my $protocol = $self->protocol;
150  
151 7 ▶ $_mutex //= Future::Mutex->new;
Condition66%2/3
$_mutex //= "Future::Mutex"->new
l!l&&r!l&&!r
520
MC/DC0%0/2
$_mutex //= "Future::Mutex"->new
$_mutex "Future::Mutex"->new
Truth table66%2/3
A //= B
A= $_mutexB= "Future::Mutex"->new
ABresult
1X1
011
000
152  
153 7 ▶ return await $_mutex->enter( async sub {
Subroutine __ANON__: called CC=5 SCAR=16.3
154 7 await $self->protocol->write( pack "C", $cmd );
155  
156 7 my $attempts = 10;
157 7 ▶ while( $attempts ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($attempts) 8 0 8
158 8 my $f = $protocol->read( 2 );
159 8 ▶ $attempts-- and $f = $f->else_done( undef );
Branch: 1 branch
BranchTrueFalseTotal
$attempts-- and $f = $f->else_done(undef) 8 0 8
160  
161 8 my $bytes = await $f;
162 # TODO: Is this a tinyUSSB bug? We're successfully reading two 0xFF
163 # bytes before it's ready
164 8 ▶ defined $bytes and $bytes ne "\xFF\xFF" and
Branch: 1 branch
BranchTrueFalseTotal
defined $bytes and $bytes ne "\377\377" and return unpack("S>", $bytes) 7 1 8
Condition66%2/3
defined $bytes and $bytes ne "\377\377"
!ll&&!rl&&r
107
MC/DC50%1/2
defined $bytes and $bytes ne "\377\377"
defined $bytes $bytes ne "\377\377"
Truth table66%2/3
A and B
A= defined $bytesB= $bytes ne "\377\377"
ABresult
0X0
100
111
165 return unpack "S>", $bytes;
166  
167 1 await $protocol->sleep( 0.01 );
168 }
169 7 });
170 4 }
171  
172 =head1 METHODS
173  
174 =cut
175  
176 =head2 read_temperature
177  
178 $temperature = await $chip->read_temperature;
179  
180 Triggers a reading of the temperature sensor, returning a number in degrees C.
181  
182 =cut
183  
184 4 declare_sensor temperature =>
185 units => "°C",
186 precision => 2;
187  
188 4 ▶async method read_temperature ()
Subroutine read_temperature: called CC=1 SCAR=0.0
Pod documented
189 4 {
190 4 my $val = await $self->_trigger_nohold( CMD_TRIGGER_TEMP_NOHOLD );
191  
192 4 return -46.85 + 175.72 * ( $val / 2**16 );
193 4 }
194  
195 =head2 read_humidity
196  
197 $humidity = await $chip->read_humidity;
198  
199 Triggers a reading of the humidity sensor, returning a number in % RH.
200  
201 =cut
202  
203 4 declare_sensor humidity =>
204 units => "%RH",
205 precision => 1;
206  
207 3 ▶async method read_humidity ()
Subroutine read_humidity: called CC=1 SCAR=0.0
Pod documented
208 3 {
209 3 my $val = await $self->_trigger_nohold( CMD_TRIGGER_HUMID_NOHOLD );
210  
211 3 return -6 + 125 * ( $val / 2**16 );
212 4 }
213  
214 =head1 AUTHOR
215  
216 Paul Evans <leonerd@leonerd.org.uk>
217  
218 =cut
219  
220 4 0x55AA;
↑ Summary