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

filter: Statementstmt 98.8% 83 / 84 Branchbran 86.8% 33 / 38 Conditioncond 78.5% 11 / 14 MC/DCmcdc 44.4% 4 / 9 Subroutinesub 100% 8 / 8 Podpod 100% 2 / 2 totaltotal 90.9% 141 / 155 CC 31 SCAR 19.7
CC
31
Cov
93%
_decode_message
9.1
decode
9.0
export
8.1
CRAP 7.1
« blib/lib/Protocol/IR/Proto/SAMSUNG20.pm ↑ Summary blib/lib/Protocol/IR/Proto/JVC48.pm »
1 package Protocol::IR::Format::Mode2;
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 # Mode2 pulse/space capture import and export, the native format of the LIRC
10 # `mode2` tool and the MQTT IR test rig's receiver topic ("hear back timings").
11 #
12 # A mode2 capture is one timing per line: "pulse 417" and "space 1251", in
13 # microseconds, strictly alternating. Consecutive messages are separated by an
14 # inter-message space far wider than any in-frame timing; the log splits on a
15 # space of 10000 µs or more (lirc's default gap), keeping the separator in the
16 # message so the final space is part of the decoded signal.
17 #
18 # decode() reads the whole capture, splits it into messages, decodes each
19 # through every registered protocol decoder, and returns one Protocol::IR::Code per
20 # message with its raw timings kept, so a re-export is lossless. Messages with
21 # no mark at all (the doubled-gap dead air between two separators) are dropped;
22 # signals no protocol recognizes are kept as UNKNOWN rather than discarded,
23 # because a capture is a record of what was on the air, not a filter.
24 #
25 # export() writes one message as alternating pulse/space lines, ensuring the
26 # message ends on a space wide enough (>= 10000 µs) that re-importing the file
27 # splits messages back at the same boundaries.
28  
29 # lirc's default inter-message gap: any space this wide separates messages.
30 55 my $SPLIT = 10000;
31 # The gap written at the end of an exported message that does not already end
32 # on a wide space. Far above any in-frame timing and well past SPLIT.
33 55 my $EXPORT_GAP = 100000;
34  
35 # Split a flat signed timing list into messages on spaces of SPLIT us or
36 # more, keeping the separator in the message that precedes it.
37 7 ▶sub _split_messages {
Subroutine _split_messages: called CC=5 SCAR=16.1
38 7 my ($flat) = @_;
39 7 my (@msgs, @cur);
40 7 for my $v (@$flat) {
41 14999 push @cur, $v;
42 14999 ▶ if ($v < 0 && -$v >= $SPLIT) {
Branch: 1 branch
BranchTrueFalseTotal
if ($v < 0 and -$v >= $SPLIT) 375 14624 14999
Condition100%3/3
$v < 0 and -$v >= $SPLIT
!ll&&!rl&&r
74717153375
MC/DC100%2/2
$v < 0 and -$v >= $SPLIT
$v < 0 -$v >= $SPLIT
Truth table100%3/3
A and B
A= $v < 0B= -$v >= $SPLIT
ABresult
0X0
100
111
43 375 push @msgs, [@cur];
44 375 @cur = ();
45 }
46 }
47 7 ▶ push @msgs, [@cur] if @cur;
Branch: 1 branch
BranchTrueFalseTotal
if @cur 1 6 7
48 7 return \@msgs;
49 }
50  
51 # Decode one message's flat signed timings (positive marks, negative spaces)
52 # into a Protocol::IR::Code by trying every registered protocol decoder. The code keeps
53 # the raw timings so it re-exports losslessly; a signal no protocol recognizes
54 # is tagged UNKNOWN.
55 318 ▶sub _decode_message {
Subroutine _decode_message: called CC=9 SCAR=22.0
56 318 my ($values, $registry) = @_;
57 318 my @pairs;
58 318 ▶ for (my $i = 0; $i < @$values; $i += 2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @$values) 7471 318 7789
59 7471 ▶ push @pairs, [abs($values->[$i]), abs($values->[$i + 1] // 0)];
Condition100%2/2
$$values[$i + 1] // 0
l!l
74701
MC/DC100%1/1
$$values[$i + 1] // 0
$$values[$i + 1]
Truth table100%2/2
A
A= $$values[$i + 1]
Aresult
11
00
60 }
61  
62 318 my $code;
63 318 for my $proto_class ($registry->get_protocols()) {
64 5061 ▶ next unless $proto_class->can('decode_timing');
Branch: 1 branch
BranchTrueFalseTotal
unless $proto_class->can("decode_timing") 0 5061 5061
65 5061 my $decoded = $proto_class->decode_timing(\@pairs);
66 5061 ▶ if (defined $decoded) { $code = $decoded; last; }
Branch: 1 branch
BranchTrueFalseTotal
if (defined $decoded) 204 4857 5061
67 }
68 318 ▶ $code ||= Protocol::IR::Code->new(protocol => 'UNKNOWN');
Condition66%2/3
$code ||= "Protocol::IR::Code"->new("protocol", "UNKNOWN")
l!l&&r!l&&!r
2041140
MC/DC0%0/2
$code ||= "Protocol::IR::Code"->new("protocol", "UNKNOWN")
$code "Protocol::IR::Code"->new("protocol", "UNKNOWN")
Truth table66%2/3
A ||= B
A= $codeB= "Protocol::IR::Code"->new("protocol", "UNKNOWN")
ABresult
1X1
011
000
69 318 $code->timings($values);
70 318 ▶ if ($code->protocol ne 'UNKNOWN' && defined $code->data) {
Branch: 1 branch
BranchTrueFalseTotal
if ($code->protocol ne "UNKNOWN" and defined $code->data) 204 114 318
Condition66%2/3
$code->protocol ne "UNKNOWN" and defined $code->data
!ll&&!rl&&r
1140204
MC/DC50%1/2
$code->protocol ne "UNKNOWN" and defined $code->data
$code->protocol ne "UNKNOWN" defined $code->data
Truth table66%2/3
A and B
A= $code->protocol ne "UNKNOWN"B= defined $code->data
ABresult
0X0
100
111
71 204 $code->alias(Protocol::IR::Code::_data_hex($code->data));
72 }
73 318 return $code;
74 }
75  
76 # Derive a flat signed timing list for a fresh Protocol::IR::Code by round-tripping
77 # through the registered protocol encoder's Pronto output.
78 2 ▶sub _timings_from_code {
Subroutine _timings_from_code: called CC=4 SCAR=13.9
79 2 my ($ir_code, $registry) = @_;
80 2 my $pronto = $registry->export_code($ir_code, 'Pronto');
81 2 $pronto =~ s/^\s+|\s+$//g;
82 2 my @tokens = split /\s+/, $pronto;
83 2 ▶ die "Cannot derive Mode2 timings from protocol encoder output\n"
Branch: 1 branch
BranchTrueFalseTotal
if scalar @tokens < 4 0 2 2
84 if scalar(@tokens) < 4;
85  
86 2 my $freq_word = hex($tokens[1]);
87 2 ▶ my $carrier = ($freq_word > 0) ? int(1000000.0 / ($freq_word * 0.241246)) : 38000;
Branch: 1 branch
BranchTrueFalseTotal
$freq_word > 0 ? : 2 0 2
88 2 my $period = 1000000.0 / $carrier;
89  
90 2 my $pair_count = hex($tokens[2]) + hex($tokens[3]);
91 2 my @flat;
92 2 ▶ for (my $i = 0; $i < $pair_count; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $pair_count) 43 2 45
93 43 my $idx = 4 + ($i * 2);
94 43 my $mark = hex($tokens[$idx]) * $period;
95 43 my $space = hex($tokens[$idx + 1]) * $period;
96 43 push @flat, int($mark + 0.5), -int($space + 0.5);
97 }
98 2 return \@flat;
99 }
100  
101 9 ▶sub decode {
Subroutine decode: called CC=9 SCAR=22.0
Pod documented
102 9 my ($class, $input, $registry) = @_;
103 9 ▶ die "No Mode2 capture provided\n" unless defined $input;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $input 1 8 9
104  
105 8 ▶ my $text = ref $input eq 'ARRAY' ? join("\n", @$input) : "$input";
Branch: 1 branch
BranchTrueFalseTotal
ref $input eq 'ARRAY' ? : 0 8 8
106 8 my @flat;
107 8 for my $line (split /\r?\n/, $text) {
108 15000 ▶ next unless $line =~ /^\s*(pulse|space)\s+(\d+)/i;
Branch: 1 branch
BranchTrueFalseTotal
unless $line =~ /^\s*(pulse|space)\s+(\d+)/i 1 14999 15000
109 14999 ▶ my $sign = (lc($1) eq 'pulse') ? 1 : -1;
Branch: 1 branch
BranchTrueFalseTotal
lc $1 eq 'pulse' ? : 7471 7528 14999
110 14999 push @flat, $sign * ($2 + 0);
111 }
112 8 ▶ die "No Mode2 timings found\n" unless @flat;
Branch: 1 branch
BranchTrueFalseTotal
unless @flat 1 7 8
113  
114 7 my @codes;
115 7 for my $msg (@{ _split_messages(\@flat) }) {
116 # The dead air between two separators (a doubled gap) has no mark.
117 376 ▶ next unless grep { $_ > 0 } @$msg;
Branch: 1 branch
BranchTrueFalseTotal
unless grep {$_ > 0;} @$msg 58 318 376
118 318 push @codes, _decode_message($msg, $registry);
119 }
120 7 return \@codes;
121 }
122  
123 6 ▶sub export {
Subroutine export: called CC=8 SCAR=20.9
Pod documented
124 6 my ($class, $ir_code, $registry) = @_;
125 6 ▶ my $list = ref $ir_code eq 'ARRAY' ? $ir_code : [$ir_code];
Branch: 1 branch
BranchTrueFalseTotal
ref $ir_code eq 'ARRAY' ? : 3 3 6
126 6 my @lines;
127 6 for my $code (@$list) {
128 161 my $timings = $code->timings;
129 161 ▶ $timings ||= _timings_from_code($code, $registry);
Condition66%2/3
$timings ||= _timings_from_code($code, $registry)
l!l&&r!l&&!r
15920
MC/DC0%0/2
$timings ||= _timings_from_code($code, $registry)
$timings _timings_from_code($code, $registry)
Truth table66%2/3
A ||= B
A= $timingsB= _timings_from_code($code, $registry)
ABresult
1X1
011
000
130 161 $timings = [ @$timings ];
131 161 my $last = $timings->[-1];
132 161 ▶ if ($last > 0) {
Branch: 2 branches
BranchTrueFalseTotal
if ($last > 0) { } 0 161 161
elsif (-$last < $SPLIT) { } 2 159 161
133 # Ends on a mark: append an inter-message gap.
134 0 push @$timings, -$EXPORT_GAP;
135 } elsif (-$last < $SPLIT) {
136 # Ends on a space too short to split on: widen it into the gap.
137 2 $timings->[-1] = -$EXPORT_GAP;
138 }
139 161 for my $v (@$timings) {
140 7700 ▶ push @lines, sprintf("%s %d", ($v > 0 ? 'pulse' : 'space'), int(abs($v) + 0.5));
Branch: 1 branch
BranchTrueFalseTotal
$v > 0 ? : 3850 3850 7700
141 }
142 }
143 6 return join("\n", @lines) . "\n";
144 }
145  
146 55 1;
147  
148 =encoding utf8
149  
150 =head1 NAME
151  
152 Protocol::IR::Format::Mode2 - LIRC mode2 pulse/space capture import and export
153  
154 =head1 VERSION
155  
156 version 1.5
157  
158 =head1 SYNOPSIS
159  
160 use Protocol::IR::Converter;
161  
162 my $converter = Protocol::IR::Converter->new();
163  
164 # Import a mode2 capture (pulse/space lines, one timing per line)
165 my $capture = "pulse 4514\nspace 4514\npulse 549\nspace 1708\n...";
166 my $codes = $converter->import_format('Mode2', $capture);
167  
168 # Re-export a code as mode2 lines
169 my $text = $converter->export_code($codes->[0], 'Mode2');
170  
171 =head1 DESCRIPTION
172  
173 C<Protocol::IR::Format::Mode2> imports and exports the pulse/space capture
174 format of the LIRC C<mode2> tool (and the MQTT IR test rig's receiver topic):
175 one timing per line, C<pulse N> or C<space N>, in microseconds, strictly
176 alternating.
177  
178 =over 4
179  
180 =item Example capture (what C<mode2> puts on stdout)
181  
182 pulse 4514
183 space 4514
184 pulse 549
185 space 1708
186 pulse 549
187 space 1647
188 ...
189 space 100000
190  
191 =back
192  
193 C<mode2> is part of LIRC (L<https://www.lirc.org/>).
194  
195 C<decode> reads a whole capture, splits it into messages on spaces of
196 B<10000 µs> or more (lirc's default inter-message gap, keeping the separator
197 in the message that precedes it), decodes each message through every
198 registered protocol decoder, and returns one L<Protocol::IR::Code> per message.
199 Every code keeps its raw timings so a re-export is lossless. Messages with no
200 mark at all (dead air between two separators) are dropped; signals no
201 protocol recognizes are kept with protocol C<UNKNOWN> rather than discarded,
202 because a capture is a record of what was on the air. Codes that decode to a
203 known protocol get their C<alias> set to the signal's Data hex value.
204  
205 C<export> writes one message per code as alternating pulse/space lines,
206 ensuring each message ends on a space wide enough (B<100000 µs>, or its
207 existing trailing space when already B<E<gt>= 10000 µs>) that re-importing
208 the output splits messages back at the same boundaries. When a code carries
209 no raw timings (e.g. one built with C<import_code>), they are derived by
210 round-tripping the code through the protocol encoder's Pronto output, so the
211 emitted timings match the other timing formats exactly.
212  
213 =head1 METHODS
214  
215 =head2 decode
216  
217 my $codes = $class->decode($input, $registry);
218  
219 Parses a mode2 capture (a string, or an arrayref of lines) into a list of
220 L<Protocol::IR::Code> objects, one per message. Dies on empty input with no
221 timings.
222  
223 =head2 export
224  
225 my $text = $class->export($ir_code, $registry);
226  
227 Serializes one L<Protocol::IR::Code>, or an arrayref of them, into mode2
228 pulse/space lines.
229  
230 =head1 SUPPORT
231  
232 Source code: L<https://github.com/bwarden/perl-protocol-ir>
233  
234 Bug reports and feature requests: L<https://github.com/bwarden/perl-protocol-ir/issues>
235  
236 =head1 AUTHOR
237  
238 Brett T. Warden <bwarden@cpan.org>
239  
240 =head1 COPYRIGHT AND LICENSE
241  
242 Copyright (c) 2026 Brett T. Warden
243  
244 This library is free software; you can redistribute it and/or modify it
245 under the terms of the GNU General Public License as published by the
246 Free Software Foundation; either version 2 of the License, or (at your
247 option) any later version.
« blib/lib/Protocol/IR/Proto/SAMSUNG20.pm ↑ Summary blib/lib/Protocol/IR/Proto/JVC48.pm »