Răsfoiți Sursa

HDMI command

Zachary Kotlarek 10 ani în urmă
părinte
comite
a4904abe65
1 a modificat fișierele cu 135 adăugiri și 0 ștergeri
  1. 135 0
      dmx/HDMIctrl.pl

+ 135 - 0
dmx/HDMIctrl.pl

@@ -0,0 +1,135 @@
+#!/usr/bin/perl
+use strict;
+use warnings;
+use IPC::System::Simple qw( system capture );
+use Time::HiRes qw( usleep sleep time );
+
+# Local modules
+use Cwd qw(abs_path);
+use File::Basename qw(dirname);
+use lib dirname(abs_path($0));
+use DMX;
+
+# Prototypes
+sub sendCmd($$);
+
+# App config
+my $DATA_DIR     = DMX::dataDir();
+my $STATE_SOCK   = 'HDMI_CTRL';
+my $HDMI_SOCK    = 'HDMI';
+my $OUTPUT_FILE  = $DATA_DIR . $STATE_SOCK;
+my $PUSH_TIMEOUT = 20;
+my $PULL_TIMEOUT = $PUSH_TIMEOUT * 3;
+my $DELAY        = $PULL_TIMEOUT / 2;
+my $CMD_DELAY    = 5.0;
+
+# Debug
+my $DEBUG = 0;
+if ($ENV{'DEBUG'}) {
+	$DEBUG = 1;
+}
+
+# Sockets
+DMX::stateSocket($STATE_SOCK);
+DMX::stateSubscribe($STATE_SOCK);
+my $hdmi = DMX::clientSock($HDMI_SOCK);
+
+# State
+my $state     = 'OFF';
+my $source    = 'PLEX';
+my $stateLast = $state;
+my %exists    = ();
+my $pushLast  = 0;
+my $pullLast  = time();
+my $update    = 0;
+my $lastMode  = 0;
+my $lastInput = 0;
+my $lastPower = 0;
+
+# Loop forever
+while (1) {
+
+	# State is calculated; use newState to gather data
+	my $newState = $state;
+
+	# Wait for state updates
+	my $cmdState = DMX::readState($DELAY, \%exists, undef(), undef());
+
+	# Avoid repeated calls to time()
+	my $now = time();
+
+	# Record only valid states
+	if (defined($cmdState)) {
+		$newState = $cmdState;
+		$pullLast = time();
+	}
+
+	# Die if we don't see regular updates
+	if ($now - $pullLast > $PULL_TIMEOUT) {
+		die('No update on state socket in past ' . $PULL_TIMEOUT . " seconds. Exiting...\n");
+	}
+
+	# Calculate the new state
+	$stateLast = $state;
+	if ($exists{'GAME'}) {
+		$state = 'GAME';
+	} else {
+		$state = 'PLEX';
+	}
+
+	# Force updates on a periodic basis
+	if (!$update && $now - $pushLast > $PUSH_TIMEOUT) {
+
+		# Not for HDMI
+		#if ($DEBUG) {
+		#	print STDERR "Forcing periodic update\n";
+		#}
+		#$update = 1;
+	}
+
+	# Force updates on any state change
+	if (!$update && $stateLast ne $state) {
+		if ($DEBUG) {
+			print STDERR 'State change: ' . $stateLast . ' => ' . $state . "\n";
+		}
+		$update = 1;
+	}
+
+	# Force updates when there is a physical state mistmatch
+	if (!$update && $state ne $exists{'HDMI_SOURCE'}) {
+		if ($DEBUG) {
+			print STDERR 'Physical state mismatch: ' . $state . ':' . $exists{'HDMI_SOURCE'} . "\n";
+		}
+		$update = 1;
+	}
+
+	# Only allow updates every few seconds -- the projector goes dumb during power state changes
+	if ($update && $now < $pushLast + $CMD_DELAY) {
+		if ($DEBUG) {
+			print STDERR 'Ignoring overrate update: ' . $state . "\n";
+		}
+		$update = 0;
+	}
+
+	# Update the amp
+	if ($update) {
+
+		# Extra debugging to record pushes
+		if ($DEBUG) {
+			print STDERR 'State: ' . $state . "\n";
+		}
+
+		# Send master power state
+		$hdmi->send($state)
+		  or die('Unable to write command to HDMI socket: ' . $state . ": ${!}\n");
+
+		# Annouce the state change, after the fact
+		#DMX::say('Aitch-Dee-Em-I input' . lc($state));
+
+		# Update the push time
+		$pushLast = time();
+
+		# Clear the update flag
+		$update = 0;
+	}
+}