-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathmain.py
More file actions
228 lines (205 loc) · 8.06 KB
/
Copy pathmain.py
File metadata and controls
228 lines (205 loc) · 8.06 KB
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
import sys, wifi, socketpool, time, os, json, microcontroller, ampule, gc
import load_settings
import digitalio, board
import adafruit_connection_manager, adafruit_requests
settings = load_settings.settings()
from load_screen import *
from check_button import *
import patches
import storage
if "apps" in os.listdir():
for file in os.listdir("apps"):
try: os.rename("apps/" + file, file)
except: pass
try: os.rmdir("apps")
except: pass
def show_logo():
readonly = "#" if storage.getmount("/").readonly else ""
pprint(readonly + "MatrixBOX(", line=0, color="yellow", hr="¨", _refresh=False, overlay=True)
#pprint("", line=0, color="brightwhite", overlay=True)
pprint(readonly + "Matrix", line=0, color="brightwhite", overlay=True)
_anim_x = display.width+1
def logo_anim_step():
global _anim_x
W = display.width
if _anim_x >= W:
#_anim_x = 0
return
x = _anim_x
saved = []
for y in (2, 4):
old = window[x, y]
saved.append(old)
if old == 7:
window[x, y] = 9
elif old == 5:
window[x, y] = 2
elif old == 2:
window[x, y] = 5
elif old == 1:
window[x, y] = 2
refresh()
for i, y in enumerate((2, 4)):
window[x, y] = saved[i]
_anim_x += 1
if _anim_x >= W:
refresh()
show_logo()
wifi.radio.tx_power = 9.0
pool = socketpool.SocketPool(wifi.radio)
socket = pool.socket()
socket.setblocking(False)
socket.setsockopt(pool.SOL_SOCKET, pool.SO_REUSEADDR, 1)
socket.bind(('', 80))
socket.listen(5)
socket_timeout = 5
macid = "matrixbox-" + "".join([hex(i) for i in wifi.radio.mac_address]).replace("0x","")[:3] # mac-id för hotspot
ssl_context = adafruit_connection_manager.get_radio_ssl_context(wifi.radio)
requests = adafruit_requests.Session(pool, ssl_context)
screensaver = time.monotonic()
@ampule.route("/exit", method="GET")
def webinterface(request):
load_settings.app_running = False
return (200, {}, """<meta http-equiv="refresh" content="0; url=../" />""")
@ampule.route("/", method="GET")
def webinterface(request):
global _anim_x
_anim_x = 0
if load_settings.app_running:
return (200, {}, str(exitbutton) + f"""<br> running app {load_settings.app_running}""")
if request.params:
if "run" in request.params: load_settings.app_running = request.params["run"]
return (200, {}, select_app())
def initialize_app():
global autostart
ampule_routes_backup = ampule.routes.copy()
palette_backup = [palette[i] for i in range(12)] # restored on exit
try:
clearscreen(lines=True)
pprint(f"Starting {load_settings.app_running}...")
os.chdir(load_settings.app_running)
time.sleep(0.5)
# apps start with a clean route table except for system routes
# (settings, filemanager, terminal etc.)
ampule.routes.clear()
import __init__
except Exception as e:
print(f"{e}")
settings["autostart"] = 0
finally:
for i, backup in enumerate(palette_backup):
palette[i] = backup
try: microcontroller.cpu.frequency = 160000000
except: pass
autostart = False
ampule.routes = ampule_routes_backup.copy()
try:
for codefile in os.listdir():
try: del sys.modules[codefile.rsplit(".", 1)[0]]
except: pass
except: pass
gc.collect() # reclaim the exited app's bitmaps so the next launch is clean
display.root_group = rf_group
os.chdir("/")
render_home_screen()
return False
def savesettings(settings):
print("Saving...")
clearscreen(True)
saved = True
try:
with open("settings.txt","w") as f:
f.write(json.dumps(settings))
except Exception as e:
# USB-connected boards are often read-only to their own code (see boot.py)
print("Read only!", e)
saved = False
clearscreen(False)
return saved
def installed_apps():
installed_apps = []
for app in os.listdir("/"):
if app == "LICENSE": continue
if not "." in app:
try:
if not "__init__.py" in os.listdir("/" + app): continue
except: continue
installed_apps.append(app)
return installed_apps if len(installed_apps) else ["No apps found"]
def show_first_app():
try: load_settings.installed_apps_list[1]
except: load_settings.installed_apps_list = installed_apps()
pprint(load_settings.installed_apps_list[0], line=-1, color="yellow", clear=True, _refresh=True)
def render_home_screen():
# Single place that draws the persistent LED screen -- other functions
# call this instead of drawing their own version.
clearscreen(lines=True)
show_logo() # clearscreen(lines=True) above also wipes the logo overlay -- redraw it
if wifi.radio.connected:
_wifi_address = f"IP: {wifi.radio.ipv4_address}" if wifi.radio.ipv4_address else "OFFLINE"
pprint(_wifi_address, line=1)
if wlan.STATUS:
# Full message is in the Settings error box; screen is too narrow for it.
pprint("Read-only filesystem", line=2, color="red")
pprint("Restart to fix", line=3, color="red")
else:
pprint("Select app:", line=2)
show_first_app()
elif wifi.radio.ap_active:
wlan.show_setup_on_led()
def next_program_in_list(run=False):
try: load_settings.installed_apps_list[1]
except: load_settings.installed_apps_list = installed_apps()
if run:
load_settings.app_running = load_settings.installed_apps_list[0]
return
print(load_settings.installed_apps_list)
load_settings.installed_apps_list.append(load_settings.installed_apps_list[0])
load_settings.installed_apps_list.pop(0)
scroll_line(load_settings.installed_apps_list[0], color="yellow")
def check_for_button_next_program():
global screensaver
b = check_if_button_pressed()
if b == 0: pass
if b == 1: next_program_in_list()
elif b == 2: next_program_in_list(run=True)
if load_settings.app_running:
load_settings.app_running = initialize_app()
screensaver = time.monotonic()
autostart = settings["autostart"]
screensaver_app = settings.get("screensaver", "starcloud")
wifi.radio.tx_power = float(settings["wifi_power"])
from web_interface import *
import wlan
wlan.connect_to_network()
while 1:
print("Entered main loop")
while not wifi.radio.connected and not wifi.radio.ap_active:
check_network_again_timer = time.monotonic()
wlan.start_hotspot()
while wifi.radio.ap_active and not wifi.radio.connected:
ampule.listen(socket)
if autostart:
print(ampule.listen(socket))
load_settings.app_running = autostart
check_for_button_next_program()
if time.monotonic() > check_network_again_timer + 10:
print("Attempting... " + str(wifi.radio.tx_power))
wifi.radio.tx_power += 1
if wifi.radio.tx_power == 21: wifi.radio.tx_power = 18
check_network_again_timer = wlan.connect_to_network(timeout=3, silent=True)
if wifi.radio.connected: wifi.radio.stop_ap()
while wifi.radio.connected or wifi.radio.ap_active:
settings["wifi_power"] = wifi.radio.tx_power
render_home_screen()
while wifi.radio.connected:# or wifi.radio.ap_active:
if autostart:
print(ampule.listen(socket))
load_settings.app_running = autostart
elif not load_settings.app_running: ampule.listen(socket)
check_for_button_next_program()
logo_anim_step()
if screensaver_app and time.monotonic() > screensaver + 60:
try: load_settings.app_running = screensaver_app
except Exception as e: pprint(e)
screensaver = time.monotonic()