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