]>
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 | |
17 | * along with this program; if not, write to the Free Software | |
18 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA | |
19 | */ | |
20 | ||
cef18fc6 | 21 | #include <extdef.h> |
7cd5faf8 | 22 | |
d2344534 JH |
23 | #include <cassert> |
24 | #include <cmath> | |
3e46726a | 25 | |
640bd149 PZ |
26 | #include <algorithm> |
27 | ||
b213ef09 JH |
28 | #include <QFormLayout> |
29 | #include <QToolBar> | |
30 | ||
28a4c9c5 | 31 | #include "logicsignal.h" |
2658961b | 32 | #include "view.h" |
0a78bcaf | 33 | |
19adbc2c | 34 | #include <pv/sigsession.h> |
640bd149 | 35 | #include <pv/devicemanager.h> |
19adbc2c JH |
36 | #include <pv/data/logic.h> |
37 | #include <pv/data/logicsnapshot.h> | |
38 | #include <pv/view/view.h> | |
28a4c9c5 | 39 | |
e8d00928 ML |
40 | #include <libsigrok/libsigrok.hpp> |
41 | ||
819f4c25 JH |
42 | using std::deque; |
43 | using std::max; | |
a5d93c27 | 44 | using std::make_pair; |
819f4c25 JH |
45 | using std::min; |
46 | using std::pair; | |
f9abf97e | 47 | using std::shared_ptr; |
819f4c25 | 48 | using std::vector; |
81a635d1 | 49 | |
e8d00928 ML |
50 | using sigrok::Channel; |
51 | using sigrok::ConfigKey; | |
52 | using sigrok::Device; | |
53 | using sigrok::Error; | |
54 | using sigrok::Trigger; | |
640bd149 PZ |
55 | using sigrok::TriggerStage; |
56 | using sigrok::TriggerMatch; | |
e8d00928 ML |
57 | using sigrok::TriggerMatchType; |
58 | ||
51e77110 | 59 | namespace pv { |
8d634081 | 60 | namespace view { |
51e77110 | 61 | |
a5d93c27 JH |
62 | const int LogicSignal::SignalHeight = 30; |
63 | const int LogicSignal::SignalMargin = 10; | |
64 | ||
17d94d7c | 65 | const float LogicSignal::Oversampling = 2.0f; |
131e8012 | 66 | |
64b60583 JH |
67 | const QColor LogicSignal::EdgeColour(0x80, 0x80, 0x80); |
68 | const QColor LogicSignal::HighColour(0x00, 0xC0, 0x00); | |
69 | const QColor LogicSignal::LowColour(0xC0, 0x00, 0x00); | |
131e8012 | 70 | |
299d0e75 | 71 | const QColor LogicSignal::SignalColours[10] = { |
3e46726a JH |
72 | QColor(0x16, 0x19, 0x1A), // Black |
73 | QColor(0x8F, 0x52, 0x02), // Brown | |
74 | QColor(0xCC, 0x00, 0x00), // Red | |
75 | QColor(0xF5, 0x79, 0x00), // Orange | |
76 | QColor(0xED, 0xD4, 0x00), // Yellow | |
77 | QColor(0x73, 0xD2, 0x16), // Green | |
78 | QColor(0x34, 0x65, 0xA4), // Blue | |
79 | QColor(0x75, 0x50, 0x7B), // Violet | |
80 | QColor(0x88, 0x8A, 0x85), // Grey | |
81 | QColor(0xEE, 0xEE, 0xEC), // White | |
82 | }; | |
83 | ||
b86aa8f4 JH |
84 | LogicSignal::LogicSignal( |
85 | pv::SigSession &session, | |
86 | shared_ptr<Device> device, | |
87 | shared_ptr<Channel> channel, | |
88 | shared_ptr<data::Logic> data) : | |
89 | Signal(session, channel), | |
e8d00928 | 90 | _device(device), |
4145b248 | 91 | _data(data), |
ef8311a4 JH |
92 | _trigger_none(NULL), |
93 | _trigger_rising(NULL), | |
94 | _trigger_high(NULL), | |
95 | _trigger_falling(NULL), | |
96 | _trigger_low(NULL), | |
97 | _trigger_change(NULL) | |
28a4c9c5 | 98 | { |
e8d00928 | 99 | shared_ptr<Trigger> trigger; |
b1e8c93d | 100 | |
e8d00928 | 101 | _colour = SignalColours[channel->index() % countof(SignalColours)]; |
b1e8c93d BV |
102 | |
103 | /* Populate this channel's trigger setting with whatever we | |
104 | * find in the current session trigger, if anything. */ | |
e8d00928 | 105 | _trigger_match = nullptr; |
1f4caa77 | 106 | if ((trigger = _session.session()->trigger())) |
e8d00928 ML |
107 | for (auto stage : trigger->stages()) |
108 | for (auto match : stage->matches()) | |
109 | if (match->channel() == _channel) | |
110 | _trigger_match = match->type(); | |
ef8311a4 JH |
111 | } |
112 | ||
113 | LogicSignal::~LogicSignal() | |
114 | { | |
115 | } | |
4145b248 | 116 | |
748dd753 | 117 | shared_ptr<pv::data::SignalData> LogicSignal::data() const |
9a0cd293 JH |
118 | { |
119 | return _data; | |
120 | } | |
121 | ||
748dd753 | 122 | shared_ptr<pv::data::Logic> LogicSignal::logic_data() const |
e0fc5810 JH |
123 | { |
124 | return _data; | |
125 | } | |
126 | ||
a5d93c27 JH |
127 | std::pair<int, int> LogicSignal::v_extents() const |
128 | { | |
129 | return make_pair(-SignalHeight - SignalMargin, SignalMargin); | |
130 | } | |
131 | ||
fe08b6e8 JH |
132 | void LogicSignal::paint_back(QPainter &p, int left, int right) |
133 | { | |
e8d00928 | 134 | if (_channel->enabled()) |
be9e7b4b | 135 | paint_axis(p, get_visual_y(), left, right); |
fe08b6e8 JH |
136 | } |
137 | ||
138 | void LogicSignal::paint_mid(QPainter &p, int left, int right) | |
e3f65ace | 139 | { |
2658961b JH |
140 | using pv::view::View; |
141 | ||
64b60583 | 142 | QLineF *line; |
2858b391 JH |
143 | |
144 | vector< pair<int64_t, bool> > edges; | |
145 | ||
6ac6242b | 146 | assert(_channel); |
2858b391 | 147 | assert(_data); |
2658961b | 148 | assert(right >= left); |
eae6e30a | 149 | assert(_owner); |
2858b391 | 150 | |
be9e7b4b | 151 | const int y = get_visual_y(); |
eae6e30a JH |
152 | |
153 | const View *const view = _owner->view(); | |
154 | assert(view); | |
be9e7b4b | 155 | |
eae6e30a | 156 | const double scale = view->scale(); |
01fd3263 | 157 | assert(scale > 0); |
ece37fe8 | 158 | |
eae6e30a | 159 | const double offset = view->offset(); |
01fd3263 | 160 | |
e8d00928 | 161 | if (!_channel->enabled()) |
cec48d16 JH |
162 | return; |
163 | ||
a5d93c27 | 164 | const float high_offset = y - SignalHeight + 0.5f; |
2658961b | 165 | const float low_offset = y + 0.5f; |
131e8012 | 166 | |
1b1ec774 | 167 | const deque< shared_ptr<pv::data::LogicSnapshot> > &snapshots = |
2858b391 | 168 | _data->get_snapshots(); |
333d5bbc | 169 | if (snapshots.empty()) |
2858b391 JH |
170 | return; |
171 | ||
1b1ec774 | 172 | const shared_ptr<pv::data::LogicSnapshot> &snapshot = |
8d634081 | 173 | snapshots.front(); |
2858b391 | 174 | |
9d28da5a | 175 | double samplerate = _data->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 | ||
fb0d32cb | 181 | const double pixels_offset = offset / scale; |
fb0d32cb | 182 | const double start_time = _data->get_start_time(); |
652010ea | 183 | const int64_t last_sample = snapshot->get_sample_count() - 1; |
fb0d32cb | 184 | const double samples_per_pixel = samplerate * scale; |
652010ea | 185 | const double start = samplerate * (offset - start_time); |
2658961b | 186 | const double end = start + samples_per_pixel * (right - left); |
2858b391 | 187 | |
652010ea JH |
188 | snapshot->get_subsampled_edges(edges, |
189 | min(max((int64_t)floor(start), (int64_t)0), last_sample), | |
190 | min(max((int64_t)ceil(end), (int64_t)0), last_sample), | |
e8d00928 | 191 | samples_per_pixel / Oversampling, _channel->index()); |
c352ce60 | 192 | assert(edges.size() >= 2); |
2858b391 JH |
193 | |
194 | // Paint the edges | |
64b60583 JH |
195 | const unsigned int edge_count = edges.size() - 2; |
196 | QLineF *const edge_lines = new QLineF[edge_count]; | |
197 | line = edge_lines; | |
198 | ||
f46e495e | 199 | for (auto i = edges.cbegin() + 1; i != edges.cend() - 1; i++) { |
ce6e73a8 | 200 | const float x = ((*i).first / samples_per_pixel - |
2658961b | 201 | pixels_offset) + left; |
64b60583 | 202 | *line++ = QLineF(x, high_offset, x, low_offset); |
2858b391 | 203 | } |
81a635d1 | 204 | |
64b60583 JH |
205 | p.setPen(EdgeColour); |
206 | p.drawLines(edge_lines, edge_count); | |
207 | delete[] edge_lines; | |
2858b391 JH |
208 | |
209 | // Paint the caps | |
175d6573 | 210 | const unsigned int max_cap_line_count = edges.size(); |
64b60583 | 211 | QLineF *const cap_lines = new QLineF[max_cap_line_count]; |
2858b391 | 212 | |
64b60583 JH |
213 | p.setPen(HighColour); |
214 | paint_caps(p, cap_lines, edges, true, samples_per_pixel, | |
2658961b | 215 | pixels_offset, left, high_offset); |
64b60583 JH |
216 | p.setPen(LowColour); |
217 | paint_caps(p, cap_lines, edges, false, samples_per_pixel, | |
2658961b | 218 | pixels_offset, left, low_offset); |
2858b391 | 219 | |
64b60583 | 220 | delete[] cap_lines; |
131e8012 | 221 | } |
2858b391 | 222 | |
f4270878 | 223 | void LogicSignal::paint_caps(QPainter &p, QLineF *const lines, |
131e8012 | 224 | vector< pair<int64_t, bool> > &edges, bool level, |
ce6e73a8 JH |
225 | double samples_per_pixel, double pixels_offset, float x_offset, |
226 | float y_offset) | |
131e8012 | 227 | { |
64b60583 | 228 | QLineF *line = lines; |
2858b391 | 229 | |
f46e495e | 230 | for (auto i = edges.begin(); i != (edges.end() - 1); i++) |
333d5bbc | 231 | if ((*i).second == level) { |
64b60583 | 232 | *line++ = QLineF( |
ce6e73a8 | 233 | ((*i).first / samples_per_pixel - |
64b60583 | 234 | pixels_offset) + x_offset, y_offset, |
ce6e73a8 | 235 | ((*(i+1)).first / samples_per_pixel - |
64b60583 | 236 | pixels_offset) + x_offset, y_offset); |
131e8012 JH |
237 | } |
238 | ||
64b60583 | 239 | p.drawLines(lines, line - lines); |
e3f65ace | 240 | } |
3e46726a | 241 | |
767281c8 JH |
242 | void LogicSignal::init_trigger_actions(QWidget *parent) |
243 | { | |
244 | _trigger_none = new QAction(QIcon(":/icons/trigger-none.svg"), | |
245 | tr("No trigger"), parent); | |
246 | _trigger_none->setCheckable(true); | |
b1e8c93d | 247 | connect(_trigger_none, SIGNAL(triggered()), this, SLOT(on_trigger())); |
767281c8 JH |
248 | |
249 | _trigger_rising = new QAction(QIcon(":/icons/trigger-rising.svg"), | |
250 | tr("Trigger on rising edge"), parent); | |
251 | _trigger_rising->setCheckable(true); | |
b1e8c93d | 252 | connect(_trigger_rising, SIGNAL(triggered()), this, SLOT(on_trigger())); |
767281c8 JH |
253 | |
254 | _trigger_high = new QAction(QIcon(":/icons/trigger-high.svg"), | |
255 | tr("Trigger on high level"), parent); | |
256 | _trigger_high->setCheckable(true); | |
b1e8c93d | 257 | connect(_trigger_high, SIGNAL(triggered()), this, SLOT(on_trigger())); |
767281c8 JH |
258 | |
259 | _trigger_falling = new QAction(QIcon(":/icons/trigger-falling.svg"), | |
260 | tr("Trigger on falling edge"), parent); | |
261 | _trigger_falling->setCheckable(true); | |
b1e8c93d | 262 | connect(_trigger_falling, SIGNAL(triggered()), this, SLOT(on_trigger())); |
767281c8 JH |
263 | |
264 | _trigger_low = new QAction(QIcon(":/icons/trigger-low.svg"), | |
265 | tr("Trigger on low level"), parent); | |
266 | _trigger_low->setCheckable(true); | |
b1e8c93d | 267 | connect(_trigger_low, SIGNAL(triggered()), this, SLOT(on_trigger())); |
767281c8 JH |
268 | |
269 | _trigger_change = new QAction(QIcon(":/icons/trigger-change.svg"), | |
270 | tr("Trigger on rising or falling edge"), parent); | |
271 | _trigger_change->setCheckable(true); | |
b1e8c93d | 272 | connect(_trigger_change, SIGNAL(triggered()), this, SLOT(on_trigger())); |
767281c8 JH |
273 | } |
274 | ||
e8d00928 | 275 | QAction* LogicSignal::match_action(const TriggerMatchType *type) |
767281c8 | 276 | { |
b1e8c93d BV |
277 | QAction *action; |
278 | ||
279 | action = _trigger_none; | |
640bd149 PZ |
280 | if (type) { |
281 | switch (type->id()) { | |
282 | case SR_TRIGGER_ZERO: | |
283 | action = _trigger_low; | |
284 | break; | |
285 | case SR_TRIGGER_ONE: | |
286 | action = _trigger_high; | |
287 | break; | |
288 | case SR_TRIGGER_RISING: | |
289 | action = _trigger_rising; | |
290 | break; | |
291 | case SR_TRIGGER_FALLING: | |
292 | action = _trigger_falling; | |
293 | break; | |
294 | case SR_TRIGGER_EDGE: | |
295 | action = _trigger_change; | |
296 | break; | |
297 | default: | |
298 | assert(0); | |
299 | } | |
767281c8 | 300 | } |
767281c8 | 301 | |
b1e8c93d | 302 | return action; |
767281c8 JH |
303 | } |
304 | ||
e8d00928 | 305 | const TriggerMatchType *LogicSignal::action_match(QAction *action) |
139fef92 | 306 | { |
b1e8c93d | 307 | if (action == _trigger_low) |
e8d00928 | 308 | return TriggerMatchType::ZERO; |
b1e8c93d | 309 | else if (action == _trigger_high) |
e8d00928 | 310 | return TriggerMatchType::ONE; |
b1e8c93d | 311 | else if (action == _trigger_rising) |
e8d00928 | 312 | return TriggerMatchType::RISING; |
b1e8c93d | 313 | else if (action == _trigger_falling) |
e8d00928 | 314 | return TriggerMatchType::FALLING; |
b1e8c93d | 315 | else if (action == _trigger_change) |
e8d00928 | 316 | return TriggerMatchType::EDGE; |
b1e8c93d | 317 | else |
e8d00928 | 318 | return nullptr; |
139fef92 JH |
319 | } |
320 | ||
b1e8c93d | 321 | void LogicSignal::populate_popup_form(QWidget *parent, QFormLayout *form) |
b08d7222 | 322 | { |
e8d00928 ML |
323 | Glib::VariantContainerBase gvar; |
324 | vector<int32_t> trig_types; | |
19adbc2c | 325 | |
b1e8c93d | 326 | Signal::populate_popup_form(parent, form); |
b08d7222 | 327 | |
e8d00928 ML |
328 | try { |
329 | gvar = _device->config_list(ConfigKey::TRIGGER_MATCH); | |
330 | } catch (Error e) { | |
b1e8c93d | 331 | return; |
e8d00928 | 332 | } |
b08d7222 | 333 | |
b1e8c93d BV |
334 | _trigger_bar = new QToolBar(parent); |
335 | init_trigger_actions(_trigger_bar); | |
336 | _trigger_bar->addAction(_trigger_none); | |
640bd149 | 337 | _trigger_none->setChecked(!_trigger_match); |
e8d00928 ML |
338 | trig_types = |
339 | Glib::VariantBase::cast_dynamic<Glib::Variant<vector<int32_t>>>( | |
340 | gvar).get(); | |
341 | for (auto type_id : trig_types) { | |
342 | auto type = TriggerMatchType::get(type_id); | |
343 | _trigger_bar->addAction(match_action(type)); | |
7c4b01a1 | 344 | match_action(type)->setChecked(_trigger_match == type); |
b08d7222 | 345 | } |
b1e8c93d | 346 | form->addRow(tr("Trigger"), _trigger_bar); |
139fef92 | 347 | |
b08d7222 JH |
348 | } |
349 | ||
640bd149 PZ |
350 | void LogicSignal::modify_trigger() |
351 | { | |
352 | auto trigger = _session.session()->trigger(); | |
353 | auto new_trigger = _session.device_manager().context()->create_trigger("pulseview"); | |
354 | ||
355 | if (trigger) { | |
356 | for (auto stage : trigger->stages()) { | |
357 | const auto &matches = stage->matches(); | |
358 | if (std::none_of(begin(matches), end(matches), | |
359 | [&](shared_ptr<TriggerMatch> match) { | |
360 | return match->channel() != _channel; })) | |
361 | continue; | |
362 | ||
363 | auto new_stage = new_trigger->add_stage(); | |
364 | for (auto match : stage->matches()) { | |
365 | if (match->channel() == _channel) | |
366 | continue; | |
367 | new_stage->add_match(match->channel(), match->type()); | |
368 | } | |
369 | } | |
370 | } | |
371 | ||
372 | if (_trigger_match) | |
373 | new_trigger->add_stage()->add_match(_channel, _trigger_match); | |
374 | ||
375 | _session.session()->set_trigger( | |
376 | new_trigger->stages().empty() ? nullptr : new_trigger); | |
377 | } | |
378 | ||
b1e8c93d | 379 | void LogicSignal::on_trigger() |
b08d7222 | 380 | { |
b1e8c93d | 381 | QAction *action; |
b08d7222 | 382 | |
923f8b22 | 383 | match_action(_trigger_match)->setChecked(false); |
b08d7222 | 384 | |
b1e8c93d | 385 | action = (QAction *)sender(); |
923f8b22 | 386 | action->setChecked(true); |
b1e8c93d | 387 | _trigger_match = action_match(action); |
b08d7222 | 388 | |
640bd149 | 389 | modify_trigger(); |
b08d7222 JH |
390 | } |
391 | ||
8d634081 | 392 | } // namespace view |
51e77110 | 393 | } // namespace pv |