Skip to content

Commit

Permalink
DofCallback - add support to create plus and minus callback with filter
Browse files Browse the repository at this point in the history
  • Loading branch information
JakubAndrysek committed Mar 25, 2021
1 parent 8a66345 commit b2a1a74
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 10 deletions.
34 changes: 25 additions & 9 deletions pyspacemouse/pyspacemouse.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from typing import Callable, Union, List

# current version number
__version__ = "0.1.3"
__version__ = "1.0.1"

# clock for timing
high_acc_clock = timeit.default_timer
Expand Down Expand Up @@ -58,11 +58,18 @@ class DofCallback:
"""Register new DoF callback"""

def __init__(
self, axis: str, callback: Callable[[int], None], sleep: float = 0.0
self,
axis: str,
callback: Callable[[int], None],
sleep: float = 0.0,
callback_minus: Callable[[int], None] = None,
filter: float = 0.0
):
self.axis = axis
self.callback = callback
self.sleep = sleep
self.callback_minus = callback_minus
self.filter = filter


class Config:
Expand Down Expand Up @@ -262,11 +269,19 @@ def process(self, data):
# foreach all callbacks (ButtonCallback)
for block_dof_callback in self.dof_callback_arr:
now = high_acc_clock()
axis = block_dof_callback.axis
if now >= self.dict_state_last[axis] + block_dof_callback.sleep:
# if True:
block_dof_callback.callback(self.tuple_state, self.dict_state[axis])
self.dict_state_last[axis] = now
axis_name = block_dof_callback.axis
if now >= self.dict_state_last[axis_name] + block_dof_callback.sleep:
axis_val = self.dict_state[axis_name]
# is minus callback defined
if block_dof_callback.callback_minus:
# is axis value greater than filter
if axis_val > block_dof_callback.filter:
block_dof_callback.callback(self.tuple_state, axis_val)
elif axis_val < -block_dof_callback.filter:
block_dof_callback.callback_minus(self.tuple_state, axis_val)
else:
block_dof_callback.callback(self.tuple_state, axis_val)
self.dict_state_last[axis_name] = now

# only call the button callback if the button state actually changed
if self.button_callback and button_changed:
Expand All @@ -289,7 +304,8 @@ def process(self, data):
run = False
# call callback
if run:
block_button_callback.callback(self.tuple_state, self.tuple_state.buttons, block_button_callback.buttons)
block_button_callback.callback(self.tuple_state, self.tuple_state.buttons,
block_button_callback.buttons)

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


def check_config(callback=None, dof_callback=None, dof_callback_arr=None, button_callback=None,
button_callback_arr=None):
button_callback_arr=None):
"""Check that the input configuration has the correct components.
Raise an exception if it encounters incorrect component.
"""
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

setuptools.setup(
name='pyspacemouse',
version='1.0.0',
version='1.0.1',
author='Jakub Andrýsek',
author_email='[email protected]',
description='Multiplatform Python interface to the 3DConnexion Space Mouse - forked from pyspacenavigator',
Expand Down
15 changes: 15 additions & 0 deletions tests/test_clb_axis_arr.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import pyspacemouse
import time


pyspacemouse.open(dof_callback_arr=[
pyspacemouse.DofCallback("x", lambda state, axis: print(f"X plus {axis}"), 0.01, lambda state, axis: print(f"X minus {axis}")),
pyspacemouse.DofCallback("y", lambda state, axis: print(f"Y plus {axis}"), 0.01, lambda state, axis: print(f"Y minus {axis}"), 0.5),
])


while True:
out = pyspacemouse.read()
# print(out.x)
time.sleep(0.0001)

0 comments on commit b2a1a74

Please sign in to comment.