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