view: Minor whitespace fixes.
[pulseview.git] / pv / view / logicsignal.cpp
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 <QFormLayout>
27 #include <QToolBar>
28
29 #include "logicsignal.h"
30 #include "view.h"
31
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>
37
38 using std::deque;
39 using std::max;
40 using std::min;
41 using std::pair;
42 using std::shared_ptr;
43 using std::vector;
44
45 namespace pv {
46 namespace view {
47
48 const float LogicSignal::Oversampling = 2.0f;
49
50 const QColor LogicSignal::EdgeColour(0x80, 0x80, 0x80);
51 const QColor LogicSignal::HighColour(0x00, 0xC0, 0x00);
52 const QColor LogicSignal::LowColour(0xC0, 0x00, 0x00);
53
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
65 };
66
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),
70         _data(data),
71         _trigger_none(NULL),
72         _trigger_rising(NULL),
73         _trigger_high(NULL),
74         _trigger_falling(NULL),
75         _trigger_low(NULL),
76         _trigger_change(NULL)
77 {
78         struct sr_trigger *trigger;
79         struct sr_trigger_stage *stage;
80         struct sr_trigger_match *match;
81         const GSList *l, *m;
82
83         _colour = SignalColours[probe->index % countof(SignalColours)];
84
85         /* Populate this channel's trigger setting with whatever we
86          * find in the current session trigger, if anything. */
87         _trigger_match = 0;
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;
95                         }
96                 }
97         }
98 }
99
100 LogicSignal::~LogicSignal()
101 {
102 }
103
104 shared_ptr<pv::data::SignalData> LogicSignal::data() const
105 {
106         return _data;
107 }
108
109 shared_ptr<pv::data::Logic> LogicSignal::logic_data() const
110 {
111         return _data;
112 }
113
114 void LogicSignal::paint_back(QPainter &p, int left, int right)
115 {
116         if (_probe->enabled)
117                 paint_axis(p, get_y(), left, right);
118 }
119
120 void LogicSignal::paint_mid(QPainter &p, int left, int right)
121 {
122         using pv::view::View;
123
124         QLineF *line;
125
126         vector< pair<int64_t, bool> > edges;
127
128         assert(_probe);
129         assert(_data);
130         assert(right >= left);
131
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
140         if (!_probe->enabled)
141                 return;
142
143         const float high_offset = y - View::SignalHeight + 0.5f;
144         const float low_offset = y + 0.5f;
145
146         const deque< shared_ptr<pv::data::LogicSnapshot> > &snapshots =
147                 _data->get_snapshots();
148         if (snapshots.empty())
149                 return;
150
151         const shared_ptr<pv::data::LogicSnapshot> &snapshot =
152                 snapshots.front();
153
154         double samplerate = _data->samplerate();
155
156         // Show sample rate as 1Hz when it is unknown
157         if (samplerate == 0.0)
158                 samplerate = 1.0;
159
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);
166
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);
172
173         // Paint the edges
174         const unsigned int edge_count = edges.size() - 2;
175         QLineF *const edge_lines = new QLineF[edge_count];
176         line = edge_lines;
177
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);
182         }
183
184         p.setPen(EdgeColour);
185         p.drawLines(edge_lines, edge_count);
186         delete[] edge_lines;
187
188         // Paint the caps
189         const unsigned int max_cap_line_count = edges.size();
190         QLineF *const cap_lines = new QLineF[max_cap_line_count];
191
192         p.setPen(HighColour);
193         paint_caps(p, cap_lines, edges, true, samples_per_pixel,
194                 pixels_offset, left, high_offset);
195         p.setPen(LowColour);
196         paint_caps(p, cap_lines, edges, false, samples_per_pixel,
197                 pixels_offset, left, low_offset);
198
199         delete[] cap_lines;
200 }
201
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,
205         float y_offset)
206 {
207         QLineF *line = lines;
208
209         for (auto i = edges.begin(); i != (edges.end() - 1); i++)
210                 if ((*i).second == level) {
211                         *line++ = QLineF(
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);
216                 }
217
218         p.drawLines(lines, line - lines);
219 }
220
221 void 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);
226         connect(_trigger_none, SIGNAL(triggered()), this, SLOT(on_trigger()));
227
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()));
232
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()));
237
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()));
242
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()));
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);
251         connect(_trigger_change, SIGNAL(triggered()), this, SLOT(on_trigger()));
252 }
253
254 QAction* LogicSignal::match_action(int match)
255 {
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;
275         }
276
277         return action;
278 }
279
280 int LogicSignal::action_match(QAction *action)
281 {
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;
298 }
299
300 void LogicSignal::populate_popup_form(QWidget *parent, QFormLayout *form)
301 {
302         GVariant *gvar;
303         gsize num_opts;
304         const int32_t *trig_matches;
305         unsigned int i;
306         bool is_checked;
307
308         Signal::populate_popup_form(parent, form);
309
310         if (!(gvar = _dev_inst->list_config(NULL, SR_CONF_TRIGGER_MATCH)))
311                 return;
312
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);
322         }
323         form->addRow(tr("Trigger"), _trigger_bar);
324         g_variant_unref(gvar);
325
326 }
327
328 void LogicSignal::on_trigger()
329 {
330         QAction *action;
331
332         match_action(_trigger_match)->setChecked(FALSE);
333
334         action = (QAction *)sender();
335         action->setChecked(TRUE);
336         _trigger_match = action_match(action);
337
338 }
339
340 } // namespace view
341 } // namespace pv