Skip to content

Commit

Permalink
python 내에 state machine 로직 구현
Browse files Browse the repository at this point in the history
  • Loading branch information
coreanq committed Aug 26, 2016
1 parent 253e614 commit 12ca751
Show file tree
Hide file tree
Showing 5 changed files with 26,892 additions and 652 deletions.
84 changes: 78 additions & 6 deletions main.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,88 @@
import sys
from PyQt5.QtQml import QQmlApplicationEngine
from PyQt5.QtWidgets import QApplication
from PyQt5 import QtCore
from PyQt5.QtCore import QObject, pyqtSlot, pyqtSignal, QUrl
from PyQt5.QtCore import QStateMachine, QState, QTimer, QFinalState, QMetaObject
import qml_rc

class UiState(QObject):
sigComPortOpened = pyqtSignal()
sigComPortClosed = pyqtSignal()
sigPowerOn = pyqtSignal()
sigPowerOff = pyqtSignal()

def __init__(self):
super().__init__()
self.fsm = QStateMachine()
self.qmlEngine = QQmlApplicationEngine()
self.qmlEngine.addImportPath("qml")
self.qmlEngine.addImportPath("lib")
self.qmlEngine.load(QUrl('qrc:/qml/main.qml'))
self.rootObject = self.qmlEngine.rootObjects()[0]

self.rootObject.comPortOpened.connect(self.sigComPortOpened)
self.rootObject.comPortClosed.connect(self.sigComPortClosed)
self.rootObject.powerOn.connect(self.sigPowerOn)
self.rootObject.powerOff.connect(self.sigPowerOff)
self.createState()
pass

def createState(self):
# state defintion
mainState = QState(self.fsm)
finalState = QFinalState(self.fsm)
self.fsm.setInitialState(mainState)

initState = QState(mainState)
openState = QState(mainState)
mainState.setInitialState(initState)

standbyState = QState(openState)
processingState = QState(openState)
openState.setInitialState(standbyState)

# transition defition
initState.addTransition(self.sigComPortOpened, openState)
openState.addTransition(self.sigComPortClosed, initState)

standbyState.addTransition(self.sigPowerOn, processingState)
processingState.addTransition(self.sigPowerOff, standbyState)

initState.entered.connect(self.initStateEntered)
openState.entered.connect(self.openStateEntered)
standbyState.entered.connect(self.standbyStateEntered)
processingState.entered.connect(self.processingStateEntered)

# fsm start
self.fsm.start()
pass

@pyqtSlot()
def initStateEntered(self):
print("init")
QMetaObject.invokeMethod(self.rootObject, "setPathViewIndex", QtCore.Q_ARG("QVariant", 0))
pass

@pyqtSlot()
def openStateEntered(self):
print("open")
pass

@pyqtSlot()
def standbyStateEntered(self):
print("standby")
QMetaObject.invokeMethod(self.rootObject, "setPathViewIndex", QtCore.Q_ARG("QVariant", 1))
pass

@pyqtSlot()
def processingStateEntered(self):
print("processing")
QMetaObject.invokeMethod(self.rootObject, "setPathViewIndex",QtCore.Q_ARG("QVariant", 2))
pass

if __name__ == "__main__":
myApp = QApplication(sys.argv)
qmlEngine = QQmlApplicationEngine()
qmlEngine.addImportPath("qml")
qmlEngine.addImportPath("lib")
rootContext = qmlEngine.rootContext()
qmlEngine.load(QUrl('qrc:/qml/main.qml'))
uiState = UiState()
sys.exit(myApp.exec())
# rootContext.setContextProperty("")

4 changes: 4 additions & 0 deletions qml.qrc
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,9 @@
<file>image/click.png</file>
<file>image/computer.png</file>
<file>image/wired.png</file>
<file>image/background.jpg</file>
<file>image/card_back.jpg</file>
<file>image/card_back.png</file>
<file>image/switch.png</file>
</qresource>
</RCC>
3 changes: 0 additions & 3 deletions qml/ProcessingWnd.qml
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,6 @@ Rectangle{
// console.log("%")
dataUpdate(rawValue)
}
onComSwitchChanged:{
console.log(state)
}
}

QChartGallery{
Expand Down
32 changes: 13 additions & 19 deletions qml/main.qml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,15 @@ ApplicationWindow{
height: 640
title: "도어열림감지센서"

function setPathViewIndex(index){
while(1){
if( pathView.currentIndex == index){
break;
}
pathView.incrementCurrentIndex()
}
}

PortSelector {
id: port
anchors.top: parent.top
Expand All @@ -41,7 +50,6 @@ ApplicationWindow{
else if( state == 0)
powerOff()
}

}
VisualItemModel{
id: itemModel
Expand Down Expand Up @@ -205,12 +213,7 @@ ApplicationWindow{
signal: comPortOpened
}
onEntered: {
while(1){
if( pathView.currentIndex == 0){
break;
}
pathView.incrementCurrentIndex()
}
// setPathViewIndex(0)
}
}
DSM.State {
Expand All @@ -228,12 +231,7 @@ ApplicationWindow{
signal: powerOn
}
onEntered: {
while(1){
if( pathView.currentIndex == 1){
break;
}
pathView.incrementCurrentIndex()
}
// setPathViewIndex(1)
}
}
DSM.State{
Expand All @@ -243,12 +241,8 @@ ApplicationWindow{
signal: powerOff
}
onEntered: {
while(1){
if( pathView.currentIndex == 2){
break;
}
pathView.incrementCurrentIndex()
}
// setPathViewIndex(2)

}
}
}
Expand Down
Loading

0 comments on commit 12ca751

Please sign in to comment.