rumbleCtl.pl 2.9 KB

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