]> sigrok.org Git - pulseview.git/blame_incremental - pv/view/logicsignal.cpp
LogicSignal: Renamed action_match and match_action
[pulseview.git] / pv / view / logicsignal.cpp
... / ...
CommitLineData
1/*
2 * This file is part of the PulseView project.
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
21#include <extdef.h>
22
23#include <cassert>
24#include <cmath>
25
26#include <algorithm>
27
28#include <QFormLayout>
29#include <QToolBar>
30
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>
39
40#include <libsigrok/libsigrok.hpp>
41
42using std::deque;
43using std::max;
44using std::make_pair;
45using std::min;
46using std::pair;
47using std::shared_ptr;
48using std::vector;
49
50using sigrok::Channel;
51using sigrok::ConfigKey;
52using sigrok::Device;
53using sigrok::Error;
54using sigrok::Trigger;
55using sigrok::TriggerStage;
56using sigrok::TriggerMatch;
57using sigrok::TriggerMatchType;
58
59namespace pv {
60namespace view {
61
62const int LogicSignal::SignalHeight = 30;
63const int LogicSignal::SignalMargin = 10;
64
65const float LogicSignal::Oversampling = 2.0f;
66
67const QColor LogicSignal::EdgeColour(0x80, 0x80, 0x80);
68const QColor LogicSignal::HighColour(0x00, 0xC0, 0x00);
69const QColor LogicSignal::LowColour(0xC0, 0x00, 0x00);
70
71const QColor LogicSignal::SignalColours[10] = {
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
84LogicSignal::LogicSignal(
85 pv::Session &session,
86 shared_ptr<Device> device,
87 shared_ptr<Channel> channel,
88 shared_ptr<data::Logic> data) :
89 Signal(session, channel),
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)
98{
99 shared_ptr<Trigger> trigger;
100
101 colour_ = SignalColours[channel->index() % countof(SignalColours)];
102
103 /* Populate this channel's trigger setting with whatever we
104 * find in the current session trigger, if anything. */
105 trigger_match_ = nullptr;
106 if ((trigger = session_.session()->trigger()))
107 for (auto stage : trigger->stages())
108 for (auto match : stage->matches())
109 if (match->channel() == channel_)
110 trigger_match_ = match->type();
111}
112
113LogicSignal::~LogicSignal()
114{
115}
116
117shared_ptr<pv::data::SignalData> LogicSignal::data() const
118{
119 return data_;
120}
121
122shared_ptr<pv::data::Logic> LogicSignal::logic_data() const
123{
124 return data_;
125}
126
127std::pair<int, int> LogicSignal::v_extents() const
128{
129 return make_pair(-SignalHeight - SignalMargin, SignalMargin);
130}
131
132void LogicSignal::paint_back(QPainter &p, int left, int right)
133{
134 if (channel_->enabled())
135 paint_axis(p, get_visual_y(), left, right);
136}
137
138void LogicSignal::paint_mid(QPainter &p, int left, int right)
139{
140 using pv::view::View;
141
142 QLineF *line;
143
144 vector< pair<int64_t, bool> > edges;
145
146 assert(channel_);
147 assert(data_);
148 assert(right >= left);
149 assert(owner_);
150
151 const int y = get_visual_y();
152
153 const View *const view = owner_->view();
154 assert(view);
155
156 const double scale = view->scale();
157 assert(scale > 0);
158
159 const double offset = view->offset();
160
161 if (!channel_->enabled())
162 return;
163
164 const float high_offset = y - SignalHeight + 0.5f;
165 const float low_offset = y + 0.5f;
166
167 const deque< shared_ptr<pv::data::LogicSnapshot> > &snapshots =
168 data_->get_snapshots();
169 if (snapshots.empty())
170 return;
171
172 const shared_ptr<pv::data::LogicSnapshot> &snapshot =
173 snapshots.front();
174
175 double samplerate = data_->samplerate();
176
177 // Show sample rate as 1Hz when it is unknown
178 if (samplerate == 0.0)
179 samplerate = 1.0;
180
181 const double pixels_offset = offset / scale;
182 const double start_time = data_->get_start_time();
183 const int64_t last_sample = snapshot->get_sample_count() - 1;
184 const double samples_per_pixel = samplerate * scale;
185 const double start = samplerate * (offset - start_time);
186 const double end = start + samples_per_pixel * (right - left);
187
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),
191 samples_per_pixel / Oversampling, channel_->index());
192 assert(edges.size() >= 2);
193
194 // Paint the edges
195 const unsigned int edge_count = edges.size() - 2;
196 QLineF *const edge_lines = new QLineF[edge_count];
197 line = edge_lines;
198
199 for (auto i = edges.cbegin() + 1; i != edges.cend() - 1; i++) {
200 const float x = ((*i).first / samples_per_pixel -
201 pixels_offset) + left;
202 *line++ = QLineF(x, high_offset, x, low_offset);
203 }
204
205 p.setPen(EdgeColour);
206 p.drawLines(edge_lines, edge_count);
207 delete[] edge_lines;
208
209 // Paint the caps
210 const unsigned int max_cap_line_count = edges.size();
211 QLineF *const cap_lines = new QLineF[max_cap_line_count];
212
213 p.setPen(HighColour);
214 paint_caps(p, cap_lines, edges, true, samples_per_pixel,
215 pixels_offset, left, high_offset);
216 p.setPen(LowColour);
217 paint_caps(p, cap_lines, edges, false, samples_per_pixel,
218 pixels_offset, left, low_offset);
219
220 delete[] cap_lines;
221}
222
223void LogicSignal::paint_caps(QPainter &p, QLineF *const lines,
224 vector< pair<int64_t, bool> > &edges, bool level,
225 double samples_per_pixel, double pixels_offset, float x_offset,
226 float y_offset)
227{
228 QLineF *line = lines;
229
230 for (auto i = edges.begin(); i != (edges.end() - 1); i++)
231 if ((*i).second == level) {
232 *line++ = QLineF(
233 ((*i).first / samples_per_pixel -
234 pixels_offset) + x_offset, y_offset,
235 ((*(i+1)).first / samples_per_pixel -
236 pixels_offset) + x_offset, y_offset);
237 }
238
239 p.drawLines(lines, line - lines);
240}
241
242void 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);
247 connect(trigger_none_, SIGNAL(triggered()), this, SLOT(on_trigger()));
248
249 trigger_rising_ = new QAction(QIcon(":/icons/trigger-rising.svg"),
250 tr("Trigger on rising edge"), parent);
251 trigger_rising_->setCheckable(true);
252 connect(trigger_rising_, SIGNAL(triggered()), this, SLOT(on_trigger()));
253
254 trigger_high_ = new QAction(QIcon(":/icons/trigger-high.svg"),
255 tr("Trigger on high level"), parent);
256 trigger_high_->setCheckable(true);
257 connect(trigger_high_, SIGNAL(triggered()), this, SLOT(on_trigger()));
258
259 trigger_falling_ = new QAction(QIcon(":/icons/trigger-falling.svg"),
260 tr("Trigger on falling edge"), parent);
261 trigger_falling_->setCheckable(true);
262 connect(trigger_falling_, SIGNAL(triggered()), this, SLOT(on_trigger()));
263
264 trigger_low_ = new QAction(QIcon(":/icons/trigger-low.svg"),
265 tr("Trigger on low level"), parent);
266 trigger_low_->setCheckable(true);
267 connect(trigger_low_, SIGNAL(triggered()), this, SLOT(on_trigger()));
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);
272 connect(trigger_change_, SIGNAL(triggered()), this, SLOT(on_trigger()));
273}
274
275QAction* LogicSignal::action_from_trigger_type(const TriggerMatchType *type)
276{
277 QAction *action;
278
279 action = trigger_none_;
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 }
300 }
301
302 return action;
303}
304
305const TriggerMatchType *LogicSignal::trigger_type_from_action(QAction *action)
306{
307 if (action == trigger_low_)
308 return TriggerMatchType::ZERO;
309 else if (action == trigger_high_)
310 return TriggerMatchType::ONE;
311 else if (action == trigger_rising_)
312 return TriggerMatchType::RISING;
313 else if (action == trigger_falling_)
314 return TriggerMatchType::FALLING;
315 else if (action == trigger_change_)
316 return TriggerMatchType::EDGE;
317 else
318 return nullptr;
319}
320
321void LogicSignal::populate_popup_form(QWidget *parent, QFormLayout *form)
322{
323 Glib::VariantContainerBase gvar;
324 vector<int32_t> trig_types;
325
326 Signal::populate_popup_form(parent, form);
327
328 try {
329 gvar = device_->config_list(ConfigKey::TRIGGER_MATCH);
330 } catch (Error e) {
331 return;
332 }
333
334 trigger_bar_ = new QToolBar(parent);
335 init_trigger_actions(trigger_bar_);
336 trigger_bar_->addAction(trigger_none_);
337 trigger_none_->setChecked(!trigger_match_);
338 trig_types =
339 Glib::VariantBase::cast_dynamic<Glib::Variant<vector<int32_t>>>(
340 gvar).get();
341 for (auto type_id : trig_types) {
342 const TriggerMatchType *const type =
343 TriggerMatchType::get(type_id);
344 QAction *const action = action_from_trigger_type(type);
345 trigger_bar_->addAction(action);
346 action->setChecked(trigger_match_ == type);
347 }
348 form->addRow(tr("Trigger"), trigger_bar_);
349
350}
351
352void LogicSignal::modify_trigger()
353{
354 auto trigger = session_.session()->trigger();
355 auto new_trigger = session_.device_manager().context()->create_trigger("pulseview");
356
357 if (trigger) {
358 for (auto stage : trigger->stages()) {
359 const auto &matches = stage->matches();
360 if (std::none_of(begin(matches), end(matches),
361 [&](shared_ptr<TriggerMatch> match) {
362 return match->channel() != channel_; }))
363 continue;
364
365 auto new_stage = new_trigger->add_stage();
366 for (auto match : stage->matches()) {
367 if (match->channel() == channel_)
368 continue;
369 new_stage->add_match(match->channel(), match->type());
370 }
371 }
372 }
373
374 if (trigger_match_)
375 new_trigger->add_stage()->add_match(channel_, trigger_match_);
376
377 session_.session()->set_trigger(
378 new_trigger->stages().empty() ? nullptr : new_trigger);
379}
380
381void LogicSignal::on_trigger()
382{
383 QAction *action;
384
385 action_from_trigger_type(trigger_match_)->setChecked(false);
386
387 action = (QAction *)sender();
388 action->setChecked(true);
389 trigger_match_ = trigger_type_from_action(action);
390
391 modify_trigger();
392}
393
394} // namespace view
395} // namespace pv