I am fairly new to python and am attempting to create a custom module that I can import for my raspberry pi using evdev (not that important). My problem is I need a good way to know when a given button is pressed down preferably using True/False. If I was to import this module and use the read_stream() function what would be the easiest way to be able to gather multiple types of inputs. (Just for clarification, the gamepad.read_loop() only returns one value per iteration.
from evdev import InputDevice, categorize, ecodes
aBtn = 304
bBtn = 305
xBtn = 307
yBtn = 308
LBmper = 310
RBmper = 311
MenuBtn = 315
WindowBtn = 314
XboxBtn = 316
HorizontalDp = 16
VerticalDp = 17
RightT = 5
LeftT = 2
LeftStickClick = 317
RightStickClick = 318
Ls_X = 0
Ls_Y = 1
Rs_X = 3
Rs_Y = 4
def read_stream(input_path, trigger_deadzone):
'''Begins the loop that reads all of the incoming events.'''
button_list = ['aBtn_pressed', 'bBtn_pressed', 'xBtn_pressed', 'yBtn_pressed', 'lBmper_pressed', 'rBmper_pressed', 'mBtn_pressed', 'wBtn_pressed', 'xbxBtn_pressed', 'rDp_pressed', 'lDp_pressed', 'dDp_pressed', 'uDp_pressed', 'Ls_clicked', 'Rs_clicked', 'Ls_right', 'Ls_left', 'Ls_down', 'Ls_up', 'Rs_right', 'Rs_left', 'Rs_down', 'Rs_up', 'Lt_custom', 'Rt_custom']
for button in button_list:
button = False
gamepad = InputDevice(input_path)
if (not "Microsoft X-Box One S pad" in str(gamepad)):
raise Exception("Invalid input device, please make sure you are using an Xbox One S controller.\nAlso make sure that the correct input path is set.")
print("Stream is reading: ", gamepad)
for event in gamepad.read_loop():
value = event.value
code = event.code
if (code == aBtn):
#if (304 <= code <= 308):
if (code == aBtn):
if (value == 1):
aBtn_pressed = True
else:
aBtn_pressed = False
elif (code == bBtn):
if (value == 1):
bBtn_pressed = True
else:
bBtn_pressed = False
elif (code == xBtn):
if (value == 1):
xBtn_pressed = True
else:
xBtn_pressed = False
else:
if (value == 1):
yBtn_pressed = True
else:
yBtn_pressed = False
else:
print("Unfiltered stream input.")
It's difficult to determine exactly what you're asking, but if you'd like the state of each button every time you call that function one way would be to return the current values in a dictionary each time.
For example:
buttons = {
304:"A",
305:"B",
307:"X",
308:"Y" )
# and then your read stream function would be modified to look like
pressed = dict()
for event in gamepad.read_loop():
btn = buttons[event.code]
pressed[btn] = bool(event.value)
return pressed
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.