Skip to content

Commit

Permalink
Add cli commands
Browse files Browse the repository at this point in the history
  • Loading branch information
JakubAndrysek committed May 3, 2024
1 parent 4833a85 commit 9941f2e
Show file tree
Hide file tree
Showing 7 changed files with 215 additions and 32 deletions.
2 changes: 1 addition & 1 deletion .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 4 additions & 2 deletions .idea/pyspacemouse.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 21 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,27 @@ if success:
````
More examples can be found in the [/examples](https://github.com/JakubAndrysek/PySpaceMouse/tree/master/examples) directory or in page with [Examples](https://spacemouse.kubaandrysek.cz/mouseApi/examples/).

## Available CLI test commands
```bash
usage: pyspacemouse [-h] [--version] [--list-spacemouse]
[--list-supported-devices] [--list-all-hid-devices]
[--test-connect]
PySpaceMouse CLI
options:
-h, --help show this help message and exit
--version Version of pyspacemouse
--list-spacemouse List connected SpaceMouse devices
--list-supported-devices
List supported SpaceMouse devices
--list-all-hid-devices
List all connected HID devices
--test-connect Test connect to the first available device
For more information, visit https://spacemouse.kubaandrysek.cz
```


## Troubleshooting

Expand Down
21 changes: 21 additions & 0 deletions docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,27 @@ if success:
````
More examples can be found in the [/examples](https://github.com/JakubAndrysek/PySpaceMouse/tree/master/examples) directory or in page with [Examples](https://spacemouse.kubaandrysek.cz/mouseApi/examples/).

## Available CLI test commands
```bash
usage: pyspacemouse [-h] [--version] [--list-spacemouse]
[--list-supported-devices] [--list-all-hid-devices]
[--test-connect]
PySpaceMouse CLI
options:
-h, --help show this help message and exit
--version Version of pyspacemouse
--list-spacemouse List connected SpaceMouse devices
--list-supported-devices
List supported SpaceMouse devices
--list-all-hid-devices
List all connected HID devices
--test-connect Test connect to the first available device
For more information, visit https://spacemouse.kubaandrysek.cz
```


## Troubleshooting

Expand Down
38 changes: 34 additions & 4 deletions pyspacemouse/pyspacemouse.py
Original file line number Diff line number Diff line change
Expand Up @@ -380,7 +380,7 @@ def config_remove(self):
# ButtonSpec(channel=3, byte=5, bit=5),
# ButtonSpec(channel=3, byte=5, bit=6),
# ButtonSpec(channel=3, byte=5, bit=7),

ButtonSpec(channel=3, byte=2, bit=4), # 1
ButtonSpec(channel=3, byte=2, bit=5), # 2
ButtonSpec(channel=3, byte=2, bit=6), # 3
Expand Down Expand Up @@ -408,7 +408,7 @@ def config_remove(self):
ButtonSpec(channel=3, byte=4, bit=1), # CTRL
ButtonSpec(channel=3, byte=4, bit=2), # LOCK



],
axis_scale=350.0,
Expand Down Expand Up @@ -756,7 +756,8 @@ def list_devices():
hid = Enumeration()
except AttributeError as e:
raise Exception(
"HID API is probably not installed. See README.md for details."
"HID API is probably not installed. "
"Look at https://spacemouse.kubaandrysek.cz for details."
) from e

all_hids = hid.find()
Expand All @@ -773,6 +774,35 @@ def list_devices():
)
return devices

def list_available_devices():
"""Return a list of all supported devices from config
Returns:
A list of string names of the devices supported (device_name, vid_id, pid_id)
"""
return [
(device_name, spec.hid_id[0], spec.hid_id[1])
for device_name, spec in device_specs.items()
]

def list_all_hid_devices():
"""Return a list of all HID devices connected
Returns:
A list of HID devices (product_string, manufacturer_string, vendor_id, product_id)
"""
try:
hid = Enumeration()
except AttributeError as e:
raise Exception(
"HID API is probably not installed."
"Look at https://spacemouse.kubaandrysek.cz for details."
) from e

return [
(device.product_string, device.manufacturer_string, device.vendor_id, device.product_id)
for device in hid.find()
]

def openCfg(config: Config, set_nonblocking_loop: bool = True, device=None, DeviceNumber=0):
"""
Expand Down Expand Up @@ -824,7 +854,7 @@ def open(
if len(all_devices) > 0:
device = all_devices[0]
else:
raise Exception("No device connected/supported!")
raise Exception("No found any connected or supported devices.")

found_devices = []
hid = Enumeration()
Expand Down
104 changes: 104 additions & 0 deletions pyspacemouse/pyspacemouse_cli.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
import argparse
import time

from pyspacemouse import list_devices, list_available_devices, open as open_mouse, read as read_mouse, \
close as close_mouse, list_all_hid_devices
from pkg_resources import get_distribution


def print_version_cli():
distribution = get_distribution("pyspacemouse")
print(f"pyspacemouse version {distribution.version}")


def list_spacemouse_cli():
devices = list_devices()
if devices:
print("Connected SpaceMouse devices:")
for device in devices:
print(f"- {device}")
else:
print("Error: No connected SpaceMouse devices found.")

def list_all_hid_devices_cli():
devices = list_all_hid_devices()
if devices:
print("All HID devices:")
for (product_string, manufacturer_string, vendor_id, product_id) in devices:
if product_string == "":
product_string = "Unknown"
if manufacturer_string == "":
manufacturer_string = "Unknown"
print(f"- {product_string} by {manufacturer_string} [VID: {hex(vendor_id)}, PID: {hex(product_id)}]")
else:
print("Error: No HID devices found.")

def list_supported_devices_cli():
available_devices = list_available_devices()
if available_devices:
print("Available SpaceMouse devices:")
for (device_name, vid_id, pid_id) in available_devices:
print(f"- {device_name} [VID: {hex(vid_id)}, PID: {hex(pid_id)}]")
else:
print("Error: No available SpaceMouse devices found.")

def test_connect_cli():
try:
success = open_mouse()
except Exception as e:
print(f"Failed to open SpaceMouse: {e}")
return

if not success:
print("Failed to open SpaceMouse")
return

print("SpaceMouse opened successfully, reading x, y, z values...")
time.sleep(1)

try:
while True:
state = read_mouse()
print(state.x, state.y, state.z)
time.sleep(0.01)
except KeyboardInterrupt:
print("KeyboardInterrupt: Exiting...")
finally:
close_mouse()

def main():
parser = argparse.ArgumentParser(description="PySpaceMouse CLI",
epilog="For more information, visit https://spacemouse.kubaandrysek.cz/")
parser.add_argument(
"--version", action="store_true", help="Version of pyspacemouse"
)
parser.add_argument(
"--list-spacemouse", action="store_true", help="List connected SpaceMouse devices"
)
parser.add_argument(
"--list-supported-devices", action="store_true", help="List supported SpaceMouse devices"
)
parser.add_argument(
"--list-all-hid-devices", action="store_true", help="List all connected HID devices"
)
parser.add_argument(
"--test-connect", action="store_true", help="Test connect to the first available device"
)
args = parser.parse_args()

if args.version:
print_version_cli()
elif args.list_spacemouse:
list_spacemouse_cli()
elif args.list_supported_devices:
list_supported_devices_cli()
elif args.list_all_hid_devices:
list_all_hid_devices_cli()
elif args.test_connect:
test_connect_cli()
else:
parser.print_help()


if __name__ == "__main__":
main()
55 changes: 30 additions & 25 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,40 +10,45 @@
long_description = (HERE / "README.md").read_text()

setuptools.setup(
name='pyspacemouse',
version='1.1.2',
author='Jakub Andrýsek',
author_email='email@kubaandrysek.cz',
description='Multiplatform Python interface to the 3DConnexion Space Mouse - forked from pyspacenavigator',
url='https://github.com/JakubAndrysek/pyspacemouse',
name="pyspacemouse",
version="1.1.3",
author="Jakub Andrýsek",
author_email="email@kubaandrysek.cz",
description="Multiplatform Python interface to the 3DConnexion Space Mouse - forked from pyspacenavigator",
url="https://github.com/JakubAndrysek/pyspacemouse",
long_description=long_description,
long_description_content_type='text/markdown',
keywords='pyspacemouse, 3d, 6 DoF, HID, python, open-source, spacemouse, spacenavigator, 3dconnection, 3d-mouse',
license='MIT',
packages=['pyspacemouse'],
long_description_content_type="text/markdown",
keywords="pyspacemouse, 3d, 6 DoF, HID, python, open-source, spacemouse, spacenavigator, 3dconnection, 3d-mouse",
license="MIT",
packages=["pyspacemouse"],
install_requires=[
"easyhid",
],
extras_require={
'develop': [
'mkdocs',
'mkdocs-material',
'mkdocs-glightbox',
'mkdocs-redirects',
'mkdoxy',
'mkdocs-open-in-new-tab',
'mkdocs-git-revision-date-localized-plugin'],
"develop": [
"mkdocs",
"mkdocs-material",
"mkdocs-glightbox",
"mkdocs-redirects",
"mkdoxy",
"mkdocs-open-in-new-tab",
"mkdocs-git-revision-date-localized-plugin",
],
},

zip_safe=False,
include_package_data=True,
classifiers=[
'License :: OSI Approved :: MIT License',
'Programming Language :: Python :: 3.8',
'Programming Language :: Python :: 3.9',
'Programming Language :: Python :: 3.10',
'Programming Language :: Python :: 3.11',
'Operating System :: OS Independent',
"License :: OSI Approved :: MIT License",
"Programming Language :: Python :: 3.8",
"Programming Language :: Python :: 3.9",
"Programming Language :: Python :: 3.10",
"Programming Language :: Python :: 3.11",
"Operating System :: OS Independent",
],
python_requires=">=3.8",
entry_points={
"console_scripts": [
"pyspacemouse=pyspacemouse.pyspacemouse_cli:main",
],
},
)

0 comments on commit 9941f2e

Please sign in to comment.