Skip to content

Commit

Permalink
Changes in Psy module for Psychopy testing experiment
Browse files Browse the repository at this point in the history
  • Loading branch information
pablomm committed Sep 17, 2024
1 parent 0eca5c4 commit 08db2b6
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 6 deletions.
33 changes: 33 additions & 0 deletions dmf/psy/display.py
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,26 @@ def close(self):
self.window.close()
self.window = None

def set_trigger(self, trigger):
self.trigger = trigger

def send_trigger(self, trigger_code: Union[int, str]):
"""
Send a trigger via the configured port.
The trigger can be provided as a string (which will be mapped to a trigger code)
or directly as an integer. If the trigger code is not found in the mapping, an error will be logged.
Parameters
----------
trigger_code : Union[int, str]
The trigger code to send, either as an integer or a string key mapped to an integer.
"""
if self.trigger is None:
self.log("No trigger device configured.")
return
self.trigger.send_trigger(trigger_code)

def screen_choice(
self,
options: Tuple[str, ...],
Expand All @@ -228,6 +248,10 @@ def screen_choice(
positions: Optional[List[List[float]]] = None,
clock: Optional[core.Clock] = None,
show_keys: bool = True,
text: Optional[str] = None,
text_position: Optional[List[float]] = [0, 6],
text_wrap_width: Optional[Union[int, float]] = 40,
text_height: Optional[Union[int, float]] = 1,
) -> Tuple[Optional[str], Optional[float], Optional[str]]:
"""
Display a choice screen with visual buttons for the participant to select an option.
Expand Down Expand Up @@ -271,6 +295,15 @@ def screen_choice(

if not (len(options) == len(positions) == len(keys)):
raise ValueError("The number of options, positions, and keys must be the same.")

if text:
text_sim = visual.TextStim(
self.window, pos=text_position, color="black"
)
text_sim.wrapWidth = text_wrap_width
text_sim.text = text
text_sim.height = text_height
text_sim.draw()

# Draw buttons and texts
for option, key, position in zip(options, keys, positions):
Expand Down
6 changes: 4 additions & 2 deletions dmf/psy/screens.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,8 @@ def screen_likert(
question_color: Optional[Tuple[int, int, int]] = None,
max_wait: Optional[Union[int, float]] = None,
min_wait: Optional[Union[int, float]] = None,
slider_size: Optional[Tuple[int, int]] = (30, 1.5),
slider_pos: Optional[Tuple[int, int]] = (0, 0),
clock=None,
) -> int:
"""Display a question with a slider."""
Expand All @@ -151,8 +153,8 @@ def screen_likert(
ticks=ticks,
labels=tick_labels,
granularity=1,
size=(30, 1.5),
pos=(0, 0),
size=slider_size,
pos=slider_pos,
color="black",
borderColor="black",
font="Arial",
Expand Down
16 changes: 12 additions & 4 deletions dmf/psy/trigger.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ class Trigger:

def __init__(
self,
port: Literal["parallel", "serial"],
port_address: Any,
port: Literal["parallel", "serial", "dummy"],
port_address: Any = None,
mapping: Optional[dict] = None,
delay: float = 0.01,
**kwargs,
Expand All @@ -45,6 +45,12 @@ def __init__(
The delay in seconds between sending the trigger and resetting the port, by default 0.01.
**kwargs : dict
Additional keyword arguments passed to the serial port if applicable.
Examples
--------
Initialize a Serial port:
>>> trigger = Trigger(port="serial", port_address="COM3", delay=0.01, baudrate=115200, timeout=0)
"""
self.port = port
self.port_address = port_address
Expand Down Expand Up @@ -81,6 +87,8 @@ def send_trigger(self, trigger_code: Union[int, str]):
self._send_parallel_trigger(trigger_code)
elif self.port == "serial":
self._send_serial_trigger(trigger_code)
elif self.port == "dummy":
logger.info(f"Dummy trigger sent: {trigger_code}")

def _send_parallel_trigger(self, trigger_code: int):
"""
Expand All @@ -96,7 +104,7 @@ def _send_parallel_trigger(self, trigger_code: int):
core.wait(self.delay)
self._port.setData(0) # Reset the port after the delay

def _open_parallel_port(self):
def _open_parallel_port(self, **kwargs):
"""
Initialize the parallel port.
Expand All @@ -106,7 +114,7 @@ def _open_parallel_port(self):
"""
from psychopy import parallel
logger.info(f"Parallel port initialized at address {self.port_address}")
self._port = parallel.ParallelPort(address=self.port_address)
self._port = parallel.ParallelPort(address=self.port_address, **kwargs)
self._port.setData(0) # Reset port at initialization

def _open_serial_port(self, **kwargs):
Expand Down

0 comments on commit 08db2b6

Please sign in to comment.