blib/lib/Protocol/IR/Proto/SAMSUNG36.pm

filter: Statementstmt 100% 89 / 89 Branchbran 88.4% 23 / 26 Conditioncond 48.6% 18 / 37 MC/DCmcdc 12.5% 2 / 16 Subroutinesub 100% 15 / 15 Podpod 83.3% 5 / 6 totaltotal 80.4% 152 / 189 CC 30 SCAR 24.6
CC
30
Cov
86%
decode_timing
24.1
decode_params
6.1
to_pronto
5.0
CRAP 11.7
« blib/lib/Protocol/IR/Proto/RC5.pm ↑ Summary blib/lib/Protocol/IR/Proto/MWM.pm »
1 package Protocol::IR::Proto::SAMSUNG36;
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 55 ▶use Protocol::IR::Code;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 55 ▶use Math::BigInt;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 # Protocol::IR::Proto::SAMSUNG36 is the SAMSUNG36 protocol handler (36-bit),
10 # ported from IRremoteESP8266 (Copyright David Conran et al., GPLv2,
11 # https://github.com/crankyoldgit/IRremoteESP8266, ir_Samsung.cpp
12 # sendSamsung36/decodeSamsung36).
13 #
14 # The 36-bit data word is transmitted in two blocks. Block #1 carries the top
15 # 16 bits (the address) behind a 4515/4438 µs header; block #2 carries the
16 # remaining 20 bits (the command) with no header. Each block is sent MSB-first
17 # with a 512 µs mark and a 1468 µs (1) / 490 µs (0) space, and each block ends
18 # with a 512 µs mark; block #1's footer is followed by a 4438 µs space and
19 # block #2's by the ~27 ms inter-message gap. The decoder reads both blocks
20 # MSB-first, so the stored data word is the same value the transmitter sent -
21 # what IRremoteESP8266 reports as `value`, the "Code" column of the sample
22 # tables. The "LSB" column is the same word read bit-for-bit in reverse.
23  
24 55 my $BITS = 36;
25 55 my $ADDR_BITS = 16; # block #1
26 55 my $CMD_BITS = $BITS - $ADDR_BITS; # block #2 (20)
27 55 my $BLOCK1_SHIFT = 1 << $CMD_BITS; # 2^20: block #1 value is data / this
28  
29 39 ▶sub _parse_int {
Subroutine _parse_int: called CC=3 SCAR=11.0
30 39 my ($val) = @_;
31 39 ▶ return 0 unless defined $val;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $val 0 39 39
32 39 $val =~ s/^\s+|\s+$//g;
33 39 ▶ if ($val =~ /^0x/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($val =~ /^0x/i) 5 34 39
34 # 36-bit hex strings exceed the 32-bit range that triggers Perl's
35 # "portable" warning; the modules here handle them by design.
36 55 ▶ no warnings 'portable';
Subroutine BEGIN: called CC=1 SCAR=0.0
37 5 return Math::BigInt->from_hex($val);
38 }
39 34 return $val + 0;
40 }
41  
42 # The 36-bit word is transmitted MSB-first as a whole, so Tasmota's "Data"
43 # field is itself the accumulated form decode_raw reads. Tasmota's "DataLSB"
44 # (the per-byte bit reversal of the low 32 bits) is a display artifact that
45 # cannot reconstruct the transmitted value, so Data is preferred.
46 1 ▶sub lsb_is_accumulated { 0 }
Subroutine lsb_is_accumulated: called CC=1 SCAR=0.0
Pod undocumented
47  
48 # Reverse the full 36-bit value end to end (not per byte, as for
49 # NEC/JVC/SAMSUNG): the "LSB" column is the same word read bit-for-bit in
50 # reverse. Each `out * 2 + bit` step shifts earlier bits up, so feeding the
51 # original bits LSB-first builds the reversed word MSB-first.
52 1 ▶sub _reverse_bits {
Subroutine _reverse_bits: called CC=2 SCAR=6.9
53 1 my ($val, $bits) = @_;
54 1 my $out = 0;
55 1 for my $i (0 .. $bits - 1) {
56 36 $out = $out * 2 + (($val >> $i) & 1);
57 }
58 1 return $out;
59 }
60  
61 # Pack the 16-bit address (block #1) and the 20-bit command (block #2) into
62 # the transmitted 36-bit word.
63 34 ▶sub _encode_data {
Subroutine _encode_data: called CC=1 SCAR=0.0
64 34 my ($addr, $cmd) = @_;
65 34 return (Math::BigInt->new($addr & 0xFFFF) * Math::BigInt->new($BLOCK1_SHIFT)) + Math::BigInt->new($cmd & ($BLOCK1_SHIFT - 1));
66 }
67  
68 # decode_raw expects the display form (the "Code" column / IRremoteESP8266
69 # `value`): the word read MSB-first, address in the top 16 bits.
70 3 ▶sub decode_raw {
Subroutine decode_raw: called CC=1 SCAR=0.0
Pod documented
71 3 my ($class, $raw_val) = @_;
72 3 my $val = _parse_int($raw_val);
73  
74 3 return Protocol::IR::Code->new(
75 protocol => 'SAMSUNG36',
76 bits => $BITS,
77 address => int($val / $BLOCK1_SHIFT) & 0xFFFF,
78 subaddress => -1,
79 command => $val % $BLOCK1_SHIFT,
80 data => $val,
81 );
82 }
83  
84 # The LSB form is the same word reversed end to end, so it has to be reversed
85 # (not per-byte, as for NEC/JVC/SAMSUNG) to reach the display form decode_raw
86 # reads.
87 2 ▶sub decode_byte_order {
Subroutine decode_byte_order: called CC=2 SCAR=6.9
Pod documented
88 2 my ($class, $raw_val, $lsb) = @_;
89 2 my $val = _parse_int($raw_val);
90 2 ▶ return $class->decode_raw($lsb ? _reverse_bits($val, $BITS) : $val);
Branch: 1 branch
BranchTrueFalseTotal
$lsb ? : 1 1 2
91 }
92  
93 17 ▶sub decode_params {
Subroutine decode_params: called CC=5 SCAR=18.1
Pod documented
94 17 my ($class, %args) = @_;
95 17 ▶ my $addr = _parse_int($args{address} // $args{device} // 0);
Condition60%3/5
($args{"address"} // $args{"device"}) // 0
l!l
170
$args{"address"} // $args{"device"}
l!l&&r!l&&!r
1610
MC/DC0%0/2
($args{"address"} // $args{"device"}) // 0
$args{"address"} $args{"device"}
Truth table66%2/3
A // B
A= $args{"address"}B= $args{"device"}
ABresult
1X1
011
000
96 17 ▶ my $cmd = _parse_int($args{command} // $args{function} // 0);
Condition40%2/5
($args{"command"} // $args{"function"}) // 0
l!l
170
$args{"command"} // $args{"function"}
l!l&&r!l&&!r
1700
MC/DC0%0/2
($args{"command"} // $args{"function"}) // 0
$args{"command"} $args{"function"}
Truth table33%1/3
A // B
A= $args{"command"}B= $args{"function"}
ABresult
1X1
011
000
97  
98 17 return Protocol::IR::Code->new(
99 protocol => 'SAMSUNG36',
100 bits => $BITS,
101 address => $addr,
102 subaddress => -1,
103 command => $cmd,
104 data => _encode_data($addr, $cmd),
105 );
106 }
107  
108 # Decode microsecond timing pairs into Protocol::IR::Code. Frame layout:
109 # [header 4515/4438] [16 addr bits] [mark/4438] [20 cmd bits] [mark/26880]
110 # so 1 + 16 + 1 + 20 + 1 = 39 burst pairs. A capture may end on a bare
111 # trailing mark, in which case the final space reads as 0.
112 377 ▶sub decode_timing {
Subroutine decode_timing: called CC=18 SCAR=31.8
Pod documented
113 377 my ($class, $burst_pairs) = @_;
114 377 ▶ return undef unless scalar(@$burst_pairs) >= 39;
Branch: 1 branch
BranchTrueFalseTotal
unless scalar @$burst_pairs >= 39 247 130 377
115  
116 130 my ($hdr_mark, $hdr_space) = @{$burst_pairs->[0]};
117 130 ▶ return undef unless ($hdr_mark >= 3800 && $hdr_mark <= 5200) &&
Branch: 1 branch
BranchTrueFalseTotal
unless $hdr_mark >= 3800 and $hdr_mark <= 5200 and ($hdr_space >= 3800 and $hdr_space <= 5200) 60 70 130
Condition66%6/9
$hdr_mark >= 3800 and $hdr_mark <= 5200 and ($hdr_space >= 3800 and $hdr_space <= 5200)
!ll&&!rl&&r
60070
$hdr_mark >= 3800 and $hdr_mark <= 5200
!ll&&!rl&&r
54670
$hdr_space >= 3800 and $hdr_space <= 5200
!ll&&!rl&&r
0070
MC/DC50%2/4
$hdr_mark >= 3800 and $hdr_mark <= 5200 and ($hdr_space >= 3800 and $hdr_space <= 5200)
$hdr_mark >= 3800 $hdr_mark <= 5200 $hdr_space >= 3800 $hdr_space <= 5200
Truth table60%3/5
A and B and C and D
A= $hdr_mark >= 3800B= $hdr_mark <= 5200C= $hdr_space >= 3800D= $hdr_space <= 5200
ABCDresult
0XXX0
10XX0
110X0
11100
11111
118 ($hdr_space >= 3800 && $hdr_space <= 5200);
119  
120 70 my $block1 = 0;
121 70 for my $i (0 .. $ADDR_BITS - 1) {
122 1120 my $space = $burst_pairs->[$i + 1]->[1];
123 # Space ~1468 µs = 1, ~490 µs = 0
124 1120 ▶ $block1 = $block1 * 2 + (($space > 1000) ? 1 : 0);
Branch: 1 branch
BranchTrueFalseTotal
$space > 1000 ? : 214 906 1120
125 }
126  
127 # Mid-block footer: a 512 µs mark and the 4438 µs space separating the
128 # two blocks.
129 70 my ($mid_mark, $mid_space) = @{$burst_pairs->[$ADDR_BITS + 1]};
130 70 ▶ return undef unless ($mid_mark >= 400 && $mid_mark <= 900) &&
Branch: 1 branch
BranchTrueFalseTotal
unless $mid_mark >= 400 and $mid_mark <= 900 and ($mid_space >= 3800 and $mid_space <= 5200) 0 70 70
Condition33%3/9
$mid_mark >= 400 and $mid_mark <= 900 and ($mid_space >= 3800 and $mid_space <= 5200)
!ll&&!rl&&r
0070
$mid_mark >= 400 and $mid_mark <= 900
!ll&&!rl&&r
0070
$mid_space >= 3800 and $mid_space <= 5200
!ll&&!rl&&r
0070
MC/DC0%0/4
$mid_mark >= 400 and $mid_mark <= 900 and ($mid_space >= 3800 and $mid_space <= 5200)
$mid_mark >= 400 $mid_mark <= 900 $mid_space >= 3800 $mid_space <= 5200
Truth table20%1/5
A and B and C and D
A= $mid_mark >= 400B= $mid_mark <= 900C= $mid_space >= 3800D= $mid_space <= 5200
ABCDresult
0XXX0
10XX0
110X0
11100
11111
131 ($mid_space >= 3800 && $mid_space <= 5200);
132  
133 70 my $block2 = 0;
134 70 for my $i (0 .. $CMD_BITS - 1) {
135 1400 my $space = $burst_pairs->[$i + $ADDR_BITS + 2]->[1];
136 1400 ▶ $block2 = $block2 * 2 + (($space > 1000) ? 1 : 0);
Branch: 1 branch
BranchTrueFalseTotal
$space > 1000 ? : 786 614 1400
137 }
138  
139 # Stop bit: a short mark followed by the ~27ms inter-message gap. A
140 # capture may end on a bare trailing mark with no space.
141 70 my ($stop_mark, $stop_space) = @{$burst_pairs->[$ADDR_BITS + $CMD_BITS + 2]};
142 70 ▶ return undef unless ($stop_mark >= 400 && $stop_mark <= 900) &&
Branch: 1 branch
BranchTrueFalseTotal
unless $stop_mark >= 400 and $stop_mark <= 900 and $stop_space == 0 || $stop_space >= 8000 0 70 70
Condition44%4/9
$stop_mark >= 400 and $stop_mark <= 900 and $stop_space == 0 || $stop_space >= 8000
!ll&&!rl&&r
0070
$stop_mark >= 400 and $stop_mark <= 900
!ll&&!rl&&r
0070
$stop_space == 0 or $stop_space >= 8000
l!l&&r!l&&!r
1690
MC/DC0%0/4
$stop_mark >= 400 and $stop_mark <= 900 and $stop_space == 0 || $stop_space >= 8000
$stop_mark >= 400 $stop_mark <= 900 $stop_space == 0 $stop_space >= 8000
Truth table40%2/5
A and B and C or D
A= $stop_mark >= 400B= $stop_mark <= 900C= $stop_space == 0D= $stop_space >= 8000
ABCDresult
0XXX0
10XX0
11000
111X1
11011
143 ($stop_space == 0 || $stop_space >= 8000);
144  
145 70 my $data = ($block1 * $BLOCK1_SHIFT) + $block2;
146  
147 70 return Protocol::IR::Code->new(
148 protocol => 'SAMSUNG36',
149 bits => $BITS,
150 address => $block1,
151 subaddress => -1,
152 command => $block2,
153 data => $data,
154 );
155 }
156  
157 17 ▶sub to_pronto {
Subroutine to_pronto: called CC=5 SCAR=16.1
Pod documented
158 17 my ($class, $code) = @_;
159 17 my $carrier_hz = 38000;
160  
161 # Build the pulse conversion period from the rounded Pronto frequency
162 # word, as MakeHex and Pronto parsers do (see Protocol::IR::Proto::NEC).
163 17 my $freq_word = int(sprintf("%.0f", 1000000.0 / ($carrier_hz * 0.241246)));
164 17 my $period_us = $freq_word * 0.241246;
165  
166 17 ▶ my $us_to_pulses = sub { int(sprintf("%.0f", $_[0] / $period_us)) };
Subroutine __ANON__: called CC=1 SCAR=0.0
167  
168 17 my $data = _encode_data($code->address, $code->command);
169 17 my $block1 = int($data / $BLOCK1_SHIFT) & 0xFFFF;
170 17 my $block2 = $data % $BLOCK1_SHIFT;
171  
172 17 my @burst_pairs = (
173 [$us_to_pulses->(4515), $us_to_pulses->(4438)]
174 );
175  
176 17 ▶ for (my $i = $ADDR_BITS - 1; $i >= 0; $i--) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i >= 0) 272 17 289
177 272 my $bit = ($block1 >> $i) & 1;
178 272 ▶ push @burst_pairs, [
Branch: 1 branch
BranchTrueFalseTotal
$bit ? : 68 204 272
179 $us_to_pulses->(512),
180 $us_to_pulses->($bit ? 1468 : 490)
181 ];
182 }
183  
184 17 push @burst_pairs, [$us_to_pulses->(512), $us_to_pulses->(4438)];
185  
186 17 ▶ for (my $i = $CMD_BITS - 1; $i >= 0; $i--) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i >= 0) 340 17 357
187 340 my $bit = ($block2 >> $i) & 1;
188 340 ▶ push @burst_pairs, [
Branch: 1 branch
BranchTrueFalseTotal
$bit ? : 191 149 340
189 $us_to_pulses->(512),
190 $us_to_pulses->($bit ? 1468 : 490)
191 ];
192 }
193  
194 # The trailing mark and ~27ms inter-message gap.
195 17 push @burst_pairs, [$us_to_pulses->(512), $us_to_pulses->(26880)];
196  
197 17 my $seq1_pairs = scalar @burst_pairs;
198  
199 17 my $header = sprintf("0000 %04X %04X 0000", $freq_word, $seq1_pairs);
200 17 my @payload = map { sprintf("%04X %04X", $_->[0], $_->[1]) } @burst_pairs;
201  
202 17 return join(" ", $header, @payload);
203 }
204  
205 55 1;
206  
207 =encoding utf8
208  
209 =head1 NAME
210  
211 Protocol::IR::Proto::SAMSUNG36 - SAMSUNG36 protocol handler (36-bit)
212  
213 =head1 VERSION
214  
215 version 1.5
216  
217 =head1 SYNOPSIS
218  
219 use Protocol::IR::Converter;
220  
221 my $converter = Protocol::IR::Converter->new();
222  
223 # From a raw 36-bit value or from parameters
224 my $code = $converter->import_code('SAMSUNG36', '0x7004F023E3');
225 my $code = $converter->import_code('SAMSUNG36',
226 { address => 0x7004, command => 0xF023E });
227  
228 =head1 DESCRIPTION
229  
230 The C<SAMSUNG36> protocol transmits 36-bit frames at B<38 kHz> in two
231 blocks: block #1 carries the top 16 bits (the B<address>) behind a
232 B<4515/4438 µs> header, and block #2 carries the remaining 20 bits (the
233 B<command>) with no header. Each block is sent B<MSB-first> with a 512 µs
234 mark and a 1468 µs (1) / 490 µs (0) space, and each block ends with a 512 µs
235 mark; block #1's footer is followed by a 4438 µs space and block #2's by the
236 ~27 ms inter-message gap. The whole frame is sent once.
237  
238 Because the word is transmitted MSB-first as a whole, the stored C<data> is
239 the same value the transmitter sent -- what IRremoteESP8266 reports as
240 C<value>, the "Code" column of the sample tables. The "LSB" column is the
241 same word read bit-for-bit in reverse; see C<decode_byte_order>. SAMSUNG36
242 has no subaddress; C<subaddress> is set to C<-1>.
243  
244 Timing and bit ordering follow IRremoteESP8266 (David Conran et al., GPLv2,
245 L<https://github.com/crankyoldgit/IRremoteESP8266>): C<kSamsung36HdrMark>/
246 C<kSamsung36HdrSpace>, C<kSamsung36BitMark>, C<kSamsung36OneSpace>, and
247 C<kSamsung36ZeroSpace>.
248  
249 =head1 METHODS
250  
251 =head2 decode_raw
252  
253 my $code = $class->decode_raw('0x7004F023E3');
254  
255 Builds an L<Protocol::IR::Code> from the raw 36-bit display value.
256  
257 =head2 decode_byte_order
258  
259 my $code = $class->decode_byte_order($raw, $lsb);
260  
261 Decodes from either byte order: C<$lsb> true reads the whole word reversed
262 end to end (the "LSB" column), false reads the display form (the "Code"
263 column) directly.
264  
265 =head2 decode_params
266  
267 my $code = $class->decode_params(address => 0x7004, command => 0xF023E);
268  
269 Builds an L<Protocol::IR::Code> from discrete parameters. Accepts C<address> or
270 C<device>, and C<command> or C<function>.
271  
272 =head2 decode_timing
273  
274 my $code = $class->decode_timing(\@burst_pairs_us);
275  
276 Decodes an arrayref of microsecond C<[mark_us, space_us]> pairs. Returns an
277 L<Protocol::IR::Code> when the header, 39 burst pairs, both block footers,
278 and the stop bit match the SAMSUNG36 timing signature, otherwise C<undef>.
279  
280 =head2 to_pronto
281  
282 my $pronto = $class->to_pronto($ir_code);
283  
284 Encodes an L<Protocol::IR::Code> object as a Pronto Hex string at 38 kHz.
285  
286 =head1 SUPPORT
287  
288 Source code: L<https://github.com/bwarden/perl-protocol-ir>
289  
290 Bug reports and feature requests: L<https://github.com/bwarden/perl-protocol-ir/issues>
291  
292 =head1 AUTHOR
293  
294 Brett T. Warden <bwarden@cpan.org>
295  
296 =head1 COPYRIGHT AND LICENSE
297  
298 Copyright (c) 2026 Brett T. Warden
299  
300 This library is free software; you can redistribute it and/or modify it
301 under the terms of the GNU General Public License as published by the
302 Free Software Foundation; either version 2 of the License, or (at your
303 option) any later version.
« blib/lib/Protocol/IR/Proto/RC5.pm ↑ Summary blib/lib/Protocol/IR/Proto/MWM.pm »