Browse Source

actually run pico_ota on pico

Thomas Buck 1 year ago
parent
commit
fc7cd6ac92
2 changed files with 66 additions and 10 deletions
  1. 2
    0
      copy.sh
  2. 64
    10
      pico_ota.py

+ 2
- 0
copy.sh View File

@@ -4,6 +4,7 @@ if [ $# -ne 0 ] ; then
4 4
 cat << EOF | rshell
5 5
 cp config.py /pyboard
6 6
 cp pico.py /pyboard
7
+cp pico_ota.py /pyboard
7 8
 cp util.py /pyboard
8 9
 cp manager.py /pyboard
9 10
 cp mapper.py /pyboard
@@ -23,6 +24,7 @@ cat << EOF | rshell
23 24
 rm /pyboard/main.py
24 25
 cp config.py /pyboard
25 26
 cp pico.py /pyboard
27
+cp pico_ota.py /pyboard
26 28
 cp util.py /pyboard
27 29
 cp manager.py /pyboard
28 30
 cp mapper.py /pyboard

+ 64
- 10
pico_ota.py View File

@@ -61,7 +61,18 @@ class PicoOTA:
61 61
         try:
62 62
             #print("GET " + url)
63 63
             r = self.get(url)
64
-            r.close()
64
+
65
+            # explitic close on Response object not needed,
66
+            # handled internally by r.content / r.text / r.json()
67
+            # to avoid this automatic behaviour, first access r.content
68
+            # to trigger caching it in response object, then close
69
+            # socket.
70
+            tmp = r.content
71
+            if hasattr(r, "raw"):
72
+                if r.raw != None:
73
+                    r.raw.close()
74
+                    r.raw = None
75
+
65 76
             return r
66 77
         except Exception as e:
67 78
             print()
@@ -75,7 +86,7 @@ class PicoOTA:
75 86
     def get_stored_commit(self):
76 87
         current = "unknown"
77 88
         try:
78
-            f = open(os.path.join(self.update_path, self.version_file), "r")
89
+            f = open(self.update_path + "/" + self.version_file, "r")
79 90
             current = f.readline().strip()
80 91
             f.close()
81 92
         except Exception as e:
@@ -152,7 +163,7 @@ class PicoOTA:
152 163
                 print("Writing " + f["path"] + " to " + self.update_path)
153 164
 
154 165
             # overwrite existing file
155
-            fo = open(os.path.join(self.update_path, f["path"]), "w")
166
+            fo = open(self.update_path + "/" + f["path"], "w")
156 167
             fo.write(r)
157 168
             fo.close()
158 169
 
@@ -160,13 +171,13 @@ class PicoOTA:
160 171
                 if verbose:
161 172
                     print("Writing " + f["path"] + " to main.py")
162 173
 
163
-                fo = open(os.path.join(self.update_path, "main.py"), "w")
174
+                fo = open(self.update_path + "/" + "main.py", "w")
164 175
                 fo.write(r)
165 176
                 fo.close()
166 177
 
167 178
         # Write new commit id to local file
168
-        f = open(os.path.join(self.update_path, self.version_file), "w")
169
-        f.write(commit + os.linesep)
179
+        f = open(self.update_path + "/" + self.version_file, "w")
180
+        f.write(commit + "\n")
170 181
         f.close()
171 182
 
172 183
 def non_pico_ota_test(ota):
@@ -192,18 +203,58 @@ def non_pico_ota_test(ota):
192 203
         print("No update required")
193 204
 
194 205
 def pico_ota_run(ota):
206
+    import gc
207
+    #gc.collect()
208
+    #print(gc.mem_free())
209
+
210
+    i = util.getInput()
211
+    t = util.getTarget(i)
212
+
213
+    #gc.collect()
214
+    #print(gc.mem_free())
215
+
216
+    # Loading fonts and graphics takes a while.
217
+    # So show a splash screen while the user waits.
218
+    from splash import SplashScreen
219
+    splash = SplashScreen(t)
220
+    t.loop_start()
221
+    splash.draw()
222
+    t.loop_end()
223
+
224
+    #gc.collect()
225
+    #print(gc.mem_free())
226
+
227
+    print("Checking for updates")
195 228
     newer, commit = ota.check(True)
196 229
 
230
+    #gc.collect()
231
+    #print(gc.mem_free())
232
+
197 233
     if newer:
234
+        from pico import PicoText
235
+        s = PicoText(t)
236
+
237
+        s.setText("Update", "bitmap6")
238
+        s.draw(0, 0, False)
239
+
240
+        s.setText(commit, "bitmap6")
241
+        s.draw(0, 8, False)
242
+
243
+        print("Updating to:", commit)
198 244
         ota.update_to_commit(commit, True)
245
+
246
+        print("Resetting")
199 247
         machine.soft_reset()
200 248
 
201 249
     fallback = False
202 250
 
203 251
     try:
252
+        gc.collect()
253
+        print("Collected Garbage:", gc.mem_free())
254
+
255
+        print("Starting Application")
204 256
         import camp_pico
205 257
     except Exception as e:
206
-        fallback = True
207 258
         print()
208 259
         if hasattr(sys, "print_exception"):
209 260
             sys.print_exception(e)
@@ -211,6 +262,9 @@ def pico_ota_run(ota):
211 262
             print(e)
212 263
         print()
213 264
 
265
+        print("Falling back to previous")
266
+        fallback = True
267
+
214 268
     # TODO this would immediately cause another update on reboot
215 269
     # TODO set a flag to prevent updates after fallbacks?
216 270
     # TODO or better, blacklist failed commit_id!
@@ -219,7 +273,7 @@ def pico_ota_run(ota):
219 273
     #    ota.update_to_commit(previous, True)
220 274
     #    machine.soft_reset()
221 275
 
222
-if __name__ == "__main__":
276
+if True: #__name__ == "__main__":
223 277
     ota = PicoOTA("https://git.xythobuz.de", "thomas/rgb-matrix-visualizer")
224 278
 
225 279
     # stuff not needed on Pico
@@ -228,6 +282,7 @@ if __name__ == "__main__":
228 282
     ota.ignore("copy.sh")
229 283
     ota.ignore("config.py")
230 284
     ota.ignore("fonts")
285
+    ota.ignore("hardware")
231 286
     ota.ignore("images")
232 287
     ota.ignore("bdf.py")
233 288
     ota.ignore("camp_small.py")
@@ -235,9 +290,8 @@ if __name__ == "__main__":
235 290
     ota.ignore("pi.py")
236 291
     ota.ignore("test.py")
237 292
 
238
-    ota.exe("camp_pico.py")
239
-
240 293
     if not on_pico:
241 294
         non_pico_ota_test(ota)
242 295
     else:
296
+        ota.exe("pico_ota.py")
243 297
         pico_ota_run(ota)

Loading…
Cancel
Save