<s> try : <EOL> import psyco <EOL> psyco . full ( ) <EOL> except ImportError : <EOL> print '<STR_LIT>' <EOL> import pygame <EOL> import platform <EOL> from pygame import FULLSCREEN <EOL> if platform . system ( ) == "<STR_LIT>" : <EOL> import socket <EOL> import gevent <EOL> from emokit . emotiv import Emotiv <EOL> quality_color = { <EOL> "<STR_LIT:0>" : ( <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> ) , <EOL> "<STR_LIT:1>" : ( <NUM_LIT:255> , <NUM_LIT:0> , <NUM_LIT:0> ) , <EOL> "<STR_LIT:2>" : ( <NUM_LIT:255> , <NUM_LIT:0> , <NUM_LIT:0> ) , <EOL> "<STR_LIT:3>" : ( <NUM_LIT:255> , <NUM_LIT:255> , <NUM_LIT:0> ) , <EOL> "<STR_LIT:4>" : ( <NUM_LIT:255> , <NUM_LIT:255> , <NUM_LIT:0> ) , <EOL> "<STR_LIT:5>" : ( <NUM_LIT:0> , <NUM_LIT:255> , <NUM_LIT:0> ) , <EOL> "<STR_LIT>" : ( <NUM_LIT:0> , <NUM_LIT:255> , <NUM_LIT:0> ) , <EOL> "<STR_LIT>" : ( <NUM_LIT:0> , <NUM_LIT:255> , <NUM_LIT:0> ) , <EOL> } <EOL> old_quality_color = { <EOL> "<STR_LIT:0>" : ( <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> ) , <EOL> "<STR_LIT:1>" : ( <NUM_LIT:255> , <NUM_LIT:0> , <NUM_LIT:0> ) , <EOL> "<STR_LIT:2>" : ( <NUM_LIT:255> , <NUM_LIT:255> , <NUM_LIT:0> ) , <EOL> "<STR_LIT:3>" : ( <NUM_LIT:0> , <NUM_LIT:255> , <NUM_LIT:0> ) , <EOL> "<STR_LIT:4>" : ( <NUM_LIT:0> , <NUM_LIT:255> , <NUM_LIT:0> ) , <EOL> } <EOL> class Grapher ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , screen , name , i ) : <EOL> """<STR_LIT>""" <EOL> self . screen = screen <EOL> self . name = name <EOL> self . range = float ( <NUM_LIT:1> << <NUM_LIT> ) <EOL> self . x_offset = <NUM_LIT> <EOL> self . y = i * gheight <EOL> self . buffer = [ ] <EOL> font = pygame . font . Font ( None , <NUM_LIT> ) <EOL> self . text = font . render ( self . name , <NUM_LIT:1> , ( <NUM_LIT:255> , <NUM_LIT:0> , <NUM_LIT:0> ) ) <EOL> self . text_pos = self . text . get_rect ( ) <EOL> self . text_pos . centery = self . y + gheight <EOL> self . first_packet = True <EOL> self . y_offset = <NUM_LIT:0> <EOL> def update ( self , packet ) : <EOL> """<STR_LIT>""" <EOL> if len ( self . buffer ) == <NUM_LIT> - self . x_offset : <EOL> self . buffer = self . buffer [ <NUM_LIT:1> : ] <EOL> self . buffer . append ( [ packet . sensors [ self . name ] [ '<STR_LIT:value>' ] , packet . sensors [ self . name ] [ '<STR_LIT>' ] , packet . old_model ] ) <EOL> def calc_y ( self , val ) : <EOL> """<STR_LIT>""" <EOL> return val - self . y_offset + gheight <EOL> def draw ( self ) : <EOL> """<STR_LIT>""" <EOL> if len ( self . buffer ) == <NUM_LIT:0> : <EOL> return <EOL> if self . first_packet : <EOL> self . y_offset = self . buffer [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] <EOL> self . first_packet = False <EOL> pos = self . x_offset , self . calc_y ( self . buffer [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] ) + self . y <EOL> for i , ( value , quality , old_model ) in enumerate ( self . buffer ) : <EOL> y = self . calc_y ( value ) + self . y <EOL> if old_model : <EOL> color = old_quality_color [ str ( quality ) ] <EOL> else : <EOL> color = quality_color [ str ( quality ) ] <EOL> pygame . draw . line ( self . screen , color , pos , ( self . x_offset + i , y ) ) <EOL> pos = ( self . x_offset + i , y ) <EOL> self . screen . blit ( self . text , self . text_pos ) <EOL> def main ( ) : <EOL> """<STR_LIT>""" <EOL> global gheight <EOL> pygame . init ( ) <EOL> screen = pygame . display . set_mode ( ( <NUM_LIT> , <NUM_LIT> ) ) <EOL> graphers = [ ] <EOL> recordings = [ ] <EOL> recording = False <EOL> record_packets = [ ] <EOL> updated = False <EOL> cursor_x , cursor_y = <NUM_LIT> , <NUM_LIT> <EOL> for name in '<STR_LIT>' . split ( '<STR_LIT:U+0020>' ) : <EOL> graphers . append ( Grapher ( screen , name , len ( graphers ) ) ) <EOL> fullscreen = False <EOL> emotiv = Emotiv ( display_output = True ) <EOL> gevent . spawn ( emotiv . setup ) <EOL> gevent . sleep ( <NUM_LIT:0> ) <EOL> while emotiv . running : <EOL> for event in pygame . event . get ( ) : <EOL> if event . type == pygame . QUIT : <EOL> emotiv . close ( ) <EOL> return <EOL> if event . type == pygame . KEYDOWN : <EOL> if event . key == pygame . K_ESCAPE : <EOL> emotiv . close ( ) <EOL> return <EOL> elif event . key == pygame . K_f : <EOL> if fullscreen : <EOL> screen = pygame . display . set_mode ( ( <NUM_LIT> , <NUM_LIT> ) ) <EOL> fullscreen = False <EOL> else : <EOL> screen = pygame . display . set_mode ( ( <NUM_LIT> , <NUM_LIT> ) , FULLSCREEN , <NUM_LIT:16> ) <EOL> fullscreen = True <EOL> elif event . key == pygame . K_r : <EOL> if not recording : <EOL> record_packets = [ ] <EOL> recording = True <EOL> else : <EOL> recording = False <EOL> recordings . append ( list ( record_packets ) ) <EOL> record_packets = None <EOL> packets_in_queue = <NUM_LIT:0> <EOL> try : <EOL> while packets_in_queue < <NUM_LIT:8> : <EOL> packet = emotiv . dequeue ( ) <EOL> if abs ( packet . gyro_x ) > <NUM_LIT:1> : <EOL> cursor_x = max ( <NUM_LIT:0> , min ( cursor_x , <NUM_LIT> ) ) <EOL> cursor_x -= packet . gyro_x <EOL> if abs ( packet . gyro_y ) > <NUM_LIT:1> : <EOL> cursor_y += packet . gyro_y <EOL> cursor_y = max ( <NUM_LIT:0> , min ( cursor_y , <NUM_LIT> ) ) <EOL> map ( lambda x : x . update ( packet ) , graphers ) <EOL> if recording : <EOL> record_packets . append ( packet ) <EOL> updated = True <EOL> packets_in_queue += <NUM_LIT:1> <EOL> except Exception , ex : <EOL> print ex <EOL> if updated : <EOL> screen . fill ( ( <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ) ) <EOL> map ( lambda x : x . draw ( ) , graphers ) <EOL> pygame . draw . rect ( screen , ( <NUM_LIT:255> , <NUM_LIT:255> , <NUM_LIT:255> ) , ( cursor_x - <NUM_LIT:5> , cursor_y - <NUM_LIT:5> , <NUM_LIT:10> , <NUM_LIT:10> ) , <NUM_LIT:0> ) <EOL> pygame . display . flip ( ) <EOL> updated = False <EOL> gevent . sleep ( <NUM_LIT:0> ) <EOL> try : <EOL> gheight = <NUM_LIT> / <NUM_LIT> <EOL> main ( ) <EOL> except Exception , e : <EOL> print e </s>
