leds.pl 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. #!/usr/bin/perl
  2. use strict;
  3. use warnings;
  4. use IO::Select;
  5. use Math::Random;
  6. use Time::HiRes qw( usleep );
  7. # Local modules
  8. use Cwd qw(abs_path);
  9. use File::Basename qw(dirname);
  10. use lib dirname(abs_path($0));
  11. use DMX;
  12. # Prototypes
  13. sub red_alert();
  14. sub red_flash();
  15. # Effects states
  16. my %EFFECTS = (
  17. 'RED_ALERT' => \&red_alert,
  18. 'RED_FLASH' => \&red_flash,
  19. );
  20. # User config
  21. my $COLOR_TIMEOUT = 30;
  22. my $COLOR_TIME_MIN = int($COLOR_TIMEOUT / 2);
  23. my %COLOR_VAR = (
  24. 'PLAY' => 0.50,
  25. 'PLAY_HIGH' => 0.50,
  26. 'PAUSE' => 0.65,
  27. 'MOTION' => 0.15,
  28. );
  29. my %DIM = (
  30. 'OFF' => [
  31. # Handled by rope.pl
  32. ],
  33. 'PLAY' => [
  34. { 'channel' => 13, 'value' => 10, 'time' => 500 },
  35. { 'channel' => 14, 'value' => 10, 'time' => 500 },
  36. { 'channel' => 15, 'value' => 8, 'time' => 500 },
  37. ],
  38. 'PLAY_HIGH' => [
  39. { 'channel' => 13, 'value' => 64, 'time' => 1000, 'delay' => 3000 },
  40. { 'channel' => 14, 'value' => 64, 'time' => 1000, 'delay' => 1500 },
  41. { 'channel' => 15, 'value' => 64, 'time' => 1000, 'delay' => 0 },
  42. ],
  43. 'PAUSE' => [
  44. { 'channel' => 13, 'value' => 96, 'time' => 3000, 'delay' => 3000 },
  45. { 'channel' => 14, 'value' => 96, 'time' => 3000, 'delay' => 0 },
  46. { 'channel' => 15, 'value' => 96, 'time' => 3000, 'delay' => 7000 },
  47. ],
  48. 'MOTION' => [
  49. { 'channel' => 13, 'value' => 144, 'time' => 1000 },
  50. { 'channel' => 14, 'value' => 144, 'time' => 1000 },
  51. { 'channel' => 15, 'value' => 144, 'time' => 1000 },
  52. ],
  53. );
  54. # App config
  55. my $TEMP_DIR = `getconf DARWIN_USER_TEMP_DIR`;
  56. chomp($TEMP_DIR);
  57. my $DATA_DIR = $TEMP_DIR . 'plexMonitor/';
  58. my $OUTPUT_FILE = $DATA_DIR . 'LED';
  59. my $STATE_SOCK = $OUTPUT_FILE . '.socket';
  60. my $PUSH_TIMEOUT = 20;
  61. my $PULL_TIMEOUT = 60;
  62. # Reset the push timeout if the color timeout is longer
  63. if ($PUSH_TIMEOUT < $COLOR_TIMEOUT) {
  64. $PUSH_TIMEOUT = $COLOR_TIMEOUT;
  65. }
  66. # Debug
  67. my $DEBUG = 0;
  68. if ($ENV{'DEBUG'}) {
  69. $DEBUG = 1;
  70. }
  71. # Command-line arguments
  72. my ($DELAY) = @ARGV;
  73. if (!$DELAY) {
  74. $DELAY = $PULL_TIMEOUT / 2;
  75. if ($DELAY > $COLOR_TIMEOUT / 2) {
  76. $DELAY = $COLOR_TIMEOUT / 2;
  77. }
  78. }
  79. # Sanity check
  80. if (!-d $DATA_DIR) {
  81. die("Bad config\n");
  82. }
  83. # Sockets
  84. my $select = DMX::stateSocket($STATE_SOCK);
  85. DMX::stateSubscribe($STATE_SOCK);
  86. my $dmx_fh = DMX::dmxSock();
  87. # State
  88. my $state = 'OFF';
  89. my $stateLast = $state;
  90. my %exists = ();
  91. my %existsLast = %exists;
  92. my $pushLast = 0;
  93. my $pullLast = time();
  94. my @COLOR = ();
  95. my $colorChange = time();
  96. # Always force lights out at launch
  97. DMX::dim({ 'channel' => 13, 'value' => 0, 'time' => 0 });
  98. DMX::dim({ 'channel' => 14, 'value' => 0, 'time' => 0 });
  99. DMX::dim({ 'channel' => 15, 'value' => 0, 'time' => 0 });
  100. # Loop forever
  101. while (1) {
  102. # Record the last state/exists data for diffs/resets
  103. $stateLast = $state;
  104. %existsLast = %exists;
  105. # Set anywhere to force an update this cycle
  106. my $forceUpdate = 0;
  107. # State is calculated; use newState to gather data
  108. my $newState = $state;
  109. # Wait for state updates
  110. my @ready_clients = $select->can_read($DELAY);
  111. foreach my $fh (@ready_clients) {
  112. # Read the global state
  113. my $cmdState = DMX::parseState($fh, \%exists);
  114. # Only accept valid states
  115. if (!defined($DIM{$cmdState}) && !defined($EFFECTS{$cmdState})) {
  116. print STDERR 'Invalid state: ' . $cmdState . "\n";
  117. next;
  118. }
  119. # Propogate the most recent command state
  120. $newState = $cmdState;
  121. $pullLast = time();
  122. }
  123. # Special handling for effects states
  124. if (defined($EFFECTS{$newState})) {
  125. # Dispatch the handler
  126. $EFFECTS{$newState}->();
  127. # Force an update back to the original state
  128. $newState = $stateLast;
  129. %exists = %existsLast;
  130. @COLOR = ();
  131. $colorChange = time() + $COLOR_TIME_MIN;
  132. $forceUpdate = 1;
  133. }
  134. # Calculate the new state
  135. if ($exists{'LIGHTS'}) {
  136. if ($newState eq 'PLAY') {
  137. $newState = 'PLAY_HIGH';
  138. }
  139. } else {
  140. if ($newState eq 'PLAY_HIGH') {
  141. $newState = 'PLAY';
  142. }
  143. }
  144. $state = $newState;
  145. # Color changes
  146. if ($COLOR_VAR{$state} && time() - $colorChange > $COLOR_TIMEOUT) {
  147. @COLOR = ();
  148. # Grab the default (white) data
  149. my $lums = 0;
  150. foreach my $data (@{ $DIM{$state} }) {
  151. $lums += $data->{'value'};
  152. }
  153. my $numChans = scalar(@{ $DIM{$state} });
  154. my $max = $lums / $numChans;
  155. # Pick the change interval
  156. my $time = int((rand($COLOR_TIMEOUT - $COLOR_TIME_MIN) + $COLOR_TIME_MIN) * 1000);
  157. # Assign each channel
  158. my @vals = random_normal($numChans, $max, $max * $COLOR_VAR{$state});
  159. foreach my $data (@{ $DIM{$state} }) {
  160. my $color = pop(@vals);
  161. $color = int($color);
  162. if ($color < 0) {
  163. $color = 0;
  164. } elsif ($color > 255) {
  165. $color = 255;
  166. }
  167. push(@COLOR, { 'channel' => $data->{'channel'}, 'value' => $color, 'time' => $time });
  168. }
  169. # Update
  170. $forceUpdate = 1;
  171. $colorChange = time();
  172. if ($DEBUG) {
  173. print STDERR "New color\n";
  174. }
  175. }
  176. # Force updates on a periodic basis
  177. if (time() - $pushLast > $PUSH_TIMEOUT) {
  178. $forceUpdate = 1;
  179. }
  180. # Die if we don't see regular updates
  181. if (time() - $pullLast > $PULL_TIMEOUT) {
  182. die('No update on state socket in past ' . $PULL_TIMEOUT . " seconds. Exiting...\n");
  183. }
  184. # Force updates on any state change
  185. if ($stateLast ne $state) {
  186. $forceUpdate = 1;
  187. # Reset the color change sequence, so we always spend 1 cycle at white
  188. @COLOR = ();
  189. $colorChange = time() + $COLOR_TIME_MIN;
  190. # Debug
  191. if ($DEBUG) {
  192. print STDERR 'State change: ' . $stateLast . ' => ' . $state . "\n";
  193. }
  194. }
  195. # Update the lighting
  196. if ($forceUpdate) {
  197. # Select a data set (color or standard)
  198. my @data_set = ();
  199. my $local_state = $state;
  200. if (scalar(@COLOR)) {
  201. @data_set = @COLOR;
  202. $local_state .= ' (Color)';
  203. } else {
  204. @data_set = @{ $DIM{$state} };
  205. }
  206. # Update
  207. DMX::applyDataset(\@data_set, $local_state, $OUTPUT_FILE);
  208. # Update the push time
  209. $pushLast = time();
  210. }
  211. }
  212. # ======================================
  213. # Effects routines
  214. # These are blocking, so be careful
  215. # ======================================
  216. sub red_alert() {
  217. my $ramp = 500;
  218. my @sound = ('afplay', '/mnt/media/Sounds/DMX/Red Alert.mp3');
  219. my $sleep = $ramp;
  220. my @other = ();
  221. push(@other, { 'channel' => 13, 'value' => 0, 'time' => 0 });
  222. push(@other, { 'channel' => 15, 'value' => 0, 'time' => 0 });
  223. foreach my $data (@other) {
  224. DMX::dim($data);
  225. }
  226. my %high = ('channel' => 14, 'value' => 255, 'time' => $ramp);
  227. my %low = %high;
  228. $low{'value'} = 64;
  229. DMX::dim(\%high);
  230. system(@sound);
  231. DMX::dim(\%low);
  232. usleep($sleep * 1000);
  233. DMX::dim(\%high);
  234. system(@sound);
  235. DMX::dim(\%low);
  236. usleep($sleep * 1000);
  237. DMX::dim(\%high);
  238. system(@sound);
  239. DMX::dim(\%low);
  240. usleep($sleep * 1000);
  241. }
  242. sub red_flash() {
  243. }