Added stick tilt functionality

This commit is contained in:
Brikwerk 2020-08-02 21:56:42 -07:00
commit 1619c96cd7
3 changed files with 57 additions and 2 deletions

13
demo.py
View file

@ -3,6 +3,7 @@ from random import randint
import nxbt
from nxbt import Buttons
from nxbt import Sticks
MACRO = """
LOOP 12
@ -78,7 +79,6 @@ if __name__ == "__main__":
# Select the last controller for input
controller_idx = controller_idxs[-1]
time.sleep(4)
# Wait for the switch to connect to the controller
nx.wait_for_connection(controller_idx)
@ -93,8 +93,17 @@ if __name__ == "__main__":
nx.stop_macro(controller_idx, macro_id)
print("Stopped Macro")
# Moving the selected home screen item two spaces to the right and back.
nx.tilt_stick(controller_idx, Sticks.RIGHT_STICK, 100, 0,
tilted=0.25, released=0.25)
nx.tilt_stick(controller_idx, Sticks.RIGHT_STICK, 100, 0,
tilted=0.25, released=0.25)
nx.tilt_stick(controller_idx, Sticks.RIGHT_STICK, -100, 0,
tilted=0.25, released=0.25)
nx.tilt_stick(controller_idx, Sticks.RIGHT_STICK, -100, 0,
tilted=0.25, released=0.25)
# Return to the "Change Grip/Order Screen"
time.sleep(2)
nx.press_buttons(controller_idx, [Buttons.A])
time.sleep(2)
nx.press_buttons(controller_idx, [Buttons.A])

View file

@ -6,6 +6,7 @@ from .controller import Controller
from .bluez import *
from .nxbt import Nxbt
from .nxbt import Buttons
from .nxbt import Sticks
from .nxbt import JOYCON_L
from .nxbt import JOYCON_R
from .nxbt import PRO_CONTROLLER

View file

@ -45,6 +45,12 @@ class Buttons():
ZL = 'ZL'
class Sticks():
RIGHT_STICK = "R_STICK"
LEFT_STICK = "L_STICK"
class NxbtCommands(Enum):
CREATE_CONTROLLER = 0
@ -209,6 +215,45 @@ class Nxbt():
return macro_id
def tilt_stick(self, controller_index, stick, x, y,
tilted=0.1, released=0.1, block=True):
if controller_index not in self.manager_state.keys():
raise ValueError("Specified controller does not exist")
if x >= 0:
x_parsed = f'+{x:03}'
else:
x_parsed = f'{x:04}'
if y >= 0:
y_parsed = f'+{y:03}'
else:
y_parsed = f'{y:04}'
macro = f'{stick}@{x_parsed}{y_parsed} {tilted}s\n{released}s'
# Get a unique ID to identify the button press
# so we can check when the controller is done inputting it
macro_id = os.urandom(24).hex()
self.task_queue.put({
"command": NxbtCommands.INPUT_MACRO,
"arguments": {
"controller_index": controller_index,
"macro": macro,
"macro_id": macro_id,
}
})
if block:
while True:
finished = (self.manager_state
[controller_index]["finished_macros"])
if macro_id in finished:
break
return macro_id
def stop_macro(self, controller_index, macro_id, block=True):
if controller_index not in self.manager_state.keys():