@@ -25,7 +25,7 @@ class Gamepad:
2525 DPAD_L = const (16 )
2626 DPAD_R = const (17 )
2727
28- joyData = [
28+ _joyData = [
2929 0.0 ,
3030 0.0 ,
3131 0.0 ,
@@ -35,7 +35,7 @@ class Gamepad:
3535 @classmethod
3636 def get_default_gamepad (cls ):
3737 """
38- Get the default XRP bluetooth joystick instance. This is a singleton, so only one instance of the reflectance sensor will ever exist.
38+ Get the default XRP bluetooth joystick instance. This is a singleton, so only one instance of the gamepad sensor will ever exist.
3939 """
4040 if cls ._DEFAULT_GAMEPAD_INSTANCE is None :
4141 cls ._DEFAULT_GAMEPAD_INSTANCE = cls ()
@@ -44,30 +44,54 @@ def get_default_gamepad(cls):
4444
4545 def __init__ (self ):
4646 """
47- """
47+ Manages communication with gamepad data coming from a remote computer via bluetooth
4848
49+ """
4950 def start (self ):
50- for i in range (len (self .joyData )):
51- self .joyData [i ] = 0.0
51+ """
52+ Signals the remote computer to begin sending gamepad data packets.
53+ """
54+ for i in range (len (self ._joyData )):
55+ self ._joyData [i ] = 0.0
5256 uart .set_data_callback (self ._data_callback )
5357 sys .stdout .write (chr (27 ))
5458 sys .stdout .write (chr (101 ))
5559
5660
5761 def stop (self ):
58- uart .clear_data_callback ()
62+ """
63+ Signals the remote computer to stop sending gamepad data packets.
64+ """
5965 sys .stdout .write (chr (27 ))
6066 sys .stdout .write (chr (102 ))
6167
62- def get_value (self , index ):
63- return - self .joyData [index ] #returning the negative to make normal for user
68+ def get_value (self , index :int ) -> float :
69+ """
70+ Get the current value of a joystick axis
71+
72+ :param index: The joystick axis index
73+ Gamepad.X1, Gamepad.Y1, Gamepad.X2, Gamepad.Y2
74+ :type int
75+ :returns: The value of the joystick between -1 and 1
76+ :rtype: float
77+ """
78+ return - self ._joyData [index ] #returning the negative to make normal for user
6479
65- def is_button_pressed (self , index ):
66- return self .joyData [index ] > 0
80+ def is_button_pressed (self , index :int ) -> bool :
81+ """
82+ Checks if a specific button is currently pressed.
83+
84+ :param index: The button index
85+ Gamepad.BUTTON_A, Gamepad.TRIGGER_L, Gamepad.DPAD_UP, etc
86+ :type int
87+ :returns: The value of the button 1 or 0
88+ :rtype: bool
89+ """
90+ return self ._joyData [index ] > 0
6791
6892 def _data_callback (self , data ):
6993 if (data [0 ] == 0x55 and len (data ) == data [1 ] + 2 ):
7094 for i in range (2 , data [1 ] + 2 , 2 ):
71- self .joyData [data [i ]] = round (data [i + 1 ]/ 127.5 - 1 , 2 )
95+ self ._joyData [data [i ]] = round (data [i + 1 ]/ 127.5 - 1 , 2 )
7296
7397
0 commit comments