Skip to content

Commit

Permalink
refactored code, created directory structure
Browse files Browse the repository at this point in the history
  • Loading branch information
gabrielibagon committed Aug 27, 2016
1 parent 7bbe3dd commit 7b84f50
Show file tree
Hide file tree
Showing 6 changed files with 100 additions and 51 deletions.
Empty file added lib/__init__.py
Empty file.
23 changes: 18 additions & 5 deletions gui.py → lib/gui.py
@@ -1,23 +1,36 @@
'''
gui.py
------
This module creates and controls GUI function, using the PyQt4 framework. Using the GUI,
the user can manipulate LSL parameters and board configuration, as well as control the
creation, start, and stop of the streams.
'''

from collections import OrderedDict
import signal
import threading
import lsl_openbci
import open_bci_v3 as bci
import lib.streamerlsl as streamerlsl
import lib.open_bci_v3 as bci
import time
import sys

try:
from PyQt4 import QtGui,QtCore
except ImportError:
print("GUI unavailable, use command line interface: \n" + \
" python lsl_openbci.py --stream")
print("GUI unavailable: PyQt4 not installed. \n" + \
"Use command line interface: \n" + \
" python lsl_openbci.py [port] --stream")
sys.exit(0)


class GUI(QtGui.QWidget):
def __init__(self):
super(GUI, self).__init__()
self.gui_setup()
self.lsl = lsl_openbci.StreamerLSL(GUI=True)
self.lsl = streamerlsl.StreamerLSL(GUI=True)
signal.signal(signal.SIGINT, signal.SIG_DFL)


Expand Down
2 changes: 1 addition & 1 deletion open_bci_v3.py → lib/open_bci_v3.py
Expand Up @@ -82,7 +82,7 @@ def __init__(self, port=None, baud=115200, filter_data=True,
if print_enable:
print("Serial established...")
else:
print("Detecting default settings...")
print("Detecting board settings...")
time.sleep(2)
#Initialize 32-bit board, doesn't affect 8bit board
self.ser.write(b'v');
Expand Down
59 changes: 19 additions & 40 deletions lsl_openbci.py → lib/streamerlsl.py
@@ -1,21 +1,25 @@
#!/usr/bin/python
'''
lsl_openbci.py
streamerlsl.py
---------------
LSL is a
This is the module that handles the creation and function of LSL using OpenBCI data.
If the GUI application is used, the GUI controls the parameters of the stream, and calls
the functions of this class to create, run, and stop each stream instance.
If the command line application is used, this module creates the LSL instances
using default parameters, and then allows the user interaction with the stream via the CLI.
'''

import sys
import open_bci_v3 as bci
import threading
from PyQt4 import QtGui,QtCore
from pylsl import StreamInfo,StreamOutlet
import signal
from collections import OrderedDict
import time


import lib.open_bci_v3 as bci
from pylsl import StreamInfo, StreamOutlet
import sys

class StreamerLSL():

Expand All @@ -24,8 +28,8 @@ def __init__(self,port=None,GUI=False):
self.current_settings = OrderedDict()

# initial settings
self.eeg_channels = 8
self.aux_channels = 3
# self.eeg_channels = 8
# self.aux_channels = 3

if not GUI:
if port is None:
Expand All @@ -49,10 +53,9 @@ def initialize_board(self,autodetect=False,port=None,daisy=None):

def init_board_settings(self):
#set default board configuration
if self.eeg_channels == 16:
self.default_settings["Number_Channels"] = [b'C']
else:
self.default_settings["Number_Channels"] = [b'c']

#default to 16 channels initially
self.default_settings["Number_Channels"] = [b'C']
for i in range(16):
current = "channel{}".format(i+1)
self.default_settings[current] = []
Expand Down Expand Up @@ -264,28 +267,4 @@ def begin(self):
if sys.hexversion > 0x03000000:
s = input('--> ')
else:
s = raw_input('--> ')


def main(argv):
if not argv:
import gui
app = QtGui.QApplication(sys.argv)
window = gui.GUI()
sys.exit(app.exec_())
else:
if argv[0] == '--stream':
lsl = StreamerLSL(GUI=False)
lsl.create_lsl()
lsl.begin()
else:
port = argv[0]
if argv[1] == '--stream':
lsl = StreamerLSL(port=port,GUI=False)
lsl.create_lsl()
lsl.begin()
# else:
# print("Command '%s' not recognized" % argv[0])

if __name__ == '__main__':
main(sys.argv[1:])
s = raw_input('--> ')
56 changes: 56 additions & 0 deletions openbci_lsl.py
@@ -0,0 +1,56 @@
#!/usr/bin/python
'''
openbci_lsl.py
---------------
This is the main module for establishing an OpenBCI stream through the Lab Streaming Layer (LSL).
Lab streaming layer is a networking system for real time streaming, recording, and analysis of time-series
data. LSL can be used to connect OpenBCI to applications that can record, analyze, and manipulate the data,
such as Matlab, NeuroPype, BCILAB, and others.
To run the program as a GUI application, enter `python openbci_lsl.py`.
To run the program as a command line interface, enter `python openbci_lsl.py [port] --stream`. The port argument
is optional, since the program automatically detects the OpenBCI port. However, if this functionality fails, the
port must be specified as an argument.
For more information, see the readme on the Github repo:
https://github.com/gabrielibagon/OpenBCI_LSL
'''

import sys
import lib.streamerlsl as streamerlsl

def main(argv):

# if no arguments are provided, default to the GUI application
if not argv:
import lib.gui as gui
from PyQt4 import QtGui
app = QtGui.QApplication(sys.argv)
window = gui.GUI()
sys.exit(app.exec_())

# if user specifies CLI using the "--stream" argument, check if a port is also specified
else:
if argv[0] == '--stream':
lsl = streamerlsl.StreamerLSL(GUI=False)
else:
try:
if argv[1] != '--stream':
print ("Command '%s' not recognized" % argv[1])
return
except IndexError:
print("Command '%s' not recognized" % argv[0])
return
port = argv[0]
lsl = streamerlsl.StreamerLSL(port=port,GUI=False)
lsl.create_lsl()
lsl.begin()


if __name__ == '__main__':
main(sys.argv[1:])
11 changes: 6 additions & 5 deletions readme.md
Expand Up @@ -5,7 +5,8 @@ This tutorial contains information on how to stream [OpenBCI](http://openbci.com

Lab streaming layer is a networking system for real time streaming, recording, and analysis of time-series data. LSL can be used to connect OpenBCI to applications that can record, analyze, and manipulate the data, such as Matlab, NeuroPype, BCILAB, and others.

The [OpenBCI_LSL](link) repo contains a Python script that establishes an LSL stream of OpenBCI data, as well as the libraries and files needed to run LSL.
The [OpenBCI_LSL](link) repo contains a Python script that establishes an LSL stream of OpenBCI data, as well as the libraries and files needed to run LSL. Works on Windows, OSX, and Linux.



# SETUP
Expand Down Expand Up @@ -35,19 +36,19 @@ The [OpenBCI_LSL](link) repo contains a Python script that establishes an LSL st

First, make sure your dongle is plugged in and board is powered on. Then go the the "OpenBCI_LSL" folder and type the following command:

`python lsl_openbci.py --stream`
`python openbci_lsl.py --stream`

After a few moments, you should see this output:

![cli](https://raw.githubusercontent.com/gabrielibagon/OpenBCI_LSL/master/images/CLI.jpg)

If an error is raised about not being able to find your the board or serial port, you can override the automatic board detection by specifying the serial port in a command line argument before "--stream". The format is as follows:

`python lsl_openbci.py [PORT] --stream`
`python openbci_lsl.py [PORT] --stream`

For example:

`python lsl_openbci.py /dev/ttyUSB0 --stream`
`python openbci_lsl.py /dev/ttyUSB0 --stream`

After board initialization, you are now ready to start streaming.

Expand Down Expand Up @@ -77,7 +78,7 @@ If you get an error message at any point while using the command line interface,

If you would like the ability to configure the board and LSL stream with advanced settings, you can do so by running the GUI. The GUI comes up by default if you run the program with no flags:

`python lsl_openbci.py`
`python openbci_lsl.py`

If you plug in your board and dongle before running the above command, the program should have already detected the appropriate settings for your board (port and daisy). If not, you can enter those yourself in the appropriate fields.

Expand Down

0 comments on commit 7b84f50

Please sign in to comment.