blib/lib/Protocol/IR/Converter.pm

filter: Statementstmt 100% 194 / 194 Branchbran 70.8% 17 / 24 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 46 / 46 Podpod 83.3% 10 / 12 totaltotal 96.7% 267 / 276 CC 13 SCAR 5.3
CC
13
Cov
97%
cross_protocol
5.1
_import_byte_order
3.0
import_code
3.0
CRAP 1.7
« blib/lib/Protocol/IR/Proto/Pioneer.pm ↑ Summary blib/lib/Protocol/IR/Proto/NEC2.pm »
1 package Protocol::IR::Converter;
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 55 ▶use Protocol::IR::Proto::Aiwa;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 55 ▶use Protocol::IR::Proto::GICable;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 55 ▶use Protocol::IR::Proto::Mitsubishi;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 55 ▶use Protocol::IR::Proto::Dish;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 55 ▶use Protocol::IR::Proto::Pioneer;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 55 ▶use Protocol::IR::Proto::Denon;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 55 ▶use Protocol::IR::Proto::Sharp;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 55 ▶use Protocol::IR::Proto::JVC;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 55 ▶use Protocol::IR::Proto::JVC48;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 55 ▶use Protocol::IR::Proto::MWM;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 55 ▶use Protocol::IR::Proto::NEC;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 55 ▶use Protocol::IR::Proto::NEC2;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 55 ▶use Protocol::IR::Proto::NEC48;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 55 ▶use Protocol::IR::Proto::NEC482;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 55 ▶use Protocol::IR::Proto::NECX1;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 55 ▶use Protocol::IR::Proto::NECX2;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 55 ▶use Protocol::IR::Proto::Panasonic;
Subroutine BEGIN: called CC=1 SCAR=0.0
25 55 ▶use Protocol::IR::Proto::RC5;
Subroutine BEGIN: called CC=1 SCAR=0.0
26 55 ▶use Protocol::IR::Proto::RC6;
Subroutine BEGIN: called CC=1 SCAR=0.0
27 55 ▶use Protocol::IR::Proto::SAMSUNG;
Subroutine BEGIN: called CC=1 SCAR=0.0
28 55 ▶use Protocol::IR::Proto::SAMSUNG20;
Subroutine BEGIN: called CC=1 SCAR=0.0
29 55 ▶use Protocol::IR::Proto::SAMSUNG36;
Subroutine BEGIN: called CC=1 SCAR=0.0
30 55 ▶use Protocol::IR::Proto::Sony;
Subroutine BEGIN: called CC=1 SCAR=0.0
31 55 ▶use Protocol::IR::Format::Pronto;
Subroutine BEGIN: called CC=1 SCAR=0.0
32 55 ▶use Protocol::IR::Format::CSV;
Subroutine BEGIN: called CC=1 SCAR=0.0
33 55 ▶use Protocol::IR::Format::Wig;
Subroutine BEGIN: called CC=1 SCAR=0.0
34 55 ▶use Protocol::IR::Format::JSON;
Subroutine BEGIN: called CC=1 SCAR=0.0
35 55 ▶use Protocol::IR::Format::Tasmota;
Subroutine BEGIN: called CC=1 SCAR=0.0
36 55 ▶use Protocol::IR::Format::Mode2;
Subroutine BEGIN: called CC=1 SCAR=0.0
37 55 ▶use Protocol::IR::Format::LIRC;
Subroutine BEGIN: called CC=1 SCAR=0.0
38  
39 50 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod documented
40 50 my ($class) = @_;
41 50 my $self = bless {
42 protocols => {},
43 protocol_order => [],
44 formats => {},
45 }, $class;
46  
47 # Protocols, registered in the order Pronto/Tasmota/wig decoding tries
48 # them. Handlers whose timing signatures overlap must be ordered so the
49 # most likely interpretation wins, because the timing decoders cannot
50 # tell them apart:
51 #
52 # NEC before NEC2 - a single NEC2 frame is timing-identical to a NEC1
53 # frame (the "2" variants only differ in repeat structure), so NEC,
54 # the far more common framing, absorbs NEC2 frames on a timing decode.
55 # NEC2 stays registered so codes imported by name (IRDB CSV) keep the
56 # NEC2 identity.
57 #
58 # 48-NEC1/48-NEC2 after NEC - the 48-bit frames share NEC's 9000/4500
59 # us header, but fail the 32-bit stop check (pair 33 is a data bit, so
60 # its space never reaches the 3000 µs threshold), so they only reach
61 # the 48-bit decoders. 48-NEC1 before 48-NEC2: single frames are
62 # timing-identical, so the base variant absorbs the timing decode.
63 #
64 # JVC before JVC-48 - JVC-48's 3456/1728 µs header is rejected by the
65 # 32-bit JVC header check (7000-9800 µs), so it only reaches the
66 # 48-bit decoder.
67 #
68 # SAMSUNG before NECX1/NECX2 - NECx frames use Samsung's 4500/4500 µs
69 # half header, so the two families share a header and bit timing. A
70 # single-frame NECx1/NECx2 capture is therefore only distinguished from
71 # SAMSUNG by the Samsung byte structure (address repeated, command
72 # followed by its complement) that SAMSUNG's strict decoder enforces
73 # and NECx frames lack (they carry a subaddress byte). The NECx decoder
74 # that follows is then the one that matches, so NECx frames keep their
75 # NECx1/NECX2 identity on a timing decode. (IRremoteESP8266 has no NECx
76 # decoder and reports the same capture as UNKNOWN; ours names the
77 # protocol it recognizes.)
78 #
79 # SAMSUNG before SAMSUNG36 - SAMSUNG's stop check (a >= 3000 µs space)
80 # rejects a 36-bit frame's mid-frame pairs, so SAMSUNG36 is only
81 # reached when its 39-pair framing matches.
82 #
83 # SAMSUNG20 last - its 22-pair frames cannot match the 34-pair
84 # minimums of SAMSUNG/NEC or the 39-pair minimum of SAMSUNG36.
85 #
86 # MWM last - MWM frames have no header at all (a 417 µs start mark),
87 # so every other decoder's header check rejects them first, and MWM's
88 # strict tick matching rejects any header-bearing frame in turn.
89 # DECODE ORDER MATTERS: protocols are tried in registration order;
90 # a later protocol whose timing is a subset of an earlier one never matches.
91 # Register more specific / longer frames first (Pioneer before NEC,
92 # Mitsubishi before Denon, Denon before Sharp). Derivative protocols that
93 # share a timing signature (NECX1/NECX2 -> SAMSUNG) rely on this ordering.
94 50 $self->register_protocol('PIONEER', 'Protocol::IR::Proto::Pioneer');
95 50 $self->register_protocol('NEC', 'Protocol::IR::Proto::NEC');
96 50 $self->register_protocol('NEC2', 'Protocol::IR::Proto::NEC2');
97 50 $self->register_protocol('48-NEC1', 'Protocol::IR::Proto::NEC48');
98 50 $self->register_protocol('48-NEC2', 'Protocol::IR::Proto::NEC482');
99 50 $self->register_protocol('JVC', 'Protocol::IR::Proto::JVC');
100 50 $self->register_protocol('JVC-48', 'Protocol::IR::Proto::JVC48');
101 50 $self->register_protocol('PANASONIC', 'Protocol::IR::Proto::Panasonic');
102 50 $self->register_protocol('SONY20', 'Protocol::IR::Proto::Sony20');
103 50 $self->register_protocol('SONY15', 'Protocol::IR::Proto::Sony15');
104 50 $self->register_protocol('SONY12', 'Protocol::IR::Proto::Sony12');
105 50 $self->register_protocol('SAMSUNG', 'Protocol::IR::Proto::SAMSUNG');
106 50 $self->register_protocol('SAMSUNG36', 'Protocol::IR::Proto::SAMSUNG36');
107 50 $self->register_protocol('SAMSUNG20', 'Protocol::IR::Proto::SAMSUNG20');
108 50 $self->register_protocol('NECX1', 'Protocol::IR::Proto::NECX1');
109 50 $self->register_protocol('NECX2', 'Protocol::IR::Proto::NECX2');
110 50 $self->register_protocol('MWM', 'Protocol::IR::Proto::MWM');
111 50 $self->register_protocol('RC5', 'Protocol::IR::Proto::RC5');
112 50 $self->register_protocol('RC6', 'Protocol::IR::Proto::RC6');
113 50 $self->register_protocol('MITSUBISHI', 'Protocol::IR::Proto::Mitsubishi');
114 50 $self->register_protocol('DENON', 'Protocol::IR::Proto::Denon');
115 50 $self->register_protocol('SHARP', 'Protocol::IR::Proto::Sharp');
116 50 $self->register_protocol('AIWA', 'Protocol::IR::Proto::Aiwa');
117 50 $self->register_protocol('G.I.CABLE', 'Protocol::IR::Proto::GICable');
118 50 $self->register_protocol('DISH_NETWORK', 'Protocol::IR::Proto::Dish');
119 50 $self->register_protocol('DENON-K', 'Protocol::IR::Proto::DenonK');
120 50 $self->register_protocol('DENON{1}','Protocol::IR::Proto::Denon1');
121  
122 # Formats
123 50 $self->register_format('Pronto', 'Protocol::IR::Format::Pronto');
124 50 $self->register_format('CSV', 'Protocol::IR::Format::CSV');
125 50 $self->register_format('wig', 'Protocol::IR::Format::Wig');
126 50 $self->register_format('JSON', 'Protocol::IR::Format::JSON');
127 50 $self->register_format('Tasmota','Protocol::IR::Format::Tasmota');
128 50 $self->register_format('Mode2', 'Protocol::IR::Format::Mode2');
129 50 $self->register_format('LIRC', 'Protocol::IR::Format::LIRC');
130  
131 50 return $self;
132 }
133  
134 1351 ▶sub register_protocol {
Subroutine register_protocol: called CC=2 SCAR=7.0
Pod documented
135 1351 my ($self, $name, $class) = @_;
136 1351 my $key = uc $name;
137 1351 $self->{protocols}{$key} = $class;
138 1351 push @{$self->{protocol_order}}, $key
139 1351 ▶ unless grep { $_ eq $key } @{$self->{protocol_order}};
Branch: 1 branch
BranchTrueFalseTotal
unless grep {$_ eq $key;} @{$$self{"protocol_order"};} 1351 0 1351
140 }
141  
142 350 ▶sub register_format {
Subroutine register_format: called CC=1 SCAR=0.0
Pod documented
143 350 my ($self, $name, $class) = @_;
144 350 $self->{formats}{uc $name} = $class;
145 }
146  
147 1185 ▶sub get_protocol {
Subroutine get_protocol: called CC=1 SCAR=0.0
Pod documented
148 1185 my ($self, $name) = @_;
149 1185 return $self->{protocols}{uc $name};
150 }
151  
152 # Registered protocols in deterministic registration order.
153 # Pronto decoding tries protocols in this order, so handlers whose timing
154 # signatures overlap must be registered most-specific-first.
155 940 ▶sub get_protocols {
Subroutine get_protocols: called CC=1 SCAR=0.0
Pod documented
156 940 my ($self) = @_;
157 940 return map { $self->{protocols}{$_} } @{$self->{protocol_order}};
158 }
159  
160 # Cross-protocol mappings: protocols with identical timing and compatible
161 # frame layouts but different field naming conventions.
162 #
163 # NECX2 <-> SAMSUNG: same 4500/4500 µs header, 560/1680 µs bit timing,
164 # 32 bits, per-byte LSB-first. Samsung enforces addr,addr,cmd,~cmd
165 # while NECX2 allows addr,subaddr,cmd,~cmd. The Samsung address is the
166 # bit-reversal of the NECX2 device byte, and likewise for the command.
167 55 my %CROSS_PROTOCOL = (
168 SAMSUNG => 'NECX2',
169 NECX2 => 'SAMSUNG',
170 );
171  
172 7 ▶sub cross_protocol {
Subroutine cross_protocol: called CC=5 SCAR=16.3
Pod documented
173 7 my ($self, $code) = @_;
174 7 my $proto = uc($code->protocol);
175 7 my $target = $CROSS_PROTOCOL{$proto};
176 7 ▶ return [] unless $target;
Branch: 1 branch
BranchTrueFalseTotal
unless $target 2 5 7
177  
178 5 my $target_class = $self->get_protocol($target);
179 5 ▶ return [] unless $target_class;
Branch: 1 branch
BranchTrueFalseTotal
unless $target_class 0 5 5
180  
181 # Build the equivalent code in the target protocol.
182 # The source protocol handler provides an as_<target>_params method.
183 5 my $source_class = $self->get_protocol($proto);
184 5 my $method = "as_\L${target}_params";
185 5 ▶ my $params = $source_class->can($method)
Branch: 1 branch
BranchTrueFalseTotal
$source_class->can($method) ? : 5 0 5
186 ? $source_class->$method($code)
187 : undef;
188 5 ▶ return [] unless $params;
Branch: 1 branch
BranchTrueFalseTotal
unless $params 0 5 5
189  
190 5 my $equiv = $target_class->decode_params(%$params);
191 5 return [$equiv];
192 }
193  
194 356 ▶sub import_code {
Subroutine import_code: called CC=3 SCAR=11.0
Pod documented
195 356 my ($self, $protocol_name, $input) = @_;
196 356 my $proto_class = $self->get_protocol($protocol_name);
197 356 ▶ die "Unsupported protocol: $protocol_name\n" unless $proto_class;
Branch: 1 branch
BranchTrueFalseTotal
unless $proto_class 2 354 356
198  
199 354 ▶ if (ref $input eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $input eq 'HASH') { } 328 26 354
200 328 return $proto_class->decode_params(%$input);
201 } else {
202 26 return $proto_class->decode_raw($input);
203 }
204 }
205  
206 # Import a transmitted frame value in a named byte order, mirroring the JS
207 # importLsb/importMsb and the LIRC/Tasmota routing. When $lsb is true the
208 # value is the accumulated form (Tasmota DataLSB); when false it is the
209 # display form (Tasmota Data). Protocols with a byte-order distinction
210 # translate so the decoded address/subaddress/command always matches the
211 # transmitted bytes; protocols without one decode the value as-is.
212 12 ▶sub _import_byte_order {
Subroutine _import_byte_order: called CC=3 SCAR=11.0
213 12 my ($self, $protocol_name, $input, $lsb) = @_;
214 12 my $proto_class = $self->get_protocol($protocol_name);
215 12 ▶ die "Unsupported protocol: $protocol_name\n" unless $proto_class;
Branch: 1 branch
BranchTrueFalseTotal
unless $proto_class 0 12 12
216  
217 12 ▶ if ($proto_class->can('decode_byte_order')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($proto_class->can("decode_byte_order")) 8 4 12
218 8 return $proto_class->decode_byte_order($input, $lsb);
219 }
220 4 return $proto_class->decode_raw($input);
221 }
222  
223 # Import from the accumulated value (Tasmota DataLSB).
224 2 ▶sub import_lsb {
Subroutine import_lsb: called CC=1 SCAR=0.0
Pod undocumented
225 2 my ($self, $protocol_name, $input) = @_;
226 2 return $self->_import_byte_order($protocol_name, $input, 1);
227 }
228  
229 # Import from the display value (Tasmota Data).
230 10 ▶sub import_msb {
Subroutine import_msb: called CC=1 SCAR=0.0
Pod undocumented
231 10 my ($self, $protocol_name, $input) = @_;
232 10 return $self->_import_byte_order($protocol_name, $input, 0);
233 }
234  
235 401 ▶sub export_code {
Subroutine export_code: called CC=2 SCAR=6.9
Pod documented
236 401 my ($self, $ir_code, $format_name, %opts) = @_;
237 401 my $format_class = $self->{formats}{uc $format_name};
238 401 ▶ die "Unsupported format: $format_name\n" unless $format_class;
Branch: 1 branch
BranchTrueFalseTotal
unless $format_class 1 400 401
239  
240 400 return $format_class->export($ir_code, $self, %opts);
241 }
242  
243 # Export a set of Protocol::IR::Code objects to a container format (e.g. wig).
244 39 ▶sub export_codes {
Subroutine export_codes: called CC=2 SCAR=7.0
Pod documented
245 39 my ($self, $format_name, $codes, %opts) = @_;
246 39 my $format_class = $self->{formats}{uc $format_name};
247 39 ▶ die "Unsupported format: $format_name\n" unless $format_class;
Branch: 1 branch
BranchTrueFalseTotal
unless $format_class 0 39 39
248  
249 39 return $format_class->export($codes, $self, %opts);
250 }
251  
252 734 ▶sub import_format {
Subroutine import_format: called CC=2 SCAR=7.0
Pod documented
253 734 my ($self, $format_name, $input) = @_;
254 734 my $format_class = $self->{formats}{uc $format_name};
255 734 ▶ die "Unsupported format: $format_name\n" unless $format_class;
Branch: 1 branch
BranchTrueFalseTotal
unless $format_class 0 734 734
256  
257 734 return $format_class->decode($input, $self);
258 }
259  
260 55 1;
261  
262 =encoding utf8
263  
264 =head1 NAME
265  
266 Protocol::IR::Converter - Registry and manager for IR code protocols and formats
267  
268 =head1 VERSION
269  
270 version 1.5
271  
272 =head1 SYNOPSIS
273  
274 use Protocol::IR::Converter;
275 use Data::Dumper;
276  
277 my $converter = Protocol::IR::Converter->new();
278  
279 # Import a raw 32-bit NEC hex value
280 my $code = $converter->import_code('NEC', '0x10EF00FF');
281  
282 # Tasmota IRSend JSON payload
283 print Dumper($code->to_irsend);
284 # { Protocol => 'NEC', Bits => 32, Data => '0x10EF00FF' }
285  
286 # Export to Pronto Hex
287 my $pronto = $converter->export_code($code, 'Pronto');
288  
289 # Decode Pronto Hex back into an Protocol::IR::Code
290 my $decoded = $converter->import_format('Pronto',
291 '0000 006D 0022 0000 0157 00AC ...');
292  
293 # Import an IRDB-style CSV file
294 my $codes = $converter->import_format('CSV',
295 "functionname,protocol,device,subdevice,function\nKEY_POWER,NEC1,4,0,8");
296  
297 # Export a set of Protocol::IR::Code objects to a HAIR wig file
298 my $wig = $converter->export_codes('wig', $codes,
299 name => 'Tigersecu DVR',
300 brand => 'Tigersecu',
301 kind => 'dvr',
302 );
303  
304 # Import and re-export Tasmota RawData captures
305 my $capture = $converter->import_format('Tasmota',
306 "+9185-4490+650-500+655dE-1630C-505+630-525Ed...")->[0];
307 my $irsend = $converter->export_code($capture, 'Tasmota',
308 style => 'comma', frequency => 38000);
309  
310 =head1 DESCRIPTION
311  
312 C<Protocol::IR::Converter> is the heart of the Protocol::IR::Code distribution: a registry that
313 knows which protocol and format modules are available and routes every
314 import and export through them. All codes are normalized into
315 L<Protocol::IR::Code> objects, so a signal decoded from one format can be encoded into
316 any other.
317  
318 The following protocols and formats are registered by default:
319  
320 =over 4
321  
322 =item * Protocols: C<NEC> (32-bit), C<NEC2> (32-bit, whole-frame repeat),
323 C<48-NEC1>/C<48-NEC2> (48-bit), C<JVC> (16-bit), C<JVC-48> (48-bit Kaseikyo),
324 C<SAMSUNG> (32-bit), C<SAMSUNG20> (20-bit), C<SAMSUNG36> (36-bit),
325 C<NECX1>/C<NECX2> (extended NEC, half header), C<MWM> (Disney "Made With
326 Magic", 24-144 bit serial)
327  
328 =item * Formats: C<Pronto>, C<Tasmota>, C<wig>, C<CSV>, C<Mode2>,
329 C<LIRC>, C<JSON> (import only)
330  
331 =back
332  
333 C<Pronto>, C<Tasmota>, C<Mode2>, and C<LIRC> are timing-based formats;
334 L<Protocol::IR::Format::Pronto>, L<Protocol::IR::Format::Tasmota>,
335 L<Protocol::IR::Format::Mode2>, and L<Protocol::IR::Format::LIRC> document
336 their exact behavior.
337 L<Protocol::IR::Format::Wig> implements the HAIR "wig"
338 JSON format. L<Protocol::IR::Format::CSV> imports IRDB-style button listings.
339 L<Protocol::IR::Format::JSON> imports a proprietary JSON IR database dump.
340  
341 =head1 Decoding versus generating timings
342  
343 For the timing-based formats (Tasmota C<RawData>, Pronto Hex, and wig), the
344 goal of this library is to I<generate> correct timings from fully decoded
345 C<Protocol::IR::Code> objects. That direction is authoritative: given a correctly
346 decoded code, the emitted timings are exact.
347  
348 The reverse direction -- decoding raw timings back into commands -- is
349 best-effort. The protocol decoders match against simple header and
350 mark/space thresholds and are not designed to handle the signal variance,
351 jitter, and corruption that a real decoding library (such as
352 C<IRremoteESP8266>) accounts for. A capture may therefore fail to be
353 recognized, or in rare ambiguous cases be misidentified. Conversions
354 between fully decoded representations (raw hex, parameter hashes, IRDB CSV,
355 wig) are exact and reliable.
356  
357 Conversions between the timing formats themselves (Tasmota C<RawData>,
358 Pronto Hex, and wig) are exact and lossless: a signal decoded from any timed
359 input keeps its quantized C<timings> and, for Pronto Hex, the original hex
360 verbatim (see L<Protocol::IR::Code>), so a re-export reproduces the same
361 capture byte-for-byte rather than re-quantizing through a protocol encoder.
362 For recognized protocols, wig files produced by HAIR are assumed to carry
363 timings that have already been quantized and cleaned up, so they should
364 decode correctly -- but, as with any raw timing input, this is not
365 guaranteed.
366  
367 =head1 INSTALLATION
368  
369 perl Makefile.PL
370 make
371 make test
372 make install
373  
374 Or with a cpan client:
375  
376 cpanm Protocol::IR::Code
377  
378 The modules have no runtime dependencies beyond core Perl. C<JSON::PP>
379 (core since Perl 5.14) is declared as a prerequisite for older versions.
380  
381 Two command-line tools are installed with the distribution:
382  
383 =over 4
384  
385 =item * C<ir-irdb2wig> -- convert an IRDB CSV file (local, fetched from a
386 URL, or downloaded from the IRDB repository by device path) to a HAIR wig
387 JSON file.
388  
389 =item * C<ir-convert> -- general converter between the supported formats
390 (CSV, wig, Pronto, Tasmota, LIRC, JSON).
391  
392 =back
393  
394 Both read the Protocol::IR::Code modules from your normal Perl installation, so they
395 work from any directory once the distribution is installed.
396  
397 =head1 METHODS
398  
399 =head2 new
400  
401 my $converter = Protocol::IR::Converter->new;
402  
403 Creates a converter, registering the bundled protocols (NEC, NEC2, 48-NEC1,
404 48-NEC2, JVC, JVC-48, SAMSUNG, SAMSUNG20, SAMSUNG36, NECX1, NECX2, MWM) and
405 formats (Pronto, CSV, wig, Tasmota, Mode2, LIRC, JSON).
406  
407 =head2 import_code
408  
409 my $code = $converter->import_code('NEC', '0x10EF00FF');
410 my $code = $converter->import_code('JVC', { address => 3, command => 12 });
411  
412 Builds an L<Protocol::IR::Code> for the named protocol from either a raw value
413 (string or number) or a hash of parameters. Dies on an unregistered
414 protocol.
415  
416 =head2 export_code
417  
418 my $pronto = $converter->export_code($code, 'Pronto');
419 my $irsend = $converter->export_code($code, 'Tasmota',
420 style => 'comma', frequency => 38000);
421  
422 Serializes a single L<Protocol::IR::Code> object into the named format, passing any
423 extra options through to the format's C<export> method.
424  
425 =head2 export_codes
426  
427 my $wig = $converter->export_codes('wig', \@codes, name => 'Remote');
428  
429 Serializes one or more L<Protocol::IR::Code> objects into a container format such as
430 wig. A single object is wrapped in an arrayref automatically.
431  
432 =head2 import_format
433  
434 my $codes = $converter->import_format('wig', 'remote.wig.json');
435 my $code = $converter->import_format('Pronto', $pronto_str);
436  
437 Parses external input in the named format (file path, raw string, JSON,
438 etc.) into a list of L<Protocol::IR::Code> objects. Returns an arrayref. Dies on an
439 unregistered format.
440  
441 =head2 register_protocol
442  
443 $converter->register_protocol('RC5', 'Protocol::IR::RC5');
444 $self->register_protocol('RC6', 'Protocol::IR::Proto::RC6');
445 $self->register_protocol('MITSUBISHI', 'Protocol::IR::Proto::Mitsubishi');
446 $self->register_protocol('DENON', 'Protocol::IR::Proto::Denon');
447 $self->register_protocol('AIWA', 'Protocol::IR::Proto::Aiwa');
448 $self->register_protocol('G.I.CABLE', 'Protocol::IR::Proto::GICable');
449 $self->register_protocol('DISH_NETWORK', 'Protocol::IR::Proto::Dish');
450 $self->register_protocol('DENON-K', 'Protocol::IR::Proto::DenonK');
451 $self->register_protocol('DENON{1}','Protocol::IR::Proto::Denon1');
452  
453 Registers a protocol handler class under a name. See L</"EXTENDING THE
454 FRAMEWORK">.
455  
456 =head2 register_format
457  
458 $converter->register_format('JSON', 'Protocol::IR::Format::JSON');
459  
460 Registers a format handler class under a name. See L</"EXTENDING THE
461 FRAMEWORK">.
462  
463 =head2 get_protocol
464  
465 my $class = $converter->get_protocol('NEC');
466  
467 Returns the handler class registered for a protocol name, or C<undef>.
468  
469 =head2 get_protocols
470  
471 my @classes = $converter->get_protocols;
472  
473 Returns the registered protocol handler classes in deterministic
474 registration order. Pronto and Tasmota decoding try protocols in this
475 order, so handlers whose timing signatures overlap must be registered
476 most-specific-first.
477  
478 =head2 cross_protocol
479  
480 my $equivs = $converter->cross_protocol($code);
481  
482 Converts a code to its equivalent in a cross-protocol partner. Currently
483 supports:
484  
485 =over 4
486  
487 =item SAMSUNG <-> NECX2
488  
489 Both protocols share identical 4500/4500 µs half-header timing and 32-bit
490 LSB-first encoding, but name the fields differently. The Samsung address
491 is the bit-reversal of the NECX2 device byte, and likewise for the
492 command/function byte. See L<Protocol::IR::Proto::SAMSUNG/CROSS-PROTOCOL MAPPING>
493 for details.
494  
495 =back
496  
497 Returns an arrayref of L<Protocol::IR::Code> objects (zero or one entries)
498 in the equivalent protocol. Returns C<[]> when the protocol has no
499 cross-protocol partner.
500  
501 Example:
502  
503 # A Samsung TV POWER capture (address=0xE0, command=0x40)
504 # converts to NECX2 (device=7, subdevice=7, function=2)
505 my $necx2_equivs = $converter->cross_protocol($samsung_code);
506 my $necx2_code = $necx2_equivs->[0];
507  
508 # A NECX2 code from an IRDB CSV converts to SAMSUNG for capture matching
509 my $sam_equivs = $converter->cross_protocol($necx2_code);
510  
511 =head1 EXTENDING THE FRAMEWORK
512  
513 =head2 Adding a new protocol (Protocol::IR::NAME)
514  
515 Create a package under C<lib/Protocol/IR/> implementing these methods:
516  
517 =over 4
518  
519 =item * C<decode_raw($class, $raw_int)> -- takes a raw packed integer/hex
520 value and returns an L<Protocol::IR::Code> object.
521  
522 =item * C<decode_params($class, %args)> -- accepts discrete parameters
523 (C<device>, C<subdevice>, C<command>) and returns an L<Protocol::IR::Code> object.
524  
525 =item * C<decode_timing($class, \@burst_pairs_us)> -- accepts an arrayref
526 of microsecond C<[mark_us, space_us]> pairs, matches the protocol's timing
527 signature (headers, mark/space thresholds), and returns an L<Protocol::IR::Code>
528 object if valid, or C<undef> if non-matching.
529  
530 =item * C<to_pronto($class, $ir_code)> -- converts an L<Protocol::IR::Code> object
531 into a Pronto Hex string.
532  
533 =back
534  
535 Register it in C<Protocol::IR::Converter::new()>:
536  
537 $self->register_protocol('RC5', 'Protocol::IR::RC5');
538 $self->register_protocol('RC6', 'Protocol::IR::Proto::RC6');
539 $self->register_protocol('MITSUBISHI', 'Protocol::IR::Proto::Mitsubishi');
540 $self->register_protocol('DENON', 'Protocol::IR::Proto::Denon');
541 $self->register_protocol('AIWA', 'Protocol::IR::Proto::Aiwa');
542 $self->register_protocol('G.I.CABLE', 'Protocol::IR::Proto::GICable');
543 $self->register_protocol('DISH_NETWORK', 'Protocol::IR::Proto::Dish');
544 $self->register_protocol('DENON-K', 'Protocol::IR::Proto::DenonK');
545 $self->register_protocol('DENON{1}','Protocol::IR::Proto::Denon1');
546  
547 =head2 Adding a new format (Protocol::IR::Format::NAME)
548  
549 Create a package under C<lib/Protocol/IR/Format/> implementing these methods:
550  
551 =over 4
552  
553 =item * C<export($class, $ir_code, $registry)> -- serializes one or more
554 L<Protocol::IR::Code> objects into the target format.
555  
556 =item * C<decode($class, $input_data, $registry)> -- parses external input
557 (file path, raw string, JSON, etc.) into one or more L<Protocol::IR::Code> objects,
558 using C<< $registry->import_code(...) >> to build them.
559  
560 =back
561  
562 Register it in C<Protocol::IR::Converter::new()>:
563  
564 $self->register_format('JSON', 'Protocol::IR::Format::JSON');
565  
566 =head1 SUPPORT
567  
568 Source code: L<https://github.com/bwarden/perl-protocol-ir>
569  
570 Bug reports and feature requests: L<https://github.com/bwarden/perl-protocol-ir/issues>
571  
572 =head1 AUTHOR
573  
574 Brett T. Warden <bwarden@cpan.org>
575  
576 =head1 COPYRIGHT AND LICENSE
577  
578 Copyright (c) 2026 Brett T. Warden
579  
580 This library is free software; you can redistribute it and/or modify it
581 under the terms of the GNU General Public License as published by the
582 Free Software Foundation; either version 2 of the License, or (at your
583 option) any later version.
« blib/lib/Protocol/IR/Proto/Pioneer.pm ↑ Summary blib/lib/Protocol/IR/Proto/NEC2.pm »