Source code for trgenpy.trgen
[docs]
class TrgenPort:
"""
Class representing a trigger generator port (TrgenPort).
A TrgenPort is identified by its ID and has a specific memory length.
Args:
id (int): The ID of the trigger (0-25).
memory_length (int): The length of the trigger memory (2^mtml).
Attributes:
id (int): The ID of the trigger (0-25).
memory_length (int): The length of the trigger memory (1-64).
type (str): The type of the trigger (NeuroScan, Synamps, BNCO, BNCI, GPIO).
memory (list): The list of instructions in the trigger memory.
Raises:
ValueError: If id or memory_length is out of allowed range.
TypeError: If id or memory_length is not an integer.
Example:
.. code-block:: python
from trgenpy import TrgenPort
# Create a TrgenPort with ID 0 and memory length of 32
trgen_port = TrgenPort(id=0, memory_length=32)
print(trgen_port)
"""
def __init__(self, id, memory_length=32):
"""
Initialize a Trgen instance.
Args:
id (int): The ID of the trigger (0-25).
memory_length (int): The length of the trigger memory (1-64).
Default is 5, which means 2^5 = 32 instructions.
Raises:
TypeError: If id or memory_length is not an integer.
ValueError: If id or memory_length is out of allowed range.
Raises:
ValueError: If either 'id' or 'memory_length' is not provided.
ValueError: If 'memory_length' is not between 1 and 64.
TypeError: If 'memory_length' is not an integer.
TypeError: If 'id' or 'memory_length' is not an integer.
Example:
.. code-block:: python
trgen_port = TrgenPort(id=0, memory_length=32)
print(trgen_port)
"""
if id is None or memory_length is None:
raise ValueError("Both 'id' and 'memory_length' must be provided.")
if memory_length < 1 or memory_length > 64:
raise ValueError("'memory_length' must be between 1 and 64.")
if memory_length != int(memory_length):
raise TypeError("'memory_length' must be an integer.")
if not isinstance(id, int) or not isinstance(memory_length, int):
raise TypeError("'id' and 'memory_length' must be integers.")
if not (0 <= id <= 26):
raise ValueError(f"ID {id} out of allowed range (0-26)")
if not (1 <= memory_length <= 64):
raise ValueError(f"Memory length {memory_length} out of allowed range (1-64)")
# Set ID
self._id = id
# Set type label
self.type = ""
if 0 <= id < 7:
self.type = "NeuroScan"
elif 8 <= id < 15:
self.type = "Synamps"
elif id == 16:
self.type = "BNCO"
elif id == 17:
self.type = "BNCI"
elif 18 <= id < 25:
self.type = "GPIO"
elif 26 <= id < 32:
self.type = "PD"
# set max trigger memory length (mtml)
self._memory_length = memory_length
self._memory = [0] * (memory_length)
@property
def id(self):
"""Get the ID of the trgen."""
return self._id
@property
def memory(self):
return self._memory
def __repr__(self):
return f"<Trgen id={self.id} instructions={len(self._memory)}>"
[docs]
def setInstruction(self, index, instruction):
"""
Set an instruction at a specific index in the trigger memory.
Args:
index (int): The index in the trigger memory to set the instruction.
instruction (int): The instruction to set.
Raises:
IndexError: If the index is out of bounds.
Example:
.. code-block:: python
trgen_port = TrgenPort(id=0, memory_length=32)
trgen_port.setInstruction(0, 123)
"""
if not (0 <= index < len(self._memory)):
raise IndexError(f"Indice {index} fuori limiti (max {self._memory_length})")
self._memory[index] = instruction