-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
255 lines (169 loc) · 6.92 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
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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from __future__ import absolute_import, division, print_function, unicode_literals
import os,time, sys
from _thread import start_new_thread
import rrdtool
import pi3d
import importlib
import core.graphics as graphics
import core.peripherals as peripherals
import config
slides = []
subslides = dict()
for slidestring in config.slides:
slides.append(importlib.import_module('slides.'+slidestring))
for slidestring in config.subslides:
subslides[slidestring] = importlib.import_module('subslides.'+slidestring)
# make 4M ramdisk for graph
os.popen('sudo mkdir /media/ramdisk')
os.popen('sudo mount -t tmpfs -o size=4M tmpfs /media/ramdisk')
#os.chdir('/media/ramdisk')
if not os.path.isfile('temperatures.rrd'):
print('create rrd')
rrdtool.create(
"temperatures.rrd",
"--step","60",
"DS:act_temp:GAUGE:120:-127:127",
"DS:gpu:GAUGE:120:-127:127",
"DS:cpu:GAUGE:120:-127:127",
"DS:atmega:GAUGE:120:-127:127",
"DS:sht:GAUGE:120:-127:127",
"DS:bmp280:GAUGE:120:-127:127",
"DS:mlxamb:GAUGE:120:-127:127",
"DS:mlxobj:GAUGE:120:-127:127",
"DS:ntc:GAUGE:120:-127:127",
"RRA:MAX:0.5:1:1500",
"RRA:MAX:0.5:10:1500",
"RRA:MAX:0.5:60:1500")
nextsensorcheck = 0
everysecond = 0
nexttm = 0
last_backlight_level = 0
a = 0
def get_files():
file_list = []
extensions = ['.png','.jpg','.jpeg']
for root, dirnames, filenames in os.walk(config.installpath + 'backgrounds'):
for filename in filenames:
ext = os.path.splitext(filename)[1].lower()
if ext in extensions and not filename.startswith('.'):
file_list.append(os.path.join(root, filename))
#random.shuffle(file_list)
return file_list, len(file_list)
iFiles, nFi = get_files()
pic_num = nFi - 1
if config.startmqttclient:
import core.mqttclient as mqttclient
try: mqttclient.init()
except: pass
if config.starthttpserver:
from http.server import BaseHTTPRequestHandler, HTTPServer #ThreadingHTTPServer for python 3.7
from core.httpserver import ServerHandler
try:
littleserver = HTTPServer(("0.0.0.0", config.HTTP_PORT), ServerHandler)
#littleserver = ThreadingHTTPServer(("0.0.0.0", 9000), ServerHandler)
littleserver.timeout = 0.1
except:
print('server error')
slide_offset = 0 # change by touch and slide
textchange = True
sfg = graphics.tex_load(iFiles[pic_num])
peripherals.get_status() #run all one for init eg_object
peripherals.get_sensors() # ''
peripherals.get_infrared() # ''
peripherals.eg_object.slide = config.slide
while graphics.DISPLAY.loop_running():
now = time.time()
if peripherals.eg_object.alert:
peripherals.alert()
elif config.subslide == 'alert':
peripherals.alert(0)
config.subslide = None
if config.startmqttclient: mqttclient.publish('alert','off')
if config.backlight_auto:
if now < peripherals.eg_object.lastmotion + config.backlight_auto:
peripherals.eg_object.backlight_level = peripherals.eg_object.max_backlight
else:
peripherals.eg_object.backlight_level = 0
if peripherals.eg_object.backlight_level != last_backlight_level:
print('set backlight:' + str(peripherals.eg_object.backlight_level))
peripherals.controlbacklight(peripherals.eg_object.backlight_level)
last_backlight_level = peripherals.eg_object.backlight_level
if (now > everysecond):
peripherals.get_infrared()
everysecond = now + config.INFRARED_TM
if (now > nextsensorcheck):
peripherals.get_sensors() #needs to be threaded
nextsensorcheck = now + config.SENSOR_TM
if config.coolingrelay and config.coolingrelay == config.heatingrelay:
peripherals.coolingheating()
else:
if config.coolingrelay: peripherals.cooling()
if config.heatingrelay: peripherals.heating()
peripherals.get_status() #needs to be threaded
if hasattr(peripherals.eg_object,'bmp280_temp'): bmp280_temp = peripherals.eg_object.bmp280_temp
else: bmp280_temp = 0
if hasattr(peripherals.eg_object,'sht_temp'): sht_temp = peripherals.eg_object.sht_temp
else: sht_temp = 0
temperatures_str = 'N:{:.2f}:{:.2f}:{:.2f}:{:.2f}:{:.2f}:{:.2f}:{:.2f}:{:.2f}:{:.2f}'.format(
peripherals.eg_object.act_temp, peripherals.eg_object.gputemp, peripherals.eg_object.cputemp, peripherals.eg_object.atmega_temp,
sht_temp, bmp280_temp, peripherals.eg_object.mlxamb, peripherals.eg_object.mlxobj,(0.0))
start_new_thread(rrdtool.update,('temperatures.rrd', temperatures_str))
print(temperatures_str)
if config.show_airquality:
redvalue = 255 if peripherals.eg_object.a4 > 600 else int(0.03 * peripherals.eg_object.a4)
greenvalue = 0 if peripherals.eg_object.a4 > 400 else int(0.02*(400 - peripherals.eg_object.a4))
peripherals.controlled([redvalue,greenvalue,0])
if config.startmqttclient: mqttclient.publishall()
textchange = True
if not config.subslide:
if now > nexttm and not peripherals.touched(): # change background
nexttm = now + config.TMDELAY
a = 0.0
sbg = sfg
sbg.positionZ(5)
pic_num = (pic_num + 1) % nFi
sfg = graphics.tex_load(iFiles[pic_num])
if a < 1.0: # fade to new background
activity = True #we calculate more frames, when there is activity, otherwise we add sleep.time at end
a += 0.01
sbg.draw()
sfg.set_alpha(a)
sfg.draw()
if peripherals.touched(): # and (peripherals.lasttouch + 0.4 > time.time()): # check if touch is pressed, to detect sliding
x,y = peripherals.get_touch()
activity = True
if ((x != 400) and peripherals.lastx): #catch 0,0 -> 400,-240
movex = (peripherals.lastx - x)
#movey = (peripherals.lasty - y)
if (abs(movex) > 50): #calculate slider movement
slide_offset = movex
else:
movex = 0
if movex < -300 and peripherals.eg_object.slide > 0 and peripherals.lasttouch < (now - 0.1): #start sliding when there is enough touchmovement
peripherals.lastx = 0
movex = 0
peripherals.eg_object.slide -= 1
sbg.set_alpha(0)
a = 0
slide_offset += 400
if movex > 300 and peripherals.eg_object.slide < len(config.slides) - 1 and peripherals.lasttouch < (now - 0.1):
peripherals.lastx = 0
movex = 0
peripherals.eg_object.slide += 1
sbg.set_alpha(0)
a = 0
slide_offset -= 400
if config.subslide != None:
activity = subslides[config.subslide].inloop(textchange,activity)
elif -1 < peripherals.eg_object.slide < len(config.slides):
activity,slide_offset = slides[peripherals.eg_object.slide].inloop(textchange,activity,slide_offset)
if (activity == False) & (slide_offset == 0) :
if config.starthttpserver:
littleserver.handle_request()
#else:
# time.sleep(0.1)
activity = False
textchange = False
graphics.DISPLAY.destroy()