Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added joystick.name accessor #73

Conversation

PsykomantaBrain
Copy link
Contributor

I've exposed joystick.name field to python. This allows scripts to manage devices by name.

Using the following python functions, you can store an accessor to a joystick device retrieved by its name (as seen in joy.cpl), so controller scripts can be made agnostic to device indices (which can easily change when devices are un/plugged).

def get_joystick_index(name, joysticks):
    i = 0
    for j in joysticks:
        if (j.name == name):
            return i
        i = i + 1
    return -1

def get_joystick_accessor(name, joysticks):
    index = get_joystick_index(name, joysticks)
    if (not(index == -1)):
        return lambda: joysticks[index] 
    else:
        return null

Finally, on the user script side, joystick accessors are used like this:

import harvutil

if (starting):
    global getSidePanel
    getSidePanel = harvutil.get_joystick_accessor("Saitek Side Panel Control Deck", joystick)

    global getPedals
    getPedals = harvutil.get_joystick_accessor("Saitek Pro Flight Rudder Pedals", joystick)

#steering
    vJoy[0].y = getPedals().zRotation * 16.4 + \
                (getSidePanel().xRotation * 16.4 * (1 + (0.7 * abs(getSidePanel().yRotation) / 1000))) 

    #accel/brakes
    vJoy[0].x = (getSidePanel().yRotation * 16.4 * (1 + (0.7 * abs(getSidePanel().xRotation) / 1000))) + \
                filters.mapRange(getPedals().y, -1000, 1000, 0, -16354) + \
                filters.mapRange(getPedals().x, -1000, 1000, 0, 16354)

in this example, the functions above are defined in a harvutil.py module

This is only made possible with this change to JoystickPlugin.cs, which exposes the instance name of joystick devices to python. Hopefully this change can be merged in without any problems, but feel free to tweak as needed for proper code style/consistency/sanity.

Cheers

@AndersMalmgren
Copy link
Owner

Ah, nice idea, I wish there was a elegant way of letting FreePIE do this for you. But not without rewriting alot of code. Le me think about the best FreePIE way of fixing this without breaking any current scripts etc.

edit: Your code will initialize and lock all joysticks to FreePIE process so there is a difference of doing this and doing joysticks[1]. The latter will only lock the last joystick

@AndersMalmgren
Copy link
Owner

Off course the best way is to implement a custom GlobalIndexer that takes both nam and index (string and int). I will have a look at this for you, sadly I do not have alot of time on my hand these days, But will look at it asap

@AndersMalmgren
Copy link
Owner

@AndersMalmgren
Copy link
Owner

Is my solution accepted over yours so that I can close this pull?

@PsykomantaBrain
Copy link
Contributor Author

Hi, yes, definitely! Go ahead. :)
On Tue, Jan 26, 2016 at 8:45 AM Anders Malmgren notifications@github.com
wrote:

Is my solution accepted over yours so that I can close this pull?


Reply to this email directly or view it on GitHub
#73 (comment)
.

@AndersMalmgren
Copy link
Owner

Thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants