amplifierPower.pl 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. #!/usr/bin/perl
  2. use strict;
  3. use warnings;
  4. # Local modules
  5. use Cwd qw(abs_path);
  6. use File::Basename qw(dirname);
  7. use lib dirname(abs_path($0));
  8. use DMX;
  9. # Config
  10. my $USB_DEV = 'USB Audio CODEC ';
  11. my $AMP_DEV = 'Built-in Output';
  12. my $DEFAULT_DEV = $USB_DEV;
  13. my $RAVE_DEV = $AMP_DEV;
  14. # App config
  15. my $DATA_DIR = DMX::dataDir();
  16. my $OUTPUT_FILE = $DATA_DIR . 'AMPLIFIER_POWER';
  17. my $STATE_SOCK = $OUTPUT_FILE . '.socket';
  18. my $AMP_SOCK = $DATA_DIR . 'AMPLIFIER.socket';
  19. my $PUSH_TIMEOUT = 20;
  20. my $PULL_TIMEOUT = $PUSH_TIMEOUT * 3;
  21. my $DELAY = $PULL_TIMEOUT / 2;
  22. my $START_DELAY = 3.5;
  23. my @AUDIO_SET = ('/Users/tv/bin/SwitchAudioSource', '-s');
  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 $amp = DMX::clientSock($AMP_SOCK);
  33. # State
  34. my $state = 'OFF';
  35. my $stateLast = $state;
  36. my %exists = ();
  37. my $pushLast = 0;
  38. my $pullLast = time();
  39. my $update = 0;
  40. # Loop forever
  41. while (1) {
  42. # State is calculated; use newState to gather data
  43. my $newState = $state;
  44. # Wait for state updates
  45. my $cmdState = DMX::readState($DELAY, \%exists, undef(), undef());
  46. if (defined($cmdState)) {
  47. $newState = $cmdState;
  48. $pullLast = time();
  49. }
  50. # Die if we don't see regular updates
  51. if (time() - $pullLast > $PULL_TIMEOUT) {
  52. die('No update on state socket in past ' . $PULL_TIMEOUT . " seconds. Exiting...\n");
  53. }
  54. # Calculate the new state
  55. $stateLast = $state;
  56. if ($newState eq 'PLAY') {
  57. $state = 'ON';
  58. } elsif ($exists{'RAVE'}) {
  59. $state = 'RAVE';
  60. } elsif ($newState eq 'PAUSE') {
  61. $state = 'ON';
  62. } else {
  63. $state = 'OFF';
  64. }
  65. # Force updates on a periodic basis
  66. if (!$update && time() - $pushLast > $PUSH_TIMEOUT) {
  67. # Not for the amp
  68. #if ($DEBUG) {
  69. # print STDERR "Forcing periodic update\n";
  70. #}
  71. #$update = 1;
  72. }
  73. # Force updates on any state change
  74. if (!$update && $stateLast ne $state) {
  75. if ($DEBUG) {
  76. print STDERR 'State change: ' . $stateLast . ' => ' . $state . "\n";
  77. }
  78. $update = 1;
  79. }
  80. # Force updates when there is a physical state mistmatch
  81. if (!$update) {
  82. if (($state eq 'OFF' && $exists{'AMPLIFIER'}) || ($state eq 'ON' && !$exists{'AMPLIFIER'})) {
  83. if ($DEBUG) {
  84. print STDERR 'Physical state mismatch: ' . $state . ':' . $exists{'AMPLIFIER'} . "\n";
  85. }
  86. $update = 1;
  87. }
  88. }
  89. # Update the amp
  90. if ($update) {
  91. # Extra debugging to record pushes
  92. if ($DEBUG) {
  93. print STDERR 'State: ' . $state . "\n";
  94. }
  95. # Send master power state
  96. my $cmd = undef();
  97. if ($state eq 'OFF' || $state eq 'ON') {
  98. $cmd = $state;
  99. } elsif ($state eq 'RAVE') {
  100. $cmd = 'ON';
  101. }
  102. if (defined($cmd)) {
  103. $amp->send($cmd)
  104. or die('Unable to write command to amp socket: ' . $cmd . ": ${!}\n");
  105. }
  106. # Select an output device, changing if needed
  107. my $outDev = undef();
  108. if ($state eq 'RAVE') {
  109. $outDev = $RAVE_DEV;
  110. } elsif (defined($cmd) && $cmd eq 'ON') {
  111. $outDev = $DEFAULT_DEV;
  112. }
  113. if (defined($outDev)) {
  114. if ($DEBUG) {
  115. print STDERR 'Selecting output device: ' . $outDev . "\n";
  116. }
  117. my @CMD = @AUDIO_SET;
  118. push(@CMD, $outDev);
  119. system(@CMD);
  120. }
  121. # Reset to TV @ 5.1 at power on
  122. if (defined($cmd) && $cmd eq 'ON') {
  123. if ($DEBUG) {
  124. print STDERR "Selecting ON mode\n";
  125. }
  126. # Wait for the amp to boot
  127. if (!$exists{'AMPLIFIER'}) {
  128. sleep($START_DELAY);
  129. }
  130. # Set the mode
  131. $amp->send('TV')
  132. or die('Unable to write command to amp socket: TV' . ": ${!}\n");
  133. $amp->send('SURROUND')
  134. or die('Unable to write command to amp socket: SURROUND' . ": ${!}\n");
  135. }
  136. # Rave through the main amp
  137. if ($state eq 'RAVE') {
  138. if ($DEBUG) {
  139. print STDERR "Selecting RAVE mode\n";
  140. }
  141. # Set the mode
  142. $amp->send('TV')
  143. or die('Unable to write command to amp socket: TV' . ": ${!}\n");
  144. $amp->send('STEREO')
  145. or die('Unable to write command to amp socket: STEREO' . ": ${!}\n");
  146. }
  147. # No output file
  148. # Update the push time
  149. $pushLast = time();
  150. # Clear the update flag
  151. $update = 0;
  152. }
  153. }