Added more functionality

Added previously connected Switch finding

Cleaned up demo CLI command

Added ability to stop  macros

Fixed reconnecting and timing quirks

Bugfixes and polish
This commit is contained in:
Brikwerk 2020-08-01 23:11:47 -07:00
commit 7e99a36675
10 changed files with 745 additions and 182 deletions

95
demo.py
View file

@ -2,46 +2,40 @@ import time
from random import randint
import nxbt
from nxbt import Buttons
MACRO = """
B 0.1s
0.1s
B 0.1s
0.1s
B 0.1s
0.1s
B 0.1s
LOOP 12
B 0.1s
0.1s
1.5s
DPAD_RIGHT 0.075s
0.075s
A 0.1s
1.5s
DPAD_DOWN 1.0s
DPAD_DOWN 1.00s
A 0.1s
0.25s
DPAD_DOWN 0.8s
DPAD_DOWN 0.95s
A 0.1s
0.25s
L_STICK_PRESS 0.1s
1.0s
L_STICK@-100+000 0.75s
L_STICK@+000+100 0.75s
L_STICK@+100+000 0.75s
L_STICK@+000-100 0.75s
L_STICK@-100+000 0.5s
L_STICK@+000+100 0.5s
L_STICK@+100+000 0.5s
L_STICK@+000-100 0.5s
B 0.1s
0.25s
R_STICK_PRESS 0.1s
1.0s
R_STICK@-100+000 0.75s
R_STICK@+000+100 0.75s
R_STICK@+100+000 0.75s
R_STICK@+000-100 0.75s
B 0.1s
0.1s
B 0.1s
0.1s
B 0.1s
0.1s
R_STICK@-100+000 0.5s
R_STICK@+000+100 0.5s
R_STICK@+100+000 0.5s
R_STICK@+000-100 0.5s
LOOP 4
B 0.1s
0.1s
B 0.1s
0.4s
DPAD_LEFT 0.1s
@ -49,7 +43,7 @@ DPAD_LEFT 0.1s
A 0.1s
1.5s
A 0.1s
0.1s
5.0s
"""
@ -64,11 +58,16 @@ def random_colour():
if __name__ == "__main__":
# Init NXBT
nx = nxbt.Nxbt()
# Get a list of all available Bluetooth adapters
adapters = nx.get_available_adapters()
# Prepare a list to store the indexes of the
# created controllers.
controller_idxs = []
# Loop over all Bluetooth adapters and create
# Switch Pro Controllers
nx = nxbt.Nxbt()
adapters = nx.get_available_adapters()
controller_idxs = []
for i in range(0, len(adapters)):
index = nx.create_controller(
nxbt.PRO_CONTROLLER,
@ -77,16 +76,38 @@ if __name__ == "__main__":
colour_buttons=random_colour())
controller_idxs.append(index)
# 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)
# Run a macro on the last controller
# and don't wait for the macro to complete
nx.macro(controller_idxs[-1], MACRO, block=False)
print("Macro Started")
macro_id = nx.macro(controller_idx, MACRO, block=False)
time.sleep(3)
# Stop the macro
print("Stopping Macro")
nx.stop_macro(controller_idx, macro_id)
print("Stopped Macro")
# Check the state
while True:
time.sleep(1)
for key in nx.state.keys():
state = nx.state[key]
if not state["errors"]:
print(state)
else:
print(state["errors"])
# 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])
time.sleep(2)
# Enter the same macro, but block this time
print("Macro Started")
macro_id = nx.macro(controller_idx, MACRO)
print("Macro finished, going to the home screen...")
nx.press_buttons(controller_idx, [Buttons.B])
time.sleep(2)
nx.press_buttons(controller_idx, [Buttons.B])
time.sleep(2)
print("Exiting...")