amplifierPower.pl 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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());
  46. if (defined($cmdState)) {
  47. $newState = $cmdState;
  48. $pullLast = time();
  49. }
  50. # Calculate the new state
  51. $stateLast = $state;
  52. if ($newState eq 'ON') {
  53. $state = 'ON';
  54. } elsif ($exists{'RAVE'}) {
  55. $state = 'RAVE';
  56. } else {
  57. $state = 'OFF';
  58. }
  59. # Force updates when there is a physical state mistmatch
  60. if ($state eq 'OFF') {
  61. if ($exists{'AMPLIFIER'}) {
  62. $update = 1;
  63. }
  64. } else {
  65. if (!$exists{'AMPLIFIER'}) {
  66. $update = 1;
  67. }
  68. }
  69. # Force updates on a periodic basis
  70. if (time() - $pushLast > $PUSH_TIMEOUT) {
  71. # Not for the amp
  72. #$update = 1;
  73. }
  74. # Die if we don't see regular updates
  75. if (time() - $pullLast > $PULL_TIMEOUT) {
  76. die('No update on state socket in past ' . $PULL_TIMEOUT . " seconds. Exiting...\n");
  77. }
  78. # Force updates on any state change
  79. if ($stateLast ne $state) {
  80. if ($DEBUG) {
  81. print STDERR 'State change: ' . $stateLast . ' => ' . $state . "\n";
  82. }
  83. $update = 1;
  84. }
  85. # Update the amp
  86. if ($update) {
  87. # Extra debugging to record pushes
  88. if ($DEBUG) {
  89. print STDERR 'State: ' . $state . "\n";
  90. }
  91. # Send master power state
  92. my $cmd = undef();
  93. if ($state eq 'OFF' || $state eq 'ON') {
  94. $cmd = $state;
  95. } elsif ($state eq 'RAVE') {
  96. $cmd = 'ON';
  97. }
  98. if (defined($cmd)) {
  99. $amp->send($cmd)
  100. or die('Unable to write command to amp socket: ' . $cmd . ": ${!}\n");
  101. }
  102. # Reset to TV @ 5.1 at power on
  103. if (defined($cmd) && $cmd eq 'ON') {
  104. # Set the audio source back to the default
  105. my @CMD = @AUDIO_SET;
  106. push(@CMD, $DEFAULT_DEV);
  107. system(@CMD);
  108. # Wait for the amp to boot
  109. sleep($START_DELAY);
  110. # Set the mode
  111. $amp->send('TV')
  112. or die('Unable to write command to amp socket: TV' . ": ${!}\n");
  113. $amp->send('SURROUND')
  114. or die('Unable to write command to amp socket: SURROUND' . ": ${!}\n");
  115. }
  116. # Rave through the main amp
  117. if ($state eq 'RAVE') {
  118. # Set the audio source to the RAVE device
  119. my @CMD = @AUDIO_SET;
  120. push(@CMD, $RAVE_DEV);
  121. system(@CMD);
  122. # Set the mode
  123. $amp->send('STEREO')
  124. or die('Unable to write command to amp socket: STEREO' . ": ${!}\n");
  125. }
  126. # No output file
  127. # Update the push time
  128. $pushLast = time();
  129. # Clear the update flag
  130. $update = 0;
  131. }
  132. }