From 1619c96cd735abb0be720b52f302957710ec97b2 Mon Sep 17 00:00:00 2001 From: Brikwerk Date: Sun, 2 Aug 2020 21:56:42 -0700 Subject: [PATCH] Added stick tilt functionality --- demo.py | 13 +++++++++++-- nxbt/__init__.py | 1 + nxbt/nxbt.py | 45 +++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 57 insertions(+), 2 deletions(-) diff --git a/demo.py b/demo.py index 8d47718..75bcbca 100644 --- a/demo.py +++ b/demo.py @@ -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]) diff --git a/nxbt/__init__.py b/nxbt/__init__.py index c447e5b..0582752 100644 --- a/nxbt/__init__.py +++ b/nxbt/__init__.py @@ -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 diff --git a/nxbt/nxbt.py b/nxbt/nxbt.py index 64b49ba..5d6d606 100644 --- a/nxbt/nxbt.py +++ b/nxbt/nxbt.py @@ -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():