summaryrefslogtreecommitdiffhomepage
path: root/tracker-hatire/thread.hpp
blob: f36d114e8134c5a5ca205eaf5be9abdfda194d7f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#pragma once

#include <QSerialPort>
#include <QByteArray>
#include <QThread>
#include <QMutex>

#include <QFile>
#include <QCoreApplication>

enum results
{
    result_ok,
    result_open_error,
    result_error,
};

//#define HATIRE_DEBUG_LOGFILE "d:/putty-hatire.log"

#ifdef HATIRE_DEBUG_LOGFILE
#   include <QFile>
#   include <QTimer>
#endif

#ifdef __GNUC__
#   define unused(t, i) t __attribute__((unused)) i
#else
#   define unused(t, i) t i
#endif

struct thread_settings
{
    QByteArray  sCmdStart;
    QByteArray  sCmdStop;
    QByteArray  sCmdInit;
    QByteArray  sCmdReset;
    QByteArray  sCmdCenter;
    QByteArray  sCmdZero;

    QString sSerialPortName;
    QSerialPort::BaudRate iBaudRate;
    QSerialPort::DataBits iDataBits;
    QSerialPort::Parity iParity;
    QSerialPort::StopBits iStopBits;
    QSerialPort::FlowControl iFlowControl;

    int iDelayInit;
    int iDelayStart;
    int iDelaySeq;
    bool bBigEndian;
    volatile bool bEnableLogging;

    thread_settings() :
        iBaudRate(QSerialPort::UnknownBaud),
        iDataBits(QSerialPort::UnknownDataBits),
        iParity(QSerialPort::UnknownParity),
        iStopBits(QSerialPort::UnknownStopBits),
        iFlowControl(QSerialPort::UnknownFlowControl),
        iDelayInit(0),
        iDelayStart(0),
        iDelaySeq(0),
        bBigEndian(false),
        bEnableLogging(false)
    {
    }
};

#include <QMetaType>

Q_DECLARE_METATYPE(thread_settings)

struct serial_result
{
    serial_result() : code(result_ok) {}
    serial_result(results code, const QString& error) : error(error), code(code) {}

    QString error;
    results code;
};

struct Diag : public QFile
{
    Diag()
    {
        setFileName(QCoreApplication::applicationDirPath() + "/HATDiagnostics.txt");
    }
};

class hatire_thread : public QThread
{
    Q_OBJECT

#ifdef HATIRE_DEBUG_LOGFILE
    using serial_t = QFile;
    QTimer read_timer;
#else
    using serial_t = QSerialPort;
#endif

    QByteArray data_read;
    serial_t com_port;
    thread_settings s;

    void run() override;

private slots:
    void on_serial_read();
    void teardown_serial();

    void sendcmd_impl(unused(const QByteArray, &cmd));
    void serial_info_impl();
    serial_result init_serial_port_impl();
    void update_serial_settings_impl(const thread_settings& s);
    thread_settings serial_settings_impl();

signals:
    void serial_debug_info(const QByteArray &MsgInfo);

    void sendcmd(const QByteArray& cmd);
    void serial_info();
    serial_result init_serial_port();
    void update_serial_settings(const thread_settings& s);
    thread_settings serial_settings();

public:
    void start(const thread_settings &s_);
    ~hatire_thread() override;
    hatire_thread();

    QByteArray& send_data_read_nolock(bool& ret);

    void Log(const QString& message);

    QMutex data_mtx;
};