Skip to content

Light Stimulator

Source code in src\eConEXG\triggerBox\triggerbox.py
 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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
class lightStimulator:
    def __init__(self, port: str = None):
        from serial import Serial
        from serial.tools.list_ports import comports

        self.wait_time = 0.1
        self.channels = 6

        if not port:
            for ports in comports():
                if (
                    ports.pid == 0x6001
                    and ports.vid == 0x0403
                    and ports.serial_number in ["LIGHTSTIMA", "LIGHTSTIM"]
                ):
                    port = ports.device
                    break
            else:
                raise Exception("Light stimulator not found")
        self.dev = Serial(port, baudrate=115200, timeout=2)
        self.dev.read_all()

    def vep_mode(self, fs: list[Optional[float]] = [1, 1, 1, 1, 1, 1]):
        """
        Enter VEP mode, which allows you to control the frequency of each channel separately.

        Args:
            fs: List of frequencies in Hz, range from 0 to 100 with 0.1Hz resolution.
                If a corresponding frequency is None, 0  or not given, it will be set to off.

        Raises:
            Exception: If the frequency is invalid or hardware error.
        """
        fss = fs.copy()
        if len(fss) < self.channels:
            fss += [0] * (self.channels - len(fss))
        for i in range(self.channels):
            fss[i] = self._validate_fs(fss[i])
        command = ",".join([f"{f:.1f}" for f in fss[: self.channels]])
        command = f"AT+VEP={command}\r\n".encode()
        self.dev.write(command)
        time.sleep(self.wait_time)
        ret = self.dev.read_all()
        if b"SSVEP MODE OK" not in ret:
            raise Exception("Failed to set VEP mode")

    def erp_mode(self, fs: float):
        """
        Enter ERP mode, which allows you to control the frequency of all channels at once.

        Args:
            fs: Frequency in Hz, range from 0 to 100 with 0.1Hz resolution.

        Raises:
            Exception: If the frequency is invalid or hardware error.
        """
        fs = self._validate_fs(fs)
        command = f"AT+ERP={fs:.1f}\r\n".encode()
        self.dev.write(command)
        time.sleep(self.wait_time)
        ret = self.dev.read_all()
        if b"ERP MODE OK" not in ret:
            raise Exception("Failed to set VEP mode")

    def _validate_fs(self, fs: Optional[float]):
        if not isinstance(fs, (int, float)):
            if fs is None:
                fs = 0
            else:
                raise Exception("Invalid frequency")
        if fs > 100 or fs < 0:
            raise Exception("Invalid frequency")
        return fs

    def close_dev(self):
        self.dev.close()

erp_mode(fs)

Enter ERP mode, which allows you to control the frequency of all channels at once.

Parameters:

Name Type Description Default
fs float

Frequency in Hz, range from 0 to 100 with 0.1Hz resolution.

required

Raises:

Type Description
Exception

If the frequency is invalid or hardware error.

Source code in src\eConEXG\triggerBox\triggerbox.py
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
def erp_mode(self, fs: float):
    """
    Enter ERP mode, which allows you to control the frequency of all channels at once.

    Args:
        fs: Frequency in Hz, range from 0 to 100 with 0.1Hz resolution.

    Raises:
        Exception: If the frequency is invalid or hardware error.
    """
    fs = self._validate_fs(fs)
    command = f"AT+ERP={fs:.1f}\r\n".encode()
    self.dev.write(command)
    time.sleep(self.wait_time)
    ret = self.dev.read_all()
    if b"ERP MODE OK" not in ret:
        raise Exception("Failed to set VEP mode")

vep_mode(fs=[1, 1, 1, 1, 1, 1])

Enter VEP mode, which allows you to control the frequency of each channel separately.

Parameters:

Name Type Description Default
fs list[Optional[float]]

List of frequencies in Hz, range from 0 to 100 with 0.1Hz resolution. If a corresponding frequency is None, 0 or not given, it will be set to off.

[1, 1, 1, 1, 1, 1]

Raises:

Type Description
Exception

If the frequency is invalid or hardware error.

Source code in src\eConEXG\triggerBox\triggerbox.py
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
def vep_mode(self, fs: list[Optional[float]] = [1, 1, 1, 1, 1, 1]):
    """
    Enter VEP mode, which allows you to control the frequency of each channel separately.

    Args:
        fs: List of frequencies in Hz, range from 0 to 100 with 0.1Hz resolution.
            If a corresponding frequency is None, 0  or not given, it will be set to off.

    Raises:
        Exception: If the frequency is invalid or hardware error.
    """
    fss = fs.copy()
    if len(fss) < self.channels:
        fss += [0] * (self.channels - len(fss))
    for i in range(self.channels):
        fss[i] = self._validate_fs(fss[i])
    command = ",".join([f"{f:.1f}" for f in fss[: self.channels]])
    command = f"AT+VEP={command}\r\n".encode()
    self.dev.write(command)
    time.sleep(self.wait_time)
    ret = self.dev.read_all()
    if b"SSVEP MODE OK" not in ret:
        raise Exception("Failed to set VEP mode")