Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

States Machine



  • I am developing an embedded application where I am using state machine in qml, with image.bmp amnipulation, in this application at the push of a button a serial command is sent to an external hardware and depending on the response of that hardware it is necessary to change the state of the machine through a sign coming from c ++!

    Does anyone have a problem to do to change the state of the machine through c ++ ??

    abs !!


  • Qt Champions 2017

    Avoid changing the state from C++. When the information comes from c++, emit signal c++. Handle the signal & make appropriate state change at qml only. Also simple sample code will help us understand how u r using the state.



  • import QtQuick 2.0

    import QlChannelSerial 1.0

    Rectangle{

    property int whiteId: 3
    property int gain: 0
    property int zomm: 0
    property bool rec: false
    property int laparo_: 1
    property bool pisca: true
    property bool show: false
    property var date: 0
    property var date_: 0
    
    property variant ikeEnv: [20]
    property int  size: 0
    property int  bcs: 0
    property int ok: 0
    
    //Define dos comandos da camera
    property int  zoom: 0
     property int whiteBalance: 1
    
    
    id:telaCamera
    Image {
        id: camera
        source: "telas/telaPrincipalCamera.bmp"
    
        // desenha p botão menu no canto superior da tela
        Image {
            id: botaMenu1
            x:719
            y:0
            source: "image/BotaoMenu.png"
    
            MouseArea{
                anchors.fill: parent
    
                onClicked: {
                    //anchors.fill
                    mainRect.state = "Menu"
                    console.log("go menu")
                }
            }
        }
        // tempo de 800 molissegundos para piscar o Icone do white balance
        Timer{
            id:timer1
            interval: 800
            repeat: true
            running: true
    
            onTriggered: {
    
                if(pisca == false ) {
                    pisca = true;
                    white.state ="Piscpreto"
    
    
                }
    
                else {
                     pisca = false;
                    white.state = "PiscVerde"
                }
    
             }
        }
    
        Timer
        {
            id:timerEnhancement
            interval: 2000
            repeat: false
            running: false
    
            onTriggered:
            {
    
                //APAGA O QUE ESTIVER ESCRITO NO ENHANCEMENT
                serial.open("ttymxc0");
                serial.writeBytes([0x7A,0x13,0x00,0x1A,0x30,0x10,0x2A,0x3E,0x11,0x27,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,
                                   0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x00,0x0B],0);
            }
        }
    

    //Icone para ir a tela da fonte de led
    Image {
    id: fonteLed
    x:558
    y:9
    source: "Icones/Lamp.png"

            MouseArea{
                anchors.fill: parent
               onClicked: {
                     mainRect.state = "Principal10"
               }
            }
        }
    

    //Area do Icone qhite balance
    Rectangle{
    id:white
    Image {
    id: whiteBalence
    x:228
    y:92
    source: "Icones/white1.png"

            MouseArea{
    
                anchors.fill:parent
                onClicked: {
                     serial.open("ttymxc0");
                   ok = serial.writeBytes([0x7A,0x13,0x00,0x04,0x19,0x09,0x00,0x07],1);  // envia comando de white balance
                }
            }
    
            states: [
                State {
                    name: "PiscVerde"
               },
                State {
                    name: "Piscpreto"
               }
    
            ]
    
           Image {
                id:pscverde
                source: "Icones/white1.png"
                visible:white.state == "PiscVerde"
            }
            Image {
                id:pscpreto
                source: "Icones/white2.png"
                visible:white.state == "Piscpreto"
            }
        }
    
    
        }
    

    //Icone dos enhance
    Rectangle{
    id:enhance
    Image {
    id: high
    x:50
    y:104
    source: "Icones/Enhancehigh.png"
    MouseArea{
    anchors.fill: parent
    onClicked: {
    whiteId --;
    if(whiteId <0) whiteId = 3;
    switch(whiteId)
    {
    case 0:
    serial.open("ttymxc0");
    serial.writeBytes([0x7A,0x13,0x00,0x1A,0x30,0x10,0x2A,0x3E,0x11,0x27,0x45,0x4E,0x48,0x41,0x4E,0x43,0x45,0x4D,0x45,
    0x4E,0x54,0x20,0x4F,0x46,0x46,0x20,0x20,0x20,0x00,0x1C],0);

                            enhance.state = "Off";
                            timerEnhancement.start();
                        break
                        case 1:
                           serial.open("ttymxc0");
                            serial.writeBytes([0x7A,0x13,0x00,0x06,0x1D,0x7D,0x04,0xB0,0x00,0xC1],0);
    
                            //ESCREVE O ENHANCEMENT LOW NO MONITOR
                            serial.writeBytes([0x7A,0x13,0x00,0x1A,0x30,0x10,0x2A,0x3E,0x11,0x27,0x45,0x4E,0x48,0x41,0x4E,0x43,0x45,0x4D,0x45,
                                               0x4E,0x54,0x20,0x4C,0x4F,0x57,0x20,0x20,0x20,0x00,0x07],0);
                            enhance.state = "Low";
                             timerEnhancement.start();  break
    
    
                        case 2:
                           serial.open("ttymxc0");
                           serial.writeBytes([0x7A,0x13,0x00,0x06,0x1D,0x7D,0x06,0x40,0x00,0x33],70);
    
                            //ESCREVE O ENHANCEMENT MEDIUM NO MONITOR
                            serial.writeBytes([0x7A,0x13,0x00,0x1A,0x30,0x10,0x2A,0x3E,0x11,0x27,0x45,0x4E,0x48,0x41,
                                               0x4E,0x43,0x45,0x4D,0x45,0x4E,0x54,0x20,0x4D,0x45,0x44,0x49,0x55,0x4D,0x00,0x6E],0);
                            enhance.state = "Med";
    
                            timerEnhancement.start();  break
                        case 3:
                            serial.open("ttymxc0");
                           serial.writeBytes([0x7A,0x13,0x00,0x06,0x1D,0x7D,0x07,0xF0,0x00,0x82],0);
    
                            //ESCREVE O ENHANCEMENT HIGH NO MONITOR
                             serial.writeBytes([0x7A,0x13,0x00,0x1A,0x30,0x10,0x2A,0x3E,0x11,0x27,0x45,0x4E,0x48,0x41,0x4E,0x43,0x45,0x4D,0x45,
                                                0x4E,0x54,0x20,0x48,0x49,0x47,0x48,0x20,0x20,0x00,0x7D],0);
                            enhance.state = "High";
    
                            timerEnhancement.start(); break
    
                        }
                    }
                 }
    
                 states: [
                     State {
                         name: "High"
                      },
    
                     State {
                         name: "Med"
                     },
                     State {
                         name: "Low"
                     },
                     State {
                         name: "Off"
                     }
                 ]
    
                 Image {
                     id:alto
                     source: "Icones/Enhancehigh.png"
                      visible:enhance.state == "High"
                 }
    
                 Image {
                     id:medio
                     source: "Icones/Enhancemed.png"
                      visible:enhance.state == "Med"
                 }
    
                 Image {
                     id:baixo
                     source: "Icones/EnhanceLow.png"
                      visible:enhance.state == "Low"
                 }
    
                 Image {
                     id:desligado
                     source: "Icones/EnhanceOff.png"
                      visible:enhance.state == "Off"
                 }
               }
            }
        //Ganhos
        Rectangle{
            id:ganho
    
            Image {
                id: zero
                x:510
                y:96
                //source: "Icones/Ganho0.png"
                source: "Icones/Ganho0.png"
                MouseArea{
                    anchors.fill: parent
                    onClicked: {
                        gain ++;
                        if(gain > 2) gain = 0;
                        switch(gain)
                        {
                            case 0: ganho.state = "Zero"; break
                            case 1: ganho.state = "One"; break
                            case 2: ganho.state = "Two"; break
                        }
                     }
                  }
    
                   states: [
                   State {
                        name: "Zero"
                   },
                   State {
                         name: "One"
                   },
                   State {
                       name: "Two"
                  }
              ]
    
                   Image {
                       id: zerado
                       source: "Icones/Ganho0.png"
                        visible:ganho.state == "Zero"
                   }
                   Image {
                       id: hum
                       source: "Icones/Ganho1.png"
                        visible:ganho.state == "One"
                   }
                   Image {
                       id: dois
                       source: "Icones/Ganho2.png"
                       visible:ganho.state == "Two"
                   }
          }
        }
    

    //zoom
    Rectangle{
    id:zoom
    Image {
    id: zoon
    x:510
    y:254
    source: "Icones/Zoom0.png"
    MouseArea{
    anchors.fill: parent
    onClicked: {
    zomm++;
    if(zomm > 3)zomm = 0;
    serial.open("ttymxc0"); // abre a serial para mandar para a ikegami
    switch(zomm) // envia zoom para a camera
    {
    case 0:
    serial.writeBytes([0x7A,0x13,0x00,0x07,0x14,0x8F,0x6F,0x10,0x80,0x00,0x70],1);

                            // Escreve na tela nada porque nao tem zoom
                            serial.open("ttymxc0");  // abre a serial para mandar para a ikegami
                            serial.writeBytes([0x7A,0x13,0x00,0x0C,0x30,0x10,0x20,0x3E,0x11,0x27,0x20,0x20,
                            0x20,0x20,0x00,0x17],1);
                            zoom.state = "Zoom0"; break;
                        case 1:
                           serial.writeBytes([0x7A,0x13,0x00,0x07,0x14,0x8F,0x6F,0x15,0x80,0x00,0x75],1);
    
                               //escreve 1.0 na tela
                            serial.open("ttymxc0");  // abre a serial para mandar para a ikegami
                            serial.writeBytes([0x7A,0x13,0x00,0x0C,0x30,0x10,0x20,0x3E,0x11,0x27,0x78,0x31,
                            0x2E,0x35,0x00,0x45],1);
                            zoom.state = "Zoom1"; break;
                        case 2:
                            serial.writeBytes([0x7A,0x13,0x00,0x07,0x14,0x8F,0x6F,0x1A,0x80,0x00,0x7A],1);
    
                           // escreve 2.0 na tela
                            serial.open("ttymxc0");  // abre a serial para mandar para a ikegami
                          serial.writeBytes([0x7A,0x13,0x00,0x0C,0x30,0x10,0x20,0x3E,0x11,0x27,0x78,0x32,
                          0x2E,0x30,0x00,0x43],1);
                            zoom.state = "Zoom2"; break;
                        case 3:
                           serial.writeBytes([0x7A,0x13,0x00,0x07,0x14,0x8F,0x6F,0x1F,0x80,0x00,0x7F],0);
    
                           // escreve 2.5 na tela
                            serial.open("ttymxc0");  // abre a serial para mandar para a ikegami
                          serial.writeBytes([0x7A,0x13,0x00,0x0C,0x30,0x10,0x20,0x3E,0x11,0x27,0x78,0x32,
                         0x2E,0x35,0x00,0x46],0);
                            zoom.state = "Zoom3";break;
                        }
                    }
                }
    
                states: [
                    State {
                        name: "Zoom0"
                    },
                    State {
                        name: "Zoom1"
                    },
                    State {
                        name: "Zoom2"
                    },
    
                    State {
                        name: "Zoom3"
                    }
    
                  ]
                Image {
                    id:zeroo
                    source: "Icones/Zoom0.png"
                     visible: zoom.state == "Zoom0"
                }
                Image {
                    id:one
                    source: "Icones/Zoom1.png"
                     visible: zoom.state == "Zoom1"
                }
                Image {
                    id:two
                    source: "Icones/Zoom2.png"
                     visible: zoom.state == "Zoom2"
                }
                Image {
                    id:tree
                    source: "Icones/Zoom3.png"
                     visible: zoom.state == "Zoom3"
                }
            }
        }
    
        //Gravaçao
        Rectangle{
            id: grava
            Image {
                id: recc
                x:50
                y:254
    
                source: "Icones/RecStop.png"
                MouseArea{
                    anchors.fill: parent
                    onClicked: {
                        if(rec == true) rec = false;
                        else rec = true;
                        switch(rec)
                        {
                        case true: grava.state = "Gravando";break;
                        case false: grava.state = "Parado"; break;
                        }
                    }
                }
    
                states: [
                    State {
                        name: "Gravando"
                      },
                    State {
                        name: "Parado"
                      }
                ]
    
                Image {
                   id: grav
                    source: "Icones/RecRun.png"
                      visible: grava.state == "Gravando"
                 }
    
                Image {
                   id: stpograv
                    source: "Icones/RecStop.png"
                      visible: grava.state == "Parado"
                 }
            }
    
        }
        Rectangle{
            id:user
    
            Image {
                id: laparo
                x:750
                y:112
                source: "Icones/Laparo01.png"
    
                MouseArea{
                    anchors.fill: parent
                    onClicked: {
                        laparo_ ++;
                        if(laparo_ > 4) laparo_ = 1;
                        switch(laparo_)
                        {
                        case 1: user.state = "Laparo01";break;
                        case 2: user.state = "Laparo02";break;
                        case 3: user.state = "Endoflexivel";break;
                        case 4: user.state = "Histero/Uro";break;
                        }
    
                        console.log("Go users")
    
                    }
                }
    
               states: [
                    State {
                        name: "Laparo01"
                     },
                    State {
                        name: "Laparo02"
                     },
                    State {
                        name: "Endoflexivel"
                     },
                    State {
                        name: "Histero/Uro"
                     }
                 ]
    
                Image {
                    id:laparo1
                    source: "Icones/Laparo01.png"
                    visible: user.state == "Laparo01"
                }
    
                Image {
                    id:laparo2
                    source: "Icones/Laparo.png"
                    visible: user.state == "Laparo02"
                }
    
                Image {
                    id:laparo3
                    source: "Icones/Endoflexivel.png"
                    visible: user.state == "Endoflexivel"
                }
    
                Image {
                    id:laparo4
                    source: "Icones/Histero.png"
                    visible: user.state == "Histero/Uro"
                }
            }
    
            // desenha p botão menu no canto superior da tela
            Image {
                id:connect
                x:10
                y:420
                source: "Icones/conectOn.png"
             }
        }
    }
    

    }


  • Qt Champions 2017

    ok. What is the issue now ? Are you not getting the signal from c++ side ? Is it working ?



  • Actually this is not a problem, because I'm still researching how to change the state of the machine when of c ++ signal arrives !!

    If anyone has a very basic example of how to do this operation I will be very grateful !!


  • Qt Champions 2017

    OK. You can look at the examples my GIT repository. You can look at 'QMLStateChangeFromCPP' under QML directory.



  • Thanks for your help!!
    Surely my doubt will be solved, a big hug !!
    Greetings from Brazil!!


  • Qt Champions 2017

    Cool. Enjoy QML coding. You can put the case in to SOLVED state. If you like the answer you can upvote as well :)


Log in to reply