Skip to content
This repository has been archived by the owner on Apr 25, 2021. It is now read-only.

Commit

Permalink
Update OpenPython OS: #24
Browse files Browse the repository at this point in the history
  • Loading branch information
EcmaXp committed Nov 3, 2018
1 parent be19940 commit 69912e4
Show file tree
Hide file tree
Showing 39 changed files with 1,162 additions and 98 deletions.
1 change: 1 addition & 0 deletions src/main/resources/assets/openpython/opos/.prop
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{label="OpenPython OS", reboot=true, setlabel=true, setboot=true}
16 changes: 0 additions & 16 deletions src/main/resources/assets/openpython/opos/boot/01_basic.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,7 @@
import machine

import event
import value

buf = []


@machine.hook_stdin
def input_handler():
while not buf:
event.wait(10)

return int(buf.pop(0))


@event.register("key_down")
def handle_key_down(_0, _1, char, *_):
buf.append(char)


event.setup()
value.setup()
3 changes: 3 additions & 0 deletions src/main/resources/assets/openpython/opos/boot/03_value.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import value

value.setup()
20 changes: 20 additions & 0 deletions src/main/resources/assets/openpython/opos/boot/04_builtin.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,26 @@
import builtins

import event
import machine
import sys

last_input = []
buf = []


def check_key_down(name, *_):
return name == "key_down"


@machine.hook_stdin
def input_handler():
while True:
signal = event.pull_filtered(1, check_key_down)
if signal is not None:
_name, _address, char, code, _user = signal
if char:
return int(char)


# noinspection PyShadowingBuiltins
def input(prompt=None):
Expand Down
7 changes: 5 additions & 2 deletions src/main/resources/assets/openpython/opos/init.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,15 +51,18 @@ def print_handler(string):
def init():
uos.mount(FileSystem(__path__), '/')
sys.path.append('/lib')
sys.path.append('/lib/internal')
sys.path.append('/lib/openos')
sys.path.append('/usr/lib')
sys.path.append('/lib/micropython')

for filename in sorted(uos.listdir("/boot")):
context = {'__name__': '__main__', '__path__': __path__}
# noinspection PyUnresolvedReferences
execfile("/boot/" + filename, context)

from shell import spawn
spawn("/bin/python.py")
# from shell import spawn
# spawn("/bin/python.py")


if __name__ == "__main__":
Expand Down
2 changes: 0 additions & 2 deletions src/main/resources/assets/openpython/opos/lib/computer.py

This file was deleted.

69 changes: 0 additions & 69 deletions src/main/resources/assets/openpython/opos/lib/event.py

This file was deleted.

Empty file.
44 changes: 44 additions & 0 deletions src/main/resources/assets/openpython/opos/lib/openos/colors.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# https://github.com/MightyPirates/OpenComputers/blob/master-MC1.12/
# - src/main/resources/assets/opencomputers/loot/openos/lib/colors.lua

from micropython import const

__all__ = ["WHITE", "ORANGE", "MAGENTA", "LIGHTBLUE",
"YELLOW", "LIME", "PINK", "GRAY",
"SILVER", "CYAN", "PURPLE", "BLUE",
"BROWN", "GREEN", "RED", "BLACK"]

WHITE = const(0)
ORANGE = const(1)
MAGENTA = const(2)
LIGHTBLUE = const(3)
YELLOW = const(4)
LIME = const(5)
PINK = const(6)
GRAY = const(7)
SILVER = const(8)
CYAN = const(9)
PURPLE = const(10)
BLUE = const(11)
BROWN = const(12)
GREEN = const(13)
RED = const(14)
BLACK = const(15)

# alias
white = WHITE
orange = ORANGE
magenta = MAGENTA
lightblue = LIGHTBLUE
yellow = YELLOW
lime = LIME
pink = PINK
gray = GRAY
silver = SILVER
cyan = CYAN
purple = PURPLE
blue = BLUE
brown = BROWN
green = GREEN
red = RED
black = BLACK
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
from ucomponent import invoke, get_methods, get_doc, get_list, get_type, get_slot
from ucomponent import invoke, invokes, get_methods, get_doc, get_list, get_type, get_slot

import event
import sys

_list = list

__all__ = ['Component', 'is_available', 'get_primary', 'get_primary_checked', 'set_primary']
__all__ = ['Component', 'is_available', 'get_primary', 'get_primary_checked', 'set_primary', 'guard']

primaries = {}

Expand All @@ -19,6 +20,15 @@ def __init__(self, component, name):
def __call__(self, *args):
return invoke(self.component.address, self.name, *args)

def call(self, *args):
return self(*args) # alias

def _call(self, *args):
return invokes(self.component.address, self.name, *args)

def _guard_call(self, count, *args):
return guard(self._call(*args), count)

@property
def __doc__(self):
return doc(self.component.address, self.name)
Expand Down Expand Up @@ -128,14 +138,21 @@ def set_primary(component_type: str, address: str):
primaries[component_type] = proxy(address)


@event.register("component_added")
def guard(result, count: int):
if isinstance(result, tuple):
return result + (None,) * (count - len(result))
else:
return (result,) + (None,) * (count - 1)


@event.on("component_added")
def on_component_added(_, address, component_type):
prev = primaries.get(component_type)
if prev is None:
primaries[component_type] = proxy(address)


@event.register("component_removed")
@event.on("component_removed")
def on_component_removed(_, address, component_type):
prev = primaries.get(component_type)
if prev is not None and prev.address == address:
Expand All @@ -150,3 +167,12 @@ def setup():
for address, component_type in get_list().items():
if not is_available(component_type):
set_primary(component_type, address)


def import_component(component_type: str, module_name: str) -> Component:
component = get_primary(component_type)
if component is None:
del sys.modules[module_name]
raise ImportError("component {!r} is missing; import {!r} failed".format(component_type, module_name))

return component
21 changes: 21 additions & 0 deletions src/main/resources/assets/openpython/opos/lib/openos/computer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# noinspection PyUnresolvedReferences
from ucomputer import *
import ucomputer
import utime


def address():
return ucomputer.get_computer_address()


def uptime():
return utime.time_up()


def pull_signal(seconds):
signal = ucomputer.pop_signal(int(seconds * 20))
if signal is None:
return None

name, args = signal
return (name,) + args
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
raise NotImplementedError
Loading

0 comments on commit 69912e4

Please sign in to comment.