]>
Commit | Line | Data |
---|---|---|
aba1dd16 JH |
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 | ||
3b68d03d JH |
23 | #include <cassert> |
24 | #include <cmath> | |
8a2fafcc JH |
25 | #include <cstdlib> |
26 | #include <limits> | |
aba1dd16 | 27 | |
37b9fed4 | 28 | #include <QApplication> |
368a37c2 | 29 | #include <QComboBox> |
4cffac16 | 30 | #include <QFormLayout> |
368a37c2 SA |
31 | #include <QGridLayout> |
32 | #include <QLabel> | |
4cffac16 | 33 | #include <QSpinBox> |
37b9fed4 | 34 | |
2acdb232 JH |
35 | #include "analogsignal.hpp" |
36 | #include "pv/data/analog.hpp" | |
f3d66e52 | 37 | #include "pv/data/analogsegment.hpp" |
2acdb232 | 38 | #include "pv/view/view.hpp" |
aba1dd16 | 39 | |
fe3a1c21 | 40 | #include <libsigrokcxx/libsigrokcxx.hpp> |
e8d00928 | 41 | |
819f4c25 | 42 | using std::max; |
a5d93c27 | 43 | using std::make_pair; |
819f4c25 | 44 | using std::min; |
f9abf97e | 45 | using std::shared_ptr; |
819f4c25 | 46 | using std::deque; |
aba1dd16 | 47 | |
e8d00928 ML |
48 | using sigrok::Channel; |
49 | ||
aba1dd16 JH |
50 | namespace pv { |
51 | namespace view { | |
52 | ||
568b90d4 JH |
53 | const QColor AnalogSignal::SignalColours[4] = { |
54 | QColor(0xC4, 0xA0, 0x00), // Yellow | |
55 | QColor(0x87, 0x20, 0x7A), // Magenta | |
56 | QColor(0x20, 0x4A, 0x87), // Blue | |
57 | QColor(0x4E, 0x9A, 0x06) // Green | |
58 | }; | |
59 | ||
37b9fed4 SA |
60 | const QColor AnalogSignal::GridMajorColor = QColor(0xB0, 0xB0, 0xB0); |
61 | const QColor AnalogSignal::GridMinorColor = QColor(0xD0, 0xD0, 0xD0); | |
62 | ||
7c68ddae JH |
63 | const float AnalogSignal::EnvelopeThreshold = 256.0f; |
64 | ||
4cffac16 | 65 | const int AnalogSignal::MaximumVDivs = 10; |
368a37c2 SA |
66 | const int AnalogSignal::MinScaleIndex = -6; |
67 | const int AnalogSignal::MaxScaleIndex = 7; | |
4cffac16 | 68 | |
b86aa8f4 | 69 | AnalogSignal::AnalogSignal( |
2b81ae46 | 70 | pv::Session &session, |
b86aa8f4 | 71 | shared_ptr<Channel> channel, |
e8d00928 | 72 | shared_ptr<data::Analog> data) : |
b86aa8f4 | 73 | Signal(session, channel), |
8dbbc7f0 | 74 | data_(data), |
834a4f1b | 75 | scale_index_(4), // 20 per div |
37b9fed4 SA |
76 | scale_index_drag_offset_(0), |
77 | div_height_(3 * QFontMetrics(QApplication::font()).height()), | |
834a4f1b SA |
78 | vdivs_(1), |
79 | resolution_(0) | |
aba1dd16 | 80 | { |
99af6802 | 81 | set_colour(SignalColours[channel_->index() % countof(SignalColours)]); |
834a4f1b | 82 | update_scale(); |
aba1dd16 JH |
83 | } |
84 | ||
3009b5b3 JH |
85 | shared_ptr<pv::data::SignalData> AnalogSignal::data() const |
86 | { | |
8dbbc7f0 | 87 | return data_; |
3009b5b3 JH |
88 | } |
89 | ||
90 | shared_ptr<pv::data::Analog> AnalogSignal::analog_data() const | |
9a0cd293 | 91 | { |
8dbbc7f0 | 92 | return data_; |
9a0cd293 JH |
93 | } |
94 | ||
a5d93c27 JH |
95 | std::pair<int, int> AnalogSignal::v_extents() const |
96 | { | |
37b9fed4 | 97 | const int h = vdivs_ * div_height_; |
8a2fafcc | 98 | return make_pair(-h, h); |
a5d93c27 JH |
99 | } |
100 | ||
214470fc JH |
101 | int AnalogSignal::scale_handle_offset() const |
102 | { | |
37b9fed4 SA |
103 | const int h = vdivs_ * div_height_; |
104 | ||
8a2fafcc | 105 | return ((scale_index_drag_offset_ - scale_index_) * |
37b9fed4 | 106 | h / 4) - h / 2; |
214470fc JH |
107 | } |
108 | ||
109 | void AnalogSignal::scale_handle_dragged(int offset) | |
110 | { | |
37b9fed4 SA |
111 | const int h = vdivs_ * div_height_; |
112 | ||
8a2fafcc | 113 | scale_index_ = scale_index_drag_offset_ - |
37b9fed4 | 114 | (offset + h / 2) / (h / 4); |
834a4f1b SA |
115 | |
116 | update_scale(); | |
8a2fafcc JH |
117 | } |
118 | ||
119 | void AnalogSignal::scale_handle_drag_release() | |
120 | { | |
121 | scale_index_drag_offset_ = scale_index_; | |
834a4f1b | 122 | update_scale(); |
214470fc JH |
123 | } |
124 | ||
5b5fa4da | 125 | void AnalogSignal::paint_back(QPainter &p, const ViewItemPaintParams &pp) |
fe08b6e8 | 126 | { |
99af6802 SA |
127 | if (channel_->enabled()) { |
128 | Trace::paint_back(p, pp); | |
97904bf7 | 129 | paint_axis(p, pp, get_visual_y()); |
99af6802 | 130 | } |
fe08b6e8 JH |
131 | } |
132 | ||
5b5fa4da | 133 | void AnalogSignal::paint_mid(QPainter &p, const ViewItemPaintParams &pp) |
aba1dd16 | 134 | { |
8dbbc7f0 | 135 | assert(data_); |
8dbbc7f0 | 136 | assert(owner_); |
a8acb46e | 137 | |
be9e7b4b | 138 | const int y = get_visual_y(); |
01fd3263 | 139 | |
8dbbc7f0 | 140 | if (!channel_->enabled()) |
cec48d16 JH |
141 | return; |
142 | ||
37b9fed4 SA |
143 | paint_grid(p, y, pp.left(), pp.right()); |
144 | ||
f3d66e52 JH |
145 | const deque< shared_ptr<pv::data::AnalogSegment> > &segments = |
146 | data_->analog_segments(); | |
147 | if (segments.empty()) | |
a8acb46e JH |
148 | return; |
149 | ||
f3d66e52 JH |
150 | const shared_ptr<pv::data::AnalogSegment> &segment = |
151 | segments.front(); | |
a8acb46e | 152 | |
4c8a6a6d | 153 | const double pixels_offset = pp.pixels_offset(); |
69e33a1b | 154 | const double samplerate = max(1.0, segment->samplerate()); |
60d9b99a | 155 | const pv::util::Timestamp& start_time = segment->start_time(); |
f3d66e52 | 156 | const int64_t last_sample = segment->get_sample_count() - 1; |
4c8a6a6d | 157 | const double samples_per_pixel = samplerate * pp.scale(); |
60d9b99a JS |
158 | const pv::util::Timestamp start = samplerate * (pp.offset() - start_time); |
159 | const pv::util::Timestamp end = start + samples_per_pixel * pp.width(); | |
a8acb46e | 160 | |
60d9b99a | 161 | const int64_t start_sample = min(max(floor(start).convert_to<int64_t>(), |
a8acb46e | 162 | (int64_t)0), last_sample); |
60d9b99a | 163 | const int64_t end_sample = min(max((ceil(end) + 1).convert_to<int64_t>(), |
a8acb46e JH |
164 | (int64_t)0), last_sample); |
165 | ||
7c68ddae | 166 | if (samples_per_pixel < EnvelopeThreshold) |
f3d66e52 | 167 | paint_trace(p, segment, y, pp.left(), |
7c68ddae JH |
168 | start_sample, end_sample, |
169 | pixels_offset, samples_per_pixel); | |
170 | else | |
f3d66e52 | 171 | paint_envelope(p, segment, y, pp.left(), |
7c68ddae JH |
172 | start_sample, end_sample, |
173 | pixels_offset, samples_per_pixel); | |
174 | } | |
175 | ||
37b9fed4 SA |
176 | void AnalogSignal::paint_grid(QPainter &p, int y, int left, int right) |
177 | { | |
178 | p.setPen(QPen(GridMajorColor, 0.5, Qt::DashLine)); | |
179 | for (int i = 1; i <= vdivs_; i++) { | |
180 | const int dy = i * div_height_; | |
181 | p.drawLine(QLineF(left, y - dy, right, y - dy)); | |
182 | p.drawLine(QLineF(left, y + dy, right, y + dy)); | |
183 | } | |
184 | ||
185 | p.setPen(QPen(GridMinorColor, 0.5, Qt::DashLine)); | |
186 | for (int i = 0; i < vdivs_; i++) { | |
187 | const int dy = i * div_height_; | |
188 | const float dy25 = dy + (0.25 * div_height_); | |
189 | const float dy50 = dy + (0.50 * div_height_); | |
190 | const float dy75 = dy + (0.75 * div_height_); | |
191 | p.drawLine(QLineF(left, y - dy25, right, y - dy25)); | |
192 | p.drawLine(QLineF(left, y + dy25, right, y + dy25)); | |
193 | p.drawLine(QLineF(left, y - dy50, right, y - dy50)); | |
194 | p.drawLine(QLineF(left, y + dy50, right, y + dy50)); | |
195 | p.drawLine(QLineF(left, y - dy75, right, y - dy75)); | |
196 | p.drawLine(QLineF(left, y + dy75, right, y + dy75)); | |
197 | } | |
198 | } | |
199 | ||
7c68ddae | 200 | void AnalogSignal::paint_trace(QPainter &p, |
f3d66e52 | 201 | const shared_ptr<pv::data::AnalogSegment> &segment, |
7c68ddae JH |
202 | int y, int left, const int64_t start, const int64_t end, |
203 | const double pixels_offset, const double samples_per_pixel) | |
204 | { | |
205 | const int64_t sample_count = end - start; | |
206 | ||
f3d66e52 | 207 | const float *const samples = segment->get_samples(start, end); |
a8acb46e JH |
208 | assert(samples); |
209 | ||
8dbbc7f0 | 210 | p.setPen(colour_); |
7c68ddae | 211 | |
d3758367 | 212 | QPointF *points = new QPointF[sample_count]; |
a8acb46e JH |
213 | QPointF *point = points; |
214 | ||
7c68ddae | 215 | for (int64_t sample = start; sample != end; sample++) { |
a8acb46e | 216 | const float x = (sample / samples_per_pixel - |
2658961b | 217 | pixels_offset) + left; |
d3758367 | 218 | *point++ = QPointF(x, |
834a4f1b | 219 | y - samples[sample - start] * scale_); |
a8acb46e JH |
220 | } |
221 | ||
306d43a7 | 222 | p.drawPolyline(points, point - points); |
a8acb46e | 223 | |
7c68ddae | 224 | delete[] samples; |
a8acb46e | 225 | delete[] points; |
aba1dd16 JH |
226 | } |
227 | ||
7c68ddae | 228 | void AnalogSignal::paint_envelope(QPainter &p, |
f3d66e52 | 229 | const shared_ptr<pv::data::AnalogSegment> &segment, |
7c68ddae JH |
230 | int y, int left, const int64_t start, const int64_t end, |
231 | const double pixels_offset, const double samples_per_pixel) | |
232 | { | |
f3d66e52 | 233 | using pv::data::AnalogSegment; |
7c68ddae | 234 | |
f3d66e52 JH |
235 | AnalogSegment::EnvelopeSection e; |
236 | segment->get_envelope_section(e, start, end, samples_per_pixel); | |
7c68ddae JH |
237 | |
238 | if (e.length < 2) | |
239 | return; | |
240 | ||
819f4c25 | 241 | p.setPen(QPen(Qt::NoPen)); |
8dbbc7f0 | 242 | p.setBrush(colour_); |
7c68ddae JH |
243 | |
244 | QRectF *const rects = new QRectF[e.length]; | |
245 | QRectF *rect = rects; | |
246 | ||
f3290553 | 247 | for (uint64_t sample = 0; sample < e.length-1; sample++) { |
7c68ddae JH |
248 | const float x = ((e.scale * sample + e.start) / |
249 | samples_per_pixel - pixels_offset) + left; | |
f3d66e52 | 250 | const AnalogSegment::EnvelopeSample *const s = |
7c68ddae JH |
251 | e.samples + sample; |
252 | ||
253 | // We overlap this sample with the next so that vertical | |
254 | // gaps do not appear during steep rising or falling edges | |
834a4f1b SA |
255 | const float b = y - max(s->max, (s+1)->min) * scale_; |
256 | const float t = y - min(s->min, (s+1)->max) * scale_; | |
7c68ddae JH |
257 | |
258 | float h = b - t; | |
f3290553 | 259 | if (h >= 0.0f && h <= 1.0f) |
7c68ddae | 260 | h = 1.0f; |
f3290553 | 261 | if (h <= 0.0f && h >= -1.0f) |
7c68ddae JH |
262 | h = -1.0f; |
263 | ||
264 | *rect++ = QRectF(x, t, 1.0f, h); | |
265 | } | |
266 | ||
267 | p.drawRects(rects, e.length); | |
268 | ||
269 | delete[] rects; | |
270 | delete[] e.samples; | |
271 | } | |
272 | ||
368a37c2 | 273 | float AnalogSignal::get_resolution(int scale_index) |
8a2fafcc JH |
274 | { |
275 | const float seq[] = {1.0f, 2.0f, 5.0f}; | |
834a4f1b | 276 | |
8a2fafcc JH |
277 | const int offset = std::numeric_limits<int>::max() / (2 * countof(seq)); |
278 | const std::div_t d = std::div( | |
368a37c2 | 279 | (int)(scale_index + countof(seq) * offset), |
8a2fafcc | 280 | countof(seq)); |
834a4f1b | 281 | |
368a37c2 SA |
282 | return powf(10.0f, d.quot - offset) * seq[d.rem]; |
283 | } | |
284 | ||
285 | void AnalogSignal::update_scale() | |
286 | { | |
287 | resolution_ = get_resolution(scale_index_); | |
834a4f1b | 288 | scale_ = div_height_ / resolution_; |
8a2fafcc JH |
289 | } |
290 | ||
4cffac16 SA |
291 | void AnalogSignal::populate_popup_form(QWidget *parent, QFormLayout *form) |
292 | { | |
293 | // Add the standard options | |
294 | Signal::populate_popup_form(parent, form); | |
295 | ||
368a37c2 SA |
296 | QFormLayout *const layout = new QFormLayout; |
297 | ||
298 | // Add the number of vdivs | |
4cffac16 SA |
299 | QSpinBox *vdiv_sb = new QSpinBox(parent); |
300 | vdiv_sb->setRange(1, MaximumVDivs); | |
301 | vdiv_sb->setValue(vdivs_); | |
302 | connect(vdiv_sb, SIGNAL(valueChanged(int)), | |
303 | this, SLOT(on_vdivs_changed(int))); | |
368a37c2 SA |
304 | layout->addRow(tr("Number of vertical divs"), vdiv_sb); |
305 | ||
306 | // Add the vertical resolution | |
307 | resolution_cb_ = new QComboBox(parent); | |
308 | ||
309 | for (int i = MinScaleIndex; i < MaxScaleIndex; i++) { | |
310 | const QString label = QString("%1").arg(get_resolution(i)); | |
311 | resolution_cb_->insertItem(0, label, QVariant(i)); | |
312 | } | |
313 | ||
314 | const int cur_idx = resolution_cb_->findData(QVariant(scale_index_)); | |
315 | resolution_cb_->setCurrentIndex(cur_idx); | |
316 | ||
317 | connect(resolution_cb_, SIGNAL(currentIndexChanged(int)), | |
318 | this, SLOT(on_resolution_changed(int))); | |
319 | ||
320 | QGridLayout *const vdiv_layout = new QGridLayout; | |
321 | QLabel *const vdiv_unit = new QLabel(tr("V/div")); | |
322 | vdiv_layout->addWidget(resolution_cb_, 0, 0); | |
323 | vdiv_layout->addWidget(vdiv_unit, 0, 1); | |
324 | ||
325 | layout->addRow(tr("Vertical resolution"), vdiv_layout); | |
326 | ||
327 | form->addRow(layout); | |
4cffac16 SA |
328 | } |
329 | ||
330 | void AnalogSignal::on_vdivs_changed(int vdivs) | |
331 | { | |
332 | vdivs_ = vdivs; | |
333 | ||
334 | if (owner_) | |
335 | owner_->extents_changed(false, true); | |
336 | } | |
337 | ||
368a37c2 SA |
338 | void AnalogSignal::on_resolution_changed(int index) |
339 | { | |
340 | scale_index_ = resolution_cb_->itemData(index).toInt(); | |
341 | update_scale(); | |
342 | ||
343 | if (owner_) | |
344 | owner_->row_item_appearance_changed(false, true); | |
345 | } | |
4cffac16 | 346 | |
aba1dd16 JH |
347 | } // namespace view |
348 | } // namespace pv |