amplifierPower.pl 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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 = 'AMPLIFIER_POWER';
  16. my $AMP_SOCK = 'AMPLIFIER';
  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 $AUTO_CMD = 'INPUT_AUTO';
  23. # Debug
  24. my $DEBUG = 0;
  25. if ($ENV{'DEBUG'}) {
  26. $DEBUG = 1;
  27. }
  28. # Sockets
  29. DMX::stateSocket($STATE_SOCK);
  30. DMX::stateSubscribe($STATE_SOCK);
  31. my $AMP = DMX::clientSock($AMP_SOCK);
  32. # State
  33. my $power = 'OFF';
  34. my %exists = ();
  35. my $pushLast = 0;
  36. my $pullLast = time();
  37. my $lastMode = 0;
  38. my $modeCmd = undef();
  39. my $lastInput = 0;
  40. my $inputCmd = undef();
  41. my $lastPower = 0;
  42. my $powerCmd = undef();
  43. # Loop forever
  44. while (1) {
  45. # Wait for state updates
  46. my $cmdState = DMX::readState($DELAY, \%exists, undef(), undef());
  47. # Avoid repeated calls to time()
  48. my $now = time();
  49. # Record only valid states
  50. my $newState = undef();
  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. # Set the power state as needed
  60. my $powerCmd = undef();
  61. {
  62. my $power = 'OFF';
  63. if ($newState eq 'PLAY') {
  64. $power = 'ON';
  65. } elsif ($exists{'RAVE'}) {
  66. $power = 'ON';
  67. } elsif ($newState eq 'PAUSE') {
  68. $power = 'ON';
  69. }
  70. if (($power eq 'OFF' && $exists{'AMPLIFIER'}) || ($power eq 'ON' && !$exists{'AMPLIFIER'})) {
  71. $powerCmd = $power;
  72. }
  73. }
  74. # Set the channel mode as needed
  75. my $modeCmd = undef();
  76. {
  77. my $mode = 'SURROUND';
  78. if ($exists{'STEREO_CMD'} || $exists{'PLAYING_TYPE'} eq 'audio' || $exists{'RAVE'}) {
  79. $mode = 'STEREO';
  80. }
  81. if ($exists{'AMPLIFIER'} && $exists{'AMPLIFIER_MODE'} ne $mode) {
  82. $modeCmd = $mode;
  83. }
  84. }
  85. # Set the input as needed
  86. my $inputCmd = undef();
  87. {
  88. my $input = 'PLEX';
  89. if ($exists{'GAME'}) {
  90. $input = 'GAME';
  91. }
  92. if ($exists{'AMPLIFIER'} && $exists{'AMPLIFIER_INPUT'} ne $input) {
  93. $inputCmd = $input;
  94. }
  95. }
  96. # Update the amp
  97. if ($powerCmd || $inputCmd || $modeCmd) {
  98. # Send master power state
  99. if (defined($powerCmd) && $lastPower < $now - $CMD_DELAY) {
  100. $lastPower = $now;
  101. if ($DEBUG) {
  102. print STDERR 'State: ' . $power . "\n";
  103. }
  104. sendCmd($powerCmd);
  105. }
  106. # Send the output state
  107. if (defined($modeCmd) && $lastMode < $now - $CMD_DELAY) {
  108. $lastMode = $now;
  109. if ($DEBUG) {
  110. print STDERR 'Setting mode to: ' . $modeCmd . "\n";
  111. }
  112. sendCmd($modeCmd);
  113. # Reset the input mode anytime we switch to SURROUND
  114. if ($modeCmd eq 'SURROUND') {
  115. sendCmd($AUTO_CMD);
  116. }
  117. }
  118. # Send the input state
  119. if ($inputCmd && $lastInput < $now - $CMD_DELAY) {
  120. $lastInput = $now;
  121. if ($DEBUG) {
  122. print STDERR 'Setting input to: ' . $inputCmd . "\n";
  123. }
  124. sendCmd($inputCmd);
  125. }
  126. # No output file
  127. # Update the push time
  128. $pushLast = $now;
  129. }
  130. }
  131. # Send the requested command and enforce an inter-command delay
  132. sub sendCmd($) {
  133. my ($cmd) = @_;
  134. # Send the command
  135. $AMP->send($cmd)
  136. or die('Unable to write command to amp socket: ' . $cmd . ": ${!}\n");
  137. # A tiny delay to keep the serial port stable in repeated calls
  138. sleep(0.1);
  139. }