diff options
-rw-r--r-- | clock.py | 23 | ||||
-rw-r--r-- | linux.py | 35 | ||||
-rw-r--r-- | osc2light.py | 6 |
3 files changed, 38 insertions, 26 deletions
diff --git a/clock.py b/clock.py deleted file mode 100644 index 3e97dbf..0000000 --- a/clock.py +++ /dev/null @@ -1,23 +0,0 @@ -# Courtesy of Armin Ronacher, found at StackOverflow - -import ctypes -import os - -CLOCK_MONOTONIC = 1 # see <linux/time.h> - -class timespec(ctypes.Structure): - _fields_ = [ - ('tv_sec', ctypes.c_long), - ('tv_nsec', ctypes.c_long) - ] - -librt = ctypes.CDLL('librt.so.1', use_errno=True) -clock_gettime = librt.clock_gettime -clock_gettime.argtypes = [ctypes.c_int, ctypes.POINTER(timespec)] - -def now(): - t = timespec() - if clock_gettime(CLOCK_MONOTONIC, ctypes.pointer(t)) != 0: - errno_ = ctypes.get_errno() - raise OSError(errno_, os.strerror(errno_)) - return t.tv_sec + t.tv_nsec * 1e-9 diff --git a/linux.py b/linux.py new file mode 100644 index 0000000..5da6b0e --- /dev/null +++ b/linux.py @@ -0,0 +1,35 @@ +# Courtesy of Armin Ronacher, found at StackOverflow + +import ctypes +import os + +CLOCK_MONOTONIC = 1 # see <linux/time.h> + +class _timespec(ctypes.Structure): + _fields_ = [ + ('tv_sec', ctypes.c_long), + ('tv_nsec', ctypes.c_long) + ] + +_librt = ctypes.CDLL('librt.so.1', use_errno=True) +_libc = ctypes.CDLL('libc.so.6', use_errno=True) + +_clock_gettime = _librt.clock_gettime +_clock_gettime.argtypes = [ctypes.c_int, ctypes.POINTER(_timespec)] + +_if_nametoindex = _libc.if_nametoindex +_if_nametoindex.argtype = [ctypes.c_uint, ctypes.c_char_p] + +def now(): + t = _timespec() + if _clock_gettime(CLOCK_MONOTONIC, ctypes.pointer(t)) != 0: + errno_ = ctypes.get_errno() + raise OSError(errno_, os.strerror(errno_)) + return t.tv_sec + t.tv_nsec * 1e-9 + +def if_nametoindex(interfaceName): + rv = _if_nametoindex(interfaceName) + if rv == 0: + errno_= ctypes.get_errno() + raise OSError(errno_, os.strerror(errno_)) + return rv diff --git a/osc2light.py b/osc2light.py index e046600..78afe0e 100644 --- a/osc2light.py +++ b/osc2light.py @@ -5,7 +5,7 @@ import liblo import socket import struct import sys -import clock +import linux import time import threading @@ -45,10 +45,10 @@ class TXQueue(threading.Thread): item = self.queue.pop(0) # We can't send too often, this will cause messages to be lost - while self.last_sent + self.wait_iv > clock.now(): + while self.last_sent + self.wait_iv > linux.now(): time.sleep(self.sleep_iv) item.perform() - self.last_sent = clock.now() + self.last_sent = linux.now() def append(self, txop): assert isinstance(txop, TXOp) |