Subversion Repositories Projects

Rev

Rev 170 | Blame | Compare with Previous | Last modification | View Log | RSS feed

/***************************************************************************
 *   Copyright (C) 2008 by Manuel Schrape                                  *
 *   manuel.schrape@gmx.de                                                 *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License.        *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/


#include <QtGui>

#include <QLineEdit>
#include <QString>
#include <QTimer>
#include <QIcon>
#include <QToolButton>
#include <QSpinBox>

#include "mktool.h"
#include "dlg_Config.h"
#include "dlg_Motortest.h"
#include "dlg_Preferences.h"
#include "../global.h"
#include "../ToolBox.h"

#include <stdlib.h>

MKTool::MKTool()
{
    setupUi(this);

    Settings = new cSettings;

    init_Arrays();
    init_GUI();

    init_Objects();
    init_Connections();

    init_Plot();
}

void MKTool::init_GUI()
{
    setWindowTitle(QA_NAME + " v" + QA_VERSION);

    // Tab mit Debug-Elementen verbergen
    tab_Main->removeTab(5);

    // Settings-Tab hinzufügen.
    f_Settings = new wdg_Settings( this );
    tab_Main->insertTab ( 2, f_Settings, "FC-Settings");
    tab_Main->widget(2)->setObjectName("Tab_2");

    // Develop - Nicht gebrauchte sachen abschalten.
    ac_StartServer->setVisible(false);
    box_Flugdaten->hide();
    box_System->hide();
    pb_SettingsReset->hide();
    pb_Flash->hide();
    rb_NC->hide();

    // Zusätzliche Widgets in die Toolbar.
    toolBar->addWidget(lb_Port);
    toolBar->addWidget(le_Port);
    toolBar->addSeparator();
//    toolBar->addWidget(cb_Hardware);

    lb_Status->setText("Hallo bei QMK-Groundstation...!!!");  

#ifdef _EEEPC_
    toolBar->hide();
    lb_Status->hide();
#endif

    resize(Settings->GUI.Size);
    move(Settings->GUI.Point);

    if (Settings->GUI.isMax)
    {
        showMaximized();
    }

    // Analoglabels anzeigen
    for (int a = 0; a < MaxAnalog; a++)
    {
        lb_Analog[a]->setText(Settings->Analog1.Label[a]);
    }

    // Kopie der Tabs anlegen
    for (int b = 0; b < 6; b++)
    {
        TabWidgets[b] = tab_Main->widget(b);
    }

    // Ausgeblendete Tabs ausblenden
    for (int c = 0; c < 6; c++)
    {
        if (Settings->GUI.TabViews[c] == false)
        {
            QString TabName = QString("Tab_%1").arg(c);

            for (int d = 0; d < tab_Main->count(); d++)
            {
                if (tab_Main->widget(d)->objectName() == TabName)
                {
                    tab_Main->removeTab(d);
                }
            }
        }
    }

    ac_View0->setChecked(Settings->GUI.TabViews[0]);
    ac_View1->setChecked(Settings->GUI.TabViews[1]);
    ac_View2->setChecked(Settings->GUI.TabViews[2]);
    ac_View3->setChecked(Settings->GUI.TabViews[3]);
    ac_View4->setChecked(Settings->GUI.TabViews[4]);
    ac_View5->setChecked(Settings->GUI.TabViews[5]);

    le_Port->setText(Settings->TTY.Port);

    cb_ShowMSG->setChecked(Settings->GUI.Term_Info);
    cb_ShowData->setChecked(Settings->GUI.Term_Data);
    cb_ShowAlways->setChecked(Settings->GUI.Term_Always);
}

void MKTool::init_Objects()
{
    // QTimer-Instanzen
    Ticker = new QTimer(this);

    // Seriell-Port
    serialPort = new ManageSerialPort;

    // QFile-Instanz (Log-Datei)
    CSVFile = new QFile("");

    // Senden erlauben (Warum auch immer)
    AllowSend = true;
}

void MKTool::init_Connections()
{
    // Seriel-Port Empfang
    connect(serialPort, SIGNAL(newDataReceived(const QByteArray &)), this, SLOT(slot_newDataReceived(const QByteArray &)));

    // Serielle Verbundung öffnen / schließen
    connect(ac_ConnectTTY, SIGNAL(triggered()), this, SLOT(slot_OpenPort()));

    // Buttons Settings lesen / schreiben
    connect(f_Settings->pb_Read,   SIGNAL(clicked()), this, SLOT(slot_GetFCSettings()));
    connect(f_Settings->pb_Write,  SIGNAL(clicked()), this, SLOT(slot_SetFCSettings()));

    // LCD auf / ab
    connect(pb_LCDup,   SIGNAL(clicked()), this, SLOT(slot_LCD_UP()));
    connect(pb_LCDdown, SIGNAL(clicked()), this, SLOT(slot_LCD_DOWN()));

    // Actions
    connect(ac_Config,       SIGNAL(triggered()), this, SLOT(slot_ac_Config()));
    connect(ac_Preferences,  SIGNAL(triggered()), this, SLOT(slot_ac_Preferences()));
    connect(ac_Motortest,    SIGNAL(triggered()), this, SLOT(slot_ac_Motortest()));
    connect(ac_MehrDaten,    SIGNAL(triggered()), this, SLOT(slot_ac_MehrDaten()));
    connect(ac_KeineDaten,   SIGNAL(triggered()), this, SLOT(slot_ac_KeineDaten()));
    connect(ac_GetLabels,    SIGNAL(triggered()), this, SLOT(slot_ac_GetLabels()));

    // Plotter starten / scrollen
    connect(scroll_plot,     SIGNAL(valueChanged(int)), this, SLOT(slot_ScrollPlot(int)));
    connect(ac_StartPlotter, SIGNAL(triggered()), this, SLOT(slot_ac_StartPlotter()));

    // Tabs ein & ausblenden
    connect(ac_View0,        SIGNAL(triggered()), this, SLOT(slot_ac_View()));
    connect(ac_View1,        SIGNAL(triggered()), this, SLOT(slot_ac_View()));
    connect(ac_View2,        SIGNAL(triggered()), this, SLOT(slot_ac_View()));
    connect(ac_View3,        SIGNAL(triggered()), this, SLOT(slot_ac_View()));
    connect(ac_View4,        SIGNAL(triggered()), this, SLOT(slot_ac_View()));
    connect(ac_View5,        SIGNAL(triggered()), this, SLOT(slot_ac_View()));

    // firmeware Updateen / flashen
    connect(pb_Update,   SIGNAL(clicked()), this, SLOT(slot_pb_Update()));
    connect(pb_HexFile,  SIGNAL(clicked()), this, SLOT(slot_pb_HexFile()));

    // CVS-Record starten / stoppen
    connect(ac_RecordCSV,  SIGNAL(triggered()), this, SLOT(slot_RecordCSV()));

    // Timer-Events
    connect(Ticker,   SIGNAL(timeout()),       SLOT(slot_Ticker()));

    // Seitenwechsel
    connect(tab_Main,             SIGNAL(currentChanged(int)), this, SLOT(slot_TabChanged(int)));
    connect(f_Settings->tab_Par,  SIGNAL(currentChanged(int)), this, SLOT(slot_TabChanged(int)));

    // About QMK & About-QT Dialog einfügen
    connect(ac_About, SIGNAL(triggered()), this, SLOT(slot_ac_About()));
    menu_Help->addAction(trUtf8("Über &Qt"), qApp, SLOT(aboutQt()));
}

void MKTool::init_Arrays()
{
    lb_Analog[0]  = lb_A_0;
    lb_Analog[1]  = lb_A_1;
    lb_Analog[2]  = lb_A_2;
    lb_Analog[3]  = lb_A_3;
    lb_Analog[4]  = lb_A_4;
    lb_Analog[5]  = lb_A_5;
    lb_Analog[6]  = lb_A_6;
    lb_Analog[7]  = lb_A_7;
    lb_Analog[8]  = lb_A_8;
    lb_Analog[9]  = lb_A_9;
    lb_Analog[10] = lb_A_10;
    lb_Analog[11] = lb_A_11;
    lb_Analog[12] = lb_A_12;
    lb_Analog[13] = lb_A_13;
    lb_Analog[14] = lb_A_14;
    lb_Analog[15] = lb_A_15;
    lb_Analog[16] = lb_A_16;
    lb_Analog[17] = lb_A_17;
    lb_Analog[18] = lb_A_18;
    lb_Analog[19] = lb_A_19;
    lb_Analog[20] = lb_A_20;
    lb_Analog[21] = lb_A_21;
    lb_Analog[22] = lb_A_22;
    lb_Analog[23] = lb_A_23;
    lb_Analog[24] = lb_A_24;
    lb_Analog[25] = lb_A_25;
    lb_Analog[26] = lb_A_26;
    lb_Analog[27] = lb_A_27;
    lb_Analog[28] = lb_A_28;
    lb_Analog[29] = lb_A_29;
    lb_Analog[30] = lb_A_30;
    lb_Analog[31] = lb_A_31;
}

void MKTool::init_Plot()
{
    NextPlot = 0;

    qwtPlot->setCanvasBackground(QColor(QRgb(0x00000000)));

    qwtPlot->insertLegend(new QwtLegend(), QwtPlot::RightLegend);

    QwtPlotGrid *Grid = new QwtPlotGrid();
    Grid->setMajPen(QPen(Qt::gray, 0, Qt::DotLine));

    Grid->attach(qwtPlot);

    qwtPlot->setAxisScale(QwtPlot::xBottom,0,Settings->Data.Plotter_Count,0);

    for (int a = 0; a < MaxAnalog; a++)
    {
        Plot[a] = new QwtPlotCurve(Settings->Analog1.Label[a]);
        Plot[a]->setPen(QPen(QColor(Def_Colors[a])));
        Plot[a]->setRenderHint(QwtPlotItem::RenderAntialiased);

        if (Settings->Analog1.PlotView[a])
            Plot[a]->attach(qwtPlot);
    }
    qwtPlot->replot();
}


// Ticker-Event
///////////////
void MKTool::slot_Ticker()
{
    if (TickerDiv)
        TickerDiv = false;
    else
        TickerDiv = true;

    for (int a = 0; a < MaxTickerEvents; a++)
    {
        if (TickerEvent[a] == true)
        {
            switch(a)
            {
                case 0 :
                    if (TickerDiv)
                    {
                        QByteArray Temp(LastSend.toUtf8());
                        serialPort->sendData(Temp);
                    }
                break;
                case 1 :
                    TX_Data[0] = 0;
                    send_Data('p', ADDRESS_FC, TX_Data, 0, false);
                break;
                case 2 :
                    if (cb_LCD->isChecked())
                    {
                        TX_Data[0] = LCD_Page;
                        TX_Data[1] = 0;
                        send_Data('l', ADDRESS_ALL, TX_Data, 1, true);
                    }
                break;
                case 3 :
                    if (ac_MehrDaten->isChecked())
                    {
                        TX_Data[0] = Settings->Data.Debug_Fast / 10;
                        send_Data('d', ADDRESS_ALL, TX_Data, 1, false);
                    }
                    else
                    {
                        TX_Data[0] = Settings->Data.Debug_Slow / 10;
                        send_Data('d', ADDRESS_ALL, TX_Data, 1, false);
                    }
                break;
            }
        }
    }
}


// Slots der Actions (Menüpunkte, Buttons)
//////////////////////////////////////////
void MKTool::slot_ac_Motortest()
{
    dlg_Motortest *f_Motortest = new dlg_Motortest(this);

    connect(f_Motortest, SIGNAL(updateMotor(int, int, int, int)), this, SLOT(slot_Motortest(int, int, int, int)));

    if (f_Motortest->exec()==QDialog::Accepted)
    {
    }

    disconnect(f_Motortest, 0,0,0);
    slot_Motortest(0,0,0,0);
}

void MKTool::slot_Motortest(int Motor1, int Motor2, int Motor3, int Motor4)
{
    TX_Data[0] = Motor1;
    TX_Data[1] = Motor2;
    TX_Data[2] = Motor3;
    TX_Data[3] = Motor4;
    send_Data('t', ADDRESS_FC, TX_Data, 4, false);
}

void MKTool::slot_ac_Config()
{
    set_Analog Old_Analog1;

    Old_Analog1 = Settings->Analog1;

    dlg_Config *f_Config = new dlg_Config(this);
    f_Config->set_Settings(Settings, Mode.ID);

    if (f_Config->exec()==QDialog::Accepted)
    {
        Settings = f_Config->get_Settings();
        Settings->write_Settings_Analog(Mode.ID);

        // Plotter neu einrichten
        if (Old_Analog1.PlotView != Settings->Analog1.PlotView)
        {
            config_Plot();
        }

        // CVS-Datei neu anlegen.
        if ((CSVFile->isOpen()) && (Old_Analog1.LogView != Settings->Analog1.LogView))
        {
            slot_RecordCSV();
            slot_RecordCSV();
        }

    }
}

void MKTool::slot_ac_Preferences()
{
    dlg_Preferences *f_Preferences = new dlg_Preferences(this);

    Settings->TTY.Port = le_Port->text();
    f_Preferences->set_Settings(Settings);

    if (f_Preferences->exec()==QDialog::Accepted)
    {
        Settings = f_Preferences->get_Settings();
        Settings->write_Settings();
        le_Port->setText(Settings->TTY.Port);
        config_Plot();
    }
}

void MKTool::slot_ac_StartPlotter()
{
    if (ac_StartPlotter->isChecked())
    {
        ac_StartPlotter->setText("Stop Plotter");
        pb_StartPlotter->setText("Stop Plotter");
    }
    else
    {
        ac_StartPlotter->setText("Start Plotter");
        pb_StartPlotter->setText("Start Plotter");
    }
}

void MKTool::slot_ac_View()
{
    int Aktive;

    QAction *Action = (QAction*)sender();

    if (Action->objectName() == QString("ac_View0"))
        Aktive = 0;
    if (Action->objectName() == QString("ac_View1"))
        Aktive = 1;
    if (Action->objectName() == QString("ac_View2"))
        Aktive = 2;
    if (Action->objectName() == QString("ac_View3"))
        Aktive = 3;
    if (Action->objectName() == QString("ac_View4"))
        Aktive = 4;
    if (Action->objectName() == QString("ac_View5"))
        Aktive = 5;

    QString TabName = QString("Tab_%1").arg(Aktive);

    if (!Action->isChecked())
    {
        for (int a = 0; a < tab_Main->count(); a++)
        {
            if (tab_Main->widget(a)->objectName() == TabName)
            {
                tab_Main->removeTab(a);
            }
        }

//        Action->setChecked(false);
    }
    else
    {
//        Action->setChecked(true);
        tab_Main->insertTab(Aktive, TabWidgets[Aktive], Action->text());
    }
}

void MKTool::slot_ac_MehrDaten() // DONE 0.71g
{
    if (!ac_KeineDaten->isChecked())
    {
        if (ac_MehrDaten->isChecked())
        {
            TX_Data[0] = Settings->Data.Debug_Fast / 10;
            send_Data('d', ADDRESS_ALL, TX_Data, 1, false);
        }
        else
        {
            TX_Data[0] = Settings->Data.Debug_Slow / 10;
            send_Data('d', ADDRESS_ALL, TX_Data, 1, false);
        }
    }
}

void MKTool::slot_ac_KeineDaten() // DONE 0.71g
{
    if (ac_KeineDaten->isChecked())
    {
        TickerEvent[3] = false;
        TX_Data[0] = 0;
        send_Data('d', ADDRESS_ALL, TX_Data, 1, false);
    }
    else
    {
        // Wenn MK3MAG dann andauernd Daten neu anfragen.
        if (Mode.ID == ADDRESS_MK3MAG)
            TickerEvent[3] = true;

        if (ac_MehrDaten->isChecked())
        {
            TX_Data[0] = Settings->Data.Debug_Fast / 10;
            send_Data('d', ADDRESS_ALL, TX_Data, 1, false);
        }
        else
        {
            TX_Data[0] = Settings->Data.Debug_Slow / 10;
            send_Data('d', ADDRESS_ALL, TX_Data, 1, false);
        }
    }
}

void MKTool::slot_ac_About()
{
    QMessageBox::about(this, trUtf8(("Über ")) + QA_NAME, QA_ABOUT);
}

void MKTool::slot_ac_GetLabels() // DONE 0.71g
{
    TX_Data[0] = 0;
    send_Data('a', ADDRESS_ALL, TX_Data, 1, true);
}


//  Daten-Plotter
/////////////////
void MKTool::update_Plot()
{
    for (int a = 0; a < MaxAnalog; a++)
    {
        Plot[a]->setData(aID,aData[a],NextPlot - 1);
    }

    if ((NextPlot > Settings->Data.Plotter_Count))
    {
        scroll_plot->setMaximum(NextPlot - Settings->Data.Plotter_Count);
    }

    if ((scroll_plot->value() == NextPlot - (Settings->Data.Plotter_Count + 1)))
    {
        qwtPlot->setAxisScale(QwtPlot::xBottom,NextPlot - Settings->Data.Plotter_Count,NextPlot,0);
        scroll_plot->setValue(NextPlot - Settings->Data.Plotter_Count);
    }

    qwtPlot->replot();
}

void MKTool::config_Plot()
{
//    qDebug("Plotter rekonfiguriert..!!");
    qwtPlot->setAxisScale(QwtPlot::xBottom,0,Settings->Data.Plotter_Count,0);

    for (int a = 0; a < MaxAnalog; a++)
    {
        Plot[a]->detach();
        Plot[a]->setPen(QPen(QColor(Def_Colors[a])));

        if (Settings->Analog1.PlotView[a])
        {
            Plot[a]->setTitle(Settings->Analog1.Label[a]);
            Plot[a]->attach(qwtPlot);
        }
    }
    qwtPlot->replot();
}

void MKTool::slot_ScrollPlot(int Pos)
{
    qwtPlot->setAxisScale(QwtPlot::xBottom,Pos,Pos + Settings->Data.Plotter_Count,0);
    qwtPlot->replot();
}


// Firmeware-Update
///////////////////
void MKTool::slot_pb_Update()
{
    QString Device;
    QString Hardware;

    if (rb_FC->isChecked())
    {
        Device   = "m644";
        Hardware = "FlightCtrl";
    }
    else if (rb_MK3MAG->isChecked())
    {
        Device   = "m168";
        Hardware = "MK3MAG";
    }
    else if (rb_BL->isChecked())
    {
        Device   = "m8";
        Hardware = "BL-Ctrl";
    }

    QString Message = "Firmeware-Datei \n\n";
    Message = Message + le_HexFile->text() + "\n\n";
    Message = Message + "an " + Hardware + trUtf8(" mit AVRDUDE - Seriel & Bootloader über ") + le_Port->text() + trUtf8(" übertragen?\n");

    if (le_HexFile->text() == "")
    {
        QMessageBox::warning(this, QA_NAME, trUtf8("Bitte Firmeware-Datei wählen."), QMessageBox::Ok);
    }
    else if (QMessageBox::warning(this, QA_NAME, Message, QMessageBox::Yes | QMessageBox::No) == QMessageBox::Yes)
    {
        QString Programm = "avrdude" ;

        QStringList Argumente;

        Update = new QProcess();

        if (serialPort->isOpen())
        {
            slot_OpenPort();
        }

        Argumente << "-P";
        Argumente << le_Port->text();
        Argumente << "-p";
        Argumente << Device;
        Argumente << "-c";
        Argumente << "butterfly";
        Argumente << "-b";
        Argumente << "57600";
        Argumente << "-U";
        Argumente << "flash:w:" + le_HexFile->text();

//    QString Programm = "/home/Manuel/bin/avrdude -p m644 -P /dev/ttyS0 -c butterfly -b 57600 -U flash:w:/home/Manuel/Documents/Mikrokopter/Firmeware/FlightCtrl/Flight-Ctrl_MEGA644_V0_71h.hex";

        te_Shell->setText(""); // Ausgabefenster säubern

        connect(Update, SIGNAL(readyReadStandardOutput()), this, SLOT(slot_UpdateShell()) );
        connect(Update, SIGNAL(readyReadStandardError()), this, SLOT(slot_UpdateShell()) );

        Update->start(Programm, Argumente); // Programmaufruf
    }
}

void MKTool::slot_UpdateShell()
{
    QByteArray Output;

    Output = Update->readAllStandardError(); // Shellausgabe an Variable
    te_Shell->moveCursor(QTextCursor::End,  QTextCursor::MoveAnchor);
    te_Shell->insertPlainText(QString::fromUtf8(Output));

    Output = Update->readAll();
    te_Shell->moveCursor(QTextCursor::End,  QTextCursor::MoveAnchor);
    te_Shell->insertPlainText(QString::fromUtf8(Output));
}

void MKTool::slot_pb_HexFile()
{
    QString FileName = QFileDialog::getOpenFileName(this,trUtf8(("Firmeware-Datei wählen")),"",
                                tr("Intel Hex(*.hex);;Alle Dateien (*)"));
    if (!FileName.isEmpty())
    {
        le_HexFile->setText(FileName);
    }
}


// Wechsel der Tabs erkennen
void MKTool::slot_TabChanged(int Tab) // DONE 0.71g
{
    Tab = Tab;
    if (tab_Main->count() != 0)
    {
        if ((tab_Main->currentWidget()->objectName() == QString("Tab_2")) && (f_Settings->tab_Par->currentIndex() == 1))
        {
            TX_Data[0] = 0;
            send_Data('p', ADDRESS_FC, TX_Data, 0, true);

            Ticker->setInterval(500);
            TickerEvent[1] = true;
        }
        else
        {
            Ticker->setInterval(2000);
            TickerEvent[1] = false;
        }

        if ((tab_Main->currentWidget()->objectName() == QString("Tab_4")))
        {
            TX_Data[0] = 0;
            TX_Data[1] = 0;
            send_Data('l', ADDRESS_ALL, TX_Data, 1, true);

            Ticker->setInterval(500);
            TickerEvent[2] = true;
        }
        else
        {
            Ticker->setInterval(2000);
            TickerEvent[2] = false;
        }
    }
}

// LCD-Seiten weiterschalten
void MKTool::slot_LCD_UP() // DONE 0.71g
{
    if (LCD_Page == LCD_MAX_Page)
        TX_Data[0] = 0;
    else
        TX_Data[0] = LCD_Page + 1;

    TX_Data[1] = 0;
    send_Data('l', ADDRESS_ALL, TX_Data, 1, true);
}

void MKTool::slot_LCD_DOWN() // DONE 0.71g
{
    if (LCD_Page == 0)
        TX_Data[0] = LCD_MAX_Page;
    else
        TX_Data[0] = LCD_Page - 1;

    TX_Data[1] = 0;
    send_Data('l', ADDRESS_ALL, TX_Data, 1, true);
}

// Settings aus MK lesen / in MK schreiben
void MKTool::slot_GetFCSettings() // DONE 0.71g
{
    TX_Data[0] = f_Settings->sb_Set->value();
    TX_Data[1] = 0;
    send_Data('q', ADDRESS_FC, TX_Data, 1);
}

void MKTool::slot_SetFCSettings() // DONE 0.71g
{
    char *TX_Data2 = f_Settings->GetFCSettings();

    send_Data('s', ADDRESS_FC, TX_Data2, MaxParameter + 2, false);
}


// Save GUI-Preferences
///////////////////////
void MKTool::set_Preferences()
{
    Settings->GUI.TabViews.setBit(0, ac_View0->isChecked());
    Settings->GUI.TabViews.setBit(1, ac_View1->isChecked());
    Settings->GUI.TabViews.setBit(2, ac_View2->isChecked());
    Settings->GUI.TabViews.setBit(3, ac_View3->isChecked());
    Settings->GUI.TabViews.setBit(4, ac_View4->isChecked());
    Settings->GUI.TabViews.setBit(5, ac_View5->isChecked());

    Settings->GUI.Term_Info   = cb_ShowMSG->isChecked();
    Settings->GUI.Term_Data   = cb_ShowData->isChecked();
    Settings->GUI.Term_Always = cb_ShowAlways->isChecked();
    Settings->GUI.Term_Send   = cb_ShowSend->isChecked();
    Settings->GUI.isMax       = isMaximized();
    Settings->GUI.Size        = size();
    Settings->GUI.Point       = pos();

    Settings->TTY.Port = le_Port->text();
}


// Debug-Daten anzeigen und Aufzeichnen
///////////////////////////////////////
void MKTool::write_CSV()
{
    QTextStream Out(CSVFile);
    for (int a=0; a<MaxAnalog; a++)
    {
        if (Settings->Analog1.LogView[a])
        {
            Out << AnalogData[a];
            if (a < MaxAnalog - 1)
                Out << ';';
        }
    }
    Out << "\n";
}

void MKTool::slot_RecordCSV() // DONE 0.71g
{
    if (!CSVFile->isOpen())
    {
        QString Filename = Settings->DIR.Logging + Mode.Hardware + " - " + QDate::currentDate().toString(("yyyy-MM-dd")) + " -- " + QTime::currentTime().toString("hh-mm") + ".csv";

        CSVFile = new QFile(Filename);
        if (!CSVFile->exists())
        {
            CSVFile->open(QIODevice::Append | QIODevice::Text);

            QTextStream Out(CSVFile);

            for (int a = 0; a < MaxAnalog; a++)
            {
                if (Settings->Analog1.LogView[a])
                {
                    Out << Settings->Analog1.Label[a];

                    if (a < MaxAnalog - 1)
                        Out << ';';
                }
            }
            Out << "\n";
        }
        else
        {
            CSVFile->open(QIODevice::Append | QIODevice::Text);
        }

        pb_Record->setIcon(ToolBox::Icon(6));
        pb_Record->setText("CSV Stop");
        ac_RecordCSV->setIcon(ToolBox::Icon(6));
        ac_RecordCSV->setText("CSV Stop");
    }
    else
    {
        CSVFile->close();
        pb_Record->setIcon(ToolBox::Icon(7));
        pb_Record->setText("CSV Aufzeichnen");
        ac_RecordCSV->setIcon(ToolBox::Icon(7));
        ac_RecordCSV->setText("CSV  Aufzeichnen");
    }
}

void MKTool::show_DebugData()
{
    if (CSVFile->isOpen())
    {
        write_CSV();
    }

    if (ac_StartPlotter->isChecked())
    {
        aID[NextPlot] = NextPlot;

        for (int a = 0; a < MaxAnalog; a++)
        {
            aData[a][NextPlot] = AnalogData[a];
        }
        NextPlot++;

        if ((tab_Main->currentWidget()->objectName() == QString("Tab_1")))
            update_Plot();
    }

    le_A_0->setText(QString("%1").arg(AnalogData[0]));
    le_A_1->setText(QString("%1").arg(AnalogData[1]));
    le_A_2->setText(QString("%1").arg(AnalogData[2]));
    le_A_3->setText(QString("%1").arg(AnalogData[3]));
    le_A_4->setText(QString("%1").arg(AnalogData[4]));
    le_A_5->setText(QString("%1").arg(AnalogData[5]));
    le_A_6->setText(QString("%1").arg(AnalogData[6]));
    le_A_7->setText(QString("%1").arg(AnalogData[7]));
    le_A_8->setText(QString("%1").arg(AnalogData[8]));
    le_A_9->setText(QString("%1").arg(AnalogData[9]));
    le_A_10->setText(QString("%1").arg(AnalogData[10]));
    le_A_11->setText(QString("%1").arg(AnalogData[11]));
    le_A_12->setText(QString("%1").arg(AnalogData[12]));
    le_A_13->setText(QString("%1").arg(AnalogData[13]));
    le_A_14->setText(QString("%1").arg(AnalogData[14]));
    le_A_15->setText(QString("%1").arg(AnalogData[15]));
    le_A_16->setText(QString("%1").arg(AnalogData[16]));
    le_A_17->setText(QString("%1").arg(AnalogData[17]));
    le_A_18->setText(QString("%1").arg(AnalogData[18]));
    le_A_19->setText(QString("%1").arg(AnalogData[19]));
    le_A_20->setText(QString("%1").arg(AnalogData[20]));
    le_A_21->setText(QString("%1").arg(AnalogData[21]));
    le_A_22->setText(QString("%1").arg(AnalogData[22]));
    le_A_23->setText(QString("%1").arg(AnalogData[23]));
    le_A_24->setText(QString("%1").arg(AnalogData[24]));
    le_A_25->setText(QString("%1").arg(AnalogData[25]));
    le_A_26->setText(QString("%1").arg(AnalogData[26]));
    le_A_27->setText(QString("%1").arg(AnalogData[27]));
    le_A_28->setText(QString("%1").arg(AnalogData[28]));
    le_A_29->setText(QString("%1").arg(AnalogData[29]));
    le_A_30->setText(QString("%1").arg(AnalogData[30]));
    le_A_31->setText(QString("%1").arg(AnalogData[31]));
}


// Seriel-Port Bereich, Befehle senden und Daten empfangen
//////////////////////////////////////////////////////////

// Neues Datenpacket empfangen -> Verarbeiten
void MKTool::new_RXData(sRxData RX) // DONE 0.71g
{

    if (LastSend.length() > 2)
    {
    }

    int HardwareID = RX.Input[1] - 'a';

    switch(HardwareID)
    {
        case ADDRESS_FC :
            switch(RX.Input[2])
            {
                // Stick-Belegung der Fernsteuerung
                case 'P' : // DONE 0.71g
                    if (ToolBox::Decode64(RX))
                    {
                        f_Settings->pb_K1->setValue(ToolBox::Data2Int(RX.Decode,  2,true));
                        f_Settings->pb_K2->setValue(ToolBox::Data2Int(RX.Decode,  4,true));
                        f_Settings->pb_K3->setValue(ToolBox::Data2Int(RX.Decode,  6,true));
                        f_Settings->pb_K4->setValue(ToolBox::Data2Int(RX.Decode,  8,true));
                        f_Settings->pb_K5->setValue(ToolBox::Data2Int(RX.Decode, 10 ,true));
                        f_Settings->pb_K6->setValue(ToolBox::Data2Int(RX.Decode, 12,true));
                        f_Settings->pb_K7->setValue(ToolBox::Data2Int(RX.Decode, 14,true));
                        f_Settings->pb_K8->setValue(ToolBox::Data2Int(RX.Decode, 16,true));
                    }
                break;
                // Settings lesen
                case 'Q' : // DONE 0.71g
                    if (ToolBox::Decode64(RX))
                    {
                        TickerEvent[0] = false;

                        if (RX.Decode[1] == VERSION_SETTINGS)
                        {
                            int Settings_ID = RX.Decode[0];
                            for (int a = 0; a < MaxParameter; a++)
                            {
                                FCSettings[a] = RX.Decode[a + 2];
                            }
                            //show_ParameterSet(Settings_ID);
                            f_Settings->show_FCSettings(Settings_ID, FCSettings);
                        }
                        else
                        {
                            f_Settings->pb_Read->setDisabled(true);
                            f_Settings->pb_Write->setDisabled(true);

                            QMessageBox::warning(this, QA_NAME,
                                   "Versionen inkompatibel. \nParameterbearbeitung nicht moeglich.", QMessageBox::Ok);
                        }
                    }
                break;
                // Settings geschrieben
                case 'S' : // DONE 0.71g
                    TickerEvent[0] = false;
                break;
            }

        case ADDRESS_NC :
            switch(RX.Input[2])
            {
                // Navigationsdaten
                case 'O' : // NOT DONE 0.12h
                    if (ToolBox::Decode64(RX))
                    {
                    }
                break;
            }
//        case ADDRESS_MK3MAG :

        default :
            switch(RX.Input[2])
            {
                // LCD-Anzeige
                case 'L' : // DONE 0.71g
                    if (ToolBox::Decode64(RX))
                    {
                        int LCD[150];
                        memcpy(LCD,RX.Decode, sizeof(RX.Decode));

                        LCD_Page     = RX.Decode[0];
                        LCD_MAX_Page = RX.Decode[1];

                        le_LCD0->setText(ToolBox::Data2QString(LCD,2,22));
                        le_LCD1->setText(ToolBox::Data2QString(LCD,22,42));
                        le_LCD2->setText(ToolBox::Data2QString(LCD,42,62));
                        le_LCD3->setText(ToolBox::Data2QString(LCD,62,82));

                        TickerEvent[0] = false;
                    }
                break;
                // Analoglabels
                case 'A' : // DONE 0.71g
                    if (ToolBox::Decode64(RX))
                    {
                        int Position = RX.Decode[0];
                        if (Position != 31)
                        {
                            Settings->Analog1.Label[Position] = ToolBox::Data2QString(RX.Decode,1,17).trimmed();
                            if (Settings->Analog1.Label[Position] == "")
                            {
                                Settings->Analog1.Label[Position] = "A-" + QString("%1").arg(Position);
                            }
                            Position ++;
                            TX_Data[0] = Position;
                            send_Data('a', ADDRESS_ALL, TX_Data, 1, true);
                        }
                        if (Position == 31)
                        {
                            for (int a = 0; a < MaxAnalog; a++)
                            {
                                lb_Analog[a]->setText(Settings->Analog1.Label[a]);
                            }
                            Settings->Analog1.Version = Mode.Version;
                            Settings->write_Settings_AnalogLabels(HardwareID);
                            config_Plot();
                        }
                    }
                break;
                // Debug-Daten
                case 'D' : // DONE 0.71g
                    if (ToolBox::Decode64(RX))
                    {
                        for (int i = 0; i < MaxAnalog; i++)
                        {
                            AnalogData[i] = ToolBox::Data2Int(RX.Decode, (i * 2) + 2);
                        }
                        show_DebugData();
                    }
                break;
                // Version
                case 'V' : // DONE 0.71h
                    if (ToolBox::Decode64(RX))
                    {
                        TickerEvent[0] = false;

                        Mode.ID            = HardwareID;
                        Mode.VERSION_MAJOR = RX.Decode[0];
                        Mode.VERSION_MINOR = RX.Decode[1];
                        Mode.VERSION_PATCH = RX.Decode[4];
                        Mode.VERSION_SERIAL_MAJOR = RX.Decode[2];
                        Mode.VERSION_SERIAL_MINOR = RX.Decode[3];

                        Mode.Hardware   = HardwareType[Mode.ID];
                        Mode.Version    = QString("%1").arg(RX.Decode[0]) + "." + QString("%1").arg(RX.Decode[1]) + QString(RX.Decode[4] + 'a');

                        setWindowTitle(QA_NAME + " v" + QA_VERSION + " - " + Mode.Hardware + " " + Mode.Version);

                        if (Mode.VERSION_SERIAL_MAJOR != VERSION_SERIAL_MAJOR)
                        {
                                AllowSend = false;
                                QMessageBox::warning(this, QA_NAME,
                                   "Serielles Protokoll Inkompatibel. \nBitte neue Programmversion installieren,", QMessageBox::Ok);
                        }

                        TX_Data[0] = Settings->Data.Debug_Slow / 10;
                        send_Data('d', ADDRESS_ALL, TX_Data, 1, false);

                        // Wenn MK3MAG dann andauernd Daten neu anfragen.
                        if (Mode.ID == ADDRESS_MK3MAG)
                        {
                            TickerEvent[3] = true;
                        }

                        // Wenn FlightCtrl dann Settings abfragen.
                        if (Mode.ID == ADDRESS_FC)
                        {
                            {
                                TX_Data[0] = 0xff;
                                TX_Data[1] = 0;
                                send_Data('q', ADDRESS_FC, TX_Data, 1);
                            }
                        }
                        // Wenn nicht Lesen und Schreiben der Settings deaktivieren.
                        else
                        {
                                f_Settings->pb_Read->setDisabled(true);
                                f_Settings->pb_Write->setDisabled(true);
                        }

                        Settings->read_Settings_Analog(HardwareID);
                        Settings->read_Settings_AnalogLabels(HardwareID);

                        if (Settings->Analog1.Version != Mode.Version)
                        {
                            lb_Status->setText("Analoglabel-Version unterschiedlich. Lese Analoglabels neu aus.");
                            slot_ac_GetLabels();
                        }
                        else
                        for (int a = 0; a < MaxAnalog; a++)
                        {
                            lb_Analog[a]->setText(Settings->Analog1.Label[a]);
                        }
                        config_Plot();
                    }
                break;
            }
    }
}

// Neue Daten an der Schnittstelle
void MKTool::slot_newDataReceived(const QByteArray &dataReceived) // DONE 0.71g
{
    const char *RXt;
    RXt = dataReceived.data();
    int a = 0;

    while (RXt[a] != '\0')
    {
        if (RXt[a] == '\r')
        {
            while ((RxData.String.length() > 1) && (RxData.String.at(1) == '#'))
            {
                RxData.String.remove(0,1);
            }

            if (ToolBox::check_CRC(RxData.String))
            {
                RxData.Input = RxData.String.toLatin1().data();
                new_RXData(RxData);

                if ((cb_ShowData->isChecked()) && ((tab_Main->currentWidget()->objectName() == QString("Tab_3")) || (cb_ShowAlways->isChecked())))
                {
                    te_RX->moveCursor(QTextCursor::End,  QTextCursor::MoveAnchor);
//                    te_RX->insertPlainText(" > " + RxData.String + '\r');
                    te_RX->insertHtml("<span style=\"color:#00008b;\">" + RxData.String + "<br /></span>");
                }
            }
            else
            {
                if ((cb_ShowMSG->isChecked()) && ((tab_Main->currentWidget()->objectName() == QString("Tab_3")) || (cb_ShowAlways->isChecked())))
                {
                    te_RX->moveCursor(QTextCursor::End,  QTextCursor::MoveAnchor);
//                    te_RX->insertPlainText(" > " + RxData.String + '\r');
                    te_RX->insertHtml("<span style=\"color:#008b00;\">" + RxData.String + "<br /></span>");
                }
            }
            RxData.String = QString("");
        }
        else
        {
            {
                RxData.String = RxData.String + QString(RXt[a]);
            }
        }
        a++;
    }
}

// Seriellen Port öffnen
void MKTool::slot_OpenPort()
{
    if (serialPort->isOpen())
    {
        TX_Data[0] = Settings->Data.Debug_Off / 10;
        send_Data('d', ADDRESS_ALL, TX_Data, 1, false);
        sleep(1);
        TX_Data[0] = 0;
        TX_Data[1] = 0;
        TX_Data[2] = 0;
        TX_Data[3] = 0;
        send_Data('t', ADDRESS_FC, TX_Data, 4, false);
        serialPort->close();
        pb_Open->setText("Verbinden");
        ac_ConnectTTY->setText("Verbinden");
        pb_Open->setIcon(ToolBox::Icon(9));
        ac_ConnectTTY->setIcon(ToolBox::Icon(9));

        Ticker->stop();
    }
    else
    {
        serialPort->setPort(le_Port->text()); //Port

        serialPort->setBaudRate(BAUD57600); //BaudRate
        serialPort->setDataBits(DATA_8); //DataBits
        serialPort->setParity(PAR_NONE); //Parity
        serialPort->setStopBits(STOP_1); //StopBits
        serialPort->setFlowControl(FLOW_OFF); //FlowControl

        serialPort->setTimeout(0, 10);
        serialPort->enableSending();
        serialPort->enableReceiving();

        serialPort->open();
        if (serialPort->isOpen())
        {
            serialPort->receiveData();

            send_Data('v', ADDRESS_ALL, TX_Data, 0, true);

            pb_Open->setText("Trennen");
            ac_ConnectTTY->setText("Trennen");
            pb_Open->setIcon(ToolBox::Icon(8));
            ac_ConnectTTY->setIcon(ToolBox::Icon(8));

            Ticker->start(2000);
        }
    }
}

// Daten senden
void MKTool::send_Data(char CMD, int Address, char Data[150],unsigned int Length, bool Resend) // DONE 0.71g
{
    if (serialPort->isOpen() && AllowSend)
    {
        QString TX_Data = ToolBox::Encode64(Data, Length);

        TX_Data = QString("#") + (QString('a' + Address)) + QString(CMD) + TX_Data;

//    qDebug(TX_Data.toLatin1().data());

        TX_Data = ToolBox::add_CRC(TX_Data) + '\r';

//    qDebug(TX_Data.toLatin1().data());

        if (Resend)
        {
            LastSend = TX_Data;
            TickerEvent[0] = true;
        }

        QByteArray Temp(TX_Data.toUtf8());
        serialPort->sendData(Temp);

        if (cb_ShowSend->isChecked())
        {
            te_RX->moveCursor(QTextCursor::End,  QTextCursor::MoveAnchor);
            te_RX->insertHtml("<span style='color:#8b0000;'>" + TX_Data + "<br /></span>");
        }
    }
}


// Programm beenden
///////////////////

MKTool::~MKTool()
{
//    qDebug(" Programm Ende ..!! ");
    if (serialPort->isOpen())
    {
        serialPort->close();
    }

    set_Preferences();
    Settings->write_Settings();

    if (CSVFile->isOpen())
    {
        CSVFile->close();
    }
}