Created
November 2, 2023 15:50
-
-
Save warmans/a7c69d0fdec7e188bfb9c6b455c04208 to your computer and use it in GitHub Desktop.
openvr haptics
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
#!/bin/env python | |
import time | |
import RTk.GPIO as GPIO | |
import openvr | |
import sys | |
import atexit | |
def shutdown(): | |
print("Shutting down openvr...") | |
openvr.shutdown() | |
def check_env(): | |
if openvr.isHmdPresent(): | |
print("VR headset found") | |
if openvr.isRuntimeInstalled(): | |
print("Runtime is installed") | |
def await_init(): | |
print("Starting...") | |
while True: | |
try: | |
vrsys = openvr.init(openvr.VRApplication_Background) | |
print("Success") | |
return vrsys | |
except: | |
print("Failed to init, retrying...") | |
time.sleep(1) | |
class Solanoid: | |
pin = 2 | |
def __init__(self): | |
GPIO.setmode(GPIO.BCM) | |
GPIO.setup(self.pin, GPIO.OUT) | |
GPIO.output(self.pin, GPIO.HIGH) | |
def activate(self): | |
GPIO.output(self.pin, GPIO.LOW) | |
time.sleep(0.1) | |
GPIO.output(self.pin, GPIO.HIGH) | |
class HapticDevice: | |
def __init__(self, s: Solanoid): | |
self.__trigger_down = False | |
self.__vibration_on = False | |
self.__solanoid = s | |
def handle_event(self, e: openvr.VREvent_t): | |
if e.eventType == openvr.VREvent_ButtonPress and e.data.controller.button == 33: | |
print("trigger down") | |
sys.stdout.flush() | |
self.__trigger_down = True | |
self.emit() | |
return | |
if e.eventType == openvr.VREvent_ButtonUnpress and e.data.controller.button == 33: | |
print("trigger up\n\n") | |
sys.stdout.flush() | |
self.__trigger_down = False | |
self.emit() | |
return | |
# | |
# if e.eventType == openvr.VREvent_Input_HapticVibration: | |
# print("vibrate:", e.data.hapticVibration.fFrequency, e.data.hapticVibration.fAmplitude) | |
# sys.stdout.flush() | |
# | |
# if e.data.hapticVibration.fAmplitude == 0: | |
# self.__vibration_on = False | |
# else: | |
# self.__vibration_on = True | |
# self.emit() | |
# return | |
def emit(self): | |
if self.__trigger_down: | |
self.__solanoid.activate() | |
# | |
# main | |
# | |
check_env() | |
vr_system = await_init() | |
atexit.register(shutdown) | |
solanoid = Solanoid() | |
hd = HapticDevice(solanoid) | |
while True: | |
ev = openvr.VREvent_t() | |
while openvr.VRSystem().pollNextEvent(ev): | |
hd.handle_event(ev) | |
time.sleep(0.01) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment