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

filter: Statementstmt 99.4% 196 / 197 Branchbran 89.3% 84 / 94 Conditioncond 68.3% 41 / 60 MC/DCmcdc 51.4% 18 / 35 Subroutinesub 100% 17 / 17 Podpod 66.6% 4 / 6 totaltotal 88.0% 360 / 409 CC 80 SCAR 24.6
CC
80
Cov
91%
_decode_mwm
18.9
__ANON__
16.3
to_pronto
15.6
CRAP 11.7
« blib/lib/Protocol/IR/Proto/SAMSUNG36.pm ↑ Summary blib/lib/Protocol/IR/Format/Tasmota.pm »
1 package Protocol::IR::Proto::MWM;
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 Math::BigInt;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 55 ▶use Protocol::IR::Code;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 # MWM (Disney "Made With Magic" / Glow With The Show) protocol handler,
11 # ported from IRremoteESP8266's sendMWM/decodeMWM (ir_MWM.cpp) and validated
12 # against the Tasmota captures in samples/tasmota-capture.log.
13 #
14 # The signal is 2400 bps serial over a 38 kHz carrier: 1 start bit (mark),
15 # 8 data bits (space=1, mark=0, LSB-first), 1 stop bit (space), repeated per
16 # byte with no header, each logical bit one 417 µs tick (up to 9 ticks may
17 # merge into one measured run). Messages are 3-18 bytes (24-144 bits); the
18 # byte count is implied by the message body: state[0] carries a 4-bit payload
19 # length in the high nibble for command frames (0x9x/0xFx), and show commands
20 # open with the 0x55 0xAA signature. Because the decoded state bytes are
21 # exactly the transmitted bytes, Tasmota's "Data" field is the display form
22 # and no byte-order translation applies.
23 #
24 # Frames are up to 144 bits, beyond the native integer range, so the data
25 # word is carried as a Math::BigInt.
26  
27 55 my $kTick = 417; # us per logical bit
28 55 my $kMaxWidth = 9; # maximum consecutive same-sign ticks per measured run
29 55 my $kDelta = 150; # +/- us width tolerance, matching IRrecv::match(delta)
30 55 my $kMaxGap = 20000; # us threshold for an inter-message space
31 55 my $kFooterGap = 30000; # us inter-command delay, kMWMMinGap
32 55 my $kMinSamples = 6; # kMWMMinSamples: shortest frame has 3 bytes, >= 2 samples each
33 55 my $kStateSizeMax = 55; # IRremoteESP8266 state buffer cap
34 55 my $kMinBits = 24; # kMWMMinBits: 3 bytes
35 55 my $kMaxBits = 144; # (15 + 3) * 8: the 4-bit payload nibble caps a frame at 18 bytes
36  
37 49 ▶sub _parse_int {
Subroutine _parse_int: called CC=5 SCAR=16.2
38 49 my ($val) = @_;
39 49 ▶ return Math::BigInt->bzero() unless defined $val;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $val 0 49 49
40 49 ▶ return Math::BigInt->new($val) if ref $val;
Branch: 1 branch
BranchTrueFalseTotal
if ref $val 22 27 49
41 27 $val =~ s/^\s+|\s+$//g;
42 27 ▶ return Math::BigInt->bzero() unless length $val;
Branch: 1 branch
BranchTrueFalseTotal
unless length $val 0 27 27
43 27 ▶ return Math::BigInt->from_hex($val) if $val =~ /^0x/i;
Branch: 1 branch
BranchTrueFalseTotal
if $val =~ /^0x/i 25 2 27
44 2 return Math::BigInt->new($val);
45 }
46  
47 # MWM is a serial protocol, not an IR on-air byte stream: there is no
48 # display/accumulated byte-order distinction, so the value is used as-is.
49 3 ▶sub lsb_is_accumulated { 0 }
Subroutine lsb_is_accumulated: called CC=1 SCAR=0.0
Pod undocumented
50  
51 # A bundle is the accumulation of two or more length-declared MWM frames
52 # (typically a command A, its status companion B, and a repeat A' -- all three
53 # self-declare their own byte length in their leading byte, so the stream is
54 # walkable without any side information). Splits the value into one Code per
55 # frame. The first frame returned is the command/decode_raw frame A, which is
56 # what the structured Data field of a Tasmota MWM record normally carries;
57 # unbundle exists so a single structured hex tap can split a real A+B+A'
58 # capture into all three of its own frames.
59 26 ▶sub unbundle {
Subroutine unbundle: called CC=12 SCAR=24.9
Pod undocumented
60 26 my ($class, $raw_val) = @_;
61 26 my $val = _parse_int($raw_val);
62 26 ▶ die "MWM data must be non-negative\n" if $val->is_neg;
Branch: 1 branch
BranchTrueFalseTotal
if $val->is_neg 0 26 26
63  
64 # Walk the value as a run of length-declared frames. 0x9x/0xFx leading
65 # nibbles declare n+3 payload bytes (byte0's low nibble declaring the byte
66 # count after the fixed 3-byte header). When the walk cannot land exactly
67 # on the value end, the value is a single bare frame (24-bit show /
68 # width-based values) and this returns the one Code built from the whole
69 # value.
70 26 my $hex = $val->as_hex;
71 26 $hex =~ s/^0x//i;
72 26 ▶ $hex = '0' x (6 - length($hex)) . $hex if length($hex) < 6;
Branch: 1 branch
BranchTrueFalseTotal
if length $hex < 6 2 24 26
73 26 my @bytes = map { hex($_) } ($hex =~ /(..?)/g);
74 26 my @frames;
75 26 my $pos = 0;
76 26 ▶ while ($pos < @bytes) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pos < @bytes) 33 14 47
77 33 my $header = $bytes[$pos];
78 33 my $declared = ($header & 0x0f) + 3;
79 33 ▶ last if $pos + $declared > @bytes;
Branch: 1 branch
BranchTrueFalseTotal
if $pos + $declared > @bytes 9 24 33
80 24 my $high = $header & 0xf0;
81 24 ▶ last unless $high == 0x90 || $high == 0xf0;
Branch: 1 branch
BranchTrueFalseTotal
unless $high == 144 or $high == 240 3 21 24
Condition100%3/3
$high == 144 or $high == 240
l!l&&r!l&&!r
1833
MC/DC100%2/2
$high == 144 or $high == 240
$high == 144 $high == 240
Truth table100%3/3
A or B
A= $high == 144B= $high == 240
ABresult
1X1
011
000
82 21 push @frames, [ @bytes[$pos .. $pos + $declared - 1] ];
83 21 $pos += $declared;
84 }
85 26 ▶ if (@frames < 2 || $pos != @bytes) {
Branch: 1 branch
BranchTrueFalseTotal
if (@frames < 2 or $pos != @bytes) 23 3 26
Condition66%2/3
@frames < 2 or $pos != @bytes
l!l&&r!l&&!r
2303
MC/DC50%1/2
@frames < 2 or $pos != @bytes
@frames < 2 $pos != @bytes
Truth table66%2/3
A or B
A= @frames < 2B= $pos != @bytes
ABresult
1X1
011
000
86 # Single frame (or a non-length walk): the width-derived frame is the
87 # same one decode_raw's fallback builds -- inline that law here so a
88 # single frame never rings unbundle<->decode_raw.
89 23 my $digits = $val->as_hex;
90 23 $digits =~ s/^0x//i;
91 23 my $bits = int((length($digits) + 1) / 2) * 8;
92 23 ▶ $bits = $kMinBits if $bits < $kMinBits;
Branch: 1 branch
BranchTrueFalseTotal
if $bits < $kMinBits 2 21 23
93 23 return [ Protocol::IR::Code->new(
94 protocol => 'MWM', bits => $bits, data => $val) ];
95 }
96 3 my @codes;
97 3 for my $frame (@frames) {
98 9 my $data = Math::BigInt->bzero();
99 9 for my $b (@$frame) { $data = ($data << 8)->badd($b); }
100 9 push @codes, Protocol::IR::Code->new(
101 protocol => 'MWM',
102 bits => scalar(@$frame) * 8,
103 data => $data,
104 );
105 }
106 3 return \@codes;
107 }
108  
109 23 ▶sub decode_raw {
Subroutine decode_raw: called CC=4 SCAR=13.9
Pod documented
110 23 my ($class, $raw_val) = @_;
111 23 my $val = _parse_int($raw_val);
112 23 ▶ die "MWM data must be non-negative\n" if $val->is_neg;
Branch: 1 branch
BranchTrueFalseTotal
if $val->is_neg 1 22 23
113  
114 # A structured tap that ingested a whole A+B+A' bundle asks for a single
115 # code: the bundle's first frame (A) is the command that decode_raw
116 # returns for the record's Data field.
117 22 my $bundled = $class->unbundle($val);
118 22 ▶ return $bundled->[0] if @$bundled >= 2;
Branch: 1 branch
BranchTrueFalseTotal
if @$bundled >= 2 2 20 22
119  
120 # The frame length is implied by the value's own width, rounded up to a
121 # whole number of bytes with the 3-byte protocol minimum.
122 20 my $hex = $val->as_hex;
123 20 $hex =~ s/^0x//i;
124 20 my $bits = int((length($hex) + 1) / 2) * 8;
125 20 ▶ $bits = $kMinBits if $bits < $kMinBits;
Branch: 1 branch
BranchTrueFalseTotal
if $bits < $kMinBits 1 19 20
126  
127 20 return Protocol::IR::Code->new(
128 protocol => 'MWM',
129 bits => $bits,
130 data => $val,
131 );
132 }
133  
134 2 ▶sub decode_params {
Subroutine decode_params: called CC=3 SCAR=11.0
Pod documented
135 2 my ($class, %args) = @_;
136 die "MWM requires a data value\n"
137 2 ▶ unless defined $args{data} && "$args{data}" ne '';
Branch: 1 branch
BranchTrueFalseTotal
unless defined $args{"data"} and qq[$args{"data"}] ne "" 1 1 2
Condition66%2/3
defined $args{"data"} and qq[$args{"data"}] ne ""
!ll&&!rl&&r
101
MC/DC50%1/2
defined $args{"data"} and qq[$args{"data"}] ne ""
defined $args{"data"} qq[$args{"data"}] ne ""
Truth table66%2/3
A and B
A= defined $args{"data"}B= qq[$args{"data"}] ne ""
ABresult
0X0
100
111
138 1 return $class->decode_raw($args{data});
139 }
140  
141 # Match a measured width against `expected` within +/- kDelta us, mirroring
142 # IRrecv::match with zero tolerance and the kMWMDelta margin.
143 68346 ▶sub _match_width {
Subroutine _match_width: called CC=2 SCAR=6.9
144 68346 my ($width, $expected) = @_;
145 68346 ▶ return $width >= $expected - $kDelta && $width <= $expected + $kDelta;
Condition100%3/3
$width >= $expected - $kDelta && $width <= $expected + $kDelta
!ll&&!rl&&r
585702589518
MC/DC100%2/2
$width >= $expected - $kDelta && $width <= $expected + $kDelta
$width >= $expected - $kDelta $width <= $expected + $kDelta
Truth table100%3/3
A && B
A= $width >= $expected - $kDeltaB= $width <= $expected + $kDelta
ABresult
0X0
100
111
146 }
147  
148 # Consume one logical level from the run-length rawbuf, expanding a measured
149 # width into up to kMaxWidth ticks of the same signal, exactly like
150 # IRrecv::getRClevel(kMWMTick, 0, 0, kMWMDelta, kMWMMaxWidth). Advances
151 # `state` (a { offset, used } cursor) as levels are consumed.
152 9962 ▶sub _get_rc_level {
Subroutine _get_rc_level: called CC=10 SCAR=23.0
153 9962 my ($rawbuf, $state) = @_;
154 9962 ▶ return 1 if $state->{offset} >= @$rawbuf; # bare space past the end
Branch: 1 branch
BranchTrueFalseTotal
if $state->{"offset"} >= @$rawbuf 0 9962 9962
155 9962 my $width = $rawbuf->[$state->{offset}];
156 # rawbuf alternates mark/space from index 1, so odd indices are marks.
157 9962 ▶ my $val = ($state->{offset} % 2) ? 0 : 1;
Branch: 1 branch
BranchTrueFalseTotal
$state->{'offset'} % 2 ? : 5637 4325 9962
158 # A space wider than the max signal gap or the widest run is an
159 # inter-message gap: read it as a bare space without consuming it.
160 9962 ▶ if ($val == 1 &&
Branch: 1 branch
BranchTrueFalseTotal
if ($val == 1 and $width > $kMaxGap - $kDelta || $width > $kMaxWidth * $kTick + $kDelta) 370 9592 9962
Condition100%6/6
$val == 1 and $width > $kMaxGap - $kDelta || $width > $kMaxWidth * $kTick + $kDelta
!ll&&!rl&&r
56373955370
$width > $kMaxGap - $kDelta or $width > $kMaxWidth * $kTick + $kDelta
l!l&&r!l&&!r
300703955
MC/DC100%3/3
$val == 1 and $width > $kMaxGap - $kDelta || $width > $kMaxWidth * $kTick + $kDelta
$val == 1 $width > $kMaxGap - $kDelta $width > $kMaxWidth * $kTick + $kDelta
Truth table100%4/4
A and B or C
A= $val == 1B= $width > $kMaxGap - $kDeltaC= $width > $kMaxWidth * $kTick + $kDelta
ABCresult
0XX0
1000
11X1
1011
161 ($width > $kMaxGap - $kDelta || $width > $kMaxWidth * $kTick + $kDelta)) {
162 370 return 1;
163 }
164 9592 my $avail = 0;
165 9592 for my $a (reverse 1 .. $kMaxWidth) {
166 68346 ▶ if (_match_width($width, $a * $kTick)) { $avail = $a; last; }
Branch: 1 branch
BranchTrueFalseTotal
if (_match_width($width, $a * $kTick)) 9518 58828 68346
167 }
168 9592 ▶ return -1 unless $avail; # the width matches no whole number of ticks
Branch: 1 branch
BranchTrueFalseTotal
unless $avail 74 9518 9592
169 9518 $state->{used}++;
170 9518 ▶ if ($state->{used} >= $avail) {
Branch: 1 branch
BranchTrueFalseTotal
if ($state->{"used"} >= $avail) 4943 4575 9518
171 4943 $state->{used} = 0;
172 4943 $state->{offset}++;
173 }
174 9518 return $val;
175 }
176  
177 # Decode a rawbuf of absolute timings into the transmitted state bytes.
178 # Mirrors IRrecv::decodeMWM with strict matching (Tasmota's decoder is
179 # non-strict and additionally accepts frames whose trailing bytes exceed the
180 # payload length; the strict length check keeps a truncated capture from
181 # silently decoding on its own). A capture whose final byte's stop space
182 # merged into the omitted footer is back-filled and accepted only when the
183 # result is length- and checksum-consistent. Returns undef when the signal is
184 # not a well-formed MWM message.
185 261 ▶sub _decode_mwm {
Subroutine _decode_mwm: called CC=18 SCAR=29.4
186 261 my ($rawbuf) = @_;
187 # kMWMMinSamples: a message is >= 3 bytes and a byte has >= 2 samples, so a
188 # run-length buffer of 6 entries or fewer cannot hold a full byte.
189 261 ▶ return undef if @$rawbuf <= $kMinSamples;
Branch: 1 branch
BranchTrueFalseTotal
if @$rawbuf <= $kMinSamples 56 205 261
190  
191 205 my $state = { offset => 1, used => 0 };
192 205 my @state_bytes;
193 205 my $data = Math::BigInt->bzero();
194 205 my ($frame_bits, $data_bits, $done) = (0, 0, 0);
195  
196 205 ▶ while ($state->{offset} < @$rawbuf && $data_bits < 8 * $kStateSizeMax && !$done) {
Branch: 1 branch
BranchTrueFalseTotal
if ($state->{"offset"} < @$rawbuf and $data_bits < 8 * $kStateSizeMax and not $done) 9962 126 10088
Condition50%3/6
$state->{"offset"} < @$rawbuf and $data_bits < 8 * $kStateSizeMax and not $done
!ll&&!rl&&r
01269962
$state->{"offset"} < @$rawbuf and $data_bits < 8 * $kStateSizeMax
!ll&&!rl&&r
0010088
MC/DC33%1/3
$state->{"offset"} < @$rawbuf and $data_bits < 8 * $kStateSizeMax and not $done
$state->{"offset"} < @$rawbuf $data_bits < 8 * $kStateSizeMax not $done
Truth table50%2/4
A and B and not(C)
A= $state->{"offset"} < @$rawbufB= $data_bits < 8 * $kStateSizeMaxC= not $done
ABCresult
0XX0
10X0
1100
1111
197 9962 my $level = _get_rc_level($rawbuf, $state);
198 9962 ▶ last if $level < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $level < 0 74 9888 9962
199 9888 my $slot = $frame_bits % 10;
200 9888 ▶ if ($slot == 0) {
Branch: 2 branches
BranchTrueFalseTotal
if ($slot == 0) { } 1114 8774 9888
elsif ($slot == 9) { } 963 7811 8774
201 1114 ▶ $done = 1 if $level != 0; # start bit must be mark
Branch: 1 branch
BranchTrueFalseTotal
if $level != 0 126 988 1114
202 } elsif ($slot == 9) {
203 963 ▶ return undef if $level != 1; # stop bit must be space
Branch: 1 branch
BranchTrueFalseTotal
if $level != 1 5 958 963
204 # $data_bits is the count of completed data bits, a multiple of 8 here.
205 958 $state_bytes[int($data_bits / 8) - 1] = $data->copy->band(0xFF)->numify;
206 958 $data = Math::BigInt->bzero();
207 } else {
208 # Data bit, LSB-first, space = 1: data = (data + (space?256:0)) >> 1
209 7811 ▶ $data->badd(($level == 1) ? 256 : 0);
Branch: 1 branch
BranchTrueFalseTotal
$level == 1 ? : 3239 4572 7811
210 7811 $data->brsft(1);
211 7811 $data_bits++;
212 }
213 9883 $frame_bits++;
214 }
215  
216 # The message body implies its own length. Command frames carry a payload
217 # byte count in the high nibble of bytes[0]; show commands always open with
218 # 0x55 0xAA. The show-command signature is rejected only when BOTH first
219 # bytes differ from 0x55 0xAA (a 0x550808 show command, say, differs only
220 # in the second byte). Returns the frame when length-consistent, else undef.
221 224 ▶ my $validate = sub {
Subroutine __ANON__: called CC=13 SCAR=27.9
222 224 my ($bytes) = @_;
223 224 my $nb = scalar @$bytes;
224 224 my $nbit = $nb * 8;
225 224 ▶ return undef if $nbit < $kMinBits || $nbit > $kMaxBits || !$nb;
Branch: 1 branch
BranchTrueFalseTotal
if $nbit < $kMinBits or $nbit > $kMaxBits or not $nb 130 94 224
Condition66%4/6
$nbit < $kMinBits or $nbit > $kMaxBits or not $nb
l!l&&r!l&&!r
130094
$nbit < $kMinBits or $nbit > $kMaxBits
l!l&&r!l&&!r
130094
MC/DC33%1/3
$nbit < $kMinBits or $nbit > $kMaxBits or not $nb
$nbit < $kMinBits $nbit > $kMaxBits not $nb
Truth table50%2/4
A or B or not(C)
A= $nbit < $kMinBitsB= $nbit > $kMaxBitsC= not $nb
ABCresult
1XX1
01X1
0011
0000
226 94 my $payload = 0;
227 94 ▶ my $b0 = $bytes->[0] // 0;
Condition50%1/2
$bytes->[0] // 0
l!l
940
MC/DC0%0/1
$bytes->[0] // 0
$bytes->[0]
Truth table50%1/2
A
A= $bytes->[0]
Aresult
11
00
228 94 ▶ if (($b0 & 0xf0) == 0x90 || ($b0 & 0xf0) == 0xf0) {
Branch: 2 branches
BranchTrueFalseTotal
if (($b0 & 240) == 144 or ($b0 & 240) == 240) { } 90 4 94
elsif ($b0 != 85 and ($bytes->[1] // 0) != 170) { } 0 8 8
Condition100%3/3
($b0 & 240) == 144 or ($b0 & 240) == 240
l!l&&r!l&&!r
8824
MC/DC100%2/2
($b0 & 240) == 144 or ($b0 & 240) == 240
($b0 & 240) == 144 ($b0 & 240) == 240
Truth table100%3/3
A or B
A= ($b0 & 240) == 144B= ($b0 & 240) == 240
ABresult
1X1
011
000
229 90 $payload = $b0 & 0x0f;
230 ▶ } elsif ($b0 != 0x55 && ($bytes->[1] // 0) != 0xaa) {
Condition20%1/5
$b0 != 85 and ($bytes->[1] // 0) != 170
!ll&&!rl&&r
400
$bytes->[1] // 0
l!l
00
MC/DC0%0/2
$b0 != 85 and ($bytes->[1] // 0) != 170
$b0 != 85 $bytes->[1]
Truth table33%1/3
A and B
A= $b0 != 85B= $bytes->[1]
ABresult
0X0
100
111
231 0 return undef;
232 }
233 94 ▶ return undef if $nbit < ($payload + 3) * 8;
Branch: 1 branch
BranchTrueFalseTotal
if $nbit < ($payload + 3) * 8 3 91 94
234 91 ▶ return undef if $payload && $nbit > ($payload + 3) * 8;
Branch: 1 branch
BranchTrueFalseTotal
if $payload and $nbit > ($payload + 3) * 8 0 91 91
Condition66%2/3
$payload and $nbit > ($payload + 3) * 8
!ll&&!rl&&r
6850
MC/DC0%0/2
$payload and $nbit > ($payload + 3) * 8
$payload $nbit > ($payload + 3) * 8
Truth table66%2/3
A and B
A= $payloadB= $nbit > ($payload + 3) * 8
ABresult
0X0
100
111
235 91 return { bytes => $bytes, bits => $nbit };
236 200 };
237  
238 # A complete, naturally-consistent frame stands on its own -- e.g. a 3-byte
239 # 55 08 08 show capture whose trailing interference must not be folded into
240 # a fabricated extra byte.
241 200 my $natural = $validate->(\@state_bytes);
242 200 ▶ return $natural if $natural;
Branch: 1 branch
BranchTrueFalseTotal
if $natural 89 111 200
243  
244 # Footerless capture: Tasmota omits the ~30 ms inter-command gap, so the
245 # final byte's stop space (and any trailing 1-bits) ride invisibly inside
246 # it and the signal can end mid-byte with only the stop bit missing. Only
247 # when the natural decode came up short, back-fill the remaining
248 # space-valued levels and commit the byte -- and accept the result only if
249 # the checksum confirms the reconstructed trailing byte.
250 111 ▶ if (!$done && $frame_bits % 10 != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $done and $frame_bits % 10 != 0) 24 87 111
Condition100%3/3
not $done and $frame_bits % 10 != 0
!ll&&!rl&&r
384924
MC/DC100%2/2
not $done and $frame_bits % 10 != 0
not $done $frame_bits % 10 != 0
Truth table100%3/3
not(A) and B
A= not $doneB= $frame_bits % 10 != 0
ABresult
0X0
100
111
251 24 ▶ while ($frame_bits % 10 != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($frame_bits % 10 != 0) 114 0 114
252 114 ▶ if ($frame_bits % 10 == 9) { # stop bit position
Branch: 1 branch
BranchTrueFalseTotal
if ($frame_bits % 10 == 9) 24 90 114
253 24 push @state_bytes, $data->copy->band(0xFF)->numify;
254 24 $data = Math::BigInt->bzero();
255 24 $frame_bits++;
256 24 last;
257 }
258 # data bit, space = 1
259 90 $data->brsft(1);
260 90 $data->badd(0x80);
261 90 $frame_bits++;
262 }
263 24 my $rec = $validate->(\@state_bytes);
264 24 ▶ if ($rec && $state_bytes[-1] == _crc8(@state_bytes[0 .. $#state_bytes - 1])) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rec and $state_bytes[-1] == _crc8(@state_bytes[0 .. $#state_bytes - 1])) 2 22 24
Condition66%2/3
$rec and $state_bytes[-1] == _crc8(@state_bytes[0 .. $#state_bytes - 1])
!ll&&!rl&&r
2202
MC/DC50%1/2
$rec and $state_bytes[-1] == _crc8(@state_bytes[0 .. $#state_bytes - 1])
$rec $state_bytes[-1] == _crc8(@state_bytes[0 .. $#state_bytes - 1])
Truth table66%2/3
A and B
A= $recB= $state_bytes[-1] == _crc8(@state_bytes[0 .. $#state_bytes - 1])
ABresult
0X0
100
111
265 2 return $rec;
266 }
267 }
268  
269 109 return undef;
270 }
271  
272 # CRC-8 (poly 0x8c, MSB-first, init 0), the byte 0 of a 0x9x/0xFx command
273 # frame. Mirrors crc8 in the python-mwm library.
274 2 ▶sub _crc8 {
Subroutine _crc8: called CC=4 SCAR=13.9
275 2 my (@bytes) = @_;
276 2 my $crc = 0;
277 2 for my $b (@bytes) {
278 11 $crc ^= $b;
279 11 ▶ for (1 .. 8) { $crc = ($crc & 1) ? (($crc >> 1) ^ 0x8c) : ($crc >> 1) }
Branch: 1 branch
BranchTrueFalseTotal
$crc & 1 ? : 47 41 88
280 }
281 2 return $crc & 0xff;
282 }
283  
284 261 ▶sub decode_timing {
Subroutine decode_timing: called CC=4 SCAR=13.9
Pod documented
285 261 my ($class, $burst_pairs) = @_;
286 # Flatten the pairs into the run-length rawbuf IRrecv feeds decodeMWM:
287 # index 0 is the leading gap, then alternating mark/space.
288 261 my @rawbuf = (0);
289 261 for my $pair (@$burst_pairs) {
290 6879 push @rawbuf, $pair->[0], $pair->[1];
291 }
292 261 my $decoded = _decode_mwm(\@rawbuf);
293 261 ▶ return undef unless $decoded;
Branch: 1 branch
BranchTrueFalseTotal
unless $decoded 170 91 261
294  
295 91 my $value = Math::BigInt->bzero();
296 91 for my $b (@{ $decoded->{bytes} }) {
297 875 $value->blsft(8);
298 875 $value->badd($b);
299 }
300  
301 return Protocol::IR::Code->new(
302 protocol => 'MWM',
303 bits => $decoded->{bits},
304 91 data => $value,
305 );
306 }
307  
308 # Encode the frame: per byte a 417 µs start mark, the 8 data bits LSB-first
309 # (space = 1) and a 417 µs stop space, then the 30000 µs inter-command gap.
310 # Consecutive same-sign ticks merge into a single measured run.
311 11 ▶sub to_pronto {
Subroutine to_pronto: called CC=15 SCAR=27.4
Pod documented
312 11 my ($class, $code) = @_;
313 11 my $carrier_hz = 38000;
314  
315 # Pronto frequency word and pulse conversion, mirroring NEC::to_pronto.
316 11 my $freq_word = int(sprintf("%.0f", 1000000.0 / ($carrier_hz * 0.241246)));
317 11 my $period_us = $freq_word * 0.241246;
318 11 ▶ my $us_to_pulses = sub { int(sprintf("%.0f", $_[0] / $period_us)) };
Subroutine __ANON__: called CC=1 SCAR=0.0
319  
320 11 ▶ my $nbytes = int(($code->bits || $kMinBits) / 8);
Condition33%1/3
$code->bits || $kMinBits
l!l&&r!l&&!r
1100
MC/DC0%0/2
$code->bits || $kMinBits
$code->bits $kMinBits
Truth table33%1/3
A || B
A= $code->bitsB= $kMinBits
ABresult
1X1
011
000
321 11 ▶ $nbytes = 3 if $nbytes < 3;
Branch: 1 branch
BranchTrueFalseTotal
if $nbytes < 3 0 11 11
322 11 my $val = $code->data;
323 11 ▶ $val = Math::BigInt->bzero() unless defined $val;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $val 0 11 11
324 11 ▶ $val = Math::BigInt->new($val) unless ref $val && $val->isa('Math::BigInt');
Branch: 1 branch
BranchTrueFalseTotal
unless ref $val and $val->isa("Math::BigInt") 0 11 11
Condition33%1/3
ref $val and $val->isa("Math::BigInt")
!ll&&!rl&&r
0011
MC/DC0%0/2
ref $val and $val->isa("Math::BigInt")
ref $val $val->isa("Math::BigInt")
Truth table33%1/3
A and B
A= ref $valB= $val->isa("Math::BigInt")
ABresult
0X0
100
111
325  
326 11 my @bytes;
327 11 my $tmp = $val->copy();
328 11 for (1 .. $nbytes) {
329 91 push @bytes, $tmp->copy->band(0xFF)->numify;
330 91 $tmp = $tmp->brsft(8);
331 }
332 11 @bytes = reverse @bytes; # most significant byte first
333  
334 11 my @flat;
335 11 for my $b (@bytes) {
336 91 push @flat, $kTick; # start bit (mark)
337 91 for my $i (0 .. 7) { # space = 1, mark = 0, LSB-first
338 728 ▶ push @flat, (($b >> $i) & 1) ? -$kTick : $kTick;
Branch: 1 branch
BranchTrueFalseTotal
$b >> $i & 1 ? : 318 410 728
339 }
340 91 push @flat, -$kTick; # stop bit (space)
341 }
342 11 push @flat, -$kFooterGap;
343  
344 11 my @merged;
345 11 for my $v (@flat) {
346 921 ▶ if (@merged && (($merged[-1] > 0) == ($v > 0))) {
Branch: 1 branch
BranchTrueFalseTotal
if (@merged and $merged[-1] > 0 == $v > 0) { } 551 370 921
Condition100%3/3
@merged and $merged[-1] > 0 == $v > 0
!ll&&!rl&&r
11359551
MC/DC100%2/2
@merged and $merged[-1] > 0 == $v > 0
@merged $merged[-1] > 0 == $v > 0
Truth table100%3/3
A and B
A= @mergedB= $merged[-1] > 0 == $v > 0
ABresult
0X0
100
111
347 551 $merged[-1] += $v;
348 } else {
349 370 push @merged, $v;
350 }
351 }
352  
353 11 my @pairs;
354 11 ▶ for (my $i = 0; $i < @merged; $i += 2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @merged) 185 11 196
355 185 ▶ push @pairs, [abs($merged[$i]), abs($merged[$i + 1] // 0)];
Condition50%1/2
$merged[$i + 1] // 0
l!l
1850
MC/DC0%0/1
$merged[$i + 1] // 0
$merged[$i + 1]
Truth table50%1/2
A
A= $merged[$i + 1]
Aresult
11
00
356 }
357  
358 11 my $header = sprintf("0000 %04X %04X 0000", $freq_word, scalar(@pairs));
359 my @payload = map {
360 11 sprintf("%04X %04X", $us_to_pulses->($_->[0]), $us_to_pulses->($_->[1]))
361 } @pairs;
362  
363 11 return join(" ", $header, @payload);
364 }
365  
366 55 1;
367  
368 =encoding utf8
369  
370 =head1 NAME
371  
372 Protocol::IR::Proto::MWM - MWM protocol handler (Disney "Made With Magic")
373  
374 =head1 VERSION
375  
376 version 1.5
377  
378 =head1 SYNOPSIS
379  
380 use Protocol::IR::Converter;
381  
382 my $converter = Protocol::IR::Converter->new();
383  
384 # From a raw display-form value
385 my $code = $converter->import_code('MWM', '0x550808');
386  
387 # From a data parameter (required)
388 my $code = $converter->import_code('MWM', { data => '0x550808' });
389  
390 =head1 DESCRIPTION
391  
392 The MWM protocol ("Made With Magic", used by Disney light-up products such
393 as Glow With The Show) is 2400 bps serial over a 38 kHz carrier, with no
394 header. Each byte is a B<417 µs> start mark, B<8 data bits> (space = 1,
395 mark = 0, LSB-first), and a B<417 µs> stop space; up to 9 same-sign bits may
396 merge into one measured run. Messages are B<3 to 18 bytes> (24-144 bits) and
397 the byte count is implied by the message body: command frames carry a 4-bit
398 payload length in the high nibble of the first byte (B<0x9x>/B<0xFx>), while
399 show commands open with the B<0x55 0xAA> signature.
400  
401 Because the decoded state bytes are exactly the transmitted bytes, the
402 C<data> value is the display form and matches Tasmota's C<Data> field; no
403 byte-order translation applies. Frames can reach 144 bits, which exceeds the
404 native integer range, so C<data> is always a L<Math::BigInt> for this
405 protocol.
406  
407 The C<decode_timing> decoder is strict: it validates the start/stop bits of
408 every byte and enforces the payload-length check, so a genuinely truncated
409 capture is rejected rather than silently misdecoded. A capture whose final
410 byte's stop space merged into the omitted Tasmota footer is back-filled and
411 accepted only when the reconstructed trailing byte passes the checksum.
412  
413 =head1 METHODS
414  
415 =head2 decode_raw
416  
417 my $code = $class->decode_raw('0x550808');
418  
419 Builds an L<Protocol::IR::Code> from the raw display-form value. The frame
420 length is implied by the value's width, rounded up to a whole number of
421 bytes with the 3-byte protocol minimum.
422  
423 =head2 decode_params
424  
425 my $code = $class->decode_params(data => '0x550808');
426  
427 Builds an L<Protocol::IR::Code> from a single required C<data> parameter.
428 Dies without one.
429  
430 =head2 decode_timing
431  
432 my $code = $class->decode_timing(\@burst_pairs_us);
433  
434 Decodes an arrayref of microsecond C<[mark_us, space_us]> pairs. Returns an
435 L<Protocol::IR::Code> when the frame is a well-formed MWM message (valid
436 start/stop bits, implied byte count, 24-144 bits), otherwise C<undef>.
437  
438 =head2 to_pronto
439  
440 my $pronto = $class->to_pronto($ir_code);
441  
442 Encodes an L<Protocol::IR::Code> object as a Pronto Hex string at 38 kHz.
443  
444 =head1 SUPPORT
445  
446 Source code: L<https://github.com/bwarden/perl-protocol-ir>
447  
448 Bug reports and feature requests: L<https://github.com/bwarden/perl-protocol-ir/issues>
449  
450 =head1 AUTHOR
451  
452 Brett T. Warden <bwarden@cpan.org>
453  
454 =head1 COPYRIGHT AND LICENSE
455  
456 Copyright (c) 2026 Brett T. Warden
457  
458 This library is free software; you can redistribute it and/or modify it
459 under the terms of the GNU General Public License as published by the
460 Free Software Foundation; either version 2 of the License, or (at your
461 option) any later version.
« blib/lib/Protocol/IR/Proto/SAMSUNG36.pm ↑ Summary blib/lib/Protocol/IR/Format/Tasmota.pm »