Пример #1
0
class Controller():
    '''Main class for controlling the operations of the application'''
    def __init__(self):
        #Create a new Tkinter interface
        self.root = Tk()
        #Set an exit protocol
        self.root.protocol("WM_DELETE_WINDOW", self.exitRoot)

        #Create a model
        self.model = Model()
        self.model.loadConfig() #Load default configuration parameters
        #self.view  = View()

        #Start timer thread
        self.txTimer = TimerThread(self, "tmr")
        #Create joystick interface
        self.jsFrame = JoystickFrame(self.root)
        self.joystick = self.jsFrame.getJSHandle()
        self.statusBox = self.jsFrame.getTextHandle()

        #Initialise a telnet rxtx thread for wireless communication
        self.rxThread = RxTxThread(self,"rxtxthread", self.model.getHost(), self.model.getPort())
        if (self.rxThread.getTN() == 0):
            self.statusBox.setText('Could not establish a connection. Terminating...')
            return
        #Start Threads
        self.rxThread.start()
        self.txTimer.start()

        self.statusBox.setText('Connected\n')

        print self.rxThread.getRXText()

        self.rxThread.checkConnection()
        self.root.mainloop()

    def processMessages(self, messages):
        '''Displays received messages in a window box'''
        for msg in messages:
            self.statusBox.setText(msg + '\n')

    def transmitControl(self):
        '''Transmits the coordinates of the joystick if it it being actuated.
        Not complete in interfacing.'''
        if not self.joystick.isReleased():         #Joystick in use
            spdL,spdR =  self.joystick.getSpeeds() #Retrieve position as speeds
            print spdL, spdR
        if self.jsFrame.keyReady():                # WASD Control
            keyChar = self.jsFrame.getJSKey()      # Retrieve valid keypresses
            self.statusBox.setText("Pressed: "+keyChar+"\n")

            self.rxThread.txCmd(keyChar)           #Transmit typed character

    def exitRoot(self):
        '''Protocol for exiting main application'''
        self.rxThread.txCmd('!') #Stop robot
        self.txTimer.pause(True) #Stop timer
        self.rxThread.stop()     #Stop thread
        self.root.destroy()