Example #1
0
void txThread::sendNext()
{
    QString tmp;
    if(lineCounter <= lineMax)
    {
        tmp = textfile.section("\n",lineCounter,lineCounter);
//        qDebug() << tmp;
        if(tmp.contains("M01"))
        {
            if(ignoreFirstM01)
            {
                ignoreFirstM01 = false;
            }
            else
            {
                emit layerTransmitted();
            }
        }
        tmp.append("\n");
        serialConn->send(tmp);
        double progress= (double) lineCounter/(double)lineMax;
        emit progressChanged(progress*100);
        lineCounter++;
    }
    else
    {
        emit fileTransmitted();
        resetState();
        return;
    }
    if(tmp.contains("G4"))
    {
        msleep(300);
    }
}
void spherebot::processAnswer(QString answer)
{
//    int linenumber = getOption(line,"N");
    //qDebug()<< "process answer: " << answer << endl;
    //qDebug()<< "buffer: " << toSendBuffer << endl;
    if(answer.contains("rs"))
    {
        resendLine();
    }
    else if (answer.contains("ok"))
    {
        //qDebug()<< "answer.contains(ok): "<< endl;
        lastLineTransmitted = true;
        timeout_timer->stop();

        if(toSendBuffer.size() > 0)
        {
            trySendBufferLine();
        }
        else if(sendingFile)
        {
            if( toSendBuffer.size() == 0 && lineCounter > lineMax)
            {
               emit fileTransmitted();
            }
            sendNext();
        }
        else
        {
            //everything sent
            retry_timer->stop();
        }
        emit dataSent(lastLine);
    }
    else if(answer.contains("Spherebot"))
    {
        //qDebug() << "received version" << endl;
        bot_connected = true;
    }
    else
    {
        //qDebug() << "answer did not contain rs or ok" << endl;
    }
}
void MainWindow::initSateMachine()
{
    connected = new QState();
    disconnected = new QState();

    transmitting = new QState(connected);   //transmitting means sending or stopped
    sending =      new QState(transmitting);
    stopped =      new QState(transmitting);

    idle =             new QState(connected);
    abort =            new QState(connected);
    ask_for_restart =  new QState(connected);
    restart =          new QState(connected);
    ask_for_next_layer=new QState(connected);
    load_file_dialog = new QState(connected);
    start_sending =    new QState(connected);

    /////////////////////////////////////////////////// Transitions

    connected->addTransition(ui->connectButton,     SIGNAL(clicked()),disconnected);
    disconnected->addTransition(ui->connectButton,  SIGNAL(clicked()),connected);

    connected->setInitialState(idle);

    idle->addTransition(ui->sendFileButton,SIGNAL(clicked()),start_sending);
    idle->addTransition(ui->loadFileButton,SIGNAL(clicked()),load_file_dialog);

    transmitting->addTransition(ui->loadFileButton, SIGNAL(clicked()),abort);
    transmitting->addTransition(ui->restartButton,  SIGNAL(clicked()),restart);
    //
    sending->addTransition(ui->sendFileButton,SIGNAL(clicked()),stopped);
    sending->addTransition(this->bot,         SIGNAL(fileTransmitted()),ask_for_restart);
    sending->addTransition(this->bot,         SIGNAL(layerTransmitted()),ask_for_next_layer);

    stopped->addTransition(ui->sendFileButton,SIGNAL(clicked()),start_sending);
    //
    start_sending->addTransition(start_sending,SIGNAL(entered()),sending);

    ask_for_restart->addTransition(restartLayerMsgBox,SIGNAL(accepted()),restart);
    ask_for_restart->addTransition(restartLayerMsgBox,SIGNAL(rejected()),idle);
    connect(ask_for_restart,SIGNAL(exited()),this,SLOT(hey()));

    ask_for_next_layer->addTransition(nextLayerMsgBox,SIGNAL(accepted()),sending);

    load_file_dialog->addTransition(load_file_dialog,SIGNAL(entered()),idle);

    abort->addTransition(abort,SIGNAL(entered()),idle);

    restart->addTransition(restart,SIGNAL(entered()),start_sending);

    /////////////////////////////////////////////////// Functions executed in states

    connect(connected,      SIGNAL(entered()),this,SLOT(entry_connected()));
    connect(disconnected,   SIGNAL(entered()),this,SLOT(entry_disconnected()));
    connect(transmitting,   SIGNAL(entered()),this,SLOT(entry_transmitting()));
    connect(idle,           SIGNAL(entered()),this,SLOT(entry_idle()));
    connect(sending,        SIGNAL(entered()),this,SLOT(entry_sending()));
    connect(start_sending,  SIGNAL(entered()),this,SLOT(entry_start_sending()));
    connect(stopped,        SIGNAL(entered()),this,SLOT(entry_stopped()));
    connect(restart,        SIGNAL(entered()),this,SLOT(entry_restart()));
    connect(ask_for_restart,SIGNAL(entered()),this,SLOT(entry_ask_for_restart()));
    connect(ask_for_next_layer,SIGNAL(entered()),this,SLOT(entry_ask_for_next_layer()));
    connect(load_file_dialog,SIGNAL(entered()),this,SLOT(entry_load_file_dialog()));

    connect(sending,        SIGNAL(exited()), this,SLOT(leave_sending()));

    ///////////////////////////////////////////////////

    machine.addState(connected);
    machine.addState(disconnected);

    machine.setInitialState(disconnected);
    machine.start();
}