]>
Commit | Line | Data |
---|---|---|
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 <cassert> | |
24 | #include <cmath> | |
25 | #include <cstdlib> | |
26 | #include <limits> | |
27 | ||
28 | #include <QApplication> | |
29 | ||
30 | #include "analogsignal.hpp" | |
31 | #include "pv/data/analog.hpp" | |
32 | #include "pv/data/analogsegment.hpp" | |
33 | #include "pv/view/view.hpp" | |
34 | ||
35 | #include <libsigrokcxx/libsigrokcxx.hpp> | |
36 | ||
37 | using std::max; | |
38 | using std::make_pair; | |
39 | using std::min; | |
40 | using std::shared_ptr; | |
41 | using std::deque; | |
42 | ||
43 | using sigrok::Channel; | |
44 | ||
45 | namespace pv { | |
46 | namespace view { | |
47 | ||
48 | const 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 | ||
55 | const QColor AnalogSignal::GridMajorColor = QColor(0xB0, 0xB0, 0xB0); | |
56 | const QColor AnalogSignal::GridMinorColor = QColor(0xD0, 0xD0, 0xD0); | |
57 | ||
58 | const float AnalogSignal::EnvelopeThreshold = 256.0f; | |
59 | ||
60 | AnalogSignal::AnalogSignal( | |
61 | pv::Session &session, | |
62 | shared_ptr<Channel> channel, | |
63 | shared_ptr<data::Analog> data) : | |
64 | Signal(session, channel), | |
65 | data_(data), | |
66 | scale_index_(0), | |
67 | scale_index_drag_offset_(0), | |
68 | div_height_(3 * QFontMetrics(QApplication::font()).height()), | |
69 | vdivs_(1) | |
70 | { | |
71 | set_colour(SignalColours[channel_->index() % countof(SignalColours)]); | |
72 | } | |
73 | ||
74 | shared_ptr<pv::data::SignalData> AnalogSignal::data() const | |
75 | { | |
76 | return data_; | |
77 | } | |
78 | ||
79 | shared_ptr<pv::data::Analog> AnalogSignal::analog_data() const | |
80 | { | |
81 | return data_; | |
82 | } | |
83 | ||
84 | std::pair<int, int> AnalogSignal::v_extents() const | |
85 | { | |
86 | const int h = vdivs_ * div_height_; | |
87 | return make_pair(-h, h); | |
88 | } | |
89 | ||
90 | int AnalogSignal::scale_handle_offset() const | |
91 | { | |
92 | const int h = vdivs_ * div_height_; | |
93 | ||
94 | return ((scale_index_drag_offset_ - scale_index_) * | |
95 | h / 4) - h / 2; | |
96 | } | |
97 | ||
98 | void AnalogSignal::scale_handle_dragged(int offset) | |
99 | { | |
100 | const int h = vdivs_ * div_height_; | |
101 | ||
102 | scale_index_ = scale_index_drag_offset_ - | |
103 | (offset + h / 2) / (h / 4); | |
104 | } | |
105 | ||
106 | void AnalogSignal::scale_handle_drag_release() | |
107 | { | |
108 | scale_index_drag_offset_ = scale_index_; | |
109 | } | |
110 | ||
111 | void AnalogSignal::paint_back(QPainter &p, const ViewItemPaintParams &pp) | |
112 | { | |
113 | if (channel_->enabled()) { | |
114 | Trace::paint_back(p, pp); | |
115 | paint_axis(p, pp, get_visual_y()); | |
116 | } | |
117 | } | |
118 | ||
119 | void AnalogSignal::paint_mid(QPainter &p, const ViewItemPaintParams &pp) | |
120 | { | |
121 | assert(data_); | |
122 | assert(owner_); | |
123 | ||
124 | const int y = get_visual_y(); | |
125 | ||
126 | if (!channel_->enabled()) | |
127 | return; | |
128 | ||
129 | paint_grid(p, y, pp.left(), pp.right()); | |
130 | ||
131 | const deque< shared_ptr<pv::data::AnalogSegment> > &segments = | |
132 | data_->analog_segments(); | |
133 | if (segments.empty()) | |
134 | return; | |
135 | ||
136 | const shared_ptr<pv::data::AnalogSegment> &segment = | |
137 | segments.front(); | |
138 | ||
139 | const double pixels_offset = pp.pixels_offset(); | |
140 | const double samplerate = max(1.0, segment->samplerate()); | |
141 | const pv::util::Timestamp& start_time = segment->start_time(); | |
142 | const int64_t last_sample = segment->get_sample_count() - 1; | |
143 | const double samples_per_pixel = samplerate * pp.scale(); | |
144 | const pv::util::Timestamp start = samplerate * (pp.offset() - start_time); | |
145 | const pv::util::Timestamp end = start + samples_per_pixel * pp.width(); | |
146 | ||
147 | const int64_t start_sample = min(max(floor(start).convert_to<int64_t>(), | |
148 | (int64_t)0), last_sample); | |
149 | const int64_t end_sample = min(max((ceil(end) + 1).convert_to<int64_t>(), | |
150 | (int64_t)0), last_sample); | |
151 | ||
152 | if (samples_per_pixel < EnvelopeThreshold) | |
153 | paint_trace(p, segment, y, pp.left(), | |
154 | start_sample, end_sample, | |
155 | pixels_offset, samples_per_pixel); | |
156 | else | |
157 | paint_envelope(p, segment, y, pp.left(), | |
158 | start_sample, end_sample, | |
159 | pixels_offset, samples_per_pixel); | |
160 | } | |
161 | ||
162 | void 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 | ||
186 | void AnalogSignal::paint_trace(QPainter &p, | |
187 | const shared_ptr<pv::data::AnalogSegment> &segment, | |
188 | int y, int left, const int64_t start, const int64_t end, | |
189 | const double pixels_offset, const double samples_per_pixel) | |
190 | { | |
191 | const float scale = this->scale(); | |
192 | const int64_t sample_count = end - start; | |
193 | ||
194 | const float *const samples = segment->get_samples(start, end); | |
195 | assert(samples); | |
196 | ||
197 | p.setPen(colour_); | |
198 | ||
199 | QPointF *points = new QPointF[sample_count]; | |
200 | QPointF *point = points; | |
201 | ||
202 | for (int64_t sample = start; sample != end; sample++) { | |
203 | const float x = (sample / samples_per_pixel - | |
204 | pixels_offset) + left; | |
205 | *point++ = QPointF(x, | |
206 | y - samples[sample - start] * scale); | |
207 | } | |
208 | ||
209 | p.drawPolyline(points, point - points); | |
210 | ||
211 | delete[] samples; | |
212 | delete[] points; | |
213 | } | |
214 | ||
215 | void AnalogSignal::paint_envelope(QPainter &p, | |
216 | const shared_ptr<pv::data::AnalogSegment> &segment, | |
217 | int y, int left, const int64_t start, const int64_t end, | |
218 | const double pixels_offset, const double samples_per_pixel) | |
219 | { | |
220 | using pv::data::AnalogSegment; | |
221 | ||
222 | const float scale = this->scale(); | |
223 | ||
224 | AnalogSegment::EnvelopeSection e; | |
225 | segment->get_envelope_section(e, start, end, samples_per_pixel); | |
226 | ||
227 | if (e.length < 2) | |
228 | return; | |
229 | ||
230 | p.setPen(QPen(Qt::NoPen)); | |
231 | p.setBrush(colour_); | |
232 | ||
233 | QRectF *const rects = new QRectF[e.length]; | |
234 | QRectF *rect = rects; | |
235 | ||
236 | for (uint64_t sample = 0; sample < e.length-1; sample++) { | |
237 | const float x = ((e.scale * sample + e.start) / | |
238 | samples_per_pixel - pixels_offset) + left; | |
239 | const AnalogSegment::EnvelopeSample *const s = | |
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 | |
244 | const float b = y - max(s->max, (s+1)->min) * scale; | |
245 | const float t = y - min(s->min, (s+1)->max) * scale; | |
246 | ||
247 | float h = b - t; | |
248 | if (h >= 0.0f && h <= 1.0f) | |
249 | h = 1.0f; | |
250 | if (h <= 0.0f && h >= -1.0f) | |
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 | ||
262 | float 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 | ||
272 | } // namespace view | |
273 | } // namespace pv |