def __init__(self): self.KeyConfig = InputManager("KeyMapping/minecraft.csv") self.mappingDict = self.KeyConfig.mappingDict self.payload = Payload() self.winDim = (1024, 720) self.mouseSens = (4, 4) self.mouseDelta = (0, 0) title = "Key Input Retriever" pygame.init() pygame.display.set_caption(title) self.screen = pygame.display.set_mode(self.winDim) self.myFont = pygame.font.SysFont("Arial", 16, bold=True) self.textColor = pygame.Color(255, 255, 255) self.screenFillColor = pygame.Color(0, 0, 0)
def processInputs(self, payload: seriallib.Payload, keysDown: list, mouseDiff: tuple) -> seriallib.Payload: dPadDir = [0, 0] for button, mappedKeys in self.mappingDict.items(): if "mx" in mappedKeys or "my" in mappedKeys: if button in ["-LX","+LX"]: payload.setLeftX(128 + mouseDiff[0]) elif button in ["-LY","+LY"]: payload.setLeftY(128 + mouseDiff[1]) elif button in ["-RX","+RX"]: payload.setRightX(128 + mouseDiff[0]) elif button in ["-RY","+RY"]: payload.setRightY(128 + mouseDiff[1]) keysDown2 = [x+96 for x in keysDown if x != 306 and isinstance(x, int)] # If you hold ctrl + a key you get a different char value. A value that differs by 96 '''try: keysDown2 = [x for x in keysDown[keysDown.index(306)+1:] if isinstance(x, int)] if len(keysDown2) > 0: for i in range(len(keysDown2)): keysDown2[i] += 96 except ValueError: pass''' newKeysDown = [] for key in keysDown: if str(key).isdigit() == False: try: newKeysDown.append(self.mappingDict[key][0]) except KeyError: newKeysDown.append(key) else: newKeysDown.append(key) if any(key in newKeysDown for key in mappedKeys) or any(key in keysDown2 for key in mappedKeys): if button in constants.validButtonValues[12:]: payload.applyButtons(1 << (constants.validButtonValues.index(button) - 12)) elif button == "-LX": payload.setLeftX(0) elif button == "+LX": payload.setLeftX(255) elif button == "-LY": payload.setLeftY(0) elif button == "+LY": payload.setLeftY(255) elif button == "-RX": payload.setRightX(0) elif button == "+RX": payload.setRightX(255) elif button == "-RY": payload.setRightX(0) elif button == "+RY": payload.setRightX(255) elif button == "DLEFT": dPadDir[0] += -1 elif button == "DRIGHT": dPadDir[0] += 1 elif button == "DUP": dPadDir[1] += -1 elif button == "DDOWN": dPadDir[1] += 1 payload.setHatFromVector(dPadDir[0], dPadDir[1]) return payload
import sys import time import pygame from serial.tools import list_ports from switchlib import InputManager from seriallib import SerialManager, Payload BAUD = 38400 UPDATES_PER_SECOND = 60 payload = Payload() def getPortFromUser(): portList = list(list_ports.grep("")) if len(portList) == 0: raise LookupError("Unable to detect Serial Device.") indexPortListString = [ f"Index: {index}, Port: {port.device}, Description: {port.description}" for index, port in enumerate(portList) ] print(indexPortListString) while True: ind = input("What port index should be used? ") if not str.isdigit(ind): print(f"Value given is not a digit") elif not (0 <= int(ind) < len(portList)): print("Value given is not an index in the list")
def processInputs(self, payload: seriallib.Payload, keysDown: list, mouseDiff: tuple) -> seriallib.Payload: dPadDir = [0, 0] for button, mappedKeys in self.mappingDict.items(): if "mx" in mappedKeys or "my" in mappedKeys: if button in ["-LX", "+LX"]: payload.setLeftX(128 + mouseDiff[0]) elif button in ["-LY", "+LY"]: payload.setLeftY(128 + mouseDiff[1]) elif button in ["-RX", "+RX"]: payload.setRightX(128 + mouseDiff[0]) elif button in ["-RY", "+RY"]: payload.setRightY(128 + mouseDiff[1]) if any(key in keysDown for key in mappedKeys): if button in constants.validButtonValues[12:]: payload.applyButtons( 1 << (constants.validButtonValues.index(button) - 12)) elif button == "-LX": payload.setLeftX(0) elif button == "+LX": payload.setLeftX(255) elif button == "-LY": payload.setLeftY(0) elif button == "+LY": payload.setLeftY(255) elif button == "-RX": payload.setRightX(0) elif button == "+RX": payload.setRightX(255) elif button == "-RY": payload.setRightX(0) elif button == "+RY": payload.setRightX(255) elif button == "DLEFT": dPadDir[0] += -1 elif button == "DRIGHT": dPadDir[0] += 1 elif button == "DUP": dPadDir[1] += -1 elif button == "DDOWN": dPadDir[1] += 1 payload.setHatFromVector(dPadDir[0], dPadDir[1]) return payload
class Control: def __init__(self): self.KeyConfig = InputManager("KeyMapping/minecraft.csv") self.mappingDict = self.KeyConfig.mappingDict self.payload = Payload() self.winDim = (1024, 720) self.mouseSens = (4, 4) self.mouseDelta = (0, 0) title = "Key Input Retriever" pygame.init() pygame.display.set_caption(title) self.screen = pygame.display.set_mode(self.winDim) self.myFont = pygame.font.SysFont("Arial", 16, bold=True) self.textColor = pygame.Color(255, 255, 255) self.screenFillColor = pygame.Color(0, 0, 0) def readPortAsIntArr(self, ser) -> tuple: serialBytes = ser.read_all() intArray = [] for singleByte in serialBytes: if type(singleByte) == int: intArray.append(singleByte) else: intArray.append(int.from_bytes(singleByte, byteorder="big")) return tuple(intArray) def sendInput(self, ser, keysDown, pause_script, lockMouse): self.payload.resetAllInputs() self.keysDown = keysDown.copy() for event in pygame.event.get(): pygame.event.pump() if event.type == pygame.QUIT: return "exit" elif event.type == pygame.MOUSEMOTION: self.mouseDelta = event.rel elif event.type == pygame.MOUSEBUTTONDOWN: keyStr = f"m{event.button}" if not keyStr in self.keysDown: print("adding:", f"m{event.button}") self.keysDown.append(keyStr) # elif event.type == pygame.MOUSEBUTTONUP: # keyStr = f"m{event.button}" # if keyStr in keysDown: # print("removing:", f"m{event.button}") # keysDown.remove(keyStr) if pause_script == False: self.KeyConfig.processInputs( self.payload, self.keysDown, (self.mouseDelta[0] * self.mouseSens[0], self.mouseDelta[1] * self.mouseSens[1])) else: #keysDown = [self.mappingDict[x][0] for x in keysDown] #print(keysDown) self.KeyConfig.processInputs(self.payload, self.keysDown, (0, 0)) if lockMouse and pygame.mouse.get_focused(): pygame.mouse.set_pos(self.winDim[0] / 2, self.winDim[1] / 2) pygame.event.get(pygame.MOUSEMOTION) self.mouseDelta = (0, 0) self.screen.fill(self.screenFillColor) self.screen.blit( self.myFont.render(f"Sending:{str(self.payload)}", True, self.textColor), (0, 0)) self.screen.blit( self.myFont.render(f"Receiving:{self.readPortAsIntArr(ser)}", True, self.textColor), (0, 20)) pygame.display.flip() if pygame.mouse.get_focused() == False and pause_script == False: ser.write(bytearray([128, 128, 128, 128, 8, 0, 0])) else: ser.write(self.payload.asByteArray()) return "success" def quit(self): pygame.quit()
def processInputs(self, payload: seriallib.Payload, keysDown: list, leftJoy: list, rightJoy: list, hat: list) -> seriallib.Payload: dPadDir = [0, 0] payload.setLeftX(leftJoy[0]) payload.setLeftY(leftJoy[1]) payload.setRightX(rightJoy[0]) payload.setRightY(rightJoy[1]) dPadDir[0] = hat[0] dPadDir[1] = hat[1] * -1 for button, mappedKeys in self.mappingDict.items(): if any(key in keysDown for key in mappedKeys): if button in constants.validButtonValues[12:]: payload.applyButtons( 1 << (constants.validButtonValues.index(button) - 12)) payload.setHatFromVector(dPadDir[0], dPadDir[1]) return payload