forked from analogdevicesinc/scopy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhardware_trigger.cpp
384 lines (312 loc) · 9.43 KB
/
hardware_trigger.cpp
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
#include "hardware_trigger.hpp"
#include <QPair>
#include <stdexcept>
using namespace adiscope;
QVector<QString> HardwareTrigger::lut_analog_trigg_cond = {
"edge-rising",
"edge-falling",
"level-low",
"level-high",
};
QVector<QString> HardwareTrigger::lut_digital_trigg_cond = {
"edge-rising",
"edge-falling",
"level-low",
"level-high",
"edge-any",
};
QVector<QString> HardwareTrigger::lut_trigg_mode = {
"always",
"analog",
"digital",
"digital_OR_analog",
"digital_AND_analog",
"digital_XOR_analog",
"!digital_OR_analog",
"!digital_AND_analog",
"!digital_XOR_analog",
};
// This is still not generic. It is specific to only 2 channels!
QVector<QString> HardwareTrigger::lut_trigg_source = {
"a",
"b",
"a_OR_b",
"a_AND_b",
"a_XOR_b",
};
typedef QPair<struct iio_channel *, QString> channel_pair;
HardwareTrigger::HardwareTrigger(struct iio_device *trigg_dev) :
m_trigger_device(trigg_dev)
{
if (!trigg_dev) {
throw std::invalid_argument("trigger_device=NULL");
}
// Get all channels and sort them ascending by name
QList<QPair<struct iio_channel *, QString>> channels;
for (uint i = 0; i < iio_device_get_channels_count(trigg_dev); i++) {
struct iio_channel *chn = iio_device_get_channel(trigg_dev, i);
if (iio_channel_is_output(chn)) {
continue;
}
QString name = QString(iio_channel_get_id(chn));
QPair<struct iio_channel *, QString> chn_pair(chn, name);
channels.push_back(chn_pair);
}
std::sort(channels.begin(), channels.end(),
[](channel_pair a, channel_pair b)
{ return a.second < b.second; });
// Pick the analog, digital, trigger_logic and delay channels
for (int i = 0; i < channels.size(); i++) {
struct iio_channel *chn = channels[i].first;
bool mode = iio_channel_find_attr(chn, "mode");
bool trigger = iio_channel_find_attr(chn, "trigger");
bool trigger_level = iio_channel_find_attr(chn,
"trigger_level");
bool trigger_hysteresis = iio_channel_find_attr(chn,
"trigger_hysteresis");
if (trigger) {
if (trigger_level && trigger_hysteresis) {
m_analog_channels.push_back(chn);
} else if (!trigger_level && !trigger_hysteresis) {
m_digital_channels.push_back(chn);
}
} else if (mode) {
m_logic_channels.push_back(chn);
}
}
m_delay_trigger = iio_device_find_channel(trigg_dev, "trigger",
false);
m_num_channels = m_analog_channels.size();
if (m_analog_channels.size() < 1) {
throw std::runtime_error(
"hardware trigger has no analog channels");
}
if (m_digital_channels.size() < 1) {
throw std::runtime_error(
"hardware trigger has no digital channels");
}
if (m_logic_channels.size() < 1) {
throw std::runtime_error(
"hardware trigger has no trigger_logic channels");
}
if (!m_delay_trigger) {
throw std::runtime_error("no delay trigger available");
}
setStreamingFlag(false);
}
uint HardwareTrigger::numChannels() const
{
return m_num_channels;
}
HardwareTrigger::condition HardwareTrigger::analogCondition(uint chnIdx)
const
{
if (chnIdx >= numChannels()) {
throw std::invalid_argument("Channel index is out of range");
}
ssize_t ret;
char buf[4096];
ret = iio_channel_attr_read(m_analog_channels[chnIdx], "trigger", buf,
sizeof(buf));
if (ret < 0) {
throw std::runtime_error("failed to read attribute: trigger");
}
auto it = std::find(lut_analog_trigg_cond.begin(),
lut_analog_trigg_cond.end(), buf);
if (it == lut_analog_trigg_cond.end()) {
throw std::runtime_error(
"unexpected value read from attribute: trigger");
}
return static_cast<condition>(it - lut_analog_trigg_cond.begin());
}
void HardwareTrigger::setAnalogCondition(uint chnIdx, condition cond)
{
if (chnIdx >= numChannels()) {
throw std::invalid_argument("Channel index is out of range");
}
if (cond == ANY_EDGE) {
return; //or throw?
}
QByteArray byteArray = lut_analog_trigg_cond[cond].toLatin1();
iio_channel_attr_write(m_analog_channels[chnIdx], "trigger",
byteArray.data());
}
HardwareTrigger::condition HardwareTrigger::digitalCondition(uint chnIdx)
const
{
if (chnIdx >= numChannels()) {
throw std::invalid_argument("Channel index is out of range");
}
ssize_t ret;
char buf[4096];
ret = iio_channel_attr_read(m_digital_channels[chnIdx], "trigger", buf,
sizeof(buf));
if (ret < 0)
throw "failed to read attribute: trigger";
auto it = std::find(lut_digital_trigg_cond.begin(),
lut_digital_trigg_cond.end(), buf);
if (it == lut_digital_trigg_cond.end()) {
throw "unexpected value read from attribute: trigger";
}
return static_cast<condition>(it - lut_digital_trigg_cond.begin());
}
void HardwareTrigger::setDigitalCondition(uint chnIdx, condition cond)
{
if (chnIdx >= numChannels()) {
throw std::invalid_argument("Channel index is out of range");
}
QByteArray byteArray = lut_digital_trigg_cond[cond].toLatin1();
iio_channel_attr_write(m_digital_channels[chnIdx], "trigger",
byteArray.data());
}
int HardwareTrigger::level(uint chnIdx) const
{
if (chnIdx >= numChannels()) {
throw std::invalid_argument("Channel index is out of range");
}
long long val;
iio_channel_attr_read_longlong(m_analog_channels[chnIdx],
"trigger_level", &val);
return static_cast<int>(val);
}
void HardwareTrigger::setLevel(uint chnIdx, int level)
{
if (chnIdx >= numChannels()) {
throw std::invalid_argument("Channel index is out of range");
}
iio_channel_attr_write_longlong(m_analog_channels[chnIdx],
"trigger_level", static_cast<long long> (level));
}
int HardwareTrigger::hysteresis(uint chnIdx) const
{
if (chnIdx >= numChannels()) {
throw std::invalid_argument("Channel index is out of range");
}
long long val;
iio_channel_attr_read_longlong(m_analog_channels[chnIdx],
"trigger_hysteresis", &val);
return static_cast<int>(val);
}
void HardwareTrigger::setHysteresis(uint chnIdx, int histeresis)
{
if (chnIdx >= numChannels()) {
throw std::invalid_argument("Channel index is out of range");
}
iio_channel_attr_write_longlong(m_analog_channels[chnIdx],
"trigger_hysteresis", static_cast<long long>(histeresis));
}
HardwareTrigger::mode HardwareTrigger::triggerMode(uint chnIdx) const
{
if (chnIdx >= numChannels()) {
throw std::invalid_argument("Channel index is out of range");
}
ssize_t ret;
char buf[4096];
ret = iio_channel_attr_read(m_logic_channels[chnIdx], "mode", buf,
sizeof(buf));
if (ret < 0) {
throw ("failed to read attribute: mode");
}
auto it = std::find(lut_trigg_mode.begin(),
lut_trigg_mode.end(), buf);
if (it == lut_trigg_mode.end()) {
throw ("unexpected value read from attribute: mode");
}
return static_cast<mode>(it - lut_trigg_mode.begin());
}
void HardwareTrigger::setTriggerMode(uint chnIdx, HardwareTrigger::mode mode)
{
if (chnIdx >= numChannels()) {
throw std::invalid_argument("Channel index is out of range");
}
QByteArray byteArray = lut_trigg_mode[mode].toLatin1();
iio_channel_attr_write(m_logic_channels[chnIdx], "mode",
byteArray.data());
}
QString HardwareTrigger::source() const
{
char buf[4096];
iio_channel_attr_read(m_delay_trigger, "logic_mode", buf, sizeof(buf));
return QString(buf);
}
void HardwareTrigger::setSource(const QString& source)
{
QByteArray byteArray = source.toLatin1();
iio_channel_attr_write(m_delay_trigger, "logic_mode",
byteArray.data());
}
/*
* Convenience function to be used when willing to use the trigger for only one
* channel at a time.
*/
int HardwareTrigger::sourceChannel() const
{
int chnIdx = -1;
QString mode = source();
// Returning the channel index if a single channel is set as a source
// and -1 if multiple channels are set.
if (mode.length() == 1) {
chnIdx = mode.at(0).digitValue() - QChar('a').digitValue();
}
return chnIdx;
}
/*
* Convenience function to be used when willing to enable the trigger for only
* one channel at a time.
*/
void HardwareTrigger::setSourceChannel(uint chnIdx)
{
if (chnIdx >= numChannels()) {
throw std::invalid_argument("Channel index is out of range");
}
// Currently we don't need trigger on multiple channels simultaneously
// Also options 'a_OR_b', 'a_AND_b' and 'a_XOR_b' don't scale well for
// 3, 4, .. channels (combinations rise exponentially).
QChar chn('a' + chnIdx);
setSource(QString(chn));
}
int HardwareTrigger::delay() const
{
long long delay;
iio_channel_attr_read_longlong(m_delay_trigger, "delay", &delay);
return static_cast<int>(delay);
}
void HardwareTrigger::setDelay(int delay)
{
iio_channel_attr_write_longlong(m_delay_trigger, "delay", delay);
}
void HardwareTrigger::setStreamingFlag(bool val)
{
m_streaming_flag = val;
iio_device_attr_write_bool(m_trigger_device, "streaming", val);
}
bool HardwareTrigger::getStreamingFlag()
{
return m_streaming_flag;
}
HardwareTrigger::settings_uptr HardwareTrigger::getCurrentHwSettings()
{
settings_uptr settings(new Settings);
for (uint i = 0; i < numChannels(); i++) {
settings->analog_condition.push_back(analogCondition(i));
settings->digital_condition.push_back(digitalCondition(i));
settings->level.push_back(level(i));
settings->hysteresis.push_back(hysteresis(i));
settings->mode.push_back(triggerMode(i));
settings->source = source();
settings->delay = delay();
}
return settings;
}
void HardwareTrigger::setHwTriggerSettings(struct Settings *settings)
{
for (uint i = 0; i < numChannels(); i++) {
setAnalogCondition(i, settings->analog_condition[i]);
setDigitalCondition(i, settings->digital_condition[i]);
setLevel(i, settings->level[i]);
setHysteresis(i, settings->hysteresis[i]);
setTriggerMode(i, settings->mode[i]);
setSource(settings->source);
setDelay(settings->delay);
}
}