-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsaver.py
77 lines (67 loc) · 2.5 KB
/
saver.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
'''Implements a screen saver/inverter for the SBCEye project
'''
import time
class Saver:
'''Saver class:
Turns the display on/off between specified times
Can also invert the display as a form of burn-in protection
modes:
'off': Screensaver disabled
'blank': Turn screen off
'invert': Invert the screen
parameters:
disp: display driver object
settings: (tuple) consisting of:
mode: (str) One of 'off', 'blank', 'invert'
start: (int) Start time, hour, 0-23
end: (int) End time, hour, 0-23
invert: (bool) Base invert state for the display
Optional, defaults to False
'''
active = False # Current state
def __init__(self, disp, settings):
self.disp = disp
(self.mode, start, end, *invert) = settings
self.invert = False
if len(invert) == 1:
self.invert = invert[0]
if self.mode != 'off':
print(f'Saver will {self.mode} display between: '\
f'{start}:00 and {end}:00')
if (start == end)\
or start not in range(0,23)\
or end not in range(0,23):
print('start/end times identical or out of range; disabling')
self.mode = 'off'
elif start < end:
self.saver_map = [False]*24
for i in range(start, end):
self.saver_map[i] = True
else:
self.saver_map = [True]*24
for i in range(end, start):
self.saver_map[i] = False
self.check()
def _apply_state(self, state):
'''Apply the desired state to the display'''
if state:
self.active = True
print('Saver activated')
if self.mode == 'invert':
self.disp.invert(not self.invert)
elif self.mode == 'blank':
self.disp.poweroff()
else:
self.active = False
print('Saver deactivated')
if self.mode == 'invert':
self.disp.invert(self.invert)
elif self.mode == 'blank':
self.disp.poweron()
def check(self):
'''Check the current state vs the time, and apply changes as
needed.'''
if self.mode != 'off':
hour = time.localtime()[3]
if self.active != self.saver_map[hour]:
self._apply_state(self.saver_map[hour])