forked from markroxor/Kimmove---Hackathon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
GUI.py
220 lines (168 loc) · 5.77 KB
/
GUI.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
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
import pygame
import os
import sys
import shutil
import subprocess
import pyautogui
import serial
import time
is_navMode , is_gameMode = False, False
pygame.init()
display_width = 400
display_height = 200
gameDisplay = pygame.display.set_mode((display_width,display_height))
pygame.display.set_caption("Mode Select")
smallfont = pygame.font.SysFont("tahoma", 25)
medfont = pygame.font.SysFont("tahoma", 50)
largefont = pygame.font.SysFont("tahoma", 85)
def text_objects(text, color,size = "small"):
if size == "small":
textSurface = smallfont.render(text, True, color)
if size == "medium":
textSurface = medfont.render(text, True, color)
if size == "large":
textSurface = largefont.render(text, True, color)
return textSurface, textSurface.get_rect()
def text_to_button(msg, color, buttonx, buttony, buttonwidth, buttonheight, size = "small"):
textSurf, textRect = text_objects(msg,color,size)
textRect.center = ((buttonx+(buttonwidth/2)), buttony+(buttonheight/2))
gameDisplay.blit(textSurf, textRect)
def close_action():
os.system('TASKKILL /F /IM OpenCV.exe')
os.system('TASKKILL /F /IM OpenCV2.exe')
def Gaming():
global is_gameMode, is_navMode
if is_navMode:
os.system('TASKKILL /F /IM OpenCV2.exe')
is_navMode = False
pid = subprocess.Popen('"OpenCV.exe"')
is_gameMode = True
def Navigation():
global is_gameMode, is_navMode
if is_gameMode:
os.system('TASKKILL /F /IM OpenCV.exe')
is_gameMode = False
pid1 = subprocess.Popen('"OpenCV2.exe"')
is_navMode = True
def Toggle():
print ("Toggle Begin!")
global is_gameMode, is_navMode
if is_gameMode:
os.system('TASKKILL /F /IM OpenCV.exe')
is_gameMode = False
time.sleep(1)
pid1 = subprocess.Popen('"OpenCV2.exe"')
is_navMode = True
elif is_navMode:
os.system('TASKKILL /F /IM OpenCV2.exe')
is_navMode = False
time.sleep(1)
# print("Task Killed")
pid1 = subprocess.Popen('"OpenCV.exe"')
is_gameMode = True
# print ("Opened New")
def button(text, x, y, width, height, action = None):
cur = pygame.mouse.get_pos()
click = pygame.mouse.get_pressed()
if x + width > cur[0] > x and y + height > cur[1] > y:
pygame.draw.rect(gameDisplay,(255,255,255), (x,y,width,height))
if click[0] == 1 and action != None:
if action == "Close":
print ("closing call")
close_action()
if action == "Gaming":
print ("gaming call")
Gaming()
if action == "Navigation":
print ("Navigation call")
Navigation()
if action == "Toggle":
Toggle()
else:
pygame.draw.rect(gameDisplay,(127,127,127), (x,y,width,height))
black = (0,0,0)
text_to_button(text,black,x,y,width,height)
def comm(inp, ser):
print inp
if "RELOAD" in inp:
pyautogui.press('R')
inp = ser.readline()
while "RELOAD" in inp:
inp = ser.readline()
pyautogui.press('R')
print "R"
elif "LR" in inp:
pyautogui.mouseDown(button='left')
pyautogui.mouseDown(button='right')
inp = ser.readline()
while "LR" in inp:
inp = ser.readline()
pyautogui.mouseUp(button='left')
pyautogui.mouseUp(button='right')
elif "LEFT" in inp:
pyautogui.mouseDown(button='left')
inp = ser.readline()
while "LEFT" in inp:
inp = ser.readline()
pyautogui.mouseUp(button='left')
elif "RIGHT" in inp:
pyautogui.mouseDown(button='right')
inp = ser.readline()
while "RIGHT" in inp:
inp = ser.readline()
pyautogui.mouseUp(button='right')
elif "TOGGLE" in inp:
inp = ser.readline()
#close_action()
Toggle()
elif "CLOSE" in inp:
inp = ser.readline()
close_action()
#Toggle()
else:
inp = ser.readline()
return inp
def getPort(lower=1):
ports = ["COM"]*(101-lower)
for i in xrange(lower,100):
ports[i-lower]+=str(i)
print ("Ports to be tested:",ports)
for port in ports:
print ("checking PORT:",port)
try:
ser = serial.Serial(port,9600,timeout=2)
inp = ser.readline()
print (inp)
if not len(inp):
ser.close()
pass
else:
return ser
except:
pass
print "No port found"
return ser
def loop():
try:
print "Trying to connect..."
ser = getPort(1)
print "Connection Established!"
except:
print "Connection Refused!"
#ser = serial.Serial('COM86', 9600)
inp = ser.readline()
while 1:
inp = comm(inp, ser)
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
white = (168,168,168)
gameDisplay.fill(white)
button("Gaming", 35,25,130,35, action="Gaming")
button("Navigation", 35,85,130,35, action="Navigation")
button("Toggle", 35,145,130,35, action="Toggle")
button("Close", 300,45,75,35, action="Close")
pygame.display.update()
if __name__ == '__main__':
loop()