]> sigrok.org Git - pulseview.git/blob - pv/view/logicsignal.cpp
e214ec78426c505d621808fb38e9e5f34dc65462
[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/data/logic.h>
34 #include <pv/data/logicsnapshot.h>
35 #include <pv/view/view.h>
36
37 #include <libsigrok/libsigrok.hpp>
38
39 using std::deque;
40 using std::max;
41 using std::min;
42 using std::pair;
43 using std::shared_ptr;
44 using std::vector;
45
46 using sigrok::Channel;
47 using sigrok::ConfigKey;
48 using sigrok::Device;
49 using sigrok::Error;
50 using sigrok::Trigger;
51 using sigrok::TriggerMatchType;
52
53 namespace pv {
54 namespace view {
55
56 const float LogicSignal::Oversampling = 2.0f;
57
58 const QColor LogicSignal::EdgeColour(0x80, 0x80, 0x80);
59 const QColor LogicSignal::HighColour(0x00, 0xC0, 0x00);
60 const QColor LogicSignal::LowColour(0xC0, 0x00, 0x00);
61
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
73 };
74
75 LogicSignal::LogicSignal(
76         pv::SigSession &session,
77         shared_ptr<Device> device,
78         shared_ptr<Channel> channel,
79         shared_ptr<data::Logic> data) :
80         Signal(session, channel),
81         _device(device),
82         _data(data),
83         _trigger_none(NULL),
84         _trigger_rising(NULL),
85         _trigger_high(NULL),
86         _trigger_falling(NULL),
87         _trigger_low(NULL),
88         _trigger_change(NULL)
89 {
90         shared_ptr<Trigger> trigger;
91
92         _colour = SignalColours[channel->index() % countof(SignalColours)];
93
94         /* Populate this channel's trigger setting with whatever we
95          * find in the current session trigger, if anything. */
96         _trigger_match = nullptr;
97         if ((trigger = SigSession::_sr_session->trigger()))
98                 for (auto stage : trigger->stages())
99                         for (auto match : stage->matches())
100                                 if (match->channel() == _channel)
101                                         _trigger_match = match->type();
102 }
103
104 LogicSignal::~LogicSignal()
105 {
106 }
107
108 shared_ptr<pv::data::SignalData> LogicSignal::data() const
109 {
110         return _data;
111 }
112
113 shared_ptr<pv::data::Logic> LogicSignal::logic_data() const
114 {
115         return _data;
116 }
117
118 void LogicSignal::paint_back(QPainter &p, int left, int right)
119 {
120         if (_channel->enabled())
121                 paint_axis(p, get_y(), left, right);
122 }
123
124 void LogicSignal::paint_mid(QPainter &p, int left, int right)
125 {
126         using pv::view::View;
127
128         QLineF *line;
129
130         vector< pair<int64_t, bool> > edges;
131
132         assert(_channel);
133         assert(_data);
134         assert(right >= left);
135
136         assert(_view);
137         const int y = _v_offset - _view->v_offset();
138
139         const double scale = _view->scale();
140         assert(scale > 0);
141
142         const double offset = _view->offset();
143
144         if (!_channel->enabled())
145                 return;
146
147         const float high_offset = y - View::SignalHeight + 0.5f;
148         const float low_offset = y + 0.5f;
149
150         const deque< shared_ptr<pv::data::LogicSnapshot> > &snapshots =
151                 _data->get_snapshots();
152         if (snapshots.empty())
153                 return;
154
155         const shared_ptr<pv::data::LogicSnapshot> &snapshot =
156                 snapshots.front();
157
158         double samplerate = _data->samplerate();
159
160         // Show sample rate as 1Hz when it is unknown
161         if (samplerate == 0.0)
162                 samplerate = 1.0;
163
164         const double pixels_offset = offset / scale;
165         const double start_time = _data->get_start_time();
166         const int64_t last_sample = snapshot->get_sample_count() - 1;
167         const double samples_per_pixel = samplerate * scale;
168         const double start = samplerate * (offset - start_time);
169         const double end = start + samples_per_pixel * (right - left);
170
171         snapshot->get_subsampled_edges(edges,
172                 min(max((int64_t)floor(start), (int64_t)0), last_sample),
173                 min(max((int64_t)ceil(end), (int64_t)0), last_sample),
174                 samples_per_pixel / Oversampling, _channel->index());
175         assert(edges.size() >= 2);
176
177         // Paint the edges
178         const unsigned int edge_count = edges.size() - 2;
179         QLineF *const edge_lines = new QLineF[edge_count];
180         line = edge_lines;
181
182         for (auto i = edges.cbegin() + 1; i != edges.cend() - 1; i++) {
183                 const float x = ((*i).first / samples_per_pixel -
184                         pixels_offset) + left;
185                 *line++ = QLineF(x, high_offset, x, low_offset);
186         }
187
188         p.setPen(EdgeColour);
189         p.drawLines(edge_lines, edge_count);
190         delete[] edge_lines;
191
192         // Paint the caps
193         const unsigned int max_cap_line_count = edges.size();
194         QLineF *const cap_lines = new QLineF[max_cap_line_count];
195
196         p.setPen(HighColour);
197         paint_caps(p, cap_lines, edges, true, samples_per_pixel,
198                 pixels_offset, left, high_offset);
199         p.setPen(LowColour);
200         paint_caps(p, cap_lines, edges, false, samples_per_pixel,
201                 pixels_offset, left, low_offset);
202
203         delete[] cap_lines;
204 }
205
206 void LogicSignal::paint_caps(QPainter &p, QLineF *const lines,
207         vector< pair<int64_t, bool> > &edges, bool level,
208         double samples_per_pixel, double pixels_offset, float x_offset,
209         float y_offset)
210 {
211         QLineF *line = lines;
212
213         for (auto i = edges.begin(); i != (edges.end() - 1); i++)
214                 if ((*i).second == level) {
215                         *line++ = QLineF(
216                                 ((*i).first / samples_per_pixel -
217                                         pixels_offset) + x_offset, y_offset,
218                                 ((*(i+1)).first / samples_per_pixel -
219                                         pixels_offset) + x_offset, y_offset);
220                 }
221
222         p.drawLines(lines, line - lines);
223 }
224
225 void LogicSignal::init_trigger_actions(QWidget *parent)
226 {
227         _trigger_none = new QAction(QIcon(":/icons/trigger-none.svg"),
228                 tr("No trigger"), parent);
229         _trigger_none->setCheckable(true);
230         connect(_trigger_none, SIGNAL(triggered()), this, SLOT(on_trigger()));
231
232         _trigger_rising = new QAction(QIcon(":/icons/trigger-rising.svg"),
233                 tr("Trigger on rising edge"), parent);
234         _trigger_rising->setCheckable(true);
235         connect(_trigger_rising, SIGNAL(triggered()), this, SLOT(on_trigger()));
236
237         _trigger_high = new QAction(QIcon(":/icons/trigger-high.svg"),
238                 tr("Trigger on high level"), parent);
239         _trigger_high->setCheckable(true);
240         connect(_trigger_high, SIGNAL(triggered()), this, SLOT(on_trigger()));
241
242         _trigger_falling = new QAction(QIcon(":/icons/trigger-falling.svg"),
243                 tr("Trigger on falling edge"), parent);
244         _trigger_falling->setCheckable(true);
245         connect(_trigger_falling, SIGNAL(triggered()), this, SLOT(on_trigger()));
246
247         _trigger_low = new QAction(QIcon(":/icons/trigger-low.svg"),
248                 tr("Trigger on low level"), parent);
249         _trigger_low->setCheckable(true);
250         connect(_trigger_low, SIGNAL(triggered()), this, SLOT(on_trigger()));
251
252         _trigger_change = new QAction(QIcon(":/icons/trigger-change.svg"),
253                 tr("Trigger on rising or falling edge"), parent);
254         _trigger_change->setCheckable(true);
255         connect(_trigger_change, SIGNAL(triggered()), this, SLOT(on_trigger()));
256 }
257
258 QAction* LogicSignal::match_action(const TriggerMatchType *type)
259 {
260         QAction *action;
261
262         action = _trigger_none;
263         switch (type->id()) {
264         case SR_TRIGGER_ZERO:
265                 action = _trigger_low;
266                 break;
267         case SR_TRIGGER_ONE:
268                 action = _trigger_high;
269                 break;
270         case SR_TRIGGER_RISING:
271                 action = _trigger_rising;
272                 break;
273         case SR_TRIGGER_FALLING:
274                 action = _trigger_falling;
275                 break;
276         case SR_TRIGGER_EDGE:
277                 action = _trigger_change;
278                 break;
279         default:
280                 assert(0);
281         }
282
283         return action;
284 }
285
286 const TriggerMatchType *LogicSignal::action_match(QAction *action)
287 {
288         if (action == _trigger_low)
289                 return TriggerMatchType::ZERO;
290         else if (action == _trigger_high)
291                 return TriggerMatchType::ONE;
292         else if (action == _trigger_rising)
293                 return TriggerMatchType::RISING;
294         else if (action == _trigger_falling)
295                 return TriggerMatchType::FALLING;
296         else if (action == _trigger_change)
297                 return TriggerMatchType::EDGE;
298         else
299                 return nullptr;
300 }
301
302 void LogicSignal::populate_popup_form(QWidget *parent, QFormLayout *form)
303 {
304         Glib::VariantContainerBase gvar;
305         vector<int32_t> trig_types;
306
307         Signal::populate_popup_form(parent, form);
308
309         try {
310                 gvar = _device->config_list(ConfigKey::TRIGGER_MATCH);
311         } catch (Error e) {
312                 return;
313         }
314
315         _trigger_bar = new QToolBar(parent);
316         init_trigger_actions(_trigger_bar);
317         _trigger_bar->addAction(_trigger_none);
318         trig_types =
319                 Glib::VariantBase::cast_dynamic<Glib::Variant<vector<int32_t>>>(
320                         gvar).get();
321         for (auto type_id : trig_types) {
322                 auto type = TriggerMatchType::get(type_id);
323                 _trigger_bar->addAction(match_action(type));
324                 match_action(type)->setChecked(_trigger_match == type);
325         }
326         form->addRow(tr("Trigger"), _trigger_bar);
327
328 }
329
330 void LogicSignal::on_trigger()
331 {
332         QAction *action;
333
334         match_action(_trigger_match)->setChecked(false);
335
336         action = (QAction *)sender();
337         action->setChecked(true);
338         _trigger_match = action_match(action);
339
340 }
341
342 } // namespace view
343 } // namespace pv