Skip to content

Commit b2a1a74

Browse files
committed
DofCallback - add support to create plus and minus callback with filter
1 parent 8a66345 commit b2a1a74

File tree

3 files changed

+41
-10
lines changed

3 files changed

+41
-10
lines changed

pyspacemouse/pyspacemouse.py

+25-9
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
from typing import Callable, Union, List
66

77
# current version number
8-
__version__ = "0.1.3"
8+
__version__ = "1.0.1"
99

1010
# clock for timing
1111
high_acc_clock = timeit.default_timer
@@ -58,11 +58,18 @@ class DofCallback:
5858
"""Register new DoF callback"""
5959

6060
def __init__(
61-
self, axis: str, callback: Callable[[int], None], sleep: float = 0.0
61+
self,
62+
axis: str,
63+
callback: Callable[[int], None],
64+
sleep: float = 0.0,
65+
callback_minus: Callable[[int], None] = None,
66+
filter: float = 0.0
6267
):
6368
self.axis = axis
6469
self.callback = callback
6570
self.sleep = sleep
71+
self.callback_minus = callback_minus
72+
self.filter = filter
6673

6774

6875
class Config:
@@ -262,11 +269,19 @@ def process(self, data):
262269
# foreach all callbacks (ButtonCallback)
263270
for block_dof_callback in self.dof_callback_arr:
264271
now = high_acc_clock()
265-
axis = block_dof_callback.axis
266-
if now >= self.dict_state_last[axis] + block_dof_callback.sleep:
267-
# if True:
268-
block_dof_callback.callback(self.tuple_state, self.dict_state[axis])
269-
self.dict_state_last[axis] = now
272+
axis_name = block_dof_callback.axis
273+
if now >= self.dict_state_last[axis_name] + block_dof_callback.sleep:
274+
axis_val = self.dict_state[axis_name]
275+
# is minus callback defined
276+
if block_dof_callback.callback_minus:
277+
# is axis value greater than filter
278+
if axis_val > block_dof_callback.filter:
279+
block_dof_callback.callback(self.tuple_state, axis_val)
280+
elif axis_val < -block_dof_callback.filter:
281+
block_dof_callback.callback_minus(self.tuple_state, axis_val)
282+
else:
283+
block_dof_callback.callback(self.tuple_state, axis_val)
284+
self.dict_state_last[axis_name] = now
270285

271286
# only call the button callback if the button state actually changed
272287
if self.button_callback and button_changed:
@@ -289,7 +304,8 @@ def process(self, data):
289304
run = False
290305
# call callback
291306
if run:
292-
block_button_callback.callback(self.tuple_state, self.tuple_state.buttons, block_button_callback.buttons)
307+
block_button_callback.callback(self.tuple_state, self.tuple_state.buttons,
308+
block_button_callback.buttons)
293309

294310
def config_set(self, config: Config):
295311
"""Set new configuration of mouse from Config class"""
@@ -693,7 +709,7 @@ def open(
693709

694710

695711
def check_config(callback=None, dof_callback=None, dof_callback_arr=None, button_callback=None,
696-
button_callback_arr=None):
712+
button_callback_arr=None):
697713
"""Check that the input configuration has the correct components.
698714
Raise an exception if it encounters incorrect component.
699715
"""

setup.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
setuptools.setup(
1313
name='pyspacemouse',
14-
version='1.0.0',
14+
version='1.0.1',
1515
author='Jakub Andrýsek',
1616
author_email='email@kubaandrysek.cz',
1717
description='Multiplatform Python interface to the 3DConnexion Space Mouse - forked from pyspacenavigator',

tests/test_clb_axis_arr.py

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import pyspacemouse
2+
import time
3+
4+
5+
pyspacemouse.open(dof_callback_arr=[
6+
pyspacemouse.DofCallback("x", lambda state, axis: print(f"X plus {axis}"), 0.01, lambda state, axis: print(f"X minus {axis}")),
7+
pyspacemouse.DofCallback("y", lambda state, axis: print(f"Y plus {axis}"), 0.01, lambda state, axis: print(f"Y minus {axis}"), 0.5),
8+
])
9+
10+
11+
while True:
12+
out = pyspacemouse.read()
13+
# print(out.x)
14+
time.sleep(0.0001)
15+

0 commit comments

Comments
 (0)