Skip to content

Commit

Permalink
Tidied up wled integration
Browse files Browse the repository at this point in the history
  • Loading branch information
TheMariday committed Feb 10, 2024
1 parent 3c12aa4 commit df6161e
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 51 deletions.
58 changes: 19 additions & 39 deletions backends/wled/wled_backend.py
Original file line number Diff line number Diff line change
@@ -1,55 +1,35 @@
import requests

wled_ip = "1.2.3.4" # Replace with the actual IP address or hostname of your WLED device


class Backend:

def __init__(self, wled_base_url):
def __init__(self, wled_base_url="1.2.3.4"):
self.wled_base_url = wled_base_url
self.led_count = self.get_led_count()

def get_led_count(self):
# Construct the WLED info API endpoint
info_endpoint = f"http://{wled_ip}/json/info"

try:
# Send the HTTP GET request to WLED info API
response = requests.get(info_endpoint)

# Get the LED Count straight from the WLED Device :D
if response.status_code == 200:
info_data = response.json()
return info_data['leds']['count']
else:
print(f"Failed to retrieve LED count. Status code: {response.status_code}")
return 0
except Exception as e:
print(f"Error: {e}")
return 0
info_endpoint = f"http://{self.wled_base_url}/json/info"

def set_led(self, led_index: int, on: bool):
# Construct the WLED API endpoint
endpoint = f"http://{wled_ip}/json/state"
# Send the HTTP GET request to WLED info API
response = requests.get(info_endpoint)

# Not the cleanest, but its honest work :3c
if on is True:
payload = {"seg": {"i": [led_index, "FFFFFF"]}}
else:
payload = {"seg": {"i": [led_index, "000000"]}}
# Get the LED Count straight from the WLED Device :D
if response.status_code != 200:
raise ConnectionError(f"Failed to retrieve LED count. Status code: {response.status_code}")

try:
# Send the HTTP POST request to WLED API
response = requests.post(endpoint, json=payload)
info_data = response.json()
return info_data['leds']['count']

def set_led(self, led_index: int, on: bool):
# Construct the WLED API endpoint
endpoint = f"http://{self.wled_base_url}/json/state"

# Check if the request was successful (HTTP status code 200)
if response.status_code == 200:
print(f"LED at index {led_index} {'turned on' if on else 'turned off'} successfully.")
else:
print(f"Failed to set LED state. Status code: {response.status_code}")
except Exception as e:
print(f"Error: {e}")
payload = {"seg": {"i": [led_index, "FFFFFF" if on else "000000"]}}

# Send the HTTP POST request to WLED API
response = requests.post(endpoint, json=payload)

backend = Backend(wled_base_url=wled_ip)
print(f"LED count: {backend.led_count}")
# Check if the request was successful (HTTP status code 200)
if response.status_code != 200:
raise ConnectionError(f"WLED Backend failed to set LED state. Status code: {response.status_code}")
19 changes: 16 additions & 3 deletions lib/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,14 @@ def add_camera_args(parser):
help='LED detection threshold, reducing this number will reduce false positives', default=128)


def get_backend(backend_name, led_count):
def add_backend_args(parser):

parser.add_argument("--backend", type=str, help="The backend used for led communication",
choices=["custom", "fadecandy", "wled", "lcm"], default="custom")

parser.add_argument("--server", type=str, help="Some backends require a server", required=True)

def get_backend(backend_name, led_count, server=""):

try:
if backend_name == "custom":
Expand All @@ -25,11 +32,17 @@ def get_backend(backend_name, led_count):

if backend_name == "fadecandy":
from backends.fadecandy import fadecandy_backend
return fadecandy_backend.Backend(led_count)
if server:
return fadecandy_backend.Backend(led_count, server)
else:
return fadecandy_backend.Backend(led_count)

if backend_name == "wled":
from backends.wled import wled_backend
return wled_backend.Backend(led_count)
if server:
return wled_backend.Backend(led_count, server)
else:
return wled_backend.Backend(led_count)

if backend_name == "lcm":
from backends.lcm import lcm_backend
Expand Down
Binary file modified requirements.txt
Binary file not shown.
10 changes: 4 additions & 6 deletions scripts/capture_sequence.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,20 @@
parser = argparse.ArgumentParser(description='Captures LED flashes to file')

utils.add_camera_args(parser)

parser.add_argument("--led_count", type=int,
help="How many LEDs are in your system", required=True)
utils.add_backend_args(parser)

parser.add_argument("--output_dir", type=str, help="The output folder for your capture", required=True)

parser.add_argument("--backend", type=str, help="The backend used for led communication",
choices=["custom", "fadecandy", "wled", "lcm"], default="custom")
parser.add_argument("--led_count", type=int,
help="How many LEDs are in your system", required=True)

parser.add_argument("--latency", type=int,
help="The expected latency in ms from an LED being updated to that being updated in the camera",
default=1000)

args = parser.parse_args()

led_backend = utils.get_backend(args.backend, args.led_count)
led_backend = utils.get_backend(args.backend, args.led_count, args.server)

l3d = L3D.L3D(args.device, args.exposure, args.threshold, width=args.width, height=args.height)

Expand Down
4 changes: 1 addition & 3 deletions scripts/latency_check.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,11 @@

parser = argparse.ArgumentParser(description='Tests the functionality and latency of your LED backend')

parser.add_argument("--backend", type=str, help="The backend used for led communication",
choices=["custom", "fadecandy", "wled", "lcm"], default="custom")

parser.add_argument("--reference_led", type=int,
help="This is the index of the LED should be visible from the camera", default=0)

utils.add_camera_args(parser)
utils.add_backend_args(parser)

args = parser.parse_args()

Expand Down

0 comments on commit df6161e

Please sign in to comment.