|
| 1 | +#!micropython |
| 2 | + |
| 3 | + |
| 4 | +def main(): |
| 5 | + globals().pop('main') |
| 6 | + from ucomponents import invoke, components |
| 7 | + from ucomputer import crash, get_computer_address |
| 8 | + |
| 9 | + def component(t): |
| 10 | + seq = components(t) |
| 11 | + return seq[0] if seq else None |
| 12 | + |
| 13 | + def check_bootable(filesystems, address): |
| 14 | + return address in filesystems and invoke(address, 'exists', '/init.py') |
| 15 | + |
| 16 | + eeprom = __path__ |
| 17 | + filesystems = components("filesystem") |
| 18 | + |
| 19 | + address = invoke(eeprom, 'getData').decode() |
| 20 | + if not check_bootable(filesystems, address): |
| 21 | + invoke(__path__, 'setData', b'') |
| 22 | + for address in filesystems: |
| 23 | + if check_bootable(filesystems, address): |
| 24 | + invoke(eeprom, 'setData', address.encode()) |
| 25 | + break |
| 26 | + else: |
| 27 | + crash("no bootable medium found") |
| 28 | + |
| 29 | + computer = get_computer_address() |
| 30 | + invoke(computer, 'beep', 1000, 0.2) |
| 31 | + |
| 32 | + gpu = component("gpu") |
| 33 | + monitor = component("monitor") |
| 34 | + if gpu and monitor: |
| 35 | + invoke(gpu, "bind", monitor) |
| 36 | + |
| 37 | + def load(address): |
| 38 | + handle = invoke(address, 'open', '/init.py', 'r') |
| 39 | + buffer = [] |
| 40 | + |
| 41 | + try: |
| 42 | + while True: |
| 43 | + buf = invoke(address, 'read', handle, 4096) |
| 44 | + if not buf: break |
| 45 | + buffer.append(buf) |
| 46 | + finally: |
| 47 | + invoke(address, 'close', handle) |
| 48 | + handle.dispose() |
| 49 | + |
| 50 | + content = b"".join(buffer) |
| 51 | + return content.decode() |
| 52 | + |
| 53 | + content = load(address) |
| 54 | + context = {'__name__': '__main__', '__path__': address} |
| 55 | + func = compile(content, '/init.py', "exec") |
| 56 | + exec(func, context) |
| 57 | + |
| 58 | + |
| 59 | +if __name__ == '__main__': |
| 60 | + main() |
0 commit comments