-
Notifications
You must be signed in to change notification settings - Fork 1
/
boot.py
186 lines (147 loc) · 5.66 KB
/
boot.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
# This file is executed on every boot (including wake-boot from deepsleep)
import gc;
import time;
import globs;
import debug;
try: #{
gc.collect();
print("\n\n");
print("[BOOT ] NightyNight D1 booting...");
from ubinascii import hexlify;
import machine;
import network;
globs.uid = hexlify(machine.unique_id()).decode();
globs.mac = hexlify(network.WLAN().config('mac'),':').decode();
gc.collect();
import config;
import ui;
# LED off
ui.led_off();
# Disable network for now
ui.w_sta.active(False);
ui.w_ap.active(False);
# Is button pressed?
if (not ui.p_but.value()): #{
# Pressed
print("[BOOT ] Button pressed. Debug mode?");
for i in range(8, 0, -1): #{
# Little flash
ui.led_on();
time.sleep(float(i) / 10.0); # i * 100 msec
ui.led_toggle();
time.sleep(0.1); # 100 msec
# Not Debug, continue boot as normal
if (ui.p_but.value()): break;
#}
# If still pressing, Debug mode, otherwise config mode
if (not ui.p_but.value()): #{
# DEBUG TIME!
debug.debug_mode();
else: #}{
print("[BOOT ] (Re)Configuration mode requested...");
globs.mode = globs.MODE_CONFIG;
#}
#}
# Normal boot (or config) here
# Turn the LED off
ui.led_off();
# Load Configuration
if (config): #{
print("[BOOT ] Loading configuration...");
config.config_load();
if (
config.config
and 'ssid' in config.config
and len(config.config['ssid']) > 0
and 'pass' in config.config
and len(config.config['pass']) > 0
): #{
print("[BOOT ] Existing AP ("
+ config.config['ssid']
+ ") configured, connecting...");
if (not ui.net_connect(
ssid = config.config['ssid']
,password = config.config['pass']
,timeout = config.config['timeout_net']
)): #{
# Timeout
print("[BOOT ] Timeout connecting to " + config.config['ssid']);
ui.led_flash(1);
globs.mode = globs.MODE_CONFIG;
else: #}{
# Connected
print("[BOOT ] Connected to " + config.config['ssid']);
print("[BOOT ] IP: " + ui.w_sta.ifconfig()[0]);
print("[BOOT ] Subnet: " + ui.w_sta.ifconfig()[1]);
print("[BOOT ] Gateway: " + ui.w_sta.ifconfig()[2]);
print("[BOOT ] DNS: " + ui.w_sta.ifconfig()[3]);
# If we are going to config mode, give the option to go with own
# AP instead
if (globs.mode == globs.MODE_CONFIG): #{
print("[BOOT ] Waiting for explicit AP request...");
for i in range(8, 0, -1): #{
# Little flash
ui.led_on();
for j in range(i * 2): #{
if (not ui.p_but.value()): break;
time.sleep(0.050); # (i * 2) * 50 msec
#}
ui.led_toggle();
# Requesting own AP, disable current client connection
if (not ui.p_but.value()): #{
ui.led_on();
print("[BOOT ] Own AP requested, disconnecting");
ui.w_sta.disconnect();
ui.w_sta.active(False);
time.sleep(1); # 1 sec
ui.led_toggle();
break;
#}
time.sleep(0.1); # 100 msec
#}
#}
#}
else: #}{
# No SSID set
print("[BOOT ] No SSID defined, forcing config mode...");
ui.led_flash(2);
globs.mode = globs.MODE_CONFIG;
#}
else : #}{
# No config found
print("[BOOT ] No config found, forcing config mode...");
ui.led_flash(3);
globs.mode = globs.MODE_CONFIG;
#}
# Did they choose config (or did we fail to connect)?
if (globs.mode == globs.MODE_CONFIG): #{
# Configuration mode
print("[BOOT ] Entering (re)configuration mode...");
# If we're connected, we are going into config mode on the AP we're
# connected to, so no need to create our own, otherwise create one.
if (not ui.w_sta.isconnected()): #{
if (not ui.net_ap_listen(
pre_ssid = 'NightyNight-' + globs.uid + '-'
,password = 'configure' + globs.uid
,timeout = config.config['timeout_net']
)): #{
# Timeout?!
print("[BOOT ] Timeout initialising AP!");
debug.sos(1);
else: #}{
# Listening
print("[BOOT ] Listening:");
print("[BOOT ] ESSID: " + ui.w_ap.config('essid'));
print("[BOOT ] Channel: " + str(ui.w_ap.config('channel')));
print("[BOOT ] IP: " + ui.w_ap.ifconfig()[0]);
print("[BOOT ] Subnet: " + ui.w_ap.ifconfig()[1]);
print("[BOOT ] Gateway: " + ui.w_ap.ifconfig()[2]);
print("[BOOT ] DNS: " + ui.w_ap.ifconfig()[3]);
#}
#}
#}
except KeyboardInterrupt: #}{
print("Break");
debug.debug_mode();
#}
# vim:ts=4:tw=80:sw=4:et:ai:si