HDMIctrl.pl 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. #!/usr/bin/perl
  2. use strict;
  3. use warnings;
  4. use IPC::System::Simple qw( system capture );
  5. use Time::HiRes qw( usleep sleep time );
  6. # Local modules
  7. use Cwd qw(abs_path);
  8. use File::Basename qw(dirname);
  9. use lib dirname(abs_path($0));
  10. use DMX;
  11. # Prototypes
  12. sub sendCmd($$);
  13. # App config
  14. my $DATA_DIR = DMX::dataDir();
  15. my $STATE_SOCK = 'HDMI_CTRL';
  16. my $HDMI_SOCK = 'HDMI';
  17. my $OUTPUT_FILE = $DATA_DIR . $STATE_SOCK;
  18. my $PUSH_TIMEOUT = 20;
  19. my $PULL_TIMEOUT = $PUSH_TIMEOUT * 3;
  20. my $DELAY = $PULL_TIMEOUT / 2;
  21. my $CMD_DELAY = 5.0;
  22. my $BOOT_DELAY = 10;
  23. my $BOOT_TIMEOUT = $BOOT_DELAY * 3;
  24. # Debug
  25. my $DEBUG = 0;
  26. if ($ENV{'DEBUG'}) {
  27. $DEBUG = 1;
  28. }
  29. # Sockets
  30. DMX::stateSocket($STATE_SOCK);
  31. DMX::stateSubscribe($STATE_SOCK);
  32. my $hdmi = DMX::clientSock($HDMI_SOCK);
  33. # State
  34. my $state = 'OFF';
  35. my $source = 'PLEX';
  36. my $stateLast = $state;
  37. my %exists = ();
  38. my $pushLast = 0;
  39. my $pullLast = time();
  40. my $update = 0;
  41. my $lastBoot = 0;
  42. # Loop forever
  43. while (1) {
  44. # State is calculated; use newState to gather data
  45. my $newState = $state;
  46. # Wait for state updates
  47. my $cmdState = DMX::readState($DELAY, \%exists, undef(), undef());
  48. # Avoid repeated calls to time()
  49. my $now = time();
  50. # Record only valid states
  51. if (defined($cmdState)) {
  52. $newState = $cmdState;
  53. $pullLast = time();
  54. }
  55. # Die if we don't see regular updates
  56. if ($now - $pullLast > $PULL_TIMEOUT) {
  57. die('No update on state socket in past ' . $PULL_TIMEOUT . " seconds. Exiting...\n");
  58. }
  59. # Calculate the new state
  60. $stateLast = $state;
  61. if ($exists{'GAME'}) {
  62. $state = 'GAME';
  63. } else {
  64. $state = 'PLEX';
  65. }
  66. # Force updates on a periodic basis
  67. if (!$update && $now - $pushLast > $PUSH_TIMEOUT) {
  68. # Not for HDMI
  69. #if ($DEBUG) {
  70. # print STDERR "Forcing periodic update\n";
  71. #}
  72. #$update = 1;
  73. }
  74. # Force updates on any state change
  75. if (!$update && $stateLast ne $state) {
  76. if ($DEBUG) {
  77. print STDERR 'State change: ' . $stateLast . ' => ' . $state . "\n";
  78. }
  79. $update = 1;
  80. }
  81. # Force updates when there is a physical state mistmatch
  82. if (!$update && $state ne $exists{'HDMI_SOURCE'}) {
  83. if ($DEBUG) {
  84. print STDERR 'Physical state mismatch: ' . $state . ':' . $exists{'HDMI_SOURCE'} . "\n";
  85. }
  86. $update = 1;
  87. }
  88. # Only allow updates every few seconds -- the projector goes dumb during power state changes
  89. if ($update && $now < $pushLast + $CMD_DELAY) {
  90. if ($DEBUG) {
  91. print STDERR 'Ignoring overrate update: ' . $state . "\n";
  92. }
  93. $update = 0;
  94. }
  95. # Update the amp
  96. if ($update) {
  97. # Extra debugging to record pushes
  98. if ($DEBUG) {
  99. print STDERR 'State: ' . $state . "\n";
  100. }
  101. # Reboot before the first update to GAME
  102. if ($state eq 'GAME' && $lastBoot < $now - $BOOT_TIMEOUT) {
  103. $hdmi->send('REBOOT')
  104. or die('Unable to write command to HDMI socket: ' . $state . ": ${!}\n");
  105. # Wait for the reboot
  106. sleep($BOOT_DELAY);
  107. $now += $BOOT_DELAY;
  108. $lastBoot = $now;
  109. }
  110. # Send master power state
  111. $hdmi->send($state)
  112. or die('Unable to write command to HDMI socket: ' . $state . ": ${!}\n");
  113. # Annouce the state change, after the fact
  114. #DMX::say('Aitch-Dee-Em-I input' . lc($state));
  115. # Update the push time
  116. $pushLast = $now;
  117. # Clear the update flag
  118. $update = 0;
  119. }
  120. }