-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
executable file
·65 lines (53 loc) · 1.98 KB
/
main.py
File metadata and controls
executable file
·65 lines (53 loc) · 1.98 KB
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
import signal
import sys
from functools import partial
from animations import ColorCycle, Pulse, Rainbow
from buttons import ButtonStateManager, MockButton
from models import WordClock
from example_local_config import birthdays, layout, display
def cleanup(cleanup_fn, *args):
"""
Cleanup function. Signal handlers take are passed in two args by default, hence the unused *args.
Args:
cleanup_fn (function): cleanup function to run
"""
cleanup_fn()
sys.exit(0)
def when_held(animations, button):
current_state = button.current_state
if current_state:
animation = animations[current_state - 1]
# Some animations will only be run if the button is currently pressed down. If it should be run after
# release until the button is pressed, add `continue_after_button_pressed = True` to the animation
# instance
if button.is_held or getattr(animations, 'continue_after_button_pressed', False):
animation.animate()
def add_color_button(clock, layout):
pulse = Pulse(clock, clock.displayer.pixels, speed=0.05, period=3)
pulse.run_alone = False
color_cycle = ColorCycle(clock, clock.displayer.pixels, speed=0.05)
rainbow = Rainbow(
clock, clock.displayer.pixels, speed=0.1, words=[layout.WORDS['ALL']])
button_animations = [pulse, color_cycle, rainbow]
button_manager = ButtonStateManager(
len(button_animations), on_reset=clock.reset
)
button = MockButton(
4,
button_manager,
hold_time=0.5,
tick_fn=partial(when_held, button_animations)
)
clock.add_button(button)
if __name__ == '__main__':
# Setup clock and display
wordclock = WordClock(display, layout, birthdays)
add_color_button(wordclock, layout)
signal.signal(signal.SIGTERM, partial(cleanup, wordclock.cleanup))
while True:
exception = None
try:
wordclock.tick()
except Exception:
wordclock.cleanup()
raise