From/Sensirion.pm

filter: Statementstmt 100% 80 / 80 Branchbran 100% 12 / 12 Conditioncond 66.6% 2 / 3 MC/DCmcdc 50.0% 1 / 2 Subroutinesub 100% 14 / 14 Podpod 100% 1 / 1 totaltotal 98.2% 110 / 112 CC 11 SCAR 13.4
CC
11
Cov
99%
_cmd
8.0
_gen_crc8
4.0
BEGIN
1.0
CRAP 3.8
« SHT4x.pm ↑ Summary SGP4x.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 10 ▶use v5.26;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 10 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 10 ▶use Object::Pad 0.800;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 10 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 package Device::Chip::From::Sensirion 0.03;
13 class Device::Chip::From::Sensirion
14 1 ▶ :isa(Device::Chip);
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 10 ▶use Sublike::Extended 0.29 'method';
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 10 ▶use Future::AsyncAwait;
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 10 ▶use constant PROTOCOL => "I2C";
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 =head1 NAME
23  
24 C<Device::Chip::From::Sensirion> - a collection of chip drivers for F<Sensirion> sensors
25  
26 =head1 DESCRIPTION
27  
28 This distribution contains a number of L<Device::Chip> drivers for various
29 sensor chips manufactured by F<Sensirion>.
30  
31 It also acts as a base class, providing common functionality for often-used
32 operations.
33  
34 =cut
35  
36 # The CRC algorithm from the data sheet
37 82 ▶sub _gen_crc8 ( $bytes )
Subroutine _gen_crc8: called CC=4 SCAR=13.9
38 {
39 82 my $crc = 0xFF;
40 82 foreach my $byte ( unpack "C*", $bytes ) {
41 164 $crc ^= $byte;
42 164 foreach ( 1 .. 8 ) {
43 1312 ▶ $crc = ( $crc << 1 ) ^ ( ( $crc & 0x80 ) ? 0x31 : 0 );
Branch: 1 branch
BranchTrueFalseTotal
$crc & 128 ? : 633 679 1312
44 }
45 164 $crc &= 0xFF;
46 }
47 82 return $crc;
48 }
49  
50 6 ▶sub _pack_with_crc ( $word )
Subroutine _pack_with_crc: called CC=1 SCAR=0.0
51 {
52 6 my $bytes = pack "S>", $word;
53 6 return pack "a2 C", $bytes, _gen_crc8( $bytes );
54 }
55  
56 =head1 METHODS
57  
58 =for highlighter language=perl
59  
60 =cut
61  
62 10 ▶use constant PACK_FORMAT_CMD => "S>";
Subroutine BEGIN: called CC=1 SCAR=0.0
63  
64 30 ▶async method _cmd ( $cmd,
Subroutine _cmd: called CC=8 SCAR=20.8
65 :$words_out = undef,
66 :$delay = undef,
67 30 :$read = 0,
68 30 ) {
69 30 my $bytes_out = pack( $self->PACK_FORMAT_CMD, $cmd );
70  
71 30 ▶ if( $words_out and @$words_out ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($words_out and @$words_out) 3 27 30
Condition66%2/3
$words_out and @$words_out
!ll&&!rl&&r
2703
MC/DC50%1/2
$words_out and @$words_out
$words_out @$words_out
Truth table66%2/3
A and B
A= $words_outB= @$words_out
ABresult
0X0
100
111
72 3 $bytes_out .= _pack_with_crc( $_ ) for @$words_out;
73 }
74  
75 30 ▶ if( !$read ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($read) 1 29 30
76 1 await $self->protocol->write( $bytes_out );
77 1 return;
78 }
79  
80 29 my $protocol = $self->protocol;
81  
82 29 my $bytes_in;
83 29 ▶ if( !defined $delay ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $delay) { } 15 14 29
84 15 $bytes_in = await $protocol->write_then_read( $bytes_out, $read * 3 );
85 }
86 else {
87 14 await $protocol->write( $bytes_out );
88 14 await $protocol->sleep( $delay );
89 14 $bytes_in = await $protocol->read( $read * 3 );
90 }
91  
92 # Bytes contains 3x ( 16bit data, 8bit CRC )
93 29 my @dat = unpack( "(a2 C)*", $bytes_in );
94 29 my @words;
95 29 ▶ while( @dat ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@dat) 54 28 82
96 54 my $word = shift @dat;
97 54 my $crc = shift @dat;
98 54 ▶ die "CRC mismatch on word " . ( scalar @words ) . "\n" if _gen_crc8( $word ) != $crc;
Branch: 1 branch
BranchTrueFalseTotal
if _gen_crc8($word) != $crc 1 53 54
99 53 push @words, unpack "S>", $word;
100 }
101 28 return @words;
102 10 }
103  
104 15 ▶async method _read ( $cmd, $words )
Subroutine _read: called CC=1 SCAR=0.0
105 15 {
106 15 return await $self->_cmd( $cmd, read => $words );
107 10 }
108  
109 # Sensirion chips seem to have a common method for reading their serial number
110  
111 =head2 get_serial_number
112  
113 $bytes = await $chip->get_serial_number;
114  
115 Returns a 6-byte encoding of the chip's internal serial number.
116  
117 =cut
118  
119 2 ▶async method get_serial_number ()
Subroutine get_serial_number: called CC=1 SCAR=0.0
Pod documented
120 2 {
121 2 my @words = await $self->_read( 0x3682, 3 );
122 1 return pack( "(S>)*", @words );
123 10 }
124  
125 =head1 AUTHOR
126  
127 Paul Evans <leonerd@leonerd.org.uk>
128  
129 =cut
130  
131 10 0x55AA;
« SHT4x.pm ↑ Summary SGP4x.pm »