Auto-centering only if near the center, and idle.

Official forum for open source FreePIE discussion and development.
Post Reply
User avatar
andreaspada
One Eyed Hopeful
Posts: 23
Joined: Thu Jul 13, 2017 10:06 am
Location: Rio de Janeiro
Contact:

Auto-centering only if near the center, and idle.

Post by andreaspada »

Hi to everyone! I'm just starting tweaking with FreePIE and vJoy. By now, I had success in simulate a simple joystick from my trackball.

Currently, this is my code:

Code: Select all

from System import Int16

if starting:
  system.setThreadTiming(TimingTypes.HighresSystemTimer)
  system.threadExecutionInterval = 5 # loop delay
  max   =  Int16.MaxValue*0.5+2
  min   = -Int16.MaxValue*0.5+1
  x     = 0.0
  y     = 0.0
  sen   = 25       # sensitivity  


x += mouse.deltaX * sen
y += mouse.deltaY * sen

if (x > max):
  x = max
elif (x < min):
  x = min

if (y > max):
  y = max
elif (y < min):
  y = min

# Centering
if keyboard.getKeyDown(Key.LeftControl): 
  x = 0
  y = 0

vJoy[1].x = x
vJoy[1].y = y

# Watch
diagnostics.watch(vJoy[1].x)
diagnostics.watch(vJoy[1].y)
It already auto-center the pointer when I press the Left Control key. However, I need some help in figure out how to implement an auto-centering that works only if I let the pointer inside a small defined circumference - near the center - and only if I let the pointer there idle for a while. Also, I want the auto-center to be smooth, not abrupt...
Any clue?
Jabberwock
Cross Eyed!
Posts: 196
Joined: Mon Mar 02, 2015 3:58 pm

Re: Auto-centering only if near the center, and idle.

Post by Jabberwock »

For centering this should be enough:

if (x < 10) and (x > 0):
x = x - 0.1

where 10 is the limit and 0.1 is a value small enough to provide gentle centering. You need four of those in the code - you can get it more elegant with checking for abs(x) > 10, but you get the idea...

I think that if it is gentle enough no idle check should be needed, but if it is then maybe something like:

if (abs(x) < 10) and (abs(y) < 10):
idlecheck = idlecheck + 1
else:
idlecheck = 0


Then maybe:

if (x < 10) and (x > 0) and (idlecheck > 1000):
x = x - 0.1


I have not tested it and I am an amateur, but I hope it works :)

Note that if you provide manual input and you are still within the area, the centering still occurs. It would be better to check for idleness of vjoy, but that depends on the devices - most of mine are spiking so much the detection would be worthless.
User avatar
andreaspada
One Eyed Hopeful
Posts: 23
Joined: Thu Jul 13, 2017 10:06 am
Location: Rio de Janeiro
Contact:

Re: Auto-centering only if near the center, and idle.

Post by andreaspada »

Wow, thanks for the super fast reply! I'll check your suggestions right now, and I let you know.
Going coding...
User avatar
andreaspada
One Eyed Hopeful
Posts: 23
Joined: Thu Jul 13, 2017 10:06 am
Location: Rio de Janeiro
Contact:

Re: Auto-centering only if near the center, and idle.

Post by andreaspada »

Ok, got some trials but problem is that, the way you proposed, it affect only positive values. I need a way to check for both positive or negative values ob both x an y. Do you know which command can I use for that?
User avatar
andreaspada
One Eyed Hopeful
Posts: 23
Joined: Thu Jul 13, 2017 10:06 am
Location: Rio de Janeiro
Contact:

Re: Auto-centering only if near the center, and idle.

Post by andreaspada »

Got it! Not too much elegant, but...

I define three variables at the starting:

Code: Select all

	speed = 1                                       # speed of auto centering
	pradius = 300                                 # radius
	nradius = pradius - (pradius *2)       # negative value of radius
Then, this:

Code: Select all

# Auto-centering
if (x < pradius) and (x > 0):
	x = x - speed
elif (x > nradius) and (x < 0):
	x = x + speed

if (y < pradius) and (y > 0):
	y = y - speed
elif (y > nradius) and (y < 0):
	y = y + speed
This way, if I want to change the behavior, I only need to modify the values in the headers.

Thanks for your help!
User avatar
andreaspada
One Eyed Hopeful
Posts: 23
Joined: Thu Jul 13, 2017 10:06 am
Location: Rio de Janeiro
Contact:

Re: Auto-centering only if near the center, and idle.

Post by andreaspada »

Here it is the current whole script. I plan to refine it, but it's working as expected. I added a toggle which enable a behavior known as Relative Mouse (I play Elite: Dangerous), which is an auto-centering system if idle. I tweaked it to be smooth enough for a space flight simulator.

Code: Select all

from System import Int16

if starting:
	system.setThreadTiming(TimingTypes.HighresSystemTimer)
	system.threadExecutionInterval = 5 # loop delay
	enabled = False
	max  	 =  Int16.MaxValue*0.5+2
	min   	 = -Int16.MaxValue*0.5+1
	x     	 = 0.0
	y     	 = 0.0
	a		 = 0
	b		 = 0
	c		 = 0
	d		 = 0
 	sensitivity = 25
	speed = 5							# auto-centering speed
	pradius = 900						# auto-centring radius
	nradius = pradius - (pradius *2)
	rm_speed = 200						# relative mouse speed
	
	
# pitch
y += mouse.deltaY * sensitivity
# yaw
x += mouse.deltaX * sensitivity

if (x > max):
  x = max
elif (x < min):
  x = min

if (y > max):
  y = max
elif (y < min):
  y = min

#####################
# Manual centering
#####################
if keyboard.getKeyDown(Key.LeftControl): 
  x = 0
  y = 0


##########################################
# Auto-centering if close to the center
##########################################

if (x < pradius) and (x > 0):
	x = x - speed
elif (x > nradius) and (x < 0):
	x = x + speed

if (y < pradius) and (y > 0):
	y = y - speed
elif (y > nradius) and (y < 0):
	y = y + speed
	
###########################
# toggle relative mouse
###########################

toggle = keyboard.getPressed(Key.RightControl)

if toggle:
    enabled = not enabled


##############################################
# relative mouse (auto centering when idle)
##############################################

if (enabled):
	a += mouse.deltaX
	b += mouse.deltaX
	if filters.stopWatch(True,600):
		c = a + 0
   
	if filters.stopWatch(True,30):
		d = b + 0

	if (c - d == 0):
		if x < -500:
			x += rm_speed
		elif x > 500:
			x -= rm_speed
		if y < -500:
			y += rm_speed
		elif y > 500:
			y -= rm_speed

# --- # --- # --- # --- # --- # --- # --- #
 
# mouse to vJoy
vJoy[1].x = x
vJoy[1].y = y

# Watch
diagnostics.watch(vJoy[1].x)
diagnostics.watch(vJoy[1].y)
diagnostics.watch(enabled)
Post Reply

Return to “FreePIE”