Skip to content

Commit

Permalink
Added more comments around the custom backend file
Browse files Browse the repository at this point in the history
  • Loading branch information
TheMariday committed Feb 9, 2024
1 parent ad6bb6a commit bdf7be0
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 28 deletions.
17 changes: 14 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,24 @@ Your LEDs are as unique as you are,
so the fastest way to connect L3D to your system is to fill in the blanks in [backends/custom/custom_backend.py](backends/custom/custom_backend.py):

```python
# import some_led_library

class Backend:

def __init__(self, led_count):
self.led_count = led_count
def __init__(self, led_count: int):
# Remove the following line after you have implemented the set_led function!
raise NotImplementedError("Could not load backend as it has not been implemented, go implement it!")

def set_led(self, led_index: int, on: bool):
# Make your LEDs do the on-off thing here!
# Write your code for controlling your LEDs here
# It should turn on or off the led at the led_index depending on the "on" variable
# For example:
# if on:
# some_led_library.set_led(led_index, (255, 255, 255))
# else:
# some_led_library.set_led(led_index, (0, 0, 0))
pass

```

You can test your backend with `python backends/test_backend.py`
Expand Down
12 changes: 11 additions & 1 deletion backends/custom/custom_backend.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,17 @@
# import some_led_library

class Backend:

def __init__(self, led_count):
def __init__(self, led_count: int):
# Remove the following line after you have implemented the set_led function!
raise NotImplementedError("Could not load backend as it has not been implemented, go implement it!")

def set_led(self, led_index: int, on: bool):
# Write your code for controlling your LEDs here
# It should turn on or off the led at the led_index depending on the "on" variable
# For example:
# if on:
# some_led_library.set_led(led_index, (255, 255, 255))
# else:
# some_led_library.set_led(led_index, (0, 0, 0))
pass
39 changes: 23 additions & 16 deletions lib/utils.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
import sys
sys.path.append('./')
from lib.color_print import cprint, Col


def add_camera_args(parser):
parser.add_argument('--device', type=int,
help='Camera device index, set to 1 if using a laptop with a USB webcam', default=0)
Expand All @@ -13,24 +18,26 @@ def add_camera_args(parser):

def get_backend(backend_name, led_count):

if backend_name == "custom":
from backends.custom import custom_backend
return custom_backend.Backend(led_count)
try:
if backend_name == "custom":
from backends.custom import custom_backend
return custom_backend.Backend(led_count)

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

if backend_name == "serial":
from backends.serial import serial_backend
return serial_backend.Backend(led_count)
if backend_name == "wled":
from backends.wled import wled_backend
return wled_backend.Backend(led_count)

if backend_name == "wled":
from backends.wled import wled_backend
return wled_backend.Backend(led_count)
if backend_name == "lcm":
from backends.lcm import lcm_backend
return lcm_backend.Backend(led_count)

if backend_name == "lcm":
from backends.lcm import lcm_backend
return lcm_backend.Backend(led_count)
raise "Invalid backend name"

raise "Invalid backend name"
except NotImplementedError:
cprint(f"Failed to initialise backend {backend_name}, you need to implement it or use the "
f"--backend argument to select from the available backends", Col.FAIL)
quit()
2 changes: 1 addition & 1 deletion scripts/capture_sequence.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
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", "serial", "wled", "lcm"], default="custom")
choices=["custom", "fadecandy", "wled", "lcm"], default="custom")

parser.add_argument("--latency", type=int,
help="The expected latency in ms from an LED being updated to that being updated in the camera",
Expand Down
9 changes: 2 additions & 7 deletions scripts/latency_check.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
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", "serial", "wled", "lcm"], default="custom")
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)
Expand All @@ -25,13 +25,8 @@
cprint(f"Loading {args.backend} backend")

led_count = args.reference_led + 1
led_backend = None

try:
led_backend = utils.get_backend(args.backend, led_count)
except NotImplementedError:
cprint(f"Failed to initialise backend {args.backend}, you need to implement it!", Col.FAIL)
quit()
led_backend = utils.get_backend(args.backend, led_count)

led_backend.set_led(args.reference_led, False)

Expand Down

0 comments on commit bdf7be0

Please sign in to comment.