-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathapp.py
139 lines (116 loc) · 5.76 KB
/
app.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
"""
Copyright [2019] [Mauro Riva <[email protected]> <lemariva.com>]
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
"""
import gc
import e7in5
import settings
import functions
import utime
import fonts.font16 as font16
import fonts.font8 as font8
import machine
from microWebSrv import MicroWebSrv
from widgets.forecast import ForecastWidget
from widgets.news import NewsWidget
import widgets.notes as NoteServer
DELAY_TIME = settings.update_interval
busy = machine.Pin(25, mode=machine.Pin.IN)
cs = machine.Pin(15, mode=machine.Pin.OUT)
rst = machine.Pin(26, mode=machine.Pin.OUT)
dc = machine.Pin(27, mode=machine.Pin.OUT)
sck = machine.Pin(13, mode=machine.Pin.OUT)
miso = machine.Pin(12, mode=machine.Pin.IN)
mosi = machine.Pin(14, mode=machine.Pin.OUT)
## Server
NoteServer.run()
## Display
epd = e7in5.EPD(rst, dc, busy, cs, sck, mosi, miso)
gc.collect()
def run():
epd.init_v1()
update_counter = 0
while True:
try:
epd.clear_frame()
gc.collect()
## Weather Section
weather_widget = ForecastWidget(api_key=settings.weather_api_key, location=settings.location)
weather = weather_widget.get_data()
weather_widget = None
epd.display_string_at(5, 5, weather['city']['name'], font16, e7in5.UNCOLORED)
epd.display_string_at(5, 25, "Sunrise: ", font16, e7in5.UNCOLORED)
epd.display_string_at(100, 25, functions.seconds_to_text(weather['city']['sunrise'], weather['city']['tz']), font16, e7in5.COLORED)
epd.display_string_at(5, 40, "Sunset: ", font16, e7in5.UNCOLORED)
epd.display_string_at(100, 40, functions.seconds_to_text(weather['city']['sunset'], weather['city']['tz']), font16, e7in5.COLORED)
x_move = 110
for index in weather['next']:
forecast = weather['next'][index]
#print(forecast)
epd.display_string_at(200 + x_move * index, 5, functions.seconds_to_text(forecast['dt'], weather['city']['tz']), font16, e7in5.COLORED)
epd.display_string_at(200 + x_move * index, 20, forecast['main'], font16, e7in5.UNCOLORED)
epd.display_string_at(200 + x_move * index, 35, forecast['temp'], font8, e7in5.UNCOLORED)
epd.display_string_at(200 + x_move * index, 45, forecast['temp_dt'], font8, e7in5.UNCOLORED)
epd.display_string_at(200 + x_move * index, 55, forecast['wind'], font8, e7in5.UNCOLORED)
epd.display_string_at(200 + x_move * index, 65, forecast['press'], font8, e7in5.UNCOLORED)
epd.draw_vertical_line(300 + x_move * index, 5, 65, e7in5.COLORED)
epd.draw_horizontal_line(1, 75, 640, e7in5.COLORED)
gc.collect()
## News Section
news_widget = NewsWidget(api_key=settings.news_api_key, country=settings.news_country)
news = news_widget.get_data()
news_widget = None
y_move = 30
if news['status'] == 'ok':
for index, article in enumerate(news['articles']):
epd.display_string_at(1, 80 + y_move * index,
functions.replace_unicode(article['title']), font16, e7in5.COLORED)
epd.display_string_at(5, 95 + y_move * index,
functions.replace_unicode(article['description']), font8, e7in5.UNCOLORED)
epd.draw_horizontal_line(1, 205, 640, e7in5.COLORED)
gc.collect()
## Notes Section
get_keys = NoteServer.models_btree.Note.get_keys()
note_idx = 0
y_move = 25
x_move = 320
for key in get_keys:
note = NoteServer.models_btree.Note.get_id(key)[0]
if note.archived == 0:
epd.display_string_at(1 + x_move * (note_idx > 5), 210 + y_move * (note_idx - (note_idx > 5) * 6),
functions.replace_unicode(note.timestamp), font8, e7in5.COLORED)
epd.display_string_at(1 + x_move * (note_idx > 5), 218 + y_move * (note_idx - (note_idx > 5) * 6),
functions.replace_unicode(note.content), font16, e7in5.UNCOLORED)
note_idx = note_idx + 1
get_keys = None
note = None
gc.collect()
## Display Update
if settings.show_last_update_time:
epd.display_string_at(1, 375, "updated: %d-%02d-%02d %02d:%02d:%02d" % utime.localtime()[:6], font8, e7in5.COLORED)
epd.display_frame()
gc.collect()
if settings.deep_sleep:
machine.deepsleep(DELAY_TIME)
else:
utime.sleep_ms(DELAY_TIME)
if update_counter > settings.calibration_interval: # clean the display and avoid ghosting
update_counter = 0
epd.clear_frame()
epd.display_frame()
gc.collect()
machine.reset()
update_counter = update_counter + 1
except Exception as e:
print("An exception occurred!" + str(e))
machine.reset()