Skip to content

Commit

Permalink
Merge pull request #3 from TGCFabian/main
Browse files Browse the repository at this point in the history
Update WLED to reset
  • Loading branch information
TheMariday committed Feb 10, 2024
2 parents cf34b02 + 51f3312 commit 9ddaf0d
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 16 deletions.
5 changes: 2 additions & 3 deletions backends/test_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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")

Expand Down
28 changes: 19 additions & 9 deletions backends/wled/wled_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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:
Expand Down
6 changes: 3 additions & 3 deletions lib/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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=""):
Expand All @@ -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
Expand Down
4 changes: 3 additions & 1 deletion scripts/latency_check.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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:]

Expand Down

0 comments on commit 9ddaf0d

Please sign in to comment.