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