amplifierPower.pl 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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 = ('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
  107. if ($state eq 'RAVE') {
  108. my @CMD = @AUDIO_SET;
  109. push(@CMD, $RAVE_DEV);
  110. system(@CMD);
  111. } else {
  112. if (defined($cmd) && $cmd eq 'ON') {
  113. # Set the audio source back to the default
  114. my @CMD = @AUDIO_SET;
  115. push(@CMD, $DEFAULT_DEV);
  116. system(@CMD);
  117. }
  118. }
  119. # Reset to TV @ 5.1 at power on
  120. if (defined($cmd) && $cmd eq 'ON') {
  121. # Wait for the amp to boot
  122. if (!$exists{'AMPLIFIER'}) {
  123. sleep($START_DELAY);
  124. }
  125. # Set the mode
  126. $amp->send('TV')
  127. or die('Unable to write command to amp socket: TV' . ": ${!}\n");
  128. $amp->send('SURROUND')
  129. or die('Unable to write command to amp socket: SURROUND' . ": ${!}\n");
  130. }
  131. # Rave through the main amp
  132. if ($state eq 'RAVE') {
  133. # Set the mode
  134. $amp->send('TV')
  135. or die('Unable to write command to amp socket: TV' . ": ${!}\n");
  136. $amp->send('STEREO')
  137. or die('Unable to write command to amp socket: STEREO' . ": ${!}\n");
  138. }
  139. # No output file
  140. # Update the push time
  141. $pushLast = time();
  142. # Clear the update flag
  143. $update = 0;
  144. }
  145. }