PX4FirmwareUpgradeWorker.cc 3.85 KB
Newer Older
Lorenz Meier's avatar
Lorenz Meier committed
1
//#include <QJsonDocument>
2 3
#include <QFile>

4 5 6 7
#include "PX4FirmwareUpgradeWorker.h"

#include <SerialLink.h>
#include <QGC.h>
8
#include "uploader.h"
9 10 11

#include <QDebug>

12 13 14 15 16 17
#define PROTO_GET_SYNC		0x21
#define PROTO_EOC            0x20
#define PROTO_NOP		0x00
#define PROTO_OK		0x10
#define PROTO_FAILED		0x11
#define PROTO_INSYNC		0x12
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35

PX4FirmwareUpgradeWorker::PX4FirmwareUpgradeWorker(QObject *parent) :
    QObject(parent),
    link(NULL)
{
}

PX4FirmwareUpgradeWorker* PX4FirmwareUpgradeWorker::putWorkerInThread(QObject *parent)
{
    PX4FirmwareUpgradeWorker *worker = new PX4FirmwareUpgradeWorker;
    QThread *workerThread = new QThread(parent);

    connect(workerThread, SIGNAL(started()), worker, SLOT(startContinousScan()));
    connect(workerThread, SIGNAL(finished()), worker, SLOT(deleteLater()));
    worker->moveToThread(workerThread);

    // Starts an event loop, and emits workerThread->started()
    workerThread->start();
36
    return worker;
37 38 39
}


40
void PX4FirmwareUpgradeWorker::startContinousScan()
41
{
42 43 44 45 46 47
    exitThread = false;
    while (!exitThread) {
//        if (detect()) {
//            break;
//        }
        QGC::SLEEP::msleep(20);
48 49
    }

50 51 52 53 54
    if (exitThread) {
        link->disconnect();
        delete link;
        exit(0);
    }
55 56
}

57
void PX4FirmwareUpgradeWorker::detect()
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
{
    if (!link)
    {
        link = new SerialLink("", 921600);
        connect(link, SIGNAL(bytesReceived(LinkInterface*,QByteArray)), this, SLOT(receiveBytes(LinkInterface*,QByteArray)));
    }

    // Get a list of ports
    QVector<QString>* ports = link->getCurrentPorts();

    // Scan
    for (int i = 0; i < ports->size(); i++)
    {
        // Ignore known wrong link names

        if (ports->at(i).contains("Bluetooth")) {
74
            //continue;
75 76 77 78 79
        }

        link->setPortName(ports->at(i));
        // Open port and talk to it
        link->connect();
80
        char buf[2] = { PROTO_GET_SYNC, PROTO_EOC };
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
        if (!link->isConnected()) {
            continue;
        }
        // Send sync request
        insync = false;
        link->writeBytes(buf, 2);
        // Wait for response
        QGC::SLEEP::msleep(20);

        if (insync)
            emit validPortFound(ports->at(i));
            break;
    }

    //ui.portName->setCurrentIndex(ui.baudRate->findText(QString("%1").arg(this->link->getPortName())));

    // Set port

    // Load current link config

}

void PX4FirmwareUpgradeWorker::receiveBytes(LinkInterface* link, QByteArray b)
{
    for (int position = 0; position < b.size(); position++) {
        qDebug() << "BYTES";
107
        qDebug() << (char)(b[position]);
108
        if (((const char)b[position]) == PROTO_INSYNC)
109 110 111
        {
            qDebug() << "SYNC";
            insync = true;
112 113
        }

114
        if (insync && ((const char)b[position]) == PROTO_OK)
115
        {
116 117 118 119 120 121 122
            emit detectionStatusChanged("Found PX4 board");
        }
    }

    printf("\n");
}

123 124
void PX4FirmwareUpgradeWorker::loadFirmware(const QString &filename)
{
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146
    qDebug() << __FILE__ << __LINE__ << "LOADING FW" << filename;

    PX4_Uploader uploader;
    const char* filenames[2];
    filenames[0] = filename.toStdString().c_str();
    filenames[1] = NULL;
    uploader.upload(filenames, "/dev/tty.usbmodem1");

//    QFile f(filename);
//    if (f.open(QIODevice::ReadOnly))
//    {
//        QByteArray buf = f.readAll();
//        f.close();
//        firmware = QJsonDocument::fromBinaryData(buf);
//        if (firmware.isNull()) {
//            emit upgradeStatusChanged(tr("Failed decoding file %1").arg(filename));
//        } else {
//            emit upgradeStatusChanged(tr("Ready to flash %1").arg(filename));
//        }
//    } else {
//        emit upgradeStatusChanged(tr("Failed opening file %1").arg(filename));
//    }
147 148 149
}

void PX4FirmwareUpgradeWorker::upgrade()
150
{
151 152
    emit upgradeStatusChanged(tr("Starting firmware upgrade.."));
}
153

154 155 156
void PX4FirmwareUpgradeWorker::abort()
{
    exitThread = true;
157
}