async def make_fish_food(controller_state: ControllerState): """ Example controller script. Navigates to the "Test Controller Buttons" menu and presses all buttons. """ if controller_state.get_controller() != Controller.PRO_CONTROLLER: raise ValueError('This script only works with the Pro Controller!') # waits until controller is fully connected await controller_state.connect() await ainput(prompt='press <enter> to start.') user_input = asyncio.ensure_future( ainput(prompt='Making fish foods... Press <enter> to stop.')) # push all buttons consecutively until user input while not user_input.done(): await button_push(controller_state, 'a') await asyncio.sleep(0.3) if user_input.done(): break # await future to trigger exceptions in case something went wrong await user_input
async def hold_b_button(controller_state: ControllerState): if controller_state.get_controller() != Controller.PRO_CONTROLLER: raise ValueError('This script only works with the Pro Controller!') await controller_state.connect() await button_push(controller_state, 'b', sec=5)
async def buy(controller_state: ControllerState): if controller_state.get_controller() != Controller.PRO_CONTROLLER: raise ValueError('This script only works with the Pro Controller!') await controller_state.connect() await ainput( prompt= '动森购买器 Make sure the Animal crossing in shop and press <enter> to continue.' ) user_input = asyncio.ensure_future( ainput(prompt='Buying gifts... Press <enter> to stop.')) while not user_input.done(): await button_push(controller_state, 'a') await button_push(controller_state, 'b') await asyncio.sleep(0.4) await button_push(controller_state, 'a') await button_push(controller_state, 'b') await asyncio.sleep(0.4) await button_push(controller_state, 'down') await button_push(controller_state, 'a') await button_push(controller_state, 'b') await asyncio.sleep(1.5) await button_push(controller_state, 'a') await button_push(controller_state, 'b') await asyncio.sleep(0.4) await button_push(controller_state, 'a') await button_push(controller_state, 'b') await asyncio.sleep(0.4) await user_input
async def wait_long(controller_state: ControllerState): if controller_state.get_controller() != Controller.PRO_CONTROLLER: raise ValueError('This script only works with the Pro Controller!') await controller_state.connect() await asyncio.sleep(1)
async def record_keyboard( controller_state: ControllerState ): #this method binds keyboard to conroller and records input for later playback if controller_state.get_controller() != Controller.PRO_CONTROLLER: raise ValueError('This script only works with the Pro Controller!') # waits until controller is fully connected await controller_state.connect() print('Using only letters and numbers, type a name for this recording') print('Then press <enter> to start recording keyboard control.') recordingName = await ainput(prompt='Recording name:') #pixels = neopixel.NeoPixel(board.D12, 6, auto_write=False) #button state handler callbacks savedRecordings = shelve.open('savedRecs', writeback=True) LeftStick = controller_state.l_stick_state RightStick = controller_state.r_stick_state bindKeyboard(controller_state) keyboard.start_recording() pixels.fill((0, 0, 0)) pixels.fill((10, 0, 0)) pixels.fill((10, 0, 0)) await ainput( prompt='Press <enter> to stop recording and exit keyboard control.') recording = keyboard.stop_recording() pixels.fill((0, 0, 0)) pixels.fill((0, 0, 0)) keyboard.unhook_all() savedRecordings[recordingName] = recording savedRecordings.close() ControllerCLI._set_stick(RightStick, 'center', None) ControllerCLI._set_stick(LeftStick, 'center', None) await controller_state.send()
async def recording_playback(controller_state: ControllerState): #This method replays saved recordings if controller_state.get_controller() != Controller.PRO_CONTROLLER: raise ValueError('This script only works with the Pro Controller!') # waits until controller is fully connected await controller_state.connect() savedRecordings = shelve.open('savedRecs', writeback=True) LeftStick = controller_state.l_stick_state RightStick = controller_state.r_stick_state recList = list(savedRecordings.keys()) print('Saved Recordings:') print(recList) print('Enter the name of the recording you want to playback') print('Then press <enter> to start playback.') recordingName = await ainput(prompt='Recording name:') if recordingName in recList: recording = savedRecordings[recordingName] speed_factor = 1 last_time = None for event in recording: if speed_factor > 0 and last_time is not None: time.sleep((event.time - last_time) / speed_factor) last_time = event.time key = event.scan_code or event.name btnTrans = keyToConBtn(key) await directStateSet(btnTrans, controller_state) if event.event_type == keyboard.KEY_DOWN else await directStateUNSet(btnTrans, controller_state) keyboard.unhook_all() ControllerCLI._set_stick(RightStick, 'center', None) ControllerCLI._set_stick(LeftStick, 'center', None) await controller_state.send() else: print('Recording name not recognized')
async def wait(controller_state: ControllerState): if controller_state.get_controller() != Controller.PRO_CONTROLLER: raise ValueError('This script only works with the Pro Controller!') await controller_state.connect() # TODO assuming controls will go through (no junk), this is fine await asyncio.sleep(.2)
async def run_at_start(controller_state: ControllerState): if controller_state.get_controller() != Controller.PRO_CONTROLLER: raise ValueError('This script only works with the Pro Controller!') await controller_state.connect() draw.rectangle((0, 0, width, height), outline=0, fill=0) draw.text((x + 12, top + 32), str("Switch Connected!"), font=font, fill=255) draw.text((x + 6, top + 40), str("Going back to game."), font=font, fill=255) disp.image(image) disp.display() await button_push(controller_state, 'a') await asyncio.sleep(1) await button_push(controller_state, 'home') await asyncio.sleep(1) await button_push(controller_state, 'left', sec=2) await asyncio.sleep(0.3) await button_push(controller_state, 'a') await asyncio.sleep(3) amiibo_button = ask_button(controller_state) #await button_push(controller_state, 'l&&r') while True: draw.rectangle((0, 0, width, height), outline=0, fill=0) draw.text((x+42,top), str("SW1 = A"),font=font, fill=255) draw.text((x+36, top + 8), str("SW2 = L+R"), font=font, fill=255) draw.text((x+42, top + 16), str("SW3 = " + amiibo_button), font=font, fill=255) draw.text((x+40, top + 32), str("SW1+2 = EXIT"), font=font, fill=255) draw.text((x+30, top + 48), str("Waiting on "), font=font, fill=255) draw.text((x+24, top + 56), str("button press"), font=font, fill=255) disp.image(image) disp.display() #time.sleep(.1) button_state1 = GPIO.input(sw1) button_state2 = GPIO.input(sw2) button_state3 = GPIO.input(sw3) if button_state1 == GPIO.LOW: await button_push(controller_state, 'a') if button_state2 == GPIO.LOW: await button_press(controller_state, 'l') await button_press(controller_state, 'r') await asyncio.sleep(1.5) await button_release(controller_state, 'l') await button_release(controller_state, 'r') if button_state3 == GPIO.LOW: await button_push(controller_state, str(amiibo_button)) await asyncio.sleep(3) draw.rectangle((0, 0, width, height), outline=0, fill=0) disp.image(image) disp.display() break if button_state1 == GPIO.LOW and button_state2 == GPIO.LOW: break
async def keyboard_control(controller_state: ControllerState):# this method binds keyboard to controller for CLI keyboard control of switch if controller_state.get_controller() != Controller.PRO_CONTROLLER: raise ValueError('This script only works with the Pro Controller!') # waits until controller is fully connected await controller_state.connect() await ainput(prompt='Press <enter> to start keyboard control.') #button state handler callbacks LeftStick = controller_state.l_stick_state RightStick = controller_state.r_stick_state bindKeyboard(controller_state) await ainput(prompt='Press <enter> to exit keyboard control.') keyboard.unhook_all() ControllerCLI._set_stick(RightStick, 'center', None) ControllerCLI._set_stick(LeftStick, 'center', None) await controller_state.send()
async def delete_recording(controller_state: ControllerState): #This method deletes saved recordings if controller_state.get_controller() != Controller.PRO_CONTROLLER: raise ValueError('This script only works with the Pro Controller!') # waits until controller is fully connected await controller_state.connect() savedRecordings = shelve.open('savedRecs', writeback=True) recList = list(savedRecordings.keys()) print('Saved Recordings:') print(recList) print('Enter the name of the recording you want to delete') print('Then press <enter> to delete.') recordingName = await ainput(prompt='Recording name:') if recordingName in recList: del savedRecordings[recordingName] print('Recording deleted') else: print('Recording name not recognized')
async def test_controller_buttons(controller_state: ControllerState): """ Example controller script. Navigates to the "Test Controller Buttons" menu and presses all buttons. """ if controller_state.get_controller() != Controller.PRO_CONTROLLER: raise ValueError('This script only works with the Pro Controller!') # waits until controller is fully connected await controller_state.connect() await ainput( prompt= 'Make sure the Switch is in the Home menu and press <enter> to continue.' ) """ # We assume we are in the "Change Grip/Order" menu of the switch await button_push(controller_state, 'home') # wait for the animation await asyncio.sleep(1) """ # Goto settings await button_push(controller_state, 'down', sec=1) await button_push(controller_state, 'right', sec=2) await asyncio.sleep(0.3) await button_push(controller_state, 'left') await asyncio.sleep(0.3) await button_push(controller_state, 'a') await asyncio.sleep(0.3) # go all the way down await button_push(controller_state, 'down', sec=4) await asyncio.sleep(0.3) # goto "Controllers and Sensors" menu for _ in range(2): await button_push(controller_state, 'up') await asyncio.sleep(0.3) await button_push(controller_state, 'right') await asyncio.sleep(0.3) # go all the way down await button_push(controller_state, 'down', sec=3) await asyncio.sleep(0.3) # goto "Test Input Devices" menu await button_push(controller_state, 'up') await asyncio.sleep(0.3) await button_push(controller_state, 'a') await asyncio.sleep(0.3) # goto "Test Controller Buttons" menu await button_push(controller_state, 'a') await asyncio.sleep(0.3) # push all buttons except home and capture button_list = controller_state.button_state.get_available_buttons() if 'capture' in button_list: button_list.remove('capture') if 'home' in button_list: button_list.remove('home') user_input = asyncio.ensure_future( ainput(prompt='Pressing all buttons... Press <enter> to stop.')) # push all buttons consecutively until user input while not user_input.done(): for button in button_list: await button_push(controller_state, button) await asyncio.sleep(0.1) if user_input.done(): break # await future to trigger exceptions in case something went wrong await user_input # go back to home await button_push(controller_state, 'home')
async def test_controller_buttons(controller_state: ControllerState): if controller_state.get_controller() != Controller.PRO_CONTROLLER: raise ValueError('This script only works with the Pro Controller!') # waits until controller is fully connected await controller_state.connect() await ainput( prompt= 'Make sure your character is in the right place press <enter> to continue.' ) user_input = asyncio.ensure_future( ainput(prompt='Starting the script... Press <enter> to stop.')) # push all buttons consecutively until user input while not user_input.done(): await button_push(controller_state, 'a') await asyncio.sleep(1) await button_push(controller_state, 'a') await asyncio.sleep(1) await button_push(controller_state, 'a') await asyncio.sleep(1) await button_push(controller_state, 'a') await asyncio.sleep(5) await button_push(controller_state, 'a') await asyncio.sleep(1) await button_push(controller_state, 'a') await asyncio.sleep(1) await button_push(controller_state, 'a') await asyncio.sleep(1) await button_push(controller_state, 'x') await asyncio.sleep(1) await button_push(controller_state, 'a') await asyncio.sleep(1) await button_push(controller_state, 'up') await asyncio.sleep(1) await button_push(controller_state, 'a') await asyncio.sleep(1) await button_push(controller_state, 'a') await asyncio.sleep(1) await button_push(controller_state, 'b') await asyncio.sleep(1) await button_push(controller_state, 'plus') await asyncio.sleep(1) await button_push(controller_state, 'up') await asyncio.sleep(1) await button_push(controller_state, 'down') await asyncio.sleep(0.5) await button_push(controller_state, 'down') await asyncio.sleep(0.5) await button_push(controller_state, 'a') await asyncio.sleep(0.5) await button_push(controller_state, 'a') await asyncio.sleep(20) if user_input.done(): break
async def xbox( controller_state: ControllerState ): # this method binds keyboard to controller for CLI keyboard control of switch if controller_state.get_controller() != Controller.PRO_CONTROLLER: raise ValueError('This script only works with the Pro Controller!') # waits until controller is fully connected await controller_state.connect() lv = 0 lh = 0 rv = 0 rh = 0 #button state handler callbacks Working = True start_time = time.time() while Working: for event in pygame.event.get(): if event.type == pygame.KEYDOWN: if event.key == pygame.K_RETURN: Working = False break if event.type == pygame.USEREVENT: pygame.time.set_timer(pygame.USEREVENT, 0) Working = False break LeftStick = controller_state.l_stick_state RightStick = controller_state.r_stick_state joysticks = [ pygame.joystick.Joystick(x) for x in range(pygame.joystick.get_count()) ] for i in range(pygame.joystick.get_count()): joysticks[i].init() for i in range(pygame.joystick.get_count()): #Left Stick if round(lh, -1) != round( 2048 + joysticks[i].get_axis(0) * 1792, -1) or round( lv, -1) != round( 2048 + joysticks[i].get_axis(1) * 1792, -1): lh = (2048 + joysticks[i].get_axis(0) * 1792) lv = (2048 - joysticks[i].get_axis(1) * 1792) if lh < 2150 and lh > 1950: lh = 2048 if lv < 2150 and lv > 1950: lv = 2048 ControllerCLI._set_stick(LeftStick, 'h', lh) ControllerCLI._set_stick(LeftStick, 'v', lv) #Right Stick if round(rh, -1) != round( 2048 + joysticks[i].get_axis(3) * 1792, -1) or round( rv, -1) != round( 2048 + joysticks[i].get_axis(4) * 1792, -1): rh = (2048 + joysticks[i].get_axis(3) * 1792) rv = (2048 - joysticks[i].get_axis(4) * 1792) if rh < 2150 and rh > 1950: rh = 2048 if rv < 2150 and rv > 1950: rv = 2048 ControllerCLI._set_stick(RightStick, 'h', rh) ControllerCLI._set_stick(RightStick, 'v', rv) #Triggers if joysticks[i].get_axis(2) >= 0.2: controller_state.button_state.set_button('zl') else: controller_state.button_state.set_button('zl', pushed=False) if joysticks[i].get_axis(5) >= -0.2: controller_state.button_state.set_button('zr') else: controller_state.button_state.set_button('zr', pushed=False) #Buttons if joysticks[i].get_button(0) == 1: #B controller_state.button_state.set_button('b') else: controller_state.button_state.set_button('b', pushed=False) if joysticks[i].get_button(1) == 1: #A controller_state.button_state.set_button('a') else: controller_state.button_state.set_button('a', pushed=False) if joysticks[i].get_button(2) == 1: #Y controller_state.button_state.set_button('y') else: controller_state.button_state.set_button('y', pushed=False) if joysticks[i].get_button(3) == 1: #X controller_state.button_state.set_button('x') else: controller_state.button_state.set_button('x', pushed=False) #Trigger Buttons if joysticks[i].get_button(4) == 1: #Left controller_state.button_state.set_button('l') else: controller_state.button_state.set_button('l', pushed=False) if joysticks[i].get_button(5) == 1: #Right controller_state.button_state.set_button('r') else: controller_state.button_state.set_button('r', pushed=False) #Other Buttons if joysticks[i].get_button(6) == 1: #Minius controller_state.button_state.set_button('minus') else: controller_state.button_state.set_button('minus', pushed=False) if joysticks[i].get_button(7) == 1: #plus controller_state.button_state.set_button('plus') else: controller_state.button_state.set_button('plus', pushed=False) if joysticks[i].get_button(8) == 1: #Home controller_state.button_state.set_button('home') else: controller_state.button_state.set_button('home', pushed=False) if joysticks[i].get_button(9) == 1: #Left Stick Click controller_state.button_state.set_button('l_stick') else: controller_state.button_state.set_button('l_stick', pushed=False) if joysticks[i].get_button(10) == 1: #Right Stick Click controller_state.button_state.set_button('r_stick') else: controller_state.button_state.set_button('r_stick', pushed=False) #Dpad hat = joysticks[i].get_hat(0) if hat[0] == 1: #Right controller_state.button_state.set_button('right') else: controller_state.button_state.set_button('right', pushed=False) if hat[0] == -1: #Left controller_state.button_state.set_button('left') else: controller_state.button_state.set_button('left', pushed=False) if hat[1] == 1: #Up controller_state.button_state.set_button('up') else: controller_state.button_state.set_button('up', pushed=False) if hat[1] == -1: #Down controller_state.button_state.set_button('down') else: controller_state.button_state.set_button('down', pushed=False) await controller_state.send()