Skip to content

Commit

Permalink
extract specific config to separate file
Browse files Browse the repository at this point in the history
  • Loading branch information
abeln94 committed May 10, 2023
1 parent 16825c4 commit 8d082ba
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 19 deletions.
18 changes: 18 additions & 0 deletions CONFIG.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
FPGA_COMMAND_LIST = "pnputil /enum-devices /class USB /connected"
FPGA_COMMAND_ENABLE = "pnputil /enable-device %s"
FPGA_COMMAND_DISABLE = "pnputil /disable-device %s"

FPGA_DESCRIPTION = "USB Serial Converter A"

FPGA_STATUS_DISABLED = ["Disabled", "Deshabilitado"]
FPGA_STATUS_ENABLED = ["Started", "Iniciado"]

# --- #

VIVADO_PATH = "C:/Xilinx/Vivado/*/bin/vivado.bat"
VIVADO_PRE_LOAD = True

# --- #

UI_THEME = 'SystemDefaultForReal'
UI_REFRESH_TIMEOUT = 1000
10 changes: 5 additions & 5 deletions UI.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

import PySimpleGUI as sg

TIMEOUT = 1000
from CONFIG import UI_THEME, UI_REFRESH_TIMEOUT

INIT = "Initializing..."
ICON_SIZE = 10
Expand All @@ -20,9 +20,9 @@ def __init__(self, is_vivado_available):
self.total = 0
self.steps_values = []

sg.theme('SystemDefaultForReal')
sg.theme(UI_THEME)

boards_layout = sg.Frame(title=INIT, key="info", expand_y=True, layout=[
boards_layout = sg.Frame(title=INIT, key='info', expand_y=True, layout=[
[
# refresh
sg.Column(expand_x=True, element_justification='Left', layout=[[
Expand All @@ -44,7 +44,7 @@ def __init__(self, is_vivado_available):
[sg.Column([], key='boards', expand_x=True)],
])

program_layout = sg.Frame(title=INIT, key="stepsFrame", vertical_alignment='top', expand_y=True, layout=[
program_layout = sg.Frame(title=INIT, key='stepsFrame', vertical_alignment='top', expand_y=True, layout=[
[
sg.Listbox(expand_x=True, expand_y=True, key='steps', values=[], enable_events=True),

Expand Down Expand Up @@ -145,7 +145,7 @@ def tick(self):
Performs a windows tick.
Returns the fired event
"""
event, self.values = self.window.read(timeout=TIMEOUT if self.values.get('autoRefresh', True) else None)
event, self.values = self.window.read(timeout=UI_REFRESH_TIMEOUT if self.values.get('autoRefresh', True) else None)

# act
if event == sg.WINDOW_CLOSED:
Expand Down
17 changes: 8 additions & 9 deletions fpgas.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
import subprocess
from types import SimpleNamespace

from CONFIG import FPGA_STATUS_DISABLED, FPGA_STATUS_ENABLED, FPGA_DESCRIPTION, FPGA_COMMAND_LIST, FPGA_COMMAND_ENABLE, FPGA_COMMAND_DISABLE


class FPGAs:
def __init__(self):
Expand All @@ -15,10 +17,7 @@ def update(self):
"""

# get all
output = subprocess.check_output(
"pnputil /enum-devices /class USB /connected",
universal_newlines=True
)
output = subprocess.check_output(FPGA_COMMAND_LIST, universal_newlines=True)

# process
devices = []
Expand All @@ -31,15 +30,15 @@ def update(self):
))

# filter
fpgas = [device for device in devices if device.device_description == "USB Serial Converter A"]
fpgas = [device for device in devices if device.device_description == FPGA_DESCRIPTION]

# modify
prefix = len(os.path.commonprefix([fpga.id for fpga in fpgas]))
suffix = len(os.path.commonprefix([fpga.id[::-1] for fpga in fpgas]))
for fpga in fpgas:
fpga.enabled = (
True if fpga.status in ["Started", "Iniciado"]
else False if fpga.status in ["Disabled", "Disabled"]
True if fpga.status in FPGA_STATUS_ENABLED
else False if fpga.status in FPGA_STATUS_DISABLED
else None
)
fpga.name = fpga.id[prefix:-suffix] if len(fpgas) > 1 else fpga.id
Expand Down Expand Up @@ -98,7 +97,7 @@ def enable(self, i):
print("Enabling", i)
for _ in range(10):
try:
print(subprocess.check_call(f"pnputil /enable-device {self.fpgas[i].id}"))
print(subprocess.check_call(FPGA_COMMAND_ENABLE % self.fpgas[i].id))
break
except Exception:
pass
Expand All @@ -115,7 +114,7 @@ def disable(self, i):
print("Disabling", i)
for _ in range(10):
try:
print(subprocess.check_call(f"pnputil /disable-device {self.fpgas[i].id}"))
print(subprocess.check_call(FPGA_COMMAND_DISABLE % self.fpgas[i].id))
break
except Exception:
pass
Expand Down
3 changes: 2 additions & 1 deletion main.py
Original file line number Diff line number Diff line change
Expand Up @@ -196,14 +196,15 @@ def _update_steps(self, selection=-1):
ui.tick()

vivado.close()
print("Bye!")


@run_as_admin
def main_admin():
try:
main()
except Exception as e:
print("An exception ocurred:")
print("An exception occurred:")
print(e)
input("Press enter to exit")

Expand Down
9 changes: 5 additions & 4 deletions vivado.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from glob import glob
from time import sleep

PRE_LOAD = True
from CONFIG import VIVADO_PATH, VIVADO_PRE_LOAD


class Vivado:
Expand All @@ -11,10 +11,10 @@ def __init__(self):
self.ready = False

# TODO allow user to choose version
self.launcher = ([None] + sorted(glob("C:/Xilinx/Vivado/*/bin/vivado.bat")))[-1]
self.launcher = ([None] + sorted(glob(VIVADO_PATH)))[-1]

# preload
if PRE_LOAD:
if VIVADO_PRE_LOAD:
print("Preloading Vivado")
self.prepare(wait_ready=False)

Expand Down Expand Up @@ -88,8 +88,9 @@ def close(self):
if self._instance is None: return

try:
print("Closing vivado")
print("Closing Vivado")
self._instance.communicate("exit", timeout=2)
except subprocess.TimeoutExpired:
print("Killing Vivado")
self._instance.kill()
self._instance.communicate()

0 comments on commit 8d082ba

Please sign in to comment.