-
Notifications
You must be signed in to change notification settings - Fork 0
/
hippo.py
executable file
·118 lines (102 loc) · 2.93 KB
/
hippo.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from __future__ import print_function, division
import pygame
import random
import cv2
import canvas
import numpy as np
import itertools
import time
import config
import play
import multiprocessing
import arduino_client
import config
black_ = 0, 0, 0
# If no activity is detected for timeout_ second, starts random activity.
timeout_ = 10.0
timeWithoutActivity_ = 0
# OpenCV highgui
def on_mouse(event, x, y, flag, params ):
global timeWithoutActivity_
if event == 1:
timeWithoutActivity_ = 0
W = config.w_ / config.num_notes_
note = int(x / W) + 1
if y > 400:
canvas.inject_alphabet_ca3(note, do_play = True)
if note == 8:
canvas.resetAll()
cv2.setMouseCallback( canvas.winName_, on_mouse )
with open( './songs_format.txt', 'r' ) as f:
txt = f.read()
seqs_ = [ x for x in txt.split( '\n' ) if x.strip() ]
def handle_arduio_command( line, q ):
global seqs_
cmd, arg = line[:2], line[2:]
if len(line) < 2:
return
print( cmd, arg )
if cmd == '#B':
canvas.inject_alphabet_ca3(1+int(arg))
timeWithoutActivity_ = 0
elif cmd == '#P':
canvas.progressFromArduino(arg)
elif cmd == '#R':
print( 'Arduino said reset everything.' )
play.play('a1')
canvas.resetAll()
while not q.empty():
q.get()
elif cmd == '#S':
play.play_seq( seqs_[int(arg)] )
elif cmd == '#T':
play.play( arg )
else:
print( 'Uknown command: %s' % line )
def runApp(q):
global timeWithoutActivity_, timeout_
canvas.init()
t = 0
for i in itertools.count():
t0 = time.time()
canvas.update_graphs()
if i % 2 == 0:
canvas.plot_graphs()
if i % 20 == 0:
canvas.add_inset( )
k = 0.85
# img = k*config.canvas_ + (1-k)*config.backgroundImg_
img = config.canvas_
canvas.show_frame(np.uint8(img))
dt = time.time() - t0
t += dt
timeWithoutActivity_ += dt
# check for arduino input.
if not q.empty():
line = q.get()
handle_arduio_command( line, q )
## if auto is enabled then inject random stimulus.
#if timeWithoutActivity_ > timeout_:
# canvas.inject_alphabet_ca3( random.choice(config.alphabets_))
def main( args ):
config.args_ = args
# Launch the arduino client in a separate process.
q = multiprocessing.Queue()
p = multiprocessing.Process( target=arduino_client.main, args=(q,))
p.start()
runApp(q)
p.join()
if __name__ == '__main__':
import argparse
# Argument parser.
description = '''Hippocampus.'''
parser = argparse.ArgumentParser(description=description)
class Args: pass
args = Args()
parser.parse_args(namespace=args)
try:
main( args )
except KeyboardInterrupt as e:
pass