-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathemulate.py
More file actions
executable file
·83 lines (61 loc) · 2.08 KB
/
emulate.py
File metadata and controls
executable file
·83 lines (61 loc) · 2.08 KB
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
75
76
77
78
79
80
81
82
#! /usr/bin/python
#------------------------------------------------------------------------------
#------------------------------------------------------------------------------
import pygame
from pygame.locals import *
import wc1
#------------------------------------------------------------------------------
_screen_x = 300
_screen_y = 300
_pushbutton = 0
_toggle = 1
#------------------------------------------------------------------------------
def inv(x):
return ~x & 1
class plc:
def __init__(self, fsm, key_map):
pygame.init()
self.screen = pygame.display.set_mode((_screen_x, _screen_y))
pygame.display.set_caption('PLC')
self.fsm = fsm
self.key_map = key_map
self.sv = fsm.s.isv
self.iv = fsm.i.iiv
self.ov = fsm.o.ov
def __str__(self):
return str(self.fsm)
def get_inputs(self):
"""work out the current input vector"""
for event in pygame.event.get():
if event.type in (KEYDOWN, KEYUP):
if self.key_map[event.key]:
(bit, switch) = self.key_map[event.key]
if event.type == KEYDOWN:
self.iv[bit] = inv(self.iv[bit])
elif event.type == KEYUP:
if switch == _pushbutton:
self.iv[bit] = inv(self.iv[bit])
def run(self):
self.get_inputs()
# run the state machine
self.sv, self.ov = self.fsm.fsm(self.sv, self.iv)
#------------------------------------------------------------------------------
def main():
key_map = {
K_f: (0, _pushbutton),
K_w: (1, _pushbutton),
K_s: (2, _pushbutton),
K_x: (3, _toggle),
}
x = plc(wc1.wc1(), key_map)
prev_state = None
state_count = 0
while True:
state = str(x)
if state != prev_state:
prev_state = state
state_count += 1
print '%d: %s' % (state_count, state)
x.run()
main()
#------------------------------------------------------------------------------