-
Notifications
You must be signed in to change notification settings - Fork 0
/
toy.py
executable file
·176 lines (161 loc) · 5.56 KB
/
toy.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
#!/usr/bin/env python
import pygame
from pygame.locals import *
import sys
import time
import select
import numpy
import xwiimote
import glob
from postures import *
''''
Make a copy of the image you want to show (to not change the original) and use following:
self.image = self.original_image.copy()
# this works on images with per pixel alpha too
alpha = 128
self.image.fill((255, 255, 255, alpha), None, pygame.BLEND_RGBA_MULT)
def blit_alpha(target, source, location, opacity):
x = location[0]
y = location[1]
temp = pygame.Surface((source.get_width(), source.get_height())).convert()
temp.blit(target, (-x, -y))
temp.blit(source, (0, 0))
temp.set_alpha(opacity)
target.blit(temp, location)
Replace the screen.blit(happy, (100, 100)) with a call to blit_alpha(screen, happy, (100, 100), 128)
'''''
def IsBoard(dev):
time.sleep(2) # if we check the devtype to early it is reported as 'unknown' :(
iface = xwiimote.iface(dev)
return iface.get_devtype() == 'balanceboard'
def WaitForBoard():
print("Waiting for board to connect...")
mon = xwiimote.monitor(True, False)
dev = None
while True:
print('waiting for board')
mon.get_fd(True) # blocks
connected = mon.poll()
if connected == None:
continue
elif IsBoard(connected):
print("Found board.")
dev = connected
break
else:
print("Waiting...")
return dev
def GetMeasurementsFromBoard(iface, board_p):
board_p.poll(16) # with no argument, it blocks
event = xwiimote.event()
iface.dispatch(event)
tl = event.get_abs(2)[0]
tr = event.get_abs(0)[0]
bl = event.get_abs(3)[0]
br = event.get_abs(1)[0]
return numpy.array((tl,tr,bl,br)) / 100.0
def InitBoard():
iface = xwiimote.iface(WaitForBoard())
iface.open(xwiimote.IFACE_BALANCE_BOARD)
board_p = select.epoll.fromfd(iface.get_fd())
return iface, board_p
def UpdatePoseText(last_pose_change, posture):
pose_text = []
for n,p in enumerate(posture):
color = 110 + 145 * (last_pose_change == n)
pose_text.append(font.render(p[1] + ' ' + p[3], True, (color, color, color)))
if len(posture) == 2:
pose_text.append(font.render('', True, (255, 255, 255)))
return pose_text
USE_BOARD = False
CHANGE_SOUND = 1
POSE_TIME = 4.5
iface = None
if USE_BOARD:
iface, board_p = InitBoard()
# pygame init
pygame.init()
display = (1920, 1080)
screen = pygame.display.set_mode(display, DOUBLEBUF) # |FULLSCREEN)
clock = pygame.time.Clock()
# fonts
# print(pygame.font.get_fonts())
font = pygame.font.SysFont("ubuntu", 72)
# sounds
sound_files = glob.glob('sounds/*.wav')
print(sound_files)
effects = []
for file in sound_files:
effects.append(pygame.mixer.Sound(file))
sound_count = 0
# images
glow = pygame.image.load('images/glow.png')
body = pygame.image.load('images/body_white.png')
body = pygame.transform.scale(body, (body.get_width() * 2, body.get_height() * 2))
direction_stats = []
for i in range(6):
direction_stats.append(0)
pose_count = -1 # number of poses
frames = 0
before = time.time()
pose_time = time.time()
change_sound_played = False
while True:
if (time.time() - pose_time) > POSE_TIME - 0.4:
if not change_sound_played:
effects[CHANGE_SOUND].play()
change_sound_played = True
if (time.time() - pose_time) > POSE_TIME:
pose_count = pose_count + 1
if pose_count < 3:
if pose_count == 0:
previous_posture = GenerateNewPosture(None)
pose_text = UpdatePoseText(pose_count, previous_posture)
else:
last_pose_change,previous_posture = ChangeSomething(previous_posture)
for n,p in enumerate(previous_posture):
direction_stats[previous_posture[n][2]] += 1
# print(direction_stats)
pose_text = UpdatePoseText(last_pose_change, previous_posture)
pose_time = time.time()
change_sound_played = False
if iface == None:
m = numpy.array((0, 0, 0, 0))
else:
m = GetMeasurementsFromBoard(iface, board_p)
if frames == 10:
zero = m
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
print('quit')
quit()
if event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE:
pygame.quit()
print('quit')
quit()
if event.type == pygame.KEYDOWN and event.key == pygame.K_s:
print(sound_count)
effect = effects[sound_count]
effect.play()
sound_count = sound_count + 1
screen.fill((100, 100, 100))
screen.blit(body, (1920 // 2 - body.get_width() // 2, 1080 // 2 - body.get_height() // 2))
if frames > 11:
press = m - zero
# print(press)
if numpy.sum(press) > 10.0:
x = 4.0 * ((press[1] + press[3]) - (press[0] + press[2])) / (numpy.sum(press))
y = 2.0 * ((press[0] + press[1]) - (press[2] + press[3])) / (numpy.sum(press))
screen.blit(glow, (int(x * 500 + 900), int(- y * 500 + 500)))
# print(x, y)
if pose_count > -1:
for n,text in enumerate(pose_text):
screen.blit(text, (1920 // 2 - text.get_width() // 2, 200 + 100 * n - text.get_height() // 2))
pygame.display.flip()
# pygame.time.wait(15) # milliseconds
# time.sleep(1.0 / 90.0) # seconds
clock.tick(60) # fps
frames = frames + 1
if frames % 60 == 0:
print('fps %.1f' % (float(frames) / (time.time() - before)))