rumbleCtl.pl 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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. # App config
  12. my $DATA_DIR = DMX::dataDir();
  13. my $STATE_SOCK = 'RUMBLE_CTL';
  14. my $OUT_SOCK = 'RUMBLE';
  15. my $OUTPUT_FILE = $DATA_DIR . $STATE_SOCK;
  16. my $PUSH_TIMEOUT = 20;
  17. my $PULL_TIMEOUT = $PUSH_TIMEOUT * 3;
  18. my $DELAY = 1;
  19. my $RAND_MAX_INT = 30;
  20. # Debug
  21. my $DEBUG = 0;
  22. if ($ENV{'DEBUG'}) {
  23. $DEBUG = 1;
  24. }
  25. # Sockets
  26. DMX::stateSocket($STATE_SOCK);
  27. DMX::stateSubscribe($STATE_SOCK);
  28. my $out = DMX::clientSock($OUT_SOCK);
  29. # State
  30. my $state = 'OFF';
  31. my $stateLast = $state;
  32. my %exists = ();
  33. my $pushLast = 0;
  34. my $pullLast = time();
  35. my $update = 0;
  36. my $stateEnd = 0;
  37. my $bumpNext = 0;
  38. # Loop forever
  39. while (1) {
  40. # State is calculated; use newState to gather data
  41. my $newState = $state;
  42. # Wait for state updates
  43. my $cmdState = DMX::readState($DELAY, \%exists, undef(), undef());
  44. # Avoid repeated calls to time()
  45. my $now = time();
  46. # Record only valid states
  47. if (defined($cmdState)) {
  48. $newState = $cmdState;
  49. $pullLast = time();
  50. }
  51. # Die if we don't see regular updates
  52. if ($now - $pullLast > $PULL_TIMEOUT) {
  53. die('No update on state socket in past ' . $PULL_TIMEOUT . " seconds. Exiting...\n");
  54. }
  55. # Calculate the new state, but only after the timer has elapsed
  56. if ($stateEnd <= $now) {
  57. # Pick a new delay window
  58. my $delay = 0;
  59. if (exists($exists{'RUMBLE_DELAY_MIN'})) {
  60. $delay = int($exists{'RUMBLE_DELAY_MIN'});
  61. }
  62. if (exists($exists{'RUMBLE_DELAY_MAX'}) && $exists{'RUMBLE_DELAY_MAX'} > $delay) {
  63. $delay = int(rand($exists{'RUMBLE_DELAY_MAX'} - $delay)) + $delay;
  64. }
  65. $stateEnd = $now + $delay;
  66. $bumpNext = 0;
  67. # Set the state
  68. $state = 'OFF';
  69. if ($exists{'RUMBLE_CMD'} && $exists{'RUMBLE_VALUE'}) {
  70. if ($exists{'RUMBLE_CMD'} =~ /^(?:LEVEL|RAW)$/) {
  71. $state = sprintf('%s_%d', $exists{'RUMBLE_CMD'}, int($exists{'RUMBLE_VALUE'}));
  72. } elsif ($exists{'RUMBLE_CMD'} eq 'RANDOM') {
  73. if ($exists{'RUMBLE_VALUE'} =~ /^(?:MIN|LOW|MED|FULL|HIGH)$/) {
  74. $state = sprintf('RANDOM_%s', $exists{'RUMBLE_VALUE'});
  75. }
  76. }
  77. }
  78. }
  79. # Force updates on a periodic basis
  80. if (!$update && $now - $pushLast > $PUSH_TIMEOUT) {
  81. if ($DEBUG) {
  82. print STDERR "Forcing periodic update\n";
  83. }
  84. $update = 1;
  85. }
  86. # Re-send the same command at randomized intervals based on user delay settings
  87. if ($bumpNext <= $now) {
  88. if ($DEBUG) {
  89. print STDERR "Bumping\n";
  90. }
  91. my $max = $RAND_MAX_INT;
  92. if ($exists{'RUMBLE_DELAY_MAX'} &&
  93. $exists{'RUMBLE_DELAY_MAX'} < $RAND_MAX_INT &&
  94. $exists{'RUMBLE_DELAY_MAX'} > 1) {
  95. $max = int($exists{'RUMBLE_DELAY_MAX'});
  96. }
  97. my $min = 1;
  98. if ($exists{'RUMBLE_DELAY_MIN'}) {
  99. $min = int($exists{'RUMBLE_DELAY_MIN'} / 4);
  100. }
  101. if ($min > $max) {
  102. $min = $max;
  103. }
  104. $bumpNext = int(rand($max - $min)) + $min - int(rand($min));
  105. $bumpNext += $now;
  106. $update = 1;
  107. }
  108. # Force updates on any state change
  109. if (!$update && $stateLast ne $state) {
  110. if ($DEBUG) {
  111. print STDERR 'State change: ' . $stateLast . ' => ' . $state . "\n";
  112. }
  113. $update = 1;
  114. }
  115. # Update the rumbler
  116. if ($update) {
  117. # Send master power state
  118. $out->send($state)
  119. or die('Unable to write command to rumble socket: ' . $state . ": ${!}\n");
  120. $stateLast = $state;
  121. # No output file
  122. if ($DEBUG) {
  123. print STDERR 'Sending rumble state: ' . $state . "\n";
  124. }
  125. # Update the push time
  126. $pushLast = $now;
  127. # Clear the update flag
  128. $update = 0;
  129. }
  130. }