-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpokeComm.py
More file actions
129 lines (102 loc) · 3.71 KB
/
pokeComm.py
File metadata and controls
129 lines (102 loc) · 3.71 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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
import jsonpickle
import functionsTiming as funTime
# Echo server program
import socket
HOST = '' # Symbolic name meaning all available interfaces
PORT = 50007 # Arbitrary non-privileged port
SEND_PERIOD = 100
class Button:
def __init__(self):
self.count = 0
self.prevCount = 0
self.pulse_Pressed = False
class CommHandler:
def __init__(self):
self.periodTimer = funTime.TimerON()
self.sock = 0
self.client = 0
self.addr = 0
self.imageCaptureCount = 0
self.hashFlat = False
self.hashMatch = False
self.tbDialogue = False
self.tbGrey = False
self.tbFight = False
self.recordState = 0
commHandler = CommHandler()
buttons = []
class sendData:
def __init__(self):
self.slider = []
self.button = []
def init_socketServer() :
global commHandler
global buttons
buttons = [Button() for i in range(10)]
commHandler.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
commHandler.sock.bind((HOST, PORT))
commHandler.sock.listen(1)
commHandler.sock.setblocking(False)
commHandler.client = None
def handle_socketServer() :
global commHandler
global buttons
# Initialize pulse bits
for b in buttons:
b.pulse_Pressed = False
# Attempt to allow connection with client
if commHandler.client is None:
try:
commHandler.client, commHandler.addr = commHandler.sock.accept()
except BlockingIOError:
commHandler.client = None
else:
print('Connected by', commHandler.addr)
# This executes if a client is connected
else:
# Send data once ever send period
commHandler.periodTimer.run(1,SEND_PERIOD)
if commHandler.periodTimer.done:
commHandler.periodTimer.reset()
msgPayload = {}
msgPayload['dataReq'] = True
msgPayload['captureCount'] = commHandler.imageCaptureCount
msgPayload['flatHash'] = commHandler.hashFlat
msgPayload['tbBlue'] = commHandler.tbDialogue
msgPayload['tbGrey'] = commHandler.tbGrey
msgPayload['tbFight'] = commHandler.tbFight
msgPayload['recordState'] = commHandler.recordState
jsonDumpStr = jsonpickle.dumps(msgPayload)
jsonDumpBytes = jsonDumpStr.encode('UTF-8')
try:
commHandler.client.sendall(jsonDumpBytes)
except:
commHandler.client = None
try:
raw = commHandler.client.recv(1024)
except:
pass
else:
if raw:
rawString = raw.decode('utf-8')
panelData = jsonpickle.decode(rawString)
buttonsDict = panelData.get('buttons')
if buttonsDict:
index0 = 0
for b in buttonsDict:
if index0 < len(buttons):
buttons[index0].count = list(b.values())[0]
if buttons[index0].count != buttons[index0].prevCount:
buttons[index0].pulse_Pressed = True
buttons[index0].prevCount = buttons[index0].count
print(f'Button {index0} pressed!')
index0 = index0 + 1
if __name__ == "__main__":
init_socketServer()
incrementCountTimer = funTime.TimerON()
incrementCountTimer.preset = 2000
while True:
if incrementCountTimer.run(1):
incrementCountTimer.reset()
commHandler.imageCaptureCount += 1
handle_socketServer()