-
Notifications
You must be signed in to change notification settings - Fork 3
/
KeyboardInterface.py
229 lines (186 loc) · 6.58 KB
/
KeyboardInterface.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
from PyQt5.QtCore import Qt, QTimer
from PyQt5.QtGui import QKeySequence
from PyQt5.QtWidgets import QApplication, QMainWindow
import sys
import os
import configparser
import tkinter as tk
from tkinter import ttk
from pupperpy.Controller.Controller_QT import Ui_MainWindow
import time
from pupperpy.BluetoothInterface import BluetoothClient
from pupperpy.CommandInterface import ControllerState
__location__ = os.path.dirname(__file__)
RESOURCE_DIR = os.path.join(__location__, 'pupperpy', 'resources')
KEY_CONFIG = os.path.join(RESOURCE_DIR, 'keyboard_mapping.conf')
MESSAGE_RATE = 20
PUPPER_COLOR = {"red":255, "blue":0, "green":0}
RESET_KEY = Qt.Key_Space
EXIT_KEY = Qt.Key_Escape
def launch_pupper_controller():
app = QApplication(sys.argv)
cgi = ControlGUI()
sys.exit(app.exec_())
def load_keymap(keymap=KEY_CONFIG):
config = configparser.ConfigParser()
config.read(keymap)
out_map = {}
for k1, v1 in config.items():
for k2, v2 in v1.items():
if v2 == '':
continue
key = QKeySequence(v2)[0]
out_map[key] = (k1, k2)
return out_map
class DummyBLE(object):
def __init__(self):
pass
def send(self, msg):
pass
def recv(self):
return None
def close(self):
pass
class ControlGUI(QMainWindow):
'''This will start a keyboard listener and display the controller state. It
will loop and poll the listener for controller state, then send the state
on to the raspberry pi via Bluetooth, will also check for a response from
the pi
'''
def __init__(self, *args, **kwargs):
if 'keymap' in kwargs:
keymap = kwargs.pop('keymap')
else:
keymap = KEY_CONFIG
super(ControlGUI, self).__init__(*args, **kwargs)
#window_center(self.root)
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
self.state = ControllerState()
self.pupper_color = PUPPER_COLOR
self.active = False
self.config = load_keymap(keymap)
self.updateUI()
#self.ble_interface = BluetoothClient()
self.ble_interface = DummyBLE()
self.timer = QTimer()
self.timer.setInterval(1000 / MESSAGE_RATE)
self.timer.timeout.connect(self.send_signal)
self.timer.start()
self.show()
def keyPressEvent(self, event):
key = event.key()
mapped = self.config.get(key)
if key == EXIT_KEY:
self.close()
if key == RESET_KEY:
self.state = ControllerState()
elif mapped is None:
return
else:
group, key_val = mapped
if key_val == 'reset':
self.state = ControllerState()
elif key_val == 'quit':
self.close()
elif group == 'toggle':
setattr(self.state, key_val, True)
else:
if key_val == 'increment':
self.state.increment(group)
elif key_val == 'decrement':
self.state.decrement(group)
else:
raise ValueError('Unknown keyword %s' % key_val)
self.updateUI()
def keyReleaseEvent(self, event):
key = event.key()
mapped = self.config.get(key)
if mapped is None:
return
group, key_val = mapped
if group is None or key_val is None:
return
if group == 'toggle':
setattr(self.state, key_val, False)
elif 'dpad' in group:
# reset dpad values when reset, dpad must be 1 or -1 while pressed
# and 0 otherwise
if key_val == 'increment':
self.state.decrement(group)
elif key_val == 'decrement':
self.state.increment(group)
self.updateUI()
print(self.state)
@property
def pupper_color(self):
return self._pupper_color
@pupper_color.setter
def pupper_color(self, value):
if value is None:
return
if not all([x in value.keys() for x in ['red', 'blue', 'green']]):
raise ValueError('Invalid Color Message')
self._pupper_color = value
def updateUI(self, state=None, pupper_msg=None):
if state is not None:
self.state = state
else:
state = self.state
if pupper_msg is not None:
self.pupper_color = pupper_msg['ps4_color']
state_dict = state.get_state()
lx = state.left_analog_x
ly = state.left_analog_y
rx = state.right_analog_x
ry = state.right_analog_y
dx = state.dpad_x
dy = state.dpad_y
l1 = state.l1
r1 = state.r1
hop = state.cross
if l1 and not self.active:
self.active = True
self.ui.active_label.setText('Active')
r,g,b = (self._pupper_color[x] for x in ['red', 'green', 'blue'])
self.ui.active_label.setStyleSheet("background-color: rgb(%i, %i, %i)"
% (r,g,b))
set_toggle(self.ui.gait_button, r1)
set_toggle(self.ui.hop_button, hop)
if dx > 0:
set_toggle(self.ui.dpad_right, True)
set_toggle(self.ui.dpad_left, False)
elif dx < 0:
set_toggle(self.ui.dpad_right, False)
set_toggle(self.ui.dpad_left, True)
else:
set_toggle(self.ui.dpad_right, False)
set_toggle(self.ui.dpad_left, False)
if dy > 0:
set_toggle(self.ui.dpad_up, True)
set_toggle(self.ui.dpad_down, False)
elif dy < 0:
set_toggle(self.ui.dpad_up, False)
set_toggle(self.ui.dpad_down, True)
else:
set_toggle(self.ui.dpad_up, False)
set_toggle(self.ui.dpad_down, False)
self.ui.left_x_slider.setValue(int(100 * lx / state.LEFT_ANALOG_X_MAX))
self.ui.left_y_slider.setValue(int(100 * ly / state.LEFT_ANALOG_Y_MAX))
self.ui.right_x_slider.setValue(int(100 * rx / state.RIGHT_ANALOG_X_MAX))
self.ui.right_y_slider.setValue(int(100 * ry / state.RIGHT_ANALOG_Y_MAX))
def send_signal(self):
self.ble_interface.send(self.state.get_state())
msg = self.ble_interface.recv()
if msg is not None:
self.pupper_color = msg['ps4_color']
def closeEvent(self, event):
self.active = False
self.ble_interface.close()
self.timer.stop()
event.accept()
def set_toggle(obj, val):
if obj.isChecked() != val:
obj.toggle()
if __name__ == '__main__':
launch_pupper_controller()