-
Notifications
You must be signed in to change notification settings - Fork 0
/
hud.py
executable file
·370 lines (340 loc) · 10.2 KB
/
hud.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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
#!/usr/bin/python
from time import gmtime, strftime, sleep, mktime
import subprocess
import gmail
import datetime
import os
import sys
import RPi.GPIO as GPIO
import curses
# Set up the curses window basics
stdscr = curses.initscr() # Init the screen
curses.noecho() # Do not echo out keys pressed
curses.cbreak() # Capture keys without requiring the ENTER key
stdscr.keypad(1) # Capture cursor keys
# Create a new curses window instance
begin_x = 0
begin_y = 0
height = 10
width = 40
win = curses.newwin(height, width, begin_y, begin_x)
def cursesWrite(y, x, txt, color):
try:
win.addstr(y, x, txt, color)
except curses.error:
pass
def updateHead(txt):
cursesWrite(0, 1, ' ', 1)
cursesWrite(0, 1, txt, curses.COLOR_RED)
win.refresh()
def updateStat(txt):
clearStat()
cursesWrite(1, 1, txt, curses.A_REVERSE)
win.refresh()
def updateNotifications(txt):
clearNotifications()
x = 36 - len(txt)
cursesWrite(1, x, txt, curses.A_REVERSE)
win.refresh()
def updateBody(txtArr):
x = 2
for txt in txtArr:
cursesWrite(x, 0, ' ', 1)
cursesWrite(x, 1, txt, 1)
x += 1
win.refresh()
def updateBodyList(txtArr):
x = 0
newArr = []
for txt in txtArr:
if txt != '':
y = x + 3
cursesWrite(y, 0, ' ', 1)
txt = str(x + 1) + '. ' + txt
cursesWrite(y, 1, txt, 1)
x += 1
win.refresh()
def clearStat():
cursesWrite(1, 0, ' ', 1)
cursesWrite(1, 1, '', 1)
win.refresh()
def clearNotifications():
cursesWrite(1, 20, ' ', 1)
cursesWrite(1, 20, '', 1)
win.refresh()
headerText = 'Press button | "menu" for commands'
updateHead(headerText)
cursesWrite(2, 1, '----------------------------------', 1)
g = 0
pid = 99999999
fileList = []
def logEvent(txt):
f = open('log.txt', 'a')
timestamp = strftime("%Y-%m-%d %H:%M:%S", gmtime())
f.write(timestamp + ' - ' + txt + "\n")
f.close()
def voiceCommand():
global pid
headerText = 'Listening...'
p = subprocess.Popen(["./speech.sh", ""], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
out, err = p.communicate()
out = out.strip(' \t\n\r')
logEvent('VOICE - ' + out)
updateHead('You said: '+out)
updateStat('')
if (out == 'exit') or (out == "leave") or (out == "please") or (out == "lease") or (out == "leaf"):
updateStat('exit')
clearStat()
logEvent('SYSTEM - Shutting down video preview at pid ' + str(pid))
os.kill(pid, 9)
return 'exit'
elif out == 'update software':
#command = './update.sh'
#logEvent('EXEC - ' + command)
#logEvent('SYSTEM - Updating software from Github ')
#p = subprocess.Popen([command, ""], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
#out, err = p.communicate()
#out = out.strip(' \t\n\r')
headerText = 'PMGG System software updated!'
updateHead(headerText)
elif (out == 'take a photo') or (out == 'take a picture'):
headerText = 'Smile while I take your picture'
updateHead(headerText)
command = './image.sh'
logEvent('EXEC - ' + command)
logEvent('SYSTEM - Shutting down video preview at pid ' + str(pid))
os.kill(pid, 9)
p = subprocess.Popen([command, ""], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
sleep(1)
pid = preview()
updateStat('*CLICK*')
out, err = p.communicate()
out = out.strip(' \t\n\r')
sleep(1)
clearStat()
elif out == 'list photos':
fileDir = './photos/'
fileList = [ f for f in os.listdir(fileDir) if os.path.isfile(os.path.join(fileDir,f)) ]
logEvent('Listing photos')
headerText = 'Available photos (say "view photo"):'
updateHead(headerText)
updateBodyList(fileList)
return fileList
elif (out == 'view photo') or (out == 'view photos'):
sleep(1)
headerText = 'Number of the photo to view?'
updateHead(headerText)
updateStat('talk')
photoViewPrompt()
clearStat()
elif (out == 'list video') or (out == 'list videos') or (out == 'videos'):
fileDir = './videos/'
fileList = [ f for f in os.listdir(fileDir) if os.path.isfile(os.path.join(fileDir,f)) ]
logEvent('Listing videos')
headerText = 'Available videos (say "play video"):'
updateHead(headerText)
updateBodyList(fileList)
return fileList
elif out == 'play video':
sleep(1)
headerText = 'Number of the video to play?'
updateHead(headerText)
updateStat('talk')
videoPlayPrompt()
clearStat()
elif out == 'capture video':
sleep(1)
headerText = 'Capturing 5 seconds of video'
updateHead(headerText)
command = './video.sh'
logEvent('EXEC - ' + command)
logEvent('SYSTEM - Shutting down video preview at pid ' + str(pid))
os.kill(pid, 9)
p = subprocess.Popen([command, ""], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
out, err = p.communicate()
out = out.strip(' \t\n\r')
sleep(1)
pid = preview()
elif out == 'menu':
sleep(1)
txt = [
'Commands:',
'menu: This menu',
'update software: Updates system software',
'tweet: Send a tweet',
'check email: Retrieve new emails',
'take a photo: Takes a photo',
'capture video: Captures 5 seconds of video',
'list videos: Lists captured videos,'
'leave: Exits the HUD'
]
updateBody(txt)
elif out == 'check email':
sleep(1)
headerText = 'One moment while I fetch new email'
updateHead(headerText)
getMail()
elif out == 'tweet':
sleep(1)
headerText = 'Go ahead... (10 seconds)'
updateHead(headerText)
updateStat('talk')
tweetMsg()
clearStat()
return headerText
def tweetMsg():
p = subprocess.Popen(["./speech-long.sh", ""], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
out, err = p.communicate()
out = out.strip(' \t\n\r')
sleep(1)
headerText = 'I heard ' + out + '. Confirm by saying Send.'
def preview():
sleep(1)
logEvent('SYSTEM - Starting video preview overlay')
try:
return os.spawnlp(os.P_NOWAIT, 'raspivid', 'raspivid', '-t', '9999999999999', '-op', '125', '-f')
except Exception, e:
logEvent('FAILURE - ' + str(e))
def photoViewPrompt():
global fileList
global pid
p = subprocess.Popen(["./speech.sh", ""], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
out, err = p.communicate()
out = out.strip(' \t\n\r')
sleep(1)
logEvent('Viewing photo ' + out)
try:
pic = int(out)
pic = pic - 1
command = ['fbi', '-autodown', '-t', '3', '-1', './photos/' + fileList[pic]]
logEvent('EXEC - fbi -autodown -t 3 -1 ' + command[5])
logEvent('SYSTEM - Shutting down video preview at pid ' + str(pid))
os.kill(pid, 9)
p = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
sleep(8)
pid = preview()
except Exception, e:
logEvent('FAILURE - ' + str(e) + ' - ' + out)
headerText = 'Sorry. Try again'
updateHead(headerText)
def videoPlayPrompt():
global fileList
global pid
p = subprocess.Popen(["./speech.sh", ""], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
out, err = p.communicate()
out = out.strip(' \t\n\r')
sleep(1)
logEvent('Playing video ' + out)
try:
vid = int(out)
vid = vid - 1
command = ['omxplayer', './videos/' + fileList[vid]]
logEvent('EXEC - omxplayer ' + command[1])
logEvent('SYSTEM - Shutting down video preview at pid ' + str(pid))
os.kill(pid, 9)
p = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
sleep(8)
pid = preview()
except Exception, e:
logEvent('FAILURE - ' + str(e) + ' - ' + out)
headerText = 'Sorry. Try again'
updateHead(headerText)
def initEmail():
logEvent('SYSTEM - Initializing email')
f = open('email_creds.txt', 'r')
line = f.read()
creds = line.split('|')
f.close()
# g = gmail.login(creds[0], creds[1])
return gmail.login('pifacetest', 'SuperSecure!')
def getMail():
global g
logEvent('SYSTEM - Fetching email')
mail = g.inbox().mail(unread=True)
inbox_txt = ''
for msg in mail:
msg.fetch()
inbox_txt += msg.fr + msg.subject + "||JOE||"
emailList = inbox_txt.split("||JOE||")
if len(emailList) > 0:
updateNotifications('New Mail')
updateBodyList(emailList)
def checkForEmail(now):
global g
logEvent('SYSTEM - Checking for email')
#mail = g.inbox().mail(unread=True, before=now)
mail = g.inbox().mail(unread=True)
inbox_txt = ''
for msg in mail:
msg.fetch()
inbox_txt += msg.fr + msg.subject + "||JOE||"
emailList = inbox_txt.split("||JOE||")
if len(emailList) > 0:
updateNotifications('New Mail')
# Set up button listener
GPIO.setmode(GPIO.BCM)
GPIO.setup(17, GPIO.IN)
# set up some fun animations
spinnerArrow = [
' |',
'> |',
'-> |',
'--> |',
'---> |',
'----> |',
' ----)|',
' ---(-|',
' --(--|',
' -(---|',
' <----|',
'<---- |',
'<---- |',
'---- |',
'--- |',
'-- |',
'- |',
' |']
spinnerThrob = [
'.',
'o',
'0',
'o']
spinnerStar = [
'-',
'\\',
'|',
'+',
'/',
'+']
def spinner(spinnerChars):
for ch in spinnerChars:
win.move(0,0)
win.addstr(ch)
win.refresh()
sleep(.3)
# Initialize Gmail
g = initEmail()
# Begin video overlay
pid = preview()
# Begin main loop
exit = 'nope'
counter = 0
now = strftime("%Y-%m-%d %H:%M:%S", gmtime())
while exit != 'exit':
buttonStatus = GPIO.input(17)
if buttonStatus == False:
updateStat('talk')
logEvent('Button pressed')
exit = voiceCommand()
if isinstance(exit, (list, tuple)):
fileList = exit
if counter > 600:
counter = 0
now = strftime("%Y-%m-%d %H:%M:%S", gmtime())
checkForEmail(now)
counter += 1
sleep(0.1)
curses.nocbreak()
stdscr.keypad(0)
curses.echo()
curses.endwin()