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

filter: Statementstmt 98.5% 67 / 68 Branchbran 88.8% 16 / 18 Conditioncond 66.6% 22 / 33 MC/DCmcdc 42.8% 6 / 14 Subroutinesub 91.6% 11 / 12 Podpod 80.0% 4 / 5 totaltotal 84.0% 126 / 150 CC 25 SCAR 19.8
CC
25
Cov
88%
decode_timing
12.5
decode_params
8.7
to_pronto
5.0
CRAP 7.3
« blib/lib/Protocol/IR/Proto/Aiwa.pm ↑ Summary blib/lib/Protocol/IR/Format/Mode2.pm »
1 package Protocol::IR::Proto::SAMSUNG20;
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::SAMSUNG20 is the SAMSUNG20 protocol handler
10 # (20-bit), from the MakeHex Samsung20.irp and DecodeIR definitions:
11 #
12 # {38.4k,564}<1,-1|1,-3>(8,-8,D:6,S:6,F:8,1,-44)
13 #
14 # A 20-bit frame carries a 6-bit device, a 6-bit subdevice, and an 8-bit
15 # function, transmitted LSB-first within each field behind a 4512/4512 µs
16 # header (the same header Samsung's 32-bit protocol uses). The whole frame is
17 # sent once, so a capture is header + 20 bits + stop. IRDB uses it for Samsung
18 # air-conditioner handsets.
19  
20 55 ▶sub _parse_int {
Subroutine _parse_int: called CC=3 SCAR=11.0
21 55 my ($val) = @_;
22 55 ▶ return 0 unless defined $val;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $val 0 55 55
23 55 $val =~ s/^\s+|\s+$//g;
24 55 ▶ return Math::BigInt->from_hex($val) if $val =~ /^0x/i;
Branch: 1 branch
BranchTrueFalseTotal
if $val =~ /^0x/i 1 54 55
25 54 return $val + 0;
26 }
27  
28 # True when the accumulated word (Tasmota DataLSB) is the form decode_raw
29 # reads; SAMSUNG20 has no display/accumulated distinction, so decode_byte_order
30 # is inherited from decode_raw.
31 0 ▶sub lsb_is_accumulated { 1 }
Subroutine lsb_is_accumulated: not called CC=1 SCAR=6.9
Pod undocumented
32  
33 # Pack device/subdevice/function into the transmitted 20-bit word. The
34 # default subdevice is 0 per Samsung20.irp (Default S=0), so a caller that
35 # omits it (subaddress -1) sends a zero field.
36 36 ▶sub _encode_data {
Subroutine _encode_data: called CC=1 SCAR=0.0
37 36 my ($addr, $subaddr, $cmd) = @_;
38 36 return (($cmd & 0xFF) << 12) | (($subaddr & 0x3F) << 6) | ($addr & 0x3F);
39 }
40  
41 1 ▶sub decode_raw {
Subroutine decode_raw: called CC=1 SCAR=0.0
Pod documented
42 1 my ($class, $raw_val) = @_;
43 1 my $val = _parse_int($raw_val);
44  
45 1 return Protocol::IR::Code->new(
46 protocol => 'SAMSUNG20',
47 bits => 20,
48 address => $val & 0x3F,
49 subaddress => ($val >> 6) & 0x3F,
50 command => ($val >> 12) & 0xFF,
51 data => $val,
52 );
53 }
54  
55 18 ▶sub decode_params {
Subroutine decode_params: called CC=8 SCAR=21.6
Pod documented
56 18 my ($class, %args) = @_;
57 18 ▶ my $addr = _parse_int($args{address} // $args{device} // 0) & 0x3F;
Condition60%3/5
($args{"address"} // $args{"device"}) // 0
l!l
180
$args{"address"} // $args{"device"}
l!l&&r!l&&!r
1710
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
58 18 ▶ my $subaddr_arg = _parse_int($args{subaddress} // $args{subdevice} // -1);
Condition100%5/5
($args{"subaddress"} // $args{"subdevice"}) // -1
l!l
171
$args{"subaddress"} // $args{"subdevice"}
l!l&&r!l&&!r
1611
MC/DC100%2/2
($args{"subaddress"} // $args{"subdevice"}) // -1
$args{"subaddress"} $args{"subdevice"}
Truth table100%3/3
A // B
A= $args{"subaddress"}B= $args{"subdevice"}
ABresult
1X1
011
000
59 18 ▶ my $cmd = _parse_int($args{command} // $args{function} // 0) & 0xFF;
Condition40%2/5
($args{"command"} // $args{"function"}) // 0
l!l
180
$args{"command"} // $args{"function"}
l!l&&r!l&&!r
1800
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
60  
61 # Default S=0: a missing subdevice sends a zero field; the stored
62 # subaddress stays -1.
63 18 ▶ my $subaddr = ($subaddr_arg == -1) ? 0 : $subaddr_arg & 0x3F;
Branch: 1 branch
BranchTrueFalseTotal
$subaddr_arg == -1 ? : 1 17 18
64  
65 18 return Protocol::IR::Code->new(
66 protocol => 'SAMSUNG20',
67 bits => 20,
68 address => $addr,
69 subaddress => $subaddr_arg,
70 command => $cmd,
71 data => _encode_data($addr, $subaddr, $cmd),
72 );
73 }
74  
75 # Decode microsecond timing pairs into Protocol::IR::Code
76 307 ▶sub decode_timing {
Subroutine decode_timing: called CC=12 SCAR=25.2
Pod documented
77 307 my ($class, $burst_pairs) = @_;
78 307 ▶ return undef unless scalar(@$burst_pairs) >= 22; # Header + 20 bits + Stop
Branch: 1 branch
BranchTrueFalseTotal
unless scalar @$burst_pairs >= 22 149 158 307
79  
80 158 my ($hdr_mark, $hdr_space) = @{$burst_pairs->[0]};
81  
82 # SAMSUNG20 Header Check: ~4512 µs mark, ~4512 µs space
83 158 ▶ 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) 109 49 158
Condition66%6/9
$hdr_mark >= 3800 and $hdr_mark <= 5200 and ($hdr_space >= 3800 and $hdr_space <= 5200)
!ll&&!rl&&r
109049
$hdr_mark >= 3800 and $hdr_mark <= 5200
!ll&&!rl&&r
101849
$hdr_space >= 3800 and $hdr_space <= 5200
!ll&&!rl&&r
0049
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
84 ($hdr_space >= 3800 && $hdr_space <= 5200);
85  
86 49 my $value = 0;
87 49 for my $i (0 .. 19) {
88 980 my $pair = $burst_pairs->[$i + 1];
89 980 my $space = $pair->[1];
90  
91 # Space ~1692 µs = 1, ~564 µs = 0
92 980 ▶ my $bit = ($space > 1100) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$space > 1100 ? : 269 711 980
93 980 $value |= ($bit << $i); # LSB-first
94 }
95  
96 # Stop bit: a short mark followed by the ~25ms inter-frame space. A
97 # capture may end on a bare trailing mark with no space.
98 49 my ($stop_mark, $stop_space) = @{$burst_pairs->[21]};
99 49 ▶ 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 >= 3000 1 48 49
Condition66%6/9
$stop_mark >= 400 and $stop_mark <= 900 and $stop_space == 0 || $stop_space >= 3000
!ll&&!rl&&r
0148
$stop_mark >= 400 and $stop_mark <= 900
!ll&&!rl&&r
0049
$stop_space == 0 or $stop_space >= 3000
l!l&&r!l&&!r
1471
MC/DC50%2/4
$stop_mark >= 400 and $stop_mark <= 900 and $stop_space == 0 || $stop_space >= 3000
$stop_mark >= 400 $stop_mark <= 900 $stop_space == 0 $stop_space >= 3000
Truth table60%3/5
A and B and C or D
A= $stop_mark >= 400B= $stop_mark <= 900C= $stop_space == 0D= $stop_space >= 3000
ABCDresult
0XXX0
10XX0
11000
111X1
11011
100 ($stop_space == 0 || $stop_space >= 3000);
101  
102 48 return Protocol::IR::Code->new(
103 protocol => 'SAMSUNG20',
104 bits => 20,
105 address => $value & 0x3F,
106 subaddress => ($value >> 6) & 0x3F,
107 command => ($value >> 12) & 0xFF,
108 data => $value,
109 );
110 }
111  
112 18 ▶sub to_pronto {
Subroutine to_pronto: called CC=5 SCAR=16.1
Pod documented
113 18 my ($class, $code) = @_;
114 18 my $carrier_hz = 38400;
115  
116 # Build the pulse conversion period from the rounded Pronto frequency
117 # word, as MakeHex and Pronto parsers do (see Protocol::IR::Proto::NEC).
118 18 my $freq_word = int(sprintf("%.0f", 1000000.0 / ($carrier_hz * 0.241246)));
119 18 my $period_us = $freq_word * 0.241246;
120  
121 18 ▶ my $us_to_pulses = sub { int(sprintf("%.0f", $_[0] / $period_us)) };
Subroutine __ANON__: called CC=1 SCAR=0.0
122  
123 18 my $addr = $code->address & 0x3F;
124 18 ▶ my $subaddr = ($code->subaddress != -1) ? ($code->subaddress & 0x3F) : 0;
Branch: 1 branch
BranchTrueFalseTotal
$code->subaddress != -1 ? : 18 0 18
125 18 my $cmd = $code->command & 0xFF;
126  
127 18 my $data = _encode_data($addr, $subaddr, $cmd);
128  
129 18 my @bits;
130 18 for my $i (0 .. 19) { push @bits, ($data >> $i) & 1; } # LSB-first
131  
132 18 my @burst_pairs = (
133 [$us_to_pulses->(4512), $us_to_pulses->(4512)]
134 );
135  
136 18 for my $bit (@bits) {
137 360 ▶ push @burst_pairs, [
Branch: 1 branch
BranchTrueFalseTotal
$bit ? : 99 261 360
138 $us_to_pulses->(564),
139 $us_to_pulses->($bit ? 1692 : 564)
140 ];
141 }
142  
143 # The trailing mark and ~25ms inter-frame space (1,-44 of 564 µs).
144 18 push @burst_pairs, [$us_to_pulses->(564), $us_to_pulses->(24816)];
145  
146 18 my $seq1_pairs = scalar @burst_pairs;
147  
148 18 my $header = sprintf("0000 %04X %04X 0000", $freq_word, $seq1_pairs);
149 18 my @payload = map { sprintf("%04X %04X", $_->[0], $_->[1]) } @burst_pairs;
150  
151 18 return join(" ", $header, @payload);
152 }
153  
154 55 1;
155  
156 =encoding utf8
157  
158 =head1 NAME
159  
160 Protocol::IR::Proto::SAMSUNG20 - SAMSUNG20 protocol handler (20-bit AC)
161  
162 =head1 VERSION
163  
164 version 1.5
165  
166 =head1 SYNOPSIS
167  
168 use Protocol::IR::Converter;
169  
170 my $converter = Protocol::IR::Converter->new();
171  
172 # From a raw 20-bit value or from parameters
173 my $code = $converter->import_code('SAMSUNG20', '0x27201');
174 my $code = $converter->import_code('SAMSUNG20',
175 { address => 1, subaddress => 8, command => 39 });
176  
177 =head1 DESCRIPTION
178  
179 The C<SAMSUNG20> protocol transmits 20-bit frames at B<38.4 kHz>: a 6-bit
180 B<device>, a 6-bit B<subdevice>, and an 8-bit B<function>, transmitted
181 LSB-first within each field behind a B<4512/4512 µs> header (the same header
182 Samsung's 32-bit protocol uses). The whole frame is sent once, so a capture
183 is header + 20 bits + stop. IRDB uses it for Samsung air-conditioner
184 handsets.
185  
186 The C<data> value matches Tasmota's C<DataLSB> field: the B<function> in
187 bits 12-19, B<subdevice> in bits 6-11, and B<device> in bits 0-5. There is no
188 display/accumulated byte-order distinction for a 20-bit word.
189  
190 A missing subdevice (C<-1>) sends a zero field per the IRP "Default S=0"
191 rule; the stored C<subaddress> stays C<-1>.
192  
193 =head1 METHODS
194  
195 =head2 decode_raw
196  
197 my $code = $class->decode_raw('0x27201');
198  
199 Builds an L<Protocol::IR::Code> from the raw 20-bit value.
200  
201 =head2 decode_params
202  
203 my $code = $class->decode_params(address => 1, subaddress => 8, command => 39);
204  
205 Builds an L<Protocol::IR::Code> from discrete parameters. Accepts C<address> or
206 C<device>, C<subaddress> or C<subdevice>, and C<command> or C<function>.
207  
208 =head2 decode_timing
209  
210 my $code = $class->decode_timing(\@burst_pairs_us);
211  
212 Decodes an arrayref of microsecond C<[mark_us, space_us]> pairs. Returns an
213 L<Protocol::IR::Code> when the header, 20 data bits, and stop bit match the
214 SAMSUNG20 timing signature, otherwise C<undef>.
215  
216 =head2 to_pronto
217  
218 my $pronto = $class->to_pronto($ir_code);
219  
220 Encodes an L<Protocol::IR::Code> object as a Pronto Hex string at 38.4 kHz.
221  
222 =head1 SUPPORT
223  
224 Source code: L<https://github.com/bwarden/perl-protocol-ir>
225  
226 Bug reports and feature requests: L<https://github.com/bwarden/perl-protocol-ir/issues>
227  
228 =head1 AUTHOR
229  
230 Brett T. Warden <bwarden@cpan.org>
231  
232 =head1 COPYRIGHT AND LICENSE
233  
234 Copyright (c) 2026 Brett T. Warden
235  
236 This library is free software; you can redistribute it and/or modify it
237 under the terms of the GNU General Public License as published by the
238 Free Software Foundation; either version 2 of the License, or (at your
239 option) any later version.
« blib/lib/Protocol/IR/Proto/Aiwa.pm ↑ Summary blib/lib/Protocol/IR/Format/Mode2.pm »