-
Notifications
You must be signed in to change notification settings - Fork 0
/
ui.py
74 lines (61 loc) · 1.87 KB
/
ui.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
from rpyc import connect_by_service
import pantograph
from threading import Thread
from time import sleep
import asyncio
# TODO: Major fault handling
class UI(pantograph.PantographHandler):
W = 64
H = 32
CW = 10 # Cell width
CH = 10
def setup(self):
self.keys = [ord(x) for x in '1234QWERASDFZXCV']
self.pressed_keys = set()
self.rects = self.rects = [0]*(UI.W*UI.H)
self.memory = UIServer.memory
assert self.memory
# self.memory = connect_by_service("memory")
def update(self):
refresh = self.memory.root.get_refresh()
if not refresh: return
self.rects = self.bytes_to_bits(self.memory.root.get_address(0xF00, 256))
self.memory.root.set_refresh(False)
for y in range(UI.H):
for x in range(UI.W):
self.fill_rect(x*UI.CW, y*UI.CH, UI.CW, UI.CH, "#FFF" if self.rects[y*UI.W+x] else "#000")
def get_keys(self):
keylist = ""
for key in self.keys:
if key in self.pressed_keys:
keylist += "1"
else:
keylist += "0"
return int(keylist, 2)
def on_key_down(self, event):
# print(f"KEYD: {event.key_code}")
if event.key_code in self.keys:
self.pressed_keys.add(event.key_code)
self.memory.root.set_io_keys(self.get_keys())
def on_key_up(self, event):
# print(f"KEYU: {event.key_code}")
if event.key_code in self.keys:
self.pressed_keys.remove(event.key_code)
self.memory.root.set_io_keys(self.get_keys())
def bytes_to_bits(self, bytelist):
bits = []
[bits.extend(map(int, [x for x in '{:08b}'.format(byte)])) for byte in bytelist]
return bits
class UIServer(Thread):
memory = None
def __init__(self, memory):
Thread.__init__(self)
UIServer.memory = memory
self.app = pantograph.SimplePantographApplication(UI)
def run(self):
print("Starting UI thread")
asyncio.set_event_loop(asyncio.new_event_loop())
self.app.run()
if __name__ == "__main__":
app = pantograph.SimplePantographApplication(UI)
app.run()