diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..1dbc687 --- /dev/null +++ b/.gitignore @@ -0,0 +1,62 @@ +# Byte-compiled / optimized / DLL files +__pycache__/ +*.py[cod] +*$py.class + +# C extensions +*.so + +# Distribution / packaging +.Python +env/ +build/ +develop-eggs/ +dist/ +downloads/ +eggs/ +.eggs/ +lib/ +lib64/ +parts/ +sdist/ +var/ +*.egg-info/ +.installed.cfg +*.egg + +# PyInstaller +# Usually these files are written by a python script from a template +# before PyInstaller builds the exe, so as to inject date/other infos into it. +*.manifest +*.spec + +# Installer logs +pip-log.txt +pip-delete-this-directory.txt + +# Unit test / coverage reports +htmlcov/ +.tox/ +.coverage +.coverage.* +.cache +nosetests.xml +coverage.xml +*,cover +.hypothesis/ + +# Translations +*.mo +*.pot + +# Django stuff: +*.log + +# Sphinx documentation +docs/_build/ + +# PyBuilder +target/ + +#Ipython Notebook +.ipynb_checkpoints diff --git a/parallel_port_trigger/info.yaml b/parallel_port_trigger/info.yaml index 89754c1..8a0068e 100644 --- a/parallel_port_trigger/info.yaml +++ b/parallel_port_trigger/info.yaml @@ -9,18 +9,8 @@ controls: label: "Value" name: "value_widget" tooltip: "Value to set port" - type: "spinbox" - min_val: 1 - max_val: 255 + type: "line_edit" var: "pp_value" -- - label: "Duration" - name: "duration_widget" - tooltip: "Expecting a value in milliseconds" - type: "spinbox" - min_val: 0 - max_val: 1000000000 - var: "pp_duration" - label: "Port Adress" name: "port_widget" diff --git a/parallel_port_trigger/parallel_port_trigger.py b/parallel_port_trigger/parallel_port_trigger.py index 9a99fb3..b1a9287 100644 --- a/parallel_port_trigger/parallel_port_trigger.py +++ b/parallel_port_trigger/parallel_port_trigger.py @@ -30,8 +30,6 @@ from libopensesame.item import item from libqtopensesame.items.qtautoplugin import qtautoplugin -from openexp.keyboard import keyboard - if os.name == 'posix': # import the local modified version of pyparallel # that allows for non-exclusive connections to the parport @@ -61,7 +59,7 @@ class parallel_port_trigger(item): """ # Provide an informative description for your plug-in. - description = u'Experiment Manager Plugin' + description = u'Parallel Port Trigger Plug-in' def reset(self): @@ -69,8 +67,8 @@ def reset(self): # Set default experimental variables and values self.var.pp_value = 0 - self.var.pp_duration = 500 self.var.pp_dummy = u'no' + if os.name == 'nt': self.var.pp_port = u'0x378' else: @@ -80,7 +78,6 @@ def reset(self): # --debug argument. debug.msg(u'Parallel Port Trigger plug-in has been initialized!') - def prepare(self): """Preparation phase""" @@ -88,21 +85,23 @@ def prepare(self): # Call the parent constructor. item.prepare(self) - if self.var.pp_dummy == u'no': - try: - if os.name == 'nt': - self.pp = windll.dlportio - else: - self.pp = parallel.Parallel() - except OSError: - print(u'Could not access the parallel port on address: %s' % self.var.pp_port) + if self.pp_dummy == u'no': + if not hasattr(self.experiment, "pp"): + try: + if os.name == 'nt': + self.experiment.pp = windll.dlportio + else: + self.experiment.pp = parallel.Parallel() + + self.experiment.cleanup_functions.append(self.close) + self.python_workspace[u'pp'] = self.experiment.pp + except: + raise osexception( + _(u'Could not access the Parallel Port')) elif self.var.pp_dummy == u'yes': - print(u'Dummy mode enabled, prepare phase') + debug.msg(u'Dummy mode enabled, prepare phase') else: - print(u'Error with dummy mode, mode is: %s' % self.var.pp_dummy) - - # create keyboard object - self.kb = keyboard(self.experiment, keylist=['escape']) + debug.msg(u'Error with dummy mode, mode is: %s' % self.var.pp_dummy) def run(self): @@ -113,32 +112,35 @@ def run(self): # Set the pp value - if self.var.pp_dummy == u'no' and self.var.pp_duration != 0: + if self.var.pp_dummy == u'no': ## turn trigger on if os.name == 'nt': - self.set_item_onset(self.pp.DlPortWritePortUchar(int(self.var.pp_port,0), self.var.pp_value)) + self.set_item_onset(self.experiment.pp.DlPortWritePortUchar(int(self.var.pp_port,0), self.var.pp_value)) else: - self.set_item_onset(self.pp.setData(self.var.pp_value)) - print(u'Sending value %s for %s ms to the parallel port on address: %s' % (self.var.pp_value,self.var.pp_duration,self.var.pp_port)) - - # use keyboard as timeout, allowing for Escape presses to abort experiment - self.kb.get_key(timeout=self.var.pp_duration) - - # turn trigger off - if os.name == 'nt': - self.pp.DlPortWritePortUchar(int(self.var.pp_port,0), 0) - else: - self.pp.setData(0) - elif self.var.pp_dummy == u'no' and self.var.pp_duration == 0: - print(u'Duration is set to 0, so not doing anything') - elif self.var.pp_dummy == u'yes' and self.var.pp_duration != 0: - print(u'Dummy mode enabled, NOT sending value %s for %s ms to the parallel port on address: %s' % (self.var.pp_value,self.var.pp_duration,self.var.pp_port)) - elif self.var.pp_dummy == u'yes' and self.var.pp_duration == 0: - print(u'Dummy mode enabled, duration is set to 0, so not doing anything') + self.set_item_onset(self.experiment.pp.setData(self.pp_value)) + debug.msg(u'Sending value %s to the parallel port on address: %s' % (self.var.pp_value,self.var.pp_port)) + elif self.var.pp_dummy == u'yes': + debug.msg(u'Dummy mode enabled, NOT sending value %s to the parallel port on address: %s' % (self.var.pp_value,self.var.pp_port)) else: - print(u'Error with dummy mode or duration') - # Report success - return True + debug.msg(u'Error with dummy mode!') + + def close(self): + + """ + desc: + Neatly close the connection to the buttonbox. + """ + + if not hasattr(self.experiment, "bb") or \ + self.experiment.bb is None: + debug.msg("no active Buttonbox") + return + try: + self.experiment.bb.close() + self.experiment.bb = None + debug.msg("buttonbox closed") + except: + debug.msg("failed to close buttonbox") class qtparallel_port_trigger(parallel_port_trigger, qtautoplugin): diff --git a/parallel_port_trigger/parallel_port_trigger.pyc b/parallel_port_trigger/parallel_port_trigger.pyc new file mode 100644 index 0000000..d36cf75 Binary files /dev/null and b/parallel_port_trigger/parallel_port_trigger.pyc differ diff --git a/parallel_port_trigger/parallelppdev.pyc b/parallel_port_trigger/parallelppdev.pyc new file mode 100644 index 0000000..f55d4fc Binary files /dev/null and b/parallel_port_trigger/parallelppdev.pyc differ