<s> import sys ; sys . path . append ( '<STR_LIT:..>' ) <EOL> import open_bci_v3 as bci <EOL> import streamer_tcp_server <EOL> import time , timeit <EOL> from threading import Thread <EOL> NB_CHANNELS = <NUM_LIT:8> <EOL> SAMPLING_FACTOR = - <NUM_LIT> <EOL> SAMPLING_RATE = <NUM_LIT> <EOL> SERVER_PORT = <NUM_LIT> <EOL> SERVER_IP = "<STR_LIT:localhost>" <EOL> DEBUG = False <EOL> last_id = - <NUM_LIT:1> <EOL> nb_samples_in = - <NUM_LIT:1> <EOL> nb_samples_out = - <NUM_LIT:1> <EOL> last_values = [ <NUM_LIT:0> ] * NB_CHANNELS <EOL> leftover_duplications = <NUM_LIT:0> <EOL> tick = timeit . default_timer ( ) <EOL> class Monitor ( Thread ) : <EOL> def __init__ ( self ) : <EOL> Thread . __init__ ( self ) <EOL> self . nb_samples_in = - <NUM_LIT:1> <EOL> self . nb_samples_out = - <NUM_LIT:1> <EOL> self . tick = timeit . default_timer ( ) <EOL> self . start_tick = self . tick <EOL> def run ( self ) : <EOL> while True : <EOL> new_tick = timeit . default_timer ( ) <EOL> elapsed_time = new_tick - self . tick <EOL> current_samples_in = nb_samples_in <EOL> current_samples_out = nb_samples_out <EOL> print "<STR_LIT>" , ( new_tick - self . start_tick ) , "<STR_LIT>" <EOL> print "<STR_LIT>" , elapsed_time <EOL> print "<STR_LIT>" , current_samples_in - self . nb_samples_in <EOL> print "<STR_LIT>" , current_samples_out - self . nb_samples_out <EOL> self . tick = new_tick <EOL> self . nb_samples_in = nb_samples_in <EOL> self . nb_samples_out = nb_samples_out <EOL> server . check_connections ( ) <EOL> time . sleep ( <NUM_LIT:1> ) <EOL> def streamData ( sample ) : <EOL> global last_values <EOL> global tick <EOL> global last_id <EOL> if sample . id != last_id + <NUM_LIT:1> : <EOL> print "<STR_LIT:time>" , tick , "<STR_LIT>" <EOL> if sample . id == <NUM_LIT:255> : <EOL> last_id = - <NUM_LIT:1> <EOL> else : <EOL> last_id = sample . id <EOL> global nb_samples_in , nb_samples_out <EOL> nb_samples_in = nb_samples_in + <NUM_LIT:1> <EOL> global leftover_duplications <EOL> if ( SAMPLING_RATE > <NUM_LIT:0> ) : <EOL> now = timeit . default_timer ( ) <EOL> elapsed_time = now - tick ; <EOL> leftover_duplications = SAMPLING_RATE * elapsed_time + leftover_duplications - <NUM_LIT:1> <EOL> tick = now <EOL> elif SAMPLING_FACTOR > <NUM_LIT:0> : <EOL> leftover_duplications = SAMPLING_FACTOR + leftover_duplications - <NUM_LIT:1> <EOL> if ( leftover_duplications > <NUM_LIT:1> ) : <EOL> leftover_duplications = leftover_duplications - <NUM_LIT:1> <EOL> interpol_values = list ( last_values ) <EOL> for i in range ( <NUM_LIT:0> , len ( interpol_values ) ) : <EOL> interpol_values [ i ] = ( last_values [ i ] + sample . channel_data [ i ] ) / <NUM_LIT:2> <EOL> if DEBUG : <EOL> print "<STR_LIT>" <EOL> print "<STR_LIT>" , last_values <EOL> print "<STR_LIT>" , interpol_values <EOL> print "<STR_LIT>" , sample . channel_data <EOL> server . broadcast_values ( interpol_values ) <EOL> nb_samples_out = nb_samples_out + <NUM_LIT:1> <EOL> server . broadcast_values ( sample . channel_data ) <EOL> nb_samples_out = nb_samples_out + <NUM_LIT:1> <EOL> last_values = list ( sample . channel_data ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> server = streamer_tcp_server . StreamerTCPServer ( ip = SERVER_IP , port = SERVER_PORT , nb_channels = NB_CHANNELS ) <EOL> port = '<STR_LIT>' <EOL> baud = <NUM_LIT> <EOL> monit = Monitor ( ) <EOL> monit . daemon = True <EOL> monit . start ( ) <EOL> board = bci . OpenBCIBoard ( port = port , baud = baud , filter_data = False ) <EOL> board . startStreaming ( streamData ) </s>
