-
Notifications
You must be signed in to change notification settings - Fork 0
/
inputOutput.py
74 lines (57 loc) · 2 KB
/
inputOutput.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
import time
class IODevice:
def __init__(self):
self.status = 0
self.data = 0
def getStatus(self):
return format(self.status, "016b") # 016 means 16 digits with leading 0s
def setStatus(self, status):
self.status = int(status, 2)
def getData(self):
return format(self.data, "016b") # 016 means 16 digits with leading 0s
def setData(self, data):
self.data = int(data, 2)
class Monitor(IODevice):
def __init__(self, refreshRate):
super().__init__()
self.status = 1 << 15
self.delay = 1 / refreshRate
def writeCharacter(self, character):
if not (self.status & 1 << 15):
return
self.data = character
self.status &= ~ (1 << 15)
def printFromData(self, display):
if not (self.status & 1 << 15):
display.configure(state="normal")
try:
if self.data == 8:
if display.get("end - 2c") == "\n":
display.delete("end - 1c", "end")
else:
display.delete("end - 2c", "end")
elif self.data == 13:
display.insert("end", "\n")
else:
display.insert("end", chr(self.data))
except:
display.insert("end", "☒")
display.configure(state="disabled")
display.see("end")
self.status |= 1 << 15
def setData(self, data):
self.data = int(data, 2)
self.status &= ~(1 << 15)
class Keyboard(IODevice):
def writeCharacter(self, character):
if self.status & 1 << 15:
return
self.data = ord(character)
self.status |= 1 << 15
def getData(self):
self.status &= ~(1 << 15)
return format(self.data, "016b") # 016 means 16 digits with leading 0s
def updateMonitor(monitor, display):
while True:
time.sleep(monitor.delay)
monitor.printFromData(display)