1
- import ugfx , time , badge , machine , uos , appglue , deepsleep
1
+ import ugfx , time , badge , machine , uos , appglue , deepsleep , network
2
2
3
3
# SHA2017 badge home screen
4
4
# Renze Nicolai 2017
@@ -9,20 +9,20 @@ def setup_services():
9
9
try :
10
10
apps = uos .listdir ('lib' )
11
11
except OSError :
12
- print ("No lib folder!" )
12
+ print ("[SPLASH] Can't setup services: no lib folder!" )
13
13
return False
14
14
for app in apps :
15
15
try :
16
16
files = uos .listdir ('lib/' + app )
17
17
except OSError :
18
- print ("Listing app files for '" + app + "' failed!" )
18
+ print ("[SPLASH] Listing app files for app '" + app + "' failed!" )
19
19
return False
20
20
status = True
21
21
found = False
22
22
for f in files :
23
23
if (f == "service.py" ):
24
24
found = True
25
- print ("Running service " + app + "..." )
25
+ print ("[SPLASH] Running service " + app + "..." )
26
26
try :
27
27
srv = __import__ ('lib/' + app + '/service' )
28
28
services .append (srv ) #Add to global list
@@ -32,7 +32,7 @@ def setup_services():
32
32
status = False
33
33
break
34
34
if not found :
35
- print ("App '" + app + "' has no service" )
35
+ print ("[SPLASH] App '" + app + "' has no service" )
36
36
return status
37
37
38
38
def loop_services (loopCnt ):
@@ -43,7 +43,7 @@ def loop_services(loopCnt):
43
43
if (srv .loop (loopCnt )):
44
44
noSleep = True
45
45
except BaseException as msg :
46
- print ("Service loop exception: " , msg )
46
+ print ("[SPLASH] Service loop exception: " , msg )
47
47
return noSleep
48
48
49
49
# RTC
@@ -64,6 +64,10 @@ def clockstring():
64
64
minstr = "0" + minstr
65
65
return daystr + "-" + monthstr + "-" + str (year )+ " " + hourstr + ":" + minstr
66
66
67
+ def disableWifi ():
68
+ nw = network .WLAN (network .STA_IF )
69
+ nw .active (False )
70
+
67
71
def getTimeNTP ():
68
72
import ntp
69
73
import wifi
@@ -75,35 +79,43 @@ def getTimeNTP():
75
79
wifi .init ()
76
80
ssid = badge .nvs_get_str ('badge' , 'wifi.ssid' , 'SHA2017-insecure' )
77
81
draw_msg ("Configuring clock..." , "Connecting to '" + ssid + "'..." )
78
- timeout = 20
82
+ timeout = 30
79
83
while not wifi .sta_if .isconnected ():
80
84
time .sleep (0.1 )
81
85
timeout = timeout - 1
82
86
if (timeout < 1 ):
83
87
draw_msg ("Error" , "Timeout while connecting!" )
88
+ disableWifi ()
84
89
time .sleep (1 )
85
90
return False
86
91
else :
87
- print ("Time to failure: " + str (timeout ))
88
92
pass
89
93
draw_msg ("Configuring clock..." , "NTP..." )
90
94
ntp .set_NTP_time ()
91
95
draw_msg ("Configuring clock..." , "Done!" )
96
+ disableWifi ()
92
97
return True
93
- else :
94
- print ("RTC already set." )
98
+ # else:
99
+ # print("[SPLASH] RTC already set.")
95
100
return True
96
101
97
102
# BATTERY
98
103
99
- def battery_percent (vempty , vfull , vbatt ):
104
+ def battery_percent_internal (vempty , vfull , vbatt ):
100
105
percent = round (((vbatt - vempty )* 100 )/ (vfull - vempty ))
101
106
if (percent < 0 ):
102
107
percent = 0
103
108
if (percent > 100 ):
104
109
percent = 100
105
110
return percent
106
111
112
+ def battery_percent ():
113
+ vbatt = 0
114
+ for i in range (0 ,10 ):
115
+ vbatt = vbatt + badge .battery_volt_sense ()
116
+ percent = battery_percent_internal (3800 , 4300 , round (vbatt / 10 ))
117
+ return percent
118
+
107
119
# GRAPHICS
108
120
109
121
def draw_msg (title , desc ):
@@ -129,38 +141,78 @@ def draw_logo(x,y,h):
129
141
ugfx .line (x + 10 + len , y + 27 , x + 10 + len , y + 45 , ugfx .BLACK )
130
142
ugfx .string (x + 10 , y + 50 ,"Anyway" ,"Roboto_BlackItalic24" ,ugfx .BLACK )
131
143
132
- def draw_home (percent , cstate , status ):
144
+ def draw_helper_clear (full ):
145
+ if full :
146
+ ugfx .clear (ugfx .BLACK )
147
+ ugfx .flush ()
133
148
ugfx .clear (ugfx .WHITE )
134
- if (cstate ):
135
- ugfx .string (0 , 0 , str (percent )+ "% & Charging... | " + status ,"Roboto_Regular12" ,ugfx .BLACK )
149
+ if full :
150
+ ugfx .flush ()
151
+
152
+ def draw_helper_battery (percent ,cstate ):
153
+ ugfx .area (2 ,2 ,40 ,18 ,ugfx .WHITE )
154
+ ugfx .box (42 ,7 ,2 ,8 ,ugfx .WHITE )
155
+ if (percent > 0 ):
156
+ if (cstate ):
157
+ ugfx .string (5 ,5 ,"chrg" ,"Roboto_Regular12" ,ugfx .BLACK )
158
+ else :
159
+ if (percent > 10 ):
160
+ w = round ((percent * 38 )/ 100 )
161
+ ugfx .area (3 ,3 ,w ,16 ,ugfx .BLACK )
162
+ else :
163
+ ugfx .string (5 ,5 ,"empty" ,"Roboto_Regular12" ,ugfx .BLACK )
136
164
else :
137
- ugfx .string (0 , 0 , str (percent )+ "% | " + status ,"Roboto_Regular12" ,ugfx .BLACK )
165
+ ugfx .string (2 ,5 ,"no batt" ,"Roboto_Regular12" ,ugfx .BLACK )
166
+
167
+ def draw_helper_header (text ):
168
+ ugfx .area (0 ,0 ,ugfx .width (),23 ,ugfx .BLACK )
169
+ ugfx .string (45 , 1 , text ,"DejaVuSans20" ,ugfx .WHITE )
138
170
139
- ugfx .string (0 , 14 , clockstring (), "Roboto_Regular12" ,ugfx .BLACK )
171
+ def draw_helper_footer (text_l , text_r ):
172
+ ugfx .string (0 , ugfx .height ()- 13 , text_l , "Roboto_Regular12" ,ugfx .BLACK )
173
+ l = ugfx .get_string_width (text_r ,"Roboto_Regular12" )
174
+ ugfx .string (ugfx .width ()- l , ugfx .height ()- 13 , text_r , "Roboto_Regular12" ,ugfx .BLACK )
140
175
176
+ def draw_helper_nick (default ):
177
+ nick = badge .nvs_get_str ("owner" , "name" , default )
178
+ ugfx .string (0 , 40 , nick , "PermanentMarker36" , ugfx .BLACK )
141
179
htext = badge .nvs_get_str ("owner" , "htext" , "" )
142
180
if (htext != "" ):
143
181
draw_logo (160 , 25 , htext )
144
182
145
- nick = badge .nvs_get_str ("owner" , "name" , "Anonymous" )
146
- ugfx .string (0 , 40 , nick , "PermanentMarker36" , ugfx .BLACK )
147
-
148
- ugfx .set_lut (ugfx .LUT_FULL )
183
+ def draw_helper_flush (full ):
184
+ if (full ):
185
+ ugfx .set_lut (ugfx .LUT_FULL )
149
186
ugfx .flush ()
150
187
ugfx .set_lut (ugfx .LUT_FASTER )
151
188
189
+ def draw_home (percent , cstate , status , full_clear , going_to_sleep ):
190
+ draw_helper_clear (full_clear )
191
+ if (cstate ):
192
+ draw_helper_header (status )
193
+ else :
194
+ draw_helper_header (status )
195
+ draw_helper_battery (percent , cstate )
196
+ if (going_to_sleep ):
197
+ info = "[ ANY: Wake up ]"
198
+ else :
199
+ info = "[ START: LAUNCHER ]"
200
+ draw_helper_footer (clockstring (),info )
201
+ draw_helper_nick (":(" )
202
+ draw_helper_flush (True )
203
+
152
204
def draw_batterylow (percent ):
153
- ugfx . clear ( ugfx . WHITE )
154
- ugfx . string ( 0 , 0 , str ( percent ) + "% - Battery empty. Please charge me!" , "Roboto_Regular12" , ugfx . BLACK )
155
- nick = badge . nvs_get_str ( "owner" , "name" , ":( Zzzz..." )
156
- ugfx . string ( 0 , 40 , nick , "PermanentMarker36" , ugfx . BLACK )
157
- ugfx . set_lut ( ugfx . LUT_FASTER )
158
- ugfx . flush ( )
205
+ draw_helper_clear ( True )
206
+ draw_helper_header ( "" )
207
+ draw_helper_battery ( percent , False )
208
+ draw_helper_footer ( "BATTERY LOW, CONNECT CHARGER!" , "" )
209
+ draw_helper_nick ( ":( Zzzz..." )
210
+ draw_helper_flush ( False )
159
211
160
212
# START LAUNCHER
161
213
162
214
def start_launcher (pushed ):
163
- print ("START LAUNCHER: " , pushed )
215
+ # print("START LAUNCHER: ", pushed)
164
216
if (pushed ):
165
217
global splashTimer
166
218
splashTimer .deinit ()
@@ -169,31 +221,34 @@ def start_launcher(pushed):
169
221
# SLEEP
170
222
171
223
def badge_sleep ():
172
- print ("Going to sleep now..." )
224
+ print ("[SPLASH] Going to sleep now..." )
173
225
badge .eink_busy_wait () #Always wait for e-ink
174
226
deepsleep .start_sleeping (30000 ) #Sleep for 30 seconds
175
227
228
+ def badge_sleep_forever ():
229
+ print ("[SPLASH] Going to sleep WITHOUT TIME WAKEUP now..." )
230
+ badge .eink_busy_wait () #Always wait for e-ink
231
+ deepsleep .start_sleeping (0 ) #Sleep until button interrupt occurs
232
+
176
233
# TIMER
177
234
178
235
def splashTimer_callback (tmr ):
179
236
global loopCnt
180
237
if loopCnt > 9 :
181
238
loopCnt = 0
182
239
cstate = badge .battery_charge_status ()
240
+ percent = battery_percent ()
183
241
vbatt = badge .battery_volt_sense ()
184
- percent = battery_percent (3800 , 4300 , vbatt )
185
- if (cstate ) or (percent > 95 ) or (percent < 1 ):
186
- if (percent == 0 ):
187
- draw_home (percent , cstate , "Huh?! You removed the battery?" )
188
- else :
189
- draw_home (percent , cstate , "Press start to open the launcher!" )
242
+ if (cstate ) or (percent > 95 ) or (vbatt < 100 ):
243
+ draw_home (percent , cstate , "" , False , False )
190
244
else :
191
245
if (percent < 10 ):
192
246
draw_batterylow (percent )
193
247
ugfx .flush ()
248
+ badge_sleep_forever ()
194
249
else :
195
- draw_home (percent , cstate , "Zzz..." )
196
- badge_sleep ()
250
+ draw_home (percent , cstate , "Zzz..." , True , True )
251
+ badge_sleep ()
197
252
else :
198
253
if (loop_services (loopCnt )):
199
254
loopCnt = 0
@@ -211,44 +266,40 @@ def restart_sleep_counter():
211
266
stop_sleep_counter ()
212
267
start_sleep_counter ()
213
268
214
- # WELCOME
269
+ # WELCOME (SETUP, SPONSORS OR CLOCK)
215
270
216
271
def welcome ():
217
- setupcompleted = badge .nvs_get_str ('badge' , 'setup.state' , 'first' )
218
- if (setupcompleted == 'first' ):
219
- #First boot!
220
- draw_msg ("Welcome to SHA2017!" , "Hello and welcome to your new badge!" )
221
- time .sleep (1 )
222
- draw_msg ("Welcome to SHA2017!" , "Please configure your nickname in the settings." )
223
- time .sleep (1 )
224
- badge .nvs_set_str ('badge' , 'setup.state' , 'done' )
272
+ setupcompleted = int (badge .nvs_get_str ('badge' , 'setup.state' , '0' ))
273
+ if (setupcompleted == 0 ): # First boot (open setup)
274
+ print ("[SPLASH] Setup not completed. Running setup!" )
275
+ appglue .start_app ("setup" )
276
+ elif (setupcompleted == 1 ): # Second boot (after setup)
277
+ print ("[SPLASH] Showing sponsors once..." )
278
+ badge .nvs_set_str ('badge' , 'setup.state' , '2' ) # Only force show sponsors once
279
+ appglue .start_app ("sponsors" )
280
+ else : # Setup completed
281
+ print ("[SPLASH] Normal boot." )
282
+ getTimeNTP () # Set clock if needed
225
283
226
284
# MAIN
227
285
228
286
def splash_main ():
229
287
cstate = badge .battery_charge_status ()
288
+ percent = battery_percent ()
230
289
vbatt = badge .battery_volt_sense ()
231
- percent = battery_percent ( 3800 , 4300 , vbatt )
290
+ print ( "[SPLASH] Vbatt = " + str ( vbatt ) )
232
291
ugfx .init ()
233
- if (cstate ) or (percent > 9 ):
292
+ if (cstate ) or (percent > 9 ) or ( vbatt < 100 ) :
234
293
ugfx .input_init ()
235
294
welcome ()
236
- getTimeNTP ()
237
- draw_home (percent , cstate , "Press start to open launcher!" )
295
+ draw_home (percent , cstate , "" , True , False )
238
296
ugfx .input_attach (ugfx .BTN_START , start_launcher )
239
297
global splashTimer
240
298
setup_services ()
241
299
start_sleep_counter ()
242
- elif (percent == 0 ):
243
- ugfx .clear (ugfx .WHITE )
244
- ugfx .string (0 , 0 , "Recovery mode" , "PermanentMarker22" , ugfx .BLACK )
245
- ugfx .string (0 , 25 , "No battery. Dropping to shell..." , "Roboto_Regular12" , ugfx .BLACK )
246
- ugfx .set_lut (ugfx .LUT_FASTER )
247
- ugfx .flush ()
248
-
249
300
else :
250
301
draw_batterylow (percent )
251
- badge_sleep ()
302
+ badge_sleep_forever ()
252
303
253
304
#Global variables
254
305
splashTimer = machine .Timer (0 )
0 commit comments