浏览代码

Merge pull request #351 from wizmo2/ledvu-update-clean

Ledvu update (clean)
philippe44 1 年之前
父节点
当前提交
6589387cd3

+ 10 - 10
README.md

@@ -321,18 +321,18 @@ See [set_GPIO](#set-gpio) for how to set the green and red LEDs (including addre
 NB: For named configuration, GPIO affected to green and red LED cannot be changed but brightness option applies
 
 ### LED Strip
-One LED strip with up to 255 addressable LEDs can be configured to offer enhanced visualizations.  The LED strip can also be controlled remotely though the LMS server (using the CLI interface).  Currently only WS2812B LEDs are supported.  Set the LED Strip configuration (or NVS led_vu_config) to `WS2812,length=<n>,gpio=<gpio>, where <n> is the number of leds in the strip (1..255), and <gpio> is the data pin.`  
-
-The latest LMS plugin update is required to set the visualizer mode and brightness, in the ESP32 settings page for the player.  The plugin also adds the following CLI command options
+One LED strip with up to 255 addressable LEDs can be configured to offer enhanced visualizations.  The VU Meter visualizer includes a battery status indicator (see Battery).  Currently only WS2812B LEDs are supported.  Set the LED Strip hardware configuration, or the NVS led_vu_config syntax is 
 ```
-<playerid> led_visual [<mode>] [brightness(1-255)]
-  Toggles or selects the visulaizer mode.
-  The visualizer brighness can be controled using the optional <brighness> tag.
-
-<playerid> dmx <R,G,B|R,G,B,R,G,B ... R,G,B> [<offset>]
-  Sets the LED at position "offset" to any RGB color where "R"(red),"G"(green), and "B"(blue) are values from 0(off) to 255(max brightness).
-  Add additional RGB values to the delimited string to set multiple LEDs. 
+type=[WS2812],length=<n>,gpio=<dataPin>[,scale=<gain>]
 ```
+where `<n>` is the number of LEDs in the strip (1..255).  A `<scale>` gain value (percentage) can be added to enhance effect responses. 
+
+The latest LMS plugin update is required to set the visualizer mode and brightness in the ESP32 Settings page for the player, or a controllable display (see Extra/SqueezeESP32 menus).  The plugin adds additional LMS CLI commands.
+
+| Command                                            | Notes       |
+| -------------------------------------------------- | ----------- |
+| \<playerid\> led_visual \[\<mode\>\] \[\<brightness\>\] | Toggles or selects the visualizer "mode".<br />The visualizer brightness(0..255) can be controlled using the "brightness" tag. |
+| \<playerid\> dmx \<R,G,B,R,G,B, ... R,G,B\> \[\<offset\>\] | Sets the LED color starting at position "offset"<br />  with "R"(red),"G"(green),and "B"(blue) color sequences.<br />Add additional RGB values to the delimited string to set multiple LEDs.<br /> |
 
 ### Rotary Encoder
 One rotary encoder is supported, quadrature shift with press. Such encoders usually have 2 pins for encoders (A and B), and common C that must be set to ground and an optional SW pin for press. A, B and SW must be pulled up, so automatic pull-up is provided by ESP32, but you can add your own resistors. A bit of filtering on A and B (~470nF) helps for debouncing which is not made by software. 

+ 0 - 3
components/led_strip/led_strip.c

@@ -293,11 +293,8 @@ bool led_strip_init(struct led_strip_t *led_strip)
     static EXT_RAM_ATTR StackType_t xStack[LED_STRIP_TASK_SIZE] __attribute__ ((aligned (4)));
 
     if ((led_strip == NULL) ||
-        (led_strip->rmt_channel >= RMT_CHANNEL_MAX) ||
-        (led_strip->gpio > GPIO_NUM_33) || 
         (led_strip->led_strip_working == NULL) ||
         (led_strip->led_strip_showing == NULL) ||
-        (led_strip->led_strip_length == 0) ||
         (led_strip->access_semaphore == NULL)) {
         return false;
     }

+ 26 - 14
components/led_strip/led_vu.c

@@ -24,6 +24,7 @@
 #include "monitor.h"
 #include "led_strip.h"
 #include "platform_config.h"
+#include "services.h"
 #include "led_vu.h"
 
 static const char *TAG = "led_vu";
@@ -55,6 +56,7 @@ static EXT_RAM_ATTR struct {
     int vu_start_l;
     int vu_start_r;
     int vu_status;
+    int vu_scale;
 } strip;
 
 static int led_addr(int pos ) {
@@ -70,31 +72,31 @@ static void battery_svc(float value, int cells) {
 	if (battery_handler_chain) battery_handler_chain(value, cells);
 }
 
+/****************************************************************************************
+ * Suspend.
+ * 
+ */
+static void led_vu_sleep(void) {
+    led_vu_clear(led_display); 
+}
+
 /****************************************************************************************
  * Initialize the led vu strip if configured.
  * 
  */
 void led_vu_init()
 {
-    char* p;
     char* config = config_alloc_get_str("led_vu_config", NULL, "N/A");
 
-    // Initialize led VU strip 
-    char* drivername = strcasestr(config, "WS2812");
-
-    if ((p = strcasestr(config, "length")) != NULL) {
-        strip.length = atoi(strchr(p, '=') + 1);
-    } // else 0
-    if ((p = strcasestr(config, "gpio")) != NULL) {
-        strip.gpio = atoi(strchr(p, '=') + 1);
-    } else {
-        strip.gpio = LED_VU_DEFAULT_GPIO;
-    }
+    PARSE_PARAM(config, "length",'=', strip.length);
+    PARSE_PARAM(config, "gpio",'=', strip.gpio);
     // check for valid configuration
-    if (!drivername || !strip.gpio) {
+    if (!strip.gpio) {
         ESP_LOGI(TAG, "led_vu configuration invalid");
         goto done;
     }
+    strip.vu_scale = 100;
+    PARSE_PARAM(config, "scale",'=',strip.vu_scale);
 
     battery_handler_chain = battery_handler_svc;
     battery_handler_svc = battery_svc;
@@ -114,7 +116,7 @@ void led_vu_init()
         strip.vu_start_r = strip.vu_length + 1;
         strip.vu_status = strip.vu_length;
     }
-    ESP_LOGI(TAG, "vu meter using length:%d left:%d right:%d status:%d", strip.vu_length, strip.vu_start_l, strip.vu_start_r, strip.vu_status);
+    ESP_LOGI(TAG, "vu meter using length:%d left:%d right:%d status:%d scale:%d", strip.vu_length, strip.vu_start_l, strip.vu_start_r, strip.vu_status, strip.vu_scale);
 
     // create driver configuration
     led_strip_config.rgb_led_type = RGB_LED_TYPE_WS2812;
@@ -138,6 +140,8 @@ void led_vu_init()
     // reserver max memory for remote management systems
     rmt_set_mem_block_num(led_strip_config.rmt_channel, 7);
 
+    services_sleep_setsuspend(led_vu_sleep);
+
     led_vu_clear(led_display);
 
     done:
@@ -157,6 +161,14 @@ uint16_t led_vu_string_length() {
     return (uint16_t)strip.length;
 }
 
+/****************************************************************************************
+ * Returns a user defined scale (percent)
+ */
+uint16_t led_vu_scale() {
+    if (!led_display) return 0;
+    return (uint16_t)strip.vu_scale;
+}
+
 /****************************************************************************************
  * Turns all LEDs off (Black)
  */

+ 1 - 0
components/led_strip/led_vu.h

@@ -21,6 +21,7 @@
 extern struct led_strip_t* led_display;
 
 uint16_t led_vu_string_length();
+uint16_t led_vu_scale();
 void led_vu_progress_bar(int pct, int bright);
 void led_vu_display(int vu_l, int vu_r, int bright, bool comet);
 void led_vu_spin_dial(int gain, int rate, int speed, bool comet);

+ 6 - 1
components/platform_console/cmd_config.c

@@ -115,6 +115,7 @@ static struct {
     struct arg_str *type;
     struct arg_int *length;
     struct arg_int *gpio;
+    struct arg_int * scale;
     struct arg_lit *clear;
     struct arg_end *end;
 } ledvu_args;
@@ -657,7 +658,7 @@ static int do_cspot_config(int argc, char **argv) {
 #endif
 
 static int do_ledvu_cmd(int argc, char **argv) {
-    ledvu_struct_t ledvu = {.type = "WS2812", .gpio = -1, .length = 0};
+    ledvu_struct_t ledvu = {.type = "WS2812", .gpio = -1, .length = 0, .scale = 100};
     esp_err_t err = ESP_OK;
     int nerrors = arg_parse(argc, argv, (void **)&ledvu_args);
     if (ledvu_args.clear->count) {
@@ -685,6 +686,8 @@ static int do_ledvu_cmd(int argc, char **argv) {
     } else {
         ledvu.length = ledvu_args.length->count > 0 ? ledvu_args.length->ival[0] : 0;
     }
+    ledvu.scale = ledvu_args.scale->count>0?ledvu_args.scale->ival[0]:ledvu.scale;
+
 
     if (!nerrors) {
         fprintf(f, "Storing ledvu parameters.\n");
@@ -914,6 +917,7 @@ cJSON *ledvu_cb() {
     } else {
         cJSON_AddStringToObject(values, "type", "WS2812");
     }
+    cJSON_AddNumberToObject(values,"scale",ledvu->scale);
     return values;
 }
 
@@ -1314,6 +1318,7 @@ void register_ledvu_config(void) {
     ledvu_args.type = arg_str1(NULL, "type", "<none>|WS2812", "Led type (supports one rgb strip to display built in effects and allow remote control through 'dmx' messaging)");
     ledvu_args.length = arg_int1(NULL, "length", "<1..255>", "Strip length (1-255 supported)");
     ledvu_args.gpio = arg_int1(NULL, "gpio", "gpio", "Data pin");
+    ledvu_args.scale = arg_int0(NULL,"scale","<n>","Gain scale (precent)");
     ledvu_args.clear = arg_lit0(NULL, "clear", "Clear configuration");
     ledvu_args.end = arg_end(4);
 

+ 6 - 7
components/services/accessors.c

@@ -329,7 +329,7 @@ esp_err_t config_ledvu_set(ledvu_struct_t * config){
 	esp_err_t err=ESP_OK;
 	char * config_buffer=malloc_init_external(buffer_size);
 	if(config_buffer)  {
-		snprintf(config_buffer,buffer_size,"%s,length=%i,gpio=%i",config->type, config->length, config->gpio);
+		snprintf(config_buffer,buffer_size,"%s,length=%i,gpio=%i,scale=%i",config->type, config->length, config->gpio, config->scale);
 		log_send_messaging(MESSAGING_INFO,"Updating ledvu configuration to %s",config_buffer);
 		err = config_set_value(NVS_TYPE_STR, "led_vu_config", config_buffer);
 		if(err!=ESP_OK){
@@ -761,14 +761,13 @@ const rotary_struct_t * config_rotary_get() {
  */
 const ledvu_struct_t * config_ledvu_get() {
 
-	static ledvu_struct_t ledvu={  .type = "WS2812", .gpio = -1, .length = 0};
+	static ledvu_struct_t ledvu={  .type = "WS2812", .gpio = -1, .length = 0, .scale= 100 };
 	char *config = config_alloc_get_default(NVS_TYPE_STR, "led_vu_config", NULL, 0);
 	if (config && *config) {
-		char *p;
-	
-		// ToDo:  Add code for future support of alternate led types
-		if ((p = strcasestr(config, "gpio")) != NULL) ledvu.gpio = atoi(strchr(p, '=') + 1);
-		if ((p = strcasestr(config, "length")) != NULL) ledvu.length = atoi(strchr(p, '=') + 1);
+		PARSE_PARAM_STR(config, "type", '=', ledvu.type, 15);
+		PARSE_PARAM(config, "gpio", '=', ledvu.gpio);
+		PARSE_PARAM(config, "length", '=', ledvu.length);
+		PARSE_PARAM(config, "scale", '=', ledvu.scale);
 		free(config);
 	}
 	return &ledvu;

+ 1 - 0
components/services/accessors.h

@@ -89,6 +89,7 @@ typedef struct {
 	char type[16];
 	int length;
 	int gpio;
+	int scale;
 } ledvu_struct_t;
 
 typedef struct {

+ 8 - 6
components/squeezelite/displayer.c

@@ -216,7 +216,7 @@ static EXT_RAM_ATTR struct {
 
 static EXT_RAM_ATTR struct {
 	int mode;
-	int n, style, max;
+	int n, style, max, gain;
 	u16_t config;
 	struct bar_s bars[MAX_BARS] ;
 } led_visu;
@@ -1089,10 +1089,10 @@ static void displayer_update(void) {
 	if (led_display && led_visu.mode) {
 		// run built in visualizer effects
 		if (led_visu.mode == VISU_VUMETER) {
-			vu_scale(led_visu.bars, led_visu.max, meters.levels);
+			vu_scale(led_visu.bars, led_visu.gain, meters.levels);
 			led_vu_display(led_visu.bars[0].current, led_visu.bars[1].current, led_visu.max, led_visu.style);
 		} else if (led_visu.mode == VISU_SPECTRUM) { 
-			spectrum_scale(led_visu.n, led_visu.bars, led_visu.max, meters.samples);
+			spectrum_scale(led_visu.n, led_visu.bars, led_visu.gain, meters.samples);
 			uint8_t* p = (uint8_t*) led_data;
 			for (int i = 0; i < led_visu.n; i++) {
 				*p = led_visu.bars[i].current;
@@ -1100,7 +1100,7 @@ static void displayer_update(void) {
 			}
 			led_vu_spectrum(led_data, led_visu.max, led_visu.n, led_visu.style);
 		} else if (led_visu.mode == VISU_WAVEFORM) {
-			spectrum_scale(led_visu.n, led_visu.bars, led_visu.max, meters.samples);
+			spectrum_scale(led_visu.n, led_visu.bars, led_visu.gain, meters.samples);
 			led_vu_spin_dial(
 				led_visu.bars[led_visu.n-2].current,
 				led_visu.bars[(led_visu.n/2)+1].current * 50 / led_visu.max,
@@ -1277,8 +1277,8 @@ static void ledv_handler( u8_t *data, int len) {
 	led_visu.mode = pkt->which;
 	led_visu.style = pkt->style;
 	led_visu.max = pkt->bright;
+	led_visu.gain = led_visu.max * led_vu_scale() / 100; 
 
-	led_vu_clear();
 	if (led_visu.mode) {
 		if (led_visu.mode == VISU_SPECTRUM) {
 			led_visu.n = (led_visu.config < MAX_BARS) ? led_visu.config : MAX_BARS;
@@ -1293,8 +1293,10 @@ static void ledv_handler( u8_t *data, int len) {
 		// reset bars maximum
 		for (int i = led_visu.n; --i >= 0;) led_visu.bars[i].max = 0;
 		
-		LOG_INFO("LED Visualizer mode %u with bars:%u max:%u style:%d", led_visu.mode, led_visu.n, led_visu.max, led_visu.style);
+		LOG_INFO("LED Visualizer mode %u with bars:%u max:%u style:%d gain:%u", led_visu.mode, led_visu.n, led_visu.max, led_visu.style, led_visu.gain);
 	} else {
+		led_vu_clear();
+	
 		LOG_INFO("Stopping led visualizer");
 	}	
 	

+ 3 - 0
plugin/SqueezeESP32/Plugin.pm

@@ -64,6 +64,9 @@ sub initPlugin {
 	Slim::Control::Request::subscribe( sub { onNotification(@_) }, [ ['newmetadata'] ] );
 	Slim::Control::Request::subscribe( sub { onNotification(@_) }, [ ['playlist'], ['open', 'newsong'] ]);
 	Slim::Control::Request::subscribe( \&onStopClear, [ ['playlist'], ['stop', 'clear'] ]);
+
+	# Add menu item to extras
+	Slim::Buttons::Home::addSubMenu('PLUGINS', 'PLUGIN_SQUEEZEESP32',  { 'useMode'  => 'squeezeesp32_mode', });
 }
 
 sub onStopClear {

+ 72 - 0
plugin/SqueezeESP32/RgbLed.pm

@@ -174,6 +174,20 @@ sub setLEDVisu {
 	}
 	
 	updateLED($client);
+
+	# display name
+	my $modes  = ledVisualizerModes; 
+	my $desc = $modes->[$visu]{'desc'};
+	my $name = '';
+	for (my $j = 0; $j < scalar @$desc; $j++) {
+		$name .= ' ' if ($j > 0);
+		$name .= $client->string(@{$desc}[$j]) || @{$desc}[$j];
+	}
+
+	$client->showBriefly( {
+		'line1' => $client->string('PLUGIN_SQUEEZEESP32_LED_VISUALIZER'),
+		'line2' => $name,
+	});
 }
 
 sub onNotification {
@@ -186,4 +200,62 @@ sub onNotification {
 	}
 }
 
+sub setMainMode {
+	my $client = shift;
+	my $method = shift;
+	if ($method eq 'pop') {
+		Slim::Buttons::Common::popMode($client);
+		$client->update();
+		return;
+	}
+	
+	Slim::Buttons::Common::pushModeLeft($client, 'INPUT.Choice', {
+		'listRef'         => [ 
+			{
+				name      => string('PLUGIN_SQUEEZEESP32_LED_VISUALIZER'),
+				onPlay   => sub { Slim::Control::Request::executeRequest($client, ['led_visual']); },
+			},
+			{
+				name      => string('PLUGIN_SQUEEZEESP32_LED_BRIGHTNESS'),
+				onPlay   => sub { Slim::Buttons::Common::pushModeLeft($client, 'squeezeesp32_ledvu_bright'); },
+			},
+		],
+		'header'         => string('PLUGIN_SQUEEZEESP32'),
+		'headerAddCount' => 1,
+		'overlayRef'      => sub { return (undef, shift->symbols('rightarrow')) },
+	});
+}
+
+sub setLedvuBrightMode {
+	my $client = shift;
+	my $method = shift;
+	if ($method eq 'pop') {
+		Slim::Buttons::Common::popMode($client);
+		$client->update();
+		return;
+	}
+
+	my $bright = $prefs->client($client)->get('led_brightness');
+
+	Slim::Control::Request::executeRequest($client, ['led_visual',1,$bright]);
+	Slim::Buttons::Common::pushMode($client, 'INPUT.Bar', {
+		'header'       => 'PLUGIN_SQUEEZEESP32_LED_BRIGHTNESS',
+		'stringHeader' => 1,
+		'headerValue'  => 'unscaled',
+		'min'          => 1,
+		'max'          => 255,
+		'increment'    => 1,
+		'onChange'     => sub {
+			my ($client, $value) = @_;
+			
+			$bright = $bright + $value;
+			if ($bright > 0 && $bright <= 255) {
+				$prefs->client($client)->set('led_brightness', $bright);
+				updateLED($client);
+							}
+		},
+		'valueRef' => $bright,
+	});
+}
+
 1;