-
Notifications
You must be signed in to change notification settings - Fork 32
/
pipresents.py
441 lines (348 loc) · 15.6 KB
/
pipresents.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
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
#! /usr/bin/env python
"""
Part of Pi Presents
Pi Presents is a presentation package, running on the Raspberry Pi, for museum exhibits, galleries, and presentations.
Copyright 2012,2013,2014,2015,2016,217,2018 Ken Thompson
See manual.pdf for instructions.
"""
import os
import sys
import copy
import traceback
from subprocess import call
import time
from Tkinter import *
import Tkinter as tk
import tkMessageBox
from pp_options import command_options
from pp_showlist import ShowList
from pp_validate import Validator
from pp_showmanager import ShowManager
from pp_resourcereader import ResourceReader
from pp_screendriver import ScreenDriver
from pp_timeofday import TimeOfDay
from pp_kbddriver import KbdDriver
from pp_controlsmanager import ControlsManager
from pp_utils import Monitor
from pp_utils import StopWatch
class PiPresents:
def __init__(self):
self.pipresents_issue="1.2"
self.pipresents_minorissue = '1.2.3f'
self.nonfull_window_width = 0.5 # proportion of width
self.nonfull_window_height= 0.6 # proportion of height
self.nonfull_window_x = 0 # position of top left corner
self.nonfull_window_y=0 # position of top left corner
StopWatch.global_enable=False
#****************************************
# Initialisation
# ***************************************
# get command line options
self.options=command_options()
# get pi presents code directory
pp_dir=sys.path[0]
self.pp_dir=pp_dir
if not os.path.exists(pp_dir+"/pipresents.py"):
tkMessageBox.showwarning("Pi Presents","Bad Application Directory")
exit()
#Initialise logging
Monitor.log_path=pp_dir
self.mon=Monitor()
self.mon.on()
# 0 - errors only
# 1 - errors and warnings
# 2 - everything
if self.options['debug']==True:
Monitor.global_enable=2
else:
Monitor.global_enable=0
# UNCOMMENT THIS TO LOG WARNINGS AND ERRORS ONLY
# Monitor.global_enable=1
self.mon.log (self, "\n\n\n\n\n*****************\nPi Presents is starting, Version:"+self.pipresents_minorissue)
self.mon.log (self, "Version: " + self.pipresents_minorissue)
self.mon.log (self," OS and separator:" + os.name +' ' + os.sep)
self.mon.log(self,"sys.path[0] - location of code: "+sys.path[0])
# self.mon.log(self,"os.getenv('HOME') - user home directory (not used): " + os.getenv('HOME'))
# self.mon.log(self,"os.path.expanduser('~') - user home directory: " + os.path.expanduser('~'))
# optional other classes used
self.ppio=None
self.tod=None
#get profile path from -p option
if self.options['profile']<>"":
self.pp_profile_path="/pp_profiles/"+self.options['profile']
else:
self.pp_profile_path = "/pp_profiles/pp_profile"
#get directory containing pp_home from the command,
if self.options['home'] =="":
home = os.path.expanduser('~')+ os.sep+"pp_home"
else:
home = self.options['home'] + os.sep+ "pp_home"
self.mon.log(self,"pp_home directory is: " + home)
#check if pp_home exists.
# try for 10 seconds to allow usb stick to automount
# fall back to pipresents/pp_home
self.pp_home=pp_dir+"/pp_home"
found=False
for i in range (1, 10):
self.mon.log(self,"Trying pp_home at: " + home + " (" + str(i)+')')
if os.path.exists(home):
found=True
self.pp_home=home
break
time.sleep (1)
if found==True:
self.mon.log(self,"Found Requested Home Directory, using pp_home at: " + home)
else:
self.mon.log(self,"FAILED to find requested home directory, using default to display error message: " + self.pp_home)
#check profile exists, if not default to error profile inside pipresents
self.pp_profile=self.pp_home+self.pp_profile_path
if os.path.exists(self.pp_profile):
self.mon.log(self,"Found Requested profile - pp_profile directory is: " + self.pp_profile)
else:
self.pp_profile=pp_dir+"/pp_home/pp_profiles/pp_profile"
self.mon.log(self,"FAILED to find requested profile, using default to display error message: pp_profile")
if self.options['verify']==True:
val =Validator()
if val.validate_profile(None,pp_dir,self.pp_home,self.pp_profile,self.pipresents_issue,False) == False:
tkMessageBox.showwarning("Pi Presents","Validation Failed")
exit()
# open the resources
self.rr=ResourceReader()
# read the file, done once for all the other classes to use.
if self.rr.read(pp_dir,self.pp_home,self.pp_profile)==False:
self.end('error','cannot find resources.cfg')
#initialise and read the showlist in the profile
self.showlist=ShowList()
self.showlist_file= self.pp_profile+ "/pp_showlist.json"
if os.path.exists(self.showlist_file):
self.showlist.open_json(self.showlist_file)
else:
self.mon.err(self,"showlist not found at "+self.showlist_file)
self.end('error','showlist not found')
# check profile and Pi Presents issues are compatible
if float(self.showlist.sissue())<>float(self.pipresents_issue):
self.mon.err(self,"Version of profile " + self.showlist.sissue() + " is not same as Pi Presents, must exit")
self.end('error','wrong version of profile')
# get the 'start' show from the showlist
index = self.showlist.index_of_show('start')
if index >=0:
self.showlist.select(index)
self.starter_show=self.showlist.selected_show()
else:
self.mon.err(self,"Show [start] not found in showlist")
self.end('error','start show not found')
# ********************
# SET UP THE GUI
# ********************
#turn off the screenblanking and saver
if self.options['noblank']==True:
call(["xset","s", "off"])
call(["xset","s", "-dpms"])
self.root=Tk()
self.title='Pi Presents - '+ self.pp_profile
self.icon_text= 'Pi Presents'
self.root.title(self.title)
self.root.iconname(self.icon_text)
self.root.config(bg='black')
# get size of the screen
self.screen_width = self.root.winfo_screenwidth()
self.screen_height = self.root.winfo_screenheight()
# set window dimensions and decorations
if self.options['fullscreen']==True:
self.root.attributes('-fullscreen', True)
os.system('unclutter &')
self.window_width=self.screen_width
self.window_height=self.screen_height
self.window_x=0
self.window_y=0
self.root.geometry("%dx%d%+d%+d" % (self.window_width,self.window_height,self.window_x,self.window_y))
self.root.attributes('-zoomed','1')
else:
self.window_width=int(self.screen_width*self.nonfull_window_width)
self.window_height=int(self.screen_height*self.nonfull_window_height)
self.window_x=self.nonfull_window_x
self.window_y=self.nonfull_window_y
self.root.geometry("%dx%d%+d%+d" % (self.window_width,self.window_height,self.window_x,self.window_y))
#canvas covers the whole window
self.canvas_height=self.screen_height
self.canvas_width=self.screen_width
# make sure focus is set.
self.root.focus_set()
#define response to main window closing.
self.root.protocol ("WM_DELETE_WINDOW", self.exit_pressed)
#setup a canvas onto which will be drawn the images or text
self.canvas = Canvas(self.root, bg='black')
self.canvas.config(height=self.canvas_height,
width=self.canvas_width,
highlightthickness=0)
# self.canvas.pack()
self.canvas.place(x=0,y=0)
self.canvas.focus_set()
# ****************************************
# INITIALISE THE INPUT DRIVERS
# ****************************************
# looks after bindings between symbolic names and internal operations
controlsmanager=ControlsManager()
if controlsmanager.read(pp_dir,self.pp_home,self.pp_profile)==False:
self.end('error','cannot find or error in controls.cfg.cfg')
else:
controlsmanager.parse_defaults()
# each driver takes a set of inputs, binds them to symboic names
# and sets up a callback which returns the symbolic name when an input event occurs/
# use keyboard driver to bind keys to symbolic names and to set up callback
kbd=KbdDriver()
if kbd.read(pp_dir,self.pp_home,self.pp_profile)==False:
self.end('error','cannot find or error in keys.cfg')
kbd.bind_keys(self.root,self.input_pressed)
self.sr=ScreenDriver()
# read the screen click area config file
if self.sr.read(pp_dir,self.pp_home,self.pp_profile)==False:
self.end('error','cannot find screen.cfg')
# create click areas on the canvas, must be polygon as outline rectangles are not filled as far as find_closest goes
reason,message = self.sr.make_click_areas(self.canvas,self.input_pressed)
if reason=='error':
self.mon.err(self,message)
self.end('error',message)
# ****************************************
# INITIALISE THE APPLICATION AND START
# ****************************************
self.shutdown_required=False
#kick off GPIO if enabled by command line option
if self.options['gpio']==True:
from pp_gpio import PPIO
# initialise the GPIO
self.ppio=PPIO()
# PPIO.gpio_enabled=False
if self.ppio.init(pp_dir,self.pp_home,self.pp_profile,self.canvas,50,self.gpio_pressed)==False:
self.end('error','gpio error')
# and start polling gpio
self.ppio.poll()
#kick off the time of day scheduler
self.tod=TimeOfDay()
self.tod.init(pp_dir,self.pp_home,self.canvas,500)
self.tod.poll()
# Create list of start shows initialise them and then run them
self.run_start_shows()
#start tkinter
self.root.mainloop( )
# *********************
# RUN START SHOWS
# ********************
def run_start_shows(self):
#start show manager
show_id=-1 #start show
self.show_manager=ShowManager(show_id,self.showlist,self.starter_show,self.root,self.canvas,self.pp_dir,self.pp_profile,self.pp_home)
#first time through so empty show register and set callback to terminate Pi Presents if all shows have ended.
self.show_manager.init(self.all_shows_ended_callback)
#parse the start shows field and start the initial shows
start_shows_text=self.starter_show['start-show']
self.show_manager.start_initial_shows(start_shows_text)
#callback from ShowManager when all shows have ended
def all_shows_ended_callback(self,reason,message,force_shutdown):
self.mon.log(self,"All shows ended, so terminate Pi Presents")
if force_shutdown==True:
self.shutdown_required=True
self.mon.log(self,"shutdown forced by profile")
self.terminate('killed')
else:
self.end(reason,message)
# *********************
# User inputs
# ********************
#gpio callback - symbol provided by gpio
def gpio_pressed(self,index,symbol,edge):
self.mon.log(self, "GPIO Pressed: "+ symbol)
self.input_pressed(symbol,edge,'gpio')
# all input events call this callback with a symbolic name.
def input_pressed(self,symbol,edge,source):
self.mon.log(self,"input received: "+symbol)
if symbol=='pp-exit':
self.exit_pressed()
elif symbol=='pp-shutdown':
self.shutdown_pressed('delay')
elif symbol=='pp-shutdownnow':
self.shutdown_pressed('now')
else:
for show in self.show_manager.shows:
show_obj=show[ShowManager.SHOW_OBJ]
if show_obj<>None:
show_obj.input_pressed(symbol,edge,source)
# **************************************
# respond to exit inputs by terminating
# **************************************
def shutdown_pressed(self, when):
if when=='delay':
self.root.after(5000,self.on_shutdown_delay)
else:
self.shutdown_required=True
self.exit_pressed()
def on_shutdown_delay(self):
if self.ppio.shutdown_pressed():
self.shutdown_required=True
self.exit_pressed()
def exit_pressed(self):
self.mon.log(self, "kill received from user")
#terminate any running shows and players
self.mon.log(self,"kill sent to shows")
self.terminate('killed')
# kill or error
def terminate(self,reason):
needs_termination=False
for show in self.show_manager.shows:
if show[ShowManager.SHOW_OBJ]<>None:
needs_termination=True
self.mon.log(self,"Sent terminate to show "+ show[ShowManager.SHOW_REF])
show[ShowManager.SHOW_OBJ].terminate(reason)
if needs_termination==False:
self.end(reason,'terminate - no termination of lower levels required')
# ******************************
# Ending Pi Presents after all the showers and players are closed
# **************************
def end(self,reason,message):
self.mon.log(self,"Pi Presents ending with message: " + reason + ' ' + message)
if reason=='error':
self.tidy_up()
self.mon.log(self, "exiting because of error")
#close logging files
self.mon.finish()
exit()
else:
self.tidy_up()
self.mon.log(self,"no error - exiting normally")
#close logging files
self.mon.finish()
if self.shutdown_required==True:
# call(['sudo', 'shutdown', '-h', '-t 5','now'])
call(['sudo', 'shutdown', '-h', 'now'])
exit()
else:
exit()
# tidy up all the peripheral bits of Pi Presents
def tidy_up(self):
#turn screen blanking back on
if self.options['noblank']==True:
call(["xset","s", "on"])
call(["xset","s", "+dpms"])
# tidy up gpio
if self.options['gpio']==True and self.ppio<>None:
self.ppio.terminate()
#tidy up time of day scheduler
if self.tod<>None:
self.tod.terminate()
# *****************************
# utilitities
# ****************************
def resource(self,section,item):
value=self.rr.get(section,item)
if value==False:
self.mon.err(self, "resource: "+section +': '+ item + " not found" )
self.terminate("error")
else:
return value
if __name__ == '__main__':
pp = PiPresents()
#try:
#pp = PiPresents()
#except:
# traceback.print_exc(file=open("/home/pi/pp_exceptions.log","w"))
#pass