-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdriver.h
154 lines (119 loc) · 4.12 KB
/
driver.h
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
#ifndef DRIVER_H
#define DRIVER_H
#include <QObject>
#include <qqml.h>
#include <QSerialPortInfo>
#include <QSettings>
#include <QLoggingCategory>
class TwinleafCSB;
class CsbChannel : public QObject
{
Q_OBJECT
Q_PROPERTY(double offset READ getOffset WRITE setOffset NOTIFY offsetChanged)
Q_PROPERTY(double amplitude READ getAmplitude WRITE setAmplitude NOTIFY amplitudeChanged)
Q_PROPERTY(double frequency READ getFrequency WRITE setFrequency NOTIFY frequencyChanged)
Q_ENUMS(Channel)
Q_ENUMS(Parameter)
public:
enum class Channel {
None,
X,
Y,
Z,
};
enum class Parameter {
None,
Offset,
ModAmplitude,
ModFrequency,
};
explicit CsbChannel(TwinleafCSB *const parent, Channel channel);
~CsbChannel();
void setOffset(double value);
double getOffset() const { return m_offset; }
void setFrequency(double value);
double getFrequency() const { return m_frequency; }
void setAmplitude(double value);
double getAmplitude() const { return m_amplitude; }
void loadSettings(QSettings &settings);
void saveSettings(QSettings &settings) const;
signals:
void offsetChanged();
void amplitudeChanged();
void frequencyChanged();
private:
QString getChannelPrefix() const;
private:
double m_offset;
double m_amplitude;
double m_frequency;
const QPointer<TwinleafCSB> m_twinleafCsb;
const Channel m_channel;
};
class TwinleafCSB : public QObject
{
Q_OBJECT
Q_PROPERTY(QString lastResponse MEMBER m_lastResponse NOTIFY responseReceived)
Q_PROPERTY(QString lastCommand MEMBER m_lastCommand NOTIFY commandSent)
Q_PROPERTY(bool isConnected READ isConnected NOTIFY connectionStatusChanged)
Q_PROPERTY(QString currentDevice WRITE connectDevice READ getCurrentDevice NOTIFY connectionStatusChanged)
Q_PROPERTY(CsbChannel *xChannel READ getXChannel)
Q_PROPERTY(CsbChannel *yChannel READ getYChannel)
Q_PROPERTY(CsbChannel *zChannel READ getZChannel)
Q_ENUMS(SentStatus)
Q_ENUMS(ConnectStatus)
QML_ELEMENT
public:
enum class SentStatus {
SentAndConfirmed = 0,
AlreadySet = 1,
Error = 2,
SentButWrongResponse,
};
enum class ConnectStatus {
Connected = 0,
DeviceNotFound = 1,
};
explicit TwinleafCSB(QObject *parent = nullptr);
~TwinleafCSB();
Q_INVOKABLE QStringList getDeviceList() const;
Q_INVOKABLE CsbChannel *getChannel(const QString &channelName) const;
bool isConnected() const;
Q_INVOKABLE void loadSettings(bool loadDevice = true);
void saveSettings() const;
public slots:
void disconnectDevice();
ConnectStatus connectDevice(const QString &deviceString);
SentStatus sendCommand(const QString &command, const QVariant &value);
SentStatus sendCommand(const QString &command);
signals:
void deviceListChanged();
void responseReceived();
void commandSent();
void connectionStatusChanged();
void xChannelChanged();
private:
void logResponse(const QString &message);
TwinleafCSB::SentStatus writeCommand(const QByteArray &command, const QString &expectedResponse = "");
QString generateDeviceString(const QSerialPortInfo &portInfo) const;
QString generateDeviceStringFromPortInfo(const QSerialPortInfo &portInfo) const;
CsbChannel *getXChannel() const { return m_xChannel.data(); }
CsbChannel *getYChannel() const { return m_yChannel.data(); }
CsbChannel *getZChannel() const { return m_zChannel.data(); }
QString getCurrentDevice() const { return m_currentDevice; }
private:
QScopedPointer<QSerialPort> m_serialPort;
QString m_currentDevice;
QString m_lastResponse;
QString m_lastCommand;
const QStringList m_channels;
const QPointer<CsbChannel> m_xChannel;
const QPointer<CsbChannel> m_yChannel;
const QPointer<CsbChannel> m_zChannel;
QMap<QString, QVariant> m_previousCommands;
static constexpr int MAX_WRITE_ITERATIONS = 10;
static constexpr int WRITE_TIMEOUT_MS = 20;
static constexpr int WRITE_SLEEP_MS = 10;
};
Q_DECLARE_LOGGING_CATEGORY(twinleafCsbLog)
#endif // DRIVER_H