]> sigrok.org Git - pulseview.git/blame_incremental - pv/view/analogsignal.cpp
Don't allow disabled probes to be selected
[pulseview.git] / pv / view / analogsignal.cpp
... / ...
CommitLineData
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"
26#include "pv/data/analog.h"
27#include "pv/data/analogsnapshot.h"
28#include "pv/view/view.h"
29
30using boost::shared_ptr;
31using std::max;
32using std::min;
33using std::deque;
34
35namespace pv {
36namespace view {
37
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
45const float AnalogSignal::EnvelopeThreshold = 256.0f;
46
47AnalogSignal::AnalogSignal(shared_ptr<pv::device::DevInst> dev_inst,
48 const sr_probe *const probe, shared_ptr<data::Analog> data) :
49 Signal(dev_inst, probe),
50 _data(data),
51 _scale(1.0f)
52{
53 _colour = SignalColours[probe->index % countof(SignalColours)];
54}
55
56AnalogSignal::~AnalogSignal()
57{
58}
59
60shared_ptr<pv::data::SignalData> AnalogSignal::data() const
61{
62 return _data;
63}
64
65shared_ptr<pv::data::Analog> AnalogSignal::analog_data() const
66{
67 return _data;
68}
69
70void AnalogSignal::set_scale(float scale)
71{
72 _scale = scale;
73}
74
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)
82{
83 assert(_data);
84 assert(right >= left);
85
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
94 if (!_probe->enabled)
95 return;
96
97 const deque< shared_ptr<pv::data::AnalogSnapshot> > &snapshots =
98 _data->get_snapshots();
99 if (snapshots.empty())
100 return;
101
102 const shared_ptr<pv::data::AnalogSnapshot> &snapshot =
103 snapshots.front();
104
105 const double pixels_offset = offset / scale;
106 const double samplerate = _data->samplerate();
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);
111 const double end = start + samples_per_pixel * (right - left);
112
113 const int64_t start_sample = min(max((int64_t)floor(start),
114 (int64_t)0), last_sample);
115 const int64_t end_sample = min(max((int64_t)ceil(end) + 1,
116 (int64_t)0), last_sample);
117
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);
136 assert(samples);
137
138 p.setPen(_colour);
139
140 QPointF *points = new QPointF[sample_count];
141 QPointF *point = points;
142
143 for (int64_t sample = start; sample != end; sample++) {
144 const float x = (sample / samples_per_pixel -
145 pixels_offset) + left;
146 *point++ = QPointF(x,
147 y - samples[sample - start] * _scale);
148 }
149
150 p.drawPolyline(points, point - points);
151
152 delete[] samples;
153 delete[] points;
154}
155
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{
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
169 p.setPen(QPen(Qt::NoPen));
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
201} // namespace view
202} // namespace pv