-
Notifications
You must be signed in to change notification settings - Fork 0
/
sensehat.py
208 lines (174 loc) · 5.83 KB
/
sensehat.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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
import pyaudio
import numpy
import audioop
import threading
from queue import Queue
from sense_hat import SenseHat
from time import sleep
import random
#declares
sense = SenseHat()
sense.low_light = True
W = (255, 255, 255)
P = (0, 0, 0)
R = (255, 0, 0)
G = (0, 255, 0)
def move_marble(audio, x, y):
new_x = x
new_y = y
# if 0 < audio < 50:
# new_x -= 1
# elif 359 > pitch > 179:
# new_x += 1
if audio < 50:
new_y += -1
elif 50 < audio < 250:
new_y += 1
elif 250 < audio < 500:
new_y += 2
x,y = check_wall(x, y, new_x, new_y)
return x,y
def check_wall(x, y, new_x, new_y):
if (new_x >= 0 and new_y >= 1) and (new_x >= 0 and new_y <= 7) and (new_x <= 7 and new_y >= 1) and (new_x <= 7 and new_y <= 7):
return new_x, new_y
# elif (new_x < 0 and new_y > 0) and (new_x < 0 and new_y < 7):
# return x + 2, new_y
# elif (new_x > 7 and new_y > 0) and (new_x > 7 and new_y < 7):
# return x - 2, new_y
elif (new_x > 0 and new_y < 1) and (new_x < 7 and new_y < 1):
return new_x, y
elif (new_x > 0 and new_y > 7) and (new_x < 7 and new_y > 7):
return new_x, y - 1
return x,y
def floor0(floorx):
startfloor = True
while True:
floory = 0
delay = 1
wait = 0
newsize = 0
if(wait == 0):
if(startfloor == True):
floorx = [0, 1, 2, 3, 4 , 5, 6, 7]
startfloor = False
else:#like snake moving
for i in range((len(floorx) - 1), 0, -1):
floorx[i] = floorx[i - 1]
print('First Length check: ', len(floorx))
#give color
for i in range((len(floorx) - 1) ,-1 ,-1):
maze[floory][floorx[i]] = G
sleep(delay)
print('Second Length check: ', len(floorx))
#clear color
for i in range((len(floorx) - 1) ,-1 ,-1):
maze[floory][floorx[i]] = P
floorx[0] -= 1 #move forward
#when respawn floor, above -1 already = now floorx(0) = [6], append and give newbie value from #like snake moving
if(newsize > 0):
floorx.append(0)
newsize -= 1
# print('before pop Length: ', len(floorx))
# floorx.pop(0)
# print('after pop Length: ', len(floorx))
print('floorx 0: ', floorx[0])
if(floorx[0] < 0):
if(len(floorx) == 0):#late respawn value for random pattern
wait = random.randint(0, 7)
else:#what is strange
floorx.pop(0)
print('Length check: ', len(floorx))
else:
wait -= 1
if(wait == 0):
floorx.append(7)
newsize = random.randint(2, 7)
sleep(delay)
def moveFood(foodx, foody, notexq, noteyq):
while True:
delay = 0.05
randomFood = False
#delayDecrease = -0.002
notex = notexq.get()
notey = noteyq.get()
#when food ate
if ((foodx == notex or foodx == notex+1) and (foody == notey)):
randomFood = True
#delay += delayDecrease
#spawn new food
if randomFood:
randomFood = False
retry = True
while retry:
foodx = random.randint(3, 7)
foody = random.randint(3, 7)
retry = False
#print('note(x,y): ',notex, ' ', notey)
#print('food(x,y): ',foodx, ' ', foody)
if(foodx <= 0):
foodx = 7
elif(foodx > 0):
foodx -= 1
maze[foody][foodx] = R
sleep(delay)
maze[foody][foodx] = P
def marble(notex, notey, notexq, noteyq):
while True:
delay = 0.05
# Read audio
raws=stream.read(1024, exception_on_overflow = False)
samples=numpy.frombuffer(raws, dtype=numpy.int16)
result = str(samples)
f.write(result)
rms = audioop.rms(samples, 2)
# Move marble
# px = notex
# py = notey
notex,notey = move_marble(rms, notex, notey)
#put note in queue
notexq.put(notex)
noteyq.put(notey)
#print graphics
sense.clear()
# maze[py][px] = W
maze[notey][notex] = W
# maze[foodPosY][foodPosX] = R
sense.set_pixels(sum(maze,[]))
sleep(delay)
# maze[py][px] = P
maze[notey][notex] = P
if __name__ == '__main__':
#initial position
notex = 1
notey = 1
#initial Food
foodx = 7
foody = random.randint(3, 7)
#initial wall
floorx = [7]
maze = [[G, G, G, G, G, G, G, G],
[P, P, P, P, P, P, P, P],
[P, P, P, P, P, P, P, P],
[P, P, P, P, P, P, P, P],
[P, P, P, P, P, P, P, P],
[P, P, P, P, P, P, P, P],
[P, P, P, P, P, P, P, P],
[P, P, P, P, P, P, P, P]]
pa = pyaudio.PyAudio()
for i in range(pa.get_device_count()):
dev = pa.get_device_info_by_index(i)
print((i,dev['name'],dev['maxInputChannels'],dev['defaultSampleRate']))
print(dev)
stream = pa.open(format = pyaudio.paInt16,channels=1,rate=44100,input_device_index=3,input=True)
numpy.set_printoptions(threshold=numpy.inf)
f = open("demofile2.txt", "a")
# count = 0
# while True and count < 1000:
notexq = Queue()
noteyq = Queue()
thread1 = threading.Thread(target=marble, args=(notex, notey, notexq, noteyq, ))
thread2 = threading.Thread(target=moveFood, args=(foodx, foody, notexq, noteyq, ))
thread3 = threading.Thread(target=floor0, args=(floorx, ))
thread1.start()
thread2.start()
thread3.start()