1
- import ugfx , badge , sys , gc
2
- import uos as os
3
- import uerrno as errno
4
- import ujson as json
5
- import network , wifi
6
- import machine , esp , time
7
- import urequests as requests
8
- import appglue
9
-
10
- wifi .init ()
11
-
12
- ugfx .clear (ugfx .BLACK )
13
- ugfx .string (20 ,25 ,"Connecting to:" ,"Roboto_BlackItalic24" ,ugfx .WHITE )
14
- ugfx .string (140 ,75 , "WiFi" ,"PermanentMarker22" ,ugfx .WHITE )
15
- ugfx .flush ()
16
-
17
- timeout = 250
18
- while not wifi .sta_if .isconnected ():
19
- time .sleep (0.1 )
20
- timeout = timeout - 1
21
- if (timeout < 1 ):
22
- ugfx .clear (ugfx .BLACK )
23
- ugfx .string (5 ,5 ,"Failure." ,"Roboto_BlackItalic24" ,ugfx .WHITE )
24
- ugfx .flush ()
25
- time .sleep (2 )
26
- appglue .start_app ("" )
27
- pass
28
-
29
- ugfx .clear (ugfx .WHITE )
1
+ import ugfx , badge , network , gc , time , urequests , appglue
2
+
3
+ # SHA2017 Badge installer
4
+ # V2 Thomas Roos
5
+ # V1 Niek Blankers
6
+
7
+ def draw_msg (msg ):
8
+ global line_number
9
+ try :
10
+ line_number
11
+ except :
12
+ line_number = 0
13
+ ugfx .clear (ugfx .WHITE )
14
+ ugfx .string (0 , 0 , 'Still Loading Anyway...' , "PermanentMarker22" , ugfx .BLACK )
15
+ ugfx .set_lut (ugfx .LUT_FASTER )
16
+ draw_msg (msg )
17
+ else :
18
+ ugfx .string (0 , 30 + (line_number * 15 ), msg , "Roboto_Regular12" , ugfx .BLACK )
19
+ ugfx .flush ()
20
+ line_number += 1
21
+
22
+ def connectWiFi ():
23
+ nw = network .WLAN (network .STA_IF )
24
+ if not nw .isconnected ():
25
+ nw .active (True )
26
+ ssid = badge .nvs_get_str ('badge' , 'wifi.ssid' , 'SHA2017-insecure' )
27
+ password = badge .nvs_get_str ('badge' , 'wifi.password' )
28
+ nw .connect (ssid , password ) if password else nw .connect (ssid )
29
+
30
+ draw_msg ("Connecting to '" + ssid + "'..." )
31
+
32
+ timeout = badge .nvs_get_u8 ('splash' , 'wifi.timeout' , 40 )
33
+ while not nw .isconnected ():
34
+ time .sleep (0.1 )
35
+ timeout = timeout - 1
36
+ if (timeout < 1 ):
37
+ draw_msg ("Timeout while connecting!" )
38
+ nw .active (True )
39
+ return False
40
+ return True
30
41
31
42
def show_description (active ):
32
43
if active :
44
+ global text
33
45
text .text (packages [options .selected_index ()]["description" ])
34
46
ugfx .flush ()
35
47
36
- def empty_options ():
37
- global options
38
- options .destroy ()
39
- options = ugfx .List (0 ,0 ,int (ugfx .width ()/ 2 ),ugfx .height ())
40
-
41
- ugfx .input_init ()
42
-
43
- window = ugfx .Container (0 , 0 , ugfx .width (), ugfx .height ())
44
-
45
- options = ugfx .List (0 ,0 ,int (ugfx .width ()/ 2 ),ugfx .height ())
46
-
47
- text = ugfx .Textbox (int (ugfx .width ()/ 2 ),0 , int (ugfx .width ()/ 2 ), ugfx .height ())
48
- text .text ("Downloading category list..." )
49
-
50
- ugfx .flush (ugfx .LUT_FULL )
51
- badge .eink_busy_wait ()
52
- ugfx .set_lut (ugfx .LUT_FASTER )
53
-
54
- packages = {} # global variable
55
-
56
- gc .collect ()
57
-
58
- f = requests .get ("https://badge.sha2017.org/eggs/categories/json" )
59
- try :
60
- categories = f .json ()
61
- finally :
62
- f .close ()
63
-
64
- gc .collect ()
65
-
66
- def show_category (active ):
67
- if active :
68
- ugfx .string_box (148 ,0 ,148 ,26 , "Hatchery" , "Roboto_BlackItalic24" , ugfx .BLACK , ugfx .justifyCenter )
69
- text .text ("Install or update eggs from the hatchery here.\n Select a category to start, or press B to return to the launcher.\n \n badge.sha2017.org" )
70
- ugfx .flush ()
71
-
72
- def list_categories ():
73
- global options
74
- global text
75
-
76
- empty_options ()
77
- text .destroy ()
78
- text = ugfx .Textbox (int (ugfx .width ()/ 2 ),26 ,int (ugfx .width ()/ 2 ),ugfx .height ()- 26 )
79
-
80
- ugfx .input_attach (ugfx .JOY_UP , show_category )
81
- ugfx .input_attach (ugfx .JOY_DOWN , show_category )
82
- ugfx .input_attach (ugfx .BTN_A , select_category )
83
- ugfx .input_attach (ugfx .BTN_B , lambda pushed : appglue .start_app ("launcher" ) if pushed else 0 )
84
- ugfx .input_attach (ugfx .BTN_START , lambda pushed : appglue .start_app ("" ) if pushed else 0 )
85
-
86
- for category in categories :
87
- options .add_item ("%s (%d) >" % (category ["name" ], category ["eggs" ]))
88
-
89
- show_category (True )
90
-
91
48
def select_category (active ):
92
49
if active :
93
50
global categories
51
+ global options
94
52
index = options .selected_index ()
95
53
category = categories [index ]["slug" ]
96
54
list_apps (category )
@@ -106,46 +64,48 @@ def list_apps(slug):
106
64
ugfx .input_attach (ugfx .BTN_B , 0 )
107
65
ugfx .input_attach (ugfx .BTN_START , 0 )
108
66
109
- empty_options ()
110
- text .destroy ()
111
- text = ugfx .Textbox (int (ugfx .width ()/ 2 ),0 , int (ugfx .width ()/ 2 ), ugfx .height ())
67
+ while options .count () > 0 :
68
+ options .remove_item (0 )
112
69
text .text ("Downloading list of eggs..." )
113
- ugfx .flush (ugfx .LUT_FULL )
114
- badge .eink_busy_wait ()
70
+ ugfx .flush ()
115
71
116
- gc .collect ()
117
- f = requests .get ("https://badge.sha2017.org/eggs/category/%s/json" % slug )
118
72
try :
73
+ f = urequests .get ("https://badge.sha2017.org/eggs/category/%s/json" % slug )
119
74
packages = f .json ()
120
75
finally :
121
76
f .close ()
122
77
123
- gc .collect ()
124
-
125
78
for package in packages :
126
79
options .add_item ("%s rev. %s" % (package ["name" ], package ["revision" ]))
127
80
128
81
ugfx .input_attach (ugfx .JOY_UP , show_description )
129
82
ugfx .input_attach (ugfx .JOY_DOWN , show_description )
130
83
ugfx .input_attach (ugfx .BTN_A , install_app )
131
- ugfx .input_attach (ugfx .BTN_B , lambda pushed : appglue . start_app ( "installer" ) if pushed else 0 )
132
- ugfx .input_attach (ugfx .BTN_START , lambda pushed : appglue .start_app ("installer" ) if pushed else 0 )
84
+ ugfx .input_attach (ugfx .BTN_B , lambda pushed : list_categories ( ) if pushed else 0 )
85
+ ugfx .input_attach (ugfx .BTN_START , lambda pushed : appglue .start_app ('' ) if pushed else 0 )
133
86
134
87
show_description (True )
135
- badge .eink_busy_wait ()
136
88
ugfx .set_lut (ugfx .LUT_FASTER )
89
+ gc .collect ()
90
+
91
+ def start_categories (pushed ):
92
+ if pushed :
93
+ list_categories ()
94
+
95
+ def start_app (pushed ):
96
+ if pushed :
97
+ global selected_app
98
+ appglue .start_app (selected_app )
137
99
138
100
def install_app (active ):
139
101
if active :
140
102
global options
141
103
global text
142
104
global packages
105
+ global selected_app
143
106
144
107
index = options .selected_index ()
145
108
146
- options .destroy ()
147
- text .destroy ()
148
-
149
109
ugfx .input_attach (ugfx .JOY_UP , 0 )
150
110
ugfx .input_attach (ugfx .JOY_DOWN , 0 )
151
111
ugfx .input_attach (ugfx .BTN_A , 0 )
@@ -158,19 +118,71 @@ def install_app(active):
158
118
ugfx .flush ()
159
119
160
120
import woezel
161
- woezel .install (packages [index ]["slug" ])
121
+ selected_app = packages [index ]["slug" ]
122
+ woezel .install (selected_app )
162
123
163
124
ugfx .clear (ugfx .WHITE )
164
125
ugfx .string (40 ,25 ,"Installed:" ,"Roboto_BlackItalic24" ,ugfx .BLACK )
165
126
ugfx .string (100 ,55 , packages [index ]["name" ],"PermanentMarker22" ,ugfx .BLACK )
166
- text = ugfx .Textbox (0 ,100 , ugfx .width (), ugfx .height ()- 100 )
167
- text .text ("Press A to start %s, or B to return to the installer" % packages [index ]["name" ])
168
-
169
- ugfx .input_attach (ugfx .BTN_A , lambda pushed : appglue .start_app (packages [index ]["slug" ]) if pushed else 0 )
170
- ugfx .input_attach (ugfx .BTN_B , lambda pushed : appglue .start_app ("installer" ))
171
- ugfx .input_attach (ugfx .BTN_START , lambda pushed : appglue .start_app ("" ))
127
+ ugfx .string (0 , 115 , "[ A: START | B: BACK ]" , "Roboto_Regular12" , ugfx .BLACK )
128
+
129
+ ugfx .input_attach (ugfx .BTN_A , start_app )
130
+ ugfx .input_attach (ugfx .BTN_B , start_categories )
131
+ ugfx .input_attach (ugfx .BTN_START , lambda pushed : appglue .start_app ("" ) if pushed else 0 )
172
132
173
133
ugfx .flush ()
134
+ gc .collect ()
135
+
136
+ def list_categories ():
137
+ global options
138
+ global text
139
+ global categories
140
+
141
+ try :
142
+ categories
143
+ except :
144
+ ugfx .input_init ()
145
+ draw_msg ('Getting categories' )
146
+ try :
147
+ f = urequests .get ("https://badge.sha2017.org/eggs/categories/json" )
148
+ categories = f .json ()
149
+ except :
150
+ draw_msg ('Failed!' )
151
+ draw_msg ('Returning to launcher :(' )
152
+ appglue .start_app ('launcher' )
153
+
154
+ f .close ()
155
+ draw_msg ('Done!' )
156
+
157
+ ugfx .input_attach (ugfx .JOY_UP , lambda pushed : ugfx .flush () if pushed else 0 )
158
+ ugfx .input_attach (ugfx .JOY_DOWN , lambda pushed : ugfx .flush () if pushed else 0 )
159
+ ugfx .input_attach (ugfx .BTN_A , select_category )
160
+ ugfx .input_attach (ugfx .BTN_B , lambda pushed : appglue .start_app ("launcher" ) if pushed else 0 )
161
+ ugfx .input_attach (ugfx .BTN_START , lambda pushed : appglue .start_app ("" ) if pushed else 0 )
174
162
163
+ ugfx .clear (ugfx .WHITE )
164
+ ugfx .flush ()
175
165
176
- list_categories ()
166
+ while options .count () > 0 :
167
+ options .remove_item (0 )
168
+ for category in categories :
169
+ options .add_item ("%s (%d) >" % (category ["name" ], category ["eggs" ]))
170
+
171
+ ugfx .string_box (148 ,0 ,148 ,26 , "Hatchery" , "Roboto_BlackItalic24" , ugfx .BLACK , ugfx .justifyCenter )
172
+ text .text ("Install or update eggs from the hatchery here\n \n \n \n " )
173
+ ugfx .line (148 , 78 , 296 , 78 , ugfx .BLACK )
174
+ ugfx .string_box (148 ,78 ,148 ,18 , " A: Open catergory" , "Roboto_Regular12" , ugfx .BLACK , ugfx .justifyLeft )
175
+ ugfx .string_box (148 ,92 ,148 ,18 , " B: Return to home" , "Roboto_Regular12" , ugfx .BLACK , ugfx .justifyLeft )
176
+ ugfx .line (148 , 110 , 296 , 110 , ugfx .BLACK )
177
+ ugfx .string_box (148 ,110 ,148 ,18 , " badge.sha2017.org" , "Roboto_Regular12" , ugfx .BLACK , ugfx .justifyLeft )
178
+ ugfx .flush (ugfx .LUT_FULL )
179
+ gc .collect ()
180
+
181
+
182
+ if not connectWiFi ():
183
+ draw_msg ('Returning to launcher :(' )
184
+ appglue .start_app ('launcher' )
185
+ else :
186
+ options = ugfx .List (0 ,0 ,int (ugfx .width ()/ 2 ),ugfx .height ())
187
+ text = ugfx .Textbox (int (ugfx .width ()/ 2 ),0 , int (ugfx .width ()/ 2 ), ugfx .height ())
188
+ list_categories ()
0 commit comments