-
Notifications
You must be signed in to change notification settings - Fork 3
Created mouse_inputs.py #4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
quantum9Innovation
wants to merge
1
commit into
MIT-Assistive-Technology:server
Choose a base branch
from
tigestd:patch-1
base: server
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,75 @@ | ||
| import hid | ||
| import time | ||
|
|
||
| # SpaceMouse VID and PID details | ||
| vendor_id = 0x256f | ||
| product_id = 0xc638 | ||
|
|
||
| device = hid.device() | ||
| device.open(vendor_id, product_id) | ||
| print("Connected to SpaceMouse Pro!") | ||
|
|
||
| # keep running even when no data is sent | ||
| device.set_nonblocking(True) | ||
|
|
||
| def interpret_motion(data): | ||
| """ | ||
| Given 13 byte data from the device (important), do 3 things: | ||
| 1. Read in the x, y, z, and corresponding rotation values | ||
| 2. interpret how far user moved mouse from its original position | ||
| 3. Print to the console/terminal | ||
|
|
||
| Caveat: Spacemouse must be plugged in via USB - data is "hidden" via | ||
| bluetooth AND the reciever unfortunately | ||
| """ | ||
|
|
||
| if len(data) < 7: | ||
| return | ||
|
|
||
| # Translation (movement) data | ||
| x = int.from_bytes(data[1:3], byteorder='little', signed=True) | ||
| y = int.from_bytes(data[3:5], byteorder='little', signed=True) | ||
| z = int.from_bytes(data[5:7], byteorder='little', signed=True) | ||
|
|
||
| # Rotation data (pitch, yaw, roll) | ||
| rot_x = int.from_bytes(data[7:9], byteorder='little', signed=True) | ||
| rot_y = int.from_bytes(data[9:11], byteorder='little', signed=True) | ||
| rot_z = int.from_bytes(data[11:13], byteorder='little', signed=True) | ||
|
|
||
| threshold = 100 # Ignore very small noise | ||
| rotation_threshold = 200 # Rotation threshold | ||
|
|
||
| directions = [] | ||
|
|
||
| # Translation (movement) | ||
| if abs(x) > threshold: | ||
| direction = "right" if x > 0 else "left" | ||
| directions.append(f"Moved {direction}") | ||
| if abs(y) > threshold: | ||
| direction = "backward" if y > 0 else "forward" | ||
| directions.append(f"Moved {direction}") | ||
| if abs(z) > threshold: | ||
| direction = "down" if z > 0 else "up" | ||
| directions.append(f"Moved {direction}") | ||
|
|
||
| # Rotation (pitch, yaw, roll) | ||
| if abs(rot_x) > rotation_threshold: | ||
| direction = "clockwise" if rot_x > 0 else "counterclockwise" | ||
| directions.append(f"Rotated pitch {direction}") | ||
| if abs(rot_y) > rotation_threshold: | ||
| direction = "clockwise" if rot_y > 0 else "counterclockwise" | ||
| directions.append(f"Rotated yaw {direction}") | ||
| if abs(rot_z) > rotation_threshold: | ||
| direction = "clockwise" if rot_z > 0 else "counterclockwise" | ||
| directions.append(f"Rotated roll {direction}") | ||
|
|
||
| # If multiple directions at the same time | ||
| if directions: | ||
| print(", ".join(directions)) | ||
|
|
||
| # Running at all times | ||
| while True: | ||
| report = device.read(13) # 13 byte data | ||
| if report: | ||
| interpret_motion(report) # only interpret WHEN data is sent | ||
| time.sleep(0.01) | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The data length check is insufficient: the function expects 13-byte data but only verifies a minimum of 7 bytes, which may lead to index errors.