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