def main(): # TODO CLI pygame.init() pygame.joystick.init() joystick = {} ser = {} zumo = {} # init each joystick for i in range(pygame.joystick.get_count()): # joystick joystick[i] = InputDevice() joystick[i].start(i) steeringAxis, accBtn, revBtn, powBtn = getConfig(joystick[i]) joystick[i].configure(steeringAxis, accBtn, revBtn, powBtn) # serial/Zumo port = choose_serial() ser[i] = serial.Serial(port, baudrate=9600) zumo[i] = Zumo(ser[i], 0.01) zumo[i].beginControlThrustSteer(joystick[i].getSpeed, joystick[i].getDir) # 1 thread per zumo InputDevice.startReadThread(0.01) # 1 thread for all joysticks print("q to exit") read = "" while read.lower() is not "q": read = raw_input('>') print joystick[0].getSpeed()
def choose_serial(): print("Select port:") ports = InputDevice.serialPorts() for i in range(len(ports)): print("{0}: {1}".format(i+1, ports[i])) while True: s = raw_input(">") try: return ports[int(s)-1] except ValueError: print("Select Port:")
def main(stdscr): """The Main Game!!!""" # init pygame #pygame.init(); pygame.display.init() pygame.joystick.init(); noPlayers = pygame.joystick.get_count(); # no controllers, no game. if noPlayers == 0: raise(Exception('no controllers detected')) return # init CLI cli = CLI(stdscr, noPlayers); sys.stdout = cli; Game.cli = cli; cli.setEventHandler(Game.inputHandler); # run through setup steps Game.setupState = 0; while Game.setupState < len(Game.setupStep): # still setting up cli.log('Setup step %i of %i' % (Game.setupState + 1, len(Game.setupStep))); cli.instructions = Game.setupStep[Game.setupState]["inst"]; cli.inputloop(); time.sleep(0.2); # throttle cli.instructions = '(S)tart, (L)ist Serial or (Q)uit'; # update timer thread def update(): cli.updateTime(Game.timer.getTime()); Game.cli.printGameScreen(); t = threading.Timer(0.1, update); t.daemon = True; t.start(); update(); # start # init each player for i in range(noPlayers): # joystick def playprint(str): cli.playerLog(i, str); try: Game.joystick[i] = InputDevice(playprint); except IndexError: Game.joystick.append(InputDevice(playprint)); Game.joystick[i].start(i); steeringAxis = Game.conf["controller"]["steeringAxis"]; accBtn = Game.conf["controller"]["accBtn"]; revBtn = Game.conf["controller"]["revBtn"]; powBtn = Game.conf["controller"]["powBtn"]; Game.joystick[i].configure(steeringAxis, accBtn, revBtn, powBtn); # player try: Game.player[i] = Player(); except IndexError: Game.player.append(Player()); # serial/Zumo try: port = Game.conf["zumoser"][i]; try: Game.ser[i] = serial.Serial(port, baudrate=9600); except IndexError: Game.ser.append(serial.Serial(port, baudrate=9600)); try: Game.zumo[i] = Zumo(Game.ser[i], 0.01); except IndexError: Game.zumo.append(Zumo(Game.ser[i], 0.01)); # 1 thread per zumo Game.zumo[i].beginControlThrustSteer( Game.joystick[i].getSpeed, Game.joystick[i].getDir, Game.player[i].getBoost) cli.playerTrackXY(i, Game.joystick[i].getDir, Game.joystick[i].getSpeed) playprint('Initialised on %s' % port) except OSError: playprint('Could not initialise.') InputDevice.startReadThread(0.01); # 1 thread for all joysticks while cli.running: cli.inputloop();
def print_serial(stream): stream("Detected available serial ports:") ports = InputDevice.serialPorts() for i in range(len(ports)): stream("{0}: {1}".format(i+1, ports[i])) """