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

filter: Statementstmt 100% 46 / 46 Branchbran 86.3% 19 / 22 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 4 / 4 Podpod 100% 2 / 2 totaltotal 95.9% 71 / 74 CC 14 SCAR 22.1
CC
14
Cov
96%
decode
12.0
export
3.0
BEGIN
1.0
CRAP 9.1
« blib/lib/Protocol/IR/Proto/Dish.pm ↑ Summary blib/lib/Protocol/IR/Proto/GICable.pm »
1 package Protocol::IR::Format::Pronto;
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 354 ▶sub export {
Subroutine export: called CC=3 SCAR=11.0
Pod documented
8 354 my ($class, $ir_code, $registry) = @_;
9  
10 # A code decoded from Pronto Hex carries its original hex verbatim (see
11 # decode), so re-emitting it needs no protocol encoder and round-trips
12 # byte-identically. Only codes built from decoded fields (import_code,
13 # CSV) are encoded here.
14 354 ▶ return $ir_code->pronto if defined $ir_code->pronto;
Branch: 1 branch
BranchTrueFalseTotal
if defined $ir_code->pronto 33 321 354
15  
16 321 my $proto_name = $ir_code->protocol;
17 321 my $plugin = $registry->get_protocol($proto_name);
18  
19 321 ▶ die "No protocol encoder registered for: $proto_name\n" unless $plugin;
Branch: 1 branch
BranchTrueFalseTotal
unless $plugin 1 320 321
20 320 return $plugin->to_pronto($ir_code);
21 }
22  
23 # Renamed from 'import' to 'decode' to avoid Perl's module import hook
24 515 ▶sub decode {
Subroutine decode: called CC=12 SCAR=24.9
Pod documented
25 515 my ($class, $pronto_str, $registry) = @_;
26  
27 515 ▶ die "No Pronto Hex string provided\n" unless defined $pronto_str;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $pronto_str 0 515 515
28  
29 # Clean whitespace and split hex tokens
30 515 $pronto_str =~ s/^\s+|\s+$//g;
31 515 my @tokens = split(/\s+/, $pronto_str);
32  
33 515 ▶ die "Invalid Pronto Hex string (too short)\n" if scalar(@tokens) < 4;
Branch: 1 branch
BranchTrueFalseTotal
if scalar @tokens < 4 2 513 515
34  
35 513 my $raw_format = hex($tokens[0]);
36 513 ▶ die "Only raw Pronto Hex format (0000) is supported\n" unless $raw_format == 0;
Branch: 1 branch
BranchTrueFalseTotal
unless $raw_format == 0 1 512 513
37  
38 512 my $freq_word = hex($tokens[1]);
39 512 my $seq1_pairs = hex($tokens[2]);
40 512 my $seq2_pairs = hex($tokens[3]);
41  
42 # Calculate carrier frequency and period in microseconds
43 512 ▶ my $carrier_hz = ($freq_word > 0) ? int(1000000.0 / ($freq_word * 0.241246)) : 38000;
Branch: 1 branch
BranchTrueFalseTotal
$freq_word > 0 ? : 511 1 512
44 512 my $period_us = 1000000.0 / $carrier_hz;
45  
46 512 my @burst_pairs_us;
47 512 my $pair_count = $seq1_pairs + $seq2_pairs;
48  
49 512 ▶ for (my $i = 0; $i < $pair_count; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $pair_count) 17735 512 18247
50 17735 my $idx = 4 + ($i * 2);
51 17735 ▶ last if $idx + 1 >= scalar(@tokens);
Branch: 1 branch
BranchTrueFalseTotal
if $idx + 1 >= scalar @tokens 0 17735 17735
52  
53 17735 my $mark_cycles = hex($tokens[$idx]);
54 17735 my $space_cycles = hex($tokens[$idx + 1]);
55  
56 17735 push @burst_pairs_us, [
57 $mark_cycles * $period_us,
58 $space_cycles * $period_us
59 ];
60 }
61  
62 # Keep the capture so any re-export is lossless: the quantized mark/space
63 # waveform (for the timing formats) and the original hex verbatim (for a
64 # Pronto re-export or a Pronto-passthrough container such as wig), exactly
65 # as the other timing formats (Tasmota, mode2, LIRC) already do.
66 512 my @timings;
67 512 for my $pair (@burst_pairs_us) {
68 17735 push @timings, int($pair->[0]), -int($pair->[1]);
69 }
70  
71 # Iterate over registered protocols (in registration order) to decode
72 # the timing array
73 512 for my $proto_class ($registry->get_protocols()) {
74 4876 ▶ if ($proto_class->can('decode_timing')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($proto_class->can("decode_timing")) 4876 0 4876
75 4876 my $code = $proto_class->decode_timing(\@burst_pairs_us);
76 4876 ▶ if (defined $code) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $code) 502 4374 4876
77 502 $code->timings(\@timings);
78 502 $code->pronto($pronto_str);
79 502 return $code;
80 }
81 }
82 }
83  
84 # No registered protocol matched. A payload with real timing data is
85 # still valid raw Pronto Hex, so keep it as an opaque UNKNOWN code rather
86 # than failing: container conversions that just move Pronto hex (e.g.
87 # JSON dump to wig) must not depend on naming the protocol. A truncated
88 # or empty payload is malformed, not merely unknown, and still dies.
89 10 ▶ die "Unable to decode Pronto Hex string into a known protocol\n"
Branch: 1 branch
BranchTrueFalseTotal
unless @burst_pairs_us 1 9 10
90 unless @burst_pairs_us;
91  
92 9 return Protocol::IR::Code->new(
93 protocol => 'UNKNOWN',
94 bypass_protocol => 1,
95 timings => \@timings,
96 pronto => $pronto_str,
97 );
98 }
99  
100 55 1;
101  
102 =head1 NAME
103  
104 Protocol::IR::Format::Pronto - Raw Pronto Hex encoder and decoder
105  
106 =head1 VERSION
107  
108 version 1.5
109  
110 =head1 SYNOPSIS
111  
112 use Protocol::IR::Converter;
113  
114 my $converter = Protocol::IR::Converter->new();
115 my $code = $converter->import_code('NEC', '0x10EF00FF');
116  
117 # Encode an Protocol::IR::Code object as Pronto Hex
118 my $pronto = $converter->export_code($code, 'Pronto');
119  
120 # Decode Pronto Hex back into an Protocol::IR::Code
121 my $decoded = $converter->import_format('Pronto', $pronto);
122  
123 =head1 DESCRIPTION
124  
125 C<Protocol::IR::Format::Pronto> encodes L<Protocol::IR::Code> objects into Pronto Hex strings
126 and decodes Pronto Hex back into L<Protocol::IR::Code> objects. Only the I<raw> form
127 (header C<0000>) is supported.
128  
129 =over 4
130  
131 =item Example Pronto Hex (a 32-bit NEC transmission)
132  
133 0000 006D 0022 0000 0157 00AC 0015 0015 0015 0015 0015 0015 0015 0015 \
134 0015 0040 0015 0015 0015 0015 0015 0015 0015 0040 0015 0040 0015 0040 \
135 0015 0040 0015 0015 0015 0040 0015 0040 0015 0040 0015 0015 0015 0015 \
136 0015 0015 0015 0015 0015 0015 0015 0015 0015 0015 0015 0015 0015 0040 \
137 0015 0040 0015 0040 0015 0040 0015 0040 0015 0040 0015 0040 0015 0040 \
138 0015 0689
139  
140 The fields are the raw-format marker (C<0000>), the carrier frequency word
141 (C<006D>), the burst-pair counts for the one-time and repeat sequences
142 (C<0022 0000>), then that many mark/space pulse-count pairs, the final value
143 being the trailing stop bit. See the IR Scrutinizer glossary for the full
144 field layout (L<http://www.harctoolbox.org/Glossary.html>).
145  
146 =back
147  
148 The carrier frequency word is stored in the frequency field and converted
149 to a period in microseconds: the pulse count stored for each mark/space is
150 the duration in carrier cycles, so the actual duration depends on the
151 transmitter's carrier frequency. Protocol encoders emit frequency words for
152 their nominal carrier (typically 38 kHz) and pulse counts quantized to that
153 carrier, so the decoded microsecond timings round-trip cleanly.
154  
155 Because decoding works from the microsecond timing signature (see
156 L<Protocol::IR::Converter/"DECODING VERSUS GENERATING TIMINGS">), a Pronto string is
157 only recognized if it matches a registered protocol. Whether or not it does,
158 the decoded L<Protocol::IR::Code> keeps the exact capture -- the quantized
159 C<timings> and the hex stashed verbatim in C<pronto> -- so C<export> (and any
160 Pronto-passthrough container such as wig or the JSON dump importer)
161 round-trips the string byte-identically. A well-formed string with real
162 timing data that no protocol recognizes is not an error: it decodes to an
163 opaque C<UNKNOWN> code (C<bypass_protocol> set) so container conversions
164 that move Pronto hex can still complete. Truncated or empty payloads are
165 still rejected.
166  
167 =over 4
168  
169 =item * C<pronto> is set on every Pronto decode, so C<export> re-emits the
170 original string rather than re-quantizing through the protocol encoder.
171  
172 =item * C<timings> is set on every Pronto decode, so the same microsecond
173 waveform feeds the other timing formats (Tasmota, mode2, LIRC) losslessly.
174  
175 =back
176  
177 =head1 METHODS
178  
179 =head2 export
180  
181 my $pronto = $class->export($ir_code, $registry);
182  
183 Converts a single L<Protocol::IR::Code> object into a Pronto Hex string.
184 When the code was itself decoded from Pronto Hex, its stashed C<pronto>
185 string is re-emitted verbatim. Otherwise the registered protocol handler for
186 the code's protocol encodes it from its decoded fields (L<to_pronto>).
187  
188 =head2 decode
189  
190 my $code = $class->decode($pronto_str, $registry);
191  
192 Parses a Pronto Hex string into microsecond mark/space pairs and tries each
193 registered protocol's C<decode_timing> in registration order. Returns the
194 first matching L<Protocol::IR::Code>, or dies if the string cannot be decoded.
195  
196 C<decode> is named C<decode> rather than C<import> to avoid clashing with
197 Perl's module import hook.
198  
199 =head1 SUPPORT
200  
201 Source code: L<https://github.com/bwarden/perl-protocol-ir>
202  
203 Bug reports and feature requests: L<https://github.com/bwarden/perl-protocol-ir/issues>
204  
205 =head1 AUTHOR
206  
207 Brett T. Warden <bwarden@cpan.org>
208  
209 =head1 COPYRIGHT AND LICENSE
210  
211 Copyright (c) 2026 Brett T. Warden
212  
213 This library is free software; you can redistribute it and/or modify it
214 under the terms of the GNU General Public License as published by the
215 Free Software Foundation; either version 2 of the License, or (at your
216 option) any later version.
« blib/lib/Protocol/IR/Proto/Dish.pm ↑ Summary blib/lib/Protocol/IR/Proto/GICable.pm »