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

filter: Statementstmt 92.8% 155 / 167 Branchbran 74.5% 79 / 106 Conditioncond 78.5% 22 / 28 MC/DCmcdc 66.6% 12 / 18 Subroutinesub 100% 14 / 14 Podpod 100% 3 / 3 totaltotal 84.8% 285 / 336 CC 69 SCAR 24.5
CC
69
Cov
85%
decode_dump
23.3
decode
15.6
_to_signed_values
10.3
CRAP 11.6
« blib/lib/Protocol/IR/Proto/MWM.pm ↑ Summary blib/lib/Protocol/IR/Proto/Sony.pm »
1 package Protocol::IR::Format::Tasmota;
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 # Tasmota IR raw data support.
10 #
11 # decode() accepts the content of a Tasmota "RawData" field, either as the
12 # compact letter-compressed form ("+8570-4240+550-1580C-510+565-1565F-505Fh...")
13 # or as a plain comma-separated mark/space list ("926,844,958,..."). The
14 # timings are decoded to microsecond mark/space pairs, tried against every
15 # registered protocol, and the resulting Protocol::IR::Code keeps the raw timings so
16 # the signal can be re-exported losslessly.
17 #
18 # export() produces a Tasmota IRSend raw command:
19 # IRSend <frequency>,<rawdata>
20 # with the compact form by default, or the comma list with style => 'comma'.
21  
22 # --- compact format codec ----------------------------------------------
23 #
24 # Each new timing value gets the next letter (A-Z); a repeated value is
25 # written as that letter, uppercase for a HIGH (mark) signal and lowercase
26 # for a LOW (space) signal. Magnitudes are multiples of 5 microseconds.
27  
28 1736 ▶sub _round5 {
Subroutine _round5: called CC=1 SCAR=0.0
29 1736 my ($v) = @_;
30 1736 return int(($v + 2.5) / 5) * 5;
31 }
32  
33 # The Tasmota compact encoding assigns letters (A-Z) to the first 26 distinct
34 # timing magnitudes in order of first appearance. A repeated value is written
35 # as its letter, uppercase for a mark and lowercase for a space. Values beyond
36 # the 26-letter table are written numerically every time they occur.
37 90 ▶sub _decode_compact {
Subroutine _decode_compact: called CC=9 SCAR=22.0
38 90 my ($text) = @_;
39 90 my @values;
40 90 my (%letter_for, %rev); # magnitude -> letter, letter -> magnitude
41 90 my $count = 0;
42 90 ▶ while ($text =~ /([+\-]\d+|[A-Za-z])/g) {
Branch: 1 branch
BranchTrueFalseTotal
if ($text =~ /([+\-]\d+|[A-Za-z])/g) 11120 89 11209
43 11120 my $tok = $1;
44 11120 ▶ if ($tok =~ /^([+\-])(\d+)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tok =~ /^([+\-])(\d+)$/) { } 4020 7100 11120
45 4020 my $mag = $2 + 0;
46 4020 ▶ if (!exists $letter_for{$mag} && $count < 26) {
Branch: 1 branch
BranchTrueFalseTotal
if (not exists $letter_for{$mag} and $count < 26) 2022 1998 4020
Condition100%3/3
not exists $letter_for{$mag} and $count < 26
!ll&&!rl&&r
8019182022
MC/DC100%2/2
not exists $letter_for{$mag} and $count < 26
not exists $letter_for{$mag} $count < 26
Truth table100%3/3
not(A) and B
A= not exists $letter_for{$mag}B= $count < 26
ABresult
0X0
100
111
47 2022 $letter_for{$mag} = chr(ord('A') + $count++);
48 2022 $rev{ $letter_for{$mag} } = $mag;
49 }
50 4020 ▶ push @values, ($1 eq '+' ? 1 : -1) * $mag;
Branch: 1 branch
BranchTrueFalseTotal
$1 eq '+' ? : 1896 2124 4020
51 } else {
52 die "Tasmota compact format references undefined timing letter '$tok'\n"
53 7100 ▶ unless exists $rev{uc $tok};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $rev{uc $tok} 1 7099 7100
54 7099 my $mag = $rev{uc $tok};
55 7099 ▶ push @values, ($tok =~ /^[A-Z]/ ? 1 : -1) * $mag;
Branch: 1 branch
BranchTrueFalseTotal
$tok =~ /^[A-Z]/ ? : 3706 3393 7099
56 }
57 }
58 89 ▶ die "Tasmota compact format contains no timing data\n" unless @values;
Branch: 1 branch
BranchTrueFalseTotal
unless @values 0 89 89
59 89 return \@values;
60 }
61  
62 19 ▶sub _encode_compact {
Subroutine _encode_compact: called CC=7 SCAR=19.5
63 19 my ($values) = @_;
64 19 my @out;
65 my %letter_for;
66 19 my $next = 0;
67 19 for my $v (@$values) {
68 1736 ▶ my $sign = ($v < 0) ? '-' : '+';
Branch: 1 branch
BranchTrueFalseTotal
$v < 0 ? : 860 876 1736
69 1736 my $mag = _round5(abs($v));
70 1736 ▶ if (!exists $letter_for{$mag}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not exists $letter_for{$mag}) { } 361 1375 1736
71 361 ▶ if ($next < 26) {
Branch: 1 branch
BranchTrueFalseTotal
if ($next < 26) 339 22 361
72 339 $letter_for{$mag} = chr(ord('A') + $next++);
73 }
74 361 push @out, $sign . $mag;
75 } else {
76 1375 my $letter = $letter_for{$mag};
77 1375 ▶ $letter = uc($letter) if $sign eq '+';
Branch: 1 branch
BranchTrueFalseTotal
if $sign eq "+" 740 635 1375
78 1375 ▶ $letter = lc($letter) if $sign eq '-';
Branch: 1 branch
BranchTrueFalseTotal
if $sign eq "-" 635 740 1375
79 1375 push @out, $letter;
80 }
81 }
82 19 return join '', @out;
83 }
84  
85 # Normalize the various RawData forms into a flat list of signed timings,
86 # positive for marks and negative for spaces.
87 111 ▶sub _to_signed_values {
Subroutine _to_signed_values: called CC=9 SCAR=23.3
88 111 my ($input) = @_;
89 111 ▶ die "No Tasmota RawData provided\n" unless defined $input;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $input 0 111 111
90  
91 111 ▶ if (ref $input eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $input eq "ARRAY") 0 111 111
92 0 my @values;
93 0 for my $i (0 .. $#$input) {
94 0 ▶ push @values, ($i % 2 == 0 ? 1 : -1) * ($input->[$i] + 0);
Branch: 1 branch
BranchTrueFalseTotal
$i % 2 == 0 ? : 0 0 0
95 }
96 0 return \@values;
97 }
98  
99 111 my $text = $input;
100 111 $text =~ s/^\s+|\s+$//g;
101 # Accept a full "IRSend <freq>,<rawdata>" command line as well.
102 111 $text =~ s/^IRsend\s+\d+,//i;
103  
104 111 ▶ if ($text =~ /,/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($text =~ /,/) 19 92 111
105 19 my @values;
106 19 my $i = 0;
107 19 for my $tok (split /,/, $text) {
108 1500 $tok =~ s/^\s+|\s+$//g;
109 1500 ▶ push @values, ($i++ % 2 == 0 ? 1 : -1) * ($tok + 0);
Branch: 1 branch
BranchTrueFalseTotal
$i++ % 2 == 0 ? : 756 744 1500
110 }
111 19 return \@values;
112 }
113 92 ▶ if ($text =~ /[A-Za-z+\-]/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($text =~ /[A-Za-z+\-]/) 90 2 92
114 90 return _decode_compact($text);
115 }
116 2 die "Unrecognized Tasmota RawData input\n";
117 }
118  
119 125 ▶sub decode {
Subroutine decode: called CC=15 SCAR=27.5
Pod documented
120 125 my ($class, $input, $registry) = @_;
121 125 ▶ die "No Tasmota RawData provided\n" unless defined $input;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $input 0 125 125
122  
123 # A multi-line dump (a Tasmota console log of IRrecv lines, possibly
124 # timestamp-prefixed) splits into one signal per line.
125 125 ▶ return $class->decode_dump($input, $registry)
Branch: 1 branch
BranchTrueFalseTotal
if not ref $input and $input =~ /[\r\n]/ 2 123 125
Condition66%2/3
not ref $input and $input =~ /[\r\n]/
!ll&&!rl&&r
01232
MC/DC50%1/2
not ref $input and $input =~ /[\r\n]/
not ref $input $input =~ /[\r\n]/
Truth table66%2/3
not(A) and B
A= not ref $inputB= $input =~ /[\r\n]/
ABresult
0X0
100
111
126 if !ref $input && $input =~ /[\r\n]/;
127  
128 # A protocol-structured line ("Protocol = NEC, Bits = 32, Data = 0x...",
129 # or the JSON form Tasmota publishes over MQTT) imports through the named
130 # protocol rather than by timing decode, preferring DataLSB for the
131 # per-byte LSB-first protocols exactly as the JS port does.
132 123 ▶ if (!ref $input) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $input) 123 0 123
133 123 my ($proto, $data, $data_lsb) = _parse_structured($input);
134 123 ▶ if (defined $proto) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $proto) 12 111 123
135 12 my $code = $class->_import_structured($proto, $data, $data_lsb, $registry);
136 12 ▶ if ($code) {
Branch: 1 branch
BranchTrueFalseTotal
if ($code) 12 0 12
137 12 ▶ $code->alias(_data_alias($code)) unless $code->alias;
Branch: 1 branch
BranchTrueFalseTotal
unless $code->alias 12 0 12
138 12 return [$code];
139 }
140 0 return [];
141 }
142 }
143  
144 111 my $values = _to_signed_values($input);
145  
146 108 my @flat = @$values;
147 108 my @pairs;
148 108 ▶ while (@flat) {
Branch: 1 branch
BranchTrueFalseTotal
if (@flat) 6357 108 6465
149 6357 my $mark = shift @flat;
150 6357 ▶ my $space = shift @flat // 0;
Condition100%2/2
shift @flat // 0
l!l
626097
MC/DC100%1/1
shift @flat // 0
shift @flat
Truth table100%2/2
A
A= shift @flat
Aresult
11
00
151 6357 push @pairs, [abs($mark), abs($space)];
152 }
153  
154 108 my $code;
155 108 for my $proto_class ($registry->get_protocols()) {
156 1274 ▶ next unless $proto_class->can('decode_timing');
Branch: 1 branch
BranchTrueFalseTotal
unless $proto_class->can("decode_timing") 0 1274 1274
157 1274 my $decoded = $proto_class->decode_timing(\@pairs);
158 1274 ▶ if (defined $decoded) { $code = $decoded; last; }
Branch: 1 branch
BranchTrueFalseTotal
if (defined $decoded) 102 1172 1274
159 }
160 108 ▶ $code ||= Protocol::IR::Code->new(protocol => 'UNKNOWN');
Condition66%2/3
$code ||= "Protocol::IR::Code"->new("protocol", "UNKNOWN")
l!l&&r!l&&!r
10260
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
161 108 $code->timings($values);
162 108 ▶ $code->alias(_data_alias($code)) if $code->protocol ne 'UNKNOWN';
Branch: 1 branch
BranchTrueFalseTotal
if $code->protocol ne "UNKNOWN" 102 6 108
163  
164 108 return [$code];
165 }
166  
167 # Parse a Tasmota IRrecv protocol-structured record, in either the console
168 # log form ("Protocol = NEC, Bits = 32, Data = 0x10EF00FF") or the JSON form
169 # Tasmota publishes ("Protocol":"NEC","Data":"0x10EF00FF","DataLSB":
170 # "0x08F700FF"). Returns the protocol name, the Data token, and the DataLSB
171 # token (or undef when absent), or an empty list when the line is not
172 # structured.
173 123 ▶sub _parse_structured {
Subroutine _parse_structured: called CC=3 SCAR=11.0
174 123 my ($text) = @_;
175 123 ▶ my ($proto) = $text =~ /\bProtocol"?\s*[=:]\s*"?([A-Za-z][A-Za-z0-9_-]*)"?/i
Branch: 1 branch
BranchTrueFalseTotal
my($proto) = $text =~ /\bProtocol"?\s*[=:]\s*"?([A-Za-z][A-Za-z0-9_-]*)"?/i or return () 12 111 123
176 or return ();
177 12 ▶ my ($data) = $text =~ /\bData"?\s*[=:]\s*"?((?:0[xX])?[0-9A-Fa-f]+)"?/i
Branch: 1 branch
BranchTrueFalseTotal
my($data) = $text =~ /\bData"?\s*[=:]\s*"?((?:0[xX])?[0-9A-Fa-f]+)"?/i or return () 12 0 12
178 or return ();
179 12 my ($data_lsb) = $text =~ /\bDataLSB"?\s*[=:]\s*"?((?:0[xX])?[0-9A-Fa-f]+)"?/i;
180 12 return ($proto, $data, $data_lsb);
181 }
182  
183 # Import a structured record's fields through the named protocol, mirroring
184 # the JS decodeRecord routing. Tasmota's "Data" is IRremoteESP8266's decoded
185 # value and "DataLSB" the per-byte bit reversal Tasmota computes from it; for
186 # the protocols that transmit each byte LSB-first (NEC, JVC, SAMSUNG) that
187 # reversal is the accumulated form decode_raw reads, so DataLSB wins, while
188 # for whole-word MSB-first protocols (SAMSUNG36) Data itself is the
189 # accumulated form. Returns undef for an unregistered protocol name.
190 12 ▶sub _import_structured {
Subroutine _import_structured: called CC=5 SCAR=16.1
191 12 my ($class, $proto, $data, $data_lsb, $registry) = @_;
192 12 my $proto_class = $registry->get_protocol($proto);
193 12 ▶ return undef unless $proto_class;
Branch: 1 branch
BranchTrueFalseTotal
unless $proto_class 0 12 12
194  
195 12 my $lsb_is_accumulated = 1;
196 12 ▶ $lsb_is_accumulated = $proto_class->lsb_is_accumulated
Branch: 1 branch
BranchTrueFalseTotal
if $proto_class->can("lsb_is_accumulated") 11 1 12
197 if $proto_class->can('lsb_is_accumulated');
198  
199 12 ▶ if ($lsb_is_accumulated && defined $data_lsb) {
Branch: 1 branch
BranchTrueFalseTotal
if ($lsb_is_accumulated and defined $data_lsb) 2 10 12
Condition100%3/3
$lsb_is_accumulated and defined $data_lsb
!ll&&!rl&&r
462
MC/DC100%2/2
$lsb_is_accumulated and defined $data_lsb
$lsb_is_accumulated defined $data_lsb
Truth table100%3/3
A and B
A= $lsb_is_accumulatedB= defined $data_lsb
ABresult
0X0
100
111
200 2 return $registry->import_lsb($proto, $data_lsb);
201 }
202 10 return $registry->import_msb($proto, $data);
203 }
204  
205 # The button-name substitute used when a capture has no known key name: the
206 # code's "Data" hex value, formatted like Tasmota's Data field (0x030C for a
207 # 16-bit JVC frame, 0x10EF00FF for a 32-bit NEC frame, the full word for the
208 # wide protocols). A wig built from these gets editable button names the
209 # user can correct after importing it in HAIR.
210 114 ▶sub _data_alias {
Subroutine _data_alias: called CC=2 SCAR=7.1
211 114 my ($code) = @_;
212 114 ▶ return '' unless defined $code->data;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $code->data 0 114 114
213 114 return Protocol::IR::Code::_data_hex($code->data);
214 }
215  
216 # Split a Tasmota console dump into its individual signals and decode each.
217 #
218 # A dump is one or more IRrecv lines, each either protocol-structured
219 # ("Protocol = NEC, Bits = 32, Data = 0x...") or raw timing data
220 # ("RawData = +...-..." compact, or "RawData = 9,9,..." comma), plus
221 # "IRsend <freq>,<rawdata>" command lines; each line is one signal.
222 # Timestamps and other log noise are ignored. Signals that fail to decode
223 # to a registered protocol -- an unsupported protocol name, or raw data
224 # that matches no protocol -- are dropped. Every decoded signal gets a
225 # data-hex alias (see _data_alias) so a wig built from the result has
226 # editable button names.
227 3 ▶sub decode_dump {
Subroutine decode_dump: called CC=15 SCAR=31.5
Pod documented
228 3 my ($class, $input, $registry) = @_;
229 3 ▶ die "No Tasmota dump provided\n" unless defined $input;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $input 0 3 3
230  
231 3 ▶ my $text = ref $input eq 'ARRAY' ? join("\n", @$input) : $input;
Branch: 1 branch
BranchTrueFalseTotal
ref $input eq 'ARRAY' ? : 0 3 3
232 3 my @codes;
233 3 for my $line (split /\r?\n/, $text) {
234 9 $line =~ s/^\s+|\s+$//g;
235 9 ▶ next unless $line;
Branch: 1 branch
BranchTrueFalseTotal
unless $line 0 9 9
236  
237 9 my $signal;
238 9 ▶ if ($line =~ /\bProtocol\s*[=:]/i) {
Branch: 3 branches
BranchTrueFalseTotal
if ($line =~ /\bProtocol\s*[=:]/i) { } 4 5 9
elsif ($line =~ /\bRawData\s*=\s*(.+)$/i) { } 3 2 5
elsif ($line =~ /^IRsend\b/i) { } 1 1 2
239 4 $signal = $line;
240 } elsif ($line =~ /\bRawData\s*=\s*(.+)$/i) {
241 3 $signal = $1;
242 } elsif ($line =~ /^IRsend\b/i) {
243 1 $signal = $line;
244 } else {
245 1 next;
246 }
247  
248 8 my $code;
249 8 ▶ eval { $code = $class->decode($signal, $registry)->[0]; 1 }
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $code = $class->decode($signal, $registry)->[0]; 1 } } or next 8 0 8
250 or next;
251 8 ▶ next unless $code && $code->protocol ne 'UNKNOWN';
Branch: 1 branch
BranchTrueFalseTotal
unless $code and $code->protocol ne "UNKNOWN" 2 6 8
Condition66%2/3
$code and $code->protocol ne "UNKNOWN"
!ll&&!rl&&r
026
MC/DC50%1/2
$code and $code->protocol ne "UNKNOWN"
$code $code->protocol ne "UNKNOWN"
Truth table66%2/3
A and B
A= $codeB= $code->protocol ne "UNKNOWN"
ABresult
0X0
100
111
252 # A structured MWM record's Data field can carry a whole A+B+A' bundle
253 # (a "capture"), which Tasmota logged as one value -- unwrap it through
254 # the protocol's optional unbundle capability so every frame of the
255 # bundle is yielded as its own code.
256 6 ▶ if ($code->protocol eq 'MWM') {
Branch: 1 branch
BranchTrueFalseTotal
if ($code->protocol eq "MWM") 0 6 6
257 0 my $proto_class = $registry->get_protocol('MWM');
258 0 ▶ my $bundled = $proto_class && $proto_class->can('unbundle')
Branch: 1 branch
BranchTrueFalseTotal
$proto_class && $proto_class->can('unbundle') ? : 0 0 0
Condition0%0/3
$proto_class and $proto_class->can("unbundle")
!ll&&!rl&&r
000
MC/DC0%0/2
$proto_class and $proto_class->can("unbundle")
$proto_class $proto_class->can("unbundle")
Truth table0%0/3
A and B
A= $proto_classB= $proto_class->can("unbundle")
ABresult
0X0
100
111
259 ? $proto_class->unbundle($code->data)
260 : [];
261 0 ▶ if (@$bundled >= 2) { push @codes, @$bundled; next; }
Branch: 1 branch
BranchTrueFalseTotal
if (@$bundled >= 2) 0 0 0
262 }
263 6 push @codes, $code;
264 }
265 3 return \@codes;
266 }
267  
268 38 ▶sub export {
Subroutine export: called CC=9 SCAR=22.3
Pod documented
269 38 my ($class, $ir_code, $registry, %opts) = @_;
270 38 ▶ if (ref $ir_code eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $ir_code eq "ARRAY") 0 38 38
271 0 ▶ die "Tasmota IRSend can only export a single signal\n" unless @$ir_code;
Branch: 1 branch
BranchTrueFalseTotal
unless @$ir_code 0 0 0
272 0 $ir_code = $ir_code->[0];
273 }
274  
275 38 ▶ my $style = lc($opts{style} // 'compact');
Condition100%2/2
$opts{"style"} // "compact"
l!l
1820
MC/DC100%1/1
$opts{"style"} // "compact"
$opts{"style"}
Truth table100%2/2
A
A= $opts{"style"}
Aresult
11
00
276 38 ▶ die "Unsupported Tasmota export style: $style\n"
Branch: 1 branch
BranchTrueFalseTotal
unless $style eq "compact" or $style eq "comma" 1 37 38
Condition100%3/3
$style eq "compact" or $style eq "comma"
l!l&&r!l&&!r
20171
MC/DC100%2/2
$style eq "compact" or $style eq "comma"
$style eq "compact" $style eq "comma"
Truth table100%3/3
A or B
A= $style eq "compact"B= $style eq "comma"
ABresult
1X1
011
000
277 unless $style eq 'compact' || $style eq 'comma';
278  
279 37 my $timings = $ir_code->timings;
280 37 ▶ $timings ||= _timings_from_code($ir_code, $registry);
Condition100%3/3
$timings ||= _timings_from_code($ir_code, $registry)
l!l&&r!l&&!r
3241
MC/DC100%2/2
$timings ||= _timings_from_code($ir_code, $registry)
$timings _timings_from_code($ir_code, $registry)
Truth table100%3/3
A ||= B
A= $timingsB= _timings_from_code($ir_code, $registry)
ABresult
1X1
011
000
281  
282 36 ▶ my $freq = defined $opts{frequency} ? $opts{frequency} + 0 : 0;
Branch: 1 branch
BranchTrueFalseTotal
defined $opts{'frequency'} ? : 1 35 36
283 36 my $data;
284 36 ▶ if ($style eq 'compact') {
Branch: 1 branch
BranchTrueFalseTotal
if ($style eq 'compact') { } 19 17 36
285 19 $data = _encode_compact($timings);
286 } else {
287 17 $data = join(',', map { int(abs($_) + 0.5) } @$timings);
288 }
289 36 return "IRSend $freq,$data";
290 }
291  
292 # Derive a flat signed timing list for a fresh Protocol::IR::Code by round-tripping
293 # through the registered protocol encoder's Pronto output.
294 5 ▶sub _timings_from_code {
Subroutine _timings_from_code: called CC=4 SCAR=13.9
295 5 my ($ir_code, $registry) = @_;
296 5 my $pronto = $registry->export_code($ir_code, 'Pronto');
297 4 $pronto =~ s/^\s+|\s+$//g;
298 4 my @tokens = split /\s+/, $pronto;
299 4 ▶ die "Cannot derive Tasmota timings from protocol encoder output\n"
Branch: 1 branch
BranchTrueFalseTotal
if scalar @tokens < 4 0 4 4
300 if scalar(@tokens) < 4;
301  
302 4 my $freq_word = hex($tokens[1]);
303 4 ▶ my $carrier = ($freq_word > 0) ? int(1000000.0 / ($freq_word * 0.241246)) : 38000;
Branch: 1 branch
BranchTrueFalseTotal
$freq_word > 0 ? : 4 0 4
304 4 my $period = 1000000.0 / $carrier;
305  
306 4 my $pair_count = hex($tokens[2]) + hex($tokens[3]);
307 4 my @flat;
308 4 ▶ for (my $i = 0; $i < $pair_count; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $pair_count) 120 4 124
309 120 my $idx = 4 + ($i * 2);
310 120 my $mark = hex($tokens[$idx]) * $period;
311 120 my $space = hex($tokens[$idx + 1]) * $period;
312 120 push @flat, int($mark + 0.5), -int($space + 0.5);
313 }
314 4 return \@flat;
315 }
316  
317 55 1;
318  
319 =head1 NAME
320  
321 Protocol::IR::Format::Tasmota - Tasmota RawData import and IRSend export
322  
323 =head1 VERSION
324  
325 version 1.5
326  
327 =head1 SYNOPSIS
328  
329 use Protocol::IR::Converter;
330  
331 my $converter = Protocol::IR::Converter->new();
332  
333 # Decode a Tasmota IR log capture (compact letter-coded RawData)
334 my $capture = "+9185-4490+650-500+655dE-1630C-505+630-525Ed...";
335 my $code = $converter->import_format('Tasmota', $capture)->[0];
336 print $code->protocol; # NEC
337 print $code->data; # Tasmota DataLSB value
338  
339 # Re-export as a Tasmota IRSend command (comma style with carrier)
340 my $irsend = $converter->export_code($code, 'Tasmota',
341 style => 'comma', frequency => 38000);
342 # IRSend 38000,9185,4490,650,...
343  
344 # Compact letter-coded style is the default
345 my $compact = $converter->export_code($code, 'Tasmota');
346 # IRSend 0,+9185-4490+650-500+655dE-...
347  
348 =head1 DESCRIPTION
349  
350 C<Protocol::IR::Format::Tasmota> imports Tasmota "RawData" IR captures and exports
351 Tasmota C<IRSend> commands.
352  
353 =over 4
354  
355 =item Example RawData captures (a NEC transmission)
356  
357 Compact letter-coded form (as logged by Tasmota's IRrecv):
358  
359 +9185-4490+650-500+655dE-1630C-505+630-525Ed...
360  
361 Comma-separated mark/space form:
362  
363 +9185,-4490,+650,-500,+655,...
364  
365 =back
366  
367 Tasmota renders IR captures with the IRremoteESP8266 library
368 (L<https://github.com/crankyoldgit/IRremoteESP8266>); full command
369 documentation lives in the Tasmota manual
370 (L<https://tasmota.github.io/docs/Tasmota-IR/>).
371  
372 =head2 Decode input forms
373  
374 C<decode> accepts the content of a Tasmota C<RawData> field in any of these
375 forms:
376  
377 =over 4
378  
379 =item * The compact letter-compressed form
380 (C<+8570-4240+550-1580C-510+565-1565F-505Fh...>). Each new timing magnitude
381 gets the next letter (C<A>-C<Z>) in order of first appearance; a repeated
382 value is written as its letter, uppercase for a mark and lowercase for a
383 space. Magnitudes are multiples of 5 microseconds. Values beyond the 26
384 letter table are written numerically every time they occur.
385  
386 =item * A plain comma-separated mark/space list (C<926,844,958,...>).
387  
388 =item * A full C<IRSend E<lt>freqE<gt>,E<lt>rawdataE<gt>> command line.
389  
390 =item * An arrayref of integer timings, alternating mark, space, mark, ...
391  
392 =item * A protocol-structured IRrecv line
393 (C<IRrecv: Protocol = NEC, Bits = 32, Data = 0x10EF00FF>), possibly with a
394 timestamp prefix. The line imports through the named protocol rather than
395 by timing decode.
396  
397 =back
398  
399 The timings are decoded to microsecond mark/space pairs and tried against
400 every registered protocol. The resulting L<Protocol::IR::Code> keeps the raw timings
401 on the C<timings> accessor so the signal can be re-exported losslessly,
402 even when no protocol matches (the code's protocol is then C<UNKNOWN>).
403  
404 A protocol-structured line and a raw-data signal that decodes to a known
405 protocol get their C<alias> set to the signal's Data hex value (e.g.
406 C<0x10EF00FF>, width-matched to the protocol's bit count), so a wig built
407 from them carries editable button names.
408  
409 =head2 Decoding a full console dump
410  
411 C<decode> treats input containing newlines as a Tasmota console dump and
412 splits it into one signal per line. Each line may be protocol-structured,
413 a C<RawData> line (compact or comma form), or an C<IRsend> command line;
414 timestamps and other log noise are ignored. Lines that fail to decode to a
415 registered protocol are dropped. C<decode_dump> performs the same split
416 explicitly.
417  
418 =head2 Export forms
419  
420 C<export> produces a Tasmota C<IRSend> command:
421  
422 IRSend <frequency>,<rawdata>
423  
424 The C<rawdata> is the compact letter-coded form by default, or the
425 comma-separated mark/space list with C<style =E<gt> 'comma'>. When the code
426 was decoded from Tasmota data its original timings are reused; otherwise
427 they are derived by round-tripping the code through the protocol encoder's
428 Pronto output, so the emitted timings match the other timing formats
429 exactly.
430  
431 =head1 METHODS
432  
433 =head2 decode
434  
435 my $codes = $class->decode($input, $registry);
436  
437 Returns an arrayref containing one L<Protocol::IR::Code> per captured signal. A
438 single Tasmota RawData string or protocol-structured line yields a single
439 code; multi-line input is split into one code per signal (see
440 L</"Decoding a full console dump">).
441  
442 =head2 decode_dump
443  
444 my $codes = $class->decode_dump($dump_text, $registry);
445  
446 Splits a Tasmota console dump (any mix of protocol-structured, C<RawData>,
447 and C<IRsend> lines) into one L<Protocol::IR::Code> per decodable signal. Lines that
448 do not decode to a registered protocol are dropped; every returned code
449 carries a Data-hex alias.
450  
451 =head2 export
452  
453 my $cmd = $class->export($ir_code, $registry, %opts);
454  
455 Serializes an L<Protocol::IR::Code> (or a single-element arrayref) into an C<IRSend>
456 command. Options:
457  
458 =over 4
459  
460 =item * C<style> -- C<'compact'> (default) or C<'comma'>.
461  
462 =item * C<frequency> -- carrier frequency in Hz to embed in the command
463 (default C<0>).
464  
465 =back
466  
467 =head1 SUPPORT
468  
469 Source code: L<https://github.com/bwarden/perl-protocol-ir>
470  
471 Bug reports and feature requests: L<https://github.com/bwarden/perl-protocol-ir/issues>
472  
473 =head1 AUTHOR
474  
475 Brett T. Warden <bwarden@cpan.org>
476  
477 =head1 COPYRIGHT AND LICENSE
478  
479 Copyright (c) 2026 Brett T. Warden
480  
481 This library is free software; you can redistribute it and/or modify it
482 under the terms of the GNU General Public License as published by the
483 Free Software Foundation; either version 2 of the License, or (at your
484 option) any later version.
« blib/lib/Protocol/IR/Proto/MWM.pm ↑ Summary blib/lib/Protocol/IR/Proto/Sony.pm »