blib/lib/Protocol/IR/Proto/Sony.pm

filter: Statementstmt 88.6% 94 / 106 Branchbran 72.2% 26 / 36 Conditioncond 54.2% 19 / 35 MC/DCmcdc 23.5% 4 / 17 Subroutinesub 91.6% 22 / 24 Podpod n/a totaltotal 75.6% 165 / 218 CC 36 SCAR 23.7
CC
36
Cov
79%
decode_raw
20.0
decode_timing
18.0
decode_params
17.4
CRAP 10.7
« blib/lib/Protocol/IR/Format/Tasmota.pm ↑ Summary blib/lib/Protocol/IR/Proto/NEC.pm »
1 package Protocol::IR::Proto::Sony;
2 55 our $VERSION = '1.5';
3  
4 package Protocol::IR::Proto::Sony12;
5 55 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 55 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 55 our $VERSION = '1.5';
9  
10 55 ▶use Protocol::IR::Code;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 0 ▶sub new { bless {}, shift }
Subroutine new: not called CC=1 SCAR=6.9
13  
14 1617 ▶sub bits { 12 }
Subroutine bits: called CC=1 SCAR=0.0
15 8 ▶sub address_bits { 5 }
Subroutine address_bits: called CC=1 SCAR=0.0
16 28 ▶sub extended_bits { 0 }
Subroutine extended_bits: called CC=1 SCAR=0.0
17  
18 18 ▶sub _reverse_bits {
Subroutine _reverse_bits: called CC=2 SCAR=6.9
19 18 my ($value, $bits) = @_;
20 18 my $out = 0;
21 18 for my $i (0 .. $bits - 1) {
22 282 $out |= (($value >> $i) & 1) << ($bits - 1 - $i);
23 }
24 18 return $out;
25 }
26  
27 6 ▶sub _logical_value {
Subroutine _logical_value: called CC=2 SCAR=6.9
28 6 my ($class, $addr, $ext, $cmd) = @_;
29 6 my $v = $addr & ((1 << $class->address_bits) - 1);
30 6 ▶ if ($class->extended_bits) {
Branch: 1 branch
BranchTrueFalseTotal
if ($class->extended_bits) 2 4 6
31 2 $v |= ($ext & ((1 << $class->extended_bits) - 1)) << $class->address_bits;
32 }
33 6 $v = ($v << 7) | ($cmd & 0x7f);
34 6 return $v;
35 }
36  
37 0 ▶sub decode_raw {
Subroutine decode_raw: not called CC=4 SCAR=30.0
38 0 my ($class, $raw) = @_;
39 0 my $transmitted = $raw + 0;
40 0 ▶ $transmitted = hex($transmitted) if $transmitted =~ /^0x/i;
Branch: 1 branch
BranchTrueFalseTotal
if $transmitted =~ /^0x/i 0 0 0
41 0 my $logical = _reverse_bits($transmitted, $class->bits);
42 0 my $cmd = $logical & 0x7f;
43 0 my $addr = ($logical >> 7) & ((1 << $class->address_bits) - 1);
44 0 my $ext = 0;
45 0 ▶ if ($class->extended_bits) {
Branch: 1 branch
BranchTrueFalseTotal
if ($class->extended_bits) 0 0 0
46 0 $ext = ($logical >> (7 + $class->address_bits)) & ((1 << $class->extended_bits) - 1);
47 }
48 0 ▶ return Protocol::IR::Code->new(
Branch: 1 branch
BranchTrueFalseTotal
$class->extended_bits ? : 0 0 0
49 protocol => $class->_protocol_name,
50 bits => $class->bits,
51 address => $addr,
52 subaddress => $class->extended_bits ? $ext : -1,
53 command => $cmd,
54 data => $logical,
55 );
56 }
57  
58 3 ▶sub decode_params {
Subroutine decode_params: called CC=11 SCAR=28.6
59 3 my ($class, %args) = @_;
60 3 ▶ my $addr = $args{address} // $args{device} // 0;
Condition40%2/5
($args{"address"} // $args{"device"}) // 0
l!l
30
$args{"address"} // $args{"device"}
l!l&&r!l&&!r
300
MC/DC0%0/2
($args{"address"} // $args{"device"}) // 0
$args{"address"} $args{"device"}
Truth table33%1/3
A // B
A= $args{"address"}B= $args{"device"}
ABresult
1X1
011
000
61 3 ▶ $addr = hex($addr) if $addr =~ /^0x/i;
Branch: 1 branch
BranchTrueFalseTotal
if $addr =~ /^0x/i 0 3 3
62 3 ▶ my $ext = $args{subaddress} // $args{subdevice} // 0;
Condition40%2/5
($args{"subaddress"} // $args{"subdevice"}) // 0
l!l
30
$args{"subaddress"} // $args{"subdevice"}
l!l&&r!l&&!r
300
MC/DC0%0/2
($args{"subaddress"} // $args{"subdevice"}) // 0
$args{"subaddress"} $args{"subdevice"}
Truth table33%1/3
A // B
A= $args{"subaddress"}B= $args{"subdevice"}
ABresult
1X1
011
000
63 3 ▶ $ext = hex($ext) if $ext =~ /^0x/i;
Branch: 1 branch
BranchTrueFalseTotal
if $ext =~ /^0x/i 0 3 3
64 3 ▶ my $cmd = $args{command} // $args{function} // 0;
Condition40%2/5
($args{"command"} // $args{"function"}) // 0
l!l
30
$args{"command"} // $args{"function"}
l!l&&r!l&&!r
300
MC/DC0%0/2
($args{"command"} // $args{"function"}) // 0
$args{"command"} $args{"function"}
Truth table33%1/3
A // B
A= $args{"command"}B= $args{"function"}
ABresult
1X1
011
000
65 3 ▶ $cmd = hex($cmd) if $cmd =~ /^0x/i;
Branch: 1 branch
BranchTrueFalseTotal
if $cmd =~ /^0x/i 0 3 3
66  
67 3 my $logical = $class->_logical_value($addr, $ext, $cmd);
68 3 ▶ return Protocol::IR::Code->new(
Branch: 1 branch
BranchTrueFalseTotal
$class->extended_bits ? : 1 2 3
69 protocol => $class->_protocol_name,
70 bits => $class->bits,
71 address => $addr,
72 subaddress => $class->extended_bits ? $ext : -1,
73 command => $cmd,
74 data => $logical,
75 );
76 }
77  
78 6 ▶sub _protocol_name { 'SONY12' }
Subroutine _protocol_name: called CC=1 SCAR=0.0
79  
80 1779 ▶sub decode_timing {
Subroutine decode_timing: called CC=17 SCAR=28.9
81 1779 my ($class, $pairs) = @_;
82 1779 ▶ return undef unless ref $pairs eq 'ARRAY' && @$pairs >= $class->bits + 1;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $pairs eq "ARRAY" and @$pairs >= $class->bits + 1 324 1455 1779
Condition66%2/3
ref $pairs eq "ARRAY" and @$pairs >= $class->bits + 1
!ll&&!rl&&r
03241455
MC/DC50%1/2
ref $pairs eq "ARRAY" and @$pairs >= $class->bits + 1
ref $pairs eq "ARRAY" @$pairs >= $class->bits + 1
Truth table66%2/3
A and B
A= ref $pairs eq "ARRAY"B= @$pairs >= $class->bits + 1
ABresult
0X0
100
111
83 # Reject frames that carry more bits than this variant (e.g. a 15-bit
84 # frame must not be accepted by the 12-bit decoder).
85 1455 ▶ if (@$pairs > $class->bits + 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$pairs > $class->bits + 1) 1436 19 1455
86 1436 ▶ return undef unless ($pairs->[$class->bits + 1][0] // 0) == 0;
Branch: 1 branch
BranchTrueFalseTotal
unless ($$pairs[$class->bits + 1][0] // 0) == 0 1430 6 1436
Condition50%1/2
$$pairs[$class->bits + 1][0] // 0
l!l
14360
MC/DC0%0/1
$$pairs[$class->bits + 1][0] // 0
$$pairs[$class->bits + 1][0]
Truth table50%1/2
A
A= $$pairs[$class->bits + 1][0]
Aresult
11
00
87 }
88  
89 25 my ($hdr_mark, $hdr_space) = @{$pairs->[0]};
90 25 ▶ return undef unless $hdr_mark >= 2000 && $hdr_mark <= 2800 && $hdr_space >= 400 && $hdr_space <= 800;
Branch: 1 branch
BranchTrueFalseTotal
unless $hdr_mark >= 2000 and $hdr_mark <= 2800 and $hdr_space >= 400 and $hdr_space <= 800 10 15 25
Condition77%7/9
$hdr_mark >= 2000 and $hdr_mark <= 2800 and $hdr_space >= 400 and $hdr_space <= 800
!ll&&!rl&&r
4615
$hdr_mark >= 2000 and $hdr_mark <= 2800 and $hdr_space >= 400
!ll&&!rl&&r
4021
$hdr_mark >= 2000 and $hdr_mark <= 2800
!ll&&!rl&&r
4021
MC/DC50%2/4
$hdr_mark >= 2000 and $hdr_mark <= 2800 and $hdr_space >= 400 and $hdr_space <= 800
$hdr_mark >= 2000 $hdr_mark <= 2800 $hdr_space >= 400 $hdr_space <= 800
Truth table60%3/5
A and B and C and D
A= $hdr_mark >= 2000B= $hdr_mark <= 2800C= $hdr_space >= 400D= $hdr_space <= 800
ABCDresult
0XXX0
10XX0
110X0
11100
11111
91  
92 15 my $data = 0;
93 15 for my $k (0 .. $class->bits - 1) {
94 235 my $mark = $pairs->[$k + 1][0];
95 235 my $bit;
96 235 ▶ if ($mark >= 1000 && $mark <= 1400) { $bit = 1; }
Branch: 2 branches
BranchTrueFalseTotal
if ($mark >= 1000 and $mark <= 1400) { } 100 135 235
elsif ($mark >= 400 and $mark <= 800) { } 135 0 135
Condition66%2/3
$mark >= 1000 and $mark <= 1400
!ll&&!rl&&r
1350100
MC/DC50%1/2
$mark >= 1000 and $mark <= 1400
$mark >= 1000 $mark <= 1400
Truth table66%2/3
A and B
A= $mark >= 1000B= $mark <= 1400
ABresult
0X0
100
111
97 135 ▶ elsif ($mark >= 400 && $mark <= 800) { $bit = 0; }
Condition33%1/3
$mark >= 400 and $mark <= 800
!ll&&!rl&&r
00135
MC/DC0%0/2
$mark >= 400 and $mark <= 800
$mark >= 400 $mark <= 800
Truth table33%1/3
A and B
A= $mark >= 400B= $mark <= 800
ABresult
0X0
100
111
98 0 else { return undef; }
99 235 $data = ($data << 1) | $bit;
100 }
101  
102 15 my $logical = _reverse_bits($data, $class->bits);
103 15 my $cmd = $logical & 0x7f;
104 15 my $addr = ($logical >> 7) & ((1 << $class->address_bits) - 1);
105 15 my $ext = 0;
106 15 ▶ if ($class->extended_bits) {
Branch: 1 branch
BranchTrueFalseTotal
if ($class->extended_bits) 5 10 15
107 5 $ext = ($logical >> (7 + $class->address_bits)) & ((1 << $class->extended_bits) - 1);
108 }
109 15 ▶ return Protocol::IR::Code->new(
Branch: 1 branch
BranchTrueFalseTotal
$class->extended_bits ? : 5 10 15
110 protocol => $class->_protocol_name,
111 bits => $class->bits,
112 address => $addr,
113 subaddress => $class->extended_bits ? $ext : -1,
114 command => $cmd,
115 data => $logical,
116 );
117 }
118  
119 3 ▶sub to_pronto {
Subroutine to_pronto: called CC=5 SCAR=16.1
120 3 my ($class, $code) = @_;
121 3 my $carrier_hz = 40000;
122 # freq word
123 3 my $freq_word = int(1000000.0 / ($carrier_hz * 0.241246) + 0.5);
124 3 my $period_us = $freq_word * 0.241246;
125 3 ▶ my $us_to_pulses = sub { int($_[0] / $period_us + 0.5) };
Subroutine __ANON__: called CC=1 SCAR=0.0
126  
127 3 my $addr = $code->{address} & ((1 << $class->address_bits) - 1);
128 3 ▶ my $ext = $class->extended_bits ? ($code->{subaddress} & ((1 << $class->extended_bits) - 1)) : 0;
Branch: 1 branch
BranchTrueFalseTotal
$class->extended_bits ? : 1 2 3
129 3 my $cmd = $code->{command} & 0x7f;
130 3 my $logical = $class->_logical_value($addr, $ext, $cmd);
131 3 my $transmitted = _reverse_bits($logical, $class->bits);
132  
133 3 my @bits;
134 3 for my $i (reverse 0 .. $class->bits - 1) {
135 47 push @bits, ($transmitted >> $i) & 1;
136 }
137  
138 3 my @pairs;
139 3 push @pairs, [ $us_to_pulses->(2400), $us_to_pulses->(600) ];
140 3 for my $bit (@bits) {
141 47 ▶ push @pairs, [ $us_to_pulses->($bit ? 1200 : 600), $us_to_pulses->(600) ];
Branch: 1 branch
BranchTrueFalseTotal
$bit ? : 20 27 47
142 }
143  
144 3 my $seq1 = scalar(@pairs);
145 3 my $header = sprintf('0000 %04X %04X 0000', $freq_word, $seq1);
146 3 my $payload = join(' ', map { sprintf('%04X %04X', $_->[0], $_->[1]) } @pairs);
147 3 return "$header $payload";
148 }
149  
150 # --- SONY15 ----------------------------------------------------------------
151 package Protocol::IR::Proto::Sony15;
152 55 ▶use base 'Protocol::IR::Proto::Sony12';
Subroutine BEGIN: called CC=1 SCAR=0.0
153 1618 ▶sub bits { 15 }
Subroutine bits: called CC=1 SCAR=0.0
154 8 ▶sub address_bits { 8 }
Subroutine address_bits: called CC=1 SCAR=0.0
155 6 ▶sub _protocol_name { 'SONY15' }
Subroutine _protocol_name: called CC=1 SCAR=0.0
156  
157 # --- SONY20 ----------------------------------------------------------------
158 package Protocol::IR::Proto::Sony20;
159 55 ▶use base 'Protocol::IR::Proto::Sony12';
Subroutine BEGIN: called CC=1 SCAR=0.0
160 1489 ▶sub bits { 20 }
Subroutine bits: called CC=1 SCAR=0.0
161 15 ▶sub address_bits { 5 }
Subroutine address_bits: called CC=1 SCAR=0.0
162 22 ▶sub extended_bits { 8 }
Subroutine extended_bits: called CC=1 SCAR=0.0
163 6 ▶sub _protocol_name { 'SONY20' }
Subroutine _protocol_name: called CC=1 SCAR=0.0
164  
165 55 1;
166  
167 =head1 NAME
168  
169 Protocol::IR::Proto::Sony - IR protocol handler
170  
171 =head1 VERSION
172  
173 version 1.5
174  
175 =cut
« blib/lib/Protocol/IR/Format/Tasmota.pm ↑ Summary blib/lib/Protocol/IR/Proto/NEC.pm »