]> sigrok.org Git - pulseview.git/blame - pv/view/logicsignal.cpp
Update to new session API.
[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
b213ef09
JH
26#include <QFormLayout>
27#include <QToolBar>
28
28a4c9c5 29#include "logicsignal.h"
2658961b 30#include "view.h"
0a78bcaf 31
19adbc2c 32#include <pv/sigsession.h>
94574501 33#include <pv/device/devinst.h>
19adbc2c
JH
34#include <pv/data/logic.h>
35#include <pv/data/logicsnapshot.h>
36#include <pv/view/view.h>
28a4c9c5 37
819f4c25
JH
38using std::deque;
39using std::max;
40using std::min;
41using std::pair;
f9abf97e 42using std::shared_ptr;
819f4c25 43using std::vector;
81a635d1 44
51e77110 45namespace pv {
8d634081 46namespace view {
51e77110 47
17d94d7c 48const float LogicSignal::Oversampling = 2.0f;
131e8012 49
64b60583
JH
50const QColor LogicSignal::EdgeColour(0x80, 0x80, 0x80);
51const QColor LogicSignal::HighColour(0x00, 0xC0, 0x00);
52const QColor LogicSignal::LowColour(0xC0, 0x00, 0x00);
131e8012 53
299d0e75 54const QColor LogicSignal::SignalColours[10] = {
3e46726a
JH
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
65};
66
94574501 67LogicSignal::LogicSignal(shared_ptr<pv::device::DevInst> dev_inst,
4871ed92 68 const sr_channel *const probe, shared_ptr<data::Logic> data) :
83c23cc9 69 Signal(dev_inst, probe),
4145b248 70 _data(data),
ef8311a4
JH
71 _trigger_none(NULL),
72 _trigger_rising(NULL),
73 _trigger_high(NULL),
74 _trigger_falling(NULL),
75 _trigger_low(NULL),
76 _trigger_change(NULL)
28a4c9c5 77{
b1e8c93d
BV
78 struct sr_trigger *trigger;
79 struct sr_trigger_stage *stage;
80 struct sr_trigger_match *match;
81 const GSList *l, *m;
82
cec48d16 83 _colour = SignalColours[probe->index % countof(SignalColours)];
b1e8c93d
BV
84
85 /* Populate this channel's trigger setting with whatever we
86 * find in the current session trigger, if anything. */
87 _trigger_match = 0;
bb3030b3 88 if ((trigger = sr_session_trigger_get(SigSession::_sr_session))) {
b1e8c93d
BV
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;
95 }
96 }
97 }
ef8311a4
JH
98}
99
100LogicSignal::~LogicSignal()
101{
102}
4145b248 103
748dd753 104shared_ptr<pv::data::SignalData> LogicSignal::data() const
9a0cd293
JH
105{
106 return _data;
107}
108
748dd753 109shared_ptr<pv::data::Logic> LogicSignal::logic_data() const
e0fc5810
JH
110{
111 return _data;
112}
113
fe08b6e8
JH
114void LogicSignal::paint_back(QPainter &p, int left, int right)
115{
116 if (_probe->enabled)
117 paint_axis(p, get_y(), left, right);
118}
119
120void LogicSignal::paint_mid(QPainter &p, int left, int right)
e3f65ace 121{
2658961b
JH
122 using pv::view::View;
123
64b60583 124 QLineF *line;
2858b391
JH
125
126 vector< pair<int64_t, bool> > edges;
127
cec48d16 128 assert(_probe);
2858b391 129 assert(_data);
2658961b 130 assert(right >= left);
2858b391 131
01fd3263
JH
132 assert(_view);
133 const int y = _v_offset - _view->v_offset();
134
135 const double scale = _view->scale();
136 assert(scale > 0);
137
138 const double offset = _view->offset();
139
cec48d16
JH
140 if (!_probe->enabled)
141 return;
142
2658961b
JH
143 const float high_offset = y - View::SignalHeight + 0.5f;
144 const float low_offset = y + 0.5f;
131e8012 145
1b1ec774 146 const deque< shared_ptr<pv::data::LogicSnapshot> > &snapshots =
2858b391 147 _data->get_snapshots();
333d5bbc 148 if (snapshots.empty())
2858b391
JH
149 return;
150
1b1ec774 151 const shared_ptr<pv::data::LogicSnapshot> &snapshot =
8d634081 152 snapshots.front();
2858b391 153
9d28da5a 154 double samplerate = _data->samplerate();
9d4e5cd8
JH
155
156 // Show sample rate as 1Hz when it is unknown
9ba4ca35 157 if (samplerate == 0.0)
9d4e5cd8
JH
158 samplerate = 1.0;
159
fb0d32cb 160 const double pixels_offset = offset / scale;
fb0d32cb 161 const double start_time = _data->get_start_time();
652010ea 162 const int64_t last_sample = snapshot->get_sample_count() - 1;
fb0d32cb 163 const double samples_per_pixel = samplerate * scale;
652010ea 164 const double start = samplerate * (offset - start_time);
2658961b 165 const double end = start + samples_per_pixel * (right - left);
2858b391 166
652010ea
JH
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),
cec48d16 170 samples_per_pixel / Oversampling, _probe->index);
c352ce60 171 assert(edges.size() >= 2);
2858b391
JH
172
173 // Paint the edges
64b60583
JH
174 const unsigned int edge_count = edges.size() - 2;
175 QLineF *const edge_lines = new QLineF[edge_count];
176 line = edge_lines;
177
f46e495e 178 for (auto i = edges.cbegin() + 1; i != edges.cend() - 1; i++) {
ce6e73a8 179 const float x = ((*i).first / samples_per_pixel -
2658961b 180 pixels_offset) + left;
64b60583 181 *line++ = QLineF(x, high_offset, x, low_offset);
2858b391 182 }
81a635d1 183
64b60583
JH
184 p.setPen(EdgeColour);
185 p.drawLines(edge_lines, edge_count);
186 delete[] edge_lines;
2858b391
JH
187
188 // Paint the caps
175d6573 189 const unsigned int max_cap_line_count = edges.size();
64b60583 190 QLineF *const cap_lines = new QLineF[max_cap_line_count];
2858b391 191
64b60583
JH
192 p.setPen(HighColour);
193 paint_caps(p, cap_lines, edges, true, samples_per_pixel,
2658961b 194 pixels_offset, left, high_offset);
64b60583
JH
195 p.setPen(LowColour);
196 paint_caps(p, cap_lines, edges, false, samples_per_pixel,
2658961b 197 pixels_offset, left, low_offset);
2858b391 198
64b60583 199 delete[] cap_lines;
131e8012 200}
2858b391 201
f4270878 202void LogicSignal::paint_caps(QPainter &p, QLineF *const lines,
131e8012 203 vector< pair<int64_t, bool> > &edges, bool level,
ce6e73a8
JH
204 double samples_per_pixel, double pixels_offset, float x_offset,
205 float y_offset)
131e8012 206{
64b60583 207 QLineF *line = lines;
2858b391 208
f46e495e 209 for (auto i = edges.begin(); i != (edges.end() - 1); i++)
333d5bbc 210 if ((*i).second == level) {
64b60583 211 *line++ = QLineF(
ce6e73a8 212 ((*i).first / samples_per_pixel -
64b60583 213 pixels_offset) + x_offset, y_offset,
ce6e73a8 214 ((*(i+1)).first / samples_per_pixel -
64b60583 215 pixels_offset) + x_offset, y_offset);
131e8012
JH
216 }
217
64b60583 218 p.drawLines(lines, line - lines);
e3f65ace 219}
3e46726a 220
767281c8
JH
221void LogicSignal::init_trigger_actions(QWidget *parent)
222{
223 _trigger_none = new QAction(QIcon(":/icons/trigger-none.svg"),
224 tr("No trigger"), parent);
225 _trigger_none->setCheckable(true);
b1e8c93d 226 connect(_trigger_none, SIGNAL(triggered()), this, SLOT(on_trigger()));
767281c8
JH
227
228 _trigger_rising = new QAction(QIcon(":/icons/trigger-rising.svg"),
229 tr("Trigger on rising edge"), parent);
230 _trigger_rising->setCheckable(true);
b1e8c93d 231 connect(_trigger_rising, SIGNAL(triggered()), this, SLOT(on_trigger()));
767281c8
JH
232
233 _trigger_high = new QAction(QIcon(":/icons/trigger-high.svg"),
234 tr("Trigger on high level"), parent);
235 _trigger_high->setCheckable(true);
b1e8c93d 236 connect(_trigger_high, SIGNAL(triggered()), this, SLOT(on_trigger()));
767281c8
JH
237
238 _trigger_falling = new QAction(QIcon(":/icons/trigger-falling.svg"),
239 tr("Trigger on falling edge"), parent);
240 _trigger_falling->setCheckable(true);
b1e8c93d 241 connect(_trigger_falling, SIGNAL(triggered()), this, SLOT(on_trigger()));
767281c8
JH
242
243 _trigger_low = new QAction(QIcon(":/icons/trigger-low.svg"),
244 tr("Trigger on low level"), parent);
245 _trigger_low->setCheckable(true);
b1e8c93d 246 connect(_trigger_low, SIGNAL(triggered()), this, SLOT(on_trigger()));
767281c8
JH
247
248 _trigger_change = new QAction(QIcon(":/icons/trigger-change.svg"),
249 tr("Trigger on rising or falling edge"), parent);
250 _trigger_change->setCheckable(true);
b1e8c93d 251 connect(_trigger_change, SIGNAL(triggered()), this, SLOT(on_trigger()));
767281c8
JH
252}
253
b1e8c93d 254QAction* LogicSignal::match_action(int match)
767281c8 255{
b1e8c93d
BV
256 QAction *action;
257
258 action = _trigger_none;
259 switch (match) {
260 case SR_TRIGGER_ZERO:
261 action = _trigger_low;
262 break;
263 case SR_TRIGGER_ONE:
264 action = _trigger_high;
265 break;
266 case SR_TRIGGER_RISING:
267 action = _trigger_rising;
268 break;
269 case SR_TRIGGER_FALLING:
270 action = _trigger_falling;
271 break;
272 case SR_TRIGGER_EDGE:
273 action = _trigger_change;
274 break;
767281c8 275 }
767281c8 276
b1e8c93d 277 return action;
767281c8
JH
278}
279
b1e8c93d 280int LogicSignal::action_match(QAction *action)
139fef92 281{
b1e8c93d
BV
282 int match;
283
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;
294 else
295 match = 0;
296
297 return match;
139fef92
JH
298}
299
b1e8c93d 300void LogicSignal::populate_popup_form(QWidget *parent, QFormLayout *form)
b08d7222 301{
b1e8c93d
BV
302 GVariant *gvar;
303 gsize num_opts;
304 const int32_t *trig_matches;
305 unsigned int i;
306 bool is_checked;
19adbc2c 307
b1e8c93d 308 Signal::populate_popup_form(parent, form);
b08d7222 309
b1e8c93d
BV
310 if (!(gvar = _dev_inst->list_config(NULL, SR_CONF_TRIGGER_MATCH)))
311 return;
b08d7222 312
b1e8c93d
BV
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);
b08d7222 322 }
b1e8c93d
BV
323 form->addRow(tr("Trigger"), _trigger_bar);
324 g_variant_unref(gvar);
139fef92 325
b08d7222
JH
326}
327
b1e8c93d 328void LogicSignal::on_trigger()
b08d7222 329{
b1e8c93d 330 QAction *action;
b08d7222 331
b1e8c93d 332 match_action(_trigger_match)->setChecked(FALSE);
b08d7222 333
b1e8c93d
BV
334 action = (QAction *)sender();
335 action->setChecked(TRUE);
336 _trigger_match = action_match(action);
b08d7222 337
b08d7222
JH
338}
339
8d634081 340} // namespace view
51e77110 341} // namespace pv