-
Notifications
You must be signed in to change notification settings - Fork 0
/
LSL_Stream_Facial_Multiple_LEDs.py
41 lines (28 loc) · 1.49 KB
/
LSL_Stream_Facial_Multiple_LEDs.py
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
"""Code modified from the example program to show how to read a multi-channel time series from LSL at https://github.com/OpenBCI/OpenBCI_GUI/blob/master/Networking-Test-Kit/LSL/lslStreamTest.py."""
from pylsl import StreamInlet, resolve_stream
import time
import serial
# set up Arduino serial port - replace with the one you are using
ser = serial.Serial('COM4', 9600)
# resolve an EMG stream on the lab network and notify the user
print("Looking for an EMG stream...")
streams = resolve_stream('type', 'EMG')
inlet = StreamInlet(streams[0])
#inlet_ch2 = StreamInlet(streams[1])
print("EMG stream found!")
# initialize time threshold and variables for storing time
time_thres = 300
prev_time = 0
flex_thres = 0.3
while True:
samples, timestamp = inlet.pull_sample() # get EMG data sample and its timestamp
curr_time = int(round(time.time() * 1000)) # get current time in milliseconds
if ((samples[2] >= flex_thres) & (curr_time - time_thres > prev_time)): # if an EMG spike is detected from the cheek muscles send 'G'
prev_time = int(round(time.time() * 1000)) # update time
ser.write(b'Y')
if((samples[1] >= flex_thres) & (curr_time - time_thres > prev_time)): # if an EMG spike is detected from the eyebrow muscles send 'R'
prev_time = int(round(time.time() * 1000)) # update time
ser.write(b'R')
elif(curr_time - time_thres > prev_time): # if no spike is detected send 'B'
prev_time = int(round(time.time() * 1000)) # update time
ser.write(b'B')