alphalist = [] for (f,S) in spectrum: if f>=8 and f<=12: alphalist.append(S) alpha = np.mean(alphalist) return alpha if __name__ == "__main__": headset = Emotiv() routine = gevent.spawn(headset.setup) gevent.sleep(0) try: while True: buffin = [] for i in xrange(128): packet = headset.dequeue() buffin.append(packet.sensors['F3']['value']) #print packet.gyro_x, packet.gyro_y, packet.battery gevent.sleep(0) alpha = getAlphaPower(buffin) print alpha except KeyboardInterrupt: headset.running = False headset.close() gevent.kill(routine, KeyboardInterrupt) finally: headset.close() gevent.kill(routine) sys.exit()
def main(): """ Creates pygame window and graph drawing workers for each sensor. """ global gheight pygame.init() screen = pygame.display.set_mode((800, 600)) graphers = [] recordings = [] recording = False record_packets = [] updated = False cursor_x, cursor_y = 400, 300 for name in 'AF3 F7 F3 FC5 T7 P7 O1 O2 P8 T8 FC6 F4 F8 AF4'.split(' '): graphers.append(Grapher(screen, name, len(graphers))) fullscreen = False emotiv = Emotiv(display_output=False) # False/True routine = gevent.spawn(emotiv.setup) # add routine var gevent.sleep(0) while emotiv.running: for event in pygame.event.get(): if event.type == pygame.QUIT: emotiv.running = False gevent.kill(routine) #ME!! emotiv.close() # return break if event.type == pygame.KEYDOWN: if event.key == pygame.K_ESCAPE: emotiv.running = False gevent.kill(routine) #ME!! emotiv.close() # return break elif event.key == pygame.K_f: if fullscreen: screen = pygame.display.set_mode((800, 600)) fullscreen = False else: screen = pygame.display.set_mode((800, 600), FULLSCREEN, 16) fullscreen = True elif event.key == pygame.K_r: if not recording: record_packets = [] recording = True else: recording = False recordings.append(list(record_packets)) record_packets = None packets_in_queue = 0 try: while packets_in_queue < 8 and emotiv.running: packet = emotiv.dequeue() if abs(packet.gyro_x) > 1: cursor_x = max(0, min(cursor_x, 800)) cursor_x -= packet.gyro_x if abs(packet.gyro_y) > 1: cursor_y += packet.gyro_y cursor_y = max(0, min(cursor_y, 600)) map(lambda x: x.update(packet), graphers) if recording: record_packets.append(packet) updated = True packets_in_queue += 1 except Exception, ex: print ex if updated: screen.fill((75, 75, 75)) map(lambda x: x.draw(), graphers) pygame.draw.rect(screen, (255, 255, 255), (cursor_x - 5, cursor_y - 5, 10, 10), 0) pygame.display.flip() updated = False gevent.sleep(0)