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