Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WLED: Fix for next version and add segment parameter #552

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
5 changes: 4 additions & 1 deletion docs/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -1788,12 +1788,15 @@ gcode:
{% set brightness = params.BRIGHTNESS|default(-1)|int %}
{% set intensity = params.INTENSITY|default(-1)|int %}
{% set speed = params.SPEED|default(-1)|int %}
{% set id = params.ID|default(-1)|int %}
# Optionally specify a WLED segment with the ID parameter, which starts at 0.

{action_call_remote_method("set_wled_state",
strip=strip,
brightness=brightness,
intensity=intensity,
speed=speed)}
speed=speed,
id=id)}

[gcode_macro WLED_OFF]
description: Turn WLED strip off
Expand Down
38 changes: 28 additions & 10 deletions moonraker/components/wled.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ async def wled_on(self: Strip, preset: int) -> None:
self.brightness = -1
self.intensity = -1
self.speed = -1
await self._send_wled_command({"on": True, "ps": preset})
await self._send_wled_command({"ps": preset})

async def wled_off(self: Strip) -> None:
logging.debug(f"WLED: {self.name} off")
Expand All @@ -152,10 +152,11 @@ async def wled_off(self: Strip) -> None:
await self._send_wled_command({"on": False})

async def wled_control(self: Strip, brightness: int, intensity: int,
speed: int) -> None:
speed: int, segment: int) -> None:
logging.debug(
f"WLED: {self.name} control {self.onoff} BRIGHTNESS={brightness} "
f"INTENSITY={intensity} SPEED={speed} CURRENTPRESET={self.preset}")
f"INTENSITY={intensity} SPEED={speed} "
f"SEGMENT={segment}CURRENTPRESET={self.preset}")

if self.onoff == OnOff.off:
logging.info("wled control only permitted when strip is on")
Expand All @@ -176,29 +177,41 @@ async def wled_control(self: Strip, brightness: int, intensity: int,
logging.info("BRIGHTNESS should be between 1 and 255")
else:
shouldSend = True
self.segment = segment
self.brightness = brightness
control["bri"] = self.brightness
# Brightness in seg {} - only if a preset is on
if self.preset != -1:
control["seg"]["bri"] = self.brightness
if self.segment != -1:
# Set specific segment (segment) brightness
control["seg"]["segment"] = self.segment
control["seg"]["bri"] = self.brightness
else:
# No segment segment specified, so set master brightness
control["bri"] = self.brightness

# Intensity - only if a preset is on
if intensity > -1 and self.preset != -1:
if intensity > 255:
logging.info("INTENSITY should be between 0 and 255")
else:
shouldSend = True
self.segment = segment
self.intensity = intensity
control["seg"]["ix"] = self.intensity
if self.segment != -1:
control["seg"]["segment"] = self.segment

# Speed - only if a preset is on
if speed > -1 and self.preset != -1:
if speed > 255:
logging.info("SPEED should be between 0 and 255")
else:
shouldSend = True
self.segment = segment
self.speed = speed
control["seg"]["sx"] = self.speed
if self.segment != -1:
control["seg"]["segment"] = self.segment

# Control brightness, intensity, and speed for segment
# This will allow full control for effects such as "Percent"
Expand Down Expand Up @@ -450,6 +463,7 @@ async def set_wled_state(
strip: str,
state: Optional[str] = None,
preset: int = -1,
segment: int = -1,
brightness: int = -1,
intensity: int = -1,
speed: int = -1
Expand Down Expand Up @@ -483,7 +497,8 @@ async def set_wled_state(

# Control
if brightness != -1 or intensity != -1 or speed != -1:
await self.strips[strip].wled_control(brightness, intensity, speed)
await self.strips[strip].wled_control(
brightness, intensity, speed, segment)

# Individual pixel control, for compatibility with SET_LED
async def set_wled(self: WLED,
Expand Down Expand Up @@ -519,6 +534,7 @@ async def _handle_single_wled_request(self: WLED,
brightness: int = web_request.get_int('brightness', -1)
intensity: int = web_request.get_int('intensity', -1)
speed: int = web_request.get_int('speed', -1)
segment: int = web_request.get_int('segment', -1)

req_action = web_request.get_action()
if strip_name not in self.strips:
Expand All @@ -532,7 +548,8 @@ async def _handle_single_wled_request(self: WLED,
raise self.server.error(
f"Invalid requested action '{action}'")
result = await self._process_request(strip, action, preset,
brightness, intensity, speed)
brightness, intensity,
speed, segment)
return {strip_name: result}

async def _handle_batch_wled_request(self: WLED,
Expand All @@ -548,7 +565,7 @@ async def _handle_batch_wled_request(self: WLED,
for name, strip in requested_strips.items():
if strip is not None:
result[name] = await self._process_request(strip, req, -1,
-1, -1, -1)
-1, -1, -1, -1)
else:
result[name] = {"error": "strip_not_found"}
return result
Expand All @@ -559,7 +576,8 @@ async def _process_request(self: WLED,
preset: int,
brightness: int,
intensity: int,
speed: int
speed: int,
segment: int
) -> Dict[str, Any]:
strip_onoff = strip.onoff

Expand All @@ -577,7 +595,7 @@ async def _process_request(self: WLED,
await strip.wled_on(preset)

if brightness != -1 or intensity != -1 or speed != -1:
await strip.wled_control(brightness, intensity, speed)
await strip.wled_control(brightness, intensity, speed, segment)
else:
strip_onoff = OnOff.off
await strip.wled_off()
Expand Down