RgbLed.pm 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. package Plugins::SqueezeESP32::RgbLed;
  2. =head1 NAME
  3. Plugins::SqueezeESP32::RgbLed
  4. =head1 DESCRIPTION
  5. L<Plugins::SqueezeESP32::RgbLed>
  6. =cut
  7. use strict;
  8. use Slim::Utils::Strings qw(string cstring);
  9. use Slim::Utils::Log;
  10. use Slim::Utils::Prefs;
  11. use Plugins::SqueezeESP32::Player
  12. my $log = logger('player.RgbLed');
  13. my $prefs = preferences('plugin.squeezeesp32');
  14. my $log = logger('plugin.squeezeesp32');
  15. sub init {
  16. Slim::Control::Request::subscribe( sub { onNotification(@_) }, [ ['playlist'], ['open', 'pause', 'resume', 'stop', 'clear'] ]);
  17. # register led visualizer comands to allow independant update and command line controls.
  18. Slim::Control::Request::addDispatch([ 'dmx', '_data', '_xoff'], [1, 0, 0, \&sendDMX]);
  19. Slim::Control::Request::addDispatch([ 'led_visual', '_mode', '_bright'], [1, 0, 0, \&setLEDVisu]);
  20. }
  21. my $VISUALIZER_NONE = 0;
  22. my $VISUALIZER_VUMETER = 1;
  23. my $VISUALIZER_SPECTRUM_ANALYZER = 2;
  24. my $VISUALIZER_WAVEFORM = 3;
  25. my @ledvisualizers = (
  26. { desc => ['BLANK'],
  27. params => [$VISUALIZER_NONE],
  28. },
  29. { desc => ['VISUALIZER_ANALOG_VUMETER'],
  30. params => [$VISUALIZER_VUMETER, 0],
  31. },
  32. { desc => ['VISUALIZER_DIGITAL_VUMETER'],
  33. params => [$VISUALIZER_VUMETER, 1],
  34. },
  35. { desc => ['VISUALIZER_SPECTRUM_ANALYZER'],
  36. params => [$VISUALIZER_SPECTRUM_ANALYZER, 0],
  37. },
  38. { desc => ['VISUALIZER_SPECTRUM_ANALYZER','2'],
  39. params => [$VISUALIZER_SPECTRUM_ANALYZER, 1],
  40. },
  41. { desc => ['PLUGIN_SQUEEZEESP32_WAVEFORM'],
  42. params => [$VISUALIZER_WAVEFORM, 0],
  43. },
  44. { desc => ['PLUGIN_SQUEEZEESP32_WAVEFORM','2'],
  45. params => [$VISUALIZER_WAVEFORM, 1],
  46. },
  47. );
  48. my $nledvisualizers = $#ledvisualizers;
  49. sub ledVisualizerModes {
  50. return \@ledvisualizers;
  51. }
  52. sub ledVisualizerNModes {
  53. return $nledvisualizers;
  54. }
  55. sub updateLED {
  56. my $client = shift;
  57. my $on = shift || 1;
  58. my $cprefs = $prefs->client($client);
  59. my $visu = $cprefs->get('led_visualizer') || 0;
  60. my $bright = $cprefs->get('led_brightness') || 20;
  61. $visu = 0 if ($visu < 0 || $visu > ledVisualizerNModes || !(Slim::Player::Source::playmode($client) eq 'play') || !$on);
  62. my $modes = ledVisualizerModes;
  63. my $params = $modes->[$visu]{'params'};
  64. my $data = pack('CCC', $params->[0], $params->[1], $bright);
  65. main::INFOLOG && $log->is_debug && $log->info("Sending visu mode $visu ", $client->name);
  66. $client->sendFrame( ledv => \$data );
  67. }
  68. sub ledVisualParams {
  69. my $client = shift;
  70. my $visu = $prefs->client($client)->get('led_visualizer') || 0;
  71. return $ledvisualizers[$visu]{params};
  72. }
  73. sub ledVisualModeOptions {
  74. my $client = shift;
  75. my $display = {
  76. '-1' => ' '
  77. };
  78. my $modes = ledVisualizerModes;
  79. my $nmodes = ledVisualizerNModes;
  80. for (my $i = 0; $i <= $nmodes; $i++) {
  81. my $desc = $modes->[$i]{'desc'};
  82. for (my $j = 0; $j < scalar @$desc; $j++) {
  83. $display->{$i} .= ' ' if ($j > 0);
  84. $display->{$i} .= string(@{$desc}[$j]) || @{$desc}[$j];
  85. }
  86. }
  87. return $display;
  88. }
  89. sub sendDMX {
  90. my $request = shift;
  91. # check this is the correct command.
  92. if ($request->isNotCommand([['dmx']])) {
  93. $request->setStatusBadDispatch();
  94. return;
  95. }
  96. # get our parameters
  97. my $client = $request->client();
  98. my $count = 0;
  99. my $outData;
  100. my @values = split(',', $request->getParam('_data') || '');
  101. foreach my $val (@values) {
  102. $outData .= pack ( 'C', $val);
  103. $count++;
  104. }
  105. $count /= 3;
  106. my $data = pack('nn', $request->getParam('_xoff') || 0, $count ) . $outData;
  107. # changed from dmxt to ledd (matches 'ledc' for tricolor led in receiver player)
  108. $client->sendFrame( ledd => \$data );
  109. }
  110. sub setLEDVisu {
  111. my $request = shift;
  112. # check this is the correct command.
  113. if ($request->isNotCommand([['led_visual']])) {
  114. $request->setStatusBadDispatch();
  115. return;
  116. }
  117. my $client = $request->client();
  118. return if (!$client->hasLED);
  119. my $cprefs = $prefs->client($client);
  120. my $visu = $cprefs->get('led_visualizer') || 0;
  121. my $mode = $request->getParam('_mode') || -1;
  122. if ($mode == -1) {
  123. $visu+=1;
  124. } else {
  125. $visu = $mode;
  126. }
  127. $visu = 0 if ($visu < 0 || $visu > ledVisualizerNModes);
  128. $cprefs->set('led_visualizer', $visu);
  129. my $bright = $request->getParam('_bright') || -1;
  130. if ($bright >= 0 && $bright < 256) {
  131. $cprefs->set('led_brightness', $bright);
  132. }
  133. updateLED($client);
  134. }
  135. sub onNotification {
  136. my $request = shift;
  137. my $client = $request->client || return;
  138. foreach my $player ($client->syncGroupActiveMembers) {
  139. next unless $player->isa('Plugins::SqueezeESP32::Player');
  140. updateLED($player) if $player->hasLED;
  141. }
  142. }
  143. 1;