-
Notifications
You must be signed in to change notification settings - Fork 0
/
monitor.py
executable file
·125 lines (97 loc) · 3.2 KB
/
monitor.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
#!/usr/bin/python3
import time
import sys
import atexit
from screens.screens import HomeScreen, HelloScreen, NetworkScreen, StatScreen
from screens.menus import BacklightMenu
from gfxhat import touch, lcd, backlight, fonts
class ScreenCollection:
# TODO: Screens can send out a "bad thing happened" signal, causing the Collection to turn the backlight red
def __init__(self, timeout=30):
self.homescreen = HomeScreen()
self._screens = [] # Array of additionally added screens
self._active = 0
self.timeout = timeout # Timeout time in seconds
self.timed_out = False # Are we currently timed out?
self.last_interaction_time = time.time() # Time of last user interaction
for x in range(6):
touch.set_led(x, 0)
touch.on(x, self.touch_handler)
@property
def screens(self):
return [self.homescreen] + self._screens
@screens.setter
def screens(self, val):
self._screens = val
@property
def active(self):
return self._active
@active.setter
def active(self, n):
self._active = n % len(self.screens)
def update(self):
"""
Logic not related to drawing screen content
"""
# Handle timeout
now = time.time()
time_since_interaction = now - self.last_interaction_time
if (time_since_interaction >= self.timeout) and not self.timed_out:
self.timed_out = True
for screen_i in self.screens:
screen_i.timeout_handler()
elif (time_since_interaction <= self.timeout) and self.timed_out:
self.timed_out = False
for screen_i in self.screens:
screen_i.wake_handler()
def draw(self):
self.update()
self.screens[self.active].show()
def add(self, screen):
self.homescreen.bind(screen) # Bind any potential added functionality to the homescreen
self._screens.append(screen)
def touch_handler(self, ch, event):
# Update timeout
self.last_interaction_time = time.time()
# Event logic
if event != 'press':
return
if ch == 5:
self.active -= 1
if ch == 4:
self.active = 0
if ch == 3:
self.active += 1
if ch == 2:
self.screens[self.active].trigger()
if ch == 1:
self.screens[self.active].current_line -= 1
if ch == 0:
self.screens[self.active].current_line += 1
def cleanup():
backlight.set_all(0, 0, 0)
backlight.show()
lcd.clear()
lcd.show()
atexit.register(cleanup)
lcd.contrast(40)
lcd.rotation(180)
screen_collection = ScreenCollection()
# Create a stat screen
screen_collection.add(StatScreen())
# Create a network screen
screen_collection.add(NetworkScreen(
["eth0", "wg0", "wlan0", "test0", "test1", "test2", "test3", "test4", "test5", "test6"],
refresh_ticks=30
))
# Create a hello screen
screen_collection.add(HelloScreen())
# Create a settings screen
screen_collection.add(BacklightMenu())
try:
while True:
screen_collection.draw()
time.sleep(1.0 / 15)
except KeyboardInterrupt:
cleanup()
cleanup()