Skip to content

Instantly share code, notes, and snippets.

@dpavlin
Created November 18, 2017 08:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dpavlin/aab4be5d03fc7ef51280550a290e7aa9 to your computer and use it in GitHub Desktop.
Save dpavlin/aab4be5d03fc7ef51280550a290e7aa9 to your computer and use it in GitHub Desktop.
Control TV using command line tool on raspberry pi
#!/usr/bin/perl
use warnings;
use strict;
use Term::ReadKey;
use Data::Dump qw(dump);
my $direction = {
A => 'UP',
B => 'DOWN',
C => 'RIGHT',
D => 'LEFT',
};
sub ir {
my $key = shift;
my $cmd = "irsend SEND_ONCE LG_42H3000 $key";
warn "# $cmd\n";
system($cmd);
}
ReadMode 4; # Turn off controls keys
my $key;
my $esc;
while(1) {
while (not defined ($key = ReadKey(-1))) {
# No key yet
}
print dump($key, $esc), $/;
if ( $key eq 'q' ) {
ReadMode 0; # Reset tty mode before exiting
exit 0;
}
if ( $key eq "\e" ) {
$esc = $key;
} elsif ( $esc ) {
$esc .= $key;
}
if ( $esc && $esc =~ /\e\[([ABCD])/ ) {
printf "arrow: %s %s\n", $1, $direction->{$1};
ir('KEY_' . $direction->{$1});
$esc = '';
}
ir('KEY_MENU') if $key eq 'm';
ir('KEY_INFO') if $key eq 'i';
ir('KEY_POWER') if $key eq 'p';
ir('KEY_OK') if $key eq 'o' || $key eq "\n" || $key eq " ";
ir('KEY_EXIT') if $key eq "\x7F"; # backspace
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment