| 1 | package Protocol::IR::Proto::RC5; | ||||||||||||||||||||||||||||||||
| 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 | 0 | ▶ | sub new { bless {}, shift } | ||||||||||||||||||||||||||||||
|
Subroutine
new: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||||||
| 11 | 0 | ▶ | sub _half { 889 } | ||||||||||||||||||||||||||||||
|
Subroutine
_half: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||||||
| 13 | 1 | ▶ | sub _manchester_bits { | ||||||||||||||||||||||||||||||
|
Subroutine
_manchester_bits: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||||||
| 14 | 1 | my ($class, $toggle, $address, $command) = @_; | |||||||||||||||||||||||||||||||
| 15 | 1 | ▶ | my $start2 = ($command & 0x40) ? 0 : 1; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 16 | 1 | my $cmd6 = $command & 0x3f; | |||||||||||||||||||||||||||||||
| 17 | 1 | my @bits = (1, $start2, $toggle & 1); | |||||||||||||||||||||||||||||||
| 18 | 1 | for my $i (reverse 0 .. 4) { push @bits, ($address >> $i) & 1; } | |||||||||||||||||||||||||||||||
| 19 | 1 | for my $i (reverse 0 .. 5) { push @bits, ($cmd6 >> $i) & 1; } | |||||||||||||||||||||||||||||||
| 20 | 1 | return @bits; | |||||||||||||||||||||||||||||||
| 21 | } | ||||||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||||||
| 23 | 1 | ▶ | sub decode_raw { | ||||||||||||||||||||||||||||||
|
Subroutine
decode_raw: called CC=2 SCAR=7.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 24 | 1 | my ($class, $raw) = @_; | |||||||||||||||||||||||||||||||
| 25 | 1 | ▶ | my $val = $raw =~ /^0x/i ? hex($raw) : $raw + 0; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 26 | 1 | my $toggle = ($val >> 11) & 1; | |||||||||||||||||||||||||||||||
| 27 | 1 | my $address = ($val >> 6) & 0x1f; | |||||||||||||||||||||||||||||||
| 28 | 1 | my $command = $val & 0x3f; | |||||||||||||||||||||||||||||||
| 29 | 1 | return Protocol::IR::Code->new( | |||||||||||||||||||||||||||||||
| 30 | protocol => 'RC5', | ||||||||||||||||||||||||||||||||
| 31 | bits => 12, | ||||||||||||||||||||||||||||||||
| 32 | address => $address, | ||||||||||||||||||||||||||||||||
| 33 | subaddress => -1, | ||||||||||||||||||||||||||||||||
| 34 | command => $command, | ||||||||||||||||||||||||||||||||
| 35 | data => $val, | ||||||||||||||||||||||||||||||||
| 36 | ); | ||||||||||||||||||||||||||||||||
| 37 | } | ||||||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||||||
| 39 | 1 | ▶ | sub decode_params { | ||||||||||||||||||||||||||||||
|
Subroutine
decode_params: called CC=7 SCAR=21.8
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 40 | 1 | my ($class, %args) = @_; | |||||||||||||||||||||||||||||||
| 41 | 1 | ▶ | my $addr = $args{address} // $args{device} // 0; | ||||||||||||||||||||||||||||||
|
Condition40%2/5
($args{"address"} // $args{"device"}) // 0
$args{"address"} // $args{"device"}
MC/DC0%0/2
($args{"address"} // $args{"device"}) // 0
$args{"address"}
$args{"device"}
Truth table33%1/3
A // B
A= $args{"address"}B= $args{"device"}
| |||||||||||||||||||||||||||||||||
| 42 | 1 | ▶ | $addr = hex($addr) if $addr =~ /^0x/i; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 43 | 1 | ▶ | my $cmd = $args{command} // $args{function} // 0; | ||||||||||||||||||||||||||||||
|
Condition40%2/5
($args{"command"} // $args{"function"}) // 0
$args{"command"} // $args{"function"}
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"}
| |||||||||||||||||||||||||||||||||
| 44 | 1 | ▶ | $cmd = hex($cmd) if $cmd =~ /^0x/i; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 45 | 1 | $addr &= 0x1f; | |||||||||||||||||||||||||||||||
| 46 | 1 | $cmd &= 0x7f; | |||||||||||||||||||||||||||||||
| 47 | 1 | my $toggle = 0; | |||||||||||||||||||||||||||||||
| 48 | 1 | my $data = ($toggle << 11) | ($addr << 6) | ($cmd & 0x3f); | |||||||||||||||||||||||||||||||
| 49 | 1 | return Protocol::IR::Code->new( | |||||||||||||||||||||||||||||||
| 50 | protocol => 'RC5', | ||||||||||||||||||||||||||||||||
| 51 | bits => 12, | ||||||||||||||||||||||||||||||||
| 52 | address => $addr, | ||||||||||||||||||||||||||||||||
| 53 | subaddress => -1, | ||||||||||||||||||||||||||||||||
| 54 | command => $cmd, | ||||||||||||||||||||||||||||||||
| 55 | data => $data, | ||||||||||||||||||||||||||||||||
| 56 | ); | ||||||||||||||||||||||||||||||||
| 57 | } | ||||||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||||||
| 59 | 169 | ▶ | sub decode_timing { | ||||||||||||||||||||||||||||||
|
Subroutine
decode_timing: called CC=26 SCAR=32.8
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 60 | 169 | my ($class, $pairs) = @_; | |||||||||||||||||||||||||||||||
| 61 | 169 | ▶ | return undef unless ref $pairs eq 'ARRAY' && @$pairs; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
ref $pairs eq "ARRAY" and @$pairs
MC/DC50%1/2
ref $pairs eq "ARRAY" and @$pairs
ref $pairs eq "ARRAY"
@$pairs
Truth table66%2/3
A and B
A= ref $pairs eq "ARRAY"B= @$pairs
| |||||||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||||||
| 63 | # Convert pairs to signed halves. | ||||||||||||||||||||||||||||||||
| 64 | 168 | my @signed; | |||||||||||||||||||||||||||||||
| 65 | 168 | for my $p (@$pairs) { | |||||||||||||||||||||||||||||||
| 66 | 2427 | my ($m, $s) = @$p; | |||||||||||||||||||||||||||||||
| 67 | 2427 | ▶ | if ($m > 0) { push @signed, $m; } | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 68 | 2427 | ▶ | if ($s > 0) { push @signed, -$s; } | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 69 | } | ||||||||||||||||||||||||||||||||
| 70 | # Expand merged magnitudes into 889 units. | ||||||||||||||||||||||||||||||||
| 71 | 168 | my @expanded; | |||||||||||||||||||||||||||||||
| 72 | 168 | for my $t (@signed) { | |||||||||||||||||||||||||||||||
| 73 | 4693 | my $n = int((abs($t) + 444) / 889); | |||||||||||||||||||||||||||||||
| 74 | 4693 | ▶ | my $sign = $t >= 0 ? 1 : -1; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 75 | 4693 | for (1 .. $n) { push @expanded, $sign * 889; } | |||||||||||||||||||||||||||||||
| 76 | } | ||||||||||||||||||||||||||||||||
| 77 | # strip leading/trailing spaces | ||||||||||||||||||||||||||||||||
| 78 | 168 | ▶ | while (@expanded && $expanded[0] < 0) { shift @expanded; } | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
@expanded and $expanded[0] < 0
MC/DC100%2/2
@expanded and $expanded[0] < 0
@expanded
$expanded[0] < 0
Truth table100%3/3
A and B
A= @expandedB= $expanded[0] < 0
| |||||||||||||||||||||||||||||||||
| 79 | 168 | ▶ | while (@expanded && $expanded[-1] < 0) { pop @expanded; } | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
@expanded and $expanded[-1] < 0
MC/DC100%2/2
@expanded and $expanded[-1] < 0
@expanded
$expanded[-1] < 0
Truth table100%3/3
A and B
A= @expandedB= $expanded[-1] < 0
| |||||||||||||||||||||||||||||||||
| 80 | 168 | ▶ | return undef unless @expanded; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 81 | 146 | ▶ | return undef unless $expanded[0] > 0; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||||||
| 83 | 146 | my @bits = (1); | |||||||||||||||||||||||||||||||
| 84 | 146 | my $i = 1; | |||||||||||||||||||||||||||||||
| 85 | 146 | ▶ | while ($i + 1 < @expanded) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 86 | 205 | my ($a, $b) = @expanded[$i, $i + 1]; | |||||||||||||||||||||||||||||||
| 87 | 205 | ▶ | if ($a < 0 && $b > 0) { push @bits, 1; } | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition100%3/3
$a < 0 and $b > 0
MC/DC100%2/2
$a < 0 and $b > 0
$a < 0
$b > 0
Truth table100%3/3
A and B
A= $a < 0B= $b > 0
| |||||||||||||||||||||||||||||||||
| 88 | 52 | ▶ | elsif ($a > 0 && $b < 0) { push @bits, 0; } | ||||||||||||||||||||||||||||||
|
Condition100%3/3
$a > 0 and $b < 0
MC/DC100%2/2
$a > 0 and $b < 0
$a > 0
$b < 0
Truth table100%3/3
A and B
A= $a > 0B= $b < 0
| |||||||||||||||||||||||||||||||||
| 89 | 141 | else { return undef; } | |||||||||||||||||||||||||||||||
| 90 | 64 | $i += 2; | |||||||||||||||||||||||||||||||
| 91 | } | ||||||||||||||||||||||||||||||||
| 92 | 5 | ▶ | if ($i < @expanded) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 93 | 5 | ▶ | if ($expanded[$i] > 0) { push @bits, 0; } | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 94 | 0 | else { return undef; } | |||||||||||||||||||||||||||||||
| 95 | } | ||||||||||||||||||||||||||||||||
| 96 | 5 | ▶ | return undef unless @bits == 14; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||||||
| 98 | 5 | my $s2 = $bits[1]; | |||||||||||||||||||||||||||||||
| 99 | 5 | my $toggle = $bits[2]; | |||||||||||||||||||||||||||||||
| 100 | 5 | my $address = 0; | |||||||||||||||||||||||||||||||
| 101 | 5 | for my $k (0 .. 4) { $address = ($address << 1) | $bits[3 + $k]; } | |||||||||||||||||||||||||||||||
| 102 | 5 | my $command = 0; | |||||||||||||||||||||||||||||||
| 103 | 5 | for my $k (0 .. 5) { $command = ($command << 1) | $bits[8 + $k]; } | |||||||||||||||||||||||||||||||
| 104 | 0 | ▶ | if ($s2 == 0) { $command |= 0x40; } | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 105 | 5 | my $data = ($toggle << 11) | ($address << 6) | ($command & 0x3f); | |||||||||||||||||||||||||||||||
| 106 | 5 | return Protocol::IR::Code->new( | |||||||||||||||||||||||||||||||
| 107 | protocol => 'RC5', | ||||||||||||||||||||||||||||||||
| 108 | bits => 12, | ||||||||||||||||||||||||||||||||
| 109 | address => $address, | ||||||||||||||||||||||||||||||||
| 110 | subaddress => -1, | ||||||||||||||||||||||||||||||||
| 111 | command => $command, | ||||||||||||||||||||||||||||||||
| 112 | data => $data, | ||||||||||||||||||||||||||||||||
| 113 | ); | ||||||||||||||||||||||||||||||||
| 114 | } | ||||||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||||||
| 116 | 1 | ▶ | sub to_pronto { | ||||||||||||||||||||||||||||||
|
Subroutine
to_pronto: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 117 | 1 | my ($class, $code) = @_; | |||||||||||||||||||||||||||||||
| 118 | 1 | my $toggle = ($code->{data} >> 11) & 1; | |||||||||||||||||||||||||||||||
| 119 | 1 | my $address = $code->{address} & 0x1f; | |||||||||||||||||||||||||||||||
| 120 | 1 | my $command = $code->{command} & 0x7f; | |||||||||||||||||||||||||||||||
| 121 | 1 | my @bits = $class->_manchester_bits($toggle, $address, $command); | |||||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||||||
| 123 | 1 | my $carrier_hz = 36000; | |||||||||||||||||||||||||||||||
| 124 | 1 | my $freq_word = int(1000000.0 / ($carrier_hz * 0.241246) + 0.5); | |||||||||||||||||||||||||||||||
| 125 | 1 | my $period_us = $freq_word * 0.241246; | |||||||||||||||||||||||||||||||
| 126 | 1 | ▶ | my $us_to_pulses = sub { int($_[0] / $period_us + 0.5) }; | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||||||
| 128 | 1 | my @pronto_pairs; | |||||||||||||||||||||||||||||||
| 129 | 1 | for my $bit (@bits) { | |||||||||||||||||||||||||||||||
| 130 | 14 | ▶ | if ($bit == 0) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 131 | 11 | push @pronto_pairs, [ $us_to_pulses->(889), $us_to_pulses->(889) ]; | |||||||||||||||||||||||||||||||
| 132 | } else { | ||||||||||||||||||||||||||||||||
| 133 | 3 | push @pronto_pairs, [ 0, $us_to_pulses->(889) ]; | |||||||||||||||||||||||||||||||
| 134 | 3 | push @pronto_pairs, [ $us_to_pulses->(889), 0 ]; | |||||||||||||||||||||||||||||||
| 135 | } | ||||||||||||||||||||||||||||||||
| 136 | } | ||||||||||||||||||||||||||||||||
| 137 | 1 | my $seq1 = scalar(@pronto_pairs); | |||||||||||||||||||||||||||||||
| 138 | 1 | my $header = sprintf('0000 %04X %04X 0000', $freq_word, $seq1); | |||||||||||||||||||||||||||||||
| 139 | 1 | my $payload = join(' ', map { sprintf('%04X %04X', $_->[0], $_->[1]) } @pronto_pairs); | |||||||||||||||||||||||||||||||
| 140 | 1 | return "$header $payload"; | |||||||||||||||||||||||||||||||
| 141 | } | ||||||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||||||
| 143 | 55 | 1; | |||||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||||||
| 145 | =head1 NAME | ||||||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||||||
| 147 | Protocol::IR::Proto::RC5 - IR protocol handler | ||||||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||||||
| 149 | =head1 VERSION | ||||||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||||||
| 151 | version 1.5 | ||||||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||||||
| 153 | =cut | ||||||||||||||||||||||||||||||||