2 * This file is part of the PulseView project.
4 * Copyright (C) 2012 Joel Holdsworth <joel@airwebreathe.org.uk>
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.
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.
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
26 #include <QFormLayout>
29 #include "logicsignal.h"
32 #include <pv/sigsession.h>
33 #include <pv/data/logic.h>
34 #include <pv/data/logicsnapshot.h>
35 #include <pv/view/view.h>
37 #include <libsigrok/libsigrok.hpp>
43 using std::shared_ptr;
46 using sigrok::Channel;
47 using sigrok::ConfigKey;
50 using sigrok::Trigger;
51 using sigrok::TriggerMatchType;
56 const float LogicSignal::Oversampling = 2.0f;
58 const QColor LogicSignal::EdgeColour(0x80, 0x80, 0x80);
59 const QColor LogicSignal::HighColour(0x00, 0xC0, 0x00);
60 const QColor LogicSignal::LowColour(0xC0, 0x00, 0x00);
62 const QColor LogicSignal::SignalColours[10] = {
63 QColor(0x16, 0x19, 0x1A), // Black
64 QColor(0x8F, 0x52, 0x02), // Brown
65 QColor(0xCC, 0x00, 0x00), // Red
66 QColor(0xF5, 0x79, 0x00), // Orange
67 QColor(0xED, 0xD4, 0x00), // Yellow
68 QColor(0x73, 0xD2, 0x16), // Green
69 QColor(0x34, 0x65, 0xA4), // Blue
70 QColor(0x75, 0x50, 0x7B), // Violet
71 QColor(0x88, 0x8A, 0x85), // Grey
72 QColor(0xEE, 0xEE, 0xEC), // White
75 LogicSignal::LogicSignal(shared_ptr<Device> device,
76 shared_ptr<Channel> channel,
77 shared_ptr<data::Logic> data) :
82 _trigger_rising(NULL),
84 _trigger_falling(NULL),
88 shared_ptr<Trigger> trigger;
90 _colour = SignalColours[channel->index() % countof(SignalColours)];
92 /* Populate this channel's trigger setting with whatever we
93 * find in the current session trigger, if anything. */
94 _trigger_match = nullptr;
95 if ((trigger = SigSession::_sr_session->trigger()))
96 for (auto stage : trigger->stages())
97 for (auto match : stage->matches())
98 if (match->channel() == _channel)
99 _trigger_match = match->type();
102 LogicSignal::~LogicSignal()
106 shared_ptr<pv::data::SignalData> LogicSignal::data() const
111 shared_ptr<pv::data::Logic> LogicSignal::logic_data() const
116 void LogicSignal::paint_back(QPainter &p, int left, int right)
118 if (_channel->enabled())
119 paint_axis(p, get_y(), left, right);
122 void LogicSignal::paint_mid(QPainter &p, int left, int right)
124 using pv::view::View;
128 vector< pair<int64_t, bool> > edges;
132 assert(right >= left);
135 const int y = _v_offset - _view->v_offset();
137 const double scale = _view->scale();
140 const double offset = _view->offset();
142 if (!_channel->enabled())
145 const float high_offset = y - View::SignalHeight + 0.5f;
146 const float low_offset = y + 0.5f;
148 const deque< shared_ptr<pv::data::LogicSnapshot> > &snapshots =
149 _data->get_snapshots();
150 if (snapshots.empty())
153 const shared_ptr<pv::data::LogicSnapshot> &snapshot =
156 double samplerate = _data->samplerate();
158 // Show sample rate as 1Hz when it is unknown
159 if (samplerate == 0.0)
162 const double pixels_offset = offset / scale;
163 const double start_time = _data->get_start_time();
164 const int64_t last_sample = snapshot->get_sample_count() - 1;
165 const double samples_per_pixel = samplerate * scale;
166 const double start = samplerate * (offset - start_time);
167 const double end = start + samples_per_pixel * (right - left);
169 snapshot->get_subsampled_edges(edges,
170 min(max((int64_t)floor(start), (int64_t)0), last_sample),
171 min(max((int64_t)ceil(end), (int64_t)0), last_sample),
172 samples_per_pixel / Oversampling, _channel->index());
173 assert(edges.size() >= 2);
176 const unsigned int edge_count = edges.size() - 2;
177 QLineF *const edge_lines = new QLineF[edge_count];
180 for (auto i = edges.cbegin() + 1; i != edges.cend() - 1; i++) {
181 const float x = ((*i).first / samples_per_pixel -
182 pixels_offset) + left;
183 *line++ = QLineF(x, high_offset, x, low_offset);
186 p.setPen(EdgeColour);
187 p.drawLines(edge_lines, edge_count);
191 const unsigned int max_cap_line_count = edges.size();
192 QLineF *const cap_lines = new QLineF[max_cap_line_count];
194 p.setPen(HighColour);
195 paint_caps(p, cap_lines, edges, true, samples_per_pixel,
196 pixels_offset, left, high_offset);
198 paint_caps(p, cap_lines, edges, false, samples_per_pixel,
199 pixels_offset, left, low_offset);
204 void LogicSignal::paint_caps(QPainter &p, QLineF *const lines,
205 vector< pair<int64_t, bool> > &edges, bool level,
206 double samples_per_pixel, double pixels_offset, float x_offset,
209 QLineF *line = lines;
211 for (auto i = edges.begin(); i != (edges.end() - 1); i++)
212 if ((*i).second == level) {
214 ((*i).first / samples_per_pixel -
215 pixels_offset) + x_offset, y_offset,
216 ((*(i+1)).first / samples_per_pixel -
217 pixels_offset) + x_offset, y_offset);
220 p.drawLines(lines, line - lines);
223 void LogicSignal::init_trigger_actions(QWidget *parent)
225 _trigger_none = new QAction(QIcon(":/icons/trigger-none.svg"),
226 tr("No trigger"), parent);
227 _trigger_none->setCheckable(true);
228 connect(_trigger_none, SIGNAL(triggered()), this, SLOT(on_trigger()));
230 _trigger_rising = new QAction(QIcon(":/icons/trigger-rising.svg"),
231 tr("Trigger on rising edge"), parent);
232 _trigger_rising->setCheckable(true);
233 connect(_trigger_rising, SIGNAL(triggered()), this, SLOT(on_trigger()));
235 _trigger_high = new QAction(QIcon(":/icons/trigger-high.svg"),
236 tr("Trigger on high level"), parent);
237 _trigger_high->setCheckable(true);
238 connect(_trigger_high, SIGNAL(triggered()), this, SLOT(on_trigger()));
240 _trigger_falling = new QAction(QIcon(":/icons/trigger-falling.svg"),
241 tr("Trigger on falling edge"), parent);
242 _trigger_falling->setCheckable(true);
243 connect(_trigger_falling, SIGNAL(triggered()), this, SLOT(on_trigger()));
245 _trigger_low = new QAction(QIcon(":/icons/trigger-low.svg"),
246 tr("Trigger on low level"), parent);
247 _trigger_low->setCheckable(true);
248 connect(_trigger_low, SIGNAL(triggered()), this, SLOT(on_trigger()));
250 _trigger_change = new QAction(QIcon(":/icons/trigger-change.svg"),
251 tr("Trigger on rising or falling edge"), parent);
252 _trigger_change->setCheckable(true);
253 connect(_trigger_change, SIGNAL(triggered()), this, SLOT(on_trigger()));
256 QAction* LogicSignal::match_action(const TriggerMatchType *type)
260 action = _trigger_none;
261 switch (type->id()) {
262 case SR_TRIGGER_ZERO:
263 action = _trigger_low;
266 action = _trigger_high;
268 case SR_TRIGGER_RISING:
269 action = _trigger_rising;
271 case SR_TRIGGER_FALLING:
272 action = _trigger_falling;
274 case SR_TRIGGER_EDGE:
275 action = _trigger_change;
284 const TriggerMatchType *LogicSignal::action_match(QAction *action)
286 if (action == _trigger_low)
287 return TriggerMatchType::ZERO;
288 else if (action == _trigger_high)
289 return TriggerMatchType::ONE;
290 else if (action == _trigger_rising)
291 return TriggerMatchType::RISING;
292 else if (action == _trigger_falling)
293 return TriggerMatchType::FALLING;
294 else if (action == _trigger_change)
295 return TriggerMatchType::EDGE;
300 void LogicSignal::populate_popup_form(QWidget *parent, QFormLayout *form)
302 Glib::VariantContainerBase gvar;
303 vector<int32_t> trig_types;
306 Signal::populate_popup_form(parent, form);
309 gvar = _device->config_list(ConfigKey::TRIGGER_MATCH);
314 _trigger_bar = new QToolBar(parent);
315 init_trigger_actions(_trigger_bar);
316 _trigger_bar->addAction(_trigger_none);
318 Glib::VariantBase::cast_dynamic<Glib::Variant<vector<int32_t>>>(
320 for (auto type_id : trig_types) {
321 auto type = TriggerMatchType::get(type_id);
322 _trigger_bar->addAction(match_action(type));
323 is_checked = _trigger_match == type;
324 match_action(type)->setChecked(is_checked);
326 form->addRow(tr("Trigger"), _trigger_bar);
330 void LogicSignal::on_trigger()
334 match_action(_trigger_match)->setChecked(FALSE);
336 action = (QAction *)sender();
337 action->setChecked(TRUE);
338 _trigger_match = action_match(action);