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

Commit bbf54c1

Browse files
committed
변경된 인터럽트 프로토콜에 따라서 파이썬 코드 변경 (oprom modules)
1 parent 9de10a7 commit bbf54c1

3 files changed

Lines changed: 50 additions & 47 deletions

File tree

src/modules/_boot.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1-
from handler import print_handler, signal_handler
1+
import machine
2+
from handler import print_handler, signal_handler, input_handler
3+
machine.interrupt_hook(signal_handler)
4+
machine.input_hook(input_handler)
5+
machine.print_hook(print_handler)
26

37
__all__ = ["print_handler", "signal_handler"]
48
del __all__

src/modules/component.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,10 +59,13 @@ def scroll(self):
5959
self.gpu.copy(1, 2, self.widthSize, self.heightSize, 0, - 1)
6060
self.gpu.fill(1, self.heightSize, self.widthSize, 1, " ")
6161

62+
def put(self, buf: str):
63+
for char in buf:
64+
self.putChar(char)
65+
6266
def putChar(self, char):
63-
if char == "\r":
64-
self.widthPos = 1
6567
if char == "\n":
68+
self.widthPos = 1
6669
self.heightPos += 1
6770

6871
if self.widthPos > self.widthSize:

src/modules/handler.py

Lines changed: 40 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
import micropython
2-
from machine import signal as pop_signal, set_stdin_char, debug
3-
import ujson
1+
from machine import signal as pop_signal, debug
42

53
from component import Component, Monitor, devices
64

@@ -12,51 +10,49 @@
1210
monitor = Monitor(gpu)
1311
gpu.fill(1, 1, monitor.widthSize, monitor.heightSize, " ")
1412

13+
buf = []
14+
15+
16+
def input_handler():
17+
while not buf:
18+
signal_handler()
19+
20+
return int(buf.pop(0))
21+
1522

1623
def print_handler(buf):
1724
try:
18-
for char in buf:
19-
monitor.putChar(char)
25+
monitor.put(buf)
2026
except BaseException as e:
21-
debug("exc%s: %s" % (type(e).__name__ , e))
27+
debug("print_handler exc =? %s: %s" % (type(e).__name__, e))
2228

2329

24-
def signal_handler(_):
25-
micropython.schedule(signal_handle, None)
26-
27-
28-
def signal_handle(_):
30+
def signal_handler():
2931
DEBUG = False
30-
while True:
31-
try:
32-
# TODO: sleeping is possble with ticks=n
33-
# sleeping with some ticks but signal available then resume computer and give signal
34-
# ExecutionResult.Sleep(inf) => when signal is available then return signal
35-
signal = pop_signal()
36-
# debug("check signal " + repr(signal))
37-
if not signal:
38-
break
39-
40-
debug(ujson.dumps(signal) + '\n')
41-
42-
name, args = signal # type: str, tuple
43-
if name == "key_down" and len(args) >= 4:
44-
# when redirectKeyEvent set then never called
45-
_, char, _, _, *_ = args
46-
set_stdin_char(int(char))
47-
elif name == "clipboard" and len(args) >= 3:
48-
_, value, _, *_ = args
49-
for char in value:
50-
set_stdin_char(ord(char))
51-
elif name == "key_up" and len(args) >= 4:
52-
pass
53-
elif name == "component_added" and len(args) >= 2:
54-
if DEBUG:
55-
debug("signal", name, args, len(args))
56-
address, device_type, *_ = args
57-
devices[device_type] = Component(address, device_type)
58-
else:
59-
if DEBUG:
60-
debug("signal", name, args, len(args))
61-
except BaseException as e:
62-
debug("exc%s: %s" % (type(e).__name__ , e))
32+
try:
33+
signal = pop_signal(20)
34+
if signal is None:
35+
return
36+
37+
# debug(ujson.dumps(signal) + '\n')
38+
name, args = signal # type: str, tuple
39+
if name == "key_down" and len(args) >= 4:
40+
# when redirectKeyEvent set then never called
41+
_, char, _, _, *_ = args
42+
buf.append(char)
43+
elif name == "clipboard" and len(args) >= 3:
44+
_, value, _, *_ = args
45+
# for char in value:
46+
# set_stdin_char(ord(char))
47+
elif name == "key_up" and len(args) >= 4:
48+
pass
49+
elif name == "component_added" and len(args) >= 2:
50+
if DEBUG:
51+
debug("signal", name, args, len(args))
52+
address, device_type, *_ = args
53+
devices[device_type] = Component(address, device_type)
54+
else:
55+
if DEBUG:
56+
debug("signal", name, args, len(args))
57+
except BaseException as e:
58+
debug("signal_handler exc => %s: %s" % (type(e).__name__, e))

0 commit comments

Comments
 (0)