python-utilities / xjoypad

Python 2/3 evdev wrapper class for parsing Joypad input events.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

XJoypad

XJoypad is a Python2/3 iterator and wrapper API of evdev, for reading and parsing joypad and game-controller input events.

Byte size of init.py Open Issues Open Pull Requests Latest commits


Table of Contents


Quick Start

Bash Variables

_module_name='xjoypad'
_module_https_url="https://github.com/python-utilities/${_module_name}.git"
_module_relative_path="lib/modules/${_module_name}"

Bash Submodule Commands

cd "<your-git-project-path>"

git checkout master
mkdir -vp "lib/modules"

git submodule add\
 -b master --name "${_module_name}"\
 "${_module_https_url}" "${_module_relative_path}"

Your ReadMe File

Suggested additions for your ReadMe.md file so everyone has a good time with submodules

Install Python dependencies


    pip3 install --user evdev


Clone with the following to avoid incomplete downloads


    git clone --recurse-submodules <url-for-your-project>


Update/upgrade submodules via


    git submodule update --init --merge --recursive

Utilize XJoypad

As an import

#!/usr/bin/env python3


from lib.modules.xjoypad import XJoypad


if __name__ == '__main__':
    import time

    xjoypad = XJoypad()
    for event_data in xjoypad:
        if event_data:
            print("{name} --> {value} --> {normalized_value}".format(**event_data))

        time.sleep(0.001)

As a base class

#!/usr/bin/env python3


import time

from lib.modules.xjoypad import XJoypad


class XJoypad_Buffer(XJoypad):
    """Extends `XJoypad` class with buffer and timeout features"""

    def __init__(self, device_index = 0, amend_settings = None, **kwargs):
        """
        Use `amend_settings` to modify select `self['sleeps']` settings
        """
        self['sleeps'] = {
            'min': 0.001,
            'max': 0.01,
            'acceleration': 0.001,
            'timeout': 120,
            'current': 0.001,
        }

        super(XJoypad_Buffer, self).__init__(device_index = device_index, amend_settings = amend_settings, **kwargs)

    def next(self):
        """
        Throws `GeneratorExit` if timeout is set and reached, otherwise returns `event_data` when available
        """
        while 1:
            _event_data = super(XJoypad_Buffer, self).next()
            if _event_data:
                self['sleeps']['slept_start'] = None
                self['sleeps']['current'] = self['sleeps']['min']
                return _event_data

            if not self['sleeps'].get('slept_start'):
                self['sleeps']['slept_start'] = time.time()

            if self['sleeps']['current'] < self['sleeps']['max']:
                self['sleeps']['current'] += self['sleeps']['acceleration']

            if self['sleeps'].get('timeout') and self['sleeps'].get('slept_last'):
                if self['sleeps']['slept_last'] - self['sleeps']['slept_start'] > self['sleeps']['timeout']:
                    self.throw(GeneratorExit)

            self['sleeps']['slept_last'] = time.time()
            time.sleep(self['sleeps']['current'])


if __name__ == '__main__':
    xjoypad = XJoypad_Buffer()
    for event_data in xjoypad:
        print("Event name -> {name} -- {value} -- {normalized_value}".format(**event_data))
        time.sleep(0.001)

Commit and Push

git add .gitmodules
git add lib/modules/xjoypad


## Add any changed files too


git commit -F- <<'EOF'
:heavy_plus_sign: Adds `python-utilities/xjoypad#1` submodule

# ... anything else noteworthy
EOF


git push origin master

:tada: Excellent :tada: your repository is now ready to begin unitizing code from this project!


Notes

Pull Requests are welcome for fixing bugs and/or adding features.


License

Legal bits of Open Source software

XJoypad ReadMe documenting how things like this could be utilized
Copyright (C) 2019  S0AndS0

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published
by the Free Software Foundation; version 3 of the License.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU Affero General Public License for more details.

You should have received a copy of the GNU Affero General Public License
along with this program.  If not, see <https://www.gnu.org/licenses/>.

About

Python 2/3 evdev wrapper class for parsing Joypad input events.

License:GNU Affero General Public License v3.0


Languages

Language:Python 100.0%