Example #1
0
 def __init__(self):
     
     self._isRunning = False
     self._pollingThread = None
     
     self._joysticks = []
     
     #Events        
     self.onStart = EventHook()
     self.onStop = EventHook()
Example #2
0
 def __init__(self):
     
     self._isRunning = False
     self._pollingThread = None
     
     self._joysticks = []
     
     #Events        
     self.onStart = EventHook()
     self.onStop = EventHook()
Example #3
0
 def __init__(self, name, index, numAxes, numButtons, numHats):
 
     self._name = name #Name of the product
     self._index = index #Pygame's device index
     
     self._axes = [0.0]*numAxes
     self._buttons = [0]*numButtons
     self._hats = [0.0]*numHats
     
     #Events
     self.onChanged = EventHook()
     
     self.onAxisChanged = EventHook()
     
     self.onButtonChanged = EventHook()
     self.onButtonPressed = EventHook()
     self.onButtonReleased = EventHook()
     
     self.onHatChanged = EventHook()
     self.onHatPressedNegative = EventHook()
     self.onHatPressedPositive = EventHook()
     self.onHatReleased = EventHook()
Example #4
0
    def __init__(self, name, index, numAxes, numButtons, numHats):

        self._name = name  #Name of the product
        self._index = index  #Pygame's device index

        self._axes = [0.0] * numAxes
        self._buttons = [0] * numButtons
        self._hats = [0.0] * numHats

        #Events
        self.onChanged = EventHook()

        self.onAxisChanged = EventHook()

        self.onButtonChanged = EventHook()
        self.onButtonPressed = EventHook()
        self.onButtonReleased = EventHook()

        self.onHatChanged = EventHook()
        self.onHatPressedNegative = EventHook()
        self.onHatPressedPositive = EventHook()
        self.onHatReleased = EventHook()
Example #5
0
class Joystick(object):
    '''
    Throws events depending the user behavior
    '''

    BUTTON_X = 2
    BUTTON_Y = 3
    BUTTON_A = 0
    BUTTON_B = 1

    BUTTON_LB = 4
    BUTTON_RB = 5

    BUTTON_BACK = 6
    BUTTON_START = 7

    AXIS_LEFT_H = 0
    AXIS_LEFT_V = 1

    AXIS_RIGHT_H = 4
    AXIS_RIGHT_V = 3

    AXIS_FRONT = 2

    #Below this threshold, the axis value is zero.
    _AXIS_ZERO_THRESHOLD = 1.0

    def __init__(self, name, index, numAxes, numButtons, numHats):

        self._name = name  #Name of the product
        self._index = index  #Pygame's device index

        self._axes = [0.0] * numAxes
        self._buttons = [0] * numButtons
        self._hats = [0.0] * numHats

        #Events
        self.onChanged = EventHook()

        self.onAxisChanged = EventHook()

        self.onButtonChanged = EventHook()
        self.onButtonPressed = EventHook()
        self.onButtonReleased = EventHook()

        self.onHatChanged = EventHook()
        self.onHatPressedNegative = EventHook()
        self.onHatPressedPositive = EventHook()
        self.onHatReleased = EventHook()

    def getName(self):

        return self._name

    def getIndex(self):

        return self._index

    def _setAxisValue(self, index, value):

        self._axes[index] = value

        self.onChanged.fire(self)
        self.onAxisChanged.fire(self, index)

    def axesCount(self):

        return len(self._axes)

    def getAllAxisValues(self):

        return [Joystick._calculateAxisValue(value) for value in self._axes]

    def getAxisValue(self, index):

        return Joystick._calculateAxisValue(self._axes[index])

    @staticmethod
    def _calculateAxisValue(rawValue):

        axisValue = rawValue * rawValue * rawValue * 100.0
        if axisValue < Joystick._AXIS_ZERO_THRESHOLD and axisValue > -Joystick._AXIS_ZERO_THRESHOLD:
            axisValue = 0.0

        return axisValue

    def _setButtonValue(self, index, value):

        self._buttons[index] = value

        self.onChanged.fire(self)
        self.onButtonChanged.fire(self, index)
        if self.isButtonPressed(index):
            self.onButtonPressed.fire(self, index)
        else:
            self.onButtonReleased.fire(self, index)

    def buttonsCount(self):

        return len(self._buttons)

    def getAllButtonValues(self):

        return [value != 0 for value in self._buttons]

    def isButtonPressed(self, index):

        return self._buttons[index] != 0

    def _setHatValue(self, index, value):

        self._hats[index] = value

        self.onChanged.fire(self)
        self.onHatChanged.fire(self, index)

        if isinstance(self._hats[index], (int)):

            if self._hats[index] < 0:
                self.onHatPressedNegative.fire(self, index)
            elif self._hats[index] > 0:
                self.onHatPressedPositive.fire(self, index)
            else:
                self.onHatReleased.fire(self, index)

    def hatsCount(self):

        return len(self._hats)

    def getAllHatValues(self):

        return self._hats

    def getHatValue(self, index):

        return self._hats[index]
Example #6
0
class Joystick(object):
    '''
    Throws events depending the user behavior
    '''
    
    #Below this threshold, the axis value is zero. 
    AXIS_ZERO_THRESHOLD = 1.0

    def __init__(self, name, index, numAxes, numButtons, numHats):
    
        self._name = name #Name of the product
        self._index = index #Pygame's device index
        
        self._axes = [0.0]*numAxes
        self._buttons = [0]*numButtons
        self._hats = [0.0]*numHats
        
        #Events
        self.onChanged = EventHook()
        
        self.onAxisChanged = EventHook()
        
        self.onButtonChanged = EventHook()
        self.onButtonPressed = EventHook()
        self.onButtonReleased = EventHook()
        
        self.onHatChanged = EventHook()
        self.onHatPressedNegative = EventHook()
        self.onHatPressedPositive = EventHook()
        self.onHatReleased = EventHook()

        
    def getName(self):
        
        return self._name
    
    
    def getIndex(self):
        
        return self._index
    
    
    def _setAxisValue(self, index, value):
        
        self._axes[index] = value
        
        self.onChanged.fire(self)
        self.onAxisChanged.fire(self, index)
        
    
    def axesCount(self):
        
        return len(self._axes)
    
    
    def getAllAxisValues(self):
        
        return [Joystick._calculateAxisValue(value) for value in self._axes]
    
    
    def getAxisValue(self, index):
        
        return Joystick._calculateAxisValue(self._axes[index])
    
    
    @staticmethod
    def _calculateAxisValue(rawValue):
        
        axisValue = rawValue * rawValue * rawValue * 100.0
        if axisValue < Joystick.AXIS_ZERO_THRESHOLD and axisValue > -Joystick.AXIS_ZERO_THRESHOLD:
            axisValue = 0.0
            
        return axisValue                  
        
    
    def _setButtonValue(self, index, value):
        
        self._buttons[index] = value
        
        self.onChanged.fire(self)
        self.onButtonChanged.fire(self, index)
        if self.isButtonPressed(index):
            self.onButtonPressed.fire(self, index)
        else:
            self.onButtonReleased.fire(self, index)
        
    
    def buttonsCount(self):
        
        return len(self._buttons)
    
    
    def getAllButtonValues(self):
        
        return [value != 0 for value in self._buttons]
    
    
    def isButtonPressed(self, index):
        
        return self._buttons[index] != 0
    
    
    def _setHatValue(self, index, value):
        
        self._hats[index] = value
        
        self.onChanged.fire(self)
        self.onHatChanged.fire(self, index)
        if self._hats[index] < 0:
            self.onHatPressedNegative.fire(self, index)
        elif self._hats[index] > 0:
            self.onHatPressedPositive.fire(self, index)
        else:
            self.onHatReleased.fire(self, index)
        
    
    def hatsCount(self):
        
        return len(self._hats)
    
    
    def getAllHatValues(self):
        
        return self._hats
    
    
    def getHatValue(self, index):
        
        return self._hats[index]
Example #7
0
class JoystickManager(object):
    '''
    Implements the device update loop.
    '''
    
    _instance = None
    
    @staticmethod
    def getInstance():
        
        if JoystickManager._instance == None:            
            JoystickManager._instance = JoystickManager()
            
        return JoystickManager._instance 
    
    @contextmanager
    def suppressStdout(self):
        old_stdout = sys.stdout
        with open(os.devnull, "w") as devnull:        
            sys.stdout = devnull
            try:  
                yield
            finally:
                sys.stdout = old_stdout
                
    
    def __init__(self):
        
        self._isRunning = False
        self._pollingThread = None
        
        self._joysticks = []
        
        #Events        
        self.onStart = EventHook()
        self.onStop = EventHook()


    def start(self):
        
        if _pygameLoaded and (self._pollingThread == None or not self._pollingThread.isAlive()):
            
            # Initialize the joysticks
            pygame.init()
            pygame.joystick.init()

            joystickCount = pygame.joystick.get_count()
            
            if joystickCount != 0:
                        
                for joystickIndex in range(joystickCount):
                    joystick = pygame.joystick.Joystick(joystickIndex)
                    joystick.init()
                
                    #Get device parameters
                    name = joystick.get_name()            
                    axes = joystick.get_numaxes()
                    buttons = joystick.get_numbuttons()
                    hats = joystick.get_numhats()
                    
                    self._joysticks += [Joystick(name, joystickIndex, axes, buttons, hats)]
                
                #Init thread
                self._isRunning = True
                self._pollingThread = Thread(target=self._doPolling)
                self._pollingThread.start()
            
            self.onStart.fire(self)
        

    def stop(self):
        
        self._isRunning = False
        if self._pollingThread != None and self._pollingThread.isAlive():            
            self._pollingThread.join()            
            
            self.onStop.fire(self)            
            pygame.quit()


    def getJoysticks(self):
        
        return self._joysticks

        
    def _doPolling(self):
        
        while self._isRunning:
            
            for event in pygame.event.get(): # User did something
                if event.type == pygame.QUIT: # If user clicked close
                    self._isRunning=False            
            
            for joystick in self._joysticks:
                
                #Read core data
                with self.suppressStdout():
                    pygameJoystick = pygame.joystick.Joystick(joystick.getIndex())                    

                #Axes                
                for axisIndex in range(joystick.axesCount()):
                    with self.suppressStdout():
                        axisValue = pygameJoystick.get_axis(axisIndex)                       
                    
                    if joystick._axes[axisIndex] != axisValue:                        
                        joystick._setAxisValue(axisIndex, axisValue)
                                         
                    
                #Buttons
                for buttonIndex in range(joystick.buttonsCount()):
                    with self.suppressStdout():
                        buttonValue = pygameJoystick.get_button(buttonIndex)
                    if joystick._buttons[buttonIndex] != buttonValue:
                        joystick._setButtonValue(buttonIndex, buttonValue)                        

                #Hats                    
                for hatIndex in range(joystick.hatsCount()):
                    with self.suppressStdout():
                        hatValue = pygameJoystick.get_hat(hatIndex)
                    if joystick._hats[hatIndex] != hatValue:
                        joystick._setHatValue(hatIndex, hatValue)
                        
            sleep(0.1)
Example #8
0
class JoystickManager(object):
    '''
    Implements the device update loop.
    '''
    
    _instance = None
    
    @staticmethod
    def getInstance():
        
        if JoystickManager._instance == None:            
            JoystickManager._instance = JoystickManager()
            
        return JoystickManager._instance 
    
    @contextmanager
    def suppressStdout(self):
        old_stdout = sys.stdout
        with open(os.devnull, "w") as devnull:        
            sys.stdout = devnull
            try:  
                yield
            finally:
                sys.stdout = old_stdout
                
    
    def __init__(self):
        
        self._isRunning = False
        self._pollingThread = None
        
        self._joysticks = []
        
        #Events        
        self.onStart = EventHook()
        self.onStop = EventHook()


    def start(self):
        
        if _pygameLoaded and (self._pollingThread == None or not self._pollingThread.isAlive()):
            
            # Initialize the joysticks
            pygame.init()
            pygame.joystick.init()

            joystickCount = pygame.joystick.get_count()
            
            if joystickCount != 0:
                        
                for joystickIndex in range(joystickCount):
                    joystick = pygame.joystick.Joystick(joystickIndex)
                    joystick.init()
                
                    #Get device parameters
                    name = joystick.get_name()            
                    axes = joystick.get_numaxes()
                    buttons = joystick.get_numbuttons()
                    hats = joystick.get_numhats()
                    
                    self._joysticks += [Joystick(name, joystickIndex, axes, buttons, hats)]
                
                #Init thread
                self._isRunning = True
                self._pollingThread = Thread(target=self._doPolling)
                self._pollingThread.start()
            
            self.onStart.fire(self)
        

    def stop(self):
        
        self._isRunning = False
        if self._pollingThread != None and self._pollingThread.isAlive():            
            self._pollingThread.join()            
            
            self.onStop.fire(self)            
            pygame.quit()


    def getJoysticks(self):
        
        return self._joysticks

        
    def _doPolling(self):
        
        while self._isRunning:
            
            for event in pygame.event.get(): # User did something
                if event.type == pygame.QUIT: # If user clicked close
                    self._isRunning=False            
            
            for joystick in self._joysticks:
                
                #Read core data
                with self.suppressStdout():
                    pygameJoystick = pygame.joystick.Joystick(joystick.getIndex())                    

                #Axes                
                for axisIndex in range(joystick.axesCount()):
                    with self.suppressStdout():
                        axisValue = pygameJoystick.get_axis(axisIndex)                       
                    
                    if joystick._axes[axisIndex] != axisValue:                        
                        joystick._setAxisValue(axisIndex, axisValue)
                                         
                    
                #Buttons
                for buttonIndex in range(joystick.buttonsCount()):
                    with self.suppressStdout():
                        buttonValue = pygameJoystick.get_button(buttonIndex)
                    if joystick._buttons[buttonIndex] != buttonValue:
                        joystick._setButtonValue(buttonIndex, buttonValue)                        

                #Hats                    
                for hatIndex in range(joystick.hatsCount()):
                    with self.suppressStdout():
                        hatValue = pygameJoystick.get_hat(hatIndex)
                    if joystick._hats[hatIndex] != hatValue:
                        joystick._setHatValue(hatIndex, hatValue)
                        
            sleep(0.02)