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

filter: Statementstmt 98.9% 90 / 91 Branchbran 80.0% 16 / 20 Conditioncond 60.6% 20 / 33 MC/DCmcdc 28.5% 4 / 14 Subroutinesub 93.3% 14 / 15 Podpod 83.3% 5 / 6 totaltotal 83.2% 149 / 179 CC 28 SCAR 19.3
CC
28
Cov
88%
decode_timing
13.2
decode_params
8.7
to_pronto
6.0
CRAP 6.9
« blib/lib/Protocol/IR/Format/Mode2.pm ↑ Summary blib/lib/Protocol/IR/Proto/NEC48.pm »
1 package Protocol::IR::Proto::JVC48;
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 55 ▶use Math::BigInt;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 # Protocol::IR::Proto::JVC48 is the JVC-48 protocol handler (48-bit,
10 # Kaseikyo family OEM code 3/1), from the DecodeIR definition:
11 #
12 # {37k,432}<1,-1|1,-3>(8,-4,3:8,1:8,D:8,S:8,F:8,(D^S^F):8,1,-173)+
13 #
14 # Six bytes are transmitted in order (OEM1=3, OEM2=1, device, subdevice,
15 # function, checksum) behind a 3456/1728 µs header. Each byte is sent
16 # LSB-first, so the value a receiver accumulates is the first byte in the
17 # most significant position (Tasmota's DataLSB). The checksum byte is
18 # device^subdevice^function, the same rule the Panasonic member of the
19 # Kaseikyo family uses. A single frame carries all six bytes; the trailing
20 # '+' repeats that whole frame, invisible to the single-frame decoder here.
21  
22 15 ▶sub _parse_int {
Subroutine _parse_int: called CC=3 SCAR=11.0
23 15 my ($val) = @_;
24 15 ▶ return 0 unless defined $val;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $val 0 15 15
25 15 $val =~ s/^\s+|\s+$//g;
26 15 ▶ if ($val =~ /^0x/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($val =~ /^0x/i) 2 13 15
27 # 48-bit hex strings exceed the 32-bit range that triggers Perl's
28 # "portable" warning; the modules here handle them by design.
29 55 ▶ no warnings 'portable';
Subroutine BEGIN: called CC=1 SCAR=0.0
30 2 return Math::BigInt->from_hex($val);
31 }
32 13 return $val + 0;
33 }
34  
35 # Reverse the bits within each byte of a 48-bit value, keeping the byte
36 # order (see Protocol::IR::Proto::NEC48::_bit_reverse_bytes).
37 1 ▶sub _bit_reverse_bytes {
Subroutine _bit_reverse_bytes: called CC=2 SCAR=6.9
38 1 my ($val, $bits) = @_;
39 1 my $out = 0;
40 1 for my $i (0 .. $bits - 1) {
41 48 my $byte = int($i / 8);
42 48 my $bit = $i % 8;
43 48 my $src = 8 * $byte + (7 - $bit);
44 48 $out |= (($val >> $src) & 1) << $i;
45 }
46 1 return $out;
47 }
48  
49 # Pack device/subdevice/function into the transmitted 48-bit value,
50 # computing the checksum byte per the D^S^F rule.
51 4 ▶sub _encode_data {
Subroutine _encode_data: called CC=1 SCAR=0.0
52 4 my ($addr, $subaddr, $cmd) = @_;
53 4 my $a = $addr & 0xFF;
54 4 my $s = $subaddr & 0xFF;
55 4 my $f = $cmd & 0xFF;
56 4 return (Math::BigInt->new(3) << 40) | (Math::BigInt->new(1) << 32) | (Math::BigInt->new($a) << 24) | (Math::BigInt->new($s) << 16) |
57 (Math::BigInt->new($f) << 8) | Math::BigInt->new($a ^ $s ^ $f);
58 }
59  
60 # True when the accumulated word (Tasmota DataLSB) is the form decode_raw
61 # reads; the display form (Tasmota Data) is its per-byte bit reversal.
62 0 ▶sub lsb_is_accumulated { 1 }
Subroutine lsb_is_accumulated: not called CC=1 SCAR=6.9
Pod undocumented
63  
64 2 ▶sub decode_raw {
Subroutine decode_raw: called CC=1 SCAR=0.0
Pod documented
65 2 my ($class, $raw_val) = @_;
66 2 my $val = _parse_int($raw_val);
67  
68 2 return Protocol::IR::Code->new(
69 protocol => 'JVC-48',
70 bits => 48,
71 address => ($val >> 24) & 0xFF,
72 subaddress => ($val >> 16) & 0xFF,
73 command => ($val >> 8) & 0xFF,
74 data => $val,
75 );
76 }
77  
78 # lsb=true is the accumulated form decode_raw reads (Tasmota DataLSB);
79 # lsb=false is the display form, reached by reversing the bits within each
80 # byte (the frame bytes are each sent LSB-first).
81 1 ▶sub decode_byte_order {
Subroutine decode_byte_order: called CC=2 SCAR=7.1
Pod documented
82 1 my ($class, $raw_val, $lsb) = @_;
83 1 my $val = _parse_int($raw_val);
84 1 ▶ return $class->decode_raw($lsb ? $val : _bit_reverse_bytes($val, 48));
Branch: 1 branch
BranchTrueFalseTotal
$lsb ? : 0 1 1
85 }
86  
87 4 ▶sub decode_params {
Subroutine decode_params: called CC=8 SCAR=21.6
Pod documented
88 4 my ($class, %args) = @_;
89 4 ▶ my $addr = _parse_int($args{address} // $args{device} // 0) & 0xFF;
Condition60%3/5
($args{"address"} // $args{"device"}) // 0
l!l
40
$args{"address"} // $args{"device"}
l!l&&r!l&&!r
310
MC/DC0%0/2
($args{"address"} // $args{"device"}) // 0
$args{"address"} $args{"device"}
Truth table66%2/3
A // B
A= $args{"address"}B= $args{"device"}
ABresult
1X1
011
000
90 4 ▶ my $subaddr_arg = _parse_int($args{subaddress} // $args{subdevice} // -1);
Condition100%5/5
($args{"subaddress"} // $args{"subdevice"}) // -1
l!l
31
$args{"subaddress"} // $args{"subdevice"}
l!l&&r!l&&!r
211
MC/DC100%2/2
($args{"subaddress"} // $args{"subdevice"}) // -1
$args{"subaddress"} $args{"subdevice"}
Truth table100%3/3
A // B
A= $args{"subaddress"}B= $args{"subdevice"}
ABresult
1X1
011
000
91 4 ▶ my $cmd = _parse_int($args{command} // $args{function} // 0) & 0xFF;
Condition40%2/5
($args{"command"} // $args{"function"}) // 0
l!l
40
$args{"command"} // $args{"function"}
l!l&&r!l&&!r
400
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
92  
93 # A missing subdevice sends a zero field (there is no Default S rule);
94 # the stored subaddress stays -1.
95 4 ▶ my $subaddr = ($subaddr_arg == -1) ? 0 : $subaddr_arg & 0xFF;
Branch: 1 branch
BranchTrueFalseTotal
$subaddr_arg == -1 ? : 1 3 4
96  
97 4 return Protocol::IR::Code->new(
98 protocol => 'JVC-48',
99 bits => 48,
100 address => $addr,
101 subaddress => $subaddr_arg,
102 command => $cmd,
103 data => _encode_data($addr, $subaddr, $cmd),
104 );
105 }
106  
107 # Decode microsecond timing pairs into Protocol::IR::Code
108 607 ▶sub decode_timing {
Subroutine decode_timing: called CC=12 SCAR=25.8
Pod documented
109 607 my ($class, $burst_pairs) = @_;
110 607 ▶ return undef unless scalar(@$burst_pairs) >= 50; # Header + 48 bits + Stop
Branch: 1 branch
BranchTrueFalseTotal
unless scalar @$burst_pairs >= 50 537 70 607
111  
112 70 my ($hdr_mark, $hdr_space) = @{$burst_pairs->[0]};
113  
114 # JVC-48 Header Check: ~3456 µs mark, ~1728 µs space (8/-4 of 432 µs)
115 70 ▶ 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 9 70
Condition66%6/9
$hdr_mark >= 2800 and $hdr_mark <= 4100 and ($hdr_space >= 1300 and $hdr_space <= 2100)
!ll&&!rl&&r
6109
$hdr_mark >= 2800 and $hdr_mark <= 4100
!ll&&!rl&&r
36259
$hdr_space >= 1300 and $hdr_space <= 2100
!ll&&!rl&&r
009
MC/DC50%2/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 table60%3/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
116 ($hdr_space >= 1300 && $hdr_space <= 2100);
117  
118 9 my @bytes = (0, 0, 0, 0, 0, 0);
119 9 for my $i (0 .. 47) {
120 432 my $pair = $burst_pairs->[$i + 1];
121 432 my $space = $pair->[1];
122  
123 # Space ~1296 µs = 1, ~432 µs = 0
124 432 ▶ my $bit = ($space > 800) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$space > 800 ? : 102 330 432
125 432 my $byte_idx = int($i / 8);
126 432 my $bit_idx = $i % 8; # LSB-first
127  
128 432 $bytes[$byte_idx] |= ($bit << $bit_idx);
129 }
130  
131 # Stop bit: a short mark followed by the ~74ms inter-frame space. A
132 # capture may end on a bare trailing mark with no space.
133 9 my ($stop_mark, $stop_space) = @{$burst_pairs->[49]};
134 9 ▶ 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 9 9
Condition44%4/9
$stop_mark >= 300 and $stop_mark <= 700 and $stop_space == 0 || $stop_space >= 4000
!ll&&!rl&&r
009
$stop_mark >= 300 and $stop_mark <= 700
!ll&&!rl&&r
009
$stop_space == 0 or $stop_space >= 4000
l!l&&r!l&&!r
180
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 table40%2/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
135 ($stop_space == 0 || $stop_space >= 4000);
136  
137 9 my ($b0, $b1, $b2, $b3, $b4, $b5) = @bytes;
138 9 my $data = (Math::BigInt->new($b0) << 40) | (Math::BigInt->new($b1) << 32) | (Math::BigInt->new($b2) << 24) |
139 (Math::BigInt->new($b3) << 16) | (Math::BigInt->new($b4) << 8) | Math::BigInt->new($b5);
140  
141 9 return Protocol::IR::Code->new(
142 protocol => 'JVC-48',
143 bits => 48,
144 address => $b2,
145 subaddress => $b3,
146 command => $b4,
147 data => $data,
148 );
149 }
150  
151 3 ▶sub to_pronto {
Subroutine to_pronto: called CC=6 SCAR=17.9
Pod documented
152 3 my ($class, $code) = @_;
153 3 my $carrier_hz = 37000;
154  
155 # Build the pulse conversion period from the rounded Pronto frequency
156 # word, as MakeHex and Pronto parsers do (see Protocol::IR::Proto::NEC).
157 3 my $freq_word = int(sprintf("%.0f", 1000000.0 / ($carrier_hz * 0.241246)));
158 3 my $period_us = $freq_word * 0.241246;
159  
160 3 ▶ my $us_to_pulses = sub { int(sprintf("%.0f", $_[0] / $period_us)) };
Subroutine __ANON__: called CC=1 SCAR=0.0
161  
162 3 my $addr = $code->address & 0xFF;
163 3 ▶ my $subaddr = ($code->subaddress != -1) ? ($code->subaddress & 0xFF) : 0;
Branch: 1 branch
BranchTrueFalseTotal
$code->subaddress != -1 ? : 3 0 3
164 3 my $cmd = $code->command & 0xFF;
165  
166 3 my @bytes = (3, 1, $addr, $subaddr, $cmd, $addr ^ $subaddr ^ $cmd);
167 3 my @bits;
168 3 for my $b (@bytes) {
169 18 for my $i (0..7) { push @bits, ($b >> $i) & 1; }
170 }
171  
172 3 my @burst_pairs = (
173 [$us_to_pulses->(3456), $us_to_pulses->(1728)]
174 );
175  
176 3 for my $bit (@bits) {
177 144 ▶ push @burst_pairs, [
Branch: 1 branch
BranchTrueFalseTotal
$bit ? : 35 109 144
178 $us_to_pulses->(432),
179 $us_to_pulses->($bit ? 1296 : 432)
180 ];
181 }
182  
183 # The trailing mark and ~74ms gap (1,-173 of 432 µs).
184 3 push @burst_pairs, [$us_to_pulses->(432), $us_to_pulses->(74736)];
185  
186 3 my $seq1_pairs = scalar @burst_pairs;
187  
188 3 my $header = sprintf("0000 %04X %04X 0000", $freq_word, $seq1_pairs);
189 3 my @payload = map { sprintf("%04X %04X", $_->[0], $_->[1]) } @burst_pairs;
190  
191 3 return join(" ", $header, @payload);
192 }
193  
194 55 1;
195  
196 =encoding utf8
197  
198 =head1 NAME
199  
200 Protocol::IR::Proto::JVC48 - JVC-48 protocol handler (48-bit Kaseikyo, OEM 3/1)
201  
202 =head1 VERSION
203  
204 version 1.5
205  
206 =head1 SYNOPSIS
207  
208 use Protocol::IR::Converter;
209  
210 my $converter = Protocol::IR::Converter->new();
211  
212 # From a raw 48-bit value or from parameters
213 my $code = $converter->import_code('JVC-48', '0x030122210300');
214 my $code = $converter->import_code('JVC-48',
215 { address => 34, subaddress => 33, command => 3 });
216  
217 =head1 DESCRIPTION
218  
219 The C<JVC-48> protocol transmits 48-bit frames at B<37 kHz>: six bytes --
220 OEM code B<3>, OEM code B<1>, B<device>, B<subdevice>, B<function>, and a
221 checksum -- each sent LSB-first, preceded by a B<3456/1728 µs> header and
222 followed by a stop mark. A single frame carries all six bytes; the repeat is
223 a whole-frame retransmission invisible to the single-frame decoder here. The
224 checksum byte is B<device ^ subdevice ^ function>, the same rule the
225 Panasonic member of the Kaseikyo family uses. JVC-48 has no subaddress
226 semantics of its own; a missing subdevice sends a zero field.
227  
228 The C<data> value matches Tasmota's C<DataLSB> field: B<OEM1> in bits 40-47,
229 B<OEM2> in bits 32-39, B<device> in bits 24-31, B<subdevice> in bits 16-23,
230 B<function> in bits 8-15, and the checksum in bits 0-7. The display form
231 (Tasmota C<Data>) is the per-byte bit reversal; see C<decode_byte_order>.
232  
233 =head1 METHODS
234  
235 =head2 decode_raw
236  
237 my $code = $class->decode_raw('0x030122210300');
238  
239 Builds an L<Protocol::IR::Code> from the raw 48-bit accumulated value.
240  
241 =head2 decode_byte_order
242  
243 my $code = $class->decode_byte_order($raw, $lsb);
244  
245 Decodes from either byte order: C<$lsb> true reads the accumulated
246 (DataLSB) form, false reads the display (Data) form reached by reversing the
247 bits within each byte.
248  
249 =head2 decode_params
250  
251 my $code = $class->decode_params(address => 34, subaddress => 33, command => 3);
252  
253 Builds an L<Protocol::IR::Code> from discrete parameters. Accepts C<address> or
254 C<device>, C<subaddress> or C<subdevice>, and C<command> or C<function>.
255  
256 =head2 decode_timing
257  
258 my $code = $class->decode_timing(\@burst_pairs_us);
259  
260 Decodes an arrayref of microsecond C<[mark_us, space_us]> pairs. Returns an
261 L<Protocol::IR::Code> when the header, 48 data bits, and stop bit match the
262 JVC-48 timing signature, otherwise C<undef>.
263  
264 =head2 to_pronto
265  
266 my $pronto = $class->to_pronto($ir_code);
267  
268 Encodes an L<Protocol::IR::Code> object as a Pronto Hex string at 37 kHz.
269  
270 =head1 SUPPORT
271  
272 Source code: L<https://github.com/bwarden/perl-protocol-ir>
273  
274 Bug reports and feature requests: L<https://github.com/bwarden/perl-protocol-ir/issues>
275  
276 =head1 AUTHOR
277  
278 Brett T. Warden <bwarden@cpan.org>
279  
280 =head1 COPYRIGHT AND LICENSE
281  
282 Copyright (c) 2026 Brett T. Warden
283  
284 This library is free software; you can redistribute it and/or modify it
285 under the terms of the GNU General Public License as published by the
286 Free Software Foundation; either version 2 of the License, or (at your
287 option) any later version.
« blib/lib/Protocol/IR/Format/Mode2.pm ↑ Summary blib/lib/Protocol/IR/Proto/NEC48.pm »