]> sigrok.org Git - pulseview.git/blame - pv/view/analogsignal.cpp
AnalogSignal: Use correct scaling factor for the grid to work
[pulseview.git] / pv / view / analogsignal.cpp
CommitLineData
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
SA
28#include <QApplication>
29
2acdb232
JH
30#include "analogsignal.hpp"
31#include "pv/data/analog.hpp"
f3d66e52 32#include "pv/data/analogsegment.hpp"
2acdb232 33#include "pv/view/view.hpp"
aba1dd16 34
fe3a1c21 35#include <libsigrokcxx/libsigrokcxx.hpp>
e8d00928 36
819f4c25 37using std::max;
a5d93c27 38using std::make_pair;
819f4c25 39using std::min;
f9abf97e 40using std::shared_ptr;
819f4c25 41using std::deque;
aba1dd16 42
e8d00928
ML
43using sigrok::Channel;
44
aba1dd16
JH
45namespace pv {
46namespace view {
47
568b90d4
JH
48const QColor AnalogSignal::SignalColours[4] = {
49 QColor(0xC4, 0xA0, 0x00), // Yellow
50 QColor(0x87, 0x20, 0x7A), // Magenta
51 QColor(0x20, 0x4A, 0x87), // Blue
52 QColor(0x4E, 0x9A, 0x06) // Green
53};
54
37b9fed4
SA
55const QColor AnalogSignal::GridMajorColor = QColor(0xB0, 0xB0, 0xB0);
56const QColor AnalogSignal::GridMinorColor = QColor(0xD0, 0xD0, 0xD0);
57
7c68ddae
JH
58const float AnalogSignal::EnvelopeThreshold = 256.0f;
59
b86aa8f4 60AnalogSignal::AnalogSignal(
2b81ae46 61 pv::Session &session,
b86aa8f4 62 shared_ptr<Channel> channel,
e8d00928 63 shared_ptr<data::Analog> data) :
b86aa8f4 64 Signal(session, channel),
8dbbc7f0 65 data_(data),
834a4f1b 66 scale_index_(4), // 20 per div
37b9fed4
SA
67 scale_index_drag_offset_(0),
68 div_height_(3 * QFontMetrics(QApplication::font()).height()),
834a4f1b
SA
69 vdivs_(1),
70 resolution_(0)
aba1dd16 71{
99af6802 72 set_colour(SignalColours[channel_->index() % countof(SignalColours)]);
834a4f1b 73 update_scale();
aba1dd16
JH
74}
75
3009b5b3
JH
76shared_ptr<pv::data::SignalData> AnalogSignal::data() const
77{
8dbbc7f0 78 return data_;
3009b5b3
JH
79}
80
81shared_ptr<pv::data::Analog> AnalogSignal::analog_data() const
9a0cd293 82{
8dbbc7f0 83 return data_;
9a0cd293
JH
84}
85
a5d93c27
JH
86std::pair<int, int> AnalogSignal::v_extents() const
87{
37b9fed4 88 const int h = vdivs_ * div_height_;
8a2fafcc 89 return make_pair(-h, h);
a5d93c27
JH
90}
91
214470fc
JH
92int AnalogSignal::scale_handle_offset() const
93{
37b9fed4
SA
94 const int h = vdivs_ * div_height_;
95
8a2fafcc 96 return ((scale_index_drag_offset_ - scale_index_) *
37b9fed4 97 h / 4) - h / 2;
214470fc
JH
98}
99
100void AnalogSignal::scale_handle_dragged(int offset)
101{
37b9fed4
SA
102 const int h = vdivs_ * div_height_;
103
8a2fafcc 104 scale_index_ = scale_index_drag_offset_ -
37b9fed4 105 (offset + h / 2) / (h / 4);
834a4f1b
SA
106
107 update_scale();
8a2fafcc
JH
108}
109
110void AnalogSignal::scale_handle_drag_release()
111{
112 scale_index_drag_offset_ = scale_index_;
834a4f1b 113 update_scale();
214470fc
JH
114}
115
5b5fa4da 116void AnalogSignal::paint_back(QPainter &p, const ViewItemPaintParams &pp)
fe08b6e8 117{
99af6802
SA
118 if (channel_->enabled()) {
119 Trace::paint_back(p, pp);
97904bf7 120 paint_axis(p, pp, get_visual_y());
99af6802 121 }
fe08b6e8
JH
122}
123
5b5fa4da 124void AnalogSignal::paint_mid(QPainter &p, const ViewItemPaintParams &pp)
aba1dd16 125{
8dbbc7f0 126 assert(data_);
8dbbc7f0 127 assert(owner_);
a8acb46e 128
be9e7b4b 129 const int y = get_visual_y();
01fd3263 130
8dbbc7f0 131 if (!channel_->enabled())
cec48d16
JH
132 return;
133
37b9fed4
SA
134 paint_grid(p, y, pp.left(), pp.right());
135
f3d66e52
JH
136 const deque< shared_ptr<pv::data::AnalogSegment> > &segments =
137 data_->analog_segments();
138 if (segments.empty())
a8acb46e
JH
139 return;
140
f3d66e52
JH
141 const shared_ptr<pv::data::AnalogSegment> &segment =
142 segments.front();
a8acb46e 143
4c8a6a6d 144 const double pixels_offset = pp.pixels_offset();
69e33a1b 145 const double samplerate = max(1.0, segment->samplerate());
60d9b99a 146 const pv::util::Timestamp& start_time = segment->start_time();
f3d66e52 147 const int64_t last_sample = segment->get_sample_count() - 1;
4c8a6a6d 148 const double samples_per_pixel = samplerate * pp.scale();
60d9b99a
JS
149 const pv::util::Timestamp start = samplerate * (pp.offset() - start_time);
150 const pv::util::Timestamp end = start + samples_per_pixel * pp.width();
a8acb46e 151
60d9b99a 152 const int64_t start_sample = min(max(floor(start).convert_to<int64_t>(),
a8acb46e 153 (int64_t)0), last_sample);
60d9b99a 154 const int64_t end_sample = min(max((ceil(end) + 1).convert_to<int64_t>(),
a8acb46e
JH
155 (int64_t)0), last_sample);
156
7c68ddae 157 if (samples_per_pixel < EnvelopeThreshold)
f3d66e52 158 paint_trace(p, segment, y, pp.left(),
7c68ddae
JH
159 start_sample, end_sample,
160 pixels_offset, samples_per_pixel);
161 else
f3d66e52 162 paint_envelope(p, segment, y, pp.left(),
7c68ddae
JH
163 start_sample, end_sample,
164 pixels_offset, samples_per_pixel);
165}
166
37b9fed4
SA
167void AnalogSignal::paint_grid(QPainter &p, int y, int left, int right)
168{
169 p.setPen(QPen(GridMajorColor, 0.5, Qt::DashLine));
170 for (int i = 1; i <= vdivs_; i++) {
171 const int dy = i * div_height_;
172 p.drawLine(QLineF(left, y - dy, right, y - dy));
173 p.drawLine(QLineF(left, y + dy, right, y + dy));
174 }
175
176 p.setPen(QPen(GridMinorColor, 0.5, Qt::DashLine));
177 for (int i = 0; i < vdivs_; i++) {
178 const int dy = i * div_height_;
179 const float dy25 = dy + (0.25 * div_height_);
180 const float dy50 = dy + (0.50 * div_height_);
181 const float dy75 = dy + (0.75 * div_height_);
182 p.drawLine(QLineF(left, y - dy25, right, y - dy25));
183 p.drawLine(QLineF(left, y + dy25, right, y + dy25));
184 p.drawLine(QLineF(left, y - dy50, right, y - dy50));
185 p.drawLine(QLineF(left, y + dy50, right, y + dy50));
186 p.drawLine(QLineF(left, y - dy75, right, y - dy75));
187 p.drawLine(QLineF(left, y + dy75, right, y + dy75));
188 }
189}
190
7c68ddae 191void AnalogSignal::paint_trace(QPainter &p,
f3d66e52 192 const shared_ptr<pv::data::AnalogSegment> &segment,
7c68ddae
JH
193 int y, int left, const int64_t start, const int64_t end,
194 const double pixels_offset, const double samples_per_pixel)
195{
196 const int64_t sample_count = end - start;
197
f3d66e52 198 const float *const samples = segment->get_samples(start, end);
a8acb46e
JH
199 assert(samples);
200
8dbbc7f0 201 p.setPen(colour_);
7c68ddae 202
d3758367 203 QPointF *points = new QPointF[sample_count];
a8acb46e
JH
204 QPointF *point = points;
205
7c68ddae 206 for (int64_t sample = start; sample != end; sample++) {
a8acb46e 207 const float x = (sample / samples_per_pixel -
2658961b 208 pixels_offset) + left;
d3758367 209 *point++ = QPointF(x,
834a4f1b 210 y - samples[sample - start] * scale_);
a8acb46e
JH
211 }
212
306d43a7 213 p.drawPolyline(points, point - points);
a8acb46e 214
7c68ddae 215 delete[] samples;
a8acb46e 216 delete[] points;
aba1dd16
JH
217}
218
7c68ddae 219void AnalogSignal::paint_envelope(QPainter &p,
f3d66e52 220 const shared_ptr<pv::data::AnalogSegment> &segment,
7c68ddae
JH
221 int y, int left, const int64_t start, const int64_t end,
222 const double pixels_offset, const double samples_per_pixel)
223{
f3d66e52 224 using pv::data::AnalogSegment;
7c68ddae 225
f3d66e52
JH
226 AnalogSegment::EnvelopeSection e;
227 segment->get_envelope_section(e, start, end, samples_per_pixel);
7c68ddae
JH
228
229 if (e.length < 2)
230 return;
231
819f4c25 232 p.setPen(QPen(Qt::NoPen));
8dbbc7f0 233 p.setBrush(colour_);
7c68ddae
JH
234
235 QRectF *const rects = new QRectF[e.length];
236 QRectF *rect = rects;
237
f3290553 238 for (uint64_t sample = 0; sample < e.length-1; sample++) {
7c68ddae
JH
239 const float x = ((e.scale * sample + e.start) /
240 samples_per_pixel - pixels_offset) + left;
f3d66e52 241 const AnalogSegment::EnvelopeSample *const s =
7c68ddae
JH
242 e.samples + sample;
243
244 // We overlap this sample with the next so that vertical
245 // gaps do not appear during steep rising or falling edges
834a4f1b
SA
246 const float b = y - max(s->max, (s+1)->min) * scale_;
247 const float t = y - min(s->min, (s+1)->max) * scale_;
7c68ddae
JH
248
249 float h = b - t;
f3290553 250 if (h >= 0.0f && h <= 1.0f)
7c68ddae 251 h = 1.0f;
f3290553 252 if (h <= 0.0f && h >= -1.0f)
7c68ddae
JH
253 h = -1.0f;
254
255 *rect++ = QRectF(x, t, 1.0f, h);
256 }
257
258 p.drawRects(rects, e.length);
259
260 delete[] rects;
261 delete[] e.samples;
262}
263
834a4f1b 264void AnalogSignal::update_scale()
8a2fafcc
JH
265{
266 const float seq[] = {1.0f, 2.0f, 5.0f};
834a4f1b 267
8a2fafcc
JH
268 const int offset = std::numeric_limits<int>::max() / (2 * countof(seq));
269 const std::div_t d = std::div(
270 (int)(scale_index_ + countof(seq) * offset),
271 countof(seq));
834a4f1b
SA
272
273 resolution_ = powf(10.0f, d.quot - offset) * seq[d.rem];
274 scale_ = div_height_ / resolution_;
8a2fafcc
JH
275}
276
aba1dd16
JH
277} // namespace view
278} // namespace pv