File: _parallelport.py

package info (click to toggle)
python-expyriment 0.7.0%2Bgit34-g55a4e7e-3.2
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 1,504 kB
  • ctags: 2,094
  • sloc: python: 12,766; makefile: 150
file content (153 lines) | stat: -rw-r--r-- 4,134 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
"""
Input and output parallel port.

This module contains a class implementing parallel port input/output.

"""

__author__ = 'Florian Krause <florian@expyriment.org> \
Oliver Lindemann <oliver@expyriment.org>'
__version__ = '0.7.0'
__revision__ = '55a4e7e'
__date__ = 'Wed Mar 26 14:33:37 2014 +0100'


from sys import platform
from os import listdir

try:
    import parallel
except:
    parallel = None

import expyriment
from  _input_output  import Input, Output


class ParallelPort(Input, Output):
    """A class implementing a parallel port input and output.

    Notes
    -----
    CAUTION: Under Windows (starting from 2000) direct I/O is blocked.
    Install http://sourceforge.net/projects/pyserial/files/pyparallel/giveio/

    """

    def __init__(self):
        """Create a parallel port input and output.

        """

        import types
        if type(parallel) is not types.ModuleType:
            message = """ParallelPort can not be initialized.
The Python package 'pyParallel' is not installed."""
            raise ImportError(message)

        if float(parallel.VERSION) < 0.2:
            raise ImportError("Expyriment {0} ".format(__version__) +
                    "is not compatible with PyParallel {0}.".format(
                        parallel.VERSION) +
                      "\nPlease install PyParallel 0.2 or higher.")

        Input.__init__(self)
        Output.__init__(self)
        self._parallel = parallel.Parallel()
        self.input_history = False # dummy

    @property
    def parallel(self):
        """Getter for parallel"""
        return self._parallel

    @property
    def has_input_history(self):
        """Returns always False, because ParallelPort has no input history."""

        return False

    def clear(self):
        """Clear the parallell port.

        Dummy method required for port interfaces (see e.g. ButtonBox)

        """

        pass

    def poll(self):
        """Poll the parallel port.

        The parlallel port will be polled. The result will be put into the
        buffer and returned.
        The parallel module for Python can only read three of the status
        lines. The result is thus coded in three bits:

        Acknowledge Paper-Out Selected

        Example: '4' means only Selected is receiving data ("001").

        To send out data the actual data lines are used.

        """

        bits = "{2}{1}{0}".format(int(self._parallel.getInAcknowledge()),
                            int(self._parallel.getInPaperOut()),
                            int(self._parallel.getInSelected()))
        byte = int(bits, 2)
        if self._logging:
            expyriment._active_exp._event_file_log(
                    "ParallelPort,received,{0},poll".format(ord(byte)), 2)
        return ord(byte)

    @staticmethod
    def get_available_ports():
        """Return an array of strings representing the available serial ports.

        If pyparallel is not installed, 'None' will be returned.

        Returns
        -------
        ports : list
            array of strings representing the available serial ports

        """

        import types

        if type(parallel) is not types.ModuleType:
            return None
        ports = []
        if platform.startswith("linux"): #for Linux operation systems
            dev = listdir('/dev')
            for p in dev:
                if p.startswith("parport"):
                    ports.append(p)
        elif platform == "dawin": #for MacOS
            pass
        else: #for windows, os2
            for p in range(256):
                try:
                    p = parallel.Parallel(p)
                    ports.append("LTP{0}".format(p + 1))
                except:
                    pass
        ports.sort()

        return ports

    def send(self, data):
        """Send data.

        Parameters
        ----------
        data : int
            data to be sent

        """

        self.parallel.setData(data)
        if self._logging:
            expyriment._active_exp._event_file_log(
                                    "ParallelPort,sent,{0}".format(data), 2)