blib/lib/Device/Chip/MPL3115A2.pm

filter: Statementstmt 73.2% 167 / 228 Branchbran 75.0% 3 / 4 Conditioncond 66.6% 4 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 70.7% 29 / 41 Podpod 95.4% 21 / 22 totaltotal 73.4% 224 / 305 CC 6 SCAR 3.3
CC
6
Cov
73%
_next_trigger
2.0
_cached_read_ctrlreg
2.0
check_id
2.0
CRAP 1.4
↑ 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, 2015-2023 -- 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 package Device::Chip::MPL3115A2 0.13;
11 class Device::Chip::MPL3115A2
12 5 ▶ :isa(Device::Chip::Base::RegisteredI2C);
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 5 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 5 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 5 ▶use Future::AsyncAwait;
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 5 ▶use Data::Bitfield 0.02 qw( bitfield boolfield enumfield );
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 5 ▶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::MPL3115A2> - chip driver for a F<MPL3115A2>
29  
30 =head1 SYNOPSIS
31  
32 use Device::Chip::MPL3115A2;
33 use Future::AsyncAwait;
34  
35 my $chip = Device::Chip::MPL3115A2->new;
36 await $chip->mount( Device::Chip::Adapter::...->new );
37  
38 printf "Current pressure is %.2f kPa\n",
39 await $chip->read_pressure;
40  
41 =head1 DESCRIPTION
42  
43 This L<Device::Chip> subclass provides specific communication to a
44 F<Freescale Semiconductor> F<MPL3115A2> attached to a computer via an I²C
45 adapter.
46  
47 The reader is presumed to be familiar with the general operation of this chip;
48 the documentation here will not attempt to explain or define chip-specific
49 concepts or features, only the use of this module to access them.
50  
51 =cut
52  
53 method I2C_options
54 4 ▶{
Subroutine I2C_options: called CC=1 SCAR=0.0
Pod undocumented
55 return (
56 # This device has a constant address
57 4 addr => 0x60,
58 max_bitrate => 100E3,
59 );
60 5 }
61  
62 5 ▶use constant WHO_AM_I_ID => 0xC4;
Subroutine BEGIN: called CC=1 SCAR=0.0
63  
64 5 ▶use constant {
Subroutine BEGIN: called CC=1 SCAR=0.0
65 5 REG_STATUS => 0x00,
66 REG_OUT_P_MSB => 0x01,
67 REG_OUT_P_CSB => 0x02,
68 REG_OUT_P_LSB => 0x03,
69 REG_OUT_T_MSB => 0x04,
70 REG_OUT_T_LSB => 0x05,
71 REG_DR_STATUS => 0x06,
72 REG_OUT_P_DELTA_MSB => 0x07,
73 REG_OUT_P_DELTA_CSB => 0x08,
74 REG_OUT_P_DELTA_LSB => 0x09,
75 REG_OUT_T_DELTA_MSB => 0x0A,
76 REG_OUT_T_DELTA_LSB => 0x0B,
77 REG_WHO_AM_I => 0x0C,
78 REG_F_STATUS => 0x0D,
79 REG_F_DATA => 0x0E,
80 REG_F_SETUP => 0x0F,
81 REG_TIME_DLY => 0x10,
82 REG_SYSMOD => 0x11,
83 REG_INT_SOURCE => 0x12,
84 REG_PT_DATA_CFG => 0x13,
85 REG_BAR_IN_MSB => 0x14,
86 REG_BAR_IN_LSB => 0x15,
87 REG_P_TGT_MSB => 0x16,
88 REG_P_TGT_LSB => 0x17,
89 REG_T_TGT => 0x18,
90 REG_P_WND_MSB => 0x19,
91 REG_P_WND_LSB => 0x1A,
92 REG_T_WND => 0x1B,
93 REG_P_MIN_MSB => 0x1C,
94 REG_P_MIN_CSB => 0x1D,
95 REG_P_MIN_LSB => 0x1E,
96 REG_T_MIN_MSB => 0x1F,
97 REG_T_MIN_LSB => 0x20,
98 REG_P_MAX_MSB => 0x21,
99 REG_P_MAX_CSB => 0x22,
100 REG_P_MAX_LSB => 0x23,
101 REG_T_MAX_MSB => 0x24,
102 REG_T_MAX_LSB => 0x25,
103 REG_CTRL_REG1 => 0x26,
104 REG_CTRL_REG2 => 0x27,
105 REG_CTRL_REG3 => 0x28,
106 REG_CTRL_REG4 => 0x29,
107 REG_CTRL_REG5 => 0x2A,
108 REG_OFF_P => 0x2B,
109 REG_OFF_T => 0x2C,
110 REG_OFF_H => 0x2D,
111 5 };
112  
113 # Represent CTRL_REG1 to CTRL_REG3 as one three-byte field
114 bitfield { format => "bytes-LE" }, CTRL_REG =>
115 # CTRL_REG1
116 SBYB => enumfield( 0, qw( STANDBY ACTIVE )),
117 OST => boolfield( 1 ),
118 RST => boolfield( 2 ),
119 OS => enumfield( 3, qw( 1 2 4 8 16 32 64 128 )),
120 RAW => boolfield( 6 ),
121 ALT => boolfield( 7 ),
122  
123 # CTRL_REG2
124 5 ST => enumfield( 8, map { 1 << $_ } 0 .. 15 ),
125 ALARM_SEL => boolfield( 13 ),
126 LOAD_OUTPUT => boolfield( 14 ),
127  
128 # CTRL_REG3
129 IPOL1 => boolfield( 16 ),
130 PP_OD1 => boolfield( 17 ),
131 IPOL2 => boolfield( 20 ),
132 PP_OD2 => boolfield( 21 );
133  
134 # Converted pressure
135 3 ▶async method _mplread_p ( $reg )
Subroutine _mplread_p: called CC=1 SCAR=0.0
136 3 {
137 3 my $v = unpack "L>", "\0" . await $self->read_reg( $reg, 3 );
138 3 return $v / 64
139 5 }
140  
141 # Converted altitude
142 1 ▶async method _mplread_a ( $reg )
Subroutine _mplread_a: called CC=1 SCAR=0.0
143 1 {
144 1 my ( $msb, $lsb ) = unpack "s>C", await $self->read_reg( $reg, 3 );
145 1 return $msb + ( $lsb / 256 );
146 5 }
147  
148 # Converted temperature
149 3 ▶async method _mplread_t ( $reg )
Subroutine _mplread_t: called CC=1 SCAR=0.0
150 3 {
151 3 my ( $msb, $lsb ) = unpack "cC", await $self->read_reg( $reg, 2 );
152 3 return $msb + ( $lsb / 256 );
153 5 }
154  
155 =head1 ACCESSORS
156  
157 The following methods documented in an C<await> expression return L<Future>
158 instances.
159  
160 =cut
161  
162 =head2 read_config
163  
164 $config = await $chip->read_config;
165  
166 Returns a C<HASH> reference of the contents of control registers C<CTRL_REG1>
167 to C<CTRL_REG3>, using fields named from the data sheet.
168  
169 SBYB => "STANDBY" | "ACTIVE"
170 OST => 0 | 1
171 RST => 0 | 1
172 OS => 1 | 2 | 4 | ... | 64 | 128
173 RAW => 0 | 1
174 ALT => 0 | 1
175  
176 ST => 1 | 2 | 4 | ... | 16384 | 32768
177 ALARM_SEL => 0 | 1
178 LOAD_OUTPUT => 0 | 1
179  
180 IPOL1 => 0 | 1
181 PP_OD1 => 0 | 1
182 IPOL2 => 0 | 1
183 PP_OD2 => 0 | 1
184  
185 =head2 change_config
186  
187 await $chip->change_config( %changes );
188  
189 Writes updates to the control registers C<CTRL_REG1> to C<CTRL_REG3>. This
190 will be performed as a read-modify-write operation, so any fields not given
191 as arguments to this method will retain their current values.
192  
193 Note that these two methods use a cache of configuration bytes to make
194 subsequent modifications more efficient. This cache will not respect the
195 "one-shot" nature of the C<OST> and C<RST> bits.
196  
197 =cut
198  
199 5 field $_configbytes;
200  
201 8 ▶async method _cached_read_ctrlreg ()
Subroutine _cached_read_ctrlreg: called CC=2 SCAR=7.0
202 8 {
203 8 ▶ return $_configbytes //= await $self->read_reg( REG_CTRL_REG1, 3 );
Condition66%2/3
$_configbytes //= <custom op await>
l!l&&r!l&&!r
530
MC/DC0%0/2
$_configbytes //= <custom op await>
$_configbytes <custom op await>
Truth table66%2/3
A //= B
A= $_configbytesB= <custom op await>
ABresult
1X1
011
000
204 5 }
205  
206 3 ▶async method read_config ()
Subroutine read_config: called CC=1 SCAR=0.0
Pod documented
207 3 {
208 3 return { unpack_CTRL_REG( await $self->_cached_read_ctrlreg ) };
209 5 }
210  
211 1 ▶async method change_config ( %changes )
Subroutine change_config: called CC=2 SCAR=6.9
Pod documented
212 1 {
213 1 my $config = await $self->read_config;
214  
215 1 $config->{$_} = $changes{$_} for keys %changes;
216  
217 1 my $bytes = $_configbytes = pack_CTRL_REG( %$config );
218  
219 1 await $self->write_reg( REG_CTRL_REG1, $bytes );
220 5 }
221  
222 =head2 get_sealevel_pressure
223  
224 =head2 set_sealevel_pressure
225  
226 $pressure = await $chip->get_sealevel_pressure;
227  
228 await $chip->set_sealevel_pressure( $pressure );
229  
230 Read or write the barometric pressure calibration register which is used to
231 convert pressure to altitude when the chip is in altimeter mode, in Pascals.
232 The default value is 101,326 Pa.
233  
234 =cut
235  
236 0 ▶async method get_sealevel_pressure ()
Subroutine get_sealevel_pressure: not called CC=1 SCAR=6.9
Pod documented
237 0 {
238 0 unpack( "S<", await $self->read_reg( REG_BAR_IN_MSB, 2 ) ) * 2;
239 5 }
240  
241 0 ▶async method set_sealevel_pressure ( $pressure )
Subroutine set_sealevel_pressure: not called CC=1 SCAR=6.9
Pod documented
242 0 {
243 0 await $self->write_reg( REG_BAR_IN_MSB, pack "S<", $pressure / 2 )
244 5 }
245  
246 =head2 read_pressure
247  
248 $pressure = await $chip->read_pressure;
249  
250 Returns the value of the C<OUT_P_*> registers, suitably converted into
251 Pascals. (The chip must be in barometer mode and must I<not> be in C<RAW> mode
252 for the conversion to work).
253  
254 =cut
255  
256 3 ▶async method read_pressure () { return await $self->_mplread_p( REG_OUT_P_MSB ) }
Subroutine read_pressure: called CC=1 SCAR=0.0
Pod documented
257  
258 2 declare_sensor pressure =>
259 2 ▶ method => async method () {
Subroutine __ANON__: called CC=1 SCAR=0.0
260 2 await $self->_next_trigger;
261 2 return await $self->read_pressure;
262 5 },
263 units => "pascals",
264 sanity_bounds => [ 80_000, 120_000 ],
265 precision => 0;
266  
267 =head2 read_altitude
268  
269 $altitude = await $chip->read_altitude;
270  
271 Returns the value of the C<OUT_P_*> registers, suitably converted into metres.
272 (The chip must be in altimeter mode and must I<not> be in C<RAW> mode for the
273 conversion to work).
274  
275 =cut
276  
277 1 ▶async method read_altitude () { return await $self->_mplread_a( REG_OUT_P_MSB ) }
Subroutine read_altitude: called CC=1 SCAR=0.0
Pod documented
278  
279 =head2 read_temperature
280  
281 $temperature = await $chip->read_temperature;
282  
283 Returns the value of the C<OUT_T_*> registers, suitable converted into degrees
284 C. (The chip must I<not> be in C<RAW> mode for the conversion to work).
285  
286 =cut
287  
288 3 ▶async method read_temperature () { return await $self->_mplread_t( REG_OUT_T_MSB ) }
Subroutine read_temperature: called CC=1 SCAR=0.0
Pod documented
289  
290 2 declare_sensor temperature =>
291 2 ▶ method => async method () {
Subroutine __ANON__: called CC=1 SCAR=0.0
292 2 await $self->_next_trigger;
293 2 return await $self->read_temperature;
294 5 },
295 units => "°C",
296 sanity_bounds => [ -50, 80 ],
297 precision => 2;
298  
299 =head2 read_min_pressure
300  
301 =head2 read_max_pressure
302  
303 $pressure = await $chip->read_min_pressure;
304  
305 $pressure = await $chip->read_max_pressure;
306  
307 Returns the values of the C<P_MIN> and C<P_MAX> registers, suitably converted
308 into Pascals.
309  
310 =head2 clear_min_pressure
311  
312 =head2 clear_max_pressure
313  
314 await $chip->clear_min_pressure;
315  
316 await $chip->clear_max_pressure;
317  
318 Clear the C<P_MIN> or C<P_MAX> registers, resetting them to start again from
319 the next measurement.
320  
321 =cut
322  
323 0 ▶async method read_min_pressure () { return await $self->_mplread_p( REG_P_MIN_MSB ) }
Subroutine read_min_pressure: not called CC=1 SCAR=4.6
Pod documented
324 0 ▶async method read_max_pressure () { return await $self->_mplread_p( REG_P_MAX_MSB ) }
Subroutine read_max_pressure: not called CC=1 SCAR=4.6
Pod documented
325  
326 0 ▶async method clear_min_pressure () { return await $self->write_reg( REG_P_MIN_MSB, "\x00\x00\x00" ) }
Subroutine clear_min_pressure: not called CC=1 SCAR=4.6
Pod documented
327 0 ▶async method clear_max_pressure () { return await $self->write_reg( REG_P_MAX_MSB, "\x00\x00\x00" ) }
Subroutine clear_max_pressure: not called CC=1 SCAR=4.6
Pod documented
328  
329 =head2 read_min_altitude
330  
331 =head2 read_max_altitude
332  
333 $altitude = await $chip->read_min_altitude;
334  
335 $altitude = await $chip->read_max_altitude;
336  
337 Returns the values of the C<P_MIN> and C<P_MAX> registers, suitably converted
338 into metres.
339  
340 =cut
341  
342 =head2 clear_min_altitude
343  
344 =head2 clear_max_altitude
345  
346 await $chip->clear_min_altitude;
347  
348 await $chip->clear_max_altitude;
349  
350 Clear the C<P_MIN> or C<P_MAX> registers, resetting them to start again from
351 the next measurement.
352  
353 =cut
354  
355 0 ▶async method read_min_altitude () { return await $self->_mplread_a( REG_P_MIN_MSB ) }
Subroutine read_min_altitude: not called CC=1 SCAR=4.6
Pod documented
356 0 ▶async method read_max_altitude () { return await $self->_mplread_a( REG_P_MAX_MSB ) }
Subroutine read_max_altitude: not called CC=1 SCAR=4.6
Pod documented
357  
358 5 *clear_min_altitude = \&clear_min_pressure;
359 5 *clear_max_altitude = \&clear_max_pressure;
360  
361 =head2 read_min_temperature
362  
363 =head2 read_max_temperature
364  
365 $temperature = await $chip->read_min_temperature;
366  
367 $temperature = await $chip->read_max_temperature;
368  
369 Returns the values of the C<T_MIN> and C<T_MAX> registers, suitably converted
370 into metres.
371  
372 =head2 clear_min_temperature
373  
374 =head2 clear_max_temperature
375  
376 await $chip->clear_min_temperature;
377  
378 await $chip->clear_max_temperature;
379  
380 Clear the C<T_MIN> or C<T_MAX> registers, resetting them to start again from
381 the next measurement.
382  
383 =cut
384  
385 0 ▶async method read_min_temperature () { return await $self->_mplread_t( REG_T_MIN_MSB ) }
Subroutine read_min_temperature: not called CC=1 SCAR=4.6
Pod documented
386 0 ▶async method read_max_temperature () { return await $self->_mplread_t( REG_T_MAX_MSB ) }
Subroutine read_max_temperature: not called CC=1 SCAR=4.6
Pod documented
387  
388 0 ▶async method clear_min_temperature () { return await $self->write_reg( REG_T_MIN_MSB, "\x00\x00" ) }
Subroutine clear_min_temperature: not called CC=1 SCAR=4.6
Pod documented
389 0 ▶async method clear_max_temperature () { return await $self->write_reg( REG_T_MAX_MSB, "\x00\x00" ) }
Subroutine clear_max_temperature: not called CC=1 SCAR=4.6
Pod documented
390  
391 =head1 METHODS
392  
393 =cut
394  
395 =head2 check_id
396  
397 await $chip->check_id;
398  
399 Reads the C<WHO_AM_I> register and checks for a valid ID result. The returned
400 future fails if the expected result is not received.
401  
402 =cut
403  
404 1 ▶async method check_id ()
Subroutine check_id: called CC=2 SCAR=7.0
Pod documented
405 1 {
406 1 my $val = await $self->read_reg( REG_WHO_AM_I, 1 );
407  
408 1 my $id = unpack "C", $val;
409 1 ▶ $id == WHO_AM_I_ID or
Branch: 1 branch
BranchTrueFalseTotal
$id == 196 or die sprintf("Incorrect response from WHO_AM_I register (got %02X, expected %02X)\n", $id, 196) 1 0 1
410 die sprintf "Incorrect response from WHO_AM_I register (got %02X, expected %02X)\n",
411 $id, WHO_AM_I_ID;
412  
413 1 return $self;
414 5 }
415  
416 =head2 start_oneshot
417  
418 await $chip->start_oneshot;
419  
420 Sets the C<OST> bit of C<CTRL_REG1> to start a one-shot measurement when in
421 standby mode. After calling this method you will need to use
422 C<busywait_oneshot> to wait for the measurement to finish, or rely somehow on
423 the interrupts.
424  
425 =cut
426  
427 5 ▶async method start_oneshot ()
Subroutine start_oneshot: called CC=1 SCAR=0.0
Pod documented
428 5 {
429 5 my $bytes = await $self->_cached_read_ctrlreg;
430  
431 5 my $ctrl_reg1 = substr( $bytes, 0, 1 ) | "\x02"; # Set OST bit
432 5 await $self->write_reg( REG_CTRL_REG1, $ctrl_reg1 );
433 5 }
434  
435 =head2 busywait_oneshot
436  
437 await $chip->busywait_oneshot;
438  
439 Repeatedly reads the C<OST> bit of C<CTRL_REG1> until it becomes clear.
440  
441 =cut
442  
443 5 ▶async method busywait_oneshot ()
Subroutine busywait_oneshot: called CC=2 SCAR=6.9
Pod documented
444 5 {
445 5 while(1) {
446 12 my $ctrl_reg1 = await $self->read_reg( REG_CTRL_REG1, 1 );
447 12 ▶ last if not( ord( $ctrl_reg1 ) & 0x02 );
Branch: 1 branch
BranchTrueFalseTotal
unless ord $ctrl_reg1 & 2 5 7 12
448 }
449 5 }
450  
451 =head2 oneshot
452  
453 await $chip->oneshot;
454  
455 A convenient wrapper around C<start_oneshot> and C<busywait_oneshot>.
456  
457 =cut
458  
459 4 ▶async method oneshot ()
Subroutine oneshot: called CC=1 SCAR=0.0
Pod documented
460 4 {
461 4 await $self->start_oneshot;
462 4 await $self->busywait_oneshot;
463 5 }
464  
465 5 field $_pending_trigger;
466  
467 method _next_trigger
468 4 ▶{
Subroutine _next_trigger: called CC=2 SCAR=7.1
469 return $_pending_trigger //=
470 4 ▶ $self->oneshot->on_ready(sub { undef $_pending_trigger; });
Subroutine __ANON__: called CC=1 SCAR=0.1
Condition66%2/3
$_pending_trigger //= $self->oneshot->on_ready(sub { undef $_pending_trigger; } )
l!l&&r!l&&!r
130
MC/DC0%0/2
$_pending_trigger //= $self->oneshot->on_ready(sub { undef $_pending_trigger; } )
$_pending_trigger $self->oneshot->on_ready(sub { undef $_pending_trigger; } )
Truth table66%2/3
A //= B
A= $_pending_triggerB= $self->oneshot->on_ready(sub { undef $_pending_trigger; } )
ABresult
1X1
011
000
471 5 }
472  
473 =head1 AUTHOR
474  
475 Paul Evans <leonerd@leonerd.org.uk>
476  
477 =cut
478  
479 5 0x55AA;
↑ Summary