-
Notifications
You must be signed in to change notification settings - Fork 0
/
mainwindow.py
269 lines (207 loc) · 7.86 KB
/
mainwindow.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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
# -*- coding: utf-8 -*-
import PyQt4.QtGui as GUI
import PyQt4.QtCore as QtCore
import Actions
import Config_window
#import cv
import time
import simplejson
import joystick
import socket
from PyQt4 import uic
(Ui_MainWindow, QMainWindow) = uic.loadUiType('GUI/mainwindow.ui')
class MainWindow(QMainWindow):
"""MainWindow inherits QMainWindow"""
_sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
_packet = [4, 4, 4]
_speed = 50
_state = 0
_timer = QtCore.QTimer
_configuration_window = None
_joystick = joystick.joystick()
_writer = None
def __init__(self, parent=None):
QMainWindow.__init__(self, parent)
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
self.ui.Record_Button.clicked.connect(self.Record_Button_Clicked)
self.ui.Config_Button.clicked.connect(self.Config_Button_Clicked)
self.SpeedLCD_Display(self._speed)
self.load_Configs()
Actions.Set_Address()
self.Cam_Loop()
def Cam_Loop(self):
try:
#self.Cam_Video()
try:
self._packet = self.joystic_interpreter(
self._joystick.reading())
except:
self._packet = [4, 4, 4]
data = simplejson.dumps(self._packet)
self._sock.sendto(data, ("172.18.131.145", 5506))
finally:
self._timer.singleShot(50, self.Cam_Loop)
def joystic_interpreter(self, key):
data = [4, 4, self._speed]
if(key[1] < 0):
data[0] = 0
elif(key[1] > 0):
data[0] = 1
elif (key[0] > 0):
data[0] = 2
elif(key[0] < 0):
data[0] = 3
if(key[3] < 0):
data[1] = 0
elif(key[3] > 0):
data[1] = 1
elif (key[2] > 0):
data[1] = 2
elif(key[2] < 0):
data[1] = 3
if(key[5] == 1):
if(self._speed <= 99):
self._speed += 1
self.SpeedLCD_Display(self._speed)
data[2] = self._speed
elif (key[4] == 1):
if(self._speed >= 2):
self._speed -= 1
self.SpeedLCD_Display(self._speed)
data[2] = self._speed
elif(key[6] == 1 and key[6] != self._state):
self._state = 1
self.Record_Button_Clicked()
elif(key[6] == 0):
self._state = 0
return data
def Config_Button_Clicked(self):
if(self._configuration_window is None):
#self.ui.Config_Button.setEnabled(False)
self._configuration_window = Config_window.Config_window(self)
self._configuration_window.show()
self._configuration_window.setWindowTitle('Configurations')
self._configuration_window.exec_()
self._configuration_window = None
def Close_Button_Clicked(self):
Actions._porta_serial.close()
self.ui.Close_Button.setEnabled(False)
self.ui.OK_Button.setEnabled(True)
def OK_Button_Clicked(self):
Actions.Serial_Open(str(self.ui.comboBox_Port.currentText()),
int(self.ui.comboBox_Baud.currentText()))
self.ui.Close_Button.setEnabled(True)
self.ui.OK_Button.setEnabled(False)
self.Serial_Loop()
def Refresh_Button_Clicked(self):
for i in range(0, len(self._ports)):
self.ui.comboBox_Port.removeItem(i)
self._ports = Actions.serial_ports()
self.ui.comboBox_Port.addItems(self._ports)
def Record_Button_Clicked(self):
if(not self.ui.Record_Button.isFlat()):
self.ui.Record_Button.setText("Parar")
self.ui.Record_Button.setFlat(True)
self._writer = cv.CreateVideoWriter(
filename=("img/" + time.strftime("%H%M%S") + ".avi"),
fourcc=cv.CV_FOURCC('X', 'V', 'I', 'D'),
fps=2,
frame_size=(640, 480),
is_color=1)
else:
self.ui.Record_Button.setText("Gravar")
self.ui.Record_Button.setFlat(False)
cv.ReleaseVideoWriter(self._writer)
def SpeedLCD_Display(self, value):
self.ui.SpeedLCD.display(value)
def Image_Show(self, filename):
# Carrega uma imagen na tela
pixmap = GUI.QPixmap(filename)
self.ui.Label_Image.setPixmap(pixmap)
self.ui.Label_Image.show()
def Cam_Video(self):
# Recebe uma imagem e manda mostrar na tela
Actions.Receive_File()
if(self.ui.Record_Button.isFlat()):
frame = cv.LoadImage("img/img.jpg")
cv.WriteFrame(self._writer, frame)
self.Image_Show("img/img.jpg")
def keyPressEvent(self, event):
text = ""
key = event.key()
# Verifica se o botão pressionado é uma das teclas especias abaixo e
# grava na variavel texto uma string descrevendo a tecla, caso contrio
# grava o character que representa a tecla
if(key == QtCore.Qt.Key_Left):
text = "Left Arrow"
elif key == QtCore.Qt.Key_Right:
text = "Right Arrow"
elif key == QtCore.Qt.Key_Down:
text = "Down Arrow"
elif key == QtCore.Qt.Key_Up:
text = "Up Arrow"
elif key == QtCore.Qt.Key_Space:
text = "Space"
elif key == QtCore.Qt.Key_Control:
text = "Ctrl"
elif key == QtCore.Qt.Key_Alt:
text = "Alt"
elif key == QtCore.Qt.Key_Return:
text = "Return"
elif key == QtCore.Qt.Key_Enter:
text = "Enter"
elif key == QtCore.Qt.Key_Shift:
text = "Shift"
elif key == QtCore.Qt.Key_AltGr:
text = "AltGr"
else:
text = event.text()
self.comand(text, 0)
def comand(self, key, option):
if(option == 0):
comands = self._typesetter_config_options
else:
comands = self._control_config_options
self._packet = [4, 4, self._speed]
for i in range(0, 3):
possibilits = comands[i]
for a in range(0, len(possibilits)):
if (key == possibilits[str(a)]):
if(i == 2 and a == 0):
if(self._speed <= 99):
self._speed = self._speed + 1
self.SpeedLCD_Display(self._speed)
self._packet[i] = self._speed
elif(i == 2 and a == 1):
if(self._speed >= 2):
self._speed = self._speed - 1
self.SpeedLCD_Display(self._speed)
self._packet[i] = self._speed
elif(i == 2 and a == 2):
self.Record_Button_Clicked()
break
else:
self._packet[i] = a
i = 3
break
print((self._packet[0], self._packet[1], self._packet[2]))
data = simplejson.dumps(self._packet)
self._sock.sendto(data, ("172.18.131.145", 5506))
def load_Configs(self):
# Lê as configurações dos arquivos e grava em duas listas
self._control_config_options = []
self._typesetter_config_options = []
config_file = open('configurations/control_config.txt', 'r')
text_lines = [line.rstrip('\n') for line in config_file]
for i in range(0, len(text_lines)):
self._control_config_options.append(simplejson.loads(text_lines[i]))
config_file.close()
config_file = open('configurations/typesetter_config.txt', 'r')
text_lines = [line.rstrip('\n') for line in config_file]
for i in range(0, len(text_lines)):
self._typesetter_config_options.append(
simplejson.loads(text_lines[i]))
config_file.close()
def __del__(self):
self.ui = None