예제 #1
0
def main():
    app = QApplication(sys.argv)
    w = MainWidget()
    w.setWindowTitle("Игра \"Жизнь\"")
    w.setFixedSize(w.geometry().width(), w.geometry().height())
    w.show()
    return app.exec_()
예제 #2
0
from mainwidget import MainWidget
from PyQt5.QtWidgets import QApplication

if __name__ == "__main__":

    app = QApplication([])
    app.setStyle("Fusion")

    window = MainWidget()

    window.show()
    app.exec_()
class MainController(QObject):

    appStart = pyqtSignal()
    newCommand = pyqtSignal(tuple)

    def __init__(self):
        super(MainController, self).__init__()
        self.ser = Serial(WIFLY_SERIAL_PORT, WIFLY_BAUD_RATE)
        self.wiflyReceiver = WiflyReceiver(self.ser)
        self.wiflySender = WiflySender(self.ser)
        self.rover = Rover()
        self.mainWidget = MainWidget()
        self.wiflyReceiverThread = QThread()
        self.wiflyReceiver.moveToThread(self.wiflyReceiverThread)
        self.wiflySenderThread = QThread()
        self.wiflySender.moveToThread(self.wiflySenderThread)
        self.simState = SIMULATION_STATE_PHASE_1
        self.simTimer = QTimer()
        self.simTimer.setSingleShot(True)

        self.wiflyReceiver.msgReceived.connect(self.mainWidget.appendMsg)
        self.wiflyReceiver.msgReceived.connect(self.rover.processData)
        self.newCommand.connect(self.wiflySender.sendMsg)
        self.appStart.connect(self.wiflyReceiver.processMsg)
        self.mainWidget.ui.gearSlider.valueChanged.connect(
            self.manualGearChange)
        self.mainWidget.ui.upButton.clicked.connect(self.manualMoveForward)
        self.mainWidget.ui.downButton.clicked.connect(self.manualMoveBackward)
        self.mainWidget.ui.leftButton.clicked.connect(self.manualMoveLeft)
        self.mainWidget.ui.rightButton.clicked.connect(self.manualMoveRight)
        self.mainWidget.ui.brakeButton.clicked.connect(self.manualStop)
        self.mainWidget.ui.simulationButton.clicked.connect(
            self.simulationStart)
        self.rover.newRoverPosition.connect(self.drawRover)
        self.rover.newWallDetected.connect(self.drawNewWall)
        self.simTimer.timeout.connect(self.simulationUpdate)

        self.mapScene = QGraphicsScene(0, 0, WORLD_X / CANVAS_RATIO,
                                       WORLD_Y / CANVAS_RATIO)
        self.mainWidget.ui.mappingGraphicsView.setScene(self.mapScene)

        self.roverRect = QGraphicsRectItem()
        self.mapScene.addItem(self.roverRect)
        """
        rect1 = QGraphicsRectItem()
        rect2 = QGraphicsRectItem()
        self.mapScene.addItem(rect1)
        self.mapScene.addItem(rect2)
        rect1.setRect(100, 100, 20, 40)
        rect2.setRect(100, 100, 20, 40)
        #rect.moveBy(10, 50)
        rect2.setTransformOriginPoint(100, 100)
        rect2.setRotation(-10)
        print rect1.rect().center()
        #print rect2.transformOriginPoint().x(), rect2.transformOriginPoint().y()
        """

    @pyqtSlot(tuple, tuple)
    def drawNewWall(self, wallFront, wallRear):
        pFront = QGraphicsRectItem(wallFront[0] / CANVAS_RATIO,
                                   wallFront[1] / CANVAS_RATIO, DOT_SIZE,
                                   DOT_SIZE)
        pRear = QGraphicsRectItem(wallRear[0] / CANVAS_RATIO,
                                  wallRear[1] / CANVAS_RATIO, DOT_SIZE,
                                  DOT_SIZE)
        self.mapScene.addItem(pFront)
        self.mapScene.addItem(pRear)

    @pyqtSlot(tuple, float)
    def drawRover(self, center, orientation):
        self.roverRect.setRect((center[0] - ROVER_WIDTH / 2) / CANVAS_RATIO,
                               (center[1] - ROVER_LENGTH / 2) / CANVAS_RATIO,
                               ROVER_WIDTH / CANVAS_RATIO,
                               ROVER_LENGTH / CANVAS_RATIO)
        self.roverRect.setTransformOriginPoint(center[0] / CANVAS_RATIO,
                                               center[1] / CANVAS_RATIO)
        self.roverRect.setRotation(math.degrees(-orientation))

    @pyqtSlot()
    def manualGearChange(self):
        gear = self.mainWidget.ui.gearSlider.value()
        self.mainWidget.ui.gearLcdNumber.display(gear)
        self.rover.roverGear = gear
        self.rover.updateMotorCommand()
        self.newCommand.emit(tuple(self.rover.commandMsg))

    @pyqtSlot()
    def manualMoveForward(self):
        self.rover.roverDirection = ROVER_DIRECTION_FORWARD
        self.rover.updateMotorCommand()
        self.newCommand.emit(tuple(self.rover.commandMsg))

    @pyqtSlot()
    def manualMoveBackward(self):
        self.rover.roverDirection = ROVER_DIRECTION_BACKWARD
        self.rover.updateMotorCommand()
        self.newCommand.emit(tuple(self.rover.commandMsg))

    @pyqtSlot()
    def manualMoveLeft(self):
        self.rover.roverDirection = ROVER_DIRECTION_LEFT
        self.rover.updateMotorCommand()
        self.newCommand.emit(tuple(self.rover.commandMsg))

    @pyqtSlot()
    def manualMoveRight(self):
        self.rover.roverDirection = ROVER_DIRECTION_RIGHT
        self.rover.updateMotorCommand()
        self.newCommand.emit(tuple(self.rover.commandMsg))

    @pyqtSlot()
    def manualStop(self):
        self.mainWidget.ui.gearSlider.setValue(0)

    @pyqtSlot()
    def simulationStart(self):
        self.simState == SIMULATION_STATE_PHASE_1
        self.simTimer.start(5000)

    @pyqtSlot()
    def simulationUpdate(self):
        if self.simState == SIMULATION_STATE_PHASE_1:
            self.simState = SIMULATION_STATE_PHASE_2
            self.rover.roverGear = 2
            self.rover.roverDirection = ROVER_DIRECTION_FORWARD
            self.rover.updateMotorCommand()
            self.newCommand.emit(tuple(self.rover.commandMsg))
            self.simTimer.start(16000)
        elif self.simState == SIMULATION_STATE_PHASE_2:
            self.simState = SIMULATION_STATE_PHASE_3
            self.rover.roverDirection = ROVER_DIRECTION_BACKWARD
            self.rover.updateMotorCommand()
            self.newCommand.emit(tuple(self.rover.commandMsg))
            self.simTimer.start(6100)
        elif self.simState == SIMULATION_STATE_PHASE_3:
            self.simState = SIMULATION_STATE_PHASE_4
            self.rover.roverDirection = ROVER_DIRECTION_LEFT
            self.rover.updateMotorCommand()
            self.newCommand.emit(tuple(self.rover.commandMsg))
            self.simTimer.start(3500)
        elif self.simState == SIMULATION_STATE_PHASE_4:
            self.simState = SIMULATION_STATE_PHASE_5
            self.rover.roverDirection = ROVER_DIRECTION_BACKWARD
            self.rover.updateMotorCommand()
            self.newCommand.emit(tuple(self.rover.commandMsg))
            self.simTimer.start(8300)
        elif self.simState == SIMULATION_STATE_PHASE_5:
            self.rover.roverGear = 0
            self.rover.roverDirection = ROVER_DIRECTION_STOP
            self.rover.updateMotorCommand()
            self.newCommand.emit(tuple(self.rover.commandMsg))

    def start(self):
        self.mainWidget.show()
        self.wiflyReceiverThread.start()
        self.wiflySenderThread.start()
        self.appStart.emit()
예제 #4
0
import sys

from PySide6.QtWidgets import QApplication
from mainwidget import MainWidget

if __name__ == "__main__":
    app = QApplication(sys.argv)

    widget = MainWidget()
    widget.show()

    sys.exit(app.exec_())
class MainController(QObject):

    appStart = pyqtSignal()
    newCommand = pyqtSignal(tuple)

    def __init__(self):
        super(MainController, self).__init__()
        self.ser = Serial(WIFLY_SERIAL_PORT, WIFLY_BAUD_RATE)
        self.wiflyReceiver = WiflyReceiver(self.ser)
        self.wiflySender = WiflySender(self.ser)
        self.rover = Rover()
        self.mainWidget = MainWidget()
        self.wiflyReceiverThread = QThread()
        self.wiflyReceiver.moveToThread(self.wiflyReceiverThread)
        self.wiflySenderThread = QThread()
        self.wiflySender.moveToThread(self.wiflySenderThread)
        self.simState = SIMULATION_STATE_PHASE_1
        self.simTimer = QTimer()
        self.simTimer.setSingleShot(True)

        self.wiflyReceiver.msgReceived.connect(self.mainWidget.appendMsg)
        self.wiflyReceiver.msgReceived.connect(self.rover.processData)
        self.newCommand.connect(self.wiflySender.sendMsg)
        self.appStart.connect(self.wiflyReceiver.processMsg)
        self.mainWidget.ui.gearSlider.valueChanged.connect(self.manualGearChange)
        self.mainWidget.ui.upButton.clicked.connect(self.manualMoveForward)
        self.mainWidget.ui.downButton.clicked.connect(self.manualMoveBackward)
        self.mainWidget.ui.leftButton.clicked.connect(self.manualMoveLeft)
        self.mainWidget.ui.rightButton.clicked.connect(self.manualMoveRight)
        self.mainWidget.ui.brakeButton.clicked.connect(self.manualStop)
        self.mainWidget.ui.simulationButton.clicked.connect(self.simulationStart)
        self.rover.newRoverPosition.connect(self.drawRover)
        self.rover.newWallDetected.connect(self.drawNewWall)
        self.simTimer.timeout.connect(self.simulationUpdate)

        self.mapScene = QGraphicsScene(0, 0, WORLD_X / CANVAS_RATIO, WORLD_Y / CANVAS_RATIO)
        self.mainWidget.ui.mappingGraphicsView.setScene(self.mapScene)

        self.roverRect = QGraphicsRectItem()
        self.mapScene.addItem(self.roverRect)

        """
        rect1 = QGraphicsRectItem()
        rect2 = QGraphicsRectItem()
        self.mapScene.addItem(rect1)
        self.mapScene.addItem(rect2)
        rect1.setRect(100, 100, 20, 40)
        rect2.setRect(100, 100, 20, 40)
        #rect.moveBy(10, 50)
        rect2.setTransformOriginPoint(100, 100)
        rect2.setRotation(-10)
        print rect1.rect().center()
        #print rect2.transformOriginPoint().x(), rect2.transformOriginPoint().y()
        """

    @pyqtSlot(tuple, tuple)
    def drawNewWall(self, wallFront, wallRear):
        pFront = QGraphicsRectItem(wallFront[0] / CANVAS_RATIO,
                                   wallFront[1] / CANVAS_RATIO,
                                   DOT_SIZE, DOT_SIZE)
        pRear = QGraphicsRectItem(wallRear[0] / CANVAS_RATIO,
                                  wallRear[1] / CANVAS_RATIO,
                                  DOT_SIZE, DOT_SIZE)
        self.mapScene.addItem(pFront)
        self.mapScene.addItem(pRear)

    @pyqtSlot(tuple, float)
    def drawRover(self, center, orientation):
        self.roverRect.setRect((center[0] - ROVER_WIDTH / 2) / CANVAS_RATIO,
                               (center[1] - ROVER_LENGTH / 2) / CANVAS_RATIO,
                               ROVER_WIDTH / CANVAS_RATIO, ROVER_LENGTH / CANVAS_RATIO)
        self.roverRect.setTransformOriginPoint(center[0] / CANVAS_RATIO, center[1] / CANVAS_RATIO)
        self.roverRect.setRotation(math.degrees(-orientation))

    @pyqtSlot()
    def manualGearChange(self):
        gear = self.mainWidget.ui.gearSlider.value()
        self.mainWidget.ui.gearLcdNumber.display(gear)
        self.rover.roverGear = gear
        self.rover.updateMotorCommand()
        self.newCommand.emit(tuple(self.rover.commandMsg))

    @pyqtSlot()
    def manualMoveForward(self):
        self.rover.roverDirection = ROVER_DIRECTION_FORWARD
        self.rover.updateMotorCommand()
        self.newCommand.emit(tuple(self.rover.commandMsg))

    @pyqtSlot()
    def manualMoveBackward(self):
        self.rover.roverDirection = ROVER_DIRECTION_BACKWARD
        self.rover.updateMotorCommand()
        self.newCommand.emit(tuple(self.rover.commandMsg))

    @pyqtSlot()
    def manualMoveLeft(self):
        self.rover.roverDirection = ROVER_DIRECTION_LEFT
        self.rover.updateMotorCommand()
        self.newCommand.emit(tuple(self.rover.commandMsg))

    @pyqtSlot()
    def manualMoveRight(self):
        self.rover.roverDirection = ROVER_DIRECTION_RIGHT
        self.rover.updateMotorCommand()
        self.newCommand.emit(tuple(self.rover.commandMsg))

    @pyqtSlot()
    def manualStop(self):
        self.mainWidget.ui.gearSlider.setValue(0)

    @pyqtSlot()
    def simulationStart(self):
        self.simState == SIMULATION_STATE_PHASE_1
        self.simTimer.start(5000)

    @pyqtSlot()
    def simulationUpdate(self):
        if self.simState == SIMULATION_STATE_PHASE_1:
            self.simState = SIMULATION_STATE_PHASE_2
            self.rover.roverGear = 2
            self.rover.roverDirection = ROVER_DIRECTION_FORWARD
            self.rover.updateMotorCommand()
            self.newCommand.emit(tuple(self.rover.commandMsg))
            self.simTimer.start(16000)
        elif self.simState == SIMULATION_STATE_PHASE_2:
            self.simState = SIMULATION_STATE_PHASE_3
            self.rover.roverDirection = ROVER_DIRECTION_BACKWARD
            self.rover.updateMotorCommand()
            self.newCommand.emit(tuple(self.rover.commandMsg))
            self.simTimer.start(6100)
        elif self.simState == SIMULATION_STATE_PHASE_3:
            self.simState = SIMULATION_STATE_PHASE_4
            self.rover.roverDirection = ROVER_DIRECTION_LEFT
            self.rover.updateMotorCommand()
            self.newCommand.emit(tuple(self.rover.commandMsg))
            self.simTimer.start(3500)
        elif self.simState == SIMULATION_STATE_PHASE_4:
            self.simState = SIMULATION_STATE_PHASE_5
            self.rover.roverDirection = ROVER_DIRECTION_BACKWARD
            self.rover.updateMotorCommand()
            self.newCommand.emit(tuple(self.rover.commandMsg))
            self.simTimer.start(8300)
        elif self.simState == SIMULATION_STATE_PHASE_5:
            self.rover.roverGear = 0
            self.rover.roverDirection = ROVER_DIRECTION_STOP
            self.rover.updateMotorCommand()
            self.newCommand.emit(tuple(self.rover.commandMsg))

    def start(self):
        self.mainWidget.show()
        self.wiflyReceiverThread.start()
        self.wiflySenderThread.start()
        self.appStart.emit()
class MainController(QObject):

    appstart = pyqtSignal()

    def __init__(self):
        super(MainController, self).__init__()
        self.mainWidget = MainWidget()
        self.ControllerCommunication = ControllerCommunication()
        self.ControllerCommunication._consumingThread_()

        self.ControllerCommunication.Room1_led.connect(self.change_led_text1)
        self.ControllerCommunication.Room2_led.connect(self.change_led_text2)

        self.ControllerCommunication.Room1_music_track_name.connect(self.update_track_name1)
        self.ControllerCommunication.Room2_music_track_name.connect(self.update_track_name2)

        self.ControllerCommunication.Room1_music_status.connect(self.update_track_status1)
        self.ControllerCommunication.Room2_music_status.connect(self.update_track_status2)

        # self.ControllerCommunication.Room1_led_On_Msg.connect(self.update_text_led_on)
        # self.ControllerCommunication.Room2_led_On_Msg.connect(self.update_text_led2_on)
        #
        # self.ControllerCommunication.Room1_led_Off_Msg.connect(self.update_text_led_off)
        # self.ControllerCommunication.Room2_led_Off_Msg.connect(self.update_text_led2_off)

        self.ControllerCommunication.Room1_music_volume.connect(self.update_room1_volume)
        self.ControllerCommunication.Room2_music_volume.connect(self.update_room2_volume)


        self.mainWidget.ui.pushButton_5.clicked.connect(self.clear_text)

    """
        Updating GUI elements for room 1

                                        _
                                      /' )
                                     (_, |
                                       | |
                                       | |
                                       (_)
    """
    @pyqtSlot(str)
    def change_led_text1(self, led1_status):
        """
        change led text for room 1
        :param led1_status:
        :return:
        """
        self.mainWidget.ui.pushButton.setText(led1_status)
        self.mainWidget.ui.textEdit.insertPlainText("Turned room 1 light " + led1_status + " !")
        self.mainWidget.ui.textEdit.moveCursor(QTextCursor.Start)


    @pyqtSlot(str)
    def update_track_name1(self, track1_name):
        """
        change music track name for room 1
        :param track1_name:
        :return:
        """
        self.mainWidget.ui.music_track_name_label.setText(track1_name)
        self.mainWidget.ui.textEdit.insertPlainText("Room 1 playing " + track1_name + "...")
        self.mainWidget.ui.textEdit.moveCursor(QTextCursor.Start)

    @pyqtSlot(str)
    def update_track_status1(self, track1_status):
        """
        change music status name for room 2
        :param track1_status:
        :return:
        """
        self.mainWidget.ui.music_label_status.setText(track1_status)

    """
        Updating GUI elements for room 2

                                       __
                                     /'__`\
                                    (_)  ) )
                                       /' /
                                     /' /( )
                                    (_____/'
    """

    @pyqtSlot(str)
    def change_led_text2(self, led2_status):
        """
        change led text for room 2
        :param led2_status:
        :return:
        """
        self.mainWidget.ui.pushButton_2.setText(led2_status)
        self.mainWidget.ui.textEdit.insertPlainText("Turned room 2 light " + led2_status + " !")
        self.mainWidget.ui.textEdit.moveCursor(QTextCursor.Start)

    @pyqtSlot(str)
    def update_track_name2(self, track2_name):
        """
        change music status name for room 2
        :param track2_name:
        :return:
        """
        self.mainWidget.ui.music_track_name_label_2.setText(track2_name)
        self.mainWidget.ui.textEdit.insertPlainText("Room 2 playing " + track2_name + "...")
        self.mainWidget.ui.textEdit.moveCursor(QTextCursor.Start)


    @pyqtSlot(str)
    def update_track_status2(self, track2_status):
        """
        change music status name for room 2
        :param track2_status:
        :return:
        """
        self.mainWidget.ui.music_label_status_2.setText(track2_status)

    @pyqtSlot(int)
    def update_room1_volume(self, num):
        self.mainWidget.ui.lcdNumber.display(num)

    @pyqtSlot(int)
    def update_room2_volume(self, num2):
        self.mainWidget.ui.lcdNumber_2.display(num2)

    # @pyqtSlot()
    # def update_text_led_on(self):
    #     self.mainWidget.ui.textBrowser.append("Turned room 1 light on !")
    #
    # @pyqtSlot()
    # def update_text_led2_on(self):
    #     self.mainWidget.ui.textBrowser.append("Turned room 2 light on !")
    #
    # @pyqtSlot()
    # def update_text_led_off(self):
    #     self.mainWidget.ui.textBrowser.append("Turned room 1 light off !")
    #
    # @pyqtSlot()
    # def update_text_led2_off(self):
    #     self.mainWidget.ui.textBrowser.append("Turned room 2 light off !")
    @pyqtSlot()
    def clear_text(self):
        self.mainWidget.ui.textEdit.clear()

    def start(self):
        self.mainWidget.show()
        self.mainWidget.showFullScreen()
        self.appstart.emit()