-
-
Save akbsteam/4486369 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<config> | |
<joystick id="0"> | |
<axis number="0" low="Left" high="Right" /> | |
<axis number="1" low="Up" high="Down" /> | |
<button number="0" key="SN2" /> | |
<button number="1" key="SN1" /> | |
<button number="2" key="HC1" /> | |
<button number="3" key="HC2" /> | |
<button number="4" key="HC3" /> | |
<button number="5" key="HHC1" /> | |
<button number="6" key="HHC2" /> | |
<button number="7" key="HHO1" /> | |
<button number="9" key="HHO2" /> | |
<button number="10" key="SN3" /> | |
</joystick> | |
</config> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from xml.dom.minidom import * | |
from threads import threaded | |
import os | |
os.environ["SDL_VIDEODRIVER"] = "dummy" | |
class JoyStickAxis(): | |
def __init__(self, lowKey, highKey): | |
self.low = lowKey | |
self.high = highKey | |
self.low_active = False | |
self.high_active = False | |
class JoyStick(): | |
def __init__(self): | |
self.axis = { } | |
self.buttons = { } | |
def print_info(self): | |
print "Joystick mappings found : " | |
for axis in self.axis.keys(): | |
print " - axis %s direction low mapped to key %s" % (axis, self.axis[axis].low) | |
print " - axis %s direction high mapped to key %s" % (axis, self.axis[axis].high) | |
for key in self.buttons: | |
print " - button %s mapped to key %s" % (key, self.buttons[key]) | |
class JKeys: | |
def __init__(self, document): | |
self.joysticks = {} | |
for joy_node in document.getElementsByTagName("joystick"): | |
joy = JoyStick() | |
for axis_node in joy_node.getElementsByTagName("axis"): | |
axis = JoyStickAxis( axis_node.getAttribute("low"), axis_node.getAttribute("high")) | |
joy.axis[ axis_node.getAttribute("number") ] = axis | |
for button_node in joy_node.getElementsByTagName("button"): | |
joy.buttons[button_node.getAttribute("number")] = button_node.getAttribute("key") | |
self.joysticks[int(joy_node.getAttribute("id"))] = joy | |
joy.print_info() | |
try: | |
import pygame | |
except: | |
print "You need to install pygame to capture joystick controls" | |
pygame.init() | |
self.nbJoy = pygame.joystick.get_count() | |
for i in range(self.nbJoy): | |
pygame.joystick.Joystick(i).init() | |
if self.nbJoy == 0: | |
print "Sorry no joysticks found!!" | |
elif self.nbJoy != len(self.joysticks): | |
print "%d joysticks configured, only %d joysticks found" % (len(self.joysticks), self.nbJoy) | |
self.debug = False | |
@threaded | |
def run(self): | |
if self.nbJoy != 0: | |
import pygame | |
pygame.display.init() | |
pygame.time.set_timer(1, 100) | |
capture_events = [pygame.JOYBUTTONDOWN, pygame.JOYAXISMOTION, pygame.JOYBUTTONUP, pygame.JOYHATMOTION] | |
# clear out all pre existing pygame events else we get 1 of each | |
pygame.event.clear() | |
def key_press( key ): | |
command = "mpg321 /home/pi/mp3/Axxe-" + key + ".mp3 &" | |
os.system(command) | |
def key_release( key ): | |
print "released key %s" % (key) | |
while True: | |
pygame.event.pump() | |
ev = pygame.event.wait() | |
if ev.type in capture_events: | |
joy = self.joysticks[ ev.joy ] | |
if ev.type == pygame.JOYHATMOTION: | |
res = gethatcode(ev) | |
if self.debug: print "UNHANDLED JOY HAT code: ", res | |
elif ev.type == pygame.JOYBUTTONDOWN: | |
button = str(ev.button) | |
if joy.buttons.has_key(button): | |
key_code = joy.buttons[button] | |
if self.debug: print "Button down : " + key_code | |
key_press( key_code ) | |
elif ev.type == pygame.JOYBUTTONUP: | |
button = str(ev.button) | |
if joy.buttons.has_key(button): | |
key_code = joy.buttons[button] | |
if self.debug: print "Button up : " + key_code | |
key_release( key_code ) | |
elif ev.type == pygame.JOYAXISMOTION: | |
axis_number = str(ev.axis) | |
if joy.axis.has_key(axis_number): | |
axis = joy.axis[axis_number] | |
if ev.value < 0: | |
if self.debug: print "Button down : " + axis.low | |
key_press( axis.low ) | |
axis.low_active = True | |
elif ev.value > 0: | |
if self.debug: print "Button down : " + axis.high | |
key_press( axis.high ) | |
axis.high_active = True | |
else: | |
if axis.low_active: | |
if self.debug: print "Button up : " + axis.low | |
key_release( axis.low ) | |
axis.low_active = False | |
if axis.high_active: | |
if self.debug: print "Button up : " + axis.high | |
key_release( axis.high ) | |
axis.high_active = False | |
pygame.display.quit() | |
return res | |
def __del__(self): | |
import pygame | |
pygame.quit() | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# -*- coding: utf-8 -*- | |
import sys | |
import subprocess | |
from xml.dom.minidom import * | |
from time import sleep | |
# get the config file | |
if len(sys.argv) > 1: | |
conf = sys.argv[1] | |
# read config from xml doc | |
f = open(conf, "r") | |
xml = f.read() | |
f.close() | |
document = parseString(xml) | |
# Start joystick keys | |
from joystick import JKeys | |
j = JKeys(document) | |
j.debug = True | |
j.run() | |
while True: | |
sleep(1); | |
else: | |
print "usage: jkeys joystick.conf" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import threading | |
import time | |
def threaded(f): | |
def wrapper(*args): | |
t = threading.Thread(target=f, args=args) | |
t.setDaemon(True) # wont keep app alive | |
t.start() | |
return wrapper |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment