diff --git a/backends/test_backend.py b/backends/test_backend.py index 05dde7d..45c5700 100644 --- a/backends/test_backend.py +++ b/backends/test_backend.py @@ -9,8 +9,7 @@ parser = argparse.ArgumentParser(description='Tests a particular backend by making a reference led blink') - parser.add_argument("--backend", type=str, help="The backend used for led communication", - choices=["custom", "fadecandy", "serial", "wled", "lcm"], default="custom") + utils.add_backend_args(parser) parser.add_argument("--reference_led", type=int, help="This is the index of the LED should be visible from the camera", default=0) @@ -21,7 +20,7 @@ led_count = args.reference_led + 1 - led_backend = utils.get_backend(args.backend, led_count) + led_backend = utils.get_backend(args.backend, led_count, args.server) cprint("Press ctrl-c to cancel") diff --git a/backends/wled/wled_backend.py b/backends/wled/wled_backend.py index 843f53f..245be95 100644 --- a/backends/wled/wled_backend.py +++ b/backends/wled/wled_backend.py @@ -3,16 +3,15 @@ class Backend: - 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 __init__(self, wled_base_url="4.3.2.1"): + self.state_endpoint = f"http://{wled_base_url}/json/state" + self.info_endpoint = f"http://{wled_base_url}/json/info" + self.reset_wled() def get_led_count(self): - # Construct the WLED info API endpoint - info_endpoint = f"http://{self.wled_base_url}/json/info" # Send the HTTP GET request to WLED info API - response = requests.get(info_endpoint) + response = requests.get(self.info_endpoint) # Get the LED Count straight from the WLED Device :D if response.status_code != 200: @@ -21,14 +20,25 @@ def get_led_count(self): info_data = response.json() return info_data['leds']['count'] + def reset_wled(self): + + # Set all the LED's to black on launch + payload = {"seg": [{"start": 0, "stop": self.get_led_count(), "sel": True}, {"stop": 0}]} + + # Send the HTTP POST request to WLED API + response = requests.post(self.state_endpoint, json=payload) + + # Check if the request was successful (HTTP status code 200) + if response.status_code != 200: + raise ConnectionError(f"Failed to retrieve LED count. Status code: {response.status_code}") + [self.set_led(i, False) for i in range(self.get_led_count())] + def set_led(self, led_index: int, on: bool): - # Construct the WLED API endpoint - endpoint = f"http://{self.wled_base_url}/json/state" payload = {"seg": {"i": [led_index, "FFFFFF" if on else "000000"]}} # Send the HTTP POST request to WLED API - response = requests.post(endpoint, json=payload) + response = requests.post(self.state_endpoint, json=payload) # Check if the request was successful (HTTP status code 200) if response.status_code != 200: diff --git a/lib/utils.py b/lib/utils.py index 6223945..c1a6d3f 100644 --- a/lib/utils.py +++ b/lib/utils.py @@ -21,7 +21,7 @@ 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) + parser.add_argument("--server", type=str, help="Some backends require a server") def get_backend(backend_name, led_count, server=""): @@ -41,9 +41,9 @@ def get_backend(backend_name, led_count, server=""): if backend_name == "wled": from backends.wled import wled_backend if server: - return wled_backend.Backend(led_count, server) + return wled_backend.Backend(server) else: - return wled_backend.Backend(led_count) + return wled_backend.Backend() if backend_name == "lcm": from backends.lcm import lcm_backend diff --git a/scripts/latency_check.py b/scripts/latency_check.py index a5c3537..8a44a99 100644 --- a/scripts/latency_check.py +++ b/scripts/latency_check.py @@ -25,7 +25,7 @@ led_count = args.reference_led + 1 - led_backend = utils.get_backend(args.backend, led_count) + led_backend = utils.get_backend(args.backend, led_count, args.server) led_backend.set_led(args.reference_led, False) @@ -54,6 +54,8 @@ pass latencies.append(time.time() - led_update_time) + led_backend.set_led(args.reference_led, False) + # remove the first few as they tend to be off latencies = latencies[2:]