/*
 * scbus utils to switch card settings.
 *
 * Copyright (C) 2011 Otvos Attila oattila@onebithq.com
 *
 * This 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, or
 * (at your option) any later version.
 *
 * This 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 MPlayer; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

#ifndef __RS485CLASS_H__
#define __RS485CLASS_H__

#include <QObject>
#include <QThread>
#include <QMap>
#include <QStringList>

#include "config.h"

typedef struct RS485Config {
    bool act;
    unsigned char device;
    unsigned char input;
    unsigned char type;
    unsigned char par;
} RS485Config;

#define cmpconfig(a,b) (a.act==b.act && a.device==b.device && a.input==b.input && a.type==b.type && a.par==b.par)

typedef struct RS485Status {
    unsigned char input;
    unsigned char output;
    RS485Config config[6];
} RS485Status;

typedef struct RS485configQueue {
    unsigned char device;
    unsigned char output;
    RS485Config config;
} RS485configQueue;

class RS485Thread : public QThread {
    Q_OBJECT

    public:

        RS485Thread();

    public:
        RS485Thread(int baud, QString device);
        ~RS485Thread();
        virtual void run();
        void stop();

        QStringList getData(void);
        RS485Status getStatus(int deviceid);
        void enableRaw(bool e);

    public slots:
        void setDeviceID(int olddeviceid, int newdeviceid);
        void setConfig(int deviceid, int output, RS485Config config);
        const QString getError() {return lastError;}
        void getDeviceID(int deviceid);

    signals:
        void isData(void);
        void changeStatus(int);
        void changeDeviceID(int);
        void changeConfig(int,int);

    private:
        void* rs485;
        QString lastError;
        int runing;
        int raw;
        int olddeviceid;
        int newdeviceid;
        int trycnt;
        int qtrycnt;
        int dtrycnt;
        QStringList readedData;
        QMap<int, RS485Status> status;
        QList<RS485configQueue> configQueue;
        QList<int> getDeviceQueue;
};

#endif
