Skip to content

Commit f6bafc9

Browse files
committed
ganglion: detecting MAC address
1 parent 36cfb50 commit f6bafc9

File tree

2 files changed

+65
-4
lines changed

2 files changed

+65
-4
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@ This library includes the main open_bci_v3 class definition that instantiates an
1919

2020
For additional details on connecting your board visit: http://docs.openbci.com/tutorials/01-GettingStarted
2121

22+
### Ganglion Board
23+
24+
The Ganglion board relies on Bluetooth Low Energy connectivity (BLE). You should also retrieve the bluepy submodule for a more up-to-date version. To do so, clone this repo with the `--recursive` flag then type `make` inside `bluepy/bluepy`. Note that you may need to run the script with root privileges to for some functionality, e.g. auto-detect MAC address.
25+
2226
## Audience:
2327

2428
This python code is meant to be used by people familiar with python and programming in general. It's purpose is to allow for programmers to interface with OpenBCI technology directly, both to acquire data and to write programs that can use that data on a live setting, using python.

open_bci_ganglion.py

Lines changed: 61 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,9 @@ def handle_sample(sample):
2222
import sys
2323
import pdb
2424
import glob
25-
import bluepy
25+
# local bluepy should take precedence
26+
import sys
27+
sys.path.insert(0,"bluepy/bluepy")
2628

2729
SAMPLE_RATE = 100.0 # Hz
2830
scale_fac_uVolts_per_count = 1200 * 8388607.0 * 1.5 * 51.0;
@@ -45,7 +47,8 @@ class OpenBCIBoard(object):
4547
Handle a connection to an OpenBCI board.
4648
4749
Args:
48-
port, baud, filter_data, daisy: Not used, for compatibility with v3
50+
port: MAC address of the Ganglion Board. "None" to attempt auto-detect.
51+
baud, filter_data, daisy: Not used, for compatibility with v3
4952
"""
5053

5154
def __init__(self, port=None, baud=0, filter_data=False,
@@ -54,8 +57,10 @@ def __init__(self, port=None, baud=0, filter_data=False,
5457
self.streaming = False
5558
self.timeout = timeout
5659

57-
print("Connecting to V3 at port %s" %(port))
58-
self.ser = serial.Serial(port= port, baudrate = baud, timeout=timeout)
60+
print("Looking for Ganglion board")
61+
if port == None:
62+
port == self.find_port()
63+
self.port = port
5964

6065
print("Serial established...")
6166

@@ -80,6 +85,58 @@ def __init__(self, port=None, baud=0, filter_data=False,
8085

8186
#Disconnects from board when terminated
8287
atexit.register(self.disconnect)
88+
89+
def find_port(self):
90+
"""Detects Ganglion board MAC address -- if more than 1 around, will select first. Needs root privilege."""
91+
92+
from btle import Scanner, DefaultDelegate
93+
94+
print("Try to detect Ganglion MAC address. NB: Turn on bluetooth and run as root for this to work!")
95+
scan_time = 5
96+
print("Scanning for 5 seconds nearby devices...")
97+
98+
# From bluepy example
99+
class ScanDelegate(DefaultDelegate):
100+
def __init__(self):
101+
DefaultDelegate.__init__(self)
102+
103+
def handleDiscovery(self, dev, isNewDev, isNewData):
104+
if isNewDev:
105+
print ("Discovered device: " + dev.addr)
106+
elif isNewData:
107+
print ("Received new data from: " + dev.addr)
108+
109+
scanner = Scanner().withDelegate(ScanDelegate())
110+
devices = scanner.scan(scan_time)
111+
112+
nb_devices = len(devices)
113+
if nb_devices < 1:
114+
print("No BLE devices found. Check connectivity.")
115+
return ""
116+
else:
117+
print("Found " + str(nb_devices) + ", detecting Ganglion")
118+
list_mac = []
119+
list_id = []
120+
121+
for dev in devices:
122+
# "Ganglion" should appear inside the "value" associated to "Complete Local Name", e.g. "Ganglion-b2a6"
123+
for (adtype, desc, value) in dev.getScanData():
124+
if desc == "Complete Local Name" and value.startswith("Ganglion"):
125+
list_mac.append(dev.addr)
126+
list_id.append(value)
127+
print("Got Ganglion: " + value + ", with MAC: " + dev.addr)
128+
break
129+
nb_ganglions = len(list_mac)
130+
131+
if nb_ganglions < 1:
132+
print("No Ganglion found ;(")
133+
raise OSError('Cannot find OpenBCI Ganglion MAC address')
134+
135+
if nb_ganglions > 1:
136+
print("Found " + str(nb_ganglions) + ", selecting first")
137+
138+
print("Selecting MAC address " + list_mac[0] + " for " + list_id[0])
139+
return list_mac[0]
83140

84141
def ser_write(self, b):
85142
"""Access serial port object for write"""

0 commit comments

Comments
 (0)