overhead.pl 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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. # User config
  10. my %DIM = (
  11. 'OFF' => [ { 'channel' => 4, 'value' => 0, 'time' => 60000 }, ],
  12. 'PLAY' => [ { 'channel' => 4, 'value' => 32, 'time' => 500 }, ],
  13. 'PLAY_HIGH' => [ { 'channel' => 4, 'value' => 48, 'time' => 500 }, ],
  14. 'PAUSE' => [ { 'channel' => 4, 'value' => 96, 'time' => 6000, 'delay' => 9000 }, ],
  15. 'MOTION' => [ { 'channel' => 4, 'value' => 128, 'time' => 2500 }, ],
  16. );
  17. # App config
  18. my $DATA_DIR = DMX::dataDir();
  19. my $OUTPUT_FILE = $DATA_DIR . 'OVERHEAD';
  20. my $STATE_SOCK = $OUTPUT_FILE . '.socket';
  21. my $PUSH_TIMEOUT = 20;
  22. my $PULL_TIMEOUT = $PUSH_TIMEOUT * 3;
  23. my $DELAY = $PULL_TIMEOUT / 2;
  24. # Debug
  25. my $DEBUG = 0;
  26. if ($ENV{'DEBUG'}) {
  27. $DEBUG = 1;
  28. }
  29. # Construct a list of valid states
  30. my %VALID = ();
  31. foreach my $key (keys(%DIM)) {
  32. $VALID{$key} = 1;
  33. }
  34. # Sockets
  35. DMX::stateSocket($STATE_SOCK);
  36. DMX::stateSubscribe($STATE_SOCK);
  37. # State
  38. my $state = 'OFF';
  39. my $stateLast = $state;
  40. my %exists = ();
  41. my %last = ();
  42. my $pushLast = 0;
  43. my $pullLast = time();
  44. my $update = 0;
  45. # Always force lights out at launch
  46. DMX::dim({ 'channel' => 4, 'value' => 0, 'time' => 0 });
  47. # Loop forever
  48. while (1) {
  49. # State is calculated; use newState to gather data
  50. my $newState = $state;
  51. %last = %exists;
  52. # Wait for state updates
  53. my $cmdState = DMX::readState($DELAY, \%exists, undef(), \%VALID);
  54. if (defined($cmdState)) {
  55. $newState = $cmdState;
  56. $pullLast = time();
  57. }
  58. # Die if we don't see regular updates
  59. if (time() - $pullLast > $PULL_TIMEOUT) {
  60. die('No update on state socket in past ' . $PULL_TIMEOUT . " seconds. Exiting...\n");
  61. }
  62. # Skip processing when in RAVE or EFFECT mode
  63. if ($exists{'RAVE'} || $exists{'EFFECT'}) {
  64. if ($DEBUG) {
  65. print STDERR "Suspending normal operation while in RAVE mode\n";
  66. }
  67. $update = 1;
  68. next;
  69. }
  70. # Calculate the new state
  71. $stateLast = $state;
  72. if ($exists{'LIGHTS'}) {
  73. if ($newState eq 'PLAY') {
  74. $newState = 'PLAY_HIGH';
  75. }
  76. } else {
  77. if ($newState eq 'PLAY_HIGH') {
  78. $newState = 'PLAY';
  79. }
  80. }
  81. $state = $newState;
  82. # Speak when LIGHTS changes
  83. if (exists($exists{'LIGHTS'}) && exists($last{'LIGHTS'}) && $exists{'LIGHTS'} ne $last{'LIGHTS'}) {
  84. if ($exists{'LIGHTS'}) {
  85. DMX::say('Lights up');
  86. } else {
  87. DMX::say('Lights down');
  88. }
  89. }
  90. # Force updates on a periodic basis
  91. if (!$update && time() - $pushLast > $PUSH_TIMEOUT) {
  92. if ($DEBUG) {
  93. print STDERR "Forcing periodic update\n";
  94. }
  95. $update = 1;
  96. }
  97. # Force updates on any state change
  98. if (!$update && $stateLast ne $state) {
  99. if ($DEBUG) {
  100. print STDERR 'State change: ' . $stateLast . ' => ' . $state . "\n";
  101. }
  102. $update = 1;
  103. }
  104. # Update the lighting
  105. if ($update) {
  106. # Update
  107. DMX::applyDataset($DIM{$state}, $state, $OUTPUT_FILE);
  108. # Update the push time
  109. $pushLast = time();
  110. # Clear the update flag
  111. $update = 0;
  112. }
  113. }