FriePie using a lot of CPU. Anyway I can decrease it?

Official forum for open source FreePIE discussion and development.
Post Reply
Danmoer
One Eyed Hopeful
Posts: 5
Joined: Fri Dec 15, 2017 5:32 pm

FriePie using a lot of CPU. Anyway I can decrease it?

Post by Danmoer »

Hey good people.

I have been using FreePie to hook into my TrackIr to enable leaning (Q and E keypress) in games that don't support TrackIr natively.

But it, or the script I am using, uses way too much CPU power :(
Up to 14% on my i5 2500k (OC)

Does anyone have any tips to share on how to reduce it? I would really appreciate it.
I've already commented out the 'diagnostics.watch', but that doesn't make a noticeable difference.

Code: Select all

# FreePIE TrackIR AxisToKey
#
# Author: loenze (https://github.com/loenze)
# Bind axis value beyond given threshold to key.
#
# I wrote this for leaning in Squad,
# but the script should work in any game with any action.
#
# SQUAD forums discussion: http://goo.gl/mVh6E7
#
# To configure your key binds simply add config lists with the according parameters
# to the AxisToKeyConfig list at line 31. You may add as many as you wish there.
#
# AxisToKeyConfig = [
#     [trackIR.roll, Key.Q, -50],
#     [trackIR.roll, Key.E, 50]
# ]
#
# This example consists of two bindings for the TrackIR roll axis.
# If the head is tilted to the left beyond a value of -50, the U key will be pressed.
# Likewise for tilting to the right beyond a value of 50 and the I key.
#
# Possible axis values are: trackIR.x, trackIR.y, trackIR.z, trackIR.pitch, trackIR.roll, trackIR.yaw
# Available keys can be found here: https://github.com/AndersMalmgren/FreePIE/wiki/Reference
#
# To find the right threshold values for you, you might want to turn on debugging for
# FreePIE's Watch window by uncommenting line 77.

# CONFIGURATION
# Add lists consisting of [axis, key, threshold]
AxisToKeyConfig = [
    [trackIR.roll, Key.Q, -50],
    [trackIR.roll, Key.E, 50]
]

class AxisToKey:
    def __init__(self, axis, key, threshold):
        self.axis      = axis
        self.key       = key
        self.threshold = threshold
        self.type      = 'pos' if threshold > 0 else 'neg'
        
    def press(self):
        keyboard.setKeyDown(self.key)
    
    def release(self):
        keyboard.setKeyUp(self.key)
        
    def over_threshold(self):
        if self.type == 'pos':
            return False if self.axis < self.threshold else True
        else:
            return False if self.axis > self.threshold else True
    
    def update(self):
        self.press() if self.over_threshold() else self.release()

AxisToKeyBindings = [AxisToKey(*c) for c in AxisToKeyConfig]

class Debug:
    info = ''
    
    @staticmethod
    def read(binding):
        Debug.info += """
Axis\t""" + str(binding.axis) + """
Thres\t""" + str(binding.threshold) + """
Key\t""" + str(binding.key) + """
Status\t""" + ('PRESSED' if keyboard.getKeyDown(binding.key) else '') + """
        """
#        diagnostics.watch(Debug.info)

def update():
    for binding in AxisToKeyBindings:
        binding.update()
        # UNCOMMENT FOLLOWING LINE FOR DEBUGGING
        #Debug.read(binding)

if starting:
    trackIR.update += update

Danmoer
One Eyed Hopeful
Posts: 5
Joined: Fri Dec 15, 2017 5:32 pm

Re: FriePie using a lot of CPU. Anyway I can decrease it?

Post by Danmoer »

OK, I think I understand now that it is the python script simply going as fast as it can in a loop. So have been googling about that.
I have been seeing things like import:time and time.sleep(x) come by, but have not been able to get it to work in this script. and don't know if that's the right solution in this case.

Can anyone here comment?

EDIT: I guess maybe lowering the priority of the process taking all the CPU power (FreePIE.WorkerHost.TrackIRWorker) may be the best bet. That would make sure it would at least always lose out to the game needing the CPU power.

Danmoer
One Eyed Hopeful
Posts: 5
Joined: Fri Dec 15, 2017 5:32 pm

Re: FriePie using a lot of CPU. Anyway I can decrease it?

Post by Danmoer »

OK no additional tips I guess.

But for anyone reading this in the future, my priority solution so far seems to work well.
I wonder how trackit did it so their process takes only max 1% though.

polhoney
One Eyed Hopeful
Posts: 2
Joined: Thu Nov 30, 2017 2:07 pm

Re: FriePie using a lot of CPU. Anyway I can decrease it?

Post by polhoney »

Hi,

I tried the import time + sleep trick and it seems to work. Perhaps misspelled import (I see you typed "import:time" which is not a valid python syntax).

I got a xbox 360 controller freepie watch script and added the "import time" and "time.sleep(0.5)" lines, and results are now updated slowly (twice a second), thus greatly reducing cpu consumption:

Code: Select all

def update():
	import time
	time.sleep(0.5)
	global posxhmd
	global posyhmd
	global poszhmd
	[...]
I use FreePIE 1.10.666.0.

Pierre

Danmoer
One Eyed Hopeful
Posts: 5
Joined: Fri Dec 15, 2017 5:32 pm

Re: FriePie using a lot of CPU. Anyway I can decrease it?

Post by Danmoer »

Hello :)

Sadly, I still can't make this work. :(
Where in my script would I add this?

So far I tried changing the sleeptime to 10 to make it obvious, which, depending on where I put this, either makes the script wait 10 seconds before starting, or has no reduced impact on CPU at all.

You would really make my day if you could help me get it right.

Jabberwock
Cross Eyed!
Posts: 160
Joined: Mon Mar 02, 2015 3:58 pm

Re: FriePie using a lot of CPU. Anyway I can decrease it?

Post by Jabberwock »

Just to check the obvious - are you testing the script with TrackIR on? Because otherwise update() would probably not be used at all... Maybe put a diagnostic message right after the sleep, that way you will know sleeping is triggered correctly.

Danmoer
One Eyed Hopeful
Posts: 5
Joined: Fri Dec 15, 2017 5:32 pm

Re: FriePie using a lot of CPU. Anyway I can decrease it?

Post by Danmoer »

polhoney wrote:Hi,

I tried the import time + sleep trick and it seems to work. Perhaps misspelled import (I see you typed "import:time" which is not a valid python syntax).

I got a xbox 360 controller freepie watch script and added the "import time" and "time.sleep(0.5)" lines, and results are now updated slowly (twice a second), thus greatly reducing cpu consumption:

Code: Select all

def update():
	import time
	time.sleep(0.5)
	global posxhmd
	global posyhmd
	global poszhmd
	[...]
I use FreePIE 1.10.666.0.

Pierre

Thank you for your responses. The reply system was out for a few days, but in the meantime, by PM, Jabberwock helped me confirm that, though sleep itself works, it doesn't seem to reduce CPU usage, for whatever reason.

Even if I insert time.sleep(100) the CPU usage doesn't go down at all.

So far though, I think the lowering priority works ok'ish enough.

Post Reply

Return to “FreePIE”