-
Notifications
You must be signed in to change notification settings - Fork 0
/
yap.py
executable file
·268 lines (221 loc) · 7.7 KB
/
yap.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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
#!/usr/bin/python3
#prereqs:
#pygame-1.9.4 (pip3 install pygame)
#numpy-1.15.3 (pip3 install numpy)
import pygame
import numpy as np
import time
from gtts import gTTS
import subprocess
import random
import os
import RPi.GPIO as GPIO
def clock():
return time.time()
WIDTH = 1920
HEIGHT = 1080
white = np.array([255,255,255])
black = np.array([0,0,0])
red = np.array([255,0,0])
bgColor = white;
fgColor = black;
colorChanged = False
colorChanging = False
changeStart = 0
chageTime = 1
fading = False
fadeStart = 0
fadeTime = 1
tutorialDisplayWords = ["click","attention"]
tutorialSpeakWords = ["click","the","word","when","you","hear","it","we","value","your","attention"]
#tutorialSpeakWords = ["click","attention"]
realWordPool = ["because", "results", "track", "you", "health", "instantly", "association", "discover", "love", "guarantee", "proven", "announce", "sponsored", "safety", "hurry", "select", "upgrade", "convince", "study", "find", "subliminal", "watch", "new", "effective", "save", "best", "now", "trial", "story", "free", "increase", "attention", "sex", "number one", "recommended", "try", "money", "persuade", "opportunity", "subscription", "yes", "easy", "compare", "claim", "unique", "personalized", "improvement", "disruptive", "quick", "bargain", "challenge", "influence"]
tutorialMode = True
lastSpokenWord = ""
lastSpokenIndex = -1
lastSpeakTime = clock()
timeBetweenWords = 1
displayWord = tutorialDisplayWords[0]
wordPool = tutorialSpeakWords
timeout = 600
lastInput = clock()
displayWordChance = 0.05
#flash red for 0.5 seconds
errorTime = 0.5
erroring = False
lastError = 0
pwm = None
def reset():
global bgColor, fgColor, colorChanged, colorChanging, tutorialMode
global displayWord, wordPool, lastSpokenWord, lastSpokenIndex
global displayWordChance
bgColor = white
fgColor = black
colorChanged = False
colorChanging = False
tutorialMode = True
wordPool = tutorialSpeakWords
displayWord = tutorialDisplayWords[0]
lastSpokenWord = ""
lastSpokenIndex = -1
displayWordChance = .05
def initwords():
for word in tutorialSpeakWords+realWordPool:
if not os.path.isfile(os.path.join("words",word)):
tts = gTTS(text=word,lang='en')
tts.save(os.path.join("words",word))
def speakWord(word):
global lastSpeakTime,lastSpokenWord,lastSpokenIndex
lastSpeakTime = clock()
lastSpokenWord = word
lastSpokenIndex = wordPool.index(word)
subprocess.Popen(["mpg123",os.path.join("words",word)],stdout=subprocess.DEVNULL,stderr=subprocess.DEVNULL)
def onClick():
global displayWord, fading, fadeStart, colorChanging, changeStart, wordPool
global tutorialMode, lastSpeakTime, fgColor, lastError, erroring
global displayWordChance
if tutorialMode:
if lastSpokenWord == displayWord:
#success
wordIndex = tutorialDisplayWords.index(displayWord)
nextWordIndex = wordIndex + 1
if nextWordIndex < len(tutorialDisplayWords):
displayWord = tutorialDisplayWords[nextWordIndex]
fading = True
fadeStart = clock()
else:
tutorialMode = False
colorChanging = True
fading = True
fadeStart = clock()
changeStart = clock()
wordPool = realWordPool
displayWord = random.choice(wordPool)
dispense()
else:
#penalize failure
lastSpeakTime = clock()
lastError = clock()
erroring = True
fgColor = red
else:
#not tutorial mode
if lastSpokenWord == displayWord:
#success
fading = True
fadeStart = clock()
displayWord = random.choice(wordPool)
displayWordChance *= .5
dispense()
else:
#penalize failure
lastSpeakTime = clock()
lastError = clock()
erroring = True
fgColor = red
def displayText(screen, msg):
text = pygame.font.Font('freesansbold.ttf',115)
textSurface = text.render(msg, True, fgColor)
textRect = textSurface.get_rect()
surface = pygame.Surface(textRect.size)
surface.fill(bgColor)
surface.blit(textSurface, textRect)
global fading
if fading:
u = (clock()-fadeStart)/fadeTime
if u<1:
surface.set_alpha(255*u)
else:
fading = False
textRect.center = (WIDTH/2,HEIGHT/2)
screen.blit(surface, textRect)
return textRect
def displayCursor(screen, image, pos):
screen.blit(image,pos)
def main():
initwords()
pygame.init()
GPIO.setmode(GPIO.BOARD)
GPIO.setup(3, GPIO.OUT)
global pwm
pwm = GPIO.PWM(3, 50)
pwm.start(0)
pygame.display.set_caption("May I Have Your Attention Please")
screen = pygame.display.set_mode((WIDTH,HEIGHT), pygame.DOUBLEBUF)
pygame.display.toggle_fullscreen()
running = True
textRect = None
pointerImage = pygame.image.load("hand.png")
defaultImage = pygame.image.load("point.png");
pointerHotspot = (5,0)
pygame.mouse.set_visible(False)#this is to fix stuttering on raspi
global colorChanging, colorChanged, changeStart, bgColor, fgColor
global fading, fadeStart
global lastInput
while running:
onBox = textRect is not None and textRect.collidepoint(pos)
pos = pygame.mouse.get_pos()
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
elif event.type == pygame.MOUSEMOTION:
lastInput = clock()
elif event.type == pygame.MOUSEBUTTONDOWN:
lastInput = clock()
if onBox:
onClick()
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_SPACE:
pygame.display.toggle_fullscreen()
if colorChanging:
u = (clock()-changeStart)/chageTime
if u<1:
bgColor = black*u+white*(1-u)
fgColor = white*u+black*(1-u)
else:
bgColor = black
fgColor = white
colorChanging = False
colorChanged = True
if clock()-lastSpeakTime > timeBetweenWords:
if tutorialMode:
wordIndex = (lastSpokenIndex+1) % len(wordPool) #wrap to beginning
word = wordPool[wordIndex]
speakWord(word)
else:
if random.uniform(0,1)<displayWordChance:
word = displayWord
else:
word = random.choice(wordPool)
while word == displayWord:
word = random.choice(wordPool)
speakWord(word)
if erroring and clock()-lastError > errorTime:
fgColor = black if tutorialMode else white
if not tutorialMode and clock()-lastInput > timeout:
reset()
screen.fill(bgColor)
textRect = displayText(screen, displayWord)
if onBox:
displayPos = np.subtract(pos,pointerHotspot)
displayCursor(screen, pointerImage, displayPos)
else:
displayCursor(screen, defaultImage, pos)
pygame.display.update()
pwm.stop()
GPIO.cleanup()
pygame.quit()
def setAngle(angle):
duty = angle/18+2
GPIO.output(3, True)
pwm.ChangeDutyCycle(duty)
def dispense():
GPIO.output(3, True)
setAngle(115)
time.sleep(.35)
setAngle(40)
time.sleep(.35)
GPIO.output(3, False)
pwm.ChangeDutyCycle(0)
if __name__ == "__main__":
main()