-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlife_clock.py
executable file
·66 lines (49 loc) · 1.53 KB
/
life_clock.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
#!/usr/bin/python2
import math
import time
from collections import namedtuple
from display import Display
from color import Color
from font import FONT
from grid import Coords, Cell, Grid
ROWS = 10
COLS = 20
RADIATION = 1
FADE_STEPS = 32
def draw_clock():
grid = Grid(ROWS, COLS, RADIATION)
text = time.strftime('%H:%M')
FONT.draw_text(grid, 2, 2, text)
return grid
class Main(object):
def __init__(self):
self.grid = Grid(ROWS, COLS, RADIATION)
self.display = Display(ROWS, COLS)
self.display.begin()
def run(self):
while True:
self.show_clock()
self.apply_game_of_life()
def render(self, brightness=1):
self.grid.render_onto(self.display, brightness)
self.display.show()
def show_clock(self):
self.grid = draw_clock()
for brightness in range(1, FADE_STEPS):
self.render(brightness / float(FADE_STEPS - 1))
time.sleep(2.7)
def apply_game_of_life(self):
seen_fingerprints = set()
brightness = 1
grid_timestamp = time.clock()
while brightness >= 0:
now = time.clock()
self.render(brightness)
if (now - grid_timestamp) > 0.1:
grid_timestamp = now
seen_fingerprints.add(self.grid.fingerprint())
self.grid = self.grid.advance()
if self.grid.fingerprint() in seen_fingerprints:
brightness -= (1.0 / float(FADE_STEPS))
if __name__ == '__main__':
Main().run()