amplifierPower.pl 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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 = 'RAVE';
  67. } elsif ($newState eq 'PAUSE') {
  68. $power = 'ON';
  69. }
  70. if (!$update && (($power eq 'OFF' && $exists{'AMPLIFIER'}) || ($power eq 'ON' && !$exists{'AMPLIFIER'}))) {
  71. if ($power eq 'OFF' || $power eq 'ON') {
  72. $powerCmd = $power;
  73. } elsif ($power eq 'RAVE') {
  74. $powerCmd = 'ON';
  75. }
  76. }
  77. }
  78. # Set the channel mode as needed
  79. my $modeCmd = undef();
  80. {
  81. my $mode = 'SURROUND';
  82. if ($exists{'STEREO_CMD'} || $exists{'PLAYING_TYPE'} eq 'audio' || $power eq 'RAVE') {
  83. $mode = 'STEREO';
  84. }
  85. if ($exists{'AMPLIFIER'} && $exists{'AMPLIFIER_MODE'} ne $mode) {
  86. $modeCmd = $mode;
  87. }
  88. }
  89. # Set the input as needed
  90. my $inputCmd = undef();
  91. {
  92. my $input = 'TV';
  93. if ($exists{'GAME'}) {
  94. $input = 'GAME';
  95. }
  96. if ($exists{'AMPLIFIER'} && $exists{'AMPLIFIER_INPUT'} ne $input) {
  97. $inputCmd = $input;
  98. }
  99. }
  100. # Update the amp
  101. if ($powerCmd || $inputCmd || $modeCmd) {
  102. # Send master power state
  103. if (defined($powerCmd) && $lastPower < $now - $CMD_DELAY) {
  104. $lastPower = $now;
  105. if ($DEBUG) {
  106. print STDERR 'State: ' . $power . "\n";
  107. }
  108. sendCmd($powerCmd);
  109. }
  110. # Send the output state
  111. if (defined($mode) && $lastMode < $now - $CMD_DELAY) {
  112. $lastMode = $now;
  113. if ($DEBUG) {
  114. print STDERR 'Setting mode to: ' . $modeCmd . "\n";
  115. }
  116. sendCmd($modeCmd);
  117. # Reset the input mode anytime we switch to SURROUND
  118. if ($mode eq 'SURROUND') {
  119. sendCmd($AUTO_CMD);
  120. }
  121. }
  122. # Send the input state
  123. if ($input && $lastInput < $now - $CMD_DELAY) {
  124. $lastInput = $now;
  125. if ($DEBUG) {
  126. print STDERR 'Setting input to: ' . $inputCmd . "\n";
  127. }
  128. sendCmd($inputCmd);
  129. }
  130. # No output file
  131. # Update the push time
  132. $pushLast = $now;
  133. }
  134. }
  135. # Send the requested command and enforce an inter-command delay
  136. sub sendCmd($$) {
  137. my ($cmd) = @_;
  138. # Send the command
  139. $AMP->send($cmd)
  140. or die('Unable to write command to amp socket: ' . $cmd . ": ${!}\n");
  141. # A tiny delay to keep the serial port stable in repeated calls
  142. sleep(0.1);
  143. }