blib/lib/Protocol/IR/Format/LIRC.pm

filter: Statementstmt 79.8% 238 / 298 Branchbran 71.6% 119 / 166 Conditioncond 58.9% 139 / 236 MC/DCmcdc 14.1% 15 / 106 Subroutinesub 87.5% 14 / 16 Podpod 100% 2 / 2 totaltotal 63.9% 527 / 824 CC 182 SCAR 40.6
CC
182
Cov
71%
_export_raw_remote
156.0
_infer_protocol
114.7
_parse_remotes
52.8
CRAP 57.7
↑ Summary blib/lib/Protocol/IR/Proto/Panasonic.pm »
1 package Protocol::IR::Format::LIRC;
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  
7 55 ▶use Protocol::IR::Code;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 # LIRC remote definition format (.lircd.conf) importer and exporter.
10 #
11 # The LIRC format describes IR remotes in two encoding modes:
12 #
13 # Protocol-based (SPACE_ENC): A remote header defines timing parameters
14 # (header, one, zero, pre_data, post_data), and the codes section maps
15 # button names to hex values. The signal is reconstructed from the
16 # protocol rules.
17 #
18 # Raw codes (RAW_CODES): Direct pulse/space microsecond values for
19 # each button.
20 #
21 # This module handles both import modes and can export protocol-based
22 # LIRC files for known protocols.
23  
24 # --- LIRC timing templates for known protocols ---------------------------
25 #
26 # Each template defines the standard LIRC timing parameters for a protocol.
27 # The template is used for export; import reads whatever the file specifies.
28  
29 55 my %LIRC_TEMPLATES = (
30 NEC => {
31 bits => 32,
32 flags => 'SPACE_ENC|CONST_LENGTH',
33 header => [9000, 4500],
34 one => [560, 1690],
35 zero => [560, 560],
36 ptrail => 560,
37 repeat => [9000, 2250],
38 pre_data_bits => 0,
39 pre_data => 0,
40 post_data_bits => 0,
41 post_data => 0,
42 gap => 108000,
43 toggle_bit_mask => 0,
44 min_repeat => 1,
45 },
46 SAMSUNG => {
47 bits => 32,
48 flags => 'SPACE_ENC|CONST_LENGTH',
49 header => [4500, 4500],
50 one => [560, 1690],
51 zero => [560, 560],
52 ptrail => 560,
53 repeat => [4500, 1690],
54 pre_data_bits => 0,
55 pre_data => 0,
56 post_data_bits => 0,
57 post_data => 0,
58 gap => 108000,
59 toggle_bit_mask => 0,
60 min_repeat => 1,
61 },
62 JVC => {
63 bits => 16,
64 flags => 'SPACE_ENC|CONST_LENGTH',
65 header => [8440, 4220],
66 one => [526, 1276],
67 zero => [526, 526],
68 ptrail => 526,
69 repeat => [8440, 2110],
70 pre_data_bits => 0,
71 pre_data => 0,
72 post_data_bits => 0,
73 post_data => 0,
74 gap => 108000,
75 toggle_bit_mask => 0,
76 min_repeat => 1,
77 },
78 );
79  
80 # --- Import (decode) -----------------------------------------------------
81  
82 17 ▶sub decode {
Subroutine decode: called CC=8 SCAR=20.9
Pod documented
83 17 my ($class, $input, $registry) = @_;
84 17 ▶ die "No LIRC input provided\n" unless defined $input;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $input 0 17 17
85  
86 17 my $text = _read_input($input);
87 17 ▶ die "LIRC input is empty\n" unless $text =~ /\S/;
Branch: 1 branch
BranchTrueFalseTotal
unless $text =~ /\S/ 1 16 17
88  
89 # Split into remote blocks
90 16 my @remotes = _parse_remotes($text);
91 16 ▶ die "No 'begin remote' blocks found in LIRC input\n" unless @remotes;
Branch: 1 branch
BranchTrueFalseTotal
unless @remotes 1 15 16
92  
93 15 my @codes;
94 15 for my $remote (@remotes) {
95 15 ▶ my $remote_name = $remote->{name} // 'unknown';
Condition50%1/2
$remote->{"name"} // "unknown"
l!l
150
MC/DC0%0/1
$remote->{"name"} // "unknown"
$remote->{"name"}
Truth table50%1/2
A
A= $remote->{"name"}
Aresult
11
00
96 15 ▶ my $flags = $remote->{flags} // '';
Condition50%1/2
$remote->{"flags"} // ""
l!l
150
MC/DC0%0/1
$remote->{"flags"} // ""
$remote->{"flags"}
Truth table50%1/2
A
A= $remote->{"flags"}
Aresult
11
00
97 15 my $is_raw = ($flags =~ /RAW_CODES/i);
98  
99 15 ▶ if ($is_raw) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_raw) { } 1 14 15
100 1 push @codes, _decode_raw_codes($remote, $registry);
101 } else {
102 14 push @codes, _decode_protocol_codes($remote, $registry);
103 }
104 }
105  
106 15 return \@codes;
107 }
108  
109 # Parse all 'begin remote' ... 'end remote' blocks from the text.
110 # Uses a line-by-line state machine to avoid regex backtracking issues.
111 16 ▶sub _parse_remotes {
Subroutine _parse_remotes: called CC=45 SCAR=39.7
112 16 my ($text) = @_;
113 16 my @remotes;
114 my %remote;
115 16 my $state = 'idle'; # idle | remote | codes | raw_codes
116  
117 16 my @lines = split /\n/, $text;
118 16 for my $line (@lines) {
119 824 my $raw = $line;
120 824 $line =~ s/^\s+|\s+$//g;
121  
122 # Skip blank lines and comments
123 824 ▶ next if $line eq '' || $line =~ /^#/;
Branch: 1 branch
BranchTrueFalseTotal
if $line eq "" or $line =~ /^#/ 142 682 824
Condition100%3/3
$line eq "" or $line =~ /^#/
l!l&&r!l&&!r
5686682
MC/DC100%2/2
$line eq "" or $line =~ /^#/
$line eq "" $line =~ /^#/
Truth table100%3/3
A or B
A= $line eq ""B= $line =~ /^#/
ABresult
1X1
011
000
124  
125 682 ▶ if ($state eq 'idle') {
Branch: 4 branches
BranchTrueFalseTotal
if ($state eq 'idle') { } 17 665 682
elsif ($state eq 'remote') { } 218 447 665
elsif ($state eq 'codes') { } 436 11 447
elsif ($state eq 'raw_codes') { } 11 0 11
126 17 ▶ if ($line =~ /^begin\s+remote$/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /^begin\s+remote$/i) 15 2 17
127 15 %remote = ();
128 15 $state = 'remote';
129 }
130 }
131 elsif ($state eq 'remote') {
132 218 ▶ if ($line =~ /^end\s+remote$/i) {
Branch: 3 branches
BranchTrueFalseTotal
if ($line =~ /^end\s+remote$/i) { } 15 203 218
elsif ($line =~ /^begin\s+codes$/i) { } 14 189 203
elsif ($line =~ /^begin\s+raw_codes$/i) { } 1 188 189
133 15 ▶ push @remotes, { %remote } if $remote{name};
Branch: 1 branch
BranchTrueFalseTotal
if $remote{"name"} 15 0 15
134 15 %remote = ();
135 15 $state = 'idle';
136 }
137 elsif ($line =~ /^begin\s+codes$/i) {
138 14 $state = 'codes';
139 14 $remote{codes} = [];
140 }
141 elsif ($line =~ /^begin\s+raw_codes$/i) {
142 1 $state = 'raw_codes';
143 1 $remote{raw_codes} = [];
144 }
145 else {
146 188 my ($key, $val) = split /\s+/, $line, 2;
147 188 ▶ next unless defined $key && defined $val;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $key and defined $val 0 188 188
Condition33%1/3
defined $key and defined $val
!ll&&!rl&&r
00188
MC/DC0%0/2
defined $key and defined $val
defined $key defined $val
Truth table33%1/3
A and B
A= defined $keyB= defined $val
ABresult
0X0
100
111
148 188 $val =~ s/\s+$//;
149  
150 188 ▶ if ($key eq 'name') {
Branch: 18 branches
BranchTrueFalseTotal
if ($key eq 'name') { } 15 173 188
elsif ($key eq 'bits') { } 14 159 173
elsif ($key eq 'flags') { } 15 144 159
elsif ($key eq 'eps') { } 15 129 144
elsif ($key eq 'aeps') { } 15 114 129
elsif ($key eq 'header') { } 14 100 114
elsif ($key eq 'one') { } 14 86 100
elsif ($key eq 'zero') { } 14 72 86
elsif ($key eq 'ptrail') { } 15 57 72
elsif ($key eq 'repeat') { } 5 52 57
elsif ($key eq 'pre_data_bits') { } 9 43 52
elsif ($key eq 'pre_data') { } 9 34 43
elsif ($key eq 'post_data_bits') { } 0 34 34
elsif ($key eq 'post_data') { } 0 34 34
elsif ($key eq 'gap') { } 15 19 34
elsif ($key eq 'toggle_bit_mask') { } 12 7 19
elsif ($key eq 'min_repeat') { } 4 3 7
elsif ($key eq 'frequency') { } 0 3 3
151 15 $remote{name} = $val;
152 } elsif ($key eq 'bits') {
153 14 $remote{bits} = $val + 0;
154 } elsif ($key eq 'flags') {
155 15 $remote{flags} = $val;
156 } elsif ($key eq 'eps') {
157 15 $remote{eps} = $val + 0;
158 } elsif ($key eq 'aeps') {
159 15 $remote{aeps} = $val + 0;
160 } elsif ($key eq 'header') {
161 14 my @v = split /\s+/, $val;
162 14 $remote{header} = [@v[0,1]];
163 } elsif ($key eq 'one') {
164 14 my @v = split /\s+/, $val;
165 14 $remote{one} = [@v[0,1]];
166 } elsif ($key eq 'zero') {
167 14 my @v = split /\s+/, $val;
168 14 $remote{zero} = [@v[0,1]];
169 } elsif ($key eq 'ptrail') {
170 15 $remote{ptrail} = $val + 0;
171 } elsif ($key eq 'repeat') {
172 5 my @v = split /\s+/, $val;
173 5 $remote{repeat} = [@v[0,1]];
174 } elsif ($key eq 'pre_data_bits') {
175 9 $remote{pre_data_bits} = $val + 0;
176 } elsif ($key eq 'pre_data') {
177 9 ▶ $remote{pre_data} = hex($val) if $val =~ /^0x/i;
Branch: 1 branch
BranchTrueFalseTotal
if $val =~ /^0x/i 9 0 9
178 9 ▶ $remote{pre_data} = $val + 0 if $val =~ /^\d+$/;
Branch: 1 branch
BranchTrueFalseTotal
if $val =~ /^\d+$/ 0 9 9
179 } elsif ($key eq 'post_data_bits') {
180 0 $remote{post_data_bits} = $val + 0;
181 } elsif ($key eq 'post_data') {
182 0 ▶ $remote{post_data} = hex($val) if $val =~ /^0x/i;
Branch: 1 branch
BranchTrueFalseTotal
if $val =~ /^0x/i 0 0 0
183 0 ▶ $remote{post_data} = $val + 0 if $val =~ /^\d+$/;
Branch: 1 branch
BranchTrueFalseTotal
if $val =~ /^\d+$/ 0 0 0
184 } elsif ($key eq 'gap') {
185 15 $remote{gap} = $val + 0;
186 } elsif ($key eq 'toggle_bit_mask') {
187 12 ▶ $remote{toggle_bit_mask} = hex($val) if $val =~ /^0x/i;
Branch: 1 branch
BranchTrueFalseTotal
if $val =~ /^0x/i 12 0 12
188 12 ▶ $remote{toggle_bit_mask} = $val + 0 if $val =~ /^\d+$/;
Branch: 1 branch
BranchTrueFalseTotal
if $val =~ /^\d+$/ 0 12 12
189 } elsif ($key eq 'min_repeat') {
190 4 $remote{min_repeat} = $val + 0;
191 } elsif ($key eq 'frequency') {
192 0 $remote{frequency} = $val + 0;
193 }
194 }
195 }
196 elsif ($state eq 'codes') {
197 436 ▶ if ($line =~ /^end\s+codes$/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /^end\s+codes$/i) { } 14 422 436
198 14 $state = 'remote';
199 }
200 else {
201 # Strip inline comments before matching
202 422 (my $clean = $line) =~ s/\s*#.*$//;
203 422 ▶ if ($clean =~ /^(\S+)\s+(0[xX][0-9A-Fa-f]+|\d+)\s*$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($clean =~ /^(\S+)\s+(0[xX][0-9A-Fa-f]+|\d+)\s*$/) 422 0 422
204 422 push @{$remote{codes}}, [$1, $2];
205 }
206 }
207 }
208 elsif ($state eq 'raw_codes') {
209 11 ▶ if ($line =~ /^end\s+raw_codes$/i) {
Branch: 3 branches
BranchTrueFalseTotal
if ($line =~ /^end\s+raw_codes$/i) { } 1 10 11
elsif ($line =~ /^name\s+(\S+)/) { } 1 9 10
elsif ($line =~ /^[\d\s]+$/ and @{$remote{'raw_codes'};}) { } 9 0 9
210 1 $state = 'remote';
211 }
212 elsif ($line =~ /^name\s+(\S+)/) {
213 1 push @{$remote{raw_codes}}, [$1, []];
214 }
215 9 ▶ elsif ($line =~ /^[\d\s]+$/ && @{$remote{raw_codes}}) {
Condition33%1/3
$line =~ /^[\d\s]+$/ and @{$remote{"raw_codes"};}
!ll&&!rl&&r
009
MC/DC0%0/2
$line =~ /^[\d\s]+$/ and @{$remote{"raw_codes"};}
$line =~ /^[\d\s]+$/ @{$remote{"raw_codes"};}
Truth table33%1/3
A and B
A= $line =~ /^[\d\s]+$/B= @{$remote{"raw_codes"};}
ABresult
0X0
100
111
216 9 my @vals = split /\s+/, $line;
217 9 push @{$remote{raw_codes}[-1][1]}, @vals;
218 }
219 }
220 }
221  
222 16 return @remotes;
223 }
224  
225 # Decode protocol-based codes from a remote block.
226 14 ▶sub _decode_protocol_codes {
Subroutine _decode_protocol_codes: called CC=26 SCAR=34.6
227 14 my ($remote, $registry) = @_;
228 14 my @codes;
229  
230 14 ▶ my $remote_name = $remote->{name} // 'unknown';
Condition50%1/2
$remote->{"name"} // "unknown"
l!l
140
MC/DC0%0/1
$remote->{"name"} // "unknown"
$remote->{"name"}
Truth table50%1/2
A
A= $remote->{"name"}
Aresult
11
00
231 14 ▶ my $bits = $remote->{bits} // 0;
Condition50%1/2
$remote->{"bits"} // 0
l!l
140
MC/DC0%0/1
$remote->{"bits"} // 0
$remote->{"bits"}
Truth table50%1/2
A
A= $remote->{"bits"}
Aresult
11
00
232 14 ▶ my $pre_data_bits = $remote->{pre_data_bits} // 0;
Condition100%2/2
$remote->{"pre_data_bits"} // 0
l!l
95
MC/DC100%1/1
$remote->{"pre_data_bits"} // 0
$remote->{"pre_data_bits"}
Truth table100%2/2
A
A= $remote->{"pre_data_bits"}
Aresult
11
00
233 14 ▶ my $pre_data = $remote->{pre_data} // 0;
Condition100%2/2
$remote->{"pre_data"} // 0
l!l
95
MC/DC100%1/1
$remote->{"pre_data"} // 0
$remote->{"pre_data"}
Truth table100%2/2
A
A= $remote->{"pre_data"}
Aresult
11
00
234 14 ▶ my $post_data_bits = $remote->{post_data_bits} // 0;
Condition50%1/2
$remote->{"post_data_bits"} // 0
l!l
014
MC/DC0%0/1
$remote->{"post_data_bits"} // 0
$remote->{"post_data_bits"}
Truth table50%1/2
A
A= $remote->{"post_data_bits"}
Aresult
11
00
235 14 ▶ my $post_data = $remote->{post_data} // 0;
Condition50%1/2
$remote->{"post_data"} // 0
l!l
014
MC/DC0%0/1
$remote->{"post_data"} // 0
$remote->{"post_data"}
Truth table50%1/2
A
A= $remote->{"post_data"}
Aresult
11
00
236  
237 # LIRC SPACE_ENC remotes store each transmitted word (pre_data, post_data,
238 # and the code value) in the wire's accumulated byte order, whose per-byte
239 # LSB-first reading the decode_byte_order reduction below unwraps. A remote
240 # flagged REVERSE stores each word bit-mirrored within its own declared
241 # width instead -- the same signal, written backwards. Restoring the wire
242 # order first makes both encodings converge on one code: Vizio VX37L
243 # (REVERSE, pre_data 0xFB04 + Power 0xF708) and LCD_TV (no REVERSE,
244 # pre_data 0x20DF + Power 0x10EF) are the same NEC signal, the second
245 # being the 16-bit mirror of the first.
246 14 ▶ my $had_pre_post = ($pre_data_bits > 0 || $post_data_bits > 0);
Condition66%2/3
$pre_data_bits > 0 || $post_data_bits > 0
l!l&&r!l&&!r
905
MC/DC50%1/2
$pre_data_bits > 0 || $post_data_bits > 0
$pre_data_bits > 0 $post_data_bits > 0
Truth table66%2/3
A || B
A= $pre_data_bits > 0B= $post_data_bits > 0
ABresult
1X1
011
000
247 14 ▶ my $reversed = $had_pre_post && ($remote->{flags} // '') =~ /(?:^|\|)REVERSE(?:\||$)/i;
Condition80%4/5
$had_pre_post && ($remote->{'flags'} // '') =~ /(?:^|\|)REVERSE(?:\||$)/i
!ll&&!rl&&r
554
$remote->{"flags"} // ""
l!l
90
MC/DC50%1/2
$had_pre_post && ($remote->{'flags'} // '') =~ /(?:^|\|)REVERSE(?:\||$)/i
$had_pre_post $remote->{"flags"}
Truth table66%2/3
A && B
A= $had_pre_postB= $remote->{"flags"}
ABresult
0X0
100
111
248  
249 # Try to infer protocol from timing parameters
250 14 my $proto = _infer_protocol($remote);
251  
252 14 ▶ for my $pair (@{ $remote->{codes} || [] }) {
Condition50%1/2
$remote->{'codes'} || []
l!l
140
MC/DC0%0/1
$remote->{'codes'} || []
$remote->{'codes'}
Truth table50%1/2
A
A= $remote->{'codes'}
Aresult
11
00
253 422 my ($button_name, $hex_val) = @$pair;
254 422 my $value = hex($hex_val);
255  
256 # Mask to the declared bit width — LIRC files sometimes represent
257 # 16-bit values as 48-bit hex (zero-padded), but only the low
258 # bits matter.
259 422 ▶ if ($bits > 0 && $bits < 64) {
Branch: 1 branch
BranchTrueFalseTotal
if ($bits > 0 and $bits < 64) 422 0 422
Condition33%1/3
$bits > 0 and $bits < 64
!ll&&!rl&&r
00422
MC/DC0%0/2
$bits > 0 and $bits < 64
$bits > 0 $bits < 64
Truth table33%1/3
A and B
A= $bits > 0B= $bits < 64
ABresult
0X0
100
111
260 422 my $mask = (1 << $bits) - 1;
261 422 $value &= $mask;
262 }
263  
264 # Construct the full data word from pre_data, value, post_data. A
265 # REVERSE remote stores each word bit-mirrored, so mirror it back to
266 # the wire's accumulated order before combining; the decode_byte_order
267 # reduction below then unwraps the per-byte LSB-first reading for both
268 # encodings alike.
269 422 ▶ my $full_data = $reversed ? _mirror_word($value, $bits) : $value;
Branch: 1 branch
BranchTrueFalseTotal
$reversed ? : 192 230 422
270 422 ▶ if ($pre_data_bits > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pre_data_bits > 0) 352 70 422
271 352 ▶ my $pre = $reversed ? _mirror_word($pre_data, $pre_data_bits) : $pre_data;
Branch: 1 branch
BranchTrueFalseTotal
$reversed ? : 192 160 352
272 352 $full_data = ($pre << $bits) | $full_data;
273 }
274 422 ▶ if ($post_data_bits > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($post_data_bits > 0) 0 422 422
275 0 ▶ my $post = $reversed ? _mirror_word($post_data, $post_data_bits) : $post_data;
Branch: 1 branch
BranchTrueFalseTotal
$reversed ? : 0 0 0
276 0 $full_data = ($full_data << $post_data_bits) | $post;
277 }
278  
279 422 my $code;
280 422 ▶ my $proto_class = $proto ? $registry->get_protocol($proto) : undef;
Branch: 1 branch
BranchTrueFalseTotal
$proto ? : 418 4 422
281 422 ▶ if ($proto_class) {
Branch: 1 branch
BranchTrueFalseTotal
if ($proto_class) 418 4 422
282 # A code composed from pre_data/post_data carries the wire's
283 # accumulated byte order (e.g. NEC 0xC10000FF from pre_data 0xC100
284 # and a 16-bit value 0x00FF) and must be reduced to the display
285 # form via decode_byte_order (as the JS port and the rm-sg20 case
286 # do) to land on the real address/subaddress/command instead of
287 # treating the accumulated bytes as the fields. A plain
288 # codes-section value is already in its final form, so it keeps
289 # the raw decode_raw path.
290 418 ▶ if ($had_pre_post && $proto_class->can('decode_byte_order')) {
Branch: 2 branches
BranchTrueFalseTotal
if ($had_pre_post and $proto_class->can('decode_byte_order')) { } 352 66 418
elsif ($proto_class->can('decode_raw')) { } 66 0 66
Condition66%2/3
$had_pre_post and $proto_class->can("decode_byte_order")
!ll&&!rl&&r
660352
MC/DC50%1/2
$had_pre_post and $proto_class->can("decode_byte_order")
$had_pre_post $proto_class->can("decode_byte_order")
Truth table66%2/3
A and B
A= $had_pre_postB= $proto_class->can("decode_byte_order")
ABresult
0X0
100
111
291 352 $code = $proto_class->decode_byte_order($full_data, 0);
292 } elsif ($proto_class->can('decode_raw')) {
293 66 $code = $proto_class->decode_raw($full_data);
294 }
295 }
296 422 ▶ unless ($code) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($code) 4 418 422
297 4 ▶ $code = Protocol::IR::Code->new(
Condition50%1/2
$proto // "UNKNOWN"
l!l
04
MC/DC0%0/1
$proto // "UNKNOWN"
$proto
Truth table50%1/2
A
A= $proto
Aresult
11
00
298 protocol => $proto // 'UNKNOWN',
299 bits => $bits + $pre_data_bits + $post_data_bits,
300 data => $full_data,
301 );
302 }
303  
304 422 $code->alias($button_name);
305 422 push @codes, $code;
306 }
307  
308 14 return @codes;
309 }
310  
311 # Decode raw codes from a remote block.
312 1 ▶sub _decode_raw_codes {
Subroutine _decode_raw_codes: called CC=11 SCAR=24.2
313 1 my ($remote, $registry) = @_;
314 1 my @codes;
315  
316 1 ▶ for my $pair (@{ $remote->{raw_codes} || [] }) {
Condition50%1/2
$remote->{'raw_codes'} || []
l!l
10
MC/DC0%0/1
$remote->{'raw_codes'} || []
$remote->{'raw_codes'}
Truth table50%1/2
A
A= $remote->{'raw_codes'}
Aresult
11
00
317 1 my ($button_name, $values) = @$pair;
318  
319 # Convert alternating pulse/space values to pairs
320 1 my @flat = @$values;
321 1 my @pairs;
322 1 ▶ while (@flat) {
Branch: 1 branch
BranchTrueFalseTotal
if (@flat) 34 1 35
323 34 my $mark = shift @flat;
324 34 ▶ my $space = shift @flat // 0;
Condition50%1/2
shift @flat // 0
l!l
340
MC/DC0%0/1
shift @flat // 0
shift @flat
Truth table50%1/2
A
A= shift @flat
Aresult
11
00
325 34 push @pairs, [$mark, $space];
326 }
327  
328 # Try timing-based decode through registered protocols
329 1 my $code;
330 1 for my $proto_class ($registry->get_protocols()) {
331 2 ▶ next unless $proto_class->can('decode_timing');
Branch: 1 branch
BranchTrueFalseTotal
unless $proto_class->can("decode_timing") 0 2 2
332 2 my $decoded = $proto_class->decode_timing(\@pairs);
333 2 ▶ if (defined $decoded) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $decoded) 1 1 2
334 1 $code = $decoded;
335 1 last;
336 }
337 }
338  
339 1 ▶ $code ||= Protocol::IR::Code->new(protocol => 'UNKNOWN');
Condition33%1/3
$code ||= "Protocol::IR::Code"->new("protocol", "UNKNOWN")
l!l&&r!l&&!r
100
MC/DC0%0/2
$code ||= "Protocol::IR::Code"->new("protocol", "UNKNOWN")
$code "Protocol::IR::Code"->new("protocol", "UNKNOWN")
Truth table33%1/3
A ||= B
A= $codeB= "Protocol::IR::Code"->new("protocol", "UNKNOWN")
ABresult
1X1
011
000
340  
341 # Store raw timings for lossless re-export
342 1 my @timings;
343 1 for my $i (0 .. $#$values) {
344 68 ▶ push @timings, ($i % 2 == 0 ? 1 : -1) * $values->[$i];
Branch: 1 branch
BranchTrueFalseTotal
$i % 2 == 0 ? : 34 34 68
345 }
346 1 $code->timings(\@timings);
347 1 $code->alias($button_name);
348  
349 1 push @codes, $code;
350 }
351  
352 1 return @codes;
353 }
354  
355 # Try to infer the protocol name from the remote's timing parameters.
356 14 ▶sub _infer_protocol {
Subroutine _infer_protocol: called CC=49 SCAR=47.4
357 14 my ($remote) = @_;
358 14 ▶ my $header = $remote->{header} || [];
Condition50%1/2
$remote->{'header'} || []
l!l
140
MC/DC0%0/1
$remote->{'header'} || []
$remote->{'header'}
Truth table50%1/2
A
A= $remote->{'header'}
Aresult
11
00
359 14 ▶ my $one = $remote->{one} || [];
Condition50%1/2
$remote->{'one'} || []
l!l
140
MC/DC0%0/1
$remote->{'one'} || []
$remote->{'one'}
Truth table50%1/2
A
A= $remote->{'one'}
Aresult
11
00
360 14 ▶ my $zero = $remote->{zero} || [];
Condition50%1/2
$remote->{'zero'} || []
l!l
140
MC/DC0%0/1
$remote->{'zero'} || []
$remote->{'zero'}
Truth table50%1/2
A
A= $remote->{'zero'}
Aresult
11
00
361  
362 # NEC: ~9000/4500 header, ~560/1690 one-bit
363 14 ▶ return 'NEC' if @$header == 2 && $header->[0] >= 8500 && $header->[0] <= 10000
Branch: 1 branch
BranchTrueFalseTotal
if @$header == 2 and $header->[0] >= 8500 and $header->[0] <= 10000 and $header->[1] >= 3500 and $header->[1] <= 5500 and @$one == 2 and $one->[0] >= 400 and $one->[0] <= 700 and $one->[1] >= 1560 and $one->[1] <= 2000 and @$zero == 2 and $zero->[0] >= 400 and $zero->[0] <= 700 and $zero->[1] >= 300 and $zero->[1] <= 800 8 6 14
Condition66%28/42
@$header == 2 and $header->[0] >= 8500 and $header->[0] <= 10000 and $header->[1] >= 3500 and $header->[1] <= 5500 and @$one == 2 and $one->[0] >= 400 and $one->[0] <= 700 and $one->[1] >= 1560 and $one->[1] <= 2000 and @$zero == 2 and $zero->[0] >= 400 and $zero->[0] <= 700 and $zero->[1] >= 300 and $zero->[1] <= 800
!ll&&!rl&&r
608
@$header == 2 and $header->[0] >= 8500 and $header->[0] <= 10000 and $header->[1] >= 3500 and $header->[1] <= 5500 and @$one == 2 and $one->[0] >= 400 and $one->[0] <= 700 and $one->[1] >= 1560 and $one->[1] <= 2000 and @$zero == 2 and $zero->[0] >= 400 and $zero->[0] <= 700 and $zero->[1] >= 300
!ll&&!rl&&r
608
@$header == 2 and $header->[0] >= 8500 and $header->[0] <= 10000 and $header->[1] >= 3500 and $header->[1] <= 5500 and @$one == 2 and $one->[0] >= 400 and $one->[0] <= 700 and $one->[1] >= 1560 and $one->[1] <= 2000 and @$zero == 2 and $zero->[0] >= 400 and $zero->[0] <= 700
!ll&&!rl&&r
608
@$header == 2 and $header->[0] >= 8500 and $header->[0] <= 10000 and $header->[1] >= 3500 and $header->[1] <= 5500 and @$one == 2 and $one->[0] >= 400 and $one->[0] <= 700 and $one->[1] >= 1560 and $one->[1] <= 2000 and @$zero == 2 and $zero->[0] >= 400
!ll&&!rl&&r
608
@$header == 2 and $header->[0] >= 8500 and $header->[0] <= 10000 and $header->[1] >= 3500 and $header->[1] <= 5500 and @$one == 2 and $one->[0] >= 400 and $one->[0] <= 700 and $one->[1] >= 1560 and $one->[1] <= 2000 and @$zero == 2
!ll&&!rl&&r
608
@$header == 2 and $header->[0] >= 8500 and $header->[0] <= 10000 and $header->[1] >= 3500 and $header->[1] <= 5500 and @$one == 2 and $one->[0] >= 400 and $one->[0] <= 700 and $one->[1] >= 1560 and $one->[1] <= 2000
!ll&&!rl&&r
608
@$header == 2 and $header->[0] >= 8500 and $header->[0] <= 10000 and $header->[1] >= 3500 and $header->[1] <= 5500 and @$one == 2 and $one->[0] >= 400 and $one->[0] <= 700 and $one->[1] >= 1560
!ll&&!rl&&r
608
@$header == 2 and $header->[0] >= 8500 and $header->[0] <= 10000 and $header->[1] >= 3500 and $header->[1] <= 5500 and @$one == 2 and $one->[0] >= 400 and $one->[0] <= 700
!ll&&!rl&&r
608
@$header == 2 and $header->[0] >= 8500 and $header->[0] <= 10000 and $header->[1] >= 3500 and $header->[1] <= 5500 and @$one == 2 and $one->[0] >= 400
!ll&&!rl&&r
608
@$header == 2 and $header->[0] >= 8500 and $header->[0] <= 10000 and $header->[1] >= 3500 and $header->[1] <= 5500 and @$one == 2
!ll&&!rl&&r
608
@$header == 2 and $header->[0] >= 8500 and $header->[0] <= 10000 and $header->[1] >= 3500 and $header->[1] <= 5500
!ll&&!rl&&r
608
@$header == 2 and $header->[0] >= 8500 and $header->[0] <= 10000 and $header->[1] >= 3500
!ll&&!rl&&r
608
@$header == 2 and $header->[0] >= 8500 and $header->[0] <= 10000
!ll&&!rl&&r
608
@$header == 2 and $header->[0] >= 8500
!ll&&!rl&&r
068
MC/DC6%1/15
@$header == 2 and $header->[0] >= 8500 and $header->[0] <= 10000 and $header->[1] >= 3500 and $header->[1] <= 5500 and @$one == 2 and $one->[0] >= 400 and $one->[0] <= 700 and $one->[1] >= 1560 and $one->[1] <= 2000 and @$zero == 2 and $zero->[0] >= 400 and $zero->[0] <= 700 and $zero->[1] >= 300 and $zero->[1] <= 800
@$header == 2 $header->[0] >= 8500 $header->[0] <= 10000 $header->[1] >= 3500 $header->[1] <= 5500 @$one == 2 $one->[0] >= 400 $one->[0] <= 700 $one->[1] >= 1560 $one->[1] <= 2000 @$zero == 2 $zero->[0] >= 400 $zero->[0] <= 700 $zero->[1] >= 300 $zero->[1] <= 800
Truth table12%2/16
A and B and C and D and E and F and G and H and I and J and K and L and M and N and O
A= @$header == 2B= $header->[0] >= 8500C= $header->[0] <= 10000D= $header->[1] >= 3500E= $header->[1] <= 5500F= @$one == 2G= $one->[0] >= 400H= $one->[0] <= 700I= $one->[1] >= 1560J= $one->[1] <= 2000K= @$zero == 2L= $zero->[0] >= 400M= $zero->[0] <= 700N= $zero->[1] >= 300O= $zero->[1] <= 800
ABCDEFGHIJKLMNOresult
0XXXXXXXXXXXXXX0
10XXXXXXXXXXXXX0
110XXXXXXXXXXXX0
1110XXXXXXXXXXX0
11110XXXXXXXXXX0
111110XXXXXXXXX0
1111110XXXXXXXX0
11111110XXXXXXX0
111111110XXXXXX0
1111111110XXXXX0
11111111110XXXX0
111111111110XXX0
1111111111110XX0
11111111111110X0
1111111111111100
1111111111111111
364 && $header->[1] >= 3500 && $header->[1] <= 5500
365 && @$one == 2 && $one->[0] >= 400 && $one->[0] <= 700
366 && $one->[1] >= 1560 && $one->[1] <= 2000
367 && @$zero == 2 && $zero->[0] >= 400 && $zero->[0] <= 700
368 && $zero->[1] >= 300 && $zero->[1] <= 800;
369  
370 6 ▶ return 'SAMSUNG' if @$header == 2 && $header->[0] >= 3500 && $header->[0] <= 5500
Branch: 1 branch
BranchTrueFalseTotal
if @$header == 2 and $header->[0] >= 3500 and $header->[0] <= 5500 and $header->[1] >= 3500 and $header->[1] <= 5500 and @$one == 2 and $one->[0] >= 400 and $one->[0] <= 700 and $one->[1] >= 1400 and $one->[1] <= 2000 and @$zero == 2 and $zero->[0] >= 400 and $zero->[0] <= 700 and $zero->[1] >= 300 and $zero->[1] <= 800 4 2 6
Condition69%29/42
@$header == 2 and $header->[0] >= 3500 and $header->[0] <= 5500 and $header->[1] >= 3500 and $header->[1] <= 5500 and @$one == 2 and $one->[0] >= 400 and $one->[0] <= 700 and $one->[1] >= 1400 and $one->[1] <= 2000 and @$zero == 2 and $zero->[0] >= 400 and $zero->[0] <= 700 and $zero->[1] >= 300 and $zero->[1] <= 800
!ll&&!rl&&r
204
@$header == 2 and $header->[0] >= 3500 and $header->[0] <= 5500 and $header->[1] >= 3500 and $header->[1] <= 5500 and @$one == 2 and $one->[0] >= 400 and $one->[0] <= 700 and $one->[1] >= 1400 and $one->[1] <= 2000 and @$zero == 2 and $zero->[0] >= 400 and $zero->[0] <= 700 and $zero->[1] >= 300
!ll&&!rl&&r
204
@$header == 2 and $header->[0] >= 3500 and $header->[0] <= 5500 and $header->[1] >= 3500 and $header->[1] <= 5500 and @$one == 2 and $one->[0] >= 400 and $one->[0] <= 700 and $one->[1] >= 1400 and $one->[1] <= 2000 and @$zero == 2 and $zero->[0] >= 400 and $zero->[0] <= 700
!ll&&!rl&&r
204
@$header == 2 and $header->[0] >= 3500 and $header->[0] <= 5500 and $header->[1] >= 3500 and $header->[1] <= 5500 and @$one == 2 and $one->[0] >= 400 and $one->[0] <= 700 and $one->[1] >= 1400 and $one->[1] <= 2000 and @$zero == 2 and $zero->[0] >= 400
!ll&&!rl&&r
204
@$header == 2 and $header->[0] >= 3500 and $header->[0] <= 5500 and $header->[1] >= 3500 and $header->[1] <= 5500 and @$one == 2 and $one->[0] >= 400 and $one->[0] <= 700 and $one->[1] >= 1400 and $one->[1] <= 2000 and @$zero == 2
!ll&&!rl&&r
204
@$header == 2 and $header->[0] >= 3500 and $header->[0] <= 5500 and $header->[1] >= 3500 and $header->[1] <= 5500 and @$one == 2 and $one->[0] >= 400 and $one->[0] <= 700 and $one->[1] >= 1400 and $one->[1] <= 2000
!ll&&!rl&&r
204
@$header == 2 and $header->[0] >= 3500 and $header->[0] <= 5500 and $header->[1] >= 3500 and $header->[1] <= 5500 and @$one == 2 and $one->[0] >= 400 and $one->[0] <= 700 and $one->[1] >= 1400
!ll&&!rl&&r
204
@$header == 2 and $header->[0] >= 3500 and $header->[0] <= 5500 and $header->[1] >= 3500 and $header->[1] <= 5500 and @$one == 2 and $one->[0] >= 400 and $one->[0] <= 700
!ll&&!rl&&r
204
@$header == 2 and $header->[0] >= 3500 and $header->[0] <= 5500 and $header->[1] >= 3500 and $header->[1] <= 5500 and @$one == 2 and $one->[0] >= 400
!ll&&!rl&&r
204
@$header == 2 and $header->[0] >= 3500 and $header->[0] <= 5500 and $header->[1] >= 3500 and $header->[1] <= 5500 and @$one == 2
!ll&&!rl&&r
204
@$header == 2 and $header->[0] >= 3500 and $header->[0] <= 5500 and $header->[1] >= 3500 and $header->[1] <= 5500
!ll&&!rl&&r
204
@$header == 2 and $header->[0] >= 3500 and $header->[0] <= 5500 and $header->[1] >= 3500
!ll&&!rl&&r
204
@$header == 2 and $header->[0] >= 3500 and $header->[0] <= 5500
!ll&&!rl&&r
114
@$header == 2 and $header->[0] >= 3500
!ll&&!rl&&r
015
MC/DC13%2/15
@$header == 2 and $header->[0] >= 3500 and $header->[0] <= 5500 and $header->[1] >= 3500 and $header->[1] <= 5500 and @$one == 2 and $one->[0] >= 400 and $one->[0] <= 700 and $one->[1] >= 1400 and $one->[1] <= 2000 and @$zero == 2 and $zero->[0] >= 400 and $zero->[0] <= 700 and $zero->[1] >= 300 and $zero->[1] <= 800
@$header == 2 $header->[0] >= 3500 $header->[0] <= 5500 $header->[1] >= 3500 $header->[1] <= 5500 @$one == 2 $one->[0] >= 400 $one->[0] <= 700 $one->[1] >= 1400 $one->[1] <= 2000 @$zero == 2 $zero->[0] >= 400 $zero->[0] <= 700 $zero->[1] >= 300 $zero->[1] <= 800
Truth table18%3/16
A and B and C and D and E and F and G and H and I and J and K and L and M and N and O
A= @$header == 2B= $header->[0] >= 3500C= $header->[0] <= 5500D= $header->[1] >= 3500E= $header->[1] <= 5500F= @$one == 2G= $one->[0] >= 400H= $one->[0] <= 700I= $one->[1] >= 1400J= $one->[1] <= 2000K= @$zero == 2L= $zero->[0] >= 400M= $zero->[0] <= 700N= $zero->[1] >= 300O= $zero->[1] <= 800
ABCDEFGHIJKLMNOresult
0XXXXXXXXXXXXXX0
10XXXXXXXXXXXXX0
110XXXXXXXXXXXX0
1110XXXXXXXXXXX0
11110XXXXXXXXXX0
111110XXXXXXXXX0
1111110XXXXXXXX0
11111110XXXXXXX0
111111110XXXXXX0
1111111110XXXXX0
11111111110XXXX0
111111111110XXX0
1111111111110XX0
11111111111110X0
1111111111111100
1111111111111111
371 && $header->[1] >= 3500 && $header->[1] <= 5500
372 && @$one == 2 && $one->[0] >= 400 && $one->[0] <= 700
373 && $one->[1] >= 1400 && $one->[1] <= 2000
374 && @$zero == 2 && $zero->[0] >= 400 && $zero->[0] <= 700
375 && $zero->[1] >= 300 && $zero->[1] <= 800;
376  
377 # JVC: ~8440/4220 header, ~526/1276 one-bit (wider tolerance for variants)
378 2 ▶ return 'JVC' if @$header == 2 && $header->[0] >= 7000 && $header->[0] <= 10000
Branch: 1 branch
BranchTrueFalseTotal
if @$header == 2 and $header->[0] >= 7000 and $header->[0] <= 10000 and $header->[1] >= 3000 and $header->[1] <= 5500 and @$one == 2 and $one->[0] >= 400 and $one->[0] <= 700 and $one->[1] >= 1000 and $one->[1] <= 1560 and @$zero == 2 and $zero->[0] >= 400 and $zero->[0] <= 700 and $zero->[1] >= 300 and $zero->[1] <= 800 1 1 2
Condition66%28/42
@$header == 2 and $header->[0] >= 7000 and $header->[0] <= 10000 and $header->[1] >= 3000 and $header->[1] <= 5500 and @$one == 2 and $one->[0] >= 400 and $one->[0] <= 700 and $one->[1] >= 1000 and $one->[1] <= 1560 and @$zero == 2 and $zero->[0] >= 400 and $zero->[0] <= 700 and $zero->[1] >= 300 and $zero->[1] <= 800
!ll&&!rl&&r
101
@$header == 2 and $header->[0] >= 7000 and $header->[0] <= 10000 and $header->[1] >= 3000 and $header->[1] <= 5500 and @$one == 2 and $one->[0] >= 400 and $one->[0] <= 700 and $one->[1] >= 1000 and $one->[1] <= 1560 and @$zero == 2 and $zero->[0] >= 400 and $zero->[0] <= 700 and $zero->[1] >= 300
!ll&&!rl&&r
101
@$header == 2 and $header->[0] >= 7000 and $header->[0] <= 10000 and $header->[1] >= 3000 and $header->[1] <= 5500 and @$one == 2 and $one->[0] >= 400 and $one->[0] <= 700 and $one->[1] >= 1000 and $one->[1] <= 1560 and @$zero == 2 and $zero->[0] >= 400 and $zero->[0] <= 700
!ll&&!rl&&r
101
@$header == 2 and $header->[0] >= 7000 and $header->[0] <= 10000 and $header->[1] >= 3000 and $header->[1] <= 5500 and @$one == 2 and $one->[0] >= 400 and $one->[0] <= 700 and $one->[1] >= 1000 and $one->[1] <= 1560 and @$zero == 2 and $zero->[0] >= 400
!ll&&!rl&&r
101
@$header == 2 and $header->[0] >= 7000 and $header->[0] <= 10000 and $header->[1] >= 3000 and $header->[1] <= 5500 and @$one == 2 and $one->[0] >= 400 and $one->[0] <= 700 and $one->[1] >= 1000 and $one->[1] <= 1560 and @$zero == 2
!ll&&!rl&&r
101
@$header == 2 and $header->[0] >= 7000 and $header->[0] <= 10000 and $header->[1] >= 3000 and $header->[1] <= 5500 and @$one == 2 and $one->[0] >= 400 and $one->[0] <= 700 and $one->[1] >= 1000 and $one->[1] <= 1560
!ll&&!rl&&r
101
@$header == 2 and $header->[0] >= 7000 and $header->[0] <= 10000 and $header->[1] >= 3000 and $header->[1] <= 5500 and @$one == 2 and $one->[0] >= 400 and $one->[0] <= 700 and $one->[1] >= 1000
!ll&&!rl&&r
101
@$header == 2 and $header->[0] >= 7000 and $header->[0] <= 10000 and $header->[1] >= 3000 and $header->[1] <= 5500 and @$one == 2 and $one->[0] >= 400 and $one->[0] <= 700
!ll&&!rl&&r
101
@$header == 2 and $header->[0] >= 7000 and $header->[0] <= 10000 and $header->[1] >= 3000 and $header->[1] <= 5500 and @$one == 2 and $one->[0] >= 400
!ll&&!rl&&r
101
@$header == 2 and $header->[0] >= 7000 and $header->[0] <= 10000 and $header->[1] >= 3000 and $header->[1] <= 5500 and @$one == 2
!ll&&!rl&&r
101
@$header == 2 and $header->[0] >= 7000 and $header->[0] <= 10000 and $header->[1] >= 3000 and $header->[1] <= 5500
!ll&&!rl&&r
101
@$header == 2 and $header->[0] >= 7000 and $header->[0] <= 10000 and $header->[1] >= 3000
!ll&&!rl&&r
101
@$header == 2 and $header->[0] >= 7000 and $header->[0] <= 10000
!ll&&!rl&&r
101
@$header == 2 and $header->[0] >= 7000
!ll&&!rl&&r
011
MC/DC6%1/15
@$header == 2 and $header->[0] >= 7000 and $header->[0] <= 10000 and $header->[1] >= 3000 and $header->[1] <= 5500 and @$one == 2 and $one->[0] >= 400 and $one->[0] <= 700 and $one->[1] >= 1000 and $one->[1] <= 1560 and @$zero == 2 and $zero->[0] >= 400 and $zero->[0] <= 700 and $zero->[1] >= 300 and $zero->[1] <= 800
@$header == 2 $header->[0] >= 7000 $header->[0] <= 10000 $header->[1] >= 3000 $header->[1] <= 5500 @$one == 2 $one->[0] >= 400 $one->[0] <= 700 $one->[1] >= 1000 $one->[1] <= 1560 @$zero == 2 $zero->[0] >= 400 $zero->[0] <= 700 $zero->[1] >= 300 $zero->[1] <= 800
Truth table12%2/16
A and B and C and D and E and F and G and H and I and J and K and L and M and N and O
A= @$header == 2B= $header->[0] >= 7000C= $header->[0] <= 10000D= $header->[1] >= 3000E= $header->[1] <= 5500F= @$one == 2G= $one->[0] >= 400H= $one->[0] <= 700I= $one->[1] >= 1000J= $one->[1] <= 1560K= @$zero == 2L= $zero->[0] >= 400M= $zero->[0] <= 700N= $zero->[1] >= 300O= $zero->[1] <= 800
ABCDEFGHIJKLMNOresult
0XXXXXXXXXXXXXX0
10XXXXXXXXXXXXX0
110XXXXXXXXXXXX0
1110XXXXXXXXXXX0
11110XXXXXXXXXX0
111110XXXXXXXXX0
1111110XXXXXXXX0
11111110XXXXXXX0
111111110XXXXXX0
1111111110XXXXX0
11111111110XXXX0
111111111110XXX0
1111111111110XX0
11111111111110X0
1111111111111100
1111111111111111
379 && $header->[1] >= 3000 && $header->[1] <= 5500
380 && @$one == 2 && $one->[0] >= 400 && $one->[0] <= 700
381 && $one->[1] >= 1000 && $one->[1] <= 1560
382 && @$zero == 2 && $zero->[0] >= 400 && $zero->[0] <= 700
383 && $zero->[1] >= 300 && $zero->[1] <= 800;
384  
385 1 return undef;
386 }
387  
388 # Format a value as hex padded to the given bit width.
389 0 ▶sub _data_hex_for_bits {
Subroutine _data_hex_for_bits: not called CC=2 SCAR=17.9
390 0 my ($val, $bits) = @_;
391 0 my $bytes = int(($bits + 7) / 8);
392 0 my $hex = uc sprintf("%X", $val);
393 0 my $pad = $bytes * 2;
394 0 ▶ $hex = ('0' x ($pad - length($hex))) . $hex if length($hex) < $pad;
Branch: 1 branch
BranchTrueFalseTotal
if length $hex < $pad 0 0 0
395 0 return '0x' . $hex;
396 }
397  
398 # --- Export --------------------------------------------------------------
399  
400 5 ▶sub export {
Subroutine export: called CC=8 SCAR=21.1
Pod documented
401 5 my ($class, $codes, $registry, %opts) = @_;
402 5 ▶ $codes = [$codes] unless ref $codes eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $codes eq "ARRAY" 3 2 5
403 5 ▶ die "No codes to export\n" unless @$codes;
Branch: 1 branch
BranchTrueFalseTotal
unless @$codes 0 5 5
404  
405 5 ▶ my $remote_name = $opts{name} // 'exported';
Condition100%2/2
$opts{"name"} // "exported"
l!l
41
MC/DC100%1/1
$opts{"name"} // "exported"
$opts{"name"}
Truth table100%2/2
A
A= $opts{"name"}
Aresult
11
00
406 5 my $output = '';
407  
408 # Group codes by protocol for multi-button remotes
409 5 my %by_proto;
410 5 for my $code (@$codes) {
411 47 ▶ my $proto = $code->protocol // 'UNKNOWN';
Condition50%1/2
$code->protocol // "UNKNOWN"
l!l
470
MC/DC0%0/1
$code->protocol // "UNKNOWN"
$code->protocol
Truth table50%1/2
A
A= $code->protocol
Aresult
11
00
412 47 push @{ $by_proto{$proto} }, $code;
413 }
414  
415 5 for my $proto (sort keys %by_proto) {
416 5 my $proto_codes = $by_proto{$proto};
417 5 my $template = $LIRC_TEMPLATES{uc $proto};
418  
419 5 ▶ if ($template) {
Branch: 1 branch
BranchTrueFalseTotal
if ($template) { } 5 0 5
420 5 $output .= _export_protocol_remote($remote_name, $template, $proto_codes, $registry);
421 } else {
422 # Fall back to raw codes for unknown protocols
423 0 $output .= _export_raw_remote($remote_name, $proto_codes, $registry);
424 }
425 }
426  
427 5 return $output;
428 }
429  
430 5 ▶sub _export_protocol_remote {
Subroutine _export_protocol_remote: called CC=9 SCAR=23.1
431 5 my ($remote_name, $template, $codes, $registry) = @_;
432 5 my $out = '';
433  
434 5 $out .= "begin remote\n\n";
435 5 $out .= " name $remote_name\n";
436 5 $out .= sprintf(" bits %d\n", $template->{bits});
437 5 $out .= " flags $template->{flags}\n";
438 5 $out .= " eps 30\n";
439 5 $out .= " aeps 100\n\n";
440 5 $out .= sprintf(" header %d %d\n", @{ $template->{header} });
441 5 $out .= sprintf(" one %d %d\n", @{ $template->{one} });
442 5 $out .= sprintf(" zero %d %d\n", @{ $template->{zero} });
443 5 $out .= sprintf(" ptrail %d\n", $template->{ptrail});
444 5 $out .= sprintf(" repeat %d %d\n", @{ $template->{repeat} });
445  
446 5 ▶ if ($template->{pre_data_bits} && $template->{pre_data_bits} > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($template->{"pre_data_bits"} and $template->{"pre_data_bits"} > 0) 0 5 5
Condition33%1/3
$template->{"pre_data_bits"} and $template->{"pre_data_bits"} > 0
!ll&&!rl&&r
500
MC/DC0%0/2
$template->{"pre_data_bits"} and $template->{"pre_data_bits"} > 0
$template->{"pre_data_bits"} $template->{"pre_data_bits"} > 0
Truth table33%1/3
A and B
A= $template->{"pre_data_bits"}B= $template->{"pre_data_bits"} > 0
ABresult
0X0
100
111
447 0 $out .= sprintf(" pre_data_bits %d\n", $template->{pre_data_bits});
448 0 $out .= sprintf(" pre_data 0x%X\n", $template->{pre_data});
449 }
450 5 ▶ if ($template->{post_data_bits} && $template->{post_data_bits} > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($template->{"post_data_bits"} and $template->{"post_data_bits"} > 0) 0 5 5
Condition33%1/3
$template->{"post_data_bits"} and $template->{"post_data_bits"} > 0
!ll&&!rl&&r
500
MC/DC0%0/2
$template->{"post_data_bits"} and $template->{"post_data_bits"} > 0
$template->{"post_data_bits"} $template->{"post_data_bits"} > 0
Truth table33%1/3
A and B
A= $template->{"post_data_bits"}B= $template->{"post_data_bits"} > 0
ABresult
0X0
100
111
451 0 $out .= sprintf(" post_data_bits %d\n", $template->{post_data_bits});
452 0 $out .= sprintf(" post_data 0x%X\n", $template->{post_data});
453 }
454  
455 5 $out .= sprintf(" gap %d\n", $template->{gap});
456 5 $out .= sprintf(" toggle_bit_mask 0x%X\n", $template->{toggle_bit_mask});
457 5 ▶ $out .= sprintf(" min_repeat %d\n", $template->{min_repeat} // 1);
Condition50%1/2
$template->{"min_repeat"} // 1
l!l
50
MC/DC0%0/1
$template->{"min_repeat"} // 1
$template->{"min_repeat"}
Truth table50%1/2
A
A= $template->{"min_repeat"}
Aresult
11
00
458  
459 5 $out .= "\n begin codes\n";
460 5 for my $code (@$codes) {
461 47 ▶ my $name = ($code->alias // '') =~ /\S/ ? $code->alias : 'UNKNOWN';
Branch: 1 branch
BranchTrueFalseTotal
($code->alias // '') =~ /\S/ ? : 45 2 47
Condition50%1/2
$code->alias // ""
l!l
470
MC/DC0%0/1
$code->alias // ""
$code->alias
Truth table50%1/2
A
A= $code->alias
Aresult
11
00
462 47 my $val = _lirc_code_value($code, $template, $registry);
463 47 $out .= sprintf(" %-20s %s\n", $name, $val);
464 }
465 5 $out .= " end codes\n\n";
466 5 $out .= "end remote\n\n";
467  
468 5 return $out;
469 }
470  
471 0 ▶sub _export_raw_remote {
Subroutine _export_raw_remote: not called CC=12 SCAR=50.5
472 0 my ($remote_name, $codes, $registry) = @_;
473 0 my $out = '';
474  
475 0 $out .= "begin remote\n\n";
476 0 $out .= " name $remote_name\n";
477 0 $out .= " flags RAW_CODES|CONST_LENGTH\n";
478 0 $out .= " eps 25\n";
479 0 $out .= " aeps 100\n\n";
480 0 $out .= " ptrail 0\n";
481 0 $out .= " repeat 0 0\n";
482 0 $out .= " gap 100000\n\n";
483 0 $out .= " begin raw_codes\n\n";
484  
485 0 for my $code (@$codes) {
486 0 ▶ my $name = ($code->alias // '') =~ /\S/ ? $code->alias : 'UNKNOWN';
Branch: 1 branch
BranchTrueFalseTotal
($code->alias // '') =~ /\S/ ? : 0 0 0
Condition0%0/2
$code->alias // ""
l!l
00
MC/DC0%0/1
$code->alias // ""
$code->alias
Truth table0%0/2
A
A= $code->alias
Aresult
11
00
487 0 my $timings = $code->timings;
488  
489 0 ▶ if ($timings && @$timings) {
Branch: 1 branch
BranchTrueFalseTotal
if ($timings and @$timings) { } 0 0 0
Condition0%0/3
$timings and @$timings
!ll&&!rl&&r
000
MC/DC0%0/2
$timings and @$timings
$timings @$timings
Truth table0%0/3
A and B
A= $timingsB= @$timings
ABresult
0X0
100
111
490 # Use the stored timings
491 0 $out .= " name $name\n";
492 0 my @vals = map { int(abs($_) + 0.5) } @$timings;
493 # Format in groups of 8
494 0 ▶ while (@vals) {
Branch: 1 branch
BranchTrueFalseTotal
if (@vals) 0 0 0
495 0 my @chunk = splice(@vals, 0, 8);
496 0 $out .= " " . join(" ", @chunk) . "\n";
497 }
498 0 $out .= "\n";
499 } else {
500 # Try to generate timings via Pronto round-trip
501 0 my $pronto;
502 0 eval { $pronto = $registry->export_code($code, 'Pronto'); 1 };
503 0 ▶ next unless $pronto && $pronto !~ /^0000 0000/;
Branch: 1 branch
BranchTrueFalseTotal
unless $pronto and not $pronto =~ /^0000 0000/ 0 0 0
Condition0%0/3
$pronto and not $pronto =~ /^0000 0000/
!ll&&!rl&&r
000
MC/DC0%0/2
$pronto and not $pronto =~ /^0000 0000/
$pronto not $pronto =~ /^0000 0000/
Truth table0%0/3
A and not(B)
A= $prontoB= not $pronto =~ /^0000 0000/
ABresult
0X0
100
111
504  
505 0 my $pronto_code;
506 0 eval { $pronto_code = $registry->import_format('Pronto', $pronto); 1 };
507 0 ▶ next unless $pronto_code && $pronto_code->timings;
Branch: 1 branch
BranchTrueFalseTotal
unless $pronto_code and $pronto_code->timings 0 0 0
Condition0%0/3
$pronto_code and $pronto_code->timings
!ll&&!rl&&r
000
MC/DC0%0/2
$pronto_code and $pronto_code->timings
$pronto_code $pronto_code->timings
Truth table0%0/3
A and B
A= $pronto_codeB= $pronto_code->timings
ABresult
0X0
100
111
508  
509 0 $timings = $pronto_code->timings;
510 0 $out .= " name $name\n";
511 0 my @vals = map { int(abs($_) + 0.5) } @$timings;
512 0 ▶ while (@vals) {
Branch: 1 branch
BranchTrueFalseTotal
if (@vals) 0 0 0
513 0 my @chunk = splice(@vals, 0, 8);
514 0 $out .= " " . join(" ", @chunk) . "\n";
515 }
516 0 $out .= "\n";
517 }
518 }
519  
520 0 $out .= " end raw_codes\n\n";
521 0 $out .= "end remote\n\n";
522  
523 0 return $out;
524 }
525  
526 # Extract the button value for a protocol-based LIRC code.
527 #
528 # A code's stored data may be the accumulated wire form (e.g. SAMSUNG
529 # 0x070702FD) rather than the display form LIRC carries (0xE0E040BF). Mirror
530 # the JS exporter: probe decode_raw with the stored data and, when the decoded
531 # value comes back different, the stored data is accumulated and must be
532 # per-byte bit-reversed to the display form.
533 47 ▶sub _lirc_code_value {
Subroutine _lirc_code_value: called CC=13 SCAR=30.3
534 47 my ($code, $template, $registry) = @_;
535 47 my $data = $code->data;
536 47 ▶ return '0x00' unless defined $data;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $data 0 47 47
537  
538 47 ▶ my $bits = $template->{bits} // 32;
Condition50%1/2
$template->{"bits"} // 32
l!l
470
MC/DC0%0/1
$template->{"bits"} // 32
$template->{"bits"}
Truth table50%1/2
A
A= $template->{"bits"}
Aresult
11
00
539 47 my $mask = (1 << $bits) - 1;
540  
541 47 my $raw = $data;
542 47 ▶ if ($registry && $code->protocol && $registry->can('get_protocol')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($registry and $code->protocol and $registry->can("get_protocol")) 47 0 47
Condition33%2/6
$registry and $code->protocol and $registry->can("get_protocol")
!ll&&!rl&&r
0047
$registry and $code->protocol
!ll&&!rl&&r
0047
MC/DC0%0/3
$registry and $code->protocol and $registry->can("get_protocol")
$registry $code->protocol $registry->can("get_protocol")
Truth table25%1/4
A and B and C
A= $registryB= $code->protocolC= $registry->can("get_protocol")
ABCresult
0XX0
10X0
1100
1111
543 47 my $proto_class = $registry->get_protocol($code->protocol);
544 47 ▶ if ($proto_class && $proto_class->can('decode_raw')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($proto_class and $proto_class->can("decode_raw")) 47 0 47
Condition33%1/3
$proto_class and $proto_class->can("decode_raw")
!ll&&!rl&&r
0047
MC/DC0%0/2
$proto_class and $proto_class->can("decode_raw")
$proto_class $proto_class->can("decode_raw")
Truth table33%1/3
A and B
A= $proto_classB= $proto_class->can("decode_raw")
ABresult
0X0
100
111
545 47 my $probe = eval { $proto_class->decode_raw($data) };
546 47 ▶ if ($probe && defined $probe->data && $probe->data != $data) {
Branch: 1 branch
BranchTrueFalseTotal
if ($probe and defined $probe->data and $probe->data != $data) 44 3 47
Condition50%3/6
$probe and defined $probe->data and $probe->data != $data
!ll&&!rl&&r
0344
$probe and defined $probe->data
!ll&&!rl&&r
0047
MC/DC33%1/3
$probe and defined $probe->data and $probe->data != $data
$probe defined $probe->data $probe->data != $data
Truth table50%2/4
A and B and C
A= $probeB= defined $probe->dataC= $probe->data != $data
ABCresult
0XX0
10X0
1100
1111
547 44 $raw = _reverse_bytes($data, $bits);
548 }
549 }
550 }
551  
552 47 ▶ my $val = ref $raw && $raw->can('band') ? $raw->band($mask)->numify : $raw & $mask;
Branch: 1 branch
BranchTrueFalseTotal
ref $raw && $raw->can('band') ? : 0 47 47
Condition33%1/3
ref $raw and $raw->can("band")
!ll&&!rl&&r
4700
MC/DC0%0/2
ref $raw and $raw->can("band")
ref $raw $raw->can("band")
Truth table33%1/3
A and B
A= ref $rawB= $raw->can("band")
ABresult
0X0
100
111
553  
554 47 return sprintf("0x%X", $val);
555 }
556  
557 # Reverse the bits within each byte of a $bits-bit value, keeping the byte
558 # order (the inverse of the accumulated-to-display mapping).
559 44 ▶sub _reverse_bytes {
Subroutine _reverse_bytes: called CC=2 SCAR=6.9
560 44 my ($val, $bits) = @_;
561 44 my $out = 0;
562 44 for my $i (0 .. $bits - 1) {
563 1408 my $byte = int($i / 8);
564 1408 my $bit = $i % 8;
565 1408 my $src = 8 * $byte + (7 - $bit);
566 1408 $out |= (($val >> $src) & 1) << $i;
567 }
568 44 return $out;
569 }
570  
571 # Mirror the $width significant bits of $word (bit $i <-> bit $width-1-$i),
572 # the mapping LIRC's REVERSE flag applies to each transmitted word.
573 384 ▶sub _mirror_word {
Subroutine _mirror_word: called CC=3 SCAR=11.0
574 384 my ($word, $width) = @_;
575 384 ▶ return $word if $width <= 0;
Branch: 1 branch
BranchTrueFalseTotal
if $width <= 0 0 384 384
576 384 my $out = 0;
577 384 for my $i (0 .. $width - 1) {
578 6144 $out |= (($word >> $i) & 1) << ($width - 1 - $i);
579 }
580 384 return $out;
581 }
582  
583 17 ▶sub _read_input {
Subroutine _read_input: called CC=6 SCAR=18.1
584 17 my ($input) = @_;
585 # Only try file I/O if the input looks like a file path (no newlines)
586 17 ▶ if (!ref $input && $input !~ /\n/ && -e $input && !-d $input) {
Branch: 1 branch
BranchTrueFalseTotal
if (not ref $input || $input =~ /\n/ and -e $input and not -d $input) 8 9 17
Condition77%7/9
not ref $input || $input =~ /\n/ and -e $input and not -d $input
!ll&&!rl&&r
908
not ref $input || $input =~ /\n/ and -e $input
!ll&&!rl&&r
818
ref $input || $input =~ /\n/
l!l&&r!l&&!r
089
MC/DC50%2/4
not ref $input || $input =~ /\n/ and -e $input and not -d $input
ref $input $input =~ /\n/ -e $input not -d $input
Truth table60%3/5
not(A || B) and C and not(D)
A= ref $inputB= $input =~ /\n/C= -e $inputD= not -d $input
ABCDresult
1XXX0
01XX0
000X0
00100
00111
587 8 ▶ open my $fh, '<:raw', $input or die "Cannot open '$input': $!\n";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<:raw", $input or die "Cannot open '${input}': $!\n" 8 0 8
588 8 local $/;
589 8 my $text = <$fh>;
590 8 close $fh;
591 8 return $text;
592 }
593 9 return $input;
594 }
595  
596 55 1;
597  
598 =encoding utf8
599  
600 =head1 NAME
601  
602 Protocol::IR::Format::LIRC - LIRC remote definition format (.lircd.conf) import and export
603  
604 =head1 VERSION
605  
606 version 1.5
607  
608 =head1 SYNOPSIS
609  
610 use Protocol::IR::Converter;
611  
612 my $converter = Protocol::IR::Converter->new();
613  
614 # Import a LIRC remote definition
615 my $codes = $converter->import_format('LIRC', $lirc_text);
616 for my $code (@$codes) {
617 print $code->alias, ": ", $code->protocol, "\n";
618 }
619  
620 # Export a code as a LIRC remote definition
621 my $lirc = $converter->export_code($code, 'LIRC',
622 name => 'My Remote');
623  
624 =head1 DESCRIPTION
625  
626 C<Protocol::IR::Format::LIRC> imports and exports LIRC remote definition
627 files (C<.lircd.conf>). The LIRC format is the de facto standard for
628 sharing IR remote control definitions across the LIRC ecosystem
629 (L<https://www.lirc.org/>).
630  
631 =over 4
632  
633 =item Example C<.lircd.conf> (NEC transmission with timing template)
634  
635 begin remote
636  
637 name Samsung_TV
638 bits 16
639 flags SPACE_ENC|CONST_LENGTH
640 eps 30
641 aeps 100
642  
643 header 4500 4500
644 one 550 1650
645 zero 550 550
646 gap 107000
647  
648 begin codes
649 KEY_POWER 0xE0E040BF
650 end codes
651  
652 end remote
653  
654 =back
655  
656 =head2 Import modes
657  
658 C<decode> handles two LIRC encoding modes:
659  
660 =over 4
661  
662 =item * Protocol-based (C<SPACE_ENC>): The remote header defines timing
663 parameters (C<header>, C<one>, C<zero>, C<pre_data>, C<post_data>), and
664 the C<codes> section maps button names to hex values. The module infers
665 the protocol from the timing parameters and decodes through the
666 registered protocol handler.
667  
668 =item * Raw codes (C<RAW_CODES>): The C<raw_codes> section contains
669 explicit pulse/space microsecond values for each button. These are
670 decoded through the registered protocol timing decoders, with raw
671 timings preserved for lossless re-export.
672  
673 =back
674  
675 =head2 Export
676  
677 C<export> produces a LIRC remote definition with timing parameters
678 matched to the code's protocol. Known protocols (NEC, Samsung, JVC)
679 get protocol-based output with correct timing templates. Unknown
680 protocols fall back to raw codes derived from the stored timings or
681 a Pronto hex round-trip.
682  
683 Options:
684  
685 =over 4
686  
687 =item * C<name> -- remote name (default: C<'exported'>)
688  
689 =back
690  
691 =head1 METHODS
692  
693 =head2 decode
694  
695 my $codes = $class->decode($input, $registry);
696  
697 Parses a LIRC remote definition and returns an arrayref of
698 L<Protocol::IR::Code> objects.
699  
700 =head2 export
701  
702 my $lirc = $class->export($codes, $registry, %opts);
703  
704 Serializes one or more L<Protocol::IR::Code> objects into a LIRC remote
705 definition string.
706  
707 =head1 SUPPORT
708  
709 Source code: L<https://github.com/bwarden/perl-protocol-ir>
710  
711 Bug reports and feature requests: L<https://github.com/bwarden/perl-protocol-ir/issues>
712  
713 =head1 AUTHOR
714  
715 Brett T. Warden <bwarden@cpan.org>
716  
717 =head1 COPYRIGHT AND LICENSE
718  
719 Copyright (c) 2026 Brett T. Warden
720  
721 This library is free software; you can redistribute it and/or modify it
722 under the terms of the GNU General Public License as published by the
723 Free Software Foundation; either version 2 of the License, or (at your
724 option) any later version.
725  
726 =cut
↑ Summary blib/lib/Protocol/IR/Proto/Panasonic.pm »