forked from prehensile/waveshare-clock
-
Notifications
You must be signed in to change notification settings - Fork 6
/
epaper.py
235 lines (185 loc) · 8.27 KB
/
epaper.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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
# Original code: https://github.com/prehensile/waveshare-clock
# Modifications: https://github.com/pskowronek/epaper-clock-and-more, Apache 2 license
import logging
import json
import os
from PIL import Image
from drawing import Drawing
from providers.airly import Airly
from providers.aqicn import Aqicn
from providers.darksky import DarkSky
from providers.openweather import OpenWeather
from providers.gmaps import GMaps
from providers.system_info import SystemInfo
class EPaper(object):
# only update once an hour within these ranges
# eval - don't try this at home :) i.e. don't expose envs to alians
DEAD_TIMES = eval(os.environ.get("DEAD_TIMES", "[]"))
# whether to display two vertical dots to separate hrs and mins
CLOCK_HOURS_MINS_SEPARATOR = os.environ.get("CLOCK_HRS_MINS_SEPARATOR", "true") == "true"
# whether to prefer AQI temperature instead of DarkSky's
PREFER_AIRLY_LOCAL_TEMP = os.environ.get("PREFER_AIRLY_LOCAL_TEMP", "false") == "true"
# warn states painted black instead of white on red canvas?
WARN_PAINTED_BLACK_ON_RED = os.environ.get("WARN_PAINTED_BLACK_ON_RED", "false") == "true"
DEVICE_TYPE = os.environ.get("EPAPER_TYPE", 'waveshare-2.7')
if DEVICE_TYPE == 'waveshare-2.7': # TODO refactor to use enums
# Display resolution for 2.7"
EPD_WIDTH = 176
EPD_HEIGHT = 264
MONO_DISPLAY = False
elif DEVICE_TYPE == 'waveshare-4.2':
# Display resolution for 4.2"
EPD_WIDTH = 400
EPD_HEIGHT = 300
MONO_DISPLAY = True
else:
raise Exception('Incorrect epaper screen type: ' + DEVICE_TYPE)
MONO_DISPLAY = os.environ.get("EPAPER_MONO", "true" if MONO_DISPLAY else "false") == "true" # one may override but must replace relevant library edpXinX.py, by default lib for 2.7 is tri-color, 4.2 is mono
FAST_REFRESH = os.environ.get("EPAPER_FAST_REFRESH", "false") == "true"
drawing = Drawing(
os.environ.get("DARK_SKY_UNITS", "si"),
int(os.environ.get("WEATHER_STORM_DISTANCE_WARN", "10")),
int(os.environ.get("AQI_WARN_LEVEL", "75")),
int(os.environ.get("FIRST_TIME_WARN_ABOVE_PERCENT", "50")),
int(os.environ.get("SECONDARY_TIME_WARN_ABOVE_PERCENT", "50"))
)
aqi = None
if os.environ.get("AIRLY_KEY"):
aqi = Airly(
os.environ.get("AIRLY_KEY"),
os.environ.get("LAT"),
os.environ.get("LON"),
int(os.environ.get("AIRLY_TTL", "20"))
)
else:
aqi = Aqicn(
os.environ.get("AQICN_KEY"),
os.environ.get("LAT"),
os.environ.get("LON"),
os.environ.get("AQICN_CITY_OR_ID"),
int(os.environ.get("AQICN_TTL", "20"))
)
weather = None
if os.environ.get("OPENWEATHER_KEY"):
weather = OpenWeather(
os.environ.get("OPENWEATHER_KEY"),
os.environ.get("LAT"),
os.environ.get("LON"),
os.environ.get("OPENWEATHER_UNITS", "metric"),
int(os.environ.get("OPENWEATHER_TTL", "15"))
)
else:
weather = DarkSky(
os.environ.get("DARKSKY_KEY"),
os.environ.get("LAT"),
os.environ.get("LON"),
os.environ.get("DARKSKY_UNITS", "si"),
int(os.environ.get("DARKSKY_TTL", "15"))
)
gmaps1 = GMaps(
os.environ.get("GOOGLE_MAPS_KEY"),
os.environ.get("LAT"),
os.environ.get("LON"),
os.environ.get("FIRST_TIME_TO_DESTINATION_LAT"),
os.environ.get("FIRST_TIME_TO_DESTINATION_LON"),
os.environ.get("GOOGLE_MAPS_UNITS", "metric"),
"primary",
int(os.environ.get("GOOGLE_MAPS_TTL", "10"))
)
gmaps2 = GMaps(
os.environ.get("GOOGLE_MAPS_KEY"),
os.environ.get("LAT"),
os.environ.get("LON"),
os.environ.get("SECOND_TIME_TO_DESTINATION_LAT"),
os.environ.get("SECOND_TIME_TO_DESTINATION_LON"),
os.environ.get("GOOGLE_MAPS_UNITS", "metric"),
"secondary",
int(os.environ.get("GOOGLE_MAPS_TTL", "10"))
)
system_info = SystemInfo()
def __init__(self, debug_mode = False):
self._debug_mode = debug_mode
if not debug_mode:
if self.DEVICE_TYPE == 'waveshare-2.7':
if self.FAST_REFRESH:
logging.info("Using experimental LUT tables!")
from epds import epd2in7b_fast_lut
self._epd = epd2in7b_fast_lut.EPD()
else:
from epds import epd2in7b
self._epd = epd2in7b.EPD()
elif self.DEVICE_TYPE == 'waveshare-4.2':
from epds import epd4in2
self._epd = epd4in2.EPD()
self._epd.init()
self._str_time = "XXXX"
def display(self, black_buf, red_buf, name):
if self._debug_mode:
debug_output = "/tmp/epaper-" + ( name.strftime("%H-%M-%S") if type(name) is not str else name )
logging.info("Debug mode - saving screen output to: " + debug_output + "* bmps")
black_buf.save(debug_output + "_bw_frame.bmp")
red_buf.save(debug_output + "_red_frame.bmp")
return
if not self.MONO_DISPLAY:
logging.info("Going to display a new tri-color image...")
self._epd.display_frame(
self._epd.get_frame_buffer(black_buf),
self._epd.get_frame_buffer(red_buf)
)
else:
logging.info("Going to display a new mono-color image...")
self._epd.display_frame(
self._epd.get_frame_buffer(black_buf)
)
def display_buffer(self, black_buf, red_buf, dt):
if self.DEVICE_TYPE == 'waveshare-2.7':
black_buf = black_buf.transpose(Image.ROTATE_90)
black_buf = black_buf.resize((self.EPD_WIDTH, self.EPD_HEIGHT), Image.LANCZOS)
red_buf = red_buf.transpose(Image.ROTATE_90)
red_buf = red_buf.resize((self.EPD_WIDTH, self.EPD_HEIGHT), Image.LANCZOS)
self.display(black_buf, red_buf, dt)
def display_shutdown(self):
black_frame, red_frame = self.drawing.draw_shutdown(self.MONO_DISPLAY)
self.display_buffer(black_frame, red_frame, 'shutdown')
def display_aqi_details(self):
black_frame, red_frame = self.drawing.draw_aqi_details(self.aqi.get())
self.display_buffer(black_frame, red_frame, 'aqi')
def display_gmaps_details(self):
black_frame, red_frame = self.drawing.draw_gmaps_details(self.gmaps1.get(), self.gmaps2.get())
self.display_buffer(black_frame, red_frame, 'gmaps')
def display_weather_details(self):
black_frame, red_frame = self.drawing.draw_weather_details(self.weather.get())
self.display_buffer(black_frame, red_frame, 'weather')
def display_system_details(self):
black_frame, red_frame = self.drawing.draw_system_details(self.system_info.get())
self.display_buffer(black_frame, red_frame, 'system')
def display_main_screen(self, dt, force = False):
time_format = "%H%M"
formatted = dt.strftime(time_format)
# set blank minutes if time's hour is within dead ranges
h = formatted[:2]
for dead_range in self.DEAD_TIMES:
if int(h) in dead_range:
formatted = "{} ".format(h)
if force or formatted != self._str_time:
weather_data = self.weather.get()
logging.info("--- weather: " + json.dumps(weather_data))
aqi_data = self.aqi.get()
logging.info("--- aqi: " + json.dumps(aqi_data))
gmaps1_data = self.gmaps1.get()
logging.info("--- gmaps1: " + json.dumps(gmaps1_data))
gmaps2_data = self.gmaps2.get()
logging.info("--- gmaps2: " + json.dumps(gmaps2_data))
black_frame, red_frame = self.drawing.draw_frame(
self.MONO_DISPLAY,
formatted,
self.CLOCK_HOURS_MINS_SEPARATOR,
weather_data,
self.PREFER_AIRLY_LOCAL_TEMP,
self.WARN_PAINTED_BLACK_ON_RED,
aqi_data,
gmaps1_data,
gmaps2_data
)
self.display_buffer(black_frame, red_frame, dt)
self._str_time = formatted