leds.pl 7.1 KB

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