threers

vjoy+FreePIE mouse steering with mouse lock

Aug 1st, 2018
2,926
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 6.52 KB | None | 0 0
  1. from ctypes import *
  2. user32 = windll.user32
  3.  
  4. if starting:
  5.     # feature toggle
  6.     vjoyaxis = True
  7.     assists = False # auto throttle cut off & blip
  8.     mouselock = False # locking mouse position for assetto corsa
  9.     debouncing = True # prevent double shifting, set False to disable
  10.     # assign vjoy device number
  11.     v = vJoy[0]
  12.     # vjoy axis range, do not modify
  13.     a_max = 1 + v.axisMax
  14.     a_min = -1 - v.axisMax
  15.     # mouse steering
  16.     m_sens = 16.0 # mouse sensitivity (higher faster)
  17.     m_redu = 4 # center reduction sensitivity, acceptable range 1-50, set to 1 to disable
  18.     steering = 0 # do not modify
  19.     center_redu = 1 # center reduction; init value, do not modify
  20.     # throttle
  21.     th_axis = a_min
  22.     th_inc = 240 # increase speed (higher faster)
  23.     th_dec = 300 # decrease speed
  24.     th_max = a_max # for throttle limit
  25.     # brake
  26.     br_axis = a_min
  27.     br_inc = 240
  28.     br_dec = 300
  29.     br_max = a_max # for brake limit
  30.     # handbrake
  31.     ha_axis = a_min
  32.     ha_inc = 300
  33.     ha_dec = 600
  34.     # clutch
  35.     cl_axis = a_min
  36.     cl_inc = 2000
  37.     cl_dec = 600
  38.     # auto blip
  39.     blip_m = 0.4 # amount throttle applied on blip; range 0.0-1.0; 0.0=0% throttle, 1.0=100%
  40.     a_blip = a_max * 2 * blip_m - a_max # calculation, do not modify
  41.     # debouncing
  42.     stimer = 0 # shifting timer
  43.     t_upshift = 140 # minimum time required for next gear
  44.     # throttle limit
  45.     tlimit = 0.9 # throttle limited at 90%; range 0.0-1.0; useful for cars without traction control under low gears
  46.     th_limit = a_max * 2 * tlimit - a_max # calculation, do not modify
  47.  
  48. #======== assign key here ========#
  49. # toggle
  50. toggle_vjoyaxis = keyboard.getPressed(Key.Grave) # axis calculation
  51. toggle_mouselock = mouse.getPressed(3) # mouselock
  52. key_assists_on = keyboard.getKeyDown(Key.NumberPad1) or mouse.wheelUp
  53. key_assists_off = keyboard.getKeyDown(Key.NumberPad3) or mouse.wheelDown
  54. # vehicle control
  55. key_throttle = keyboard.getKeyDown(Key.K)
  56. key_brake = keyboard.getKeyDown(Key.L)
  57. key_handbrake = keyboard.getKeyDown(Key.Z)
  58. key_clutch = keyboard.getKeyDown(Key.X)
  59. key_centerx = mouse.getButton(2) # center steering (x-axis)
  60. key_shiftup = keyboard.getKeyDown(Key.J)
  61. key_shiftdown = mouse.getButton(1)
  62. # brake limit
  63. bl_70 = keyboard.getPressed(Key.NumberPad7)
  64. bl_75 = keyboard.getPressed(Key.NumberPad4)
  65. bl_80 = keyboard.getPressed(Key.NumberPad8)
  66. bl_85 = keyboard.getPressed(Key.NumberPad5)
  67. bl_90 = keyboard.getPressed(Key.NumberPad9)
  68. bl_95 = keyboard.getPressed(Key.NumberPad6)
  69. no_bl = keyboard.getPressed(Key.NumberPadPeriod)
  70. # throttle limit
  71. key_throttle_limit = mouse.getButton(0) # throttle limit is only applied while holding down assign key
  72.  
  73. #======== toggle ========#
  74. if toggle_vjoyaxis:
  75.     vjoyaxis = not vjoyaxis
  76. if toggle_mouselock:
  77.     mouselock = not mouselock
  78. if key_assists_on:
  79.     assists = True
  80. if key_assists_off:
  81.     assists = False
  82.  
  83. #======== mouselock ========#
  84. if (mouselock):
  85.     user32.SetCursorPos(0 , 5000) # pixel coordinates (x, y)
  86.  
  87. #======== axis calculation ========#
  88. if (vjoyaxis):
  89.     # mouse steering
  90.     if steering > 0:
  91.         center_redu = m_redu ** (1 - (steering / a_max))
  92.     elif steering < 0:
  93.         center_redu = m_redu ** (1 - (steering / a_min))
  94.     steering += (mouse.deltaX * m_sens) / center_redu
  95.     if steering > a_max:
  96.         steering = a_max
  97.     elif steering < a_min:
  98.         steering = a_min
  99.     if key_centerx:
  100.         steering = 0
  101.     # throttle axis
  102.     if key_throttle:
  103.         th_axis += th_inc
  104.     else:
  105.         th_axis -= th_dec
  106.     if th_axis > th_max:
  107.         th_axis = th_max
  108.     elif th_axis < a_min:
  109.         th_axis = a_min
  110.     # brake axis
  111.     if key_brake:
  112.         br_axis += br_inc
  113.     else:
  114.         br_axis -= br_dec
  115.     if br_axis > br_max:
  116.         br_axis = br_max
  117.     elif br_axis < a_min:
  118.         br_axis = a_min
  119.     # handbrake axis
  120.     if key_handbrake:
  121.         ha_axis += ha_inc
  122.     else:
  123.         ha_axis -= ha_dec
  124.     if ha_axis > a_max:
  125.         ha_axis = a_max
  126.     elif ha_axis < a_min:
  127.         ha_axis = a_min
  128.     # clutch axis
  129.     if key_clutch:
  130.         cl_axis += cl_inc
  131.     else:
  132.         cl_axis -= cl_dec
  133.     if cl_axis > a_max:
  134.         cl_axis = a_max
  135.     elif cl_axis < a_min:
  136.         cl_axis = a_min
  137.     # assists switch
  138.     if (assists):
  139.         if key_shiftup:
  140.             th_axis = a_min # throttle cut off while upshifting
  141.         if key_shiftdown:
  142.             th_axis = a_blip # throttle blip while downshifting
  143.     # brake limit
  144.     if bl_70:
  145.         br_max = a_max * 0.4
  146.     if bl_75:
  147.         br_max = a_max * 0.5
  148.     if bl_80:
  149.         br_max = a_max * 0.6
  150.     if bl_85:
  151.         br_max = a_max * 0.7
  152.     if bl_90:
  153.         br_max = a_max * 0.8
  154.     if bl_95:
  155.         br_max = a_max * 0.9
  156.     if no_bl:
  157.         br_max = a_max
  158.     if key_throttle_limit:
  159.         th_max = th_limit
  160.     else:
  161.         th_max = a_max
  162. else: # reset axis position
  163.     steering = 0
  164.     th_axis = a_min
  165.     br_axis = a_min
  166.     ha_axis = a_min
  167.     cl_axis = a_min
  168.  
  169. #======== map vjoy axis & button ========#
  170. v.x = int(round(steering))
  171. v.y = th_axis
  172. v.z = br_axis
  173. v.ry = ha_axis
  174. v.rx = cl_axis
  175. v.setButton(1,key_shiftdown)
  176. v.setButton(2,keyboard.getKeyDown(Key.Q))
  177. v.setButton(3,keyboard.getKeyDown(Key.W)) # add new vjoy buttons below
  178.  
  179. #======== double shifting prevention ========#
  180. if (debouncing):
  181.     if key_shiftup:
  182.         stimer = 0
  183.     elif stimer < t_upshift: # end timer on reaching minimum time
  184.         stimer += 1 # start timer on releasing shift button
  185.         current = stimer
  186.     if key_shiftup and (current >= t_upshift):
  187.         v.setButton(0,key_shiftup)
  188.     else:
  189.         v.setButton(0, False)
  190. else:
  191.     v.setButton(0,key_shiftup)
  192.  
  193. #======== diagnostics ========#
  194. # important note: diagnostics has big impact on cpu usage (about 50-80% more)
  195. # keep this section commented out, only uncomment for coding and testing
  196. #diagnostics.watch(v.x) # steering
  197. #diagnostics.watch(v.y) # throttle
  198. #diagnostics.watch(v.z) # brake
  199. #diagnostics.watch(v.ry)    # handbrake
  200. #diagnostics.watch(v.rx)    # clutch
  201. #diagnostics.watch(v.axisMax)   # vjoy axis max range
  202. #diagnostics.watch(stimer) # shifting timer
  203.  
  204. #======== reference & example ========#
  205. # vjoy axis: x, y, z, rx, ry, rz, slider, dial
  206. # keyboard assign: keyboard.getKeyDown(Key.A); keyboard.getPressed(Key.A)
  207. # mouse button assign: mouse.getButton(0); mouse.getPressed(1)
  208. # mouse button number: 0 = leftbutton; 1 = rightbutton; 2 = middlebutton; etc.
  209. # to execute an action after pressed a key or clicked mouse button, use: keyboard.getPressed() or mouse.getPressed()
  210. # to execute an action continuously while holding down a key or mouse button, use: keyboard.getKeyDown() or mouse.getButton()
  211.  
  212. #======== credits ========#
  213. # most codes of "axis calculation" are borrowed from "Skagen", and others found in https://www.lfs.net/forum/post/1862759
  214. # the codes for locking mouse are from https://bytes.com/topic/python/answers/21158-mouse-control-python
  215. # misc codes and formating by threers
  216. # last update: 2018-08-10
Add Comment
Please, Sign In to add comment