@@ -28,7 +28,7 @@ def handle_sample(sample):
2828import requests
2929import xmltodict
3030
31- from openbci .utils import k , ParseRaw , ssdp
31+ from openbci .utils import k , ParseRaw , OpenBCISample , ssdp
3232
3333SAMPLE_RATE = 0 # Hz
3434
@@ -56,14 +56,17 @@ class OpenBCIWiFi(object):
5656 """
5757
5858 def __init__ (self , ip_address = None , shield_name = None , sample_rate = None , log = True , timeout = 3 ,
59- max_packets_to_skip = 20 , latency = 10000 , high_speed = True , ssdp_attempts = 5 ):
59+ max_packets_to_skip = 20 , latency = 10000 , high_speed = True , ssdp_attempts = 5 ,
60+ num_channels = 8 ):
6061 # these one are used
62+ self .daisy = False
6163 self .high_speed = high_speed
6264 self .impedance = False
6365 self .ip_address = ip_address
6466 self .latency = latency
6567 self .log = log # print_incoming_text needs log
6668 self .max_packets_to_skip = max_packets_to_skip
69+ self .num_channles = num_channels
6770 self .sample_rate = sample_rate
6871 self .shield_name = shield_name
6972 self .ssdp_attempts = ssdp_attempts
@@ -157,8 +160,14 @@ def connect(self):
157160 gains = None
158161 if self .board_type == k .BOARD_CYTON :
159162 gains = [24 , 24 , 24 , 24 , 24 , 24 , 24 , 24 ]
163+ self .daisy = False
164+ elif self .board_type == k .BOARD_DAISY :
165+ gains = [24 , 24 , 24 , 24 , 24 , 24 , 24 , 24 , 24 , 24 , 24 , 24 , 24 , 24 , 24 , 24 ]
166+ self .daisy = True
160167 elif self .board_type == k .BOARD_GANGLION :
161168 gains = [51 , 51 , 51 , 51 ]
169+ self .daisy = False
170+ self .local_wifi_server .set_daisy (daisy = self .daisy )
162171 self .local_wifi_server .set_parser (ParseRaw (gains = gains , board_type = self .board_type ))
163172
164173 if self .high_speed :
@@ -444,7 +453,7 @@ def warn(self, text):
444453 # log how many packets where sent succesfully in between warnings
445454 if self .log_packet_count :
446455 logging .info ('Data packets received:' + str (self .log_packet_count ))
447- self .log_packet_count = 0 ;
456+ self .log_packet_count = 0
448457 logging .warning (text )
449458 print ("Warning: %s" % text )
450459
@@ -472,11 +481,14 @@ def reconnect(self):
472481
473482
474483class WiFiShieldHandler (asyncore .dispatcher_with_send ):
475- def __init__ (self , sock , callback = None , high_speed = True , parser = None ):
484+ def __init__ (self , sock , callback = None , high_speed = True ,
485+ parser = None , daisy = False ):
476486 asyncore .dispatcher_with_send .__init__ (self , sock )
477487
478- self .high_speed = high_speed
479488 self .callback = callback
489+ self .daisy = daisy
490+ self .high_speed = high_speed
491+ self .last_odd_sample = OpenBCISample ()
480492 self .parser = parser if parser is not None else ParseRaw (gains = [24 , 24 , 24 , 24 , 24 , 24 , 24 , 24 ])
481493
482494 def handle_read (self ):
@@ -490,8 +502,22 @@ def handle_read(self):
490502 samples = self .parser .transform_raw_data_packets_to_sample (raw_data_packets = raw_data_packets )
491503
492504 for sample in samples :
493- if self .callback is not None :
494- self .callback (sample )
505+ # if a daisy module is attached, wait to concatenate two samples (main board + daisy)
506+ # before passing it to callback
507+ if self .daisy :
508+ # odd sample: daisy sample, save for later
509+ if ~ sample .sample_number % 2 :
510+ self .last_odd_sample = sample
511+ # even sample: concatenate and send if last sample was the fist part, otherwise drop the packet
512+ elif sample .sample_number - 1 == self .last_odd_sample .sample_number :
513+ # the aux data will be the average between the two samples, as the channel
514+ # samples themselves have been averaged by the board
515+ daisy_sample = self .parser .make_daisy_sample_object_wifi (self .last_odd_sample , sample )
516+ if self .callback is not None :
517+ self .callback (daisy_sample )
518+ else :
519+ if self .callback is not None :
520+ self .callback (sample )
495521
496522 else :
497523 try :
@@ -516,11 +542,12 @@ def handle_read(self):
516542
517543class WiFiShieldServer (asyncore .dispatcher ):
518544
519- def __init__ (self , host , port , callback = None , gains = None , high_speed = True ):
545+ def __init__ (self , host , port , callback = None , gains = None , high_speed = True , daisy = False ):
520546 asyncore .dispatcher .__init__ (self )
521547 self .create_socket (socket .AF_INET , socket .SOCK_STREAM )
522548 self .set_reuse_addr ()
523549 self .bind ((host , port ))
550+ self .daisy = daisy
524551 self .listen (5 )
525552 self .callback = None
526553 self .handler = None
@@ -532,13 +559,19 @@ def handle_accept(self):
532559 if pair is not None :
533560 sock , addr = pair
534561 print 'Incoming connection from %s' % repr (addr )
535- self .handler = WiFiShieldHandler (sock , self .callback , high_speed = self .high_speed , parser = self .parser )
562+ self .handler = WiFiShieldHandler (sock , self .callback , high_speed = self .high_speed ,
563+ parser = self .parser , daisy = self .daisy )
536564
537565 def set_callback (self , callback ):
538566 self .callback = callback
539567 if self .handler is not None :
540568 self .handler .callback = callback
541569
570+ def set_daisy (self , daisy ):
571+ self .daisy = daisy
572+ if self .handler is not None :
573+ self .handler .daisy = daisy
574+
542575 def set_gains (self , gains ):
543576 self .parser .set_ads1299_scale_factors (gains )
544577
0 commit comments