]>
Commit | Line | Data |
---|---|---|
28a4c9c5 | 1 | /* |
b3f22de0 | 2 | * This file is part of the PulseView project. |
28a4c9c5 JH |
3 | * |
4 | * Copyright (C) 2012 Joel Holdsworth <joel@airwebreathe.org.uk> | |
5 | * | |
6 | * This program is free software; you can redistribute it and/or modify | |
7 | * it under the terms of the GNU General Public License as published by | |
8 | * the Free Software Foundation; either version 2 of the License, or | |
9 | * (at your option) any later version. | |
10 | * | |
11 | * This program is distributed in the hope that it will be useful, | |
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 | * GNU General Public License for more details. | |
15 | * | |
16 | * You should have received a copy of the GNU General Public License | |
efdec55a | 17 | * along with this program; if not, see <http://www.gnu.org/licenses/>. |
28a4c9c5 JH |
18 | */ |
19 | ||
cef18fc6 | 20 | #include <extdef.h> |
7cd5faf8 | 21 | |
d2344534 JH |
22 | #include <cassert> |
23 | #include <cmath> | |
3e46726a | 24 | |
640bd149 PZ |
25 | #include <algorithm> |
26 | ||
ab6d2eab | 27 | #include <QApplication> |
b213ef09 JH |
28 | #include <QFormLayout> |
29 | #include <QToolBar> | |
30 | ||
2acdb232 JH |
31 | #include "logicsignal.hpp" |
32 | #include "view.hpp" | |
33 | ||
2acdb232 | 34 | #include <pv/data/logic.hpp> |
f3d66e52 | 35 | #include <pv/data/logicsegment.hpp> |
bf0edd2b | 36 | #include <pv/data/signalbase.hpp> |
aca9aa83 UH |
37 | #include <pv/devicemanager.hpp> |
38 | #include <pv/devices/device.hpp> | |
051ba3b3 | 39 | #include <pv/globalsettings.hpp> |
aca9aa83 | 40 | #include <pv/session.hpp> |
28a4c9c5 | 41 | |
fe3a1c21 | 42 | #include <libsigrokcxx/libsigrokcxx.hpp> |
e8d00928 | 43 | |
819f4c25 JH |
44 | using std::deque; |
45 | using std::max; | |
a5d93c27 | 46 | using std::make_pair; |
819f4c25 | 47 | using std::min; |
6f925ba9 | 48 | using std::none_of; |
526c8c00 | 49 | using std::out_of_range; |
819f4c25 | 50 | using std::pair; |
f9abf97e | 51 | using std::shared_ptr; |
819f4c25 | 52 | using std::vector; |
81a635d1 | 53 | |
e8d00928 | 54 | using sigrok::ConfigKey; |
7bb0fbf4 | 55 | using sigrok::Capability; |
e8d00928 | 56 | using sigrok::Trigger; |
640bd149 | 57 | using sigrok::TriggerMatch; |
e8d00928 ML |
58 | using sigrok::TriggerMatchType; |
59 | ||
eeceee99 SA |
60 | using pv::data::LogicSegment; |
61 | ||
51e77110 | 62 | namespace pv { |
f4e57597 | 63 | namespace views { |
1573bf16 | 64 | namespace trace { |
51e77110 | 65 | |
17d94d7c | 66 | const float LogicSignal::Oversampling = 2.0f; |
131e8012 | 67 | |
641574bc SA |
68 | const QColor LogicSignal::EdgeColor(0x80, 0x80, 0x80); |
69 | const QColor LogicSignal::HighColor(0x00, 0xC0, 0x00); | |
70 | const QColor LogicSignal::LowColor(0xC0, 0x00, 0x00); | |
71 | const QColor LogicSignal::SamplingPointColor(0x77, 0x77, 0x77); | |
131e8012 | 72 | |
641574bc | 73 | QColor LogicSignal::TriggerMarkerBackgroundColor = QColor(0xED, 0xD4, 0x00); |
ddfda54d JH |
74 | const int LogicSignal::TriggerMarkerPadding = 2; |
75 | const char* LogicSignal::TriggerMarkerIcons[8] = { | |
76 | nullptr, | |
77 | ":/icons/trigger-marker-low.svg", | |
78 | ":/icons/trigger-marker-high.svg", | |
79 | ":/icons/trigger-marker-rising.svg", | |
80 | ":/icons/trigger-marker-falling.svg", | |
81 | ":/icons/trigger-marker-change.svg", | |
82 | nullptr, | |
83 | nullptr | |
84 | }; | |
85 | ||
86 | QCache<QString, const QIcon> LogicSignal::icon_cache_; | |
87 | QCache<QString, const QPixmap> LogicSignal::pixmap_cache_; | |
88 | ||
b86aa8f4 | 89 | LogicSignal::LogicSignal( |
2b81ae46 | 90 | pv::Session &session, |
da30ecb7 | 91 | shared_ptr<devices::Device> device, |
0aa57689 | 92 | shared_ptr<data::SignalBase> base) : |
73a25a6e | 93 | Signal(session, base), |
8dbbc7f0 | 94 | device_(device), |
2795de2e | 95 | trigger_types_(get_trigger_types()), |
4c60462b JH |
96 | trigger_none_(nullptr), |
97 | trigger_rising_(nullptr), | |
98 | trigger_high_(nullptr), | |
99 | trigger_falling_(nullptr), | |
100 | trigger_low_(nullptr), | |
101 | trigger_change_(nullptr) | |
28a4c9c5 | 102 | { |
e8d00928 | 103 | shared_ptr<Trigger> trigger; |
b1e8c93d | 104 | |
3b2ead4f SA |
105 | GlobalSettings settings; |
106 | signal_height_ = settings.value(GlobalSettings::Key_View_DefaultLogicHeight).toInt(); | |
107 | show_sampling_points_ = | |
108 | settings.value(GlobalSettings::Key_View_ShowSamplingPoints).toBool(); | |
109 | fill_high_areas_ = | |
110 | settings.value(GlobalSettings::Key_View_FillSignalHighAreas).toBool(); | |
111 | high_fill_color_ = QColor::fromRgba(settings.value( | |
112 | GlobalSettings::Key_View_FillSignalHighAreaColor).value<uint32_t>()); | |
a2b2b65e | 113 | |
b1e8c93d BV |
114 | /* Populate this channel's trigger setting with whatever we |
115 | * find in the current session trigger, if anything. */ | |
8dbbc7f0 JH |
116 | trigger_match_ = nullptr; |
117 | if ((trigger = session_.session()->trigger())) | |
e8d00928 ML |
118 | for (auto stage : trigger->stages()) |
119 | for (auto match : stage->matches()) | |
73a25a6e | 120 | if (match->channel() == base_->channel()) |
8dbbc7f0 | 121 | trigger_match_ = match->type(); |
ef8311a4 JH |
122 | } |
123 | ||
748dd753 | 124 | shared_ptr<pv::data::SignalData> LogicSignal::data() const |
9a0cd293 | 125 | { |
0aa57689 | 126 | return base_->logic_data(); |
9a0cd293 JH |
127 | } |
128 | ||
748dd753 | 129 | shared_ptr<pv::data::Logic> LogicSignal::logic_data() const |
e0fc5810 | 130 | { |
0aa57689 | 131 | return base_->logic_data(); |
8e097c27 JH |
132 | } |
133 | ||
0a952555 | 134 | std::map<QString, QVariant> LogicSignal::save_settings() const |
a2b2b65e | 135 | { |
0a952555 SA |
136 | std::map<QString, QVariant> result; |
137 | ||
138 | result["trace_height"] = signal_height_; | |
139 | ||
140 | return result; | |
a2b2b65e SA |
141 | } |
142 | ||
0a952555 | 143 | void LogicSignal::restore_settings(std::map<QString, QVariant> settings) |
a2b2b65e | 144 | { |
0a952555 SA |
145 | auto entry = settings.find("trace_height"); |
146 | if (entry != settings.end()) { | |
a2b2b65e | 147 | const int old_height = signal_height_; |
0a952555 | 148 | signal_height_ = settings["trace_height"].toInt(); |
a2b2b65e SA |
149 | |
150 | if ((signal_height_ != old_height) && owner_) { | |
151 | // Call order is important, otherwise the lazy event handler won't work | |
152 | owner_->extents_changed(false, true); | |
153 | owner_->row_item_appearance_changed(false, true); | |
154 | } | |
155 | } | |
156 | } | |
157 | ||
6f925ba9 | 158 | pair<int, int> LogicSignal::v_extents() const |
a5d93c27 | 159 | { |
ab6d2eab JH |
160 | const int signal_margin = |
161 | QFontMetrics(QApplication::font()).height() / 2; | |
303d6ea6 | 162 | return make_pair(-signal_height_ - signal_margin, signal_margin); |
a5d93c27 JH |
163 | } |
164 | ||
60938e04 | 165 | void LogicSignal::paint_mid(QPainter &p, ViewItemPaintParams &pp) |
e3f65ace | 166 | { |
64b60583 | 167 | QLineF *line; |
2858b391 JH |
168 | |
169 | vector< pair<int64_t, bool> > edges; | |
170 | ||
73a25a6e | 171 | assert(base_); |
8dbbc7f0 | 172 | assert(owner_); |
2858b391 | 173 | |
be9e7b4b | 174 | const int y = get_visual_y(); |
eae6e30a | 175 | |
73a25a6e | 176 | if (!base_->enabled()) |
cec48d16 JH |
177 | return; |
178 | ||
2658961b | 179 | const float low_offset = y + 0.5f; |
4521022b | 180 | const float high_offset = low_offset - signal_height_; |
131e8012 | 181 | |
eeceee99 | 182 | shared_ptr<LogicSegment> segment = get_logic_segment_to_paint(); |
4bd0ecb8 | 183 | if (!segment || (segment->get_sample_count() == 0)) |
526c8c00 | 184 | return; |
2858b391 | 185 | |
f3d66e52 | 186 | double samplerate = segment->samplerate(); |
9d4e5cd8 JH |
187 | |
188 | // Show sample rate as 1Hz when it is unknown | |
9ba4ca35 | 189 | if (samplerate == 0.0) |
9d4e5cd8 JH |
190 | samplerate = 1.0; |
191 | ||
4c8a6a6d | 192 | const double pixels_offset = pp.pixels_offset(); |
60d9b99a | 193 | const pv::util::Timestamp& start_time = segment->start_time(); |
20df1a39 | 194 | const int64_t last_sample = (int64_t)segment->get_sample_count() - 1; |
4c8a6a6d | 195 | const double samples_per_pixel = samplerate * pp.scale(); |
124be547 | 196 | const double pixels_per_sample = 1 / samples_per_pixel; |
60d9b99a JS |
197 | const pv::util::Timestamp start = samplerate * (pp.offset() - start_time); |
198 | const pv::util::Timestamp end = start + samples_per_pixel * pp.width(); | |
2858b391 | 199 | |
60d9b99a JS |
200 | const int64_t start_sample = min(max(floor(start).convert_to<int64_t>(), |
201 | (int64_t)0), last_sample); | |
202 | const uint64_t end_sample = min(max(ceil(end).convert_to<int64_t>(), | |
203 | (int64_t)0), last_sample); | |
204 | ||
205 | segment->get_subsampled_edges(edges, start_sample, end_sample, | |
73a25a6e | 206 | samples_per_pixel / Oversampling, base_->index()); |
c352ce60 | 207 | assert(edges.size() >= 2); |
2858b391 | 208 | |
4521022b SA |
209 | const float first_sample_x = |
210 | pp.left() + (edges.front().first / samples_per_pixel - pixels_offset); | |
211 | const float last_sample_x = | |
212 | pp.left() + (edges.back().first / samples_per_pixel - pixels_offset); | |
213 | ||
124be547 | 214 | // Check whether we need to paint the sampling points |
3b2ead4f | 215 | const bool show_sampling_points = show_sampling_points_ && (samples_per_pixel < 0.25); |
124be547 | 216 | vector<QRectF> sampling_points; |
4521022b | 217 | float sampling_point_x = first_sample_x; |
124be547 SA |
218 | int64_t sampling_point_sample = start_sample; |
219 | const int w = 2; | |
220 | ||
4521022b | 221 | if (show_sampling_points) |
124be547 | 222 | sampling_points.reserve(end_sample - start_sample + 1); |
4521022b | 223 | |
4521022b | 224 | vector<QRectF> high_rects; |
099d2317 SA |
225 | float rising_edge_x; |
226 | bool rising_edge_seen = false; | |
124be547 | 227 | |
2858b391 | 228 | // Paint the edges |
64b60583 JH |
229 | const unsigned int edge_count = edges.size() - 2; |
230 | QLineF *const edge_lines = new QLineF[edge_count]; | |
231 | line = edge_lines; | |
232 | ||
099d2317 SA |
233 | if (edges.front().second) { |
234 | // Beginning of trace is high | |
235 | rising_edge_x = first_sample_x; | |
236 | rising_edge_seen = true; | |
237 | } | |
4521022b | 238 | |
f46e495e | 239 | for (auto i = edges.cbegin() + 1; i != edges.cend() - 1; i++) { |
099d2317 SA |
240 | // Note: multiple edges occupying a single pixel are represented by an edge |
241 | // with undefined logic level. This means that only the first falling edge | |
242 | // after a rising edge corresponds to said rising edge - and vice versa. If | |
243 | // more edges with the same logic level follow, they denote multiple edges. | |
244 | ||
245 | const float x = pp.left() + ((*i).first / samples_per_pixel - pixels_offset); | |
64b60583 | 246 | *line++ = QLineF(x, high_offset, x, low_offset); |
124be547 | 247 | |
3b2ead4f | 248 | if (fill_high_areas_) { |
099d2317 | 249 | // Any edge terminates a high area |
bae42c1d SA |
250 | if (rising_edge_seen) { |
251 | const int width = x - rising_edge_x; | |
252 | if (width > 0) | |
253 | high_rects.emplace_back(rising_edge_x, high_offset, | |
254 | width, signal_height_); | |
099d2317 SA |
255 | rising_edge_seen = false; |
256 | } | |
257 | ||
258 | // Only rising edges start high areas | |
259 | if ((*i).second) { | |
260 | rising_edge_x = x; | |
261 | rising_edge_seen = true; | |
262 | } | |
4521022b SA |
263 | } |
264 | ||
124be547 SA |
265 | if (show_sampling_points) |
266 | while (sampling_point_sample < (*i).first) { | |
267 | const float y = (*i).second ? low_offset : high_offset; | |
268 | sampling_points.emplace_back( | |
269 | QRectF(sampling_point_x - (w / 2), y - (w / 2), w, w)); | |
270 | sampling_point_sample++; | |
271 | sampling_point_x += pixels_per_sample; | |
272 | }; | |
2858b391 | 273 | } |
81a635d1 | 274 | |
124be547 SA |
275 | // Calculate the sample points from the last edge to the end of the trace |
276 | if (show_sampling_points) | |
277 | while ((uint64_t)sampling_point_sample <= end_sample) { | |
278 | // Signal changed after the last edge, so the level is inverted | |
279 | const float y = (edges.cend() - 1)->second ? high_offset : low_offset; | |
280 | sampling_points.emplace_back( | |
281 | QRectF(sampling_point_x - (w / 2), y - (w / 2), w, w)); | |
282 | sampling_point_sample++; | |
283 | sampling_point_x += pixels_per_sample; | |
284 | }; | |
285 | ||
3b2ead4f | 286 | if (fill_high_areas_) { |
099d2317 SA |
287 | // Add last high rectangle if the signal is still high at the end of the trace |
288 | if (rising_edge_seen && (edges.cend() - 1)->second) | |
289 | high_rects.emplace_back(rising_edge_x, high_offset, | |
290 | last_sample_x - rising_edge_x, signal_height_); | |
4521022b | 291 | |
3b2ead4f SA |
292 | p.setPen(high_fill_color_); |
293 | p.setBrush(high_fill_color_); | |
4521022b SA |
294 | p.drawRects((const QRectF*)(high_rects.data()), high_rects.size()); |
295 | } | |
296 | ||
641574bc | 297 | p.setPen(EdgeColor); |
64b60583 JH |
298 | p.drawLines(edge_lines, edge_count); |
299 | delete[] edge_lines; | |
2858b391 JH |
300 | |
301 | // Paint the caps | |
175d6573 | 302 | const unsigned int max_cap_line_count = edges.size(); |
64b60583 | 303 | QLineF *const cap_lines = new QLineF[max_cap_line_count]; |
2858b391 | 304 | |
641574bc | 305 | p.setPen(HighColor); |
64b60583 | 306 | paint_caps(p, cap_lines, edges, true, samples_per_pixel, |
3eb29afd | 307 | pixels_offset, pp.left(), high_offset); |
641574bc | 308 | p.setPen(LowColor); |
64b60583 | 309 | paint_caps(p, cap_lines, edges, false, samples_per_pixel, |
3eb29afd | 310 | pixels_offset, pp.left(), low_offset); |
2858b391 | 311 | |
64b60583 | 312 | delete[] cap_lines; |
e9c8e87c UH |
313 | |
314 | // Paint the sampling points | |
124be547 | 315 | if (show_sampling_points) { |
641574bc | 316 | p.setPen(SamplingPointColor); |
124be547 | 317 | p.drawRects(sampling_points.data(), sampling_points.size()); |
e9c8e87c | 318 | } |
131e8012 | 319 | } |
2858b391 | 320 | |
60938e04 | 321 | void LogicSignal::paint_fore(QPainter &p, ViewItemPaintParams &pp) |
ddfda54d | 322 | { |
cab7bc99 SA |
323 | if (base_->enabled()) { |
324 | if (trigger_match_) { | |
325 | // Draw the trigger marker | |
326 | const int y = get_visual_y(); | |
327 | ||
328 | for (int32_t type_id : trigger_types_) { | |
329 | const TriggerMatchType *const type = | |
330 | TriggerMatchType::get(type_id); | |
331 | if (trigger_match_ != type || type_id < 0 || | |
332 | (size_t)type_id >= countof(TriggerMarkerIcons) || | |
333 | !TriggerMarkerIcons[type_id]) | |
334 | continue; | |
335 | ||
336 | const QPixmap *const pixmap = get_pixmap( | |
337 | TriggerMarkerIcons[type_id]); | |
338 | if (!pixmap) | |
339 | continue; | |
340 | ||
341 | const float pad = TriggerMarkerPadding - 0.5f; | |
342 | const QSize size = pixmap->size(); | |
343 | const QPoint point( | |
344 | pp.right() - size.width() - pad * 2, | |
345 | y - (signal_height_ + size.height()) / 2); | |
346 | ||
347 | p.setPen(QPen(TriggerMarkerBackgroundColor.darker())); | |
348 | p.setBrush(TriggerMarkerBackgroundColor); | |
349 | p.drawRoundedRect(QRectF(point, size).adjusted( | |
350 | -pad, -pad, pad, pad), pad, pad); | |
351 | p.drawPixmap(point, *pixmap); | |
352 | ||
353 | break; | |
354 | } | |
1931b5f9 | 355 | } |
1931b5f9 | 356 | |
cab7bc99 SA |
357 | if (show_hover_marker_) |
358 | paint_hover_marker(p); | |
359 | } | |
ddfda54d JH |
360 | } |
361 | ||
eeceee99 | 362 | vector<LogicSegment::EdgePair> LogicSignal::get_nearest_level_changes(uint64_t sample_pos) |
b4bc9b55 | 363 | { |
b4bc9b55 SA |
364 | assert(base_); |
365 | assert(owner_); | |
366 | ||
eeceee99 SA |
367 | if (sample_pos == 0) |
368 | return vector<LogicSegment::EdgePair>(); | |
b4bc9b55 | 369 | |
eeceee99 | 370 | shared_ptr<LogicSegment> segment = get_logic_segment_to_paint(); |
b4bc9b55 | 371 | if (!segment || (segment->get_sample_count() == 0)) |
eeceee99 | 372 | return vector<LogicSegment::EdgePair>(); |
b4bc9b55 SA |
373 | |
374 | const View *view = owner_->view(); | |
375 | assert(view); | |
eeceee99 | 376 | const double samples_per_pixel = base_->get_samplerate() * view->scale(); |
b4bc9b55 | 377 | |
eeceee99 | 378 | vector<LogicSegment::EdgePair> edges; |
b4bc9b55 SA |
379 | |
380 | segment->get_surrounding_edges(edges, sample_pos, | |
381 | samples_per_pixel / Oversampling, base_->index()); | |
382 | ||
383 | if (edges.empty()) | |
eeceee99 SA |
384 | return vector<LogicSegment::EdgePair>(); |
385 | ||
386 | return edges; | |
b4bc9b55 SA |
387 | } |
388 | ||
f4270878 | 389 | void LogicSignal::paint_caps(QPainter &p, QLineF *const lines, |
131e8012 | 390 | vector< pair<int64_t, bool> > &edges, bool level, |
ce6e73a8 JH |
391 | double samples_per_pixel, double pixels_offset, float x_offset, |
392 | float y_offset) | |
131e8012 | 393 | { |
64b60583 | 394 | QLineF *line = lines; |
2858b391 | 395 | |
f46e495e | 396 | for (auto i = edges.begin(); i != (edges.end() - 1); i++) |
333d5bbc | 397 | if ((*i).second == level) { |
64b60583 | 398 | *line++ = QLineF( |
ce6e73a8 | 399 | ((*i).first / samples_per_pixel - |
64b60583 | 400 | pixels_offset) + x_offset, y_offset, |
ce6e73a8 | 401 | ((*(i+1)).first / samples_per_pixel - |
64b60583 | 402 | pixels_offset) + x_offset, y_offset); |
131e8012 JH |
403 | } |
404 | ||
64b60583 | 405 | p.drawLines(lines, line - lines); |
e3f65ace | 406 | } |
3e46726a | 407 | |
7daebd05 SA |
408 | shared_ptr<pv::data::LogicSegment> LogicSignal::get_logic_segment_to_paint() const |
409 | { | |
410 | shared_ptr<pv::data::LogicSegment> segment; | |
411 | ||
412 | const deque< shared_ptr<pv::data::LogicSegment> > &segments = | |
413 | base_->logic_data()->logic_segments(); | |
414 | ||
415 | if (!segments.empty()) { | |
416 | if (segment_display_mode_ == ShowLastSegmentOnly) { | |
417 | segment = segments.back(); | |
418 | } | |
419 | ||
558ad6ce SA |
420 | if ((segment_display_mode_ == ShowSingleSegmentOnly) || |
421 | (segment_display_mode_ == ShowLastCompleteSegmentOnly)) { | |
7daebd05 SA |
422 | try { |
423 | segment = segments.at(current_segment_); | |
30677c13 | 424 | } catch (out_of_range&) { |
341d9a79 | 425 | qDebug() << "Current logic segment out of range for signal" << base_->name() << ":" << current_segment_; |
7daebd05 SA |
426 | } |
427 | } | |
428 | } | |
429 | ||
430 | return segment; | |
431 | } | |
432 | ||
767281c8 JH |
433 | void LogicSignal::init_trigger_actions(QWidget *parent) |
434 | { | |
0f1f98fe | 435 | trigger_none_ = new QAction(*get_icon(":/icons/trigger-none.svg"), |
767281c8 | 436 | tr("No trigger"), parent); |
8dbbc7f0 JH |
437 | trigger_none_->setCheckable(true); |
438 | connect(trigger_none_, SIGNAL(triggered()), this, SLOT(on_trigger())); | |
767281c8 | 439 | |
0f1f98fe | 440 | trigger_rising_ = new QAction(*get_icon(":/icons/trigger-rising.svg"), |
767281c8 | 441 | tr("Trigger on rising edge"), parent); |
8dbbc7f0 JH |
442 | trigger_rising_->setCheckable(true); |
443 | connect(trigger_rising_, SIGNAL(triggered()), this, SLOT(on_trigger())); | |
767281c8 | 444 | |
0f1f98fe | 445 | trigger_high_ = new QAction(*get_icon(":/icons/trigger-high.svg"), |
767281c8 | 446 | tr("Trigger on high level"), parent); |
8dbbc7f0 JH |
447 | trigger_high_->setCheckable(true); |
448 | connect(trigger_high_, SIGNAL(triggered()), this, SLOT(on_trigger())); | |
767281c8 | 449 | |
0f1f98fe | 450 | trigger_falling_ = new QAction(*get_icon(":/icons/trigger-falling.svg"), |
767281c8 | 451 | tr("Trigger on falling edge"), parent); |
8dbbc7f0 JH |
452 | trigger_falling_->setCheckable(true); |
453 | connect(trigger_falling_, SIGNAL(triggered()), this, SLOT(on_trigger())); | |
767281c8 | 454 | |
0f1f98fe | 455 | trigger_low_ = new QAction(*get_icon(":/icons/trigger-low.svg"), |
767281c8 | 456 | tr("Trigger on low level"), parent); |
8dbbc7f0 JH |
457 | trigger_low_->setCheckable(true); |
458 | connect(trigger_low_, SIGNAL(triggered()), this, SLOT(on_trigger())); | |
767281c8 | 459 | |
0f1f98fe | 460 | trigger_change_ = new QAction(*get_icon(":/icons/trigger-change.svg"), |
767281c8 | 461 | tr("Trigger on rising or falling edge"), parent); |
8dbbc7f0 JH |
462 | trigger_change_->setCheckable(true); |
463 | connect(trigger_change_, SIGNAL(triggered()), this, SLOT(on_trigger())); | |
767281c8 JH |
464 | } |
465 | ||
77817b43 JH |
466 | const vector<int32_t> LogicSignal::get_trigger_types() const |
467 | { | |
3afe5afe SA |
468 | // We may not be associated with a device |
469 | if (!device_) | |
470 | return vector<int32_t>(); | |
471 | ||
da30ecb7 | 472 | const auto sr_dev = device_->device(); |
7bb0fbf4 ML |
473 | if (sr_dev->config_check(ConfigKey::TRIGGER_MATCH, Capability::LIST)) { |
474 | const Glib::VariantContainerBase gvar = | |
475 | sr_dev->config_list(ConfigKey::TRIGGER_MATCH); | |
d60820d8 SA |
476 | |
477 | vector<int32_t> ttypes; | |
478 | ||
479 | for (unsigned int i = 0; i < gvar.get_n_children(); i++) { | |
480 | Glib::VariantBase tmp_vb; | |
481 | gvar.get_child(tmp_vb, i); | |
482 | ||
483 | Glib::Variant<int32_t> tmp_v = | |
484 | Glib::VariantBase::cast_dynamic< Glib::Variant<int32_t> >(tmp_vb); | |
485 | ||
486 | ttypes.push_back(tmp_v.get()); | |
487 | } | |
488 | ||
489 | return ttypes; | |
7bb0fbf4 ML |
490 | } else { |
491 | return vector<int32_t>(); | |
77817b43 JH |
492 | } |
493 | } | |
494 | ||
aa5e9140 | 495 | QAction* LogicSignal::action_from_trigger_type(const TriggerMatchType *type) |
767281c8 | 496 | { |
b1e8c93d BV |
497 | QAction *action; |
498 | ||
8dbbc7f0 | 499 | action = trigger_none_; |
640bd149 PZ |
500 | if (type) { |
501 | switch (type->id()) { | |
502 | case SR_TRIGGER_ZERO: | |
8dbbc7f0 | 503 | action = trigger_low_; |
640bd149 PZ |
504 | break; |
505 | case SR_TRIGGER_ONE: | |
8dbbc7f0 | 506 | action = trigger_high_; |
640bd149 PZ |
507 | break; |
508 | case SR_TRIGGER_RISING: | |
8dbbc7f0 | 509 | action = trigger_rising_; |
640bd149 PZ |
510 | break; |
511 | case SR_TRIGGER_FALLING: | |
8dbbc7f0 | 512 | action = trigger_falling_; |
640bd149 PZ |
513 | break; |
514 | case SR_TRIGGER_EDGE: | |
8dbbc7f0 | 515 | action = trigger_change_; |
640bd149 PZ |
516 | break; |
517 | default: | |
0402d7a3 | 518 | assert(false); |
640bd149 | 519 | } |
767281c8 | 520 | } |
767281c8 | 521 | |
b1e8c93d | 522 | return action; |
767281c8 JH |
523 | } |
524 | ||
aa5e9140 | 525 | const TriggerMatchType *LogicSignal::trigger_type_from_action(QAction *action) |
139fef92 | 526 | { |
8dbbc7f0 | 527 | if (action == trigger_low_) |
e8d00928 | 528 | return TriggerMatchType::ZERO; |
8dbbc7f0 | 529 | else if (action == trigger_high_) |
e8d00928 | 530 | return TriggerMatchType::ONE; |
8dbbc7f0 | 531 | else if (action == trigger_rising_) |
e8d00928 | 532 | return TriggerMatchType::RISING; |
8dbbc7f0 | 533 | else if (action == trigger_falling_) |
e8d00928 | 534 | return TriggerMatchType::FALLING; |
8dbbc7f0 | 535 | else if (action == trigger_change_) |
e8d00928 | 536 | return TriggerMatchType::EDGE; |
b1e8c93d | 537 | else |
e8d00928 | 538 | return nullptr; |
139fef92 JH |
539 | } |
540 | ||
b1e8c93d | 541 | void LogicSignal::populate_popup_form(QWidget *parent, QFormLayout *form) |
b08d7222 | 542 | { |
b1e8c93d | 543 | Signal::populate_popup_form(parent, form); |
b08d7222 | 544 | |
a2b2b65e SA |
545 | signal_height_sb_ = new QSpinBox(parent); |
546 | signal_height_sb_->setRange(5, 1000); | |
547 | signal_height_sb_->setSingleStep(5); | |
548 | signal_height_sb_->setSuffix(tr(" pixels")); | |
549 | signal_height_sb_->setValue(signal_height_); | |
550 | connect(signal_height_sb_, SIGNAL(valueChanged(int)), | |
551 | this, SLOT(on_signal_height_changed(int))); | |
552 | form->addRow(tr("Trace height"), signal_height_sb_); | |
553 | ||
554 | // Trigger settings | |
77817b43 | 555 | const vector<int32_t> trig_types = get_trigger_types(); |
139fef92 | 556 | |
ffd90566 JH |
557 | if (!trig_types.empty()) { |
558 | trigger_bar_ = new QToolBar(parent); | |
559 | init_trigger_actions(trigger_bar_); | |
560 | trigger_bar_->addAction(trigger_none_); | |
561 | trigger_none_->setChecked(!trigger_match_); | |
562 | ||
563 | for (auto type_id : trig_types) { | |
564 | const TriggerMatchType *const type = | |
565 | TriggerMatchType::get(type_id); | |
566 | QAction *const action = action_from_trigger_type(type); | |
567 | trigger_bar_->addAction(action); | |
568 | action->setChecked(trigger_match_ == type); | |
569 | } | |
f02b6a98 SA |
570 | |
571 | // Only allow triggers to be changed when we're stopped | |
572 | if (session_.get_capture_state() != Session::Stopped) | |
7a0d99e6 SA |
573 | for (QAction* action : trigger_bar_->findChildren<QAction*>()) // clazy:exclude=range-loop |
574 | action->setEnabled(false); | |
f02b6a98 | 575 | |
ffd90566 JH |
576 | form->addRow(tr("Trigger"), trigger_bar_); |
577 | } | |
b08d7222 JH |
578 | } |
579 | ||
640bd149 PZ |
580 | void LogicSignal::modify_trigger() |
581 | { | |
8dbbc7f0 JH |
582 | auto trigger = session_.session()->trigger(); |
583 | auto new_trigger = session_.device_manager().context()->create_trigger("pulseview"); | |
640bd149 PZ |
584 | |
585 | if (trigger) { | |
586 | for (auto stage : trigger->stages()) { | |
587 | const auto &matches = stage->matches(); | |
6f925ba9 | 588 | if (none_of(matches.begin(), matches.end(), |
640bd149 | 589 | [&](shared_ptr<TriggerMatch> match) { |
73a25a6e | 590 | return match->channel() != base_->channel(); })) |
640bd149 PZ |
591 | continue; |
592 | ||
593 | auto new_stage = new_trigger->add_stage(); | |
594 | for (auto match : stage->matches()) { | |
73a25a6e | 595 | if (match->channel() == base_->channel()) |
640bd149 PZ |
596 | continue; |
597 | new_stage->add_match(match->channel(), match->type()); | |
598 | } | |
599 | } | |
600 | } | |
601 | ||
fcc00c4d TS |
602 | if (trigger_match_) { |
603 | // Until we can let the user decide how to group trigger matches | |
604 | // into stages, put all of the matches into a single stage -- | |
605 | // most devices only support a single trigger stage. | |
606 | if (new_trigger->stages().empty()) | |
607 | new_trigger->add_stage(); | |
608 | ||
73a25a6e | 609 | new_trigger->stages().back()->add_match(base_->channel(), |
bf0edd2b | 610 | trigger_match_); |
fcc00c4d | 611 | } |
640bd149 | 612 | |
8dbbc7f0 | 613 | session_.session()->set_trigger( |
640bd149 | 614 | new_trigger->stages().empty() ? nullptr : new_trigger); |
ddfda54d JH |
615 | |
616 | if (owner_) | |
6e2c3c85 | 617 | owner_->row_item_appearance_changed(false, true); |
640bd149 PZ |
618 | } |
619 | ||
0f1f98fe JH |
620 | const QIcon* LogicSignal::get_icon(const char *path) |
621 | { | |
ed2cec68 SA |
622 | if (!icon_cache_.contains(path)) { |
623 | const QIcon *icon = new QIcon(path); | |
0f1f98fe JH |
624 | icon_cache_.insert(path, icon); |
625 | } | |
626 | ||
ed2cec68 | 627 | return icon_cache_.take(path); |
0f1f98fe JH |
628 | } |
629 | ||
ddfda54d JH |
630 | const QPixmap* LogicSignal::get_pixmap(const char *path) |
631 | { | |
ed2cec68 SA |
632 | if (!pixmap_cache_.contains(path)) { |
633 | const QPixmap *pixmap = new QPixmap(path); | |
ddfda54d JH |
634 | pixmap_cache_.insert(path, pixmap); |
635 | } | |
636 | ||
ed2cec68 | 637 | return pixmap_cache_.take(path); |
ddfda54d JH |
638 | } |
639 | ||
3b2ead4f SA |
640 | void LogicSignal::on_setting_changed(const QString &key, const QVariant &value) |
641 | { | |
642 | Signal::on_setting_changed(key, value); | |
643 | ||
644 | if (key == GlobalSettings::Key_View_ShowSamplingPoints) | |
645 | show_sampling_points_ = value.toBool(); | |
646 | ||
647 | if (key == GlobalSettings::Key_View_FillSignalHighAreas) | |
648 | fill_high_areas_ = value.toBool(); | |
649 | ||
650 | if (key == GlobalSettings::Key_View_FillSignalHighAreaColor) | |
651 | high_fill_color_ = QColor::fromRgba(value.value<uint32_t>()); | |
652 | } | |
653 | ||
b1e8c93d | 654 | void LogicSignal::on_trigger() |
b08d7222 | 655 | { |
b1e8c93d | 656 | QAction *action; |
b08d7222 | 657 | |
aa5e9140 | 658 | action_from_trigger_type(trigger_match_)->setChecked(false); |
b08d7222 | 659 | |
b1e8c93d | 660 | action = (QAction *)sender(); |
923f8b22 | 661 | action->setChecked(true); |
aa5e9140 | 662 | trigger_match_ = trigger_type_from_action(action); |
b08d7222 | 663 | |
640bd149 | 664 | modify_trigger(); |
b08d7222 JH |
665 | } |
666 | ||
a2b2b65e SA |
667 | void LogicSignal::on_signal_height_changed(int height) |
668 | { | |
669 | signal_height_ = height; | |
670 | ||
671 | if (owner_) { | |
672 | // Call order is important, otherwise the lazy event handler won't work | |
673 | owner_->extents_changed(false, true); | |
674 | owner_->row_item_appearance_changed(false, true); | |
675 | } | |
676 | } | |
677 | ||
1573bf16 | 678 | } // namespace trace |
f4e57597 | 679 | } // namespace views |
51e77110 | 680 | } // namespace pv |