Skip to content

Commit

Permalink
Initial Commit
Browse files Browse the repository at this point in the history
  • Loading branch information
sethtdenney committed Jun 19, 2024
1 parent 0f33cb8 commit 319082a
Show file tree
Hide file tree
Showing 29 changed files with 10,164 additions and 0 deletions.
127 changes: 127 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class

# C extensions
*.so

# Distribution / packaging
.Python
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
wheels/
pip-wheel-metadata/
share/python-wheels/
*.egg-info/
.installed.cfg
*.egg
MANIFEST

# PyInstaller
# Usually these files are written by a python script from a template
# before PyInstaller builds the exe, so as to inject date/other infos into it.
*.manifest
*.spec

# Installer logs
pip-log.txt
pip-delete-this-directory.txt

# Unit test / coverage reports
htmlcov/
.tox/
.nox/
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml
*.cover
*.py,cover
.hypothesis/
.pytest_cache/

# Translations
*.mo
*.pot

# Django stuff:
*.log
local_settings.py
db.sqlite3
db.sqlite3-journal

# Flask stuff:
instance/
.webassets-cache

# Scrapy stuff:
.scrapy

# Sphinx documentation
docs/_build/

# PyBuilder
target/

# Jupyter Notebook
.ipynb_checkpoints

# IPython
profile_default/
ipython_config.py

# pyenv
.python-version

# pipenv
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
# However, in case of collaboration, if having platform-specific dependencies or dependencies
# having no cross-platform support, pipenv may install dependencies that don't work, or not
# install all needed dependencies.
#Pipfile.lock

# celery beat schedule file
celerybeat-schedule

# SageMath parsed files
*.sage.py

# Environments
.env
.venv
env/
venv/
ENV/
env.bak/
venv.bak/

# Spyder project settings
.spyderproject
.spyproject

# Rope project settings
.ropeproject

# mkdocs documentation
/site

# mypy
.mypy_cache/
.dmypy.json
dmypy.json

# Pyre type checker
.pyre/

.idea/
Empty file added bhaptics/__init__.py
Empty file.
178 changes: 178 additions & 0 deletions bhaptics/better_haptic_player.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,178 @@
import json
import socket
from websocket import create_connection, WebSocket
import threading
from enum import Enum

ws = None

active_keys = set([])
connected_positions = set([])

class BhapticsPosition(Enum):
Vest = "Vest"
VestFront = "VestFront"
VestBack = "VestBack"
ForearmL = "ForearmL"
ForearmR = "ForearmR"
Head = "Head"
HandL = "HandL"
HandR = "HandR"
FootL = "FootL"
FootR = "FootR"
GloveL = "GloveL"
GloveR = "GloveR"

class WebSocketReceiver(WebSocket):
def recv_frame(self):
global active_keys
global connected_positions
frame = super().recv_frame()
try:
frame_obj = json.loads(frame.data)
active = frame_obj['ActiveKeys']

# if len(active) > 0:
# print (active)
active_keys = set(active)
connected_positions = set(frame_obj['ConnectedPositions'])
except:
# active_keys = set([])
# connected_positions = set([])
print('')

return frame


def thread_function(name):
while True:
if ws is not None:
ws.recv_frame()


def initialize():
global ws
try:
ws = create_connection("ws://localhost:15881/v2/feedbacks",
sockopt=((socket.IPPROTO_TCP, socket.TCP_NODELAY, 1),),
class_=WebSocketReceiver)

x = threading.Thread(target=thread_function, args=(1,))
x.start()
except:
print("Couldn't connect")
return


def destroy():
if ws is not None:
ws.close()


def is_playing():
return len(active_keys) > 0


def is_playing_key(key):
return key in active_keys


# position Vest Head ForeamrL ForearmR HandL HandR FootL FootR
def is_device_connected(position):
return position in connected_positions


def register(key, file_directory):
json_data = open(file_directory).read()

#print(json_data)

data = json.loads(json_data)
project = data["project"]

layout = project["layout"]
tracks = project["tracks"]

request = {
"Register": [{
"Key": key,
"Project": {
"Tracks": tracks,
"Layout": layout
}
}]
}

json_str = json.dumps(request)
__submit(json_str)


def submit_registered(key):
request = {
"Submit": [{
"Type": "key",
"Key": key,
}]
}

json_str = json.dumps(request)

__submit(json_str)


def submit_registered_with_option(
key, alt_key,
scale_option,
rotation_option):
# scaleOption: {"intensity": 1, "duration": 1}
# rotationOption: {"offsetAngleX": 90, "offsetY": 0}
request = {
"Submit": [{
"Type": "key",
"Key": key,
"Parameters": {
"altKey": alt_key,
"rotationOption": rotation_option,
"scaleOption": scale_option,
}
}]
}

json_str = json.dumps(request);

__submit(json_str)


def submit(key, frame):
request = {
"Submit": [{
"Type": "frame",
"Key": key,
"Frame": frame
}]
}

json_str = json.dumps(request);

__submit(json_str)


def submit_dot(key, position, dot_points, duration_millis):
front_frame = {
"position": position,
"dotPoints": dot_points,
"durationMillis": duration_millis
}
submit(key, front_frame)

def submit_path(key, position, path_points, duration_millis):
front_frame = {
"position": position,
"pathPoints": path_points,
"durationMillis": duration_millis
}
submit(key, front_frame)

def __submit(json_str):
if ws is not None:
ws.send(json_str)
Loading

0 comments on commit 319082a

Please sign in to comment.