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