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/device/devinst.h>
34 #include <pv/data/logic.h>
35 #include <pv/data/logicsnapshot.h>
36 #include <pv/view/view.h>
42 using std::shared_ptr;
48 const float LogicSignal::Oversampling = 2.0f;
50 const QColor LogicSignal::EdgeColour(0x80, 0x80, 0x80);
51 const QColor LogicSignal::HighColour(0x00, 0xC0, 0x00);
52 const QColor LogicSignal::LowColour(0xC0, 0x00, 0x00);
54 const QColor LogicSignal::SignalColours[10] = {
55 QColor(0x16, 0x19, 0x1A), // Black
56 QColor(0x8F, 0x52, 0x02), // Brown
57 QColor(0xCC, 0x00, 0x00), // Red
58 QColor(0xF5, 0x79, 0x00), // Orange
59 QColor(0xED, 0xD4, 0x00), // Yellow
60 QColor(0x73, 0xD2, 0x16), // Green
61 QColor(0x34, 0x65, 0xA4), // Blue
62 QColor(0x75, 0x50, 0x7B), // Violet
63 QColor(0x88, 0x8A, 0x85), // Grey
64 QColor(0xEE, 0xEE, 0xEC), // White
67 LogicSignal::LogicSignal(shared_ptr<pv::device::DevInst> dev_inst,
68 const sr_channel *const probe, shared_ptr<data::Logic> data) :
69 Signal(dev_inst, probe),
72 _trigger_rising(NULL),
74 _trigger_falling(NULL),
78 struct sr_trigger *trigger;
79 struct sr_trigger_stage *stage;
80 struct sr_trigger_match *match;
83 _colour = SignalColours[probe->index % countof(SignalColours)];
85 /* Populate this channel's trigger setting with whatever we
86 * find in the current session trigger, if anything. */
88 if ((trigger = sr_session_trigger_get(SigSession::_sr_session))) {
89 for (l = trigger->stages; l && !_trigger_match; l = l->next) {
90 stage = (struct sr_trigger_stage *)l->data;
91 for (m = stage->matches; m && !_trigger_match; m = m->next) {
92 match = (struct sr_trigger_match *)m->data;
93 if (match->channel == _probe)
94 _trigger_match = match->match;
100 LogicSignal::~LogicSignal()
104 shared_ptr<pv::data::SignalData> LogicSignal::data() const
109 shared_ptr<pv::data::Logic> LogicSignal::logic_data() const
114 void LogicSignal::paint_back(QPainter &p, int left, int right)
117 paint_axis(p, get_y(), left, right);
120 void LogicSignal::paint_mid(QPainter &p, int left, int right)
122 using pv::view::View;
126 vector< pair<int64_t, bool> > edges;
130 assert(right >= left);
133 const int y = _v_offset - _view->v_offset();
135 const double scale = _view->scale();
138 const double offset = _view->offset();
140 if (!_probe->enabled)
143 const float high_offset = y - View::SignalHeight + 0.5f;
144 const float low_offset = y + 0.5f;
146 const deque< shared_ptr<pv::data::LogicSnapshot> > &snapshots =
147 _data->get_snapshots();
148 if (snapshots.empty())
151 const shared_ptr<pv::data::LogicSnapshot> &snapshot =
154 double samplerate = _data->samplerate();
156 // Show sample rate as 1Hz when it is unknown
157 if (samplerate == 0.0)
160 const double pixels_offset = offset / scale;
161 const double start_time = _data->get_start_time();
162 const int64_t last_sample = snapshot->get_sample_count() - 1;
163 const double samples_per_pixel = samplerate * scale;
164 const double start = samplerate * (offset - start_time);
165 const double end = start + samples_per_pixel * (right - left);
167 snapshot->get_subsampled_edges(edges,
168 min(max((int64_t)floor(start), (int64_t)0), last_sample),
169 min(max((int64_t)ceil(end), (int64_t)0), last_sample),
170 samples_per_pixel / Oversampling, _probe->index);
171 assert(edges.size() >= 2);
174 const unsigned int edge_count = edges.size() - 2;
175 QLineF *const edge_lines = new QLineF[edge_count];
178 for (auto i = edges.cbegin() + 1; i != edges.cend() - 1; i++) {
179 const float x = ((*i).first / samples_per_pixel -
180 pixels_offset) + left;
181 *line++ = QLineF(x, high_offset, x, low_offset);
184 p.setPen(EdgeColour);
185 p.drawLines(edge_lines, edge_count);
189 const unsigned int max_cap_line_count = edges.size();
190 QLineF *const cap_lines = new QLineF[max_cap_line_count];
192 p.setPen(HighColour);
193 paint_caps(p, cap_lines, edges, true, samples_per_pixel,
194 pixels_offset, left, high_offset);
196 paint_caps(p, cap_lines, edges, false, samples_per_pixel,
197 pixels_offset, left, low_offset);
202 void LogicSignal::paint_caps(QPainter &p, QLineF *const lines,
203 vector< pair<int64_t, bool> > &edges, bool level,
204 double samples_per_pixel, double pixels_offset, float x_offset,
207 QLineF *line = lines;
209 for (auto i = edges.begin(); i != (edges.end() - 1); i++)
210 if ((*i).second == level) {
212 ((*i).first / samples_per_pixel -
213 pixels_offset) + x_offset, y_offset,
214 ((*(i+1)).first / samples_per_pixel -
215 pixels_offset) + x_offset, y_offset);
218 p.drawLines(lines, line - lines);
221 void LogicSignal::init_trigger_actions(QWidget *parent)
223 _trigger_none = new QAction(QIcon(":/icons/trigger-none.svg"),
224 tr("No trigger"), parent);
225 _trigger_none->setCheckable(true);
226 connect(_trigger_none, SIGNAL(triggered()), this, SLOT(on_trigger()));
228 _trigger_rising = new QAction(QIcon(":/icons/trigger-rising.svg"),
229 tr("Trigger on rising edge"), parent);
230 _trigger_rising->setCheckable(true);
231 connect(_trigger_rising, SIGNAL(triggered()), this, SLOT(on_trigger()));
233 _trigger_high = new QAction(QIcon(":/icons/trigger-high.svg"),
234 tr("Trigger on high level"), parent);
235 _trigger_high->setCheckable(true);
236 connect(_trigger_high, SIGNAL(triggered()), this, SLOT(on_trigger()));
238 _trigger_falling = new QAction(QIcon(":/icons/trigger-falling.svg"),
239 tr("Trigger on falling edge"), parent);
240 _trigger_falling->setCheckable(true);
241 connect(_trigger_falling, SIGNAL(triggered()), this, SLOT(on_trigger()));
243 _trigger_low = new QAction(QIcon(":/icons/trigger-low.svg"),
244 tr("Trigger on low level"), parent);
245 _trigger_low->setCheckable(true);
246 connect(_trigger_low, SIGNAL(triggered()), this, SLOT(on_trigger()));
248 _trigger_change = new QAction(QIcon(":/icons/trigger-change.svg"),
249 tr("Trigger on rising or falling edge"), parent);
250 _trigger_change->setCheckable(true);
251 connect(_trigger_change, SIGNAL(triggered()), this, SLOT(on_trigger()));
254 QAction* LogicSignal::match_action(int match)
258 action = _trigger_none;
260 case SR_TRIGGER_ZERO:
261 action = _trigger_low;
264 action = _trigger_high;
266 case SR_TRIGGER_RISING:
267 action = _trigger_rising;
269 case SR_TRIGGER_FALLING:
270 action = _trigger_falling;
272 case SR_TRIGGER_EDGE:
273 action = _trigger_change;
280 int LogicSignal::action_match(QAction *action)
284 if (action == _trigger_low)
285 match = SR_TRIGGER_ZERO;
286 else if (action == _trigger_high)
287 match = SR_TRIGGER_ONE;
288 else if (action == _trigger_rising)
289 match = SR_TRIGGER_RISING;
290 else if (action == _trigger_falling)
291 match = SR_TRIGGER_FALLING;
292 else if (action == _trigger_change)
293 match = SR_TRIGGER_EDGE;
300 void LogicSignal::populate_popup_form(QWidget *parent, QFormLayout *form)
304 const int32_t *trig_matches;
308 Signal::populate_popup_form(parent, form);
310 if (!(gvar = _dev_inst->list_config(NULL, SR_CONF_TRIGGER_MATCH)))
313 _trigger_bar = new QToolBar(parent);
314 init_trigger_actions(_trigger_bar);
315 _trigger_bar->addAction(_trigger_none);
316 trig_matches = (const int32_t *)g_variant_get_fixed_array(gvar,
317 &num_opts, sizeof(int32_t));
318 for (i = 0; i < num_opts; i++) {
319 _trigger_bar->addAction(match_action(trig_matches[i]));
320 is_checked = _trigger_match == trig_matches[i];
321 match_action(trig_matches[i])->setChecked(is_checked);
323 form->addRow(tr("Trigger"), _trigger_bar);
324 g_variant_unref(gvar);
328 void LogicSignal::on_trigger()
332 match_action(_trigger_match)->setChecked(FALSE);
334 action = (QAction *)sender();
335 action->setChecked(TRUE);
336 _trigger_match = action_match(action);