1+ import sys
2+ import time
3+ import ctypes #Required for colored error messages.
4+
5+ DefaultConfig = {
6+ "IP" : "127.0.0.1" ,
7+ "ListeningPort" : 9001 ,
8+ "SendingPort" : 9000 ,
9+ "RunDeadzone" : 0.70 ,
10+ "WalkDeadzone" : 0.15 ,
11+ "StrengthMultiplier" : 1.2 ,
12+ "TurningEnabled" : False ,
13+ "TurningMultiplier" : 0.75 ,
14+ "TurningDeadzone" : .15 ,
15+ "TurningGoal" : 90 ,
16+ "ActiveDelay" : 0.1 ,
17+ "InactiveDelay" : 0.5 ,
18+ "Logging" : True ,
19+ "XboxJoystickMovement" : False ,
20+
21+ "PhysboneParameters" :
22+ [
23+ "Leash"
24+ ],
25+
26+ "DirectionalParameters" :
27+ {
28+ "Z_Positive_Param" : "Leash_Z+" ,
29+ "Z_Negative_Param" : "Leash_Z-" ,
30+ "X_Positive_Param" : "Leash_X+" ,
31+ "X_Negative_Param" : "Leash_X-"
32+ }
33+ }
34+
35+
36+ class ConfigSettings :
37+
38+ def __init__ (self , configData ):
39+ self .setSettings (configData ) #Set config values
40+
41+ def setSettings (self , configJson ):
42+ try :
43+ self .IP = configJson ["IP" ]
44+ self .ListeningPort = configJson ["ListeningPort" ]
45+ self .SendingPort = configJson ["SendingPort" ]
46+ self .RunDeadzone = configJson ["RunDeadzone" ]
47+ self .WalkDeadzone = configJson ["WalkDeadzone" ]
48+ self .StrengthMultiplier = configJson ["StrengthMultiplier" ]
49+ self .TurningEnabled = configJson ["TurningEnabled" ]
50+ self .TurningMultiplier = configJson ["TurningMultiplier" ]
51+ self .TurningDeadzone = configJson ["TurningDeadzone" ]
52+ self .TurningGoal = (configJson ["TurningGoal" ]/ 180 )
53+ self .ActiveDelay = configJson ["ActiveDelay" ]
54+ self .InactiveDelay = configJson ["InactiveDelay" ]
55+ self .Logging = configJson ["Logging" ]
56+ self .XboxJoystickMovement = configJson ["XboxJoystickMovement" ]
57+ except Exception as e :
58+ print ('\x1b [1;31;40m' + 'Malformed config file. Loading default values.' + '\x1b [0m' )
59+ print (e ,"was the exception\n " )
60+ self .IP = DefaultConfig ["IP" ]
61+ self .ListeningPort = DefaultConfig ["ListeningPort" ]
62+ self .SendingPort = DefaultConfig ["SendingPort" ]
63+ self .RunDeadzone = DefaultConfig ["RunDeadzone" ]
64+ self .WalkDeadzone = DefaultConfig ["WalkDeadzone" ]
65+ self .StrengthMultiplier = DefaultConfig ["StrengthMultiplier" ]
66+ self .TurningEnabled = DefaultConfig ["TurningEnabled" ]
67+ self .TurningMultiplier = DefaultConfig ["TurningMultiplier" ]
68+ self .TurningDeadzone = DefaultConfig ["TurningDeadzone" ]
69+ self .TurningGoal = (DefaultConfig ["TurningGoal" ]/ 180 )
70+ self .ActiveDelay = DefaultConfig ["ActiveDelay" ]
71+ self .InactiveDelay = DefaultConfig ["InactiveDelay" ]
72+ self .Logging = DefaultConfig ["Logging" ]
73+ self .XboxJoystickMovement = DefaultConfig ["XboxJoystickMovement" ]
74+ time .sleep (3 )
75+
76+ def addGamepadControls (self , gamepad , runButton ):
77+ self .gamepad = gamepad
78+ self .runButton = runButton
79+
80+ def printInfo (self ):
81+ print ('\x1b [1;32;40m' + 'OSCLeash is Running!' + '\x1b [0m' )
82+
83+ if self .IP == "127.0.0.1" :
84+ print ("IP: Localhost" )
85+ else :
86+ print ("IP: Not Localhost? Wack." )
87+
88+ print (f"Listening on port { self .ListeningPort } \n Sending on port { self .SendingPort } " )
89+ print ("Run Deadzone of {:.0f}" .format (self .RunDeadzone * 100 )+ "% stretch" )
90+ print ("Walking Deadzone of {:.0f}" .format (self .WalkDeadzone * 100 )+ "% stretch" )
91+ print ("Delays of {:.0f}" .format (self .ActiveDelay * 1000 ),"& {:.0f}" .format (self .InactiveDelay * 1000 ),"ms" )
92+ if self .TurningEnabled :
93+ print (f"Turning is enabled:\n \t Multiplier of { self .TurningMultiplier } \n \t Deadzone of { self .TurningDeadzone } \n \t Goal of { self .TurningGoal * 180 } °" )
94+
95+ class Leash :
96+
97+ def __init__ (self , paraName , contacts , settings : ConfigSettings ):
98+
99+ self .Name : str = paraName
100+ self .settings = settings
101+
102+ self .Stretch : float = 0
103+ self .Z_Positive : float = 0
104+ self .Z_Negative : float = 0
105+ self .X_Positive : float = 0
106+ self .X_Negative : float = 0
107+
108+ # Booleans for thread logic
109+ self .Grabbed : bool = False
110+ self .wasGrabbed : bool = False
111+ self .Active : bool = False
112+
113+ if settings .TurningEnabled :
114+ self .LeashDirection = paraName .split ("_" )[- 1 ]
115+
116+ self .Z_Positive_ParamName : str = contacts ["Z_Positive_Param" ]
117+ self .Z_Negative_ParamName : str = contacts ["Z_Negative_Param" ]
118+ self .X_Positive_ParamName : str = contacts ["X_Positive_Param" ]
119+ self .X_Negative_ParamName : str = contacts ["X_Negative_Param" ]
120+
121+ def resetMovement (self ):
122+ self .Z_Positive : float = 0
123+ self .Z_Negative : float = 0
124+ self .X_Positive : float = 0
125+ self .X_Negative : float = 0
126+
127+ def printDirections (self ):
128+ print ("\n Contact Directions:\n " )
129+
130+ print ("{}: {}" .format (self .Z_Positive_ParamName , self .Z_Positive ))
131+ print ("{}: {}" .format (self .Z_Negative_ParamName , self .Z_Negative ))
132+ print ("{}: {}" .format (self .X_Positive_ParamName , self .X_Positive ))
133+ print ("{}: {}" .format (self .X_Negative_ParamName , self .X_Negative ))
0 commit comments