-
Notifications
You must be signed in to change notification settings - Fork 8
/
QDSpy_core_support.py
134 lines (114 loc) · 3.96 KB
/
QDSpy_core_support.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
QDSpy module - support routines related to timing and priority
'Clock'
Class that provides a high-precision clock and is based on
"clock.py", from PsychoPy library.
Copyright (C) 2015 Jonathan Peirce, Distributed under the
terms of the GNU General Public License (GPL)
'setHighProcessPrior()', 'setNormalProcessPrior()'
Routines to increase and reset process priority
Copyright (c) 2013-2022 Thomas Euler
Distributed under the terms of the GNU General Public License (GPL)
2021-10-15 - Adapt to LINUX
2022-08-06 - Some reformatting
"""
# ---------------------------------------------------------------------
__author__ = "[email protected]"
import os
import sys
import QDSpy_global as glb
from pkgutil import iter_modules
from operator import xor
if glb.QDSpy_incProcessPrior:
import psutil
PLATFORM_WINDOWS = sys.platform == "win32"
# ---------------------------------------------------------------------
# Multiprocessing support
# ---------------------------------------------------------------------
WORKING, CANCELED, TERMINATING, IDLE = (
"Working", "Canceled", "Terminating", "Idle"
)
# ---------------------------------------------------------------------
# Set the default timing mechanism
# ---------------------------------------------------------------------
getTime = None
if PLATFORM_WINDOWS:
global _fcounter, _qpfreq, _winQPC
from ctypes import byref, c_int64, windll
_fcounter = c_int64()
_qpfreq = c_int64()
windll.Kernel32.QueryPerformanceFrequency(byref(_qpfreq))
_qpfreq = float(_qpfreq.value)
_winQPC=windll.Kernel32.QueryPerformanceCounter
def getTime():
_winQPC(byref(_fcounter))
return _fcounter.value/_qpfreq
else:
cur_pyver = sys.version_info
if cur_pyver[0]==2 and cur_pyver[1]<=6:
import time
getTime = time.time
else:
import timeit
getTime = timeit.default_timer
# ---------------------------------------------------------------------
# A high-precision clock
# ---------------------------------------------------------------------
class Clock:
def __init__(self):
self.t0_s = getTime()
def getTime_s(self):
return getTime() -self.t0_s
def getOffset_s(self):
return self.t0_s
defaultClock = Clock()
# ---------------------------------------------------------------------
# IO device-related helper
# ---------------------------------------------------------------------
def setIODevicePin(_IO, _portStr, _pin, _invert, _state):
port = _IO.getPortFromStr(_portStr)
mask = 0x01 << _pin
data = _IO.readDPort(port)
if xor(_state, _invert):
data = data | mask
else:
data = data & ~mask
_IO.writeDPort(port, data)
# ---------------------------------------------------------------------
# Increase/decrease process priority
# ---------------------------------------------------------------------
# psutil.ABOVE_NORMAL_PRIORITY_CLASS
# psutil.BELOW_NORMAL_PRIORITY_CLASS
# psutil.HIGH_PRIORITY_CLASS
# psutil.IDLE_PRIORITY_CLASS
# psutil.NORMAL_PRIORITY_CLASS
# psutil.REALTIME_PRIORITY_CLASS
#
def setHighProcessPrior():
if glb.QDSpy_incProcessPrior:
if PLATFORM_WINDOWS:
proc = psutil.Process(os.getpid())
pyVersion = sys.version_info[0] +sys.version_info[1]/10
if pyVersion <= 3.4:
proc.set_nice(psutil.HIGH_PRIORITY_CLASS)
else:
proc.nice(psutil.HIGH_PRIORITY_CLASS)
return True
return False
def setNormalProcessPrior():
if glb.QDSpy_incProcessPrior:
if PLATFORM_WINDOWS:
proc = psutil.Process(os.getpid())
pyVersion = sys.version_info[0] +sys.version_info[1]/10
if pyVersion <= 3.4:
proc.set_nice(psutil.NORMAL_PRIORITY_CLASS)
else:
proc.nice(psutil.NORMAL_PRIORITY_CLASS)
return True
return False
# ---------------------------------------------------------------------
def module_exists(module_name):
return module_name in (name for loader,name,ispkg in iter_modules())
# ---------------------------------------------------------------------