Feat: Set timer resolution (1ms)

This commit is contained in:
SnuffSocket 2024-10-07 02:02:47 +03:00
parent a291b2fc4f
commit fd40ffedd3

View File

@ -29,6 +29,7 @@ import PySimpleGUI as sg
import queue
import requests
import threading
from ctypes import windll, c_int
from camera_widget import CameraWidget
from config import EyeTrackConfig
from eye import EyeId
@ -48,6 +49,13 @@ if is_nt:
from winotify import Notification
os.system("color") # init ANSI color
winmm = None
try:
winmm = windll.winmm
except OSError:
#print("[WARN] Failed to load winmm.dll")
pass
# Random environment variable to speed up webcam opening on the MSMF backend.
# https://github.com/opencv/opencv/issues/17687
os.environ["OPENCV_VIDEOIO_MSMF_ENABLE_HW_TRANSFORMS"] = "0"
@ -202,7 +210,15 @@ def create_window(config, settings, eyes):
icon=resource_path("Images/logo.ico"),
background_color="#292929")
def timerResolution(toggle):
if winmm != None:
if toggle:
rc = c_int(winmm.timeBeginPeriod(1))
if rc.value != 0:
# TIMEERR_NOCANDO = 97
print(f"[WARN] Failed to set timer resolution: {rc.value}")
else:
winmm.timeEndPeriod(1)
def main():
# Get Configuration
@ -245,6 +261,8 @@ def main():
except:
print("\033[91m[INFO] Could not check for updates. Please try again later.\033[0m")
timerResolution(True)
osc_queue: queue.Queue[OSCMessage] = queue.Queue(maxsize=10)
eyes = [
@ -323,6 +341,7 @@ def main():
eye.stop()
cancellation_event.set()
osc_manager.shutdown()
timerResolution(False)
print("\033[94m[INFO] Exiting EyeTrackApp\033[0m")
window.close()
os._exit(0) # I do not like this, but for now this fixes app hang on close