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 directStateUNSet(btnTrans, controller_state: ControllerState): #this method sets button/stick states during recording playback (button RELEASE/ stick CENTER) LeftStick = controller_state.l_stick_state RightStick = controller_state.r_stick_state btnsList = ['x', 'y', 'b', 'a', 'plus', 'minus', 'home', 'capture', 'zl', 'zr', 'l', 'r', 'up', 'down', 'left', 'right'] lStickList = ['lStickUp', 'lStickDown', 'lStickL', 'lStickR'] rStickList = ['rStickUp', 'rStickDown', 'rStickL', 'rStickR'] if btnTrans in btnsList: controller_state.button_state.set_button(btnTrans, pushed=False) await controller_state.send() elif btnTrans in lStickList: ControllerCLI._set_stick(LeftStick, 'center', None) await controller_state.send() elif btnTrans in rStickList: ControllerCLI._set_stick(RightStick, 'center', None) await controller_state.send()
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 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()
async def directStateSet( btnTrans, controller_state: ControllerState ): #this method sets button/stick states during recording playback (button PRESS/ stick UDLR) LeftStick = controller_state.l_stick_state RightStick = controller_state.r_stick_state btnsList = [ 'x', 'y', 'b', 'a', 'plus', 'minus', 'home', 'capture', 'zl', 'zr', 'l', 'r', 'up', 'down', 'left', 'right' ] lStickList = ['lStickUp', 'lStickDown', 'lStickL', 'lStickR'] rStickList = ['rStickUp', 'rStickDown', 'rStickL', 'rStickR'] if btnTrans in btnsList: #print(btnTrans) controller_state.button_state.set_button(btnTrans) await controller_state.send() elif btnTrans in lStickList: #print(btnTrans) if btnTrans == 'lStickDown': ControllerCLI._set_stick(LeftStick, 'down', None) await controller_state.send() elif btnTrans == 'lStickUp': ControllerCLI._set_stick(LeftStick, 'up', None) await controller_state.send() elif btnTrans == 'lStickL': ControllerCLI._set_stick(LeftStick, 'left', None) await controller_state.send() elif btnTrans == 'lStickR': ControllerCLI._set_stick(LeftStick, 'right', None) await controller_state.send() elif btnTrans in rStickList: if btnTrans == 'rStickDown': ControllerCLI._set_stick(RightStick, 'down', None) await controller_state.send() elif btnTrans == 'rStickUp': ControllerCLI._set_stick(RightStick, 'up', None) await controller_state.send() elif btnTrans == 'rStickL': ControllerCLI._set_stick(RightStick, 'left', None) await controller_state.send() elif btnTrans == 'rStickR': ControllerCLI._set_stick(RightStick, 'right', None) await controller_state.send()
def RStickUnpress(self): ControllerCLI._set_stick(RightStick, 'center', None)
def RightRStickPress(self): ControllerCLI._set_stick(RightStick, 'right', None)
def LeftRStickPress(self): ControllerCLI._set_stick(RightStick, 'left', None)
def DownRStickPress(self): ControllerCLI._set_stick(RightStick, 'down', None)
def UpRStickPress(self): ControllerCLI._set_stick(RightStick, 'up', None)
def LStickUnpress(self): ControllerCLI._set_stick(LeftStick, 'center', None)
def LeftLStickPress(self): ControllerCLI._set_stick(LeftStick, 'left', None)
def DownLStickPress(self): ControllerCLI._set_stick(LeftStick, 'down', None)
def UpLStickPress(self): ControllerCLI._set_stick(LeftStick, 'up', None)