Aka Joystick support in Starbase (there maybe "normal" apps on internet)

Erador

Well-known endo
Joined
Sep 2, 2021
Messages
65
#1
So, I've decided to return to my old project. Here is the my own Python code that I've done for my 'Logitech Extreme 3D Pro' Joystick.
Idk if it's ez to do with the JoystickToMoyse like applications, but it was interesting for me to do it myself.
What it does: Moving the mouse on the screen within the 'Mouse Control' mode borders on a ship

Code:
import hid

import pynput.keyboard as pk

import pynput.mouse as pm

import numpy as np

import time as t



c = pk.Controller()

k = pk.Key

m = pm.Controller()

b = pm.Button

gamepad = hid.device()

gamepad.open(0x046d,0xc215)

gamepad.set_nonblocking(True)

# SETTINGS

'''''' # Joystick

Joystick_X=[0,1020]# Joystick Min/Max

#Joystick_Y=[0,1020]

Joystick_Y=[0,4080]

Joystick_DZ_X=[500,520]# borders of deadzone (ignore input)

Joystick_DZ_Y=[2000,2080]

#Joystick_LI_X=[350,670]# borders of lowest input (in this zone input will have min value)

#Joystick_LI_Y=[350,670]

'''''' # Starbase 'c' dimensions

Mouse_DZ_X=[1230,1330]# borders of deadzone

Mouse_DZ_Y=[670,770]

mLI_Left = Mouse_DZ_X[0]-1

mLI_Right = Mouse_DZ_X[1]+1

mLI_Up = Mouse_DZ_Y[0]-1

mLI_Down = Mouse_DZ_Y[1]+1

Mouse_Center=[1280,720]

Mouse_B_X=[776,1784]# borders of interface

Mouse_B_Y=[216,1224]

''''''

# Internal variables

lastData = [0,0,0,0,0,0]

tStamp = t.time()#time stamp

tDelta = .016# delta time between loops (~57.6fps)

''''''

p = True # Needed for Pause

pSwitch = 0

''''''

_X = 2 # Needed for transition [Transition is a jump from 0 to 255 or from 255 to 0]

_Y = 8

''''''

xK = 0 # Joystick_X / Mouse_X (or via versa) ratio Coefficient. xK & yK = [left, right].

yK = 0

''''''

Left_m = Mouse_DZ_X[0] - Mouse_B_X[0] # Actual movement zone of pointer inside the interface

Right_m = Mouse_B_X[1] - Mouse_DZ_X[1]

Up_m = Mouse_DZ_Y[0] - Mouse_B_Y[0]

Down_m = Mouse_B_Y[1] - Mouse_DZ_Y[1]

Left_jck = Joystick_DZ_X[0] - Joystick_X[0] # Actual movement zone of joystick

Right_jck = Joystick_X[1] - Joystick_DZ_X[1]

Up_jck = Joystick_DZ_Y[0] - Joystick_Y[0]

Down_jck = Joystick_Y[1] - Joystick_DZ_Y[1]

''''''

# Internal functions

def getCurve (x):

    a=.9

#   a*x^{3}+(1-a)*x

  

    return (a*x*x*x + (1-a)*x)

# -------------------------------------------------------------------------



#input()



#print("\n\n Calibrating Finished")

while True:

  

    #if t.time() - tStamp > tDelta:

    data = gamepad.read(64)

    if len(data)==7:# Check if data is correct

        # X-axis Watch

        if abs(data[0]-lastData[0]) > 128:

            # it was a transition

            if data[0]>128:

                #to left

                _X-=1

            else:

                #to right

                _X+=1

        # Y-axis Watch

        if abs(data[1]-lastData[1]) > 128:

            # it was a transition

            if data[1]>128:

                #to left

                _Y-=1

            else:

                #to right

                _Y+=1

      

        #:::::::::::::::::::::::::::::::::::::::::::::::::::::::::

        # Pause

        if data[6]==2:           

            if data[6] != lastData[6]:

                if pSwitch > 0:

                    if p:

                        p = False

                        print("\n || Paused")

                        lastData = data

                    else:

                        p = True

                        print("\n[>] Resumed")

                    pSwitch = 0

                else:

                    pSwitch += 1

            lastData = data

        data[4] = '{0:08b}'.format(data[4])

        data[6] = '{0:08b}'.format(data[6])

        xJck = data[0]+255*_X

        #yJck = (data[1]/2+255*_Y/2)+254-127

        yJck = data[1]+255*_Y

        print(" X: "+str(xJck)+"    Y: "+str(yJck))



        if p:

            #:::::::::::::::::::::::::::::::::::::::::::::::::::::::::

              

            # Loop itself



            # X mouse move

            if xJck > Joystick_DZ_X[0]:

                if xJck > Joystick_DZ_X[1]:

                    # right movement zone

                    #x = Mouse_DZ_X[1] + Right_m * ((xJck-Joystick_LI_X[1])/Right_jck)

                    x = Mouse_DZ_X[1] + Right_m * getCurve(((xJck-Joystick_DZ_X[1])/Right_jck))

                else:

                    # DEAD ZONE

                    x = Mouse_Center[0]

            else:

                # left movement zone

                x = Mouse_B_X[0] + Left_m * getCurve(xJck/Left_jck)

            # Y mouse move

            if yJck > Joystick_DZ_Y[0]:

                if yJck > Joystick_DZ_Y[1]:

                    # down movement zone

                    #y = Mouse_DZ_Y[1] + Down_m * ((yJck-Joystick_LI_Y[1])/Down_jck)

                    y = Mouse_DZ_Y[1] + Down_m * getCurve((yJck-Joystick_DZ_Y[1])/Down_jck)

                  

                    #y = Mouse_DZ_Y[0] - Up_m * ((yJck-Joystick_LI_Y[1])/Down_jck)

                else:

                    # DEAD ZONE

                    y = Mouse_Center[1]

            else:

                # up movement zone

                y = Mouse_B_Y[0] + Up_m * getCurve(yJck/Up_jck)

                #y = Mouse_B_Y[1] - Down_m * (yJck/Up_jck)



            m.position = (x,y)

            lastData = data



            # Shoot

            if data[4][7]=='1':

                c.press(k.space)

            else:

                c.release(k.space)

            # ROLL

            #Q

            if data[4][0]=='1':

                c.press('q')

            else:

                c.release('q')

            # ROLL

            #E

            if data[6][4]=='1':

                c.press('e')

            else:

                c.release('e')



            # Shift

            if data[4][1]=='1':

                c.press(k.shift)

            else:

                c.release(k.shift)

      

            # AIM

            if data[4][6]=='1':

                c.press('p')

            else:

                c.release('p')
 
Top