]> sigrok.org Git - pulseview.git/blame - pv/view/analogsignal.cpp
Added AnalogSignal::analog_data accessor function
[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
23#include <math.h>
24
25#include "analogsignal.h"
1b1ec774
JH
26#include "pv/data/analog.h"
27#include "pv/data/analogsnapshot.h"
01fd3263 28#include "pv/view/view.h"
aba1dd16 29
819f4c25
JH
30using boost::shared_ptr;
31using std::max;
32using std::min;
33using std::deque;
aba1dd16
JH
34
35namespace pv {
36namespace view {
37
568b90d4
JH
38const QColor AnalogSignal::SignalColours[4] = {
39 QColor(0xC4, 0xA0, 0x00), // Yellow
40 QColor(0x87, 0x20, 0x7A), // Magenta
41 QColor(0x20, 0x4A, 0x87), // Blue
42 QColor(0x4E, 0x9A, 0x06) // Green
43};
44
7c68ddae
JH
45const float AnalogSignal::EnvelopeThreshold = 256.0f;
46
aca00b1e 47AnalogSignal::AnalogSignal(pv::SigSession &session, sr_probe *const probe,
cec48d16 48 shared_ptr<data::Analog> data) :
c0f86852 49 Signal(session, probe),
785fd8d1
JH
50 _data(data),
51 _scale(1.0f)
aba1dd16 52{
cec48d16 53 _colour = SignalColours[probe->index % countof(SignalColours)];
aba1dd16
JH
54}
55
f459c540
JH
56AnalogSignal::~AnalogSignal()
57{
58}
59
3009b5b3
JH
60shared_ptr<pv::data::SignalData> AnalogSignal::data() const
61{
62 return _data;
63}
64
65shared_ptr<pv::data::Analog> AnalogSignal::analog_data() const
9a0cd293
JH
66{
67 return _data;
68}
69
785fd8d1
JH
70void AnalogSignal::set_scale(float scale)
71{
72 _scale = scale;
73}
74
fe08b6e8
JH
75void AnalogSignal::paint_back(QPainter &p, int left, int right)
76{
77 if (_probe->enabled)
78 paint_axis(p, get_y(), left, right);
79}
80
81void AnalogSignal::paint_mid(QPainter &p, int left, int right)
aba1dd16 82{
a8acb46e 83 assert(_data);
2658961b 84 assert(right >= left);
a8acb46e 85
01fd3263
JH
86 assert(_view);
87 const int y = _v_offset - _view->v_offset();
88
89 const double scale = _view->scale();
90 assert(scale > 0);
91
92 const double offset = _view->offset();
93
cec48d16
JH
94 if (!_probe->enabled)
95 return;
96
1b1ec774 97 const deque< shared_ptr<pv::data::AnalogSnapshot> > &snapshots =
a8acb46e
JH
98 _data->get_snapshots();
99 if (snapshots.empty())
100 return;
101
1b1ec774 102 const shared_ptr<pv::data::AnalogSnapshot> &snapshot =
a8acb46e
JH
103 snapshots.front();
104
105 const double pixels_offset = offset / scale;
9d28da5a 106 const double samplerate = _data->samplerate();
a8acb46e
JH
107 const double start_time = _data->get_start_time();
108 const int64_t last_sample = snapshot->get_sample_count() - 1;
109 const double samples_per_pixel = samplerate * scale;
110 const double start = samplerate * (offset - start_time);
2658961b 111 const double end = start + samples_per_pixel * (right - left);
a8acb46e
JH
112
113 const int64_t start_sample = min(max((int64_t)floor(start),
114 (int64_t)0), last_sample);
306d43a7 115 const int64_t end_sample = min(max((int64_t)ceil(end) + 1,
a8acb46e
JH
116 (int64_t)0), last_sample);
117
7c68ddae
JH
118 if (samples_per_pixel < EnvelopeThreshold)
119 paint_trace(p, snapshot, y, left,
120 start_sample, end_sample,
121 pixels_offset, samples_per_pixel);
122 else
123 paint_envelope(p, snapshot, y, left,
124 start_sample, end_sample,
125 pixels_offset, samples_per_pixel);
126}
127
128void AnalogSignal::paint_trace(QPainter &p,
129 const shared_ptr<pv::data::AnalogSnapshot> &snapshot,
130 int y, int left, const int64_t start, const int64_t end,
131 const double pixels_offset, const double samples_per_pixel)
132{
133 const int64_t sample_count = end - start;
134
135 const float *const samples = snapshot->get_samples(start, end);
a8acb46e
JH
136 assert(samples);
137
7c68ddae
JH
138 p.setPen(_colour);
139
d3758367 140 QPointF *points = new QPointF[sample_count];
a8acb46e
JH
141 QPointF *point = points;
142
7c68ddae 143 for (int64_t sample = start; sample != end; sample++) {
a8acb46e 144 const float x = (sample / samples_per_pixel -
2658961b 145 pixels_offset) + left;
d3758367 146 *point++ = QPointF(x,
7c68ddae 147 y - samples[sample - start] * _scale);
a8acb46e
JH
148 }
149
306d43a7 150 p.drawPolyline(points, point - points);
a8acb46e 151
7c68ddae 152 delete[] samples;
a8acb46e 153 delete[] points;
aba1dd16
JH
154}
155
7c68ddae
JH
156void AnalogSignal::paint_envelope(QPainter &p,
157 const shared_ptr<pv::data::AnalogSnapshot> &snapshot,
158 int y, int left, const int64_t start, const int64_t end,
159 const double pixels_offset, const double samples_per_pixel)
160{
7c68ddae
JH
161 using pv::data::AnalogSnapshot;
162
163 AnalogSnapshot::EnvelopeSection e;
164 snapshot->get_envelope_section(e, start, end, samples_per_pixel);
165
166 if (e.length < 2)
167 return;
168
819f4c25 169 p.setPen(QPen(Qt::NoPen));
7c68ddae
JH
170 p.setBrush(_colour);
171
172 QRectF *const rects = new QRectF[e.length];
173 QRectF *rect = rects;
174
175 for(uint64_t sample = 0; sample < e.length-1; sample++) {
176 const float x = ((e.scale * sample + e.start) /
177 samples_per_pixel - pixels_offset) + left;
178 const AnalogSnapshot::EnvelopeSample *const s =
179 e.samples + sample;
180
181 // We overlap this sample with the next so that vertical
182 // gaps do not appear during steep rising or falling edges
183 const float b = y - max(s->max, (s+1)->min) * _scale;
184 const float t = y - min(s->min, (s+1)->max) * _scale;
185
186 float h = b - t;
187 if(h >= 0.0f && h <= 1.0f)
188 h = 1.0f;
189 if(h <= 0.0f && h >= -1.0f)
190 h = -1.0f;
191
192 *rect++ = QRectF(x, t, 1.0f, h);
193 }
194
195 p.drawRects(rects, e.length);
196
197 delete[] rects;
198 delete[] e.samples;
199}
200
aba1dd16
JH
201} // namespace view
202} // namespace pv