Package: xbox360controller
xbox360controllerA pythonic Xbox360 controller API built on top of thexpadLinux kernel driver.This Python Package aims to provide a pythonic and complete API for your Xbox360 and similar game controllers.
Currently it's built on top of the Linux kernel driverxpadso you can use it on almost any Linux distribution including your Rasperry Pi projects etc.The following features are supported:Registering callbacks forallButtons, Axes, Triggers and Hat in agpiozero-inspired waySetting the LED circle; allxpadprovided options are possible: blinking, rotating, setting individual LEDs on and off, ...Rumbling, both the left and right side can be controlled from 0 to 100 percentInstallationYou will need Python 3.4 or above.Any Linux distribution:pip3 install -U xbox360controllerYou might also use avirtual environmentor do a per-user install by providing the--userflag to above command.
Global installations might require the usage ofsudoor working directly from a root shell but arenot recommended.If thepip3command cannot be found, trypipor make sure to have pip installed properly:sudo apt install python3-pipOf course you don't needsudowhen working from a root shell.UsageBasicsimportsignalfromxbox360controllerimportXbox360Controllerdefon_button_pressed(button):print('Button{0}was pressed'.format(button.name))defon_button_released(button):print('Button{0}was released'.format(button.name))defon_axis_moved(axis):print('Axis{0}moved to{1}{2}'.format(axis.name,axis.x,axis.y))try:withXbox360Controller(0,axis_threshold=0.2)ascontroller:# Button A eventscontroller.button_a.when_pressed=on_button_pressedcontroller.button_a.when_released=on_button_released# Left and right axis move eventcontroller.axis_l.when_moved=on_axis_movedcontroller.axis_r.when_moved=on_axis_movedsignal.pause()exceptKeyboardInterrupt:passThe above code will run untilCtrl+Cis pressed. Each time on of the left or right axis is moved, the event will be processed. Additionally, the events of the A button are being processed.See theAPI referencefor a more detailed explanation of theXbox360Controllerclass and how to use all available buttons, axes and the hat.Rumblingimporttimefromxbox360controllerimportXbox360ControllerwithXbox360Controller()ascontroller:controller.set_rumble(0.5,0.5,1000)time.sleep(1)This will enable rumble on both sides of the controller with each 50% strength for one second (1000ms). Note that the method call is non-blocking, thus we need to manually wait one second for the rumble to finish. You won't need this in a regular use case withsignal.pause().LEDimporttimefromxbox360controllerimportXbox360ControllerwithXbox360Controller()ascontroller:controller.set_led(Xbox360Controller.LED_ROTATE)time.sleep(1)controller.set_led(Xbox360Controller.LED_OFF)This will let the LED circle rotate for one second and then turn it off.See theAPI referencefor all available LED modes.Debug informationfromxbox360controllerimportXbox360ControllerwithXbox360Controller()ascontroller:controller.info()Development/contributingThis project is now in a somewhat stable state, and I really appreciate all kinds of contributions - may it be new or improved code, documentation or just a simple typo fix.
Just provide me a PR and I'll be happy to include your work!For feature requests, general questions or problems you face regarding this package pleaseopen an issue.Release HistoryPlease seeCHANGES.mdfor a complete release history.AuthorsLinus Groh (@linusg) –mail@linusgroh.deLicenseAll the code and documentation are distributed under the MIT license. SeeLICENSEfor more information.