blib/lib/Protocol/IR/Code.pm

filter: Statementstmt 100% 56 / 56 Branchbran 85.2% 29 / 34 Conditioncond 92.5% 25 / 27 MC/DCmcdc 85.7% 12 / 14 Subroutinesub 100% 18 / 18 Podpod 93.3% 14 / 15 totaltotal 93.9% 154 / 164 CC 32 SCAR 16.8
CC
32
Cov
94%
new
13.0
_data_hex
6.0
address
2.1
CRAP 5.4
« bin/ir-convert ↑ Summary bin/ir-tasmota2wig »
1 package Protocol::IR::Code;
2 56 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 56 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 56 our $VERSION = '1.5';
6  
7 1881 ▶sub new {
Subroutine new: called CC=13 SCAR=25.7
Pod undocumented
8 1881 my ($class, %args) = @_;
9 return bless {
10 protocol => $args{protocol} // 'UNKNOWN',
11 bits => $args{bits} // 0,
12 address => $args{address} // 0,
13 subaddress => $args{subaddress} // -1,
14 command => $args{command} // 0,
15 data => $args{data} // undef,
16 alias => $args{alias} // '',
17 ditto_count => $args{ditto_count} // 0,
18 send_count => $args{send_count} // 0,
19 bypass_protocol => $args{bypass_protocol} // 0,
20 timings => $args{timings} // undef,
21 pronto => $args{pronto} // undef,
22 1881 ▶ }, $class;
Condition95%23/24
$args{"protocol"} // "UNKNOWN"
l!l
18783
$args{"bits"} // 0
l!l
1745136
$args{"address"} // 0
l!l
1597284
$args{"subaddress"} // -1
l!l
1595286
$args{"command"} // 0
l!l
1597284
$args{"data"} // undef
l!l
1744137
$args{"alias"} // ""
l!l
51876
$args{"ditto_count"} // 0
l!l
11880
$args{"send_count"} // 0
l!l
01881
$args{"bypass_protocol"} // 0
l!l
101871
$args{"timings"} // undef
l!l
101871
$args{"pronto"} // undef
l!l
91872
MC/DC100%1/1
$args{"protocol"} // "UNKNOWN"
$args{"protocol"}
MC/DC100%1/1
$args{"bits"} // 0
$args{"bits"}
MC/DC100%1/1
$args{"address"} // 0
$args{"address"}
MC/DC100%1/1
$args{"subaddress"} // -1
$args{"subaddress"}
MC/DC100%1/1
$args{"command"} // 0
$args{"command"}
MC/DC100%1/1
$args{"data"} // undef
$args{"data"}
MC/DC100%1/1
$args{"alias"} // ""
$args{"alias"}
MC/DC100%1/1
$args{"ditto_count"} // 0
$args{"ditto_count"}
MC/DC0%0/1
$args{"send_count"} // 0
$args{"send_count"}
MC/DC100%1/1
$args{"bypass_protocol"} // 0
$args{"bypass_protocol"}
MC/DC100%1/1
$args{"timings"} // undef
$args{"timings"}
MC/DC100%1/1
$args{"pronto"} // undef
$args{"pronto"}
Truth table100%2/2
A
A= $args{"protocol"}
Aresult
11
00
Truth table100%2/2
A
A= $args{"bits"}
Aresult
11
00
Truth table100%2/2
A
A= $args{"address"}
Aresult
11
00
Truth table100%2/2
A
A= $args{"subaddress"}
Aresult
11
00
Truth table100%2/2
A
A= $args{"command"}
Aresult
11
00
Truth table100%2/2
A
A= $args{"data"}
Aresult
11
00
Truth table100%2/2
A
A= $args{"alias"}
Aresult
11
00
Truth table100%2/2
A
A= $args{"ditto_count"}
Aresult
11
00
Truth table50%1/2
A
A= $args{"send_count"}
Aresult
11
00
Truth table100%2/2
A
A= $args{"bypass_protocol"}
Aresult
11
00
Truth table100%2/2
A
A= $args{"timings"}
Aresult
11
00
Truth table100%2/2
A
A= $args{"pronto"}
Aresult
11
00
23 }
24  
25 # Accessors
26 2400 ▶sub protocol { $_[0]->{protocol} = $_[1] if @_ > 1; $_[0]->{protocol} }
Subroutine protocol: called CC=2 SCAR=6.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
if @_ > 1 1 2399 2400
27 182 ▶sub bits { $_[0]->{bits} = $_[1] if @_ > 1; $_[0]->{bits} }
Subroutine bits: called CC=2 SCAR=6.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
if @_ > 1 1 181 182
28 1050 ▶sub address { $_[0]->{address} = $_[1] if @_ > 1; $_[0]->{address} }
Subroutine address: called CC=2 SCAR=7.2
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
if @_ > 1 0 1050 1050
29 714 ▶sub subaddress { $_[0]->{subaddress} = $_[1] if @_ > 1; $_[0]->{subaddress} }
Subroutine subaddress: called CC=2 SCAR=7.2
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
if @_ > 1 0 714 714
30 1055 ▶sub command { $_[0]->{command} = $_[1] if @_ > 1; $_[0]->{command} }
Subroutine command: called CC=2 SCAR=7.2
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
if @_ > 1 0 1055 1055
31 2077 ▶sub data { $_[0]->{data} = $_[1] if @_ > 1; $_[0]->{data} }
Subroutine data: called CC=2 SCAR=7.2
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
if @_ > 1 0 2077 2077
32 1833 ▶sub alias { $_[0]->{alias} = $_[1] if @_ > 1; $_[0]->{alias} }
Subroutine alias: called CC=2 SCAR=6.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
if @_ > 1 995 838 1833
33 127 ▶sub ditto_count { $_[0]->{ditto_count} = $_[1] if @_ > 1; $_[0]->{ditto_count} }
Subroutine ditto_count: called CC=2 SCAR=6.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
if @_ > 1 48 79 127
34 88 ▶sub send_count { $_[0]->{send_count} = $_[1] if @_ > 1; $_[0]->{send_count} }
Subroutine send_count: called CC=2 SCAR=6.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
if @_ > 1 6 82 88
35 129 ▶sub bypass_protocol { $_[0]->{bypass_protocol} = $_[1] if @_ > 1; $_[0]->{bypass_protocol} }
Subroutine bypass_protocol: called CC=2 SCAR=6.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
if @_ > 1 48 81 129
36 1466 ▶sub timings { $_[0]->{timings} = $_[1] if @_ > 1; $_[0]->{timings} }
Subroutine timings: called CC=2 SCAR=6.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
if @_ > 1 929 537 1466
37 892 ▶sub pronto { $_[0]->{pronto} = $_[1] if @_ > 1; $_[0]->{pronto} }
Subroutine pronto: called CC=2 SCAR=6.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
if @_ > 1 502 390 892
38  
39 3 ▶sub to_irsend {
Subroutine to_irsend: called CC=2 SCAR=6.9
Pod documented
40 3 my ($self) = @_;
41 my %hash = (
42 Protocol => $self->{protocol},
43 Bits => $self->{bits},
44 3 );
45 3 ▶ if (defined $self->{data}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"data"}) 2 1 3
46 2 $hash{Data} = _data_hex($self->{data});
47 }
48 3 return \%hash;
49 }
50  
51 # The display hex of a data word, padded to the whole bytes the value needs
52 # (minimum 4 digits), exactly as Tasmota prints Data: leading zeros are
53 # trimmed to an even digit count and the word is never truncated, so 36/48/72
54 # bit words keep every bit. Accepts a Math::BigInt for protocols whose frames
55 # exceed the native integer range (e.g. MWM, up to 144 bits).
56 320 ▶sub _data_hex {
Subroutine _data_hex: called CC=6 SCAR=18.0
57 320 my ($val) = @_;
58 320 ▶ return '' unless defined $val;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $val 0 320 320
59 320 my $hex;
60 320 ▶ if (ref $val && $val->can('as_hex')) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $val and $val->can('as_hex')) { } 88 232 320
Condition66%2/3
ref $val and $val->can("as_hex")
!ll&&!rl&&r
232088
MC/DC50%1/2
ref $val and $val->can("as_hex")
ref $val $val->can("as_hex")
Truth table66%2/3
A and B
A= ref $valB= $val->can("as_hex")
ABresult
0X0
100
111
61 88 $hex = $val->as_hex;
62 88 $hex =~ s/^0x//i;
63 } else {
64 232 $hex = sprintf("%X", $val);
65 }
66 320 $hex = uc $hex;
67 320 my $digits = length $hex;
68 320 my $width = 4;
69 320 ▶ $width = ($digits % 2) ? $digits + 1 : $digits if $digits > 4;
Branch: 2 branches
BranchTrueFalseTotal
if $digits > 4 284 36 320
$digits % 2 ? : 127 157 284
70 320 return '0x' . ('0' x ($width - $digits)) . $hex;
71 }
72  
73 # Reverse the bits within an 8-bit byte.
74 # Exported so protocol handlers can share it for cross-protocol conversion.
75 22 ▶sub reverse_byte {
Subroutine reverse_byte: called CC=2 SCAR=6.9
Pod documented
76 22 my ($val) = @_;
77 22 my $out = 0;
78 22 for my $i (0 .. 7) {
79 176 $out |= (($val >> $i) & 1) << (7 - $i);
80 }
81 22 return $out;
82 }
83  
84 56 1;
85  
86 =encoding utf8
87  
88 =head1 NAME
89  
90 Protocol::IR::Code - Intermediate representation of an IR remote control code
91  
92 =head1 VERSION
93  
94 version 1.5
95  
96 =head1 SYNOPSIS
97  
98 use Protocol::IR::Converter;
99  
100 my $converter = Protocol::IR::Converter->new();
101 my $code = $converter->import_code('NEC', '0x10EF00FF');
102  
103 print $code->protocol; # NEC
104 print $code->address; # 16
105 print $code->command; # 0
106  
107 my $hash = $code->to_irsend; # Tasmota IRSend JSON payload
108  
109 =head1 DESCRIPTION
110  
111 C<Protocol::IR::Code> is the unified intermediate representation used throughout the
112 Protocol::IR::Code distribution. Every protocol handler decodes into an C<Protocol::IR::Code>
113 object and every format exports from one, so a signal can be moved between
114 protocols and formats without loss of information.
115  
116 The object is compatible with Tasmota's C<IRSend> JSON payload structure:
117 L<to_irsend|/"to_irsend"> returns a hash with C<Protocol>, C<Bits>, and
118 C<Data> keys. The C<data> value is expressed so that it matches Tasmota's
119 C<Data> field for the protocol.
120  
121 =head1 ATTRIBUTES
122  
123 All attributes are read-write accessors, e.g. C<< $code->alias('POWER') >>.
124 They are created by the L<Protocol::IR::Converter> registry and the protocol/format
125 handlers; you normally do not construct C<Protocol::IR::Code> objects directly.
126  
127 =over 4
128  
129 =item protocol
130  
131 Protocol name (e.g. C<NEC>), or C<UNKNOWN> for undecoded timing data.
132  
133 =item bits
134  
135 Number of data bits in the frame.
136  
137 =item address
138  
139 Primary address (device) byte.
140  
141 =item subaddress
142  
143 Secondary address byte, or C<-1> when the protocol has none (JVC, SAMSUNG)
144 or when it equals the one's complement of the address (standard NEC frames).
145  
146 =item command
147  
148 Command (function) byte.
149  
150 =item data
151  
152 The raw transmitted value. Bit order matches the protocol's C<Data> field
153 in Tasmota (see the individual protocol modules).
154  
155 =item alias
156  
157 A human-readable button name, carried by CSV and wig round trips.
158  
159 =item ditto_count
160  
161 Number of repeat ("ditto") frames a wig should send after the first.
162  
163 =item send_count
164  
165 How many times the whole signal transmits per press (the wig's C<send_count>,
166 the device's C<timing.pressMinRepeats> for a JSON dump import). Zero means
167 the source carried no repeat count, so a wig export omits C<send_count> and
168 the default single press is assumed.
169  
170 =item bypass_protocol
171  
172 Flag marking that a wig should bypass protocol-aware repeat behavior.
173  
174 =item timings
175  
176 The raw waveform as a flat list of signed microsecond durations, alternating
177 mark (positive) and space (negative), exactly as captured: C<[+9185, -4490,
178 +650, -500, ...]>. It is set whenever a signal enters through a timing
179 format -- Tasmota C<RawData>, a mode2 capture, LIRC C<raw_codes>, or any
180 Pronto Hex string -- so the capture can be re-exported losslessly to any
181 timing format. C<undef> for codes built from decoded fields.
182  
183 The values are quantized to the source format's grid: Pronto durations are
184 integer carrier cycles times the period derived from the frequency word
185 (about 26.3 µs at 38 kHz), Tasmota compact timings are multiples of 5 µs, and
186 mode2/LIRC carry integer microseconds as measured. Protocol encoders emit
187 Pronto quantized to their nominal carrier, so a signal decoded from Pronto
188 re-encodes to the same pulse counts.
189  
190 =item pronto
191  
192 The verbatim Pronto Hex string the code was decoded from, when any, kept so
193 the code re-exports byte-identically through any Pronto output or
194 Pronto-passthrough container format (wig, a JSON dump) without
195 re-quantizing. On a signal no registered protocol recognizes (protocol
196 C<UNKNOWN>, C<bypass_protocol> set) this is alongside C<timings>. C<undef>
197 for codes built from decoded fields.
198  
199 C<timings> and C<pronto> are kept together on a decoded code because the two
200 format families each need their own lossless view: C<timings> feeds the
201 microsecond timing formats (Tasmota, mode2, LIRC), C<pronto> the hex container
202 formats (wig, a JSON dump, a Pronto re-export). Deriving either from the
203 other would re-quantize and could change silent fractions.
204  
205 =back
206  
207 =head1 METHODS
208  
209 =head2 to_irsend
210  
211 my $hash = $code->to_irsend;
212  
213 Returns a hashref with C<Protocol>, C<Bits>, and (when known) C<Data> keys,
214 matching the structure of a Tasmota C<IRSend> JSON payload.
215  
216 =head1 FUNCTIONS
217  
218 =head2 reverse_byte
219  
220 my $reversed = Protocol::IR::Code::reverse_byte(0xE0); # returns 0x07
221  
222 Reverses the bits within an 8-bit byte. Used by
223 L<Protocol::IR::Proto::SAMSUNG/as_necx2_params> and
224 L<Protocol::IR::Proto::NECX2/as_samsung_params> for cross-protocol
225 conversion between SAMSUNG and NECX2, where the Samsung address/command
226 bytes are the bit-reversal of the NECX2 device/function bytes.
227  
228 This is an exported package function (not a method), callable as
229 C<< Protocol::IR::Code::reverse_byte($val) >>.
230  
231 =head1 SUPPORT
232  
233 Source code: L<https://github.com/bwarden/perl-protocol-ir>
234  
235 Bug reports and feature requests: L<https://github.com/bwarden/perl-protocol-ir/issues>
236  
237 =head1 RELATED PROJECTS
238  
239 L<Protocol::IR::Code> trades formats with the IR ecosystem rather than
240 reinventing it:
241  
242 =over 4
243  
244 =item * LIRC -- L<remote definitions|https://www.lirc.org/>, and the L<mode2|Protocol::IR::Format::Mode2> capture tool
245  
246 =item * Tasmota -- L<IR send/receive and RawData|https://tasmota.github.io/docs/Tasmota-IR/>, built on L<IRremoteESP8266|https://github.com/crankyoldgit/IRremoteESP8266>
247  
248 =item * IRDB -- L<the community button/CSV database|https://github.com/probonopd/irdb>
249  
250 =item * HAIR -- L<the Home Assistant IR integration and wig format|https://github.com/DAB-LABS/HAIR>
251  
252 =item * IR Scrutinizer -- L<Pronto Hex format glossary|http://www.harctoolbox.org/Glossary.html>
253  
254 =back
255  
256 =head1 AUTHOR
257  
258 Brett T. Warden <bwarden@cpan.org>
259  
260 =head1 COPYRIGHT AND LICENSE
261  
262 Copyright (c) 2026 Brett T. Warden
263  
264 This library is free software; you can redistribute it and/or modify it
265 under the terms of the GNU General Public License as published by the
266 Free Software Foundation; either version 2 of the License, or (at your
267 option) any later version.
268  
269 =head1 TRADEMARK NOTICE
270  
271 This project exists solely to enable interoperability with independently
272 purchased hardware. It is an independent community project: it is not
273 supplied by, authorized by, affiliated with, or endorsed by The Walt Disney
274 Company or any other rights holder. "Made With Magic", "Glow With The Show",
275 and all related names and marks are trademarks of their respective owners,
276 referenced here only to identify interoperable functionality.
« bin/ir-convert ↑ Summary bin/ir-tasmota2wig »