Skip to content

Commit

Permalink
Merge pull request #9 from SiLab-Bonn/fix_cleanup_pr
Browse files Browse the repository at this point in the history
Cleanup
  • Loading branch information
rpartzsch authored Dec 17, 2024
2 parents 63d03b9 + b484fad commit f3c2a49
Show file tree
Hide file tree
Showing 18 changed files with 114 additions and 183 deletions.
7 changes: 4 additions & 3 deletions aidatlu/TLUPyProducer.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
#! /usr/bin/env python3
# load binary lib/pyeudaq.so
import pyeudaq
from pyeudaq import EUDAQ_INFO, EUDAQ_ERROR
import time
from main.tlu import AidaTLU

import pyeudaq
import uhal
from main.tlu import AidaTLU
from pyeudaq import EUDAQ_ERROR, EUDAQ_INFO

"""
Example Producer from EUDAQ
Expand Down
2 changes: 1 addition & 1 deletion aidatlu/aidatlu_run.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from main.tlu import AidaTLU
import uhal
from main.tlu import AidaTLU


class AIDATLU:
Expand Down
4 changes: 2 additions & 2 deletions aidatlu/hardware/clock_controller.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
from aidatlu import logger
from aidatlu.hardware.i2c import I2CCore
from aidatlu.hardware.ioexpander_controller import IOControl
from aidatlu import logger


class ClockControl(object):
class ClockControl:
"""The control class for the Si5344 clock chip.
Main purpose is to read/write the clock configuration file to the chip.
"""
Expand Down
26 changes: 13 additions & 13 deletions aidatlu/hardware/dac_controller.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
from aidatlu.hardware.i2c import I2CCore
from aidatlu import logger
from aidatlu.hardware.i2c import I2CCore


class DacControl(object):
class DacControl:
"""Control class for the three AD5665R. One controls the PMT control power (pwr_dac).
Two set the trigger input thresholds (dac_1, dac_2).
Each AD5665R has four parallel outputs.
Expand Down Expand Up @@ -56,10 +56,10 @@ def set_threshold(
self._set_dac_value(channel + 1, dac_value, 1)
self._set_dac_value(channel + 1, dac_value, 2)
# The DAC channels are connected in reverse order. The first two channels sit on DAC 1 in reverse order.
if channel < 2:
elif channel < 2:
self._set_dac_value(1 - channel, dac_value, 1)
# The last 4 channels sit on DAC 2 in reverse order.
if channel > 1 and channel < 6:
elif channel > 1 and channel < 6:
self._set_dac_value(3 - (channel - 2), dac_value, 2)
self.log.info(
"Threshold of input %s set to %s V" % (trigger_channel, threshold_voltage)
Expand Down Expand Up @@ -123,16 +123,16 @@ def _set_dac_reference(self, internal: bool = False, dac: int = 0) -> None:
"""
# There is a factor 2 in the output voltage between internal and external DAC reference. In general internal reference is a factor of 2 larger!
if internal:
chr = [0x00, 0x01]
char = [0x00, 0x01]
else:
chr = [0x00, 0x00]
char = [0x00, 0x00]

if dac == 0:
self.i2c.write_array(self.i2c.modules["pwr_dac"], 0x38, chr)
self.i2c.write_array(self.i2c.modules["pwr_dac"], 0x38, char)
if dac == 1:
self.i2c.write_array(self.i2c.modules["dac_1"], 0x38, chr)
self.i2c.write_array(self.i2c.modules["dac_1"], 0x38, char)
if dac == 2:
self.i2c.write_array(self.i2c.modules["dac_2"], 0x38, chr)
self.i2c.write_array(self.i2c.modules["dac_2"], 0x38, char)
self.log.info(
"Set %s DAC reference of DAC %s"
% (("internal" if internal else "external"), dac)
Expand Down Expand Up @@ -161,12 +161,12 @@ def _set_dac_value(self, channel: int, value: int, dac: int = 0) -> None:
)
value = 0xFFFF

chr = [(value >> 8) & 0xFF, value & 0xFF]
char = [(value >> 8) & 0xFF, value & 0xFF]
mem_addr = 0x18 + (channel & 0x7)

if dac == 0:
self.i2c.write_array(self.i2c.modules["pwr_dac"], mem_addr, chr)
self.i2c.write_array(self.i2c.modules["pwr_dac"], mem_addr, char)
if dac == 1:
self.i2c.write_array(self.i2c.modules["dac_1"], mem_addr, chr)
self.i2c.write_array(self.i2c.modules["dac_1"], mem_addr, char)
if dac == 2:
self.i2c.write_array(self.i2c.modules["dac_2"], mem_addr, chr)
self.i2c.write_array(self.i2c.modules["dac_2"], mem_addr, char)
8 changes: 4 additions & 4 deletions aidatlu/hardware/dut_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from aidatlu.hardware.i2c import I2CCore


class DUTLogic(object):
class DUTLogic:
def __init__(self, i2c: I2CCore):
self.log = logger.setup_derived_logger(__class__.__name__)
self.i2c = i2c
Expand All @@ -15,7 +15,7 @@ def set_dut_mask(self, enable: int | str) -> None:
Args:
value (int | str): 4-bit WORD to enable the the HDMI outputs. Can be an integer or binary string.
"""
if type(enable) == str:
if isinstance(enable, str):
enable = int(enable, 2)

if enable > 0b1111 or enable < 0b0000:
Expand All @@ -35,7 +35,7 @@ def set_dut_mask_mode(self, mode: int | str) -> None:
mode (int | str): 8-bit WORD to set the mode for each DUT. Can be an integer or binary string.
"""

if type(mode) == str:
if isinstance(mode, str):
mode = int(mode, 2)

if mode > 0b11111111 or mode < 0b00000000:
Expand Down Expand Up @@ -64,7 +64,7 @@ def set_dut_ignore_busy(self, channels: int | str) -> None:
Args:
channels (int | str): _description_#TODO
"""
if type(channels) == str:
if isinstance(channels, str):
channels = int(channels, 2)

if channels > 0b1111 or channels < 0b0000:
Expand Down
16 changes: 8 additions & 8 deletions aidatlu/hardware/i2c.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import time
from math import ceil

from aidatlu import logger

i2c_addr = {
Expand All @@ -18,7 +19,7 @@
}


class I2CCore(object):
class I2CCore:
def __init__(self, hw_int):
"""hw_int: IPBus HwInterface instance"""
self.log = logger.setup_derived_logger(__class__.__name__)
Expand Down Expand Up @@ -65,13 +66,13 @@ def write_register(self, register: str, value: int) -> None:
register: str Name of node in address file
value: int Value to be written
"""
if type(value) != int:
if not isinstance(value, int):
raise TypeError("Value must be integer")
try:
self.i2c_hw.getNode(register).write(value)
self.i2c_hw.dispatch()
except Exception:
raise
except Exception as e:
raise e

def read_register(self, register: str) -> int:
"""
Expand All @@ -82,10 +83,9 @@ def read_register(self, register: str) -> int:
self.i2c_hw.dispatch()
if ret.valid():
return ret.value()
else:
raise RuntimeError("Error reading register %s" % register)
except Exception:
raise
raise RuntimeError("Error reading register %s" % register)
except Exception as e:
raise e

def get_i2c_status(self):
return self.read_register("i2c_master.i2c_cmdstatus")
Expand Down
16 changes: 8 additions & 8 deletions aidatlu/hardware/ioexpander_controller.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import time

from aidatlu import logger
from aidatlu.hardware.i2c import I2CCore
from aidatlu.hardware.utils import _set_bit
import time


class IOControl(object):
class IOControl:
"""Main class for the control of the IO expander PCA9539PW.
Four I/O expanders are in use, two for the 11 front panel LEDs. and two
for the HDMI DUT interfaces.
Expand Down Expand Up @@ -55,7 +56,7 @@ def init_output_expander(self) -> None:
self._set_ioexpander_direction(2, exp_id=2, cmd_byte=7, direction="output")
self._set_ioexpander_output(2, exp_id=2, cmd_byte=3, value=0xB0)

""" LED Control """
### LED Control ###

def test_leds(self, single=True) -> None:
"""Test the 11 LEDs
Expand Down Expand Up @@ -149,8 +150,7 @@ def switch_led(self, led_id: int, color: str = "off") -> None:

if led_id == 5 and color not in ["r", "g", "off"]:
raise ValueError("%s color not supported for Clock LED" % color)

elif color not in ["w", "r", "g", "b", "off"]:
if color not in ["w", "r", "g", "b", "off"]:
raise ValueError("%s color not supported for LED" % color)

# Clock LED has only two LEDs
Expand Down Expand Up @@ -241,7 +241,7 @@ def _set_led(self, led_id: int, rgb: list) -> None:
if status_now[3] != status_next[3]:
self._set_ioexpander_output(1, 2, 3, status_next[3])

""" Output Control """
### Output Control ###

def configure_hdmi(self, hdmi_channel: int, enable: int | str) -> None:
"""This enables the pins of one HDMI channel as input (0) or output (1).
Expand All @@ -258,7 +258,7 @@ def configure_hdmi(self, hdmi_channel: int, enable: int | str) -> None:
if hdmi_channel < 1 or hdmi_channel > 4:
raise ValueError("HDMI channel should be between 1 and 4")

if type(enable) == str:
if isinstance(enable, str):
enable = int(enable, 2)

if enable > 0b1111 or enable < 0b0000:
Expand Down Expand Up @@ -344,7 +344,7 @@ def clock_lemo_output(self, enable: bool = True) -> None:
self.switch_led(5, "off")
self.log.info("Clock LEMO output %s" % ("enabled" if enable else "disabled"))

""" General Expander Control """
### General Expander Control ###

def _set_ioexpander_polarity(
self, io_exp: int, exp_id: int, cmd_byte: int, polarity: bool = False
Expand Down
12 changes: 6 additions & 6 deletions aidatlu/hardware/trigger_controller.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
from aidatlu import logger
from aidatlu.hardware.i2c import I2CCore
from aidatlu.hardware.utils import _pack_bits
from aidatlu import logger


class TriggerLogic(object):
class TriggerLogic:
def __init__(self, i2c: I2CCore) -> None:
self.log = logger.setup_derived_logger(__class__.__name__)
self.i2c = i2c

""" Internal Trigger Generation """
### Internal Trigger Generation ###

def set_internal_trigger_frequency(self, frequency: int) -> None:
"""Sets the internal trigger frequency.
Expand Down Expand Up @@ -63,14 +63,14 @@ def _set_internal_trigger_interval(self, interval: int) -> None:
"""
self.i2c.write_register("triggerLogic.InternalTriggerIntervalW", interval)

""" Trigger Logic """
### Trigger Logic ###

def set_trigger_veto(self, veto: bool) -> None:
"""Enables or disables new trigger. This can be used to reset the procession of new triggers.
Args:
veto (bool): Sets a veto to the trigger logic of the tlu.
"""
if type(veto) != bool:
if not isinstance(veto, bool):
raise TypeError("Veto must be type bool")

self.i2c.write_register("triggerLogic.TriggerVetoW", int(veto))
Expand Down Expand Up @@ -130,7 +130,7 @@ def set_trigger_mask_from_full_word(self, value: int) -> None:
self.i2c.write_register("triggerLogic.TriggerPattern_highW", mask_high)
self.log.debug("Trigger mask: %s" % self.get_trigger_mask())

""" Trigger Pulse Length and Delay """
### Trigger Pulse Length and Delay ###

def set_pulse_stretch_pack(self, vector: list) -> None:
"""Stretch word for trigger pulses. Each element of the input vector is stretched by N clock cycles.
Expand Down
13 changes: 6 additions & 7 deletions aidatlu/hardware/utils.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
def _set_bit(value: int, index: int, set: bool = True) -> int:
def _set_bit(value: int, index: int, set_bool: bool = True) -> int:
"""sets bit at given index of given value to bool set
Args:
Expand All @@ -10,10 +10,9 @@ def _set_bit(value: int, index: int, set: bool = True) -> int:
int: value with a set bit at index
"""

if set:
if set_bool:
return value | (1 << index)
else:
return value & ~(1 << index)
return value & ~(1 << index)


def _pack_bits(vector: list) -> int:
Expand All @@ -27,8 +26,8 @@ def _pack_bits(vector: list) -> int:
"""
packed_bits = 0
temp_int = 0
for channel in range(len(vector)):
temp_int = int(vector[channel]) << channel * 5
for i, channel in enumerate(vector):
temp_int = int(channel) << i * 5
packed_bits = packed_bits | temp_int
return packed_bits

Expand All @@ -39,7 +38,7 @@ def _pack_bits(vector: list) -> int:
def find_latest_file(path: str, index: str):
"""Find latest file that includes a given subset of strings called index in directory.
Args:
path (str): Path to directory. For same directory as python script use for e.q. './target_dir'.
path (str): Path to directory. For same directory as python script use e.g. './target_dir'.
index (str): (Optional) Find if specific characters are in Pathfile
Returns:
path: Path to file in target Director. Use str(find_path(.)) to obtain path as string.
Expand Down
2 changes: 1 addition & 1 deletion aidatlu/logger.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import logging

import coloredlogs
import argparse

FORMAT = "%(asctime)s [%(name)-18s] - %(levelname)-7s %(message)s"

Expand Down
11 changes: 5 additions & 6 deletions aidatlu/main/config_parser.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
import yaml
import logging

from aidatlu import logger


class TLUConfigure(object):
class TLUConfigure:
def __init__(self, TLU, io_control, config_path) -> None:
self.log = logger.setup_main_logger(__class__.__name__)

self.tlu = TLU
self.io_control = io_control

config_path = config_path
with open(config_path, "r") as file:
self.conf = yaml.full_load(file)

Expand Down Expand Up @@ -107,12 +106,12 @@ def get_stop_condition(self) -> tuple:
try:
max_number = int(self.conf["max_trigger_number"])
self.log.info("Stop condition maximum triggers: %s" % max_number)
except:
except KeyError:
max_number = None
try:
timeout = float(self.conf["timeout"])
self.log.info("Stop condition timeout: %s s" % timeout)
except:
except KeyError:
timeout = None
return max_number, timeout

Expand Down Expand Up @@ -232,7 +231,7 @@ def conf_trigger_inputs(self) -> None:

# Sets the Trigger Leds to green if the Input is enabled and to red if the input is set to VETO.
# TODO this breaks when there are multiple enabled and veto statements.
if trigger_configuration != None:
if trigger_configuration is not None:
for trigger_led in range(6):
if "~CH%i" % (trigger_led + 1) in trigger_configuration:
self.io_control.switch_led(trigger_led + 6, "r")
Expand Down
Loading

0 comments on commit f3c2a49

Please sign in to comment.