@@ -10,6 +10,7 @@ def handle_sample(sample):
1010board.start(handle_sample)
1111
1212TODO: Pick between several boards
13+ TODO: support impedance
1314
1415"""
1516import struct
@@ -25,14 +26,17 @@ def handle_sample(sample):
2526# local bluepy should take precedence
2627import sys
2728sys .path .insert (0 ,"bluepy/bluepy" )
29+ from btle import Scanner , DefaultDelegate , Peripheral
2830
2931SAMPLE_RATE = 100.0 # Hz
3032scale_fac_uVolts_per_count = 1200 * 8388607.0 * 1.5 * 51.0 ;
3133
34+ # service for communication, as per docs
3235BLE_SERVICE = "fe84"
33- BLE_RECEIVE = "2d30c082f39f4ce6923f3484ea480596"
34- BLE_SEND = "2d30c083f39f4ce6923f3484ea480596"
35- BLE_DISCONNECT = "2d30c084f39f4ce6923f3484ea48059"
36+ # characteristics of interest
37+ BLE_CHAR_RECEIVE = "2d30c082f39f4ce6923f3484ea480596"
38+ BLE_CHAR_SEND = "2d30c083f39f4ce6923f3484ea480596"
39+ BLE_CHAR_DISCONNECT = "2d30c084f39f4ce6923f3484ea480596"
3640
3741'''
3842#Commands for in SDK http://docs.openbci.com/Hardware/08-Ganglion_Data_Forma
@@ -59,37 +63,44 @@ def __init__(self, port=None, baud=0, filter_data=False,
5963
6064 print ("Looking for Ganglion board" )
6165 if port == None :
62- port == self .find_port ()
63- self .port = port
66+ port = self .find_port ()
67+ self .port = port # find_port might not return string
6468
65- print ("Serial established..." )
69+ print ("Init BLE connection with MAC: " + self .port )
70+ print ("NB: if it fails, try with root privileges." )
71+ self .gang = Peripheral (port , 'random' ) # ADDR_TYPE_RANDOM
6672
67- time .sleep (2 )
68- #Initialize 32-bit board, doesn't affect 8bit board
69- self .ser .write (b'v' );
73+ print ("Get mainservice..." )
74+ self .service = self .gang .getServiceByUUID (BLE_SERVICE )
75+ print ("Got:" + str (self .service ))
76+
77+ print ("Get characteristics..." )
78+ self .char_read = self .service .getCharacteristics (BLE_CHAR_RECEIVE )[0 ]
79+ print ("receive, properties: " + str (self .char_read .propertiesToString ()) + ", supports read: " + str (self .char_read .supportsRead ()))
80+
81+ self .char_write = self .service .getCharacteristics (BLE_CHAR_SEND )[0 ]
82+ print ("write, properties: " + str (self .char_write .propertiesToString ()) + ", supports read: " + str (self .char_write .supportsRead ()))
7083
84+ self .char_discon = self .service .getCharacteristics (BLE_CHAR_DISCONNECT )[0 ]
85+ print ("disconnect, properties: " + str (self .char_discon .propertiesToString ()) + ", supports read: " + str (self .char_discon .supportsRead ()))
7186
72- #wait for device to be ready
87+ print ("Connection established" )
88+
89+ #wait for device to be ready, just in case
7390 time .sleep (1 )
74- self .print_incoming_text ()
7591
7692 self .streaming = False
77- self .filtering_data = filter_data
7893 self .scaling_output = scaled_output
7994 self .eeg_channels_per_sample = 4 # number of EEG channels per sample *from the board*
8095 self .read_state = 0
8196 self .log_packet_count = 0
82- self .last_reconnect = 0
83- self .reconnect_freq = 5
8497 self .packets_dropped = 0
8598
8699 #Disconnects from board when terminated
87100 atexit .register (self .disconnect )
88101
89102 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
103+ """DetectsGanglion board MAC address -- if more than 1 around, will select first. Needs root privilege."""
93104
94105 print ("Try to detect Ganglion MAC address. NB: Turn on bluetooth and run as root for this to work!" )
95106 scan_time = 5
@@ -145,10 +156,6 @@ def ser_write(self, b):
145156 def ser_read (self ):
146157 """Access serial port object for read"""
147158 return self .ser .read ()
148-
149- def ser_inWaiting (self ):
150- """Access serial port object for inWaiting"""
151- return self .ser .inWaiting ();
152159
153160 def getSampleRate (self ):
154161 return SAMPLE_RATE
@@ -327,29 +334,6 @@ def warn(self, text):
327334 logging .warning (text )
328335 print ("Warning: %s" % text )
329336
330-
331- def print_incoming_text (self ):
332- """
333-
334- When starting the connection, print all the debug data until
335- we get to a line with the end sequence '$$$'.
336-
337- """
338- line = ''
339- #Wait for device to send data
340- time .sleep (1 )
341-
342- if self .ser .inWaiting ():
343- line = ''
344- c = ''
345- #Look for end sequence $$$
346- while '$$$' not in line :
347- c = self .ser .read ().decode ('utf-8' , errors = 'replace' ) # we're supposed to get UTF8 text, but the board might behave otherwise
348- line += c
349- print (line );
350- else :
351- self .warn ("No Message" )
352-
353337 def check_connection (self , interval = 2 , max_packets_to_skip = 10 ):
354338 # stop checking when we're no longer streaming
355339 if not self .streaming :
0 commit comments