| 1 | #!/usr/bin/env perl | ||||||||||||||||||||||||||||||
| 2 | 19 | ▶ | use strict; | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 3 | 19 | ▶ | use warnings; | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 4 | 19 | ▶ | use v5.10; | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||||
| 6 | 19 | ▶ | use FindBin; | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 7 | 19 | ▶ | use lib "$FindBin::Bin/../lib"; | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||||
| 9 | 19 | ▶ | use Getopt::Long qw(GetOptions :config no_ignore_case); | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 10 | 19 | ▶ | use Protocol::IR::Converter; | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||||
| 12 | 19 | my $USAGE = <<'USAGE'; | |||||||||||||||||||||||||||||
| 13 | ir-convert - convert IR remote codes between the formats Protocol::IR::Code supports | ||||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||||
| 15 | Reads a signal (or set of signals) in one registered format and writes it | ||||||||||||||||||||||||||||||
| 16 | out in another. Every code is normalized to an Protocol::IR::Code object in between, | ||||||||||||||||||||||||||||||
| 17 | so decoded signals survive the trip losslessly. | ||||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||||
| 19 | Formats: | ||||||||||||||||||||||||||||||
| 20 | csv IRDB-style button listing (import/export) | ||||||||||||||||||||||||||||||
| 21 | wig HAIR hair-wig/3 JSON, one file per remote (import/export) | ||||||||||||||||||||||||||||||
| 22 | pronto Raw Pronto Hex, one signal per code (import/export) | ||||||||||||||||||||||||||||||
| 23 | tasmota Tasmota IRsend raw data, one signal per line (import/export) | ||||||||||||||||||||||||||||||
| 24 | mode2 LIRC mode2 pulse/space capture, one timing per line (import/export) | ||||||||||||||||||||||||||||||
| 25 | lirc LIRC remote definition (.lircd.conf), protocol or raw codes | ||||||||||||||||||||||||||||||
| 26 | json JSON IR database dump, "commands" list or a device file | ||||||||||||||||||||||||||||||
| 27 | pointing at its code set (import only) | ||||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||||
| 29 | Usage: | ||||||||||||||||||||||||||||||
| 30 | ir-convert [options] --from FORMAT --to FORMAT | ||||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||||
| 32 | Options: | ||||||||||||||||||||||||||||||
| 33 | --from FORMAT Input format (required): csv, wig, pronto, tasmota, mode2, lirc, json | ||||||||||||||||||||||||||||||
| 34 | --to FORMAT Output format (required): csv, wig, pronto, tasmota, mode2, lirc | ||||||||||||||||||||||||||||||
| 35 | --in FILE Read input from FILE instead of stdin | ||||||||||||||||||||||||||||||
| 36 | --out FILE Write output to FILE instead of stdout | ||||||||||||||||||||||||||||||
| 37 | --name NAME wig remote name (default: Untitled) | ||||||||||||||||||||||||||||||
| 38 | --brand BRAND wig manufacturer/brand | ||||||||||||||||||||||||||||||
| 39 | --model MODEL wig model | ||||||||||||||||||||||||||||||
| 40 | --kind KIND wig device kind slug, e.g. tv, soundbar, ac | ||||||||||||||||||||||||||||||
| 41 | --notes TEXT wig free-form notes | ||||||||||||||||||||||||||||||
| 42 | --overwrite Allow --out to overwrite an existing file | ||||||||||||||||||||||||||||||
| 43 | -h, --help Show this help | ||||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||||
| 45 | Examples: | ||||||||||||||||||||||||||||||
| 46 | ir-convert --from csv --to wig --in remote.csv --out remote.wig.json \ | ||||||||||||||||||||||||||||||
| 47 | --name "Acme TV" --brand Acme --kind tv | ||||||||||||||||||||||||||||||
| 48 | cat capture.tasmota | ir-convert --from tasmota --to pronto | ||||||||||||||||||||||||||||||
| 49 | ir-convert --from wig --to tasmota --in remote.wig.json | ||||||||||||||||||||||||||||||
| 50 | ir-convert --from lirc --to pronto --in remote.lircd.conf | ||||||||||||||||||||||||||||||
| 51 | ir-convert --from json --to wig --in dump.json --name "Vacuum" | ||||||||||||||||||||||||||||||
| 52 | USAGE | ||||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||||
| 54 | 19 | my %opt; | |||||||||||||||||||||||||||||
| 55 | GetOptions( | ||||||||||||||||||||||||||||||
| 56 | 'from=s' => \$opt{from}, | ||||||||||||||||||||||||||||||
| 57 | 'to=s' => \$opt{to}, | ||||||||||||||||||||||||||||||
| 58 | 'in=s' => \$opt{in}, | ||||||||||||||||||||||||||||||
| 59 | 'out=s' => \$opt{out}, | ||||||||||||||||||||||||||||||
| 60 | 'name=s' => \$opt{name}, | ||||||||||||||||||||||||||||||
| 61 | 'brand=s' => \$opt{brand}, | ||||||||||||||||||||||||||||||
| 62 | 'model=s' => \$opt{model}, | ||||||||||||||||||||||||||||||
| 63 | 'kind=s' => \$opt{kind}, | ||||||||||||||||||||||||||||||
| 64 | 'notes=s' => \$opt{notes}, | ||||||||||||||||||||||||||||||
| 65 | 'overwrite!' => \$opt{overwrite}, | ||||||||||||||||||||||||||||||
| 66 | 'help|h' => \$opt{help}, | ||||||||||||||||||||||||||||||
| 67 | 19 | ▶ | ) or usage_error(); | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||||
| 69 | 19 | ▶ | if ($opt{help}) { | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 70 | 0 | print $USAGE; | |||||||||||||||||||||||||||||
| 71 | 0 | exit 0; | |||||||||||||||||||||||||||||
| 72 | } | ||||||||||||||||||||||||||||||
| 73 | 19 | ▶ | usage_error("--from and --to are required") unless $opt{from} && $opt{to}; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$opt{"from"} and $opt{"to"}
MC/DC50%1/2
$opt{"from"} and $opt{"to"}
$opt{"from"}
$opt{"to"}
Truth table66%2/3
A and B
A= $opt{"from"}B= $opt{"to"}
| |||||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||||
| 75 | 18 | my %importable = map { $_ => 1 } qw(csv wig pronto tasmota mode2 lirc json); | |||||||||||||||||||||||||||||
| 76 | 18 | my %exportable = map { $_ => 1 } qw(csv wig pronto tasmota mode2 lirc); | |||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||||
| 78 | usage_error("unsupported --from '" . $opt{from} . "' (choose: csv, wig, pronto, tasmota, mode2, lirc, json)") | ||||||||||||||||||||||||||||||
| 79 | 18 | ▶ | unless $importable{lc $opt{from}}; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 80 | usage_error("unsupported or non-exportable --to '" . $opt{to} . "' (choose: csv, wig, pronto, tasmota, mode2, lirc)") | ||||||||||||||||||||||||||||||
| 81 | 18 | ▶ | unless $exportable{lc $opt{to}}; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||||
| 83 | 18 | my $converter = Protocol::IR::Converter->new(); | |||||||||||||||||||||||||||||
| 84 | 18 | my $from = lc $opt{from}; | |||||||||||||||||||||||||||||
| 85 | 18 | my $to = lc $opt{to}; | |||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||||
| 87 | # A JSON dump's device document names a code set by path, so the formats | ||||||||||||||||||||||||||||||
| 88 | # that resolve such references are handed the file path, not its contents. | ||||||||||||||||||||||||||||||
| 89 | 18 | my %path_aware = map { $_ => 1 } qw(json wig); | |||||||||||||||||||||||||||||
| 90 | 18 | ▶ | my $input = defined $opt{in} && $path_aware{$from} ? $opt{in} : read_input($opt{in}); | ||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $opt{"in"} and $path_aware{$from}
MC/DC100%2/2
defined $opt{"in"} and $path_aware{$from}
defined $opt{"in"}
$path_aware{$from}
Truth table100%3/3
A and B
A= defined $opt{"in"}B= $path_aware{$from}
| |||||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||||
| 92 | 17 | my $codes = $converter->import_format($from, $input); | |||||||||||||||||||||||||||||
| 93 | 16 | ▶ | $codes = [ $codes ] unless ref $codes eq 'ARRAY'; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 94 | 16 | ▶ | die "No signals decoded from the input\n" unless @$codes; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||||
| 96 | 16 | my $output; | |||||||||||||||||||||||||||||
| 97 | 16 | ▶ | if ($to eq 'wig') { | ||||||||||||||||||||||||||||
|
Branch: 6 branches
| |||||||||||||||||||||||||||||||
| 98 | 5 | ▶ | my %meta = (origin => 'converted:ir-convert', name => $opt{name} // 'Untitled'); | ||||||||||||||||||||||||||||
|
Condition50%1/2
$opt{"name"} // "Untitled"
MC/DC0%0/1
$opt{"name"} // "Untitled"
$opt{"name"}
Truth table50%1/2
A
A= $opt{"name"}
| |||||||||||||||||||||||||||||||
| 99 | 5 | for my $k (qw(brand model kind notes)) { | |||||||||||||||||||||||||||||
| 100 | 20 | ▶ | $meta{$k} = $opt{$k} if defined $opt{$k}; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 101 | } | ||||||||||||||||||||||||||||||
| 102 | 5 | $output = $converter->export_codes('wig', $codes, %meta); | |||||||||||||||||||||||||||||
| 103 | } elsif ($to eq 'csv') { | ||||||||||||||||||||||||||||||
| 104 | 0 | $output = $converter->export_codes('CSV', $codes); | |||||||||||||||||||||||||||||
| 105 | } elsif ($to eq 'pronto') { | ||||||||||||||||||||||||||||||
| 106 | 6 | $output = join("\n", map { $converter->export_code($_, 'Pronto') } @$codes) . "\n"; | |||||||||||||||||||||||||||||
| 107 | } elsif ($to eq 'tasmota') { | ||||||||||||||||||||||||||||||
| 108 | 2 | $output = join("\n", map { $converter->export_code($_, 'Tasmota') } @$codes) . "\n"; | |||||||||||||||||||||||||||||
| 109 | } elsif ($to eq 'mode2') { | ||||||||||||||||||||||||||||||
| 110 | 2 | $output = $converter->export_codes('Mode2', $codes); | |||||||||||||||||||||||||||||
| 111 | } elsif ($to eq 'lirc') { | ||||||||||||||||||||||||||||||
| 112 | 1 | ▶ | $output = $converter->export_codes('LIRC', $codes, name => $opt{name} // 'converted'); | ||||||||||||||||||||||||||||
|
Condition50%1/2
$opt{"name"} // "converted"
MC/DC0%0/1
$opt{"name"} // "converted"
$opt{"name"}
Truth table50%1/2
A
A= $opt{"name"}
| |||||||||||||||||||||||||||||||
| 113 | } | ||||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||||
| 115 | 16 | write_output($output, $opt{out}, $opt{overwrite}); | |||||||||||||||||||||||||||||
| 116 | 16 | exit 0; | |||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||||
| 118 | # ---------------------------------------------------------------------- | ||||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||||
| 120 | 14 | ▶ | sub read_input { | ||||||||||||||||||||||||||||
|
Subroutine
read_input: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||||
| 121 | 14 | my ($file) = @_; | |||||||||||||||||||||||||||||
| 122 | 14 | ▶ | if (defined $file) { | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 123 | 13 | ▶ | open my $fh, '<:raw', $file or die "Cannot open '$file': $!\n"; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 124 | 12 | local $/; | |||||||||||||||||||||||||||||
| 125 | 12 | my $text = <$fh>; | |||||||||||||||||||||||||||||
| 126 | 12 | close $fh; | |||||||||||||||||||||||||||||
| 127 | 12 | return $text; | |||||||||||||||||||||||||||||
| 128 | } | ||||||||||||||||||||||||||||||
| 129 | 1 | local $/; | |||||||||||||||||||||||||||||
| 130 | 1 | return scalar <STDIN>; | |||||||||||||||||||||||||||||
| 131 | } | ||||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||||
| 133 | 16 | ▶ | sub write_output { | ||||||||||||||||||||||||||||
|
Subroutine
write_output: called CC=5 SCAR=26.7
| |||||||||||||||||||||||||||||||
| 134 | 16 | my ($text, $out, $overwrite) = @_; | |||||||||||||||||||||||||||||
| 135 | 16 | ▶ | if (defined $out) { | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 136 | 0 | ▶ | die "Refusing to overwrite '$out' (use --overwrite)\n" | ||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
-e $out and not $overwrite
MC/DC0%0/2
-e $out and not $overwrite
-e $out
not $overwrite
Truth table0%0/3
A and not(B)
A= -e $outB= not $overwrite
| |||||||||||||||||||||||||||||||
| 137 | if -e $out && !$overwrite; | ||||||||||||||||||||||||||||||
| 138 | 0 | ▶ | open my $fh, '>:utf8', $out or die "Cannot write '$out': $!\n"; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 139 | 0 | print {$fh} $text; | |||||||||||||||||||||||||||||
| 140 | 0 | close $fh; | |||||||||||||||||||||||||||||
| 141 | } else { | ||||||||||||||||||||||||||||||
| 142 | 16 | binmode STDOUT, ':utf8'; | |||||||||||||||||||||||||||||
| 143 | 16 | print $text; | |||||||||||||||||||||||||||||
| 144 | } | ||||||||||||||||||||||||||||||
| 145 | } | ||||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||||
| 147 | 1 | ▶ | sub usage_error { | ||||||||||||||||||||||||||||
|
Subroutine
usage_error: called CC=2 SCAR=7.0
| |||||||||||||||||||||||||||||||
| 148 | 1 | my ($msg) = @_; | |||||||||||||||||||||||||||||
| 149 | 1 | ▶ | print STDERR "$msg\n\n" if $msg; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 150 | 1 | print STDERR $USAGE; | |||||||||||||||||||||||||||||
| 151 | 1 | exit 2; | |||||||||||||||||||||||||||||
| 152 | } | ||||||||||||||||||||||||||||||