@@ -50,7 +50,8 @@ def show_map(self) -> bool:
50
50
def poll (self ):
51
51
'''
52
52
Query the input controller for a button or axis state change event.
53
- This must be threadsafe.
53
+ This must be threadsafe as it will generally be called on
54
+ a separate thread.
54
55
55
56
returns: tuple of
56
57
- button: string name of button if a button changed, otherwise None
@@ -81,8 +82,8 @@ class LinuxGameController(AbstractInputController):
81
82
def __init__ (self , button_names = {}, axis_names = {}, dev_fn = '/dev/input/js0' ):
82
83
self .axis_states = {}
83
84
self .button_states = {}
84
- self .axis_names = button_names
85
- self .button_names = axis_names
85
+ self .axis_names = axis_names
86
+ self .button_names = button_names
86
87
self .axis_map = []
87
88
self .button_map = []
88
89
self .num_axes = 0
@@ -97,7 +98,7 @@ def init(self) -> bool:
97
98
Attempt to initialize the controller.
98
99
In Linux, query available buttons and axes using fnctl and
99
100
a path in the linux device tree.
100
- If the button_names or axis_names mappings passed to the contruct
101
+ If the button_names or axis_names mappings passed to the contructor
101
102
maps to a discovered input control, then use that name when emitting
102
103
events for that input control, otherwise emit using a default name.
103
104
- on success returns self so it can be fluently chained
@@ -152,6 +153,7 @@ def init(self) -> bool:
152
153
axis_name = self .axis_names .get (axis , 'axis(0x%02x)' % axis )
153
154
self .axis_map .append (axis_name )
154
155
self .axis_states [axis_name ] = 0.0
156
+ self .axis_names [axis ] = axis_name # update the name map
155
157
156
158
# Get the button map.
157
159
buf = array .array ('H' , [0 ] * 200 )
@@ -161,7 +163,7 @@ def init(self) -> bool:
161
163
btn_name = self .button_names .get (btn , 'button(0x%03x)' % btn )
162
164
self .button_map .append (btn_name )
163
165
self .button_states [btn_name ] = 0
164
- #print(' btn', '0x%03x' % btn, ' name', btn_name)
166
+ self . button_names [ btn ] = btn_name # update the name map
165
167
166
168
self .initialized = True
167
169
return True
@@ -235,6 +237,49 @@ def poll(self):
235
237
return button , button_state , axis , axis_val
236
238
237
239
240
+ class LogitechJoystick (LinuxGameController ):
241
+ '''
242
+ An interface to a physical Logitech joystick available at /dev/input/js0
243
+ Contains mapping that work for Raspian Stretch drivers
244
+ Tested with Logitech Gamepad F710
245
+ https://www.amazon.com/Logitech-940-000117-Gamepad-F710/dp/B0041RR0TW
246
+ credit:
247
+ https://github.com/kevkruemp/donkeypart_logitech_controller/blob/master/donkeypart_logitech_controller/part.py
248
+ '''
249
+ def __init__ (self , * args , ** kwargs ):
250
+ super (LogitechJoystick , self ).__init__ (* args , ** kwargs )
251
+
252
+ self .axis_names = {
253
+ 0x00 : 'left_stick_horz' ,
254
+ 0x01 : 'left_stick_vert' ,
255
+ 0x03 : 'right_stick_horz' ,
256
+ 0x04 : 'right_stick_vert' ,
257
+
258
+ 0x02 : 'L2_pressure' ,
259
+ 0x05 : 'R2_pressure' ,
260
+
261
+ 0x10 : 'dpad_leftright' , # 1 is right, -1 is left
262
+ 0x11 : 'dpad_up_down' , # 1 is down, -1 is up
263
+ }
264
+
265
+ self .button_names = {
266
+ 0x13a : 'back' , # 8 314
267
+ 0x13b : 'start' , # 9 315
268
+ 0x13c : 'Logitech' , # a 316
269
+
270
+ 0x130 : 'A' ,
271
+ 0x131 : 'B' ,
272
+ 0x133 : 'X' ,
273
+ 0x134 : 'Y' ,
274
+
275
+ 0x136 : 'L1' ,
276
+ 0x137 : 'R1' ,
277
+
278
+ 0x13d : 'left_stick_press' ,
279
+ 0x13e : 'right_stick_press' ,
280
+ }
281
+
282
+
238
283
class InputControllerEvents (object ):
239
284
'''
240
285
Poll a AbstractGameController() and convert to button and axis events.
@@ -364,7 +409,9 @@ def shutdown(self):
364
409
# step 1: collect button and axis names
365
410
# - init must be called to initialize the
366
411
#
367
- controller = LinuxGameController (button_names = {}, axis_names = {})
412
+ # controller = LinuxGameController(button_names={}, axis_names={})
413
+ controller = LogitechJoystick ()
414
+
368
415
369
416
#
370
417
# step 2: start sending events
0 commit comments