|
| 1 | +from mpos.apps import Activity |
| 2 | + |
| 3 | +class Doom(Activity): |
| 4 | + |
| 5 | + romdir = "/roms" |
| 6 | + doomdir = romdir + "/doom" |
| 7 | + retrogodir = "/retro-go" |
| 8 | + configdir = retrogodir + "/config" |
| 9 | + bootfile = configdir + "/boot.json" |
| 10 | + #partition_label = "prboom-go" |
| 11 | + partition_label = "retro-core" |
| 12 | + # Widgets: |
| 13 | + status_label = None |
| 14 | + |
| 15 | + def onCreate(self): |
| 16 | + screen = lv.obj() |
| 17 | + self.status_label = lv.label(screen) |
| 18 | + self.status_label.set_width(lv.pct(90)) |
| 19 | + self.status_label.set_text(f'Looking for .wad or .zip files in {self.doomdir} on internal storage and SD card...') |
| 20 | + self.status_label.set_long_mode(lv.label.LONG_MODE.WRAP) |
| 21 | + self.status_label.center() |
| 22 | + self.setContentView(screen) |
| 23 | + |
| 24 | + def onResume(self, screen): |
| 25 | + self.start_wad(self.doomdir + '/Doom v1.9 Free Shareware.zip') |
| 26 | + |
| 27 | + def mkdir(self, dirname): |
| 28 | + # Would be better to only create it if it doesn't exist |
| 29 | + try: |
| 30 | + import os |
| 31 | + os.mkdir(dirname) |
| 32 | + except Exception as e: |
| 33 | + self.status_label.set_text(f"Info: could not create directory {dirname} because: {e}") |
| 34 | + |
| 35 | + def start_wad(self, wadfile): |
| 36 | + self.mkdir(self.romdir) |
| 37 | + self.mkdir(self.doomdir) |
| 38 | + self.mkdir(self.retrogodir) |
| 39 | + self.mkdir(self.configdir) |
| 40 | + try: |
| 41 | + import os |
| 42 | + import json |
| 43 | + # Would be better to only write this if it differs from what's already there: |
| 44 | + fd = open(self.bootfile, 'w') |
| 45 | + ''' |
| 46 | + bootconfig = { |
| 47 | + "BootName": "doom", |
| 48 | + "BootArgs": f"/sd{wadfile}", |
| 49 | + "BootSlot": -1, |
| 50 | + "BootFlags": 0 |
| 51 | + } |
| 52 | + ''' |
| 53 | + bootconfig = { |
| 54 | + "BootName": "nes", |
| 55 | + "BootArgs": "/sd/roms/nes/homebrew/Yun.zip", |
| 56 | + "BootSlot": -1, |
| 57 | + "BootFlags": 0 |
| 58 | + } |
| 59 | + json.dump(bootconfig, fd) |
| 60 | + fd.close() |
| 61 | + except Exception as e: |
| 62 | + self.status_label.set_text(f"ERROR: could not write config file: {e}") |
| 63 | + return |
| 64 | + results = [] |
| 65 | + try: |
| 66 | + from esp32 import Partition |
| 67 | + results = Partition.find(label=self.partition_label) |
| 68 | + except Exception as e: |
| 69 | + self.status_label.set_text(f"ERROR: could not search for internal partition with label {self.partition_label}, unable to start: {e}") |
| 70 | + return |
| 71 | + if len(results) < 1: |
| 72 | + self.status_label.set_text(f"ERROR: could not find internal partition with label {self.partition_label}, unable to start") |
| 73 | + return |
| 74 | + partition = results[0] |
| 75 | + try: |
| 76 | + partition.set_boot() |
| 77 | + except Exception as e: |
| 78 | + print(f"ERROR: could not set partition {partition} as boot, it probably doesn't contain a valid program: {e}") |
| 79 | + try: |
| 80 | + import vfs |
| 81 | + vfs.umount('/') |
| 82 | + except Exception as e: |
| 83 | + print(f"Warning: could not unmount internal filesystem from /: {e}") |
| 84 | + try: |
| 85 | + import machine |
| 86 | + machine.reset() |
| 87 | + except Exception as e: |
| 88 | + print(f"Warning: could not restart machine: {e}") |
0 commit comments