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

filter: Statementstmt 100% 84 / 84 Branchbran 95.0% 19 / 20 Conditioncond 64.2% 18 / 28 MC/DCmcdc 38.4% 5 / 13 Subroutinesub 100% 15 / 15 Podpod 100% 7 / 7 totaltotal 88.6% 148 / 167 CC 24 SCAR 18.5
CC
24
Cov
92%
decode_timing
13.2
decode_params
6.1
_parse_int
3.0
CRAP 6.3
« blib/lib/Protocol/IR/Proto/JVC.pm ↑ Summary blib/lib/Protocol/IR/Proto/Denon.pm »
1 package Protocol::IR::Proto::SAMSUNG;
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  
8 # Timing and bit ordering follow IRremoteESP8266 (Copyright David Conran et al.,
9 # GPLv2, https://github.com/crankyoldgit/IRremoteESP8266):
10 # kSamsungHdrMark/Space = 8 * 560 µs, kSamsungBitMark = 560 µs,
11 # kSamsungOneSpace = 3 * 560 µs, kSamsungZeroSpace = 560 µs, 32 bits.
12 # The 32-bit value is transmitted with the most significant byte first and
13 # each byte LSB-first within, so the value collected from a capture (and
14 # stored in ->data) matches Tasmota's DataLSB field. The customer (address)
15 # and command bytes are bit-reversed on the wire.
16  
17 822 ▶sub _parse_int {
Subroutine _parse_int: called CC=3 SCAR=11.0
18 822 my ($val) = @_;
19 822 ▶ return 0 unless defined $val;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $val 0 822 822
20 822 $val =~ s/^\s+|\s+$//g;
21 822 ▶ return hex($val) if $val =~ /^0x/i;
Branch: 1 branch
BranchTrueFalseTotal
if $val =~ /^0x/i 4 818 822
22 818 return $val + 0;
23 }
24  
25 1016 ▶sub _reverse_bits {
Subroutine _reverse_bits: called CC=2 SCAR=6.9
26 1016 my ($val) = @_;
27 1016 my $out = 0;
28 1016 for my $i (0 .. 7) {
29 8128 $out |= (($val >> $i) & 1) << (7 - $i);
30 }
31 1016 return $out;
32 }
33  
34 # Reverse the bits within each of the four bytes of a 32-bit value.
35 67 ▶sub _byte_reverse {
Subroutine _byte_reverse: called CC=2 SCAR=6.9
36 67 my ($val) = @_;
37 67 my $out = 0;
38 67 for my $i (0 .. 31) {
39 2144 my $byte = int($i / 8);
40 2144 my $bit = $i % 8;
41 2144 my $src = 8 * $byte + (7 - $bit);
42 2144 $out |= (($val >> $src) & 1) << $i;
43 }
44 67 return $out;
45 }
46  
47 # Pack customer (address) + command into the transmitted 32-bit value.
48 302 ▶sub _encode_data {
Subroutine _encode_data: called CC=1 SCAR=0.0
49 302 my ($addr, $cmd) = @_;
50 302 my $rev_customer = _reverse_bits($addr & 0xFF);
51 302 my $rev_command = _reverse_bits($cmd & 0xFF);
52 302 return (($rev_command ^ 0xFF) |
53 ($rev_command << 8) |
54 ($rev_customer << 16) |
55 ($rev_customer << 24));
56 }
57  
58 220 ▶sub decode_raw {
Subroutine decode_raw: called CC=1 SCAR=0.0
Pod documented
59 220 my ($class, $raw_val) = @_;
60 220 my $val = _parse_int($raw_val);
61 220 return $class->decode_params(
62 address => ($val >> 16) & 0xFF,
63 command => ($val >> 8) & 0xFF,
64 );
65 }
66  
67 # True when the value decode_raw reads (Tasmota DataLSB) is the accumulated
68 # per-byte LSB-first wire form; the display form (Tasmota Data) is its
69 # per-byte bit reversal.
70 1 ▶sub lsb_is_accumulated { 1 }
Subroutine lsb_is_accumulated: called CC=1 SCAR=0.0
Pod documented
71  
72 # lsb=true is the accumulated wire form (Tasmota DataLSB), which must be
73 # per-byte bit-reversed to reach the display form decode_raw reads (Tasmota
74 # Data, the hex LIRC and IRDB carry); lsb=false is that display form as-is.
75 # SAMSUNG's polarity is the reverse of NEC/JVC because the P-data displayed
76 # hexes (0xE0E040BF) are already the display form, not decode_raw's input.
77 130 ▶sub decode_byte_order {
Subroutine decode_byte_order: called CC=2 SCAR=6.9
Pod documented
78 130 my ($class, $raw_val, $lsb) = @_;
79 130 my $val = _parse_int($raw_val);
80 130 ▶ return $class->decode_raw($lsb ? _byte_reverse($val) : $val);
Branch: 1 branch
BranchTrueFalseTotal
$lsb ? : 1 129 130
81 }
82  
83 236 ▶sub decode_params {
Subroutine decode_params: called CC=5 SCAR=18.1
Pod documented
84 236 my ($class, %args) = @_;
85 236 ▶ my $addr = _parse_int($args{address} // $args{device} // 0) & 0xFF;
Condition60%3/5
($args{"address"} // $args{"device"}) // 0
l!l
2360
$args{"address"} // $args{"device"}
l!l&&r!l&&!r
23510
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
86 236 ▶ my $cmd = _parse_int($args{command} // $args{function} // 0) & 0xFF;
Condition40%2/5
($args{"command"} // $args{"function"}) // 0
l!l
2360
$args{"command"} // $args{"function"}
l!l&&r!l&&!r
23600
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
87  
88 236 return Protocol::IR::Code->new(
89 protocol => 'SAMSUNG',
90 bits => 32,
91 address => $addr,
92 subaddress => -1,
93 command => $cmd,
94 data => _encode_data($addr, $cmd),
95 );
96 }
97  
98 # Decode microsecond timing pairs into Protocol::IR::Code
99 586 ▶sub decode_timing {
Subroutine decode_timing: called CC=13 SCAR=25.8
Pod documented
100 586 my ($class, $burst_pairs) = @_;
101 586 ▶ return undef unless scalar(@$burst_pairs) >= 34; # Header + 32 bits + Stop
Branch: 1 branch
BranchTrueFalseTotal
unless scalar @$burst_pairs >= 34 241 345 586
102  
103 345 my ($hdr_mark, $hdr_space) = @{$burst_pairs->[0]};
104  
105 # SAMSUNG Header Check: ~4500 µs mark, ~4500 µs space
106 345 ▶ 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) 65 280 345
Condition66%6/9
$hdr_mark >= 3800 and $hdr_mark <= 5200 and ($hdr_space >= 3800 and $hdr_space <= 5200)
!ll&&!rl&&r
650280
$hdr_mark >= 3800 and $hdr_mark <= 5200
!ll&&!rl&&r
578280
$hdr_space >= 3800 and $hdr_space <= 5200
!ll&&!rl&&r
00280
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
107 ($hdr_space >= 3800 && $hdr_space <= 5200);
108  
109 280 my @bytes = (0, 0, 0, 0);
110 280 for my $i (0 .. 31) {
111 8960 my $space = $burst_pairs->[$i + 1]->[1];
112  
113 # Space ~1680 µs = 1, ~560 µs = 0
114 8960 ▶ my $bit = ($space > 1000) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$space > 1000 ? : 4115 4845 8960
115 8960 my $byte_idx = int($i / 8);
116 8960 my $bit_idx = $i % 8; # LSB-first
117  
118 8960 $bytes[$byte_idx] |= ($bit << $bit_idx);
119 }
120  
121 # Stop bit: a short mark followed by the long trailing space
122 280 my ($stop_mark, $stop_space) = @{$burst_pairs->[33]};
123 280 ▶ return undef unless ($stop_mark >= 350 && $stop_mark <= 900) &&
Branch: 1 branch
BranchTrueFalseTotal
unless $stop_mark >= 350 and $stop_mark <= 900 and $stop_space >= 3000 72 208 280
Condition83%5/6
$stop_mark >= 350 and $stop_mark <= 900 and $stop_space >= 3000
!ll&&!rl&&r
171208
$stop_mark >= 350 and $stop_mark <= 900
!ll&&!rl&&r
10279
MC/DC66%2/3
$stop_mark >= 350 and $stop_mark <= 900 and $stop_space >= 3000
$stop_mark >= 350 $stop_mark <= 900 $stop_space >= 3000
Truth table75%3/4
A and B and C
A= $stop_mark >= 350B= $stop_mark <= 900C= $stop_space >= 3000
ABCresult
0XX0
10X0
1100
1111
124 ($stop_space >= 3000);
125  
126 # Samsung sends the address byte twice and the command byte followed by
127 # its one's complement, so the frame bytes read back as addr, addr, cmd,
128 # ~cmd. Enforce that structure (matching IRremoteESP8266's strict
129 # decodeSAMSUNG compliance checks) so half-header NECx frames, which
130 # share the 4480/4480 µs header and per-byte LSB-first bit timing but
131 # carry a real subaddress byte instead of a repeated address, are not
132 # mislabelled as SAMSUNG. The reference decoder reports those as UNKNOWN.
133 208 ▶ return undef unless $bytes[0] == $bytes[1] &&
Branch: 1 branch
BranchTrueFalseTotal
unless $bytes[0] == $bytes[1] and $bytes[2] == (($bytes[3] ^ 255) & 255) 2 206 208
Condition66%2/3
$bytes[0] == $bytes[1] and $bytes[2] == (($bytes[3] ^ 255) & 255)
!ll&&!rl&&r
20206
MC/DC50%1/2
$bytes[0] == $bytes[1] and $bytes[2] == (($bytes[3] ^ 255) & 255)
$bytes[0] == $bytes[1] $bytes[2] == (($bytes[3] ^ 255) & 255)
Truth table66%2/3
A and B
A= $bytes[0] == $bytes[1]B= $bytes[2] == (($bytes[3] ^ 255) & 255)
ABresult
0X0
100
111
134 $bytes[2] == (($bytes[3] ^ 0xFF) & 0xFF);
135  
136 # Value is transmitted LSB-first per byte, so the first received byte is
137 # the most significant byte of the value (matching Tasmota's DataLSB).
138 206 my $data = ($bytes[0] << 24) | ($bytes[1] << 16) |
139 ($bytes[2] << 8) | $bytes[3];
140  
141 206 return Protocol::IR::Code->new(
142 protocol => 'SAMSUNG',
143 bits => 32,
144 address => _reverse_bits(($data >> 24) & 0xFF),
145 subaddress => -1,
146 command => _reverse_bits(($data >> 8) & 0xFF),
147 data => $data,
148 );
149 }
150  
151 # Convert a SAMSUNG code to its NECx2 equivalent parameters.
152 # Samsung address/command are the bit-reversal of the NECx2 device/subdevice
153 # and command bytes respectively, because Samsung transmits the same wire
154 # bitstream as NECX2 but labels the fields differently:
155 # SAMSUNG addr 0xE0 == NECx2 device 7 (reverse_byte(0xE0) = 0x07)
156 # SAMSUNG cmd 0x40 == NECx2 function 2 (reverse_byte(0x40) = 0x02)
157 # Returns a hashref suitable for import_code('NECX2', \%params).
158 #
159 # NOTE: IRDB labels these Samsung TV codes as NECX2 with the Samsung-native
160 # address and command values (device=7 is the Samsung addr 0xE0; function=2
161 # is the Samsung cmd 0x40). The data word is the same (0x070702FD), and
162 # import_code('NECX2', {device=>7, subdevice=>7, command=>2}) reproduces
163 # it. This method therefore returns the IRDB-compatible values (the
164 # bit-reversed form), not the raw Samsung values.
165 3 ▶sub as_necx2_params {
Subroutine as_necx2_params: called CC=1 SCAR=0.0
Pod documented
166 3 my ($self, $code) = @_;
167 3 my $device = Protocol::IR::Code::reverse_byte($code->address & 0xFF);
168 3 my $function = Protocol::IR::Code::reverse_byte($code->command & 0xFF);
169 return {
170 3 device => $device,
171 subdevice => $device,
172 command => $function,
173 };
174 }
175  
176 66 ▶sub to_pronto {
Subroutine to_pronto: called CC=3 SCAR=11.0
Pod documented
177 66 my ($class, $code) = @_;
178 66 my $carrier_hz = 38000;
179  
180 # Build the pulse conversion period from the rounded Pronto frequency
181 # word, as MakeHex and Pronto parsers do (see Protocol::IR::Proto::NEC).
182 66 my $freq_word = int(sprintf("%.0f", 1000000.0 / ($carrier_hz * 0.241246)));
183 66 my $period_us = $freq_word * 0.241246;
184  
185 66 ▶ my $us_to_pulses = sub { int(sprintf("%.0f", $_[0] / $period_us)) };
Subroutine __ANON__: called CC=1 SCAR=0.0
186  
187 66 my $data = _encode_data($code->address, $code->command);
188  
189 # The value is transmitted with the most significant byte first and each
190 # byte LSB-first within. The wire bytes are the MSB display form (Tasmota's
191 # Data field), which is the per-byte bit reversal of the collected value.
192 66 my $msb_val = _byte_reverse($data);
193  
194 66 my @burst_pairs = (
195 [$us_to_pulses->(4480), $us_to_pulses->(4480)]
196 );
197  
198 66 ▶ for (my $i = 31; $i >= 0; $i--) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i >= 0) 2112 66 2178
199 2112 my $bit = ($msb_val >> $i) & 1;
200 2112 ▶ push @burst_pairs, [
Branch: 1 branch
BranchTrueFalseTotal
$bit ? : 908 1204 2112
201 $us_to_pulses->(560),
202 $us_to_pulses->($bit ? 1680 : 560)
203 ];
204 }
205  
206 66 push @burst_pairs, [$us_to_pulses->(560), $us_to_pulses->(30000)];
207  
208 66 my $seq1_pairs = scalar @burst_pairs;
209  
210 66 my $header = sprintf("0000 %04X %04X 0000", $freq_word, $seq1_pairs);
211 66 my @payload = map { sprintf("%04X %04X", $_->[0], $_->[1]) } @burst_pairs;
212  
213 66 return join(" ", $header, @payload);
214 }
215  
216 55 1;
217  
218 =encoding utf8
219  
220 =head1 NAME
221  
222 Protocol::IR::Proto::SAMSUNG - SAMSUNG protocol handler (32-bit)
223  
224 =head1 VERSION
225  
226 version 1.5
227  
228 =head1 SYNOPSIS
229  
230 use Protocol::IR::Converter;
231  
232 my $converter = Protocol::IR::Converter->new();
233  
234 # From a raw value (MSB display form) or from parameters
235 my $code = $converter->import_code('SAMSUNG', '0xE0E09966');
236 my $code = $converter->import_code('SAMSUNG',
237 { address => 0xE0, command => 0x99 });
238  
239 =head1 DESCRIPTION
240  
241 The SAMSUNG protocol transmits a 32-bit frame at 38 kHz: a customer
242 (address) byte, its one's complement, a command byte, and the command's
243 one's complement. The 32-bit value is transmitted with the most significant
244 byte first and each byte LSB-first within, so the value collected from a
245 capture (and stored in C<data>) matches Tasmota's C<DataLSB> field; the
246 C<data> value exposed for a raw import is the MSB display form (Tasmota's
247 C<Data> field). The customer and command bytes are bit-reversed on the wire
248 relative to their logical values. SAMSUNG has no subaddress;
249 C<subaddress> is set to C<-1>.
250  
251 Frame timing: B<4480 µs> header mark and B<4480 µs> header space; each bit
252 is a B<560 µs> mark followed by a space of B<560 µs> for 0 or B<1680 µs>
253 for 1; a B<560 µs> stop mark ends the frame.
254  
255 Timing and bit ordering follow IRremoteESP8266 (David Conran et al., GPLv2,
256 L<https://github.com/crankyoldgit/IRremoteESP8266>): C<kSamsungHdrMark>/
257 C<kSamsungHdrSpace> = 8 * 560 µs, C<kSamsungBitMark> = 560 µs,
258 C<kSamsungOneSpace> = 3 * 560 µs, C<kSamsungZeroSpace> = 560 µs, 32 bits.
259  
260 =head1 METHODS
261  
262 =head2 decode_raw
263  
264 my $code = $class->decode_raw('0xE0E09966');
265  
266 Builds an L<Protocol::IR::Code> from the raw 32-bit value.
267  
268 =head2 decode_byte_order
269  
270 my $code = $class->decode_byte_order($raw, $lsb);
271  
272 Decodes from either byte order: C<$lsb> true reads the accumulated (DataLSB)
273 form; false reads the display (Data) form, and reverses the bytes to reach it.
274 Samsung is the opposite of NEC's polarity: C<decode_raw> reads the display
275 form directly (its C<data> field carries the accumulated word), so C<$lsb>
276 false needs no reversal and true reverses once.
277  
278 =head2 lsb_is_accumulated
279  
280 my $flag = $class->lsb_is_accumulated;
281  
282 True (always, for SAMSUNG) when the accumulated byte order is what
283 C<decode_raw> reads, informing the Tasmota structured importer which of its
284 C<Data>/C<DataLSB> fields to prefer.
285  
286 =head2 decode_params
287  
288 my $code = $class->decode_params(address => 0xE0, command => 0x99);
289  
290 Builds an L<Protocol::IR::Code> from discrete parameters. Accepts C<address> or
291 C<device>, and C<command> or C<function>.
292  
293 =head2 decode_timing
294  
295 my $code = $class->decode_timing(\@burst_pairs_us);
296  
297 Decodes an arrayref of microsecond C<[mark_us, space_us]> pairs. Returns an
298 L<Protocol::IR::Code> when the header, 32 data bits, and stop bit match the SAMSUNG
299 timing signature, otherwise C<undef>.
300  
301 =head2 to_pronto
302  
303 my $pronto = $class->to_pronto($ir_code);
304  
305 Encodes an L<Protocol::IR::Code> object as a Pronto Hex string at 38 kHz.
306  
307 =head2 as_necx2_params
308  
309 my $params = $class->as_necx2_params($code);
310  
311 Converts a SAMSUNG code to its L<Protocol::IR::Proto::NECX2> equivalent
312 parameter hashref. The Samsung address and command are bit-reversed to
313 produce the NECX2 device and function values respectively. The subaddress
314 is set equal to the device (Samsung's address-repeated convention mapped
315 to NECX2's C<Default S=D>).
316  
317 The returned hashref is suitable for
318 C<< $converter->import_code('NECX2', $params) >>.
319  
320 Example:
321  
322 # Samsung TV POWER: address=0xE0, command=0x40
323 # -> NECX2 device=7, subdevice=7, function=2
324 my $necx2_params = Protocol::IR::Proto::SAMSUNG->as_necx2_params($code);
325  
326 This is used by L<Protocol::IR::Converter/cross_protocol> and is also
327 available for direct use when building IRDB lookup tables.
328  
329 =head1 CROSS-PROTOCOL MAPPING
330  
331 The SAMSUNG protocol shares identical timing with L<Protocol::IR::Proto::NECX2>:
332 both use a B<4500/4500 µs> half header, B<560/1680 µs> bit timing, 32 bits,
333 per-byte LSB-first. The two protocols differ only in field naming:
334  
335 =over 4
336  
337 =item SAMSUNG sends C<addr, addr, cmd, ~cmd> on the wire.
338  
339 =item NECX2 sends C<device, subdevice, function, ~function>.
340  
341 =back
342  
343 Because Samsung's address byte is repeated while NECX2 carries a real
344 subaddress byte, the two cannot always be converted. However, when the
345 NECX2 subaddress equals the device byte (the common case for Samsung TV
346 codes in IRDB), the Samsung address is the bit-reversal of the NECX2
347 device byte, and likewise for the command/function byte.
348  
349 IRDB labels its Samsung TV entries as NECX2 with the Samsung address and
350 command values in the device and function columns (e.g. Samsung TV POWER
351 is C<NECx2,7,7,2>: device 7 is the bit-reversal of address 0xE0, function
352 2 is the bit-reversal of command 0x40).
353  
354 Use L<Protocol::IR::Converter/cross_protocol> to convert between the two
355 automatically, or call C<as_necx2_params> directly for IRDB lookups.
356  
357 =head1 SUPPORT
358  
359 Source code: L<https://github.com/bwarden/perl-protocol-ir>
360  
361 Bug reports and feature requests: L<https://github.com/bwarden/perl-protocol-ir/issues>
362  
363 =head1 AUTHOR
364  
365 Brett T. Warden <bwarden@cpan.org>
366  
367 =head1 COPYRIGHT AND LICENSE
368  
369 Copyright (c) 2026 Brett T. Warden
370  
371 This library is free software; you can redistribute it and/or modify it
372 under the terms of the GNU General Public License as published by the
373 Free Software Foundation; either version 2 of the License, or (at your
374 option) any later version.
« blib/lib/Protocol/IR/Proto/JVC.pm ↑ Summary blib/lib/Protocol/IR/Proto/Denon.pm »