]> sigrok.org Git - pulseview.git/blame - pv/view/logicsignal.cpp
LogicSignal: Added an icon cache
[pulseview.git] / pv / view / logicsignal.cpp
CommitLineData
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
2acdb232
JH
31#include "logicsignal.hpp"
32#include "view.hpp"
33
34#include <pv/sigsession.hpp>
35#include <pv/devicemanager.hpp>
36#include <pv/data/logic.hpp>
37#include <pv/data/logicsnapshot.hpp>
38#include <pv/view/view.hpp>
28a4c9c5 39
e8d00928
ML
40#include <libsigrok/libsigrok.hpp>
41
819f4c25
JH
42using std::deque;
43using std::max;
a5d93c27 44using std::make_pair;
819f4c25
JH
45using std::min;
46using std::pair;
f9abf97e 47using std::shared_ptr;
819f4c25 48using std::vector;
81a635d1 49
e8d00928
ML
50using sigrok::Channel;
51using sigrok::ConfigKey;
52using sigrok::Device;
53using sigrok::Error;
54using sigrok::Trigger;
640bd149
PZ
55using sigrok::TriggerStage;
56using sigrok::TriggerMatch;
e8d00928
ML
57using sigrok::TriggerMatchType;
58
51e77110 59namespace pv {
8d634081 60namespace view {
51e77110 61
a5d93c27
JH
62const int LogicSignal::SignalHeight = 30;
63const int LogicSignal::SignalMargin = 10;
64
17d94d7c 65const float LogicSignal::Oversampling = 2.0f;
131e8012 66
64b60583
JH
67const QColor LogicSignal::EdgeColour(0x80, 0x80, 0x80);
68const QColor LogicSignal::HighColour(0x00, 0xC0, 0x00);
69const QColor LogicSignal::LowColour(0xC0, 0x00, 0x00);
131e8012 70
299d0e75 71const 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 84LogicSignal::LogicSignal(
2b81ae46 85 pv::Session &session,
b86aa8f4
JH
86 shared_ptr<Device> device,
87 shared_ptr<Channel> channel,
88 shared_ptr<data::Logic> data) :
89 Signal(session, channel),
8dbbc7f0
JH
90 device_(device),
91 data_(data),
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
8dbbc7f0 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. */
8dbbc7f0
JH
105 trigger_match_ = nullptr;
106 if ((trigger = session_.session()->trigger()))
e8d00928
ML
107 for (auto stage : trigger->stages())
108 for (auto match : stage->matches())
8dbbc7f0
JH
109 if (match->channel() == channel_)
110 trigger_match_ = match->type();
ef8311a4
JH
111}
112
113LogicSignal::~LogicSignal()
114{
115}
4145b248 116
748dd753 117shared_ptr<pv::data::SignalData> LogicSignal::data() const
9a0cd293 118{
8dbbc7f0 119 return data_;
9a0cd293
JH
120}
121
748dd753 122shared_ptr<pv::data::Logic> LogicSignal::logic_data() const
e0fc5810 123{
8dbbc7f0 124 return data_;
e0fc5810
JH
125}
126
a5d93c27
JH
127std::pair<int, int> LogicSignal::v_extents() const
128{
129 return make_pair(-SignalHeight - SignalMargin, SignalMargin);
130}
131
fe08b6e8
JH
132void LogicSignal::paint_back(QPainter &p, int left, int right)
133{
8dbbc7f0 134 if (channel_->enabled())
be9e7b4b 135 paint_axis(p, get_visual_y(), left, right);
fe08b6e8
JH
136}
137
138void 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
8dbbc7f0
JH
146 assert(channel_);
147 assert(data_);
2658961b 148 assert(right >= left);
8dbbc7f0 149 assert(owner_);
2858b391 150
be9e7b4b 151 const int y = get_visual_y();
eae6e30a 152
8dbbc7f0 153 const View *const view = owner_->view();
eae6e30a 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
8dbbc7f0 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 =
8dbbc7f0 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
8dbbc7f0 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;
8dbbc7f0 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),
8dbbc7f0 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 223void 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
242void LogicSignal::init_trigger_actions(QWidget *parent)
243{
0f1f98fe 244 trigger_none_ = new QAction(*get_icon(":/icons/trigger-none.svg"),
767281c8 245 tr("No trigger"), parent);
8dbbc7f0
JH
246 trigger_none_->setCheckable(true);
247 connect(trigger_none_, SIGNAL(triggered()), this, SLOT(on_trigger()));
767281c8 248
0f1f98fe 249 trigger_rising_ = new QAction(*get_icon(":/icons/trigger-rising.svg"),
767281c8 250 tr("Trigger on rising edge"), parent);
8dbbc7f0
JH
251 trigger_rising_->setCheckable(true);
252 connect(trigger_rising_, SIGNAL(triggered()), this, SLOT(on_trigger()));
767281c8 253
0f1f98fe 254 trigger_high_ = new QAction(*get_icon(":/icons/trigger-high.svg"),
767281c8 255 tr("Trigger on high level"), parent);
8dbbc7f0
JH
256 trigger_high_->setCheckable(true);
257 connect(trigger_high_, SIGNAL(triggered()), this, SLOT(on_trigger()));
767281c8 258
0f1f98fe 259 trigger_falling_ = new QAction(*get_icon(":/icons/trigger-falling.svg"),
767281c8 260 tr("Trigger on falling edge"), parent);
8dbbc7f0
JH
261 trigger_falling_->setCheckable(true);
262 connect(trigger_falling_, SIGNAL(triggered()), this, SLOT(on_trigger()));
767281c8 263
0f1f98fe 264 trigger_low_ = new QAction(*get_icon(":/icons/trigger-low.svg"),
767281c8 265 tr("Trigger on low level"), parent);
8dbbc7f0
JH
266 trigger_low_->setCheckable(true);
267 connect(trigger_low_, SIGNAL(triggered()), this, SLOT(on_trigger()));
767281c8 268
0f1f98fe 269 trigger_change_ = new QAction(*get_icon(":/icons/trigger-change.svg"),
767281c8 270 tr("Trigger on rising or falling edge"), parent);
8dbbc7f0
JH
271 trigger_change_->setCheckable(true);
272 connect(trigger_change_, SIGNAL(triggered()), this, SLOT(on_trigger()));
767281c8
JH
273}
274
77817b43
JH
275const vector<int32_t> LogicSignal::get_trigger_types() const
276{
277 try {
278 const Glib::VariantContainerBase gvar =
279 device_->config_list(ConfigKey::TRIGGER_MATCH);
280 return Glib::VariantBase::cast_dynamic<
281 Glib::Variant<vector<int32_t>>>(gvar).get();
282 } catch (Error e) {
283 return vector<int32_t>();
284 }
285}
286
aa5e9140 287QAction* LogicSignal::action_from_trigger_type(const TriggerMatchType *type)
767281c8 288{
b1e8c93d
BV
289 QAction *action;
290
8dbbc7f0 291 action = trigger_none_;
640bd149
PZ
292 if (type) {
293 switch (type->id()) {
294 case SR_TRIGGER_ZERO:
8dbbc7f0 295 action = trigger_low_;
640bd149
PZ
296 break;
297 case SR_TRIGGER_ONE:
8dbbc7f0 298 action = trigger_high_;
640bd149
PZ
299 break;
300 case SR_TRIGGER_RISING:
8dbbc7f0 301 action = trigger_rising_;
640bd149
PZ
302 break;
303 case SR_TRIGGER_FALLING:
8dbbc7f0 304 action = trigger_falling_;
640bd149
PZ
305 break;
306 case SR_TRIGGER_EDGE:
8dbbc7f0 307 action = trigger_change_;
640bd149
PZ
308 break;
309 default:
310 assert(0);
311 }
767281c8 312 }
767281c8 313
b1e8c93d 314 return action;
767281c8
JH
315}
316
aa5e9140 317const TriggerMatchType *LogicSignal::trigger_type_from_action(QAction *action)
139fef92 318{
8dbbc7f0 319 if (action == trigger_low_)
e8d00928 320 return TriggerMatchType::ZERO;
8dbbc7f0 321 else if (action == trigger_high_)
e8d00928 322 return TriggerMatchType::ONE;
8dbbc7f0 323 else if (action == trigger_rising_)
e8d00928 324 return TriggerMatchType::RISING;
8dbbc7f0 325 else if (action == trigger_falling_)
e8d00928 326 return TriggerMatchType::FALLING;
8dbbc7f0 327 else if (action == trigger_change_)
e8d00928 328 return TriggerMatchType::EDGE;
b1e8c93d 329 else
e8d00928 330 return nullptr;
139fef92
JH
331}
332
b1e8c93d 333void LogicSignal::populate_popup_form(QWidget *parent, QFormLayout *form)
b08d7222 334{
b1e8c93d 335 Signal::populate_popup_form(parent, form);
b08d7222 336
8dbbc7f0
JH
337 trigger_bar_ = new QToolBar(parent);
338 init_trigger_actions(trigger_bar_);
339 trigger_bar_->addAction(trigger_none_);
340 trigger_none_->setChecked(!trigger_match_);
77817b43
JH
341
342 const vector<int32_t> trig_types = get_trigger_types();
e8d00928 343 for (auto type_id : trig_types) {
388cdd24
JH
344 const TriggerMatchType *const type =
345 TriggerMatchType::get(type_id);
aa5e9140 346 QAction *const action = action_from_trigger_type(type);
388cdd24
JH
347 trigger_bar_->addAction(action);
348 action->setChecked(trigger_match_ == type);
b08d7222 349 }
8dbbc7f0 350 form->addRow(tr("Trigger"), trigger_bar_);
139fef92 351
b08d7222
JH
352}
353
640bd149
PZ
354void LogicSignal::modify_trigger()
355{
8dbbc7f0
JH
356 auto trigger = session_.session()->trigger();
357 auto new_trigger = session_.device_manager().context()->create_trigger("pulseview");
640bd149
PZ
358
359 if (trigger) {
360 for (auto stage : trigger->stages()) {
361 const auto &matches = stage->matches();
362 if (std::none_of(begin(matches), end(matches),
363 [&](shared_ptr<TriggerMatch> match) {
8dbbc7f0 364 return match->channel() != channel_; }))
640bd149
PZ
365 continue;
366
367 auto new_stage = new_trigger->add_stage();
368 for (auto match : stage->matches()) {
8dbbc7f0 369 if (match->channel() == channel_)
640bd149
PZ
370 continue;
371 new_stage->add_match(match->channel(), match->type());
372 }
373 }
374 }
375
8dbbc7f0
JH
376 if (trigger_match_)
377 new_trigger->add_stage()->add_match(channel_, trigger_match_);
640bd149 378
8dbbc7f0 379 session_.session()->set_trigger(
640bd149
PZ
380 new_trigger->stages().empty() ? nullptr : new_trigger);
381}
382
0f1f98fe
JH
383const QIcon* LogicSignal::get_icon(const char *path)
384{
385 const QIcon *icon = icon_cache_.take(path);
386 if (!icon) {
387 icon = new QIcon(path);
388 icon_cache_.insert(path, icon);
389 }
390
391 return icon;
392}
393
b1e8c93d 394void LogicSignal::on_trigger()
b08d7222 395{
b1e8c93d 396 QAction *action;
b08d7222 397
aa5e9140 398 action_from_trigger_type(trigger_match_)->setChecked(false);
b08d7222 399
b1e8c93d 400 action = (QAction *)sender();
923f8b22 401 action->setChecked(true);
aa5e9140 402 trigger_match_ = trigger_type_from_action(action);
b08d7222 403
640bd149 404 modify_trigger();
b08d7222
JH
405}
406
8d634081 407} // namespace view
51e77110 408} // namespace pv