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

filter: Statementstmt 100% 77 / 77 Branchbran 87.5% 14 / 16 Conditioncond 64.2% 18 / 28 MC/DCmcdc 33.3% 4 / 12 Subroutinesub 100% 12 / 12 Podpod 100% 6 / 6 totaltotal 86.7% 131 / 151 CC 24 SCAR 18.6
CC
24
Cov
90%
decode_timing
12.2
decode_params
5.9
to_pronto
5.0
CRAP 6.4
« blib/lib/Protocol/IR/Proto/NEC48.pm ↑ Summary blib/lib/Protocol/IR/Proto/SAMSUNG.pm »
1 package Protocol::IR::Proto::JVC;
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 90 ▶sub _parse_int {
Subroutine _parse_int: called CC=3 SCAR=11.0
9 90 my ($val) = @_;
10 90 ▶ return 0 unless defined $val;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $val 0 90 90
11 90 $val =~ s/^\s+|\s+$//g;
12 90 ▶ return hex($val) if $val =~ /^0x/i;
Branch: 1 branch
BranchTrueFalseTotal
if $val =~ /^0x/i 2 88 90
13 88 return $val + 0;
14 }
15  
16 33 ▶sub decode_params {
Subroutine decode_params: called CC=5 SCAR=17.8
Pod documented
17 33 my ($class, %args) = @_;
18 33 ▶ my $addr = _parse_int($args{address} // $args{device} // 0);
Condition60%3/5
($args{"address"} // $args{"device"}) // 0
l!l
330
$args{"address"} // $args{"device"}
l!l&&r!l&&!r
5280
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
19 33 ▶ my $cmd = _parse_int($args{command} // $args{function} // 0);
Condition40%2/5
($args{"command"} // $args{"function"}) // 0
l!l
330
$args{"command"} // $args{"function"}
l!l&&r!l&&!r
3300
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
20  
21 33 my $data = (($addr & 0xFF) << 8) | ($cmd & 0xFF);
22  
23 33 return Protocol::IR::Code->new(
24 protocol => 'JVC',
25 bits => 16,
26 address => $addr,
27 subaddress => -1,
28 command => $cmd,
29 data => $data,
30 );
31 }
32  
33 23 ▶sub decode_raw {
Subroutine decode_raw: called CC=1 SCAR=0.0
Pod documented
34 23 my ($class, $raw_val) = @_;
35 23 my $val = _parse_int($raw_val);
36 23 my $addr = ($val >> 8) & 0xFF;
37 23 my $cmd = $val & 0xFF;
38  
39 23 return Protocol::IR::Code->new(
40 protocol => 'JVC',
41 bits => 16,
42 address => $addr,
43 subaddress => -1,
44 command => $cmd,
45 data => $val,
46 );
47 }
48  
49 # True when the value decode_raw reads (Tasmota DataLSB) is the accumulated
50 # per-byte LSB-first wire form; the display form (Tasmota Data) is its
51 # per-byte bit reversal.
52 1 ▶sub lsb_is_accumulated { 1 }
Subroutine lsb_is_accumulated: called CC=1 SCAR=0.0
Pod documented
53  
54 # Reverse the bits within each byte of a $bits-bit value, keeping the byte
55 # order. Each frame byte is sent LSB-first, so this maps between the
56 # accumulated wire form (Tasmota Data, the "LSB" form the LIRC
57 # pre_data/post_data composition lands on) and the display form decode_raw
58 # reads (Tasmota DataLSB).
59 1 ▶sub _bit_reverse_bytes {
Subroutine _bit_reverse_bytes: called CC=2 SCAR=6.9
60 1 my ($val, $bits) = @_;
61 1 my $out = 0;
62 1 for my $i (0 .. $bits - 1) {
63 16 my $byte = int($i / 8);
64 16 my $bit = $i % 8;
65 16 my $src = 8 * $byte + (7 - $bit);
66 16 $out |= (($val >> $src) & 1) << $i;
67 }
68 1 return $out;
69 }
70  
71 # lsb=true is the form decode_raw reads (Tasmota DataLSB);
72 # lsb=false is the accumulated wire form, reached from the display form by
73 # reversing the bits within each byte.
74 1 ▶sub decode_byte_order {
Subroutine decode_byte_order: called CC=2 SCAR=7.1
Pod documented
75 1 my ($class, $raw_val, $lsb) = @_;
76 1 my $val = _parse_int($raw_val);
77 1 ▶ return $class->decode_raw($lsb ? $val : _bit_reverse_bytes($val, 16));
Branch: 1 branch
BranchTrueFalseTotal
$lsb ? : 0 1 1
78 }
79  
80 # Decode microsecond timing pairs into Protocol::IR::Code
81 664 ▶sub decode_timing {
Subroutine decode_timing: called CC=12 SCAR=25.0
Pod documented
82 664 my ($class, $burst_pairs) = @_;
83 664 ▶ return undef unless scalar(@$burst_pairs) >= 18; # Header + 16 bits + Stop
Branch: 1 branch
BranchTrueFalseTotal
unless scalar @$burst_pairs >= 18 152 512 664
84  
85 512 my ($hdr_mark, $hdr_space) = @{$burst_pairs->[0]};
86  
87 # JVC Header Check: ~8400 µs mark, ~4200 µs space
88 512 ▶ return undef unless ($hdr_mark >= 7000 && $hdr_mark <= 9800) &&
Branch: 1 branch
BranchTrueFalseTotal
unless $hdr_mark >= 7000 and $hdr_mark <= 9800 and ($hdr_space >= 3200 and $hdr_space <= 5200) 449 63 512
Condition55%5/9
$hdr_mark >= 7000 and $hdr_mark <= 9800 and ($hdr_space >= 3200 and $hdr_space <= 5200)
!ll&&!rl&&r
449063
$hdr_mark >= 7000 and $hdr_mark <= 9800
!ll&&!rl&&r
449063
$hdr_space >= 3200 and $hdr_space <= 5200
!ll&&!rl&&r
0063
MC/DC25%1/4
$hdr_mark >= 7000 and $hdr_mark <= 9800 and ($hdr_space >= 3200 and $hdr_space <= 5200)
$hdr_mark >= 7000 $hdr_mark <= 9800 $hdr_space >= 3200 $hdr_space <= 5200
Truth table40%2/5
A and B and C and D
A= $hdr_mark >= 7000B= $hdr_mark <= 9800C= $hdr_space >= 3200D= $hdr_space <= 5200
ABCDresult
0XXX0
10XX0
110X0
11100
11111
89 ($hdr_space >= 3200 && $hdr_space <= 5200);
90  
91 63 my @bytes = (0, 0);
92 63 for my $i (0 .. 15) {
93 1008 my $pair = $burst_pairs->[$i + 1];
94 1008 my $space = $pair->[1];
95  
96 # Space ~1578 µs = 1, ~526 µs = 0
97 1008 ▶ my $bit = ($space > 1000) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$space > 1000 ? : 387 621 1008
98 1008 my $byte_idx = int($i / 8);
99 1008 my $bit_idx = $i % 8; # LSB-first
100  
101 1008 $bytes[$byte_idx] |= ($bit << $bit_idx);
102 }
103  
104 63 my ($addr, $cmd) = @bytes;
105 63 my $data = ($addr << 8) | $cmd;
106  
107 # Stop bit: a short mark followed by the inter-frame gap (~17080 µs for
108 # repeated frames, ~42000 µs for a lone frame). Single-frame captures may
109 # end on a bare trailing mark with no space. This rejects signals whose
110 # header overlaps JVC's but have a different frame structure.
111 63 my ($stop_mark, $stop_space) = @{$burst_pairs->[17]};
112 63 ▶ return undef unless $stop_mark >= 400 && $stop_mark <= 900 &&
Branch: 1 branch
BranchTrueFalseTotal
unless $stop_mark >= 400 and $stop_mark <= 900 and $stop_space == 0 || $stop_space >= 8000 7 56 63
Condition88%8/9
$stop_mark >= 400 and $stop_mark <= 900 and $stop_space == 0 || $stop_space >= 8000
!ll&&!rl&&r
1656
$stop_mark >= 400 and $stop_mark <= 900
!ll&&!rl&&r
1062
$stop_space == 0 or $stop_space >= 8000
l!l&&r!l&&!r
7496
MC/DC75%3/4
$stop_mark >= 400 and $stop_mark <= 900 and $stop_space == 0 || $stop_space >= 8000
$stop_mark >= 400 $stop_mark <= 900 $stop_space == 0 $stop_space >= 8000
Truth table80%4/5
A and B and C or D
A= $stop_mark >= 400B= $stop_mark <= 900C= $stop_space == 0D= $stop_space >= 8000
ABCDresult
0XXX0
10XX0
11000
111X1
11011
113 ($stop_space == 0 || $stop_space >= 8000);
114  
115 56 return Protocol::IR::Code->new(
116 protocol => 'JVC',
117 bits => 16,
118 address => $addr,
119 subaddress => -1,
120 command => $cmd,
121 data => $data,
122 );
123 }
124  
125 34 ▶sub to_pronto {
Subroutine to_pronto: called CC=5 SCAR=16.1
Pod documented
126 34 my ($class, $code) = @_;
127 34 my $carrier_hz = 38000;
128  
129 # Build the pulse conversion period from the rounded Pronto frequency
130 # word, as MakeHex and Pronto parsers do (see Protocol::IR::Proto::NEC).
131 34 my $freq_word = int(sprintf("%.0f", 1000000.0 / ($carrier_hz * 0.241246)));
132 34 my $period_us = $freq_word * 0.241246;
133  
134 34 ▶ my $us_to_pulses = sub { int(sprintf("%.0f", $_[0] / $period_us)) };
Subroutine __ANON__: called CC=1 SCAR=0.0
135  
136 34 my @burst_pairs = (
137 [$us_to_pulses->(8400), $us_to_pulses->(4200)]
138 );
139  
140 34 my @bits;
141 34 for my $i (0..7) { push @bits, ($code->address >> $i) & 1; }
142 34 for my $i (0..7) { push @bits, ($code->command >> $i) & 1; }
143  
144 34 for my $bit (@bits) {
145 544 ▶ push @burst_pairs, [
Branch: 1 branch
BranchTrueFalseTotal
$bit ? : 186 358 544
146 $us_to_pulses->(526),
147 $us_to_pulses->($bit ? 1578 : 526)
148 ];
149 }
150  
151 34 push @burst_pairs, [$us_to_pulses->(526), $us_to_pulses->(42000)];
152  
153 34 my $seq1_pairs = scalar @burst_pairs;
154  
155 34 my $header = sprintf("0000 %04X %04X 0000", $freq_word, $seq1_pairs);
156 34 my @payload = map { sprintf("%04X %04X", $_->[0], $_->[1]) } @burst_pairs;
157  
158 34 return join(" ", $header, @payload);
159 }
160  
161 55 1;
162  
163 =encoding utf8
164  
165 =head1 NAME
166  
167 Protocol::IR::Proto::JVC - JVC protocol handler (16-bit)
168  
169 =head1 VERSION
170  
171 version 1.5
172  
173 =head1 SYNOPSIS
174  
175 use Protocol::IR::Converter;
176  
177 my $converter = Protocol::IR::Converter->new();
178  
179 # From a raw 16-bit value or from parameters
180 my $code = $converter->import_code('JVC', '0x030C');
181 my $code = $converter->import_code('JVC',
182 { address => 3, command => 12 });
183  
184 =head1 DESCRIPTION
185  
186 The JVC protocol transmits a 16-bit frame at 38 kHz: an B<address> byte
187 followed by a B<command> byte, each sent LSB-first. The C<data> value has
188 the address in bits 8-15 and the command in bits 0-7, matching Tasmota's
189 C<Data> field. JVC has no subaddress; C<subaddress> is set to C<-1>.
190  
191 Frame timing: B<8400 µs> header mark and B<4200 µs> header space; each bit
192 is a B<526 µs> mark followed by a space of B<526 µs> for 0 or B<1578 µs>
193 for 1; a B<526 µs> stop mark ends the frame. The inter-frame gap is about
194 B<17080 µs> for repeated frames or B<42000 µs> for a lone frame.
195  
196 The C<decode_timing> decoder validates both the header and the stop bit, so
197 a capture from a protocol whose header overlaps JVC's but has a different
198 frame structure is rejected rather than misidentified.
199  
200 =head1 METHODS
201  
202 =head2 decode_raw
203  
204 my $code = $class->decode_raw('0x030C');
205  
206 Builds an L<Protocol::IR::Code> from the raw 16-bit value.
207  
208 =head2 decode_byte_order
209  
210 my $code = $class->decode_byte_order($raw, $lsb);
211  
212 Decodes from either byte order: C<$lsb> true reads the accumulated (DataLSB)
213 form directly; false reads the display (Data) form, the per-byte bit reversal
214 of the accumulated word. JVC sends each byte LSB-first, so the accumulated
215 word is the one carried on the wire and by Tasmota's C<DataLSB>.
216  
217 =head2 lsb_is_accumulated
218  
219 my $flag = $class->lsb_is_accumulated;
220  
221 True (always, for JVC) when the accumulated byte order is what
222 C<decode_raw> reads, informing the Tasmota structured importer which of its
223 C<Data>/C<DataLSB> fields to prefer.
224  
225 =head2 decode_params
226  
227 my $code = $class->decode_params(address => 3, command => 12);
228  
229 Builds an L<Protocol::IR::Code> from discrete parameters. Accepts C<address> or
230 C<device>, and C<command> or C<function>.
231  
232 =head2 decode_timing
233  
234 my $code = $class->decode_timing(\@burst_pairs_us);
235  
236 Decodes an arrayref of microsecond C<[mark_us, space_us]> pairs. Returns an
237 L<Protocol::IR::Code> when the header, 16 data bits, and stop bit match the JVC
238 timing signature, otherwise C<undef>.
239  
240 =head2 to_pronto
241  
242 my $pronto = $class->to_pronto($ir_code);
243  
244 Encodes an L<Protocol::IR::Code> object as a Pronto Hex string at 38 kHz.
245  
246 =head1 SUPPORT
247  
248 Source code: L<https://github.com/bwarden/perl-protocol-ir>
249  
250 Bug reports and feature requests: L<https://github.com/bwarden/perl-protocol-ir/issues>
251  
252 =head1 AUTHOR
253  
254 Brett T. Warden <bwarden@cpan.org>
255  
256 =head1 COPYRIGHT AND LICENSE
257  
258 Copyright (c) 2026 Brett T. Warden
259  
260 This library is free software; you can redistribute it and/or modify it
261 under the terms of the GNU General Public License as published by the
262 Free Software Foundation; either version 2 of the License, or (at your
263 option) any later version.
« blib/lib/Protocol/IR/Proto/NEC48.pm ↑ Summary blib/lib/Protocol/IR/Proto/SAMSUNG.pm »