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

filter: Statementstmt 71.6% 81 / 113 Branchbran 37.5% 9 / 24 Conditioncond 25.0% 9 / 36 MC/DCmcdc 0.0% 0 / 16 Subroutinesub 80.0% 16 / 20 Podpod 0.0% 0 / 6 totaltotal 53.4% 115 / 215 CC 31 SCAR 37.4
CC
31
Cov
57%
decode_timing
128.6
decode_params
13.3
_reverse_byte
6.0
CRAP 42.3
« blib/lib/Protocol/IR/Format/LIRC.pm ↑ Summary blib/lib/Protocol/IR/Proto/Sharp.pm »
1 package Protocol::IR::Proto::Panasonic;
2 55 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 55 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 55 our $VERSION = '1.5';
6 55 ▶use Protocol::IR::Code;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 =pod
9  
10 =encoding utf8
11  
12 =head1 NAME
13  
14 Protocol::IR::Proto::Panasonic - Panasonic (Kaseikyo) protocol handler
15  
16 =head1 VERSION
17  
18 Version 1.5
19  
20 =head1 DESCRIPTION
21  
22 The Panasonic protocol handler (48-bit, Kaseikyo family, OEM code 0x40/0x04),
23 from the DecodeIR definition:
24  
25 {36k,432}<1,-1|1,-3>(8,-4,M:8,ID:8,D:8,S:8,F:8,(D^S^F):8,1,-173)+
26  
27 Six bytes are transmitted in order (Mfg_hi=0x40, Mfg_lo=0x04, device,
28 subdevice, function, checksum) behind a 3456/1728 µs header. The frame is
29 structured identically to JVC-48 (Kaseikyo family) with different OEM codes.
30 Each byte is sent MSB-first on the wire, so the value a receiver accumulates
31 (Tasmota's DataLSB) has per-byte bit-reversed OEM and device/function bytes.
32 The address and command fields follow the IRDB convention: they store the
33 bit-reversed wire byte (e.g. wire device 0x01 -> address 128 = 0x80), which
34 is the accumulated form the decoder naturally produces. The checksum byte is
35 device^subdevice^function in the IRDB (accumulated) convention.
36  
37 =cut
38  
39 3 ▶sub _parse_int {
Subroutine _parse_int: called CC=3 SCAR=11.3
40 3 my ($val) = @_;
41 3 ▶ return 0 unless defined $val;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $val 0 3 3
42 3 $val =~ s/^\s+|\s+$//g;
43 3 ▶ if ($val =~ /^0x/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($val =~ /^0x/i) 0 3 3
44 55 ▶ no warnings 'portable';
Subroutine BEGIN: called CC=1 SCAR=0.0
45 0 return hex($val);
46 }
47 3 return $val + 0;
48 }
49  
50 # Reverse the bits within each byte of a 48-bit value, keeping the byte order.
51 1 ▶sub _bit_reverse_bytes {
Subroutine _bit_reverse_bytes: called CC=2 SCAR=6.9
52 1 my ($val, $bits) = @_;
53 1 my $out = 0;
54 1 for my $i (0 .. $bits - 1) {
55 48 my $byte = int($i / 8);
56 48 my $bit = $i % 8;
57 48 my $src = 8 * $byte + (7 - $bit);
58 48 $out |= (($val >> $src) & 1) << $i;
59 }
60 1 return $out;
61 }
62  
63 # Reverse the bits within a single byte.
64 0 ▶sub _reverse_byte {
Subroutine _reverse_byte: not called CC=2 SCAR=17.9
65 0 my ($val) = @_;
66 0 my $out = 0;
67 0 for my $i (0 .. 7) {
68 0 $out |= (($val >> $i) & 1) << (7 - $i);
69 }
70 0 return $out;
71 }
72  
73 # Panasonic manufacturer codes (wire form): 0x40, 0x04.
74 # The accumulated (bit-reversed) forms used in the data field are 0x02, 0x20.
75 55 ▶use constant MFG_HI => 0x40;
Subroutine BEGIN: called CC=1 SCAR=0.0
76 55 ▶use constant MFG_LO => 0x04;
Subroutine BEGIN: called CC=1 SCAR=0.0
77 55 ▶use constant MFG_HI_ACC => 0x02; # reverseBits(0x40)
Subroutine BEGIN: called CC=1 SCAR=0.0
78 55 ▶use constant MFG_LO_ACC => 0x20; # reverseBits(0x04)
Subroutine BEGIN: called CC=1 SCAR=0.0
79 55 ▶use constant BITS => 48;
Subroutine BEGIN: called CC=1 SCAR=0.0
80  
81 0 ▶sub lsb_is_accumulated { 1 }
Subroutine lsb_is_accumulated: not called CC=1 SCAR=6.9
Pod undocumented
82  
83 # Pack device/subdevice/function into the transmitted 48-bit accumulated
84 # value, computing the checksum byte per the D^S^F rule. The address and
85 # command follow the IRDB convention (bit-reversed wire bytes), so the OEM
86 # bytes must also be reversed to match the accumulated form.
87 2 ▶sub _encode_data {
Subroutine _encode_data: called CC=1 SCAR=0.0
88 2 my ($addr, $subaddr, $cmd) = @_;
89 2 my $a = $addr & 0xFF;
90 2 my $s = $subaddr & 0xFF;
91 2 my $f = $cmd & 0xFF;
92 2 return (MFG_HI_ACC << 40) | (MFG_LO_ACC << 32) | ($a << 24) |
93 ($s << 16) | ($f << 8) | ($a ^ $s ^ $f);
94 }
95  
96 0 ▶sub decode_raw {
Subroutine decode_raw: not called CC=1 SCAR=6.9
Pod undocumented
97 0 my ($class, $raw_val) = @_;
98 0 my $val = _parse_int($raw_val);
99  
100 # decodeRaw expects the display form (Tasmota Data): the frame bytes are
101 # bit-reversed on the wire, so each byte in the display form must be
102 # reversed to reach the accumulated / IRDB convention for address and command.
103 0 my $addr = _reverse_byte(($val >> 24) & 0xFF);
104 0 my $sub = _reverse_byte(($val >> 16) & 0xFF);
105 0 my $cmd = _reverse_byte(($val >> 8) & 0xFF);
106  
107 # Re-pack the canonical accumulated word via _encode_data rather than
108 # storing the display word as-is, so the DataLSB import and the Data
109 # import land on the same data value (matching the other protocols and
110 # the JS port, where decode_raw routes through decode_params).
111 0 return Protocol::IR::Code->new(
112 protocol => 'PANASONIC',
113 bits => BITS,
114 address => $addr,
115 subaddress => $sub,
116 command => $cmd,
117 data => _encode_data($addr, $sub, $cmd),
118 );
119 }
120  
121 # lsb=true is the accumulated form (Tasmota DataLSB);
122 # lsb=false is the display form, reached by reversing the bits within each
123 # byte (the frame bytes are each sent MSB-first on the wire).
124 0 ▶sub decode_byte_order {
Subroutine decode_byte_order: not called CC=2 SCAR=17.9
Pod undocumented
125 0 my ($class, $raw_val, $lsb) = @_;
126 0 my $val = _parse_int($raw_val);
127 0 ▶ return $class->decode_raw($lsb ? _bit_reverse_bytes($val, 48) : $val);
Branch: 1 branch
BranchTrueFalseTotal
$lsb ? : 0 0 0
128 }
129  
130 1 ▶sub decode_params {
Subroutine decode_params: called CC=8 SCAR=25.8
Pod undocumented
131 1 my ($class, %args) = @_;
132 1 ▶ my $addr = _parse_int($args{address} // $args{device} // 0) & 0xFF;
Condition40%2/5
($args{"address"} // $args{"device"}) // 0
l!l
10
$args{"address"} // $args{"device"}
l!l&&r!l&&!r
100
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
133 1 ▶ my $subaddr_arg = _parse_int($args{subaddress} // $args{subdevice} // -1);
Condition40%2/5
($args{"subaddress"} // $args{"subdevice"}) // -1
l!l
10
$args{"subaddress"} // $args{"subdevice"}
l!l&&r!l&&!r
100
MC/DC0%0/2
($args{"subaddress"} // $args{"subdevice"}) // -1
$args{"subaddress"} $args{"subdevice"}
Truth table33%1/3
A // B
A= $args{"subaddress"}B= $args{"subdevice"}
ABresult
1X1
011
000
134 1 ▶ my $cmd = _parse_int($args{command} // $args{function} // 0) & 0xFF;
Condition40%2/5
($args{"command"} // $args{"function"}) // 0
l!l
10
$args{"command"} // $args{"function"}
l!l&&r!l&&!r
100
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
135  
136 1 ▶ my $subaddr = ($subaddr_arg == -1) ? 0 : $subaddr_arg & 0xFF;
Branch: 1 branch
BranchTrueFalseTotal
$subaddr_arg == -1 ? : 0 1 1
137  
138 1 return Protocol::IR::Code->new(
139 protocol => 'PANASONIC',
140 bits => BITS,
141 address => $addr,
142 subaddress => $subaddr_arg,
143 command => $cmd,
144 data => _encode_data($addr, $subaddr, $cmd),
145 );
146 }
147  
148 598 ▶sub decode_timing {
Subroutine decode_timing: called CC=15 SCAR=48.6
Pod undocumented
149 598 my ($class, $burst_pairs) = @_;
150 598 ▶ return undef unless scalar(@$burst_pairs) >= 50; # Header + 48 bits + Stop
Branch: 1 branch
BranchTrueFalseTotal
unless scalar @$burst_pairs >= 50 537 61 598
151  
152 61 my ($hdr_mark, $hdr_space) = @{$burst_pairs->[0]};
153  
154 # Panasonic Header Check: ~3456 µs mark, ~1728 µs space (8/-4 of 432 µs).
155 61 ▶ return undef unless ($hdr_mark >= 2800 && $hdr_mark <= 4100) &&
Branch: 1 branch
BranchTrueFalseTotal
unless $hdr_mark >= 2800 and $hdr_mark <= 4100 and ($hdr_space >= 1300 and $hdr_space <= 2100) 61 0 61
Condition33%3/9
$hdr_mark >= 2800 and $hdr_mark <= 4100 and ($hdr_space >= 1300 and $hdr_space <= 2100)
!ll&&!rl&&r
6100
$hdr_mark >= 2800 and $hdr_mark <= 4100
!ll&&!rl&&r
36250
$hdr_space >= 1300 and $hdr_space <= 2100
!ll&&!rl&&r
000
MC/DC0%0/4
$hdr_mark >= 2800 and $hdr_mark <= 4100 and ($hdr_space >= 1300 and $hdr_space <= 2100)
$hdr_mark >= 2800 $hdr_mark <= 4100 $hdr_space >= 1300 $hdr_space <= 2100
Truth table40%2/5
A and B and C and D
A= $hdr_mark >= 2800B= $hdr_mark <= 4100C= $hdr_space >= 1300D= $hdr_space <= 2100
ABCDresult
0XXX0
10XX0
110X0
11100
11111
156 ($hdr_space >= 1300 && $hdr_space <= 2100);
157  
158 0 my @bytes = (0, 0, 0, 0, 0, 0);
159 0 for my $i (0 .. 47) {
160 0 my $pair = $burst_pairs->[$i + 1];
161 0 my $space = $pair->[1];
162  
163 # Space ~1296 µs = 1, ~432 µs = 0
164 0 ▶ my $bit = ($space > 800) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$space > 800 ? : 0 0 0
165 0 my $byte_idx = int($i / 8);
166 0 my $bit_idx = $i % 8; # LSB-first
167  
168 0 $bytes[$byte_idx] |= ($bit << $bit_idx);
169 }
170  
171 # Stop bit: a short mark followed by the long trailing space. A
172 # capture may end on a bare trailing mark with no space.
173 0 my ($stop_mark, $stop_space) = @{$burst_pairs->[49]};
174 0 ▶ return undef unless ($stop_mark >= 300 && $stop_mark <= 700) &&
Branch: 1 branch
BranchTrueFalseTotal
unless $stop_mark >= 300 and $stop_mark <= 700 and $stop_space == 0 || $stop_space >= 4000 0 0 0
Condition0%0/9
$stop_mark >= 300 and $stop_mark <= 700 and $stop_space == 0 || $stop_space >= 4000
!ll&&!rl&&r
000
$stop_mark >= 300 and $stop_mark <= 700
!ll&&!rl&&r
000
$stop_space == 0 or $stop_space >= 4000
l!l&&r!l&&!r
000
MC/DC0%0/4
$stop_mark >= 300 and $stop_mark <= 700 and $stop_space == 0 || $stop_space >= 4000
$stop_mark >= 300 $stop_mark <= 700 $stop_space == 0 $stop_space >= 4000
Truth table0%0/5
A and B and C or D
A= $stop_mark >= 300B= $stop_mark <= 700C= $stop_space == 0D= $stop_space >= 4000
ABCDresult
0XXX0
10XX0
11000
111X1
11011
175 ($stop_space == 0 || $stop_space >= 4000);
176  
177 0 my ($b0, $b1, $b2, $b3, $b4, $b5) = @bytes;
178  
179 # Validate Panasonic manufacturer code (0x40, 0x04). Frames with other
180 # OEM bytes belong to a different Kaseikyo-family protocol (e.g. JVC-48
181 # with OEM 3/1). Compare against the accumulated (bit-reversed) forms
182 # because the bytes here were collected LSB-first from the wire.
183 0 ▶ return undef unless $b0 == MFG_HI_ACC && $b1 == MFG_LO_ACC;
Branch: 1 branch
BranchTrueFalseTotal
unless $b0 == 2 and $b1 == 32 0 0 0
Condition0%0/3
$b0 == 2 and $b1 == 32
!ll&&!rl&&r
000
MC/DC0%0/2
$b0 == 2 and $b1 == 32
$b0 == 2 $b1 == 32
Truth table0%0/3
A and B
A= $b0 == 2B= $b1 == 32
ABresult
0X0
100
111
184  
185 # Validate checksum: device ^ subdevice ^ function
186 0 ▶ return undef unless $b5 == ($b2 ^ $b3 ^ $b4);
Branch: 1 branch
BranchTrueFalseTotal
unless $b5 == ($b2 ^ $b3 ^ $b4) 0 0 0
187  
188 0 my $data = ($b0 << 40) | ($b1 << 32) | ($b2 << 24) |
189 ($b3 << 16) | ($b4 << 8) | $b5;
190  
191 0 return Protocol::IR::Code->new(
192 protocol => 'PANASONIC',
193 bits => BITS,
194 address => $b2,
195 subaddress => $b3,
196 command => $b4,
197 data => $data,
198 );
199 }
200  
201 1 ▶sub to_pronto {
Subroutine to_pronto: called CC=5 SCAR=16.1
Pod undocumented
202 1 my ($class, $code) = @_;
203 1 my $carrier_hz = 36000;
204  
205 1 my $freq_word = int(sprintf("%.0f", 1000000.0 / ($carrier_hz * 0.241246)));
206 1 my $period_us = $freq_word * 0.241246;
207 1 ▶ my $us_to_pulses = sub { int(sprintf("%.0f", $_[0] / $period_us)) };
Subroutine __ANON__: called CC=1 SCAR=0.0
208  
209 1 my $addr = $code->address & 0xFF;
210 1 ▶ my $subaddr = ($code->subaddress != -1) ? ($code->subaddress & 0xFF) : 0;
Branch: 1 branch
BranchTrueFalseTotal
$code->subaddress != -1 ? : 1 0 1
211 1 my $cmd = $code->command & 0xFF;
212  
213 # The value is transmitted with the most significant byte first and each
214 # byte MSB-first within. The wire bytes are the MSB display form
215 # (Tasmota's Data field), which is the per-byte bit reversal of the
216 # collected value.
217 1 my $data = _encode_data($addr, $subaddr, $cmd);
218 1 my $msb_val = _bit_reverse_bytes($data, 48);
219  
220 1 my @bits;
221 1 for my $i (reverse 0 .. 47) {
222 48 push @bits, ($msb_val >> $i) & 1;
223 }
224  
225 1 my @burst_pairs = (
226 [$us_to_pulses->(3456), $us_to_pulses->(1728)]
227 );
228  
229 1 for my $bit (@bits) {
230 48 ▶ push @burst_pairs, [
Branch: 1 branch
BranchTrueFalseTotal
$bit ? : 8 40 48
231 $us_to_pulses->(432),
232 $us_to_pulses->($bit ? 1296 : 432)
233 ];
234 }
235  
236 # The trailing mark and long inter-frame space.
237 1 push @burst_pairs, [$us_to_pulses->(432), $us_to_pulses->(40000)];
238  
239 1 my $seq1_pairs = scalar @burst_pairs;
240 1 my $header = sprintf("0000 %04X %04X 0000", $freq_word, $seq1_pairs);
241 1 my @payload = map { sprintf("%04X %04X", $_->[0], $_->[1]) } @burst_pairs;
242  
243 1 return join(" ", $header, @payload);
244 }
245  
246 55 1;
« blib/lib/Protocol/IR/Format/LIRC.pm ↑ Summary blib/lib/Protocol/IR/Proto/Sharp.pm »