-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.py
102 lines (78 loc) · 2.79 KB
/
main.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
import os
from signal import signal, SIGINT, SIGTERM
import sys
from time import sleep
from threading import Timer
import pygame
from pygame.locals import MOUSEBUTTONUP
from sonos import Sonos
# import project files
import ui as ui
from ui.window import Window
import ui.colors as colors
from device.backlight import Backlight
from scenes.now_playing import NowPlaying
# Handle Terminate signal, exit gracefully.
def exit_handler(sig, frame):
print ("Exiting app...")
Window.scene = None
Window.surface.fill(colors.BLACK)
pygame.display.update()
Backlight.off()
pygame.quit()
sys.exit(0)
# Register signals for when app is interrupted or terminated
signal(SIGINT, exit_handler)
signal(SIGTERM, exit_handler)
########## MAIN LOOP ###########
# Initialize UI window
ui.init()
sonos = Sonos()
# Set the scene here
now_playing = NowPlaying(sonos)
Window.scene = now_playing
BACKLIGHT_TIMEOUT = int(os.getenv('BACKLIGHT_TIMEOUT'))
backlight_timer = Timer(BACKLIGHT_TIMEOUT, Backlight.off)
backlight_timer.start()
touch_enabled = True
def enable_touch():
global touch_enabled
touch_enabled = True
def start_backlight_timer():
global backlight_timer
if not backlight_timer.is_alive():
backlight_timer = Timer(BACKLIGHT_TIMEOUT, Backlight.off)
backlight_timer.start()
def wake_up_display():
''' Turn display on and prevent accidental touch by disabling touch for 0.5 seconds '''
global touch_enabled
Backlight.on()
touch_enabled = False
timer = Timer(0.5, enable_touch)
timer.start()
##### Main Loop #####
while True:
# Scan touchscreen events
for event in pygame.event.get():
mouse_position = pygame.mouse.get_pos()
# print ""
# print ("Tap on window at: {}".format(mouse_position))
if Backlight.enabled:
if touch_enabled:
# Cancel the timer any time the screen is touched
backlight_timer.cancel()
# Process touch events
hit_view = Window.scene.hit(mouse_position)
if event.type is MOUSEBUTTONUP:
start_backlight_timer()
if hit_view is not None and hit_view is not Window.scene:
hit_view.mouse_up(event.button, mouse_position)
else:
# Turn the backlight on if the screen has been touched
wake_up_display()
# This is necessary to turn display off if
# the screen is not touched after the initial wakeup touch
start_backlight_timer()
Window.update()
# Return time to CPU to not hog resources during loop
sleep(0.02)