-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
193 lines (147 loc) · 4.8 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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
import logging
import time
from lib.waveshare_epd import epd2in13
from PIL import Image, ImageDraw
from utils import get_date, get_time, with_interval
from draw import draw_news, draw_weather, draw_time, draw_date, font_square, white, black
from assets import background
from const import HEIGHT, WIDTH
logging.basicConfig(
filename='/home/pi/eink.log',
filemode='a',
format='%(asctime)s,%(msecs)d %(name)s %(levelname)s %(message)s',
datefmt='%H:%M:%S',
level=logging.DEBUG
)
def render_weather(epd, iteration):
epd.init(epd.lut_full_update)
epd.Clear(0x00)
canvas = Image.new('1', (HEIGHT, WIDTH), white)
canvas.paste(background, (0,0))
draw = ImageDraw.Draw(canvas)
draw.text((7,7), 'Weather', font = font_square, fill = black)
draw_weather(draw)
previous_date = draw_date(draw)
previous_time = draw_time(draw)
epd.display(epd.getbuffer(canvas))
epd.sleep()
canvas.save("1weather.jpeg")
def update():
nonlocal previous_date
nonlocal previous_time
every = with_interval(iteration)
time_now = get_time()
date_now = get_date()
updated = False
partial = True
if(every('3h')):
draw_weather(draw)
updated = True
partial = False
if(date_now != previous_date):
previous_date = draw_date(draw)
updated = True
if(time_now != previous_time):
previous_time = draw_time(draw)
updated = True
if(updated):
if(partial):
epd.init(epd.lut_partial_update)
else:
epd.init(epd.lut_full_update)
epd.display(epd.getbuffer(canvas))
epd.sleep()
return update
def render_news(epd, iteration):
epd.init(epd.lut_full_update)
epd.Clear(0x00)
canvas = Image.new('1', (HEIGHT, WIDTH), white)
canvas.paste(background, (0,0))
draw = ImageDraw.Draw(canvas)
draw.text((7,7), 'News', font = font_square, fill = black)
previous_date = draw_date(draw)
previous_time = draw_time(draw)
news = draw_news(draw, 2)
epd.display(epd.getbuffer(canvas))
epd.sleep()
def update():
nonlocal previous_date
nonlocal previous_time
nonlocal news
every = with_interval(iteration)
time_now = get_time()
date_now = get_date()
updated = False
partial = True
if(every('6h')):
news = draw_news(draw)
updated = True
partial = False
elif(every('2m')):
draw_news(draw, iteration % len(news), news)
updated = True
if(date_now != previous_date):
previous_date = draw_date(draw)
updated = True
if(time_now != previous_time):
previous_time = draw_time(draw)
updated = True
if(updated):
if(partial):
epd.init(epd.lut_partial_update)
else:
epd.init(epd.lut_full_update)
epd.display(epd.getbuffer(canvas))
epd.sleep()
return update
def main_loop(epd):
global_iteration = 0
all_screens = [{
'name': 'weather',
'iteration': 0,
'initiated': False
}, {
'name': 'news',
'iteration': 0,
'initiated': False
}]
current_index = 0
current_screen = all_screens[current_index]
renderer = None
while (True):
every = with_interval(global_iteration)
global_iteration = global_iteration + 1
current_screen['iteration'] = current_screen['iteration'] + 1
current = current_screen.get('name')
initiated = current_screen.get('initiated')
iteration = current_screen.get('iteration')
if(current == 'weather'):
if(initiated):
renderer()
else:
renderer = render_weather(epd, iteration)
current_screen['initiated'] = True
if(current == 'news'):
if(initiated):
renderer()
else:
renderer = render_news(epd, iteration)
current_screen['initiated'] = True
if(every('10m')):
current_screen['initiated'] = False
current_index = (current_index + 1) % len(all_screens)
current_screen = all_screens[current_index]
current_screen['initiated'] = False
time.sleep(60)
if __name__ == '__main__':
try:
logging.info("Starting...")
epd = epd2in13.EPD()
logging.info("Clearing Screen...")
main_loop(epd)
except IOError as e:
logging.info(e)
except KeyboardInterrupt:
logging.info("Exiting...")
epd2in13.epdconfig.module_exit()
exit()