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

filter: Statementstmt 98.9% 92 / 93 Branchbran 80.0% 16 / 20 Conditioncond 63.6% 21 / 33 MC/DCmcdc 28.5% 4 / 14 Subroutinesub 93.7% 15 / 16 Podpod 83.3% 5 / 6 totaltotal 84.0% 153 / 182 CC 28 SCAR 19.0
CC
28
Cov
88%
decode_timing
12.2
decode_params
9.1
to_pronto
7.0
CRAP 6.7
« blib/lib/Protocol/IR/Proto/JVC48.pm ↑ Summary blib/lib/Protocol/IR/Proto/JVC.pm »
1 package Protocol::IR::Proto::NEC48;
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::NEC48 is the base class for the 48-bit NEC family
10 # (48-NEC1/48-NEC2), from the DecodeIR definitions:
11 #
12 # 48-NEC1: {38.0k,564}<1,-1|1,-3>(16,-8,D:8,S:8,F:8,~F:8,E:8,~E:8,1,^108m,(16,-4,1,^108m)*)
13 # 48-NEC2: {38.0k,564}<1,-1|1,-3>(16,-8,D:8,S:8,F:8,~F:8,E:8,~E:8,1,^108m)+
14 #
15 # A single frame carries all six bytes (D, S, F, ~F, E, ~E) behind a
16 # 9024/4512 µs header; the trailing '+'/'*' repeats that whole frame, which is
17 # invisible to the single-frame decoders here. Like the 32-bit NEC family, the
18 # '2' variant only differs in repeat structure, so its name is preserved only
19 # so repeat behavior survives conversion. The trailing E byte is not part of
20 # the IRDB CSV rows, so it defaults to 0 when a code is built from
21 # device/subdevice/function parameters.
22  
23 # The protocol name this class decodes into / encodes as. The subclass
24 # NEC482 overrides this for the '2' variant.
25 111 ▶sub _protocol_name { '48-NEC1' }
Subroutine _protocol_name: called CC=1 SCAR=0.0
26  
27 # True when the accumulated word (Tasmota DataLSB) is the form decode_raw
28 # reads and decode_byte_order reverses per byte to reach the display form.
29 0 ▶sub lsb_is_accumulated { 1 }
Subroutine lsb_is_accumulated: not called CC=1 SCAR=6.9
Pod undocumented
30  
31 104 ▶sub _parse_int {
Subroutine _parse_int: called CC=3 SCAR=11.0
32 104 my ($val) = @_;
33 104 ▶ return 0 unless defined $val;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $val 0 104 104
34 104 $val =~ s/^\s+|\s+$//g;
35 104 ▶ if ($val =~ /^0x/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($val =~ /^0x/i) 3 101 104
36 # 48-bit hex strings exceed the 32-bit range that triggers Perl's
37 # "portable" warning; the modules here handle them by design.
38 55 ▶ no warnings 'portable';
Subroutine BEGIN: called CC=1 SCAR=0.0
39 3 return Math::BigInt->from_hex($val);
40 }
41 101 return $val + 0;
42 }
43  
44 # Reverse the bits within each byte of a $bits-bit value, keeping the byte
45 # order. Each frame byte is sent LSB-first, so this maps between the
46 # accumulated value a receiver collects (Tasmota DataLSB) and the display form
47 # (Tasmota Data). Arithmetic shifts rather than the 32-bit bitwise ops, so
48 # 48-bit words survive whole.
49 2 ▶sub _bit_reverse_bytes {
Subroutine _bit_reverse_bytes: called CC=2 SCAR=6.9
50 2 my ($val, $bits) = @_;
51 2 my $out = 0;
52 2 for my $i (0 .. $bits - 1) {
53 96 my $byte = int($i / 8);
54 96 my $bit = $i % 8;
55 96 my $src = 8 * $byte + (7 - $bit);
56 96 $out |= (($val >> $src) & 1) << $i;
57 }
58 2 return $out;
59 }
60  
61 # Pack address/subaddress/command/ext into the transmitted 48-bit value. The
62 # E byte is an extended function field, not part of the IRDB rows; it is sent
63 # cleared unless a code roundtripping through Pronto carries one.
64 33 ▶sub _encode_data {
Subroutine _encode_data: called CC=1 SCAR=0.0
65 33 my ($addr, $subaddr, $cmd, $ext) = @_;
66 33 my $d = $addr & 0xFF;
67 33 my $s = $subaddr & 0xFF;
68 33 my $f = $cmd & 0xFF;
69 33 my $e = $ext & 0xFF;
70 33 return (Math::BigInt->new($d) << 40) | (Math::BigInt->new($s) << 32) | (Math::BigInt->new($f) << 24) |
71 (Math::BigInt->new((~$f) & 0xFF) << 16) | (Math::BigInt->new($e) << 8) | Math::BigInt->new((~$e) & 0xFF);
72 }
73  
74 3 ▶sub decode_raw {
Subroutine decode_raw: called CC=1 SCAR=0.0
Pod documented
75 3 my ($class, $raw_val) = @_;
76 3 my $val = _parse_int($raw_val);
77  
78 3 return Protocol::IR::Code->new(
79 protocol => $class->_protocol_name,
80 bits => 48,
81 address => int(($val >> 40) & 0xFF),
82 subaddress => int(($val >> 32) & 0xFF),
83 command => int(($val >> 24) & 0xFF),
84 data => $val,
85 );
86 }
87  
88 # lsb=true is the accumulated form decode_raw reads (Tasmota DataLSB);
89 # lsb=false is the display form, reached by reversing the bits within each
90 # byte (the frame bytes are each sent LSB-first).
91 2 ▶sub decode_byte_order {
Subroutine decode_byte_order: called CC=2 SCAR=7.1
Pod documented
92 2 my ($class, $raw_val, $lsb) = @_;
93 2 my $val = _parse_int($raw_val);
94 2 ▶ return $class->decode_raw($lsb ? $val : _bit_reverse_bytes($val, 48));
Branch: 1 branch
BranchTrueFalseTotal
$lsb ? : 0 2 2
95 }
96  
97 33 ▶sub decode_params {
Subroutine decode_params: called CC=7 SCAR=22.1
Pod documented
98 33 my ($class, %args) = @_;
99 33 ▶ my $addr = _parse_int($args{address} // $args{device} // 0) & 0xFF;
Condition60%3/5
($args{"address"} // $args{"device"}) // 0
l!l
330
$args{"address"} // $args{"device"}
l!l&&r!l&&!r
3120
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
100 33 ▶ my $subaddr = _parse_int($args{subaddress} // $args{subdevice} // -1) & 0xFF;
Condition60%3/5
($args{"subaddress"} // $args{"subdevice"}) // -1
l!l
330
$args{"subaddress"} // $args{"subdevice"}
l!l&&r!l&&!r
3120
MC/DC0%0/2
($args{"subaddress"} // $args{"subdevice"}) // -1
$args{"subaddress"} $args{"subdevice"}
Truth table66%2/3
A // B
A= $args{"subaddress"}B= $args{"subdevice"}
ABresult
1X1
011
000
101 33 ▶ my $cmd = _parse_int($args{command} // $args{function} // 0) & 0xFF;
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
102  
103 # The E byte is not part of the IRDB rows; send it cleared.
104 33 return Protocol::IR::Code->new(
105 protocol => $class->_protocol_name,
106 bits => 48,
107 address => $addr,
108 subaddress => $subaddr,
109 command => $cmd,
110 data => _encode_data($addr, $subaddr, $cmd, 0),
111 );
112 }
113  
114 # Decode microsecond timing pairs into Protocol::IR::Code
115 1421 ▶sub decode_timing {
Subroutine decode_timing: called CC=12 SCAR=25.0
Pod documented
116 1421 my ($class, $burst_pairs) = @_;
117 1421 ▶ return undef unless scalar(@$burst_pairs) >= 50; # Header + 48 bits + Stop
Branch: 1 branch
BranchTrueFalseTotal
unless scalar @$burst_pairs >= 50 1178 243 1421
118  
119 243 my ($hdr_mark, $hdr_space) = @{$burst_pairs->[0]};
120  
121 # NEC Header Check: ~9000 µs mark, ~4500 µs space
122 243 ▶ return undef unless ($hdr_mark >= 7500 && $hdr_mark <= 10500) &&
Branch: 1 branch
BranchTrueFalseTotal
unless $hdr_mark >= 7500 and $hdr_mark <= 10500 and ($hdr_space >= 3500 and $hdr_space <= 5500) 140 103 243
Condition55%5/9
$hdr_mark >= 7500 and $hdr_mark <= 10500 and ($hdr_space >= 3500 and $hdr_space <= 5500)
!ll&&!rl&&r
1400103
$hdr_mark >= 7500 and $hdr_mark <= 10500
!ll&&!rl&&r
1400103
$hdr_space >= 3500 and $hdr_space <= 5500
!ll&&!rl&&r
00103
MC/DC25%1/4
$hdr_mark >= 7500 and $hdr_mark <= 10500 and ($hdr_space >= 3500 and $hdr_space <= 5500)
$hdr_mark >= 7500 $hdr_mark <= 10500 $hdr_space >= 3500 $hdr_space <= 5500
Truth table40%2/5
A and B and C and D
A= $hdr_mark >= 7500B= $hdr_mark <= 10500C= $hdr_space >= 3500D= $hdr_space <= 5500
ABCDresult
0XXX0
10XX0
110X0
11100
11111
123 ($hdr_space >= 3500 && $hdr_space <= 5500);
124  
125 103 my @bytes = (0, 0, 0, 0, 0, 0);
126 103 for my $i (0 .. 47) {
127 4944 my $pair = $burst_pairs->[$i + 1];
128 4944 my $space = $pair->[1];
129  
130 # Space ~1690 µs = 1, ~560 µs = 0
131 4944 ▶ my $bit = ($space > 1100) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$space > 1100 ? : 2488 2456 4944
132 4944 my $byte_idx = int($i / 8);
133 4944 my $bit_idx = $i % 8; # LSB-first
134  
135 4944 $bytes[$byte_idx] |= ($bit << $bit_idx);
136 }
137  
138 # Stop bit: a short mark followed by the ~108ms inter-frame space. A
139 # capture may end on a bare trailing mark with no space.
140 103 my ($stop_mark, $stop_space) = @{$burst_pairs->[49]};
141 103 ▶ 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 >= 3000 9 94 103
Condition88%8/9
$stop_mark >= 400 and $stop_mark <= 900 and $stop_space == 0 || $stop_space >= 3000
!ll&&!rl&&r
1894
$stop_mark >= 400 and $stop_mark <= 900
!ll&&!rl&&r
10102
$stop_space == 0 or $stop_space >= 3000
l!l&&r!l&&!r
1938
MC/DC75%3/4
$stop_mark >= 400 and $stop_mark <= 900 and $stop_space == 0 || $stop_space >= 3000
$stop_mark >= 400 $stop_mark <= 900 $stop_space == 0 $stop_space >= 3000
Truth table80%4/5
A and B and C or D
A= $stop_mark >= 400B= $stop_mark <= 900C= $stop_space == 0D= $stop_space >= 3000
ABCDresult
0XXX0
10XX0
11000
111X1
11011
142 ($stop_space == 0 || $stop_space >= 3000);
143  
144 94 my ($b0, $b1, $b2, $b3, $b4, $b5) = @bytes;
145 94 my $data = (Math::BigInt->new($b0) << 40) | (Math::BigInt->new($b1) << 32) | (Math::BigInt->new($b2) << 24) |
146 (Math::BigInt->new($b3) << 16) | (Math::BigInt->new($b4) << 8) | Math::BigInt->new($b5);
147  
148 94 return Protocol::IR::Code->new(
149 protocol => $class->_protocol_name,
150 bits => 48,
151 address => $b0,
152 subaddress => $b1,
153 command => $b2,
154 data => $data,
155 );
156 }
157  
158 33 ▶sub to_pronto {
Subroutine to_pronto: called CC=7 SCAR=19.5
Pod documented
159 33 my ($class, $code) = @_;
160 33 my $carrier_hz = 38000;
161  
162 # Build the pulse conversion period from the rounded Pronto frequency
163 # word, as MakeHex and Pronto parsers do (see Protocol::IR::Proto::NEC).
164 33 my $freq_word = int(sprintf("%.0f", 1000000.0 / ($carrier_hz * 0.241246)));
165 33 my $period_us = $freq_word * 0.241246;
166  
167 33 ▶ my $us_to_pulses = sub { int(sprintf("%.0f", $_[0] / $period_us)) };
Subroutine __ANON__: called CC=1 SCAR=0.0
168  
169 33 my $addr = $code->address & 0xFF;
170 33 ▶ my $subaddr = ($code->subaddress != -1) ? ($code->subaddress & 0xFF) : 0;
Branch: 1 branch
BranchTrueFalseTotal
$code->subaddress != -1 ? : 33 0 33
171 33 my $cmd = $code->command & 0xFF;
172 # The E byte rides along in the data word for roundtrip fidelity; rebuild
173 # it (cleared for codes built from IRDB rows).
174 33 ▶ my $e = defined $code->data ? (($code->data >> 8) & 0xFF) : 0;
Branch: 1 branch
BranchTrueFalseTotal
defined $code->data ? : 33 0 33
175  
176 33 my @bytes = ($addr, $subaddr, $cmd, (~$cmd) & 0xFF, $e, (~$e) & 0xFF);
177 33 my @bits;
178 33 for my $b (@bytes) {
179 198 for my $i (0..7) { push @bits, ($b >> $i) & 1; }
180 }
181  
182 33 my @burst_pairs = (
183 [$us_to_pulses->(9000), $us_to_pulses->(4500)]
184 );
185  
186 33 for my $bit (@bits) {
187 1584 ▶ push @burst_pairs, [
Branch: 1 branch
BranchTrueFalseTotal
$bit ? : 792 792 1584
188 $us_to_pulses->(560),
189 $us_to_pulses->($bit ? 1690 : 560)
190 ];
191 }
192  
193 # Suffix: a 560 µs stop mark and the ~108ms inter-frame space
194 # (^108m of the IRP).
195 33 push @burst_pairs, [$us_to_pulses->(560), $us_to_pulses->(108000)];
196  
197 33 my $seq1_pairs = scalar @burst_pairs;
198  
199 33 my $header = sprintf("0000 %04X %04X 0000", $freq_word, $seq1_pairs);
200 33 my @payload = map { sprintf("%04X %04X", $_->[0], $_->[1]) } @burst_pairs;
201  
202 33 return join(" ", $header, @payload);
203 }
204  
205 55 1;
206  
207 =encoding utf8
208  
209 =head1 NAME
210  
211 Protocol::IR::Proto::NEC48 - 48-NEC1 protocol handler (48-bit NEC family base)
212  
213 =head1 VERSION
214  
215 version 1.5
216  
217 =head1 SYNOPSIS
218  
219 use Protocol::IR::Converter;
220  
221 my $converter = Protocol::IR::Converter->new();
222  
223 # From a raw 48-bit value or from parameters
224 my $code = $converter->import_code('48-NEC1', '0x4DB2DE2100FF');
225 my $code = $converter->import_code('48-NEC1',
226 { address => 77, subaddress => 178, command => 222 });
227  
228 =head1 DESCRIPTION
229  
230 The 48-bit NEC family transmits 48-bit frames at 38 kHz: six bytes --
231 B<address> (device), B<subaddress>, B<command>, the command's one's
232 complement, an extended B<E> byte, and E's one's complement -- each sent
233 LSB-first, preceded by a B<9024/4512 µs> header and followed by a stop mark.
234 A single frame carries all six bytes; the repeat is a whole-frame
235 retransmission invisible to the single-frame decoders here.
236  
237 The C<data> value matches Tasmota's C<DataLSB> field: B<address> in bits
238 40-47, B<subaddress> in bits 32-39, B<command> in bits 24-31, its complement
239 in bits 16-23, C<E> in bits 8-15, and E's complement in bits 0-7. The
240 display form (Tasmota C<Data>) is the per-byte bit reversal; see
241 C<decode_byte_order>.
242  
243 IRDB distinguishes two single-frame formats, like the 32-bit NEC family:
244  
245 =over 4
246  
247 =item * C<48-NEC1> (this class) -- short header+gap repeat.
248  
249 =item * C<48-NEC2> (L<Protocol::IR::Proto::NEC482>) -- identical single-frame
250 timing; only the repeat differs (it re-transmits the whole frame).
251  
252 =back
253  
254 The C<E> byte is not part of the IRDB CSV rows, so codes built from
255 device/subdevice/function parameters send it cleared.
256  
257 When a subaddress is omitted (C<-1>), it is masked to C<0xFF> exactly as the
258 type-script sibling does; IRDB rows always carry an explicit subdevice.
259  
260 =head1 METHODS
261  
262 =head2 decode_raw
263  
264 my $code = $class->decode_raw('0x4DB2DE2100FF');
265  
266 Builds an L<Protocol::IR::Code> from the raw 48-bit accumulated value.
267  
268 =head2 decode_byte_order
269  
270 my $code = $class->decode_byte_order($raw, $lsb);
271  
272 Decodes from either byte order: C<$lsb> true reads the accumulated
273 (DataLSB) form, false reads the display (Data) form reached by reversing the
274 bits within each byte.
275  
276 =head2 decode_params
277  
278 my $code = $class->decode_params(address => 77, subaddress => 178, command => 222);
279  
280 Builds an L<Protocol::IR::Code> from discrete parameters. Accepts C<address> or
281 C<device>, C<subaddress> or C<subdevice>, and C<command> or C<function>.
282  
283 =head2 decode_timing
284  
285 my $code = $class->decode_timing(\@burst_pairs_us);
286  
287 Decodes an arrayref of microsecond C<[mark_us, space_us]> pairs. Returns an
288 L<Protocol::IR::Code> when the header, 48 data bits, and stop bit match the
289 48-bit NEC timing signature, otherwise C<undef>.
290  
291 =head2 to_pronto
292  
293 my $pronto = $class->to_pronto($ir_code);
294  
295 Encodes an L<Protocol::IR::Code> object as a Pronto Hex string at 38 kHz.
296  
297 =head1 SUPPORT
298  
299 Source code: L<https://github.com/bwarden/perl-protocol-ir>
300  
301 Bug reports and feature requests: L<https://github.com/bwarden/perl-protocol-ir/issues>
302  
303 =head1 AUTHOR
304  
305 Brett T. Warden <bwarden@cpan.org>
306  
307 =head1 COPYRIGHT AND LICENSE
308  
309 Copyright (c) 2026 Brett T. Warden
310  
311 This library is free software; you can redistribute it and/or modify it
312 under the terms of the GNU General Public License as published by the
313 Free Software Foundation; either version 2 of the License, or (at your
314 option) any later version.
« blib/lib/Protocol/IR/Proto/JVC48.pm ↑ Summary blib/lib/Protocol/IR/Proto/JVC.pm »