Skip to content

Commit

Permalink
new style api OpenSesame 4.0
Browse files Browse the repository at this point in the history
  • Loading branch information
dev-jam committed Aug 15, 2023
1 parent d81ea99 commit 7e763d0
Show file tree
Hide file tree
Showing 22 changed files with 256 additions and 429 deletions.
11 changes: 9 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,15 @@ ipython_config.py
# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
#poetry.lock

# PEP 582; used by e.g. github.com/David-OConnor/pyflow
# pdm
# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
#pdm.lock
# pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it
# in version control.
# https://pdm.fming.dev/#use-with-ide
.pdm.toml

# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
__pypackages__/

# Celery stuff
Expand Down Expand Up @@ -150,4 +158,3 @@ cython_debug/
# and can be added to the global gitignore or merged into this file. For a more nuclear
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
#.idea/
.DS_Store
4 changes: 0 additions & 4 deletions MANIFEST.in

This file was deleted.

3 changes: 3 additions & 0 deletions opensesame_plugins/parallel_port_trigger/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# These packages will be checked by the updater extension for updates on
# conda and pip
packages = ['opensesame-plugin-parallel_port_trigger']
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
"""Parallel Port Trigger - initializes the parallel port device"""

# The category determines the group for the plugin in the item toolbar
category = "Parallel Port Trigger"
# Defines the GUI controls
controls = [
{
"type": "checkbox",
"var": "dummy_mode",
"label": "Dummy mode",
"name": "checkbox_dummy",
"tooltip": "Run in dummy mode"
}, {
"type": "checkbox",
"var": "verbose",
"label": "Verbose mode",
"name": "checkbox_verbose",
"tooltip": "Run in verbose mode"
}, {
"type": "line_edit",
"var": "port",
"label": "Port Address",
"name": "line_edit_port",
"tooltip": "Address of the parallel port, value is a hexadecimal number (Windows) or path (Linux)"
}, {
"type": "text",
"label": "<small><b>Note:</b> Parallel Port Trigger Init item at the begin of the experiment is needed for initialization of the parallel port</small>"
}, {
"type": "text",
"label": "<small>Parallel Port Trigger version 4.0.0</small>"
}
]
Original file line number Diff line number Diff line change
Expand Up @@ -18,147 +18,120 @@
along with this plug-in. If not, see <http://www.gnu.org/licenses/>.
"""

#import warnings
import os

from libopensesame.py3compat import *
from libopensesame import debug
from libopensesame.item import item
from libqtopensesame.items.qtautoplugin import qtautoplugin
from libopensesame.exceptions import osexception

VERSION = u'3.2.0'


class parallel_port_trigger_init(item):

description = u'Parallel Port Trigger: initializes the parallel port device.'
from libopensesame.item import Item
from libqtopensesame.items.qtautoplugin import QtAutoPlugin
from libopensesame.exceptions import OSException
from libopensesame.oslogging import oslogger

def __init__(self, name, experiment, string=None):

item.__init__(self, name, experiment, string)
self.verbose = u'no'
class ParallelPortTriggerInit(Item):

def reset(self):

self.var.dummy_mode = u'no'
self.var.verbose = u'no'

self.var.dummy_mode = 'no'
self.var.verbose = 'no'
if os.name == 'nt':
self.var.port = u'0x378'
self.var.port = '0x378'
else:
self.var.port = 0

self.show_message(u'Parallel Port Trigger plug-in has been initialized!')

def init_var(self):

self.dummy_mode = self.var.dummy_mode
self.verbose = self.var.verbose
self.experiment.pptrigger_verbose = self.var.verbose

self.experiment.pptrigger_dummy_mode = self.var.dummy_mode
self.experiment.pptrigger_port = self.var.port

def prepare(self):

item.prepare(self)

super().prepare()
self.verbose = 'yes'
self.close()
self.init_var()
self._init_var()
self._check_init()

if self.dummy_mode == u'no':
if self.dummy_mode == 'no':
if os.name == 'nt':
try:
from ctypes import windll
except ImportError as e:
raise osexception(u'The ctypes module can not be loaded. Check if ctypes is installed correctly.', exception=e)

raise OSException('The ctypes module can not be loaded. Check if ctypes is installed correctly.\n\nMessage: %s' % e)
import platform

if platform.architecture()[0] == "32bit":
self.dll_file = 'inpout32.dll'
elif platform.architecture()[0] == "64bit":
self.dll_file = 'inpoutx64.dll'
else:
raise osexception('Platform not supported')
raise OSException('Platform not supported')

path_to_dll_file = os.path.join(os.path.dirname(__file__), self.dll_file)
self.show_message(path_to_dll_file)
self._show_message(path_to_dll_file)

try:
self.experiment.pptrigger = windll.LoadLibrary(path_to_dll_file)

except Exception as e:
raise osexception(
u'Could not load parallel port library ', exception=e)
raise OSException('Could not load parallel port library\n\nMessage: %s' % e)

if isinstance(self.var.port,str):
self.port = self.var.port
else:
raise osexception('Port value should be a string on Windows')

raise OSException('Port value should be a string on Windows')
else:
try:
import parallel
except ImportError as e:
raise osexception('The pyparallel module could not be loaded, please make sure pyparallel is installed correctly.', exception=e)
raise OSException('The pyparallel module could not be loaded, please make sure pyparallel is installed correctly.\n\nMessage: %s' % e)

if isinstance(self.var.port,int):
self.show_message(u'Using parallel port on address: /dev/parport%d' % self.var.port)
self._show_message('Using parallel port on address: /dev/parport%d' % self.var.port)
self.port = self.var.port
else:
raise osexception('Port value should be a integer on Linux')
raise OSException('Port value should be a integer on Linux')

try:
self.experiment.pptrigger = parallel.Parallel(port=self.port)
self._show_message('Parallel Port Trigger plug-in has been initialized!')
except Exception as e:
raise osexception(
u'Could not access the parallel port', exception=e)
raise OSException('Could not access the parallel port\n\nMessage: %s' % e)

self.experiment.cleanup_functions.append(self.close)

## reset trigger
# reset trigger
try:
if os.name == 'nt':
self.set_item_onset(self.experiment.pptrigger.DlPortWritePortUchar(int(self.port,0), 0))
self.set_item_onset(self.experiment.pptrigger.DlPortWritePortUchar(int(self.port, 0), 0))
else:
self.set_item_onset(self.experiment.pptrigger.setData(0))
self.show_message(u'Resetting the parallel port to all zero')
self._show_message('Resetting the parallel port to all zero')
except Exception as e:
raise osexception(
u'Could not access the Parallel Port', exception=e)
elif self.dummy_mode == u'yes':
self.show_message(u'Dummy mode enabled for the Parallel Port Trigger Plug-in')
raise OSException('Could not access the Parallel Port\n\nMessage: %s' % e)
elif self.dummy_mode == 'yes':
self._show_message('Dummy mode enabled for the Parallel Port Trigger Plug-in')
else:
self.show_message(u'Error with dummy mode, mode is: %s' % self.dummy_mode)

def run(self):

pass

def show_message(self, message):

debug.msg(message)
if self.verbose:
print(message)
self._show_message('Error with dummy mode, mode is: %s' % self.dummy_mode)

def close(self):

if not hasattr(self.experiment, "pptrigger") or \
self.experiment.pptrigger is None:
return
self.experiment.pptrigger is None:
return
try:
self.experiment.pptrigger = None
self.show_message("Parallel Port closed")
except:
self.show_message("failed to close Parallel port")
self._show_message("Parallel Port closed")
except Exception:
raise OSException("Failed to close Parallel port")

def _init_var(self):
self.dummy_mode = self.var.dummy_mode
self.verbose = self.var.verbose
self.experiment.pptrigger_verbose = self.var.verbose
self.experiment.pptrigger_dummy_mode = self.var.dummy_mode
self.experiment.pptrigger_port = self.var.port

class qtparallel_port_trigger_init(parallel_port_trigger_init, qtautoplugin):
def _check_init(self):
if hasattr(self.experiment, 'pptrigger'):
raise OSException('You should have only one instance of `parallel_port_trigger_init` in your experiment')

def __init__(self, name, experiment, script=None):
def _show_message(self, message):
oslogger.debug(message)
if self.verbose == 'yes':
oslogger.warning(message)

parallel_port_trigger_init.__init__(self, name, experiment, script)
qtautoplugin.__init__(self, __file__)

class qtParallelPortTriggerInit(ParallelPortTriggerInit, QtAutoPlugin):

def __init__(self, name, experiment, script=None):
ParallelPortTriggerInit.__init__(self, name, experiment, script)
QtAutoPlugin.__init__(self, __file__)
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
"""Parallel Port Trigger - send trigger"""

# The category determines the group for the plugin in the item toolbar
category = "Parallel Port Trigger"
# Defines the GUI controls
controls = [
{
"type": "line_edit",
"var": "value",
"label": "Value",
"name": "line_edit_port",
"tooltip": "Value to set port"
}, {
"type": "checkbox",
"var": "duration_check",
"label": "Enable duration",
"name": "checkbox_duration_check",
"tooltip": "When enabled, a pulse is created instead of a state change"
}, {
"type": "line_edit",
"var": "duration",
"label": "Duration (ms)",
"name": "line_edit_duration",
"tooltip": "Value in ms"
}, {
"type": "text",
"label": "<small><b>Note:</b> Parallel Port Trigger Init item at the begin of the experiment is needed for initialization of the parallel port</small>"
}, {
"type": "text",
"label": "<small>Parallel Port Trigger version 4.0.0</small>"
}
]
Loading

0 comments on commit 7e763d0

Please sign in to comment.